2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/parse/SEXP.CPP $
15 * main sexpression generator
18 * Revision 1.3 2002/06/09 04:41:25 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:48 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 66 9/13/99 11:38a Andsager
29 * Stupid switch for sexp_team_score. Don't forget *break*
31 * 65 9/09/99 11:40p Dave
32 * Handle an Assert() in beam code. Added supernova sounds. Play the right
33 * 2 end movies properly, based upon what the player did in the mission.
35 * 64 9/07/99 1:05a Andsager
36 * Added team-score sexp for multi team vs team missions
38 * 63 9/06/99 9:46p Jefff
39 * skip mission support
41 * 62 8/29/99 2:33p Andsager
42 * Fix bug in sexp_is_ship_visible()
44 * 61 8/27/99 9:07p Dave
45 * LOD explosions. Improved beam weapon accuracy.
47 * 60 8/27/99 4:07p Andsager
48 * Add is-ship-visible sexp. Make ship-vanish sexp SINGLE player only
50 * 59 8/26/99 4:21p Mikek
51 * Add new special-check for JimB, zeroing front shield quadrant.
53 * 58 8/24/99 4:25p Andsager
54 * Add ship-vanish sexp
56 * 57 8/23/99 6:05p Johnson
57 * Oops. Self destruct log should also go into the is-destroyed
60 * 56 8/23/99 3:23p Johnson
61 * Fix bug in sexp_facing2 when Player_obj does not yet existl
63 * 55 8/19/99 3:30p Andsager
64 * Make sexp_special_warp_dist check if ship if facing knossos device
66 * 54 8/19/99 9:20a Andsager
67 * Enable flashing for all guages
69 * 53 8/19/99 12:31a Andsager
70 * Add support guage as one that can flash
72 * 52 8/18/99 12:09p Andsager
73 * Add debug if message has no anim for message. Make messages come from
76 * 51 8/16/99 10:04p Andsager
77 * Add special-warp-dist and special-warpout-name sexp for Knossos device
80 * 50 8/09/99 3:32p Andsager
81 * Make has_time_elapsed work with arithmatic args.
83 * 49 8/09/99 2:00p Dave
86 * 48 8/02/99 4:26p Dave
87 * Added 2 new sexpressions.
89 * 47 7/31/99 2:30p Dave
90 * Added nifty mission message debug viewing keys.
92 * 46 7/28/99 1:36p Andsager
93 * Modify cargo1 to include flag CARGO_NO_DEPLETE. Add sexp
94 * cargo-no-deplete (only for BIG / HUGE). Modify ship struct to pack
97 * 45 7/24/99 4:56p Dave
98 * Added 3 new sexpressions.
100 * 44 7/21/99 8:10p Dave
101 * First run of supernova effect.
103 * 43 7/20/99 9:19p Andsager
104 * Added facing waypoint sexp
106 * 42 7/20/99 9:54a Andsager
107 * Add subsys-set-random sexp
109 * 41 7/19/99 12:02p Andsager
110 * Allow AWACS on any ship subsystem. Fix sexp_set_subsystem_strength to
111 * only blow up subsystem if its strength is > 0
113 * 40 7/15/99 9:22p Andsager
114 * modify secondaries_depleted sexp to report true only when all
117 * 39 7/13/99 3:37p Andsager
118 * Add secondaries-depleted sexp
120 * 38 7/08/99 12:06p Andsager
121 * Add turret-tagged-only and turret-tagged-clear sexp.
123 * 37 6/28/99 4:51p Andsager
124 * Add ship-guardian sexp (does not allow ship to be killed)
126 * 36 6/25/99 2:51p Andsager
127 * Make event_delay, goal_delay, and subsys strength work with sexp_rand
129 * 35 6/23/99 5:51p Andsager
130 * Add waypoint-cap-speed. Checkin stealth ai - inactive.
132 * 34 6/16/99 10:21a Dave
133 * Added send-message-list sexpression.
135 * 33 6/15/99 2:53p Andsager
136 * Fixed subsystem checking for AWACS type subsystems.
138 * 32 6/01/99 8:35p Dave
139 * Finished lockarm weapons. Added proper supercap weapons/damage. Added
140 * awacs-set-radius sexpression.
142 * 31 5/27/99 12:14p Andsager
143 * Some fixes for live debris when more than one subsys on ship with live
144 * debris. Set subsys strength (when 0) blows off subsystem.
145 * sexp_hits_left_subsystem works for SUBSYSTEM_UNKNOWN.
147 * 30 5/24/99 11:28a Dave
148 * Sexpression for adding/removing ships from the hud escort list.
150 * 29 4/28/99 9:33a Andsager
151 * Add turret-free and turret-lock (and -all) sexp. Stargger start time
152 * of beam weapons beam-free and beam-free-all.
154 * 28 4/26/99 2:14p Andsager
155 * Add beam-protect-ship and beam-unprotect-ship sexp.
157 * 27 4/23/99 12:01p Johnson
158 * Added SIF_HUGE_SHIP
160 * 26 4/23/99 9:45a Andsager
161 * Modify rand_sexp to return the same value each time it is called on a
162 * particular node. This prevents random time delay defaulting to min.
164 * 25 4/21/99 6:15p Dave
165 * Did some serious housecleaning in the beam code. Made it ready to go
166 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
167 * a handy macro for recalculating collision pairs for a given object.
169 * 24 4/19/99 2:28p Johnson
170 * DA: Use ship_info flags, not ship_flags for ship type. DOH!!
172 * 23 4/19/99 11:46a Johnson
175 * 22 4/19/99 11:36a Johnson
176 * Don't display multiple warning messges when large ship gets more than 1
179 * 21 4/02/99 9:55a Dave
180 * Added a few more options in the weapons.tbl for beam weapons. Attempt
181 * at putting "pain" packets into multiplayer.
183 * 20 3/31/99 2:22p Johnson
184 * Fix sexp_repair_subsystem() to accept sexp increments
186 * 19 3/28/99 5:58p Dave
187 * Added early demo code. Make objects move. Nice and framerate
188 * independant, but not much else. Don't use yet unless you're me :)
190 * 18 3/20/99 3:46p Dave
191 * Added support for model-based background nebulae. Added 3 new
194 * 17 3/08/99 7:03p Dave
195 * First run of new object update system. Looks very promising.
197 * 16 3/04/99 6:09p Dave
198 * Added in sexpressions for firing beams and checking for if a ship is
201 * 15 3/04/99 9:22a Andsager
202 * Make escort list work with ship-is-visible. When not visible, dump,
203 * when becoming visible, maybe add.
205 * 14 2/26/99 6:01p Andsager
206 * Add sexp has-been-tagged-delay and cap-subsys-cargo-known-delay
208 * 13 2/26/99 4:14p Dave
209 * Put in the ability to have multiple shockwaves for ships.
211 * 12 2/11/99 5:22p Andsager
212 * Fixed bugs, generalized block Sexp_variables
214 * 11 2/11/99 2:15p Andsager
215 * Add ship explosion modification to FRED
217 * 10 1/25/99 4:29p Andsager
218 * Modify sexp_modify_variable() to handle type string. added quick out
219 * for multiplayer_client
221 * 9 1/25/99 8:11a Andsager
222 * Add sexp_modify_variable(). Changed syntax checking to allow, adding
223 * operator return type ambiguous
225 * 8 1/24/99 11:37p Dave
226 * First full rev of beam weapons. Very customizable. Removed some bogus
227 * Int3()'s in low level net code.
229 * 7 1/20/99 9:37a Andsager
231 * 6 1/19/99 3:57p Andsager
232 * Round 2 of variables
234 * 5 1/07/99 1:52p Andsager
235 * Initial check in of Sexp_variables
237 * 4 11/05/98 5:55p Dave
238 * Big pass at reducing #includes
240 * 3 10/13/98 9:29a Dave
241 * Started neatening up freespace.h. Many variables renamed and
242 * reorganized. Added AlphaColors.[h,cpp]
244 * 2 10/07/98 10:53a Dave
247 * 1 10/07/98 10:50a Dave
249 * 321 9/20/98 7:20p Dave
250 * Added CHANGE_IFF packet.
252 * 320 9/16/98 10:42a Hoffoss
253 * Added sexp node counting to fsm files for end user designers.
255 * 319 7/06/98 4:31p Hoffoss
256 * Fixed bug in sexp parsing when formula is ( ).
258 * 318 6/09/98 5:15p Lawrance
259 * French/German localization
261 * 317 5/26/98 11:54a Allender
262 * fix multiplayer problems and sexpression crash
264 * 316 5/23/98 3:16p Allender
265 * work on object update packet optimizations (a new updating system).
266 * Don't allow medals/promotions/badges when playing singple player
267 * missions through the simulator
269 * 315 5/20/98 1:34a Allender
270 * fixed observer problems. Removed some code offending BoundsCheccker.
271 * Fixed medals for multiplayer
273 * 314 5/17/98 12:46p Mike
274 * Change special-check functions for cueing rearm suggestion.
276 * 313 5/14/98 6:29p Hoffoss
277 * Fixed some warnings a release rebuild all turned up.
279 * 312 5/14/98 10:15a Allender
280 * add optional argument to prevous-goal/event operators to specify what
281 * sexpression should return when being played as a single mission
283 * 311 5/08/98 10:16a Lawrance
284 * Add new "ship attacking count" gauge
286 * 310 4/29/98 11:39p Allender
287 * fixed nasty problem with is-event-false always returning true right
293 // Parse a symbolic expression.
294 // These are identical to Lisp functions.
295 // It uses a very baggy format, allocating 16 characters per token, regardless
296 // of how many are used.
308 #include "freespace.h"
311 #include "missionlog.h"
312 #include "missionparse.h" // for p_object definition
313 #include "missionmessage.h"
314 #include "missiontraining.h"
316 #include "linklist.h"
317 #include "model.h" // for subsystem types
319 #include "missioncampaign.h"
320 #include "missiongoals.h"
321 #include "controlsconfig.h"
324 #include "gamesequence.h"
328 #include "hudmessage.h"
330 #include "redalert.h"
331 #include "jumpnode.h"
332 #include "hudshield.h"
334 #include "multimsgs.h"
335 #include "multiutil.h"
336 #include "hudescort.h"
338 #include "supernova.h"
342 #include "multi_team.h"
347 sexp_oper Operators[] = {
348 // Operator, Identity, Min / Max arguments
349 { "+", OP_PLUS, 2, INT_MAX },
350 { "-", OP_MINUS, 2, INT_MAX },
351 { "*", OP_MUL, 2, INT_MAX },
352 { "/", OP_DIV, 2, INT_MAX },
353 { "mod", OP_MOD, 2, INT_MAX },
354 { "rand", OP_RAND, 2, 2 },
356 { "true", OP_TRUE, 0, 0, },
357 { "false", OP_FALSE, 0, 0, },
358 { "and", OP_AND, 2, INT_MAX, },
359 { "and-in-sequence", OP_AND_IN_SEQUENCE, 2, INT_MAX, },
360 { "or", OP_OR, 2, INT_MAX, },
361 { "not", OP_NOT, 1, 1, },
362 { "=", OP_EQUALS, 2, INT_MAX, },
363 { ">", OP_GREATER_THAN, 2, 2, },
364 { "<", OP_LESS_THAN, 2, 2, },
365 { "is-iff", OP_IS_IFF, 2, INT_MAX, },
366 { "has-time-elapsed", OP_HAS_TIME_ELAPSED, 1, 1, },
367 { "modify-variable", OP_MODIFY_VARIABLE, 2, 2, },
369 { "is-goal-incomplete", OP_GOAL_INCOMPLETE, 1, 1, },
370 { "is-goal-true-delay", OP_GOAL_TRUE_DELAY, 2, 2, },
371 { "is-goal-false-delay", OP_GOAL_FALSE_DELAY, 2, 2, },
372 { "is-event-incomplete", OP_EVENT_INCOMPLETE, 1, 1, },
373 { "is-event-true-delay", OP_EVENT_TRUE_DELAY, 2, 2, },
374 { "is-event-false-delay", OP_EVENT_FALSE_DELAY, 2, 2, },
375 { "is-previous-goal-incomplete", OP_PREVIOUS_GOAL_INCOMPLETE, 2, 3, },
376 { "is-previous-goal-true", OP_PREVIOUS_GOAL_TRUE, 2, 3, },
377 { "is-previous-goal-false", OP_PREVIOUS_GOAL_FALSE, 2, 3, },
378 { "is-previous-event-incomplete", OP_PREVIOUS_EVENT_INCOMPLETE, 2, 3, },
379 { "is-previous-event-true", OP_PREVIOUS_EVENT_TRUE, 2, 3, },
380 { "is-previous-event-false", OP_PREVIOUS_EVENT_FALSE, 2, 3, },
382 { "is-destroyed", OP_IS_DESTROYED, 1, INT_MAX, },
383 { "is-subsystem-destroyed", OP_IS_SUBSYSTEM_DESTROYED, 2, 2, },
384 { "is-disabled", OP_IS_DISABLED, 1, INT_MAX, },
385 { "is-disarmed", OP_IS_DISARMED, 1, INT_MAX, },
386 { "has-docked", OP_HAS_DOCKED, 3, 3, },
387 { "has-undocked", OP_HAS_UNDOCKED, 3, 3, },
388 { "has-arrived", OP_HAS_ARRIVED, 1, INT_MAX, },
389 { "has-departed", OP_HAS_DEPARTED, 1, INT_MAX, },
390 { "is-destroyed-delay", OP_IS_DESTROYED_DELAY, 2, INT_MAX, },
391 { "is-subsystem-destroyed-delay", OP_IS_SUBSYSTEM_DESTROYED_DELAY, 3, 3, },
392 { "is-disabled-delay", OP_IS_DISABLED_DELAY, 2, INT_MAX, },
393 { "is-disarmed-delay", OP_IS_DISARMED_DELAY, 2, INT_MAX, },
394 { "has-docked-delay", OP_HAS_DOCKED_DELAY, 4, 4, },
395 { "has-undocked-delay", OP_HAS_UNDOCKED_DELAY, 4, 4, },
396 { "has-arrived-delay", OP_HAS_ARRIVED_DELAY, 2, INT_MAX, },
397 { "has-departed-delay", OP_HAS_DEPARTED_DELAY, 2, INT_MAX, },
398 { "are-waypoints-done", OP_WAYPOINTS_DONE, 2, 2, },
399 { "are-waypoints-done-delay", OP_WAYPOINTS_DONE_DELAY, 3, 3, },
400 { "ship-type-destroyed", OP_SHIP_TYPE_DESTROYED, 2, 2, },
401 { "is-event-true", OP_EVENT_TRUE, 1, 1, },
402 { "is-event-false", OP_EVENT_FALSE, 1, 1, },
403 { "is-cargo-known", OP_IS_CARGO_KNOWN, 1, INT_MAX, },
404 { "was-promotion-granted", OP_WAS_PROMOTION_GRANTED, 0, 1, },
405 { "was-medal-granted", OP_WAS_MEDAL_GRANTED, 0, 1, },
406 { "percent-ships-departed", OP_PERCENT_SHIPS_DEPARTED, 2, INT_MAX },
407 { "percent-ships-destroyed", OP_PERCENT_SHIPS_DESTROYED, 2, INT_MAX },
408 { "is-cargo-known-delay", OP_CARGO_KNOWN_DELAY, 2, INT_MAX, },
409 { "cap-subsys-cargo-known-delay", OP_CAP_SUBSYS_CARGO_KNOWN_DELAY, 3, INT_MAX, },
410 { "has-been-tagged-delay", OP_HAS_BEEN_TAGGED_DELAY, 2, INT_MAX, },
411 { "depart-node-delay", OP_DEPART_NODE_DELAY, 3, INT_MAX, },
412 { "destroyed-or-departed-delay", OP_DESTROYED_DEPARTED_DELAY, 2, INT_MAX, },
413 { "is_tagged", OP_IS_TAGGED, 1, 1 },
414 { "num_kills", OP_NUM_KILLS, 1, 1 },
415 { "num_type_kills", OP_NUM_TYPE_KILLS, 2, 2 },
416 { "num_class_kills", OP_NUM_CLASS_KILLS, 2, 2 },
417 { "shield-recharge-pct", OP_SHIELD_RECHARGE_PCT, 1, 1 },
418 { "engine-recharge-pct", OP_ENGINE_RECHARGE_PCT, 1, 1 },
419 { "weapon-recharge-pct", OP_WEAPON_RECHARGE_PCT, 1, 1 },
420 { "shield-quad-low", OP_SHIELD_QUAD_LOW, 2, 2 },
421 { "secondary-ammo-pct", OP_SECONDARY_AMMO_PCT, 2, 2 },
422 { "is-secondary-selected", OP_IS_SECONDARY_SELECTED, 2, 2 },
423 { "is-primary-selected", OP_IS_PRIMARY_SELECTED, 2, 2 },
425 { "time-ship-destroyed", OP_TIME_SHIP_DESTROYED, 1, 1, },
426 { "time-ship-arrived", OP_TIME_SHIP_ARRIVED, 1, 1, },
427 { "time-ship-departed", OP_TIME_SHIP_DEPARTED, 1, 1, },
428 { "time-wing-destroyed", OP_TIME_WING_DESTROYED, 1, 1, },
429 { "time-wing-arrived", OP_TIME_WING_ARRIVED, 1, 1, },
430 { "time-wing-departed", OP_TIME_WING_DEPARTED, 1, 1, },
431 { "mission-time", OP_MISSION_TIME, 0, 0, },
432 { "time-docked", OP_TIME_DOCKED, 3, 3, },
433 { "time-undocked", OP_TIME_UNDOCKED, 3, 3, },
435 { "shields-left", OP_SHIELDS_LEFT, 1, 1, },
436 { "hits-left", OP_HITS_LEFT, 1, 1, },
437 { "hits-left-subsystem", OP_HITS_LEFT_SUBSYSTEM, 2, 2, },
438 { "distance", OP_DISTANCE, 2, 2, },
439 { "is-ship-visible", OP_IS_SHIP_VISIBLE, 1, 1, },
440 { "team-score", OP_TEAM_SCORE, 1, 1, },
441 { "time-elapsed-last-order", OP_LAST_ORDER_TIME, 2, 2, /*INT_MAX*/ },
442 { "skill-level-at-least", OP_SKILL_LEVEL_AT_LEAST, 1, 1, },
443 { "num-players", OP_NUM_PLAYERS, 0, 0, },
444 { "special-warp-dist", OP_SPECIAL_WARP_DISTANCE, 1, 1, },
446 { "special-warpout-name", OP_SET_SPECIAL_WARPOUT_NAME, 2, 2 },
448 { "do-nothing", OP_NOP, 0, 0, },
449 { "when", OP_WHEN, 2, INT_MAX, },
450 { "cond", OP_COND, 1, INT_MAX, },
452 { "add-goal", OP_ADD_GOAL, 2, 2, },
453 { "add-ship-goal", OP_ADD_SHIP_GOAL, 2, 2, },
454 { "add-wing-goal", OP_ADD_WING_GOAL, 2, 2, },
455 { "clear-goals", OP_CLEAR_GOALS, 1, INT_MAX, },
456 { "clear-ship-goals", OP_CLEAR_SHIP_GOALS, 1, 1, },
457 { "clear-wing-goals", OP_CLEAR_WING_GOALS, 1, 1, },
458 { "change-iff", OP_CHANGE_IFF, 2, INT_MAX, },
459 { "protect-ship", OP_PROTECT_SHIP, 1, INT_MAX, },
460 { "unprotect-ship", OP_UNPROTECT_SHIP, 1, INT_MAX, },
461 { "beam-protect-ship", OP_BEAM_PROTECT_SHIP, 1, INT_MAX, },
462 { "beam-unprotect-ship", OP_BEAM_UNPROTECT_SHIP, 1, INT_MAX, },
463 { "send-message", OP_SEND_MESSAGE, 3, 3, },
464 { "self-destruct", OP_SELF_DESTRUCT, 1, INT_MAX, },
465 { "next-mission", OP_NEXT_MISSION, 1, 1, },
466 { "end-campaign", OP_END_CAMPAIGN, 0, 0, },
467 { "end-of-campaign", OP_END_OF_CAMPAIGN, 0, 0, },
468 { "sabotage-subsystem", OP_SABOTAGE_SUBSYSTEM, 3, 3, },
469 { "repair-subsystem", OP_REPAIR_SUBSYSTEM, 3, 3, },
470 { "set-subsystem-strength", OP_SET_SUBSYSTEM_STRNGTH, 3, 3, },
471 { "invalidate-goal", OP_INVALIDATE_GOAL, 1, INT_MAX, },
472 { "validate-goal", OP_VALIDATE_GOAL, 1, INT_MAX, },
473 { "send-random-message", OP_SEND_RANDOM_MESSAGE, 3, INT_MAX, },
474 { "transfer-cargo", OP_TRANSFER_CARGO, 2, 2, },
475 { "exchange-cargo", OP_EXCHANGE_CARGO, 2, 2, },
476 { "end-misison-delay", OP_END_MISSION_DELAY, 1, 1, },
477 { "good-rearm-time", OP_GOOD_REARM_TIME, 2, 2, },
478 { "grant-promotion", OP_GRANT_PROMOTION, 0, 0, },
479 { "grant-medal", OP_GRANT_MEDAL, 1, 1, },
480 { "allow-ship", OP_ALLOW_SHIP, 1, 1, },
481 { "allow-weapon", OP_ALLOW_WEAPON, 1, 1, },
482 { "break-warp", OP_WARP_BROKEN, 1, INT_MAX, },
483 { "fix-warp", OP_WARP_NOT_BROKEN, 1, INT_MAX, },
484 { "never-warp", OP_WARP_NEVER, 1, INT_MAX, },
485 { "allow-warp", OP_WARP_ALLOWED, 1, INT_MAX, },
486 { "good-secondary-time", OP_GOOD_SECONDARY_TIME, 4, 4, },
487 { "ship-invisible", OP_SHIP_INVISIBLE, 1, INT_MAX },
488 { "ship-visible", OP_SHIP_VISIBLE, 1, INT_MAX },
489 { "ship-invulnerable", OP_SHIP_INVULNERABLE, 1, INT_MAX },
490 { "ship-vulnerable", OP_SHIP_VULNERABLE, 1, INT_MAX },
491 { "red-alert", OP_RED_ALERT, 0, 0 },
492 { "tech-add-ships", OP_TECH_ADD_SHIP, 1, INT_MAX },
493 { "tech-add-weapons", OP_TECH_ADD_WEAPON, 1, INT_MAX },
494 { "jettison-cargo-delay", OP_JETTISON_CARGO, 2, 2 },
495 { "fire-beam", OP_BEAM_FIRE, 3, 4 },
496 { "beam-free", OP_BEAM_FREE, 2, INT_MAX },
497 { "beam-free-all", OP_BEAM_FREE_ALL, 1, 1 },
498 { "beam-lock", OP_BEAM_LOCK, 2, INT_MAX },
499 { "beam-lock-all", OP_BEAM_LOCK_ALL, 1, 1 },
500 { "turret-free", OP_TURRET_FREE, 2, INT_MAX },
501 { "turret-free-all", OP_TURRET_FREE_ALL, 1, 1 },
502 { "turret-lock", OP_TURRET_LOCK, 2, INT_MAX },
503 { "turret-lock-all", OP_TURRET_LOCK_ALL, 1, 1 },
504 { "add-remove-escort", OP_ADD_REMOVE_ESCORT, 2, 2 },
505 { "awacs-set-radius", OP_AWACS_SET_RADIUS, 3, 3 },
506 { "send-message-list", OP_SEND_MESSAGE_LIST, 4, INT_MAX },
507 { "cap-waypoint-speed", OP_CAP_WAYPOINT_SPEED, 2, 2 },
508 { "ship-guardian", OP_SHIP_GUARDIAN, 1, INT_MAX },
509 { "ship-no-guardian", OP_SHIP_NO_GUARDIAN, 1, INT_MAX },
510 { "ship-vanish", OP_SHIP_VANISH, 1, INT_MAX },
511 { "turret-tagged-only", OP_TURRET_TAGGED_ONLY_ALL, 1, 1 },
512 { "turret-tagged-clear", OP_TURRET_TAGGED_CLEAR_ALL, 1, 1 },
513 { "subsys-set-random", OP_SUBSYS_SET_RANDOM, 3, INT_MAX },
514 { "supernova-start", OP_SUPERNOVA_START, 1, 1 },
515 { "cargo-no-deplete", OP_CARGO_NO_DEPLETE, 1, 2 },
517 { "error", OP_INT3, 0, 0 },
519 { "ai-chase", OP_AI_CHASE, 2, 2, },
520 { "ai-chase-wing", OP_AI_CHASE_WING, 2, 2, },
521 { "ai-dock", OP_AI_DOCK, 4, 4, },
522 { "ai-undock", OP_AI_UNDOCK, 1, 1, },
523 { "ai-warp", OP_AI_WARP, 2, 2, },
524 { "ai-warp-out", OP_AI_WARP_OUT, 1, 1, },
525 { "ai-waypoints", OP_AI_WAYPOINTS, 2, 2, },
526 { "ai-waypoints-once", OP_AI_WAYPOINTS_ONCE, 2, 2, },
527 { "ai-destroy-subsystem", OP_AI_DESTROY_SUBSYS, 3, 3, },
528 { "ai-disable-ship", OP_AI_DISABLE_SHIP, 2, 2, },
529 { "ai-disarm-ship", OP_AI_DISARM_SHIP, 2, 2, },
530 { "ai-guard", OP_AI_GUARD, 2, 2, },
531 { "ai-chase-any", OP_AI_CHASE_ANY, 1, 1, },
532 { "ai-guard-wing", OP_AI_GUARD_WING, 2, 2, },
533 { "ai-evade-ship", OP_AI_EVADE_SHIP, 2, 2, },
534 { "ai-stay-near-ship", OP_AI_STAY_NEAR_SHIP, 2, 2, },
535 { "ai-keep-safe-distance", OP_AI_KEEP_SAFE_DISTANCE, 1, 1, },
536 { "ai-ignore", OP_AI_IGNORE, 2, 2, },
537 { "ai-stay-still", OP_AI_STAY_STILL, 2, 2, },
538 { "ai-play-dead", OP_AI_PLAY_DEAD, 1, 1, },
540 { "goals", OP_GOALS_ID, 1, INT_MAX, },
542 { "key-pressed", OP_KEY_PRESSED, 1, 2, },
543 { "key-reset", OP_KEY_RESET, 1, INT_MAX, },
544 { "targeted", OP_TARGETED, 1, 3, },
545 { "speed", OP_SPEED, 1, 1, },
546 { "facing", OP_FACING, 2, 2, },
547 { "facing-waypoint", OP_FACING2, 2, 2, },
548 { "order", OP_ORDER, 2, 2, },
549 { "waypoint-missed", OP_WAYPOINT_MISSED, 0, 0, },
550 { "waypoint-twice", OP_WAYPOINT_TWICE, 0, 0, },
551 { "path-flown", OP_PATH_FLOWN, 0, 0, },
552 { "training-msg", OP_TRAINING_MSG, 1, 4, },
553 { "flash-hud-gauge", OP_FLASH_HUD_GAUGE, 1, 1, },
554 { "special-check", OP_SPECIAL_CHECK, 1, 1, },
555 { "secondaries-depleted", OP_SECONDARIES_DEPLETED, 1, 1, },
557 { "set-training-context-fly-path", OP_SET_TRAINING_CONTEXT_FLY_PATH, 2, 2, },
558 { "set-training-context-speed", OP_SET_TRAINING_CONTEXT_SPEED, 2, 2, },
561 sexp_ai_goal_link Sexp_ai_goal_links[] = {
562 { AI_GOAL_CHASE, OP_AI_CHASE },
563 { AI_GOAL_CHASE_WING, OP_AI_CHASE_WING },
564 { AI_GOAL_DOCK, OP_AI_DOCK },
565 { AI_GOAL_UNDOCK, OP_AI_UNDOCK },
566 { AI_GOAL_WARP, OP_AI_WARP_OUT },
567 { AI_GOAL_WARP, OP_AI_WARP },
568 { AI_GOAL_WAYPOINTS, OP_AI_WAYPOINTS },
569 { AI_GOAL_WAYPOINTS_ONCE, OP_AI_WAYPOINTS_ONCE },
570 { AI_GOAL_DESTROY_SUBSYSTEM, OP_AI_DESTROY_SUBSYS },
571 { AI_GOAL_DISABLE_SHIP, OP_AI_DISABLE_SHIP },
572 { AI_GOAL_DISARM_SHIP, OP_AI_DISARM_SHIP },
573 { AI_GOAL_GUARD, OP_AI_GUARD },
574 { AI_GOAL_CHASE_ANY, OP_AI_CHASE_ANY },
575 { AI_GOAL_GUARD_WING, OP_AI_GUARD_WING },
576 { AI_GOAL_EVADE_SHIP, OP_AI_EVADE_SHIP },
577 { AI_GOAL_STAY_NEAR_SHIP, OP_AI_STAY_NEAR_SHIP },
578 { AI_GOAL_KEEP_SAFE_DISTANCE, OP_AI_KEEP_SAFE_DISTANCE },
579 { AI_GOAL_IGNORE, OP_AI_IGNORE },
580 { AI_GOAL_STAY_STILL, OP_AI_STAY_STILL },
581 { AI_GOAL_PLAY_DEAD, OP_AI_PLAY_DEAD },
584 char *HUD_gauge_text[NUM_HUD_GAUGES] =
596 "TARGET_MONITOR_EXTRA_DATA",
597 "TARGET_SHIELD_ICON",
598 "PLAYER_SHIELD_ICON",
606 "AFTERBURNER_ENERGY",
608 "WEAPON_LINKING_GAUGE",
610 "OFFSCREEN_INDICATOR",
614 "MISSILE_WARNING_ARROW",
616 "OBJECTIVES_NOTIFY_GAUGE",
620 "ATTACKING TARGET COUNT",
629 int Sexp_useful_number; // a variable to pass useful info in from external modules
630 int Locked_sexp_true, Locked_sexp_false;
631 int Num_operators = sizeof(Operators) / sizeof(sexp_oper);
632 int Num_sexp_ai_goal_links = sizeof(Sexp_ai_goal_links) / sizeof(sexp_ai_goal_link);
634 int Sexp_clipboard = -1; // used by Fred
635 int Training_context = 0;
636 int Training_context_speed_set;
637 int Training_context_speed_min;
638 int Training_context_speed_max;
639 int Training_context_speed_timestamp;
640 int Training_context_path;
641 int Training_context_goal_waypoint;
642 int Training_context_at_waypoint;
643 float Training_context_distance;
644 char Sexp_error_text[MAX_SEXP_TEXT];
645 char *Sexp_string; //[1024] = {0};
646 sexp_node Sexp_nodes[MAX_SEXP_NODES];
647 sexp_variable Sexp_variables[MAX_SEXP_VARIABLES];
649 int Players_target = UNINITIALIZED;
650 ship_subsys *Players_targeted_subsys;
651 int Players_target_timestamp;
653 int get_sexp(char *token);
654 int eval_sexp(int cur_node);
655 void build_extended_sexp_string(int cur_node, int level, int mode);
656 void update_sexp_references(char *old_name, char *new_name, int format, int node);
657 int sexp_determine_team(char *subj);
658 int sexp_distance2(int obj1, char *subj);
659 int sexp_distance3(int obj1, int obj2);
660 int extract_sexp_variable_index(int node);
661 void init_sexp_vars();
662 int num_eval(int node);
668 for (i=0; i<MAX_SEXP_NODES; i++) {
669 if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) ){
670 Sexp_nodes[i].type = SEXP_NOT_USED;
676 Locked_sexp_false = Locked_sexp_true = -1;
677 Locked_sexp_false = alloc_sexp("false", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
678 Assert(Locked_sexp_false != -1);
679 Sexp_nodes[Locked_sexp_false].type = SEXP_ATOM; // fix bypassing value
680 Locked_sexp_true = alloc_sexp("true", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
681 Assert(Locked_sexp_true != -1);
682 Sexp_nodes[Locked_sexp_true].type = SEXP_ATOM; // fix bypassing value
685 // allocates an sexp node.
686 int alloc_sexp(char *text, int type, int subtype, int first, int rest)
690 i = find_operator(text);
691 if ((i == OP_TRUE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
692 return Locked_sexp_true;
693 } else if ((i == OP_FALSE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
694 return Locked_sexp_false;
697 i = find_free_sexp();
698 Assert(i != Locked_sexp_true);
699 Assert(i != Locked_sexp_false);
700 if (i == MAX_SEXP_NODES){
704 Assert(strlen(text) < TOKEN_LENGTH);
705 strcpy(Sexp_nodes[i].text, text);
707 Sexp_nodes[i].type = type;
708 Sexp_nodes[i].subtype = subtype;
709 Sexp_nodes[i].first = first;
710 Sexp_nodes[i].rest = rest;
711 Sexp_nodes[i].value = SEXP_UNKNOWN;
717 int count_free_sexp_nodes()
721 for (i=0; i<MAX_SEXP_NODES; i++) {
722 if (Sexp_nodes[i].type == SEXP_NOT_USED){
724 } else if (Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT){
729 if (MAX_SEXP_NODES - f > Sexp_hwm) {
730 nprintf(("Sexp", "Sexp nodes: Free=%d, Used=%d, Persistent=%d\n", f, MAX_SEXP_NODES - f, p));
731 Sexp_hwm = MAX_SEXP_NODES - f;
737 // find the next free sexp and return it's index.
742 for (i=0; i<MAX_SEXP_NODES; i++){
743 if (Sexp_nodes[i].type == SEXP_NOT_USED){
749 //count_free_sexp_nodes();
752 Assert(i != MAX_SEXP_NODES); // time to raise the limit..
753 if (i == MAX_SEXP_NODES){
760 // sexp_mark_persistent() marks a whole sexp tree with the persistent flag so that it won't
761 // get re-used between missions
762 void sexp_mark_persistent( int n )
768 // total hack because of the true/false locked sexps -- we should make those persistent as well
769 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
773 Assert( !(Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT) );
774 Sexp_nodes[n].type |= SEXP_FLAG_PERSISTENT;
776 sexp_mark_persistent(Sexp_nodes[n].first);
777 sexp_mark_persistent(Sexp_nodes[n].rest);
781 // sexp_unmark_persistent() removes the persistent flag from all nodes in the tree
782 void sexp_unmark_persistent( int n )
788 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
792 Assert( Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT );
793 Sexp_nodes[n].type &= ~SEXP_FLAG_PERSISTENT;
795 sexp_unmark_persistent(Sexp_nodes[n].first);
796 sexp_unmark_persistent(Sexp_nodes[n].rest);
799 // just frees up the specified sexp node, Leaves link chains untouched.
800 int free_one_sexp(int num)
802 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
803 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
805 if ((num == Locked_sexp_true) || (num == Locked_sexp_false)){
809 Sexp_nodes[num].type = SEXP_NOT_USED;
813 // frees a used sexp node, so it can be reused later. Should only be called on
814 // an atom or a list, and not an operator. If on a list, the list and everything
815 // in it will be freed (including the operator).
816 int free_sexp(int num)
818 int i, rest, count = 0;
820 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
821 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
823 if ((num == Locked_sexp_true) || (num == Locked_sexp_false) || (num == -1) ){
827 Sexp_nodes[num].type = SEXP_NOT_USED;
830 i = Sexp_nodes[num].first;
832 count += free_sexp(i);
833 i = Sexp_nodes[i].rest;
836 rest = Sexp_nodes[num].rest;
837 for (i=0; i<MAX_SEXP_NODES; i++) {
838 if (Sexp_nodes[i].first == num){
839 Sexp_nodes[i].first = rest;
842 if (Sexp_nodes[i].rest == num){
843 Sexp_nodes[i].rest = rest;
847 return count; // total elements freed up.
850 // used to free up an entire sexp tree. Because the root node is an operator, instead of
851 // a list, we can't simply call free_sexp(). This function should only be called on the
852 // root node of an sexp, otherwise the linking will get screwed up.
853 int free_sexp2(int num)
857 if ((num == -1) || (num == Locked_sexp_true) || (num == Locked_sexp_false)){
861 i = Sexp_nodes[num].rest;
863 count += free_sexp(i);
864 i = Sexp_nodes[i].rest;
867 count += free_sexp(num);
871 // This function resets the status of all the nodes in a tree, forcing them to all be
873 void flush_sexp_tree(int node)
879 Sexp_nodes[node].value = SEXP_UNKNOWN;
880 flush_sexp_tree(Sexp_nodes[node].first);
881 flush_sexp_tree(Sexp_nodes[node].rest);
884 int verify_sexp_tree(int node)
890 if ((Sexp_nodes[node].type == SEXP_NOT_USED) ||
891 (Sexp_nodes[node].first == node) ||
892 (Sexp_nodes[node].rest == node)) {
893 Error(LOCATION, "Sexp node is corrupt");
897 if (Sexp_nodes[node].first != -1){
898 verify_sexp_tree(Sexp_nodes[node].first);
900 if (Sexp_nodes[node].rest != -1){
901 verify_sexp_tree(Sexp_nodes[node].rest);
907 int dup_sexp_chain(int node)
909 int cur, first, rest;
915 // TODO - CASE OF SEXP VARIABLES - ONLY 1 COPY OF VARIABLE
916 first = dup_sexp_chain(Sexp_nodes[node].first);
917 rest = dup_sexp_chain(Sexp_nodes[node].rest);
918 cur = alloc_sexp(Sexp_nodes[node].text, Sexp_nodes[node].type, Sexp_nodes[node].subtype, first, rest);
932 // returns 1 if they are the same, 0 if different
933 int cmp_sexp_chains(int node1, int node2)
935 if ((node1 == -1) && (node2 == -1)){
939 if ((node1 == -1) || (node2 == -1)){
943 // DA: 1/7/99 Need to check the actual Sexp_node.text, not possible variable, which can be equal
944 if (stricmp(Sexp_nodes[node1].text, Sexp_nodes[node2].text)){
948 if (!cmp_sexp_chains(Sexp_nodes[node1].first, Sexp_nodes[node2].first)){
952 if (!cmp_sexp_chains(Sexp_nodes[node1].rest, Sexp_nodes[node2].rest)){
959 // determine if an sexp node is within the given sexp chain.
960 int query_node_in_sexp(int node, int sexp)
969 if (query_node_in_sexp(node, Sexp_nodes[sexp].first)){
972 if (query_node_in_sexp(node, Sexp_nodes[sexp].rest)){
979 // find the index of the list associated with an operator
980 int find_sexp_list(int num)
984 for (i=0; i<MAX_SEXP_NODES; i++){
985 if (Sexp_nodes[i].first == num){
993 // find index of operator that item is an argument of.
994 int find_parent_operator(int num)
998 if (Sexp_nodes[num].subtype == SEXP_ATOM_OPERATOR){
999 num = find_sexp_list(num);
1002 while (Sexp_nodes[num].subtype != SEXP_ATOM_OPERATOR) {
1003 for (i=0; i<MAX_SEXP_NODES; i++){
1004 if (Sexp_nodes[i].rest == num){
1009 if (i == MAX_SEXP_NODES){
1010 return -1; // not found, probably at top node already.
1019 // function to determine if an sexpression node is the top level node of an sexpression tree. Top
1020 // level nodes do not have their node id in anyone elses first or rest index
1021 int is_sexp_top_level( int node )
1025 if ( Sexp_nodes[node].type == SEXP_NOT_USED ){
1029 for (i = 0; i < MAX_SEXP_NODES; i++ ) {
1030 if ( (Sexp_nodes[i].type == SEXP_NOT_USED) || ( i == node ) ){ // don't check myself or unused nodes
1033 if ( (Sexp_nodes[i].first == node) || (Sexp_nodes[i].rest == node) ){
1041 int identify_operator(char *token)
1045 for (i=0; i<Num_operators; i++){
1046 if (!stricmp(token, Operators[i].text)){
1054 int find_operator(char *token)
1058 for (i=0; i<Num_operators; i++){
1059 if (!stricmp(token, Operators[i].text)){
1060 return Operators[i].value;
1067 int query_sexp_args_count(int index)
1071 while (Sexp_nodes[index].rest != -1){
1073 index = Sexp_nodes[index].rest;
1079 // returns 0 if ok, negative if there's an error in expression..
1080 // See the returns types in sexp.h
1082 int check_sexp_syntax(int index, int return_type, int recursive, int *bad_index, int mode)
1084 int i = 0, z, t, type, argnum = 0, count, op, type2 = 0, op2;
1087 Assert(index >= 0 && index < MAX_SEXP_NODES);
1088 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1089 if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER && return_type == OPR_BOOL) {
1090 // special case Mark seems to want supported
1091 Assert(Sexp_nodes[index].first == -1); // only lists should have a first pointer
1092 if (Sexp_nodes[index].rest != -1) // anything after the number?
1093 return SEXP_CHECK_NONOP_ARGS; // if so, it's a syntax error
1098 op_index = index; // save the index of the operator since we need to get to other args.
1100 *bad_index = op_index;
1102 if (Sexp_nodes[op_index].subtype != SEXP_ATOM_OPERATOR)
1103 return SEXP_CHECK_OP_EXPTECTED; // not an operator, which it should always be
1105 op = identify_operator(CTEXT(op_index));
1107 return SEXP_CHECK_UNKNOWN_OP; // unrecognized operator
1109 //special case - OPR_AMBIGUOUS matches all
1110 if (return_type != OPR_AMBIGUOUS) {
1111 if (query_operator_return_type(op) != return_type) {
1112 return SEXP_CHECK_TYPE_MISMATCH;
1116 count = query_sexp_args_count(op_index);
1117 if (count < Operators[op].min || count > Operators[op].max)
1118 return SEXP_CHECK_BAD_ARG_COUNT; // incorrect number of arguments
1120 index = Sexp_nodes[op_index].rest;
1121 while (index != -1) {
1122 type = query_operator_argument_type(op, argnum);
1123 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1127 if (Sexp_nodes[index].subtype == SEXP_ATOM_LIST) {
1128 i = Sexp_nodes[index].first;
1132 // be sure to check to see if this node is a list of stuff and not an actual operator type
1133 // thing. (i.e. in the case of a cond statement, the conditional will fall into this if
1134 // statement. MORE TO DO HERE!!!!
1135 if (Sexp_nodes[i].subtype == SEXP_ATOM_LIST)
1138 op2 = identify_operator(CTEXT(i));
1140 return SEXP_CHECK_UNKNOWN_OP;
1142 type2 = query_operator_return_type(op2);
1165 // special case for modify-variable"
1171 return SEXP_CHECK_UNKNOWN_TYPE; // no other return types available
1174 if ((z = check_sexp_syntax(i, t, recursive, bad_index)) != 0) {
1179 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER) {
1182 type2 = OPR_POSITIVE;
1189 if (type == OPF_BOOL) // allow numbers to be used where boolean is required.
1194 return SEXP_CHECK_INVALID_NUM; // not a valid number
1199 i = atoi(CTEXT(index));
1200 z = find_operator(CTEXT(op_index));
1201 if ( (z == OP_HAS_DOCKED_DELAY) || (z == OP_HAS_UNDOCKED_DELAY) )
1202 if ( (argnum == 2) && (i < 1) )
1203 return SEXP_CHECK_NUM_RANGE_INVALID;
1205 z = identify_operator(CTEXT(op_index));
1206 if ( (query_operator_return_type(z) == OPR_AI_GOAL) && (argnum == Operators[op].min - 1) )
1207 if ( (i < 0) || (i > 89) )
1208 return SEXP_CHECK_NUM_RANGE_INVALID;
1210 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_STRING) {
1211 type2 = SEXP_ATOM_STRING;
1219 if ((type2 != OPR_NUMBER) && (type2 != OPR_POSITIVE)){
1220 return SEXP_CHECK_TYPE_MISMATCH;
1226 if (type2 == OPR_NUMBER){
1227 return SEXP_CHECK_NEGATIVE_NUM;
1230 if (type2 != OPR_POSITIVE){
1231 return SEXP_CHECK_TYPE_MISMATCH;
1236 case OPF_SHIP_NOT_PLAYER:
1237 if (type2 != SEXP_ATOM_STRING){
1238 return SEXP_CHECK_TYPE_MISMATCH;
1241 if (ship_name_lookup(CTEXT(index), 0) < 0) {
1242 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))){ // == 0 when still on arrival list
1243 return SEXP_CHECK_INVALID_SHIP;
1250 case OPF_SHIP_POINT:
1251 if (type2 != SEXP_ATOM_STRING){
1252 return SEXP_CHECK_TYPE_MISMATCH;
1255 if (ship_name_lookup(CTEXT(index), 1) < 0) {
1256 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1257 if (type == OPF_SHIP){ // return invalid ship if not also looking for point
1258 return SEXP_CHECK_INVALID_SHIP;
1261 if (waypoint_lookup(CTEXT(index)) < 0){
1262 if (verify_vector(CTEXT(index))){ // verify return non-zero on invalid point
1263 return SEXP_CHECK_INVALID_POINT;
1272 if (type2 != SEXP_ATOM_STRING){
1273 return SEXP_CHECK_TYPE_MISMATCH;
1276 if (wing_name_lookup(CTEXT(index), 1) < 0){
1277 return SEXP_CHECK_INVALID_WING;
1283 case OPF_SHIP_WING_POINT:
1284 if ( type2 != SEXP_ATOM_STRING ){
1285 return SEXP_CHECK_TYPE_MISMATCH;
1288 if ((ship_name_lookup(CTEXT(index), 1) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) {
1289 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1290 if (type != OPF_SHIP_WING_POINT){ // return invalid if not also looking for point
1291 return SEXP_CHECK_INVALID_SHIP_WING;
1294 if (waypoint_lookup(CTEXT(index)) < 0){
1295 if (verify_vector(CTEXT(index))){ // non-zero on verify vector mean invalid!
1296 if (!sexp_determine_team(CTEXT(index))){
1297 return SEXP_CHECK_INVALID_POINT;
1306 case OPF_AWACS_SUBSYSTEM:
1307 case OPF_SUBSYSTEM: {
1309 int shipnum,ship_class, i;
1312 if (type2 != SEXP_ATOM_STRING){
1313 return SEXP_CHECK_TYPE_MISMATCH;
1316 // we must get the model of the ship that is part of this sexpression and find a subsystem
1317 // with that name. This code assumes that the ship *always* preceeds the subsystem in an sexpression.
1318 // if this current fact is ever not the case, the next lines of code must be changes to get the correct
1320 switch(Operators[identify_operator(CTEXT(op_index))].value){
1321 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
1322 ship_index = Sexp_nodes[Sexp_nodes[op_index].rest].rest;
1327 ship_index = Sexp_nodes[op_index].rest;
1329 ship_index = Sexp_nodes[Sexp_nodes[Sexp_nodes[op_index].rest].rest].rest;
1334 ship_index = Sexp_nodes[op_index].rest;
1338 shipname = CTEXT(ship_index);
1339 shipnum = ship_name_lookup(shipname);
1341 ship_class = Ships[shipnum].ship_info_index;
1343 // must try to find the ship in the ship_arrival_list
1344 p_object *parse_obj;
1346 parse_obj = mission_parse_get_arrival_ship( shipname );
1347 if ( parse_obj == NULL ){
1348 return SEXP_CHECK_INVALID_SHIP;
1351 ship_class = parse_obj->ship_class;
1354 // check for the special "hull" value
1355 if ( (Operators[op].value == OP_SABOTAGE_SUBSYSTEM) || (Operators[op].value == OP_REPAIR_SUBSYSTEM) || (Operators[op].value == OP_SET_SUBSYSTEM_STRNGTH) ) {
1356 if ( !stricmp( CTEXT(index), SEXP_HULL_STRING) ){
1361 for (i=0; i<Ship_info[ship_class].n_subsystems; i++){
1362 if (!stricmp(Ship_info[ship_class].subsystems[i].subobj_name, CTEXT(index))){
1367 if (i == Ship_info[ship_class].n_subsystems){
1368 return SEXP_CHECK_INVALID_SUBSYS;
1371 // if we're checking for an AWACS subsystem and this is not an awacs subsystem
1373 if((type == OPF_AWACS_SUBSYSTEM) && !(Ship_info[ship_class].subsystems[i].flags & MSS_FLAG_AWACS)){
1374 return SEXP_CHECK_INVALID_SUBSYS;
1382 if (waypoint_lookup(CTEXT(index)) < 0){
1383 if (verify_vector(CTEXT(index))){
1384 return SEXP_CHECK_INVALID_POINT;
1388 if (type2 != SEXP_ATOM_STRING){
1389 return SEXP_CHECK_TYPE_MISMATCH;
1395 if (type2 == SEXP_ATOM_STRING){
1396 for (i=0; i<Num_team_names; i++){
1397 if (!stricmp(Team_names[i], CTEXT(index))){
1403 if (i == Num_team_names){
1404 return SEXP_CHECK_INVALID_IFF;
1407 if (type2 != SEXP_ATOM_STRING){
1408 return SEXP_CHECK_TYPE_MISMATCH;
1414 if (type2 != OPR_BOOL){
1415 return SEXP_CHECK_TYPE_MISMATCH;
1421 if (type2 != OPR_NULL){
1422 return SEXP_CHECK_TYPE_MISMATCH;
1428 if ( type2 != SEXP_ATOM_STRING ){
1429 return SEXP_CHECK_TYPE_MISMATCH;
1435 if (type2 != OPR_AI_GOAL){
1436 return SEXP_CHECK_TYPE_MISMATCH;
1440 int ship_num, ship2, i, w = 0, z;
1442 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1444 w = wing_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1447 *bad_index = Sexp_nodes[op_index].rest;
1450 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1454 Assert(Sexp_nodes[index].subtype == SEXP_ATOM_LIST);
1455 z = Sexp_nodes[index].first;
1456 Assert(Sexp_nodes[z].subtype != SEXP_ATOM_LIST);
1457 z = find_operator(CTEXT(z));
1458 if (ship_num >= 0) {
1459 if (!query_sexp_ai_goal_valid(z, ship_num)){
1460 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1464 for (i=0; i<Wings[w].wave_count; i++){
1465 if (!query_sexp_ai_goal_valid(z, Wings[w].ship_index[i])){
1466 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1471 if ((z == OP_AI_DOCK) && (Sexp_nodes[index].rest >= 0)) {
1472 ship2 = ship_name_lookup(CTEXT(Sexp_nodes[index].rest));
1473 if ((ship_num < 0) || !ship_docking_valid(ship_num, ship2)){
1474 return SEXP_CHECK_DOCKING_NOT_ALLOWED;
1479 // we should check the syntax of the actual goal!!!!
1480 z = Sexp_nodes[index].first;
1481 if ((z = check_sexp_syntax(z, OPR_AI_GOAL, recursive, bad_index)) != 0){
1488 if (type2 != SEXP_ATOM_STRING){
1489 return SEXP_CHECK_TYPE_MISMATCH;
1492 for (i=0; i<MAX_SHIP_TYPE_COUNTS; i++){
1493 if (!stricmp( Ship_type_names[i], CTEXT(index))){
1498 if (i == MAX_SHIP_TYPE_COUNTS){
1499 return SEXP_CHECK_INVALID_SHIP_TYPE;
1504 case OPF_WAYPOINT_PATH:
1505 for (i=0; i<Num_waypoint_lists; i++){
1506 if (!stricmp(Waypoint_lists[i].name, CTEXT(index))){
1511 if (i == Num_waypoint_lists){
1512 return SEXP_CHECK_TYPE_MISMATCH;
1517 // code commented out because of order reversing of arguments to messaging code. Maybe
1518 // I'll comment it back in someday when older missions get fixed.
1519 if (type2 != SEXP_ATOM_STRING)
1520 return SEXP_CHECK_TYPE_MISMATCH;
1523 for (i=0; i<Num_messages; i++)
1524 if (!stricmp(Messages[i].name, CTEXT(index)))
1527 if (i == Num_messages)
1528 return SEXP_CHECK_UNKNOWN_MESSAGE;
1533 case OPF_PRIORITY: {
1534 // following code must be removed since I changed the order of the operators in the send-message
1535 // function to take priority second. Maybe someday, I'll require that all designers fix the
1537 if (type2 != SEXP_ATOM_STRING)
1538 return SEXP_CHECK_TYPE_MISMATCH;
1540 if (Fred_running) { // should still check in Fred though..
1543 name = CTEXT(index);
1544 if (!stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high"))
1547 return SEXP_CHECK_INVALID_PRIORITY;
1553 case OPF_MISSION_NAME:
1554 if (type2 != SEXP_ATOM_STRING)
1555 return SEXP_CHECK_TYPE_MISMATCH;
1558 if (mode == SEXP_MODE_CAMPAIGN) {
1559 for (i=0; i<Campaign.num_missions; i++)
1560 if (!stricmp(CTEXT(index), Campaign.missions[i].name)) {
1561 if ((i != Sexp_useful_number) && (Campaign.missions[i].level >= Campaign.missions[Sexp_useful_number].level))
1562 return SEXP_CHECK_INVALID_LEVEL;
1567 if (i == Campaign.num_missions)
1568 return SEXP_CHECK_INVALID_MISSION_NAME;
1571 // mwa -- put the following if statement to prevent Fred errors for possibly valid
1572 // conditions. We should do something else here!!!
1573 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE)
1574 || (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1577 if (!(*Mission_filename) || stricmp(Mission_filename, CTEXT(index)))
1578 return SEXP_CHECK_INVALID_MISSION_NAME;
1585 if (type2 != SEXP_ATOM_STRING)
1586 return SEXP_CHECK_TYPE_MISMATCH;
1588 // we only need to check the campaign list if running in Fred and are in campaign mode.
1589 // otherwise, check the set of current goals
1590 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1591 z = find_parent_operator(index);
1593 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1595 for (i=0; i<Campaign.num_missions; i++)
1596 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1599 // read the goal/event list from the mission file if both num_goals and num_events
1601 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1602 read_mission_goal_list(i);
1604 if (i < Campaign.num_missions) {
1605 for (t=0; t<Campaign.missions[i].num_goals; t++)
1606 if (!stricmp(CTEXT(index), Campaign.missions[i].goals[t].name))
1609 if (t == Campaign.missions[i].num_goals)
1610 return SEXP_CHECK_INVALID_GOAL_NAME;
1613 // MWA -- short circuit evaluation of these things for now.
1614 if ( (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1617 for (i=0; i<Num_goals; i++)
1618 if (!stricmp(CTEXT(index), Mission_goals[i].name))
1622 return SEXP_CHECK_INVALID_GOAL_NAME;
1627 case OPF_EVENT_NAME:
1628 if ( type2 != SEXP_ATOM_STRING )
1629 return SEXP_CHECK_TYPE_MISMATCH;
1631 // like above checking for goals, check events in the campaign only if in Fred
1632 // and only if in campaign mode. Otherwise, check the current set of events
1633 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1634 z = find_parent_operator(index);
1636 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1638 for (i=0; i<Campaign.num_missions; i++)
1639 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1642 // read the goal/event list from the mission file if both num_goals and num_events
1644 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1645 read_mission_goal_list(i);
1647 if (i < Campaign.num_missions) {
1648 for (t=0; t<Campaign.missions[i].num_events; t++)
1649 if (!stricmp(CTEXT(index), Campaign.missions[i].events[t].name))
1652 if (t == Campaign.missions[i].num_events)
1653 return SEXP_CHECK_INVALID_EVENT_NAME;
1656 // MWA -- short circuit evaluation of these things for now.
1657 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE) )
1660 for ( i = 0; i < Num_mission_events; i++ ) {
1661 if ( !stricmp(CTEXT(index), Mission_events[i].name) )
1664 if ( i == Num_mission_events )
1665 return SEXP_CHECK_INVALID_EVENT_NAME;
1669 case OPF_DOCKER_POINT:
1670 if (type2 != SEXP_ATOM_STRING)
1671 return SEXP_CHECK_TYPE_MISMATCH;
1674 int ship_num, model, i, z;
1676 z = find_parent_operator(op_index);
1677 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[z].rest));
1680 *bad_index = Sexp_nodes[z].rest;
1682 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1685 model = Ships[ship_num].modelnum;
1686 z = model_get_num_dock_points(model);
1688 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1692 return SEXP_CHECK_INVALID_DOCKER_POINT;
1697 case OPF_DOCKEE_POINT:
1698 if (type2 != SEXP_ATOM_STRING)
1699 return SEXP_CHECK_TYPE_MISMATCH;
1702 int ship_num, model, i, z;
1704 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1707 *bad_index = Sexp_nodes[op_index].rest;
1709 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1712 model = Ships[ship_num].modelnum;
1713 z = model_get_num_dock_points(model);
1715 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1719 return SEXP_CHECK_INVALID_DOCKEE_POINT;
1725 if (type2 != SEXP_ATOM_STRING)
1726 return SEXP_CHECK_TYPE_MISMATCH;
1728 if (*CTEXT(index) != '#') { // not a manual source?
1729 //if ( !stricmp(CTEXT(index), "<any allied>") )
1730 // return SEXP_CHECK_INVALID_MSG_SOURCE;
1732 if ( stricmp(CTEXT(index), "<any wingman>")) // not a special token?
1733 if ((ship_name_lookup(CTEXT(index)) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) // is it in the mission?
1734 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) // == 0 when still on arrival list
1735 return SEXP_CHECK_INVALID_MSG_SOURCE;
1741 if (type2 != SEXP_ATOM_STRING)
1742 return SEXP_CHECK_TYPE_MISMATCH;
1746 case OPF_SKILL_LEVEL:
1747 if ( type2 != SEXP_ATOM_STRING )
1748 return SEXP_CHECK_TYPE_MISMATCH;
1750 for (i = 0; i < NUM_SKILL_LEVELS; i++) {
1751 if ( !stricmp(CTEXT(index), Skill_level_names(i, 0)) )
1754 if ( i == NUM_SKILL_LEVELS )
1755 return SEXP_CHECK_INVALID_SKILL_LEVEL;
1758 case OPF_MEDAL_NAME:
1759 if ( type2 != SEXP_ATOM_STRING)
1760 return SEXP_CHECK_TYPE_MISMATCH;
1762 for (i = 0; i < NUM_MEDALS; i++) {
1763 if ( !stricmp(CTEXT(index), Medals[i].name) )
1767 if ( i == NUM_MEDALS )
1768 return SEXP_CHECK_INVALID_MEDAL_NAME;
1771 case OPF_HUGE_WEAPON:
1772 case OPF_WEAPON_NAME:
1773 if ( type2 != SEXP_ATOM_STRING )
1774 return SEXP_CHECK_TYPE_MISMATCH;
1776 for (i = 0; i < Num_weapon_types; i++ ) {
1777 if ( !stricmp(CTEXT(index), Weapon_info[i].name) )
1781 if ( i == Num_weapon_types )
1782 return SEXP_CHECK_INVALID_WEAPON_NAME;
1784 // we need to be sure that for huge weapons, the WIF_HUGE flag is set
1785 if ( type == OPF_HUGE_WEAPON ) {
1786 if ( !(Weapon_info[i].wi_flags & WIF_HUGE) )
1787 return SEXP_CHECK_INVALID_WEAPON_NAME;
1792 case OPF_SHIP_CLASS_NAME:
1793 if ( type2 != SEXP_ATOM_STRING )
1794 return SEXP_CHECK_TYPE_MISMATCH;
1796 for (i = 0; i < Num_ship_types; i++ ) {
1797 if ( !stricmp(CTEXT(index), Ship_info[i].name) )
1801 if ( i == Num_ship_types )
1802 return SEXP_CHECK_INVALID_SHIP_CLASS_NAME;
1805 case OPF_HUD_GAUGE_NAME:
1806 if ( type2 != SEXP_ATOM_STRING )
1807 return SEXP_CHECK_TYPE_MISMATCH;
1809 for ( i = 0; i < NUM_HUD_GAUGES; i++ ) {
1810 if ( !stricmp(CTEXT(index), HUD_gauge_text[i]) )
1814 // if we reached the end of the list, then the name is invalid
1815 if ( i == NUM_HUD_GAUGES )
1816 return SEXP_CHECK_INVALID_GAUGE_NAME;
1820 case OPF_JUMP_NODE_NAME:
1821 if ( type2 != SEXP_ATOM_STRING )
1822 return SEXP_CHECK_TYPE_MISMATCH;
1824 for ( i = 0; i < Num_jump_nodes; i++ ) {
1825 if ( !stricmp(Jump_nodes[i].name, CTEXT(index)) )
1829 if ( i == Num_jump_nodes )
1830 return SEXP_CHECK_INVALID_JUMP_NODE;
1835 case OPF_VARIABLE_NAME:
1836 if ( Fred_running ) {
1837 if ( get_index_sexp_variable_name(Sexp_nodes[index].text) == -1) {
1838 return SEXP_CHECK_INVALID_VARIABLE;
1841 // if Fred not running anything goes
1845 // type checking for modify-variable
1846 // string or number -- anything goes
1850 Int3(); // currently unhandled argument format (so add it now)
1853 index = Sexp_nodes[index].rest;
1860 // Stuff a string (" chars ") in *str, return length.
1862 int get_string(char *str)
1866 len = strcspn(Mp + 1, "\"");
1867 strncpy(str, Mp + 1, len);
1875 // get text to stuff into Sexp_node in case of variable
1876 // if Fred_running - stuff Sexp_variables[].variable_name
1877 // otherwise - stuff index into Sexp_variables array.
1878 void get_sexp_text_for_variable(char *text, char *token)
1883 // get variable name (up to '['
1884 end_index = strcspn(token, "[");
1885 Assert( (end_index != 0) && (end_index < TOKEN_LENGTH-1) );
1886 strncpy(text, token, end_index);
1887 text[end_index] = '\0';
1889 if ( !Fred_running ) {
1890 // freespace - get index into Sexp_variables array
1891 sexp_var_index = get_index_sexp_variable_name(text);
1892 Assert(sexp_var_index != -1);
1893 sprintf(text, "%d", sexp_var_index);
1898 // returns the first sexp index of data this function allocates. (start of this sexp)
1899 // recursive function - always sets first and then rest
1900 int get_sexp(char *token)
1902 int start, node, last, len, op, count;
1903 char variable_text[TOKEN_LENGTH];
1905 // start - the node allocated in first instance of fuction
1906 // node - the node allocated in current instance of function
1907 // count - number of nodes allocated this instance of function [do we set last.rest or first]
1908 // variable - whether string or number is a variable referencing Sexp_variables
1914 ignore_white_space();
1915 while (*Mp != ')') {
1916 Assert(*Mp != EOF_CHAR);
1920 node = alloc_sexp("", SEXP_LIST, SEXP_ATOM_LIST, get_sexp(token), -1);
1922 } else if (*Mp == '\"') {
1924 len = strcspn(Mp + 1, "\"");
1926 Assert(Mp[len + 1] == '\"'); // hit EOF first (unterminated string)
1927 Assert(len < TOKEN_LENGTH); // token is too long.
1929 // check if string variable
1930 if ( *(Mp + 1) == SEXP_VARIABLE_CHAR ) {
1932 // reduce length by 1 for end \"
1933 int length = len - 1;
1934 Assert(length < 2*TOKEN_LENGTH+2);
1936 // start copying after skipping 1st char
1937 strncpy(token, Mp + 2, length);
1940 get_sexp_text_for_variable(variable_text, token);
1941 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_STRING, -1, -1);
1943 strncpy(token, Mp + 1, len);
1945 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_STRING, -1, -1);
1948 // bump past closing \" by 1 char
1952 // Sexp operator or number
1954 bool variable = false;
1955 while (*Mp != ')' && !is_white_space(*Mp)) {
1956 if ( (len == 0) && (*Mp == SEXP_VARIABLE_CHAR) ) {
1961 Assert(*Mp != EOF_CHAR);
1962 Assert(len < TOKEN_LENGTH - 1);
1963 token[len++] = *Mp++;
1968 op = identify_operator(token);
1970 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_OPERATOR, -1, -1);
1973 // convert token text for variable
1974 get_sexp_text_for_variable(variable_text, token);
1976 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_NUMBER, -1, -1);
1978 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_NUMBER, -1, -1);
1986 Sexp_nodes[last].rest = node;
1991 Assert(node != -1); // ran out of nodes. Time to raise the MAX!
1993 ignore_white_space();
1996 Mp++; // skip past the ')'
2001 // Stuffs a list of sexp variables
2002 int stuff_sexp_variable_list()
2005 char var_name[TOKEN_LENGTH];
2006 char default_value[TOKEN_LENGTH];
2007 char str_type[TOKEN_LENGTH];
2012 required_string("$Variables:");
2013 ignore_white_space();
2015 // check for start of list
2017 error_display(1, "Reading sexp variable list. Found [%c]. Expecting '('.\n", *Mp);
2018 longjmp(parse_abort, 6);
2022 ignore_white_space();
2024 while (*Mp != ')') {
2025 Assert(count < MAX_SEXP_VARIABLES);
2027 // get index - for debug
2029 ignore_gray_space();
2032 get_string(var_name);
2033 ignore_gray_space();
2035 // get default_value;
2036 get_string(default_value);
2037 ignore_gray_space();
2040 get_string(str_type);
2041 ignore_white_space();
2044 if (!stricmp(str_type, "number")) {
2045 type = SEXP_VARIABLE_NUMBER;
2046 } else if (!stricmp(str_type, "string")) {
2047 type = SEXP_VARIABLE_STRING;
2048 } else if (!stricmp(str_type, "block")) {
2049 type = SEXP_VARIABLE_BLOCK | SEXP_VARIABLE_BLOCK_EXP;
2051 type = SEXP_VARIABLE_UNKNOWN;
2057 // check if variable name already exists
2058 if ( (type == SEXP_VARIABLE_NUMBER) || (type == SEXP_VARIABLE_STRING) ) {
2059 Assert(get_index_sexp_variable_name(var_name) == -1);
2062 sexp_add_variable(default_value, var_name, type, index);
2071 void build_sexp_text_string(char *buffer, int node, int mode)
2073 if (Sexp_nodes[node].type & SEXP_FLAG_VARIABLE) {
2075 int sexp_variables_index = get_index_sexp_variable_name(Sexp_nodes[node].text);
2076 Assert(sexp_variables_index != -1);
2077 Assert( (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER) || (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING) );
2080 if (Sexp_nodes[node].subtype == SEXP_ATOM_NUMBER) {
2081 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER);
2083 // Error check - can be Fred or Freespace
2084 if (mode == SEXP_ERROR_CHECK_MODE) {
2085 if ( Fred_running ) {
2086 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2088 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2091 // Save as string - only Fred
2092 Assert(mode == SEXP_SAVE_MODE);
2093 sprintf(buffer, "@%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2097 Assert(Sexp_nodes[node].subtype == SEXP_ATOM_STRING);
2098 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING);
2100 // Error check - can be Fred or Freespace
2101 if (mode == SEXP_ERROR_CHECK_MODE) {
2102 if ( Fred_running ) {
2103 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2105 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2108 // Save as string - only Fred
2109 Assert(mode == SEXP_SAVE_MODE);
2110 sprintf(buffer, "\"@%s[%s]\" ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2115 if (Sexp_nodes[node].subtype == SEXP_ATOM_STRING) {
2116 sprintf(buffer, "\"%s\" ", CTEXT(node));
2118 sprintf(buffer, "%s ", CTEXT(node));
2125 int build_sexp_string(int cur_node, int level, int mode)
2128 int len, offset, node;
2130 Sexp_build_flag = 0;
2131 offset = strlen(Sexp_string);
2132 strcat(Sexp_string, "( ");
2134 while (node != -1) {
2135 Assert(node >= 0 && node < MAX_SEXP_NODES);
2136 if (Sexp_nodes[node].first == -1) {
2137 // build text to string
2138 build_sexp_text_string(pstr, node, mode);
2139 strcat(Sexp_string, pstr);
2142 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2145 node = Sexp_nodes[node].rest;
2148 strcat(Sexp_string, ") ");
2149 len = strlen(Sexp_string) - offset;
2151 Sexp_string[offset] = 0;
2152 build_extended_sexp_string(cur_node, level, mode);
2159 void build_extended_sexp_string(int cur_node, int level, int mode)
2162 int i, flag = 0, node;
2164 strcat(Sexp_string, "( ");
2166 while (node != -1) {
2167 if (flag) // not the first line?
2168 for (i=0; i<level + 1; i++)
2169 strcat(Sexp_string, " ");
2172 Assert(node >= 0 && node < MAX_SEXP_NODES);
2173 if (Sexp_nodes[node].first == -1) {
2174 build_sexp_text_string(pstr,node, mode);
2175 strcat(Sexp_string, pstr);
2178 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2181 strcat(Sexp_string, "\n");
2182 node = Sexp_nodes[node].rest;
2185 for (i=0; i<level; i++)
2186 strcat(Sexp_string, " ");
2188 strcat(Sexp_string, ")");
2191 void convert_sexp_to_string(int cur_node, char *outstr, int mode)
2193 Sexp_string = outstr;
2196 build_sexp_string(cur_node, 0, mode);
2198 strcpy(Sexp_string, "( )");
2201 // determine if the named ship or wing hasn't arrived yet (wing or ship must be on arrival list)
2202 int sexp_query_has_yet_to_arrive(char *name)
2206 if (ship_query_state(name) < 0)
2209 i = wing_name_lookup(name, 1);
2211 // has not arrived yet, and never will arrive
2212 if ((i >= 0) && (Wings[i].num_waves >= 0) && (Wings[i].flags & WF_NEVER_EXISTED)){
2216 // has not arrived yet
2217 if ((i >= 0) && (Wings[i].num_waves >= 0) && !Wings[i].total_arrived_count){
2224 // arithmetic functions
2225 int add_sexps(int n)
2231 sum = eval_sexp( CAR(n) );
2233 sum = atoi( CTEXT(n) );
2235 // be sure to check for the NAN value when doing arithmetic -- this value should
2236 // get propagated to the next highest function.
2237 if ( Sexp_nodes[CAR(n)].value == SEXP_NAN )
2239 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2240 return SEXP_NAN_FOREVER;
2242 while (CDR(n) != -1) {
2243 val = eval_sexp( CDR(n) );
2244 // be sure to check for the NAN value when doing arithmetic -- this value should
2245 // get propagated to the next highest function.
2246 if ( Sexp_nodes[CDR(n)].value == SEXP_NAN )
2248 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2249 return SEXP_NAN_FOREVER;
2258 int sub_sexps(int n)
2263 if (Sexp_nodes[n].first != -1)
2264 sum = eval_sexp(CAR(n));
2266 sum = atoi(CTEXT(n));
2268 while (CDR(n) != -1) {
2269 sum -= eval_sexp(CDR(n));
2277 int mul_sexps(int n)
2282 if (Sexp_nodes[n].first != -1)
2283 sum = eval_sexp(Sexp_nodes[n].first);
2285 sum = atoi(CTEXT(n));
2287 while (Sexp_nodes[n].rest != -1) {
2288 sum *= eval_sexp(Sexp_nodes[n].rest);
2289 n = Sexp_nodes[n].rest;
2296 int div_sexps(int n)
2301 if (Sexp_nodes[n].first != -1)
2302 sum = eval_sexp(Sexp_nodes[n].first);
2304 sum = atoi(CTEXT(n));
2306 while (Sexp_nodes[n].rest != -1) {
2307 sum /= eval_sexp(Sexp_nodes[n].rest);
2308 n = Sexp_nodes[n].rest;
2315 int mod_sexps(int n)
2320 if (Sexp_nodes[n].first != -1)
2321 sum = eval_sexp(Sexp_nodes[n].first);
2323 sum = atoi(CTEXT(n));
2325 while (Sexp_nodes[n].rest != -1) {
2326 sum = sum % eval_sexp(Sexp_nodes[n].rest);
2327 n = Sexp_nodes[n].rest;
2334 int rand_internal(int low, int high)
2338 // get diff - don't allow negative or zero
2344 return (low + rand() % (diff + 1));
2348 int rand_sexp(int n, int multiple=0)
2355 if (Sexp_nodes[n].value == SEXP_NUM_EVAL) {
2356 // don't regenerate new random number
2357 rand_num = atoi(CTEXT(n));
2360 // if (Sexp_nodes[n].first != -1) {
2361 // low = eval_sexp(Sexp_nodes[n].first);
2363 // low = atoi(CTEXT(n));
2368 high = num_eval(CDR(n));
2370 // get the random number
2371 rand_num = rand_internal(low, high);
2374 // set .value and .text so random number is generated only once.
2375 Sexp_nodes[n].value = SEXP_NUM_EVAL;
2376 sprintf(Sexp_nodes[n].text, "%d", rand_num);
2384 // boolean evaluation functions. Evaluate all sexpressions in the 'or' operator. Needed to mark
2385 // entries in the mission log as essential so when pruning the log, we know which entries we might
2389 int all_false, result;
2394 if (Sexp_nodes[n].first != -1) {
2395 result |= eval_sexp(Sexp_nodes[n].first);
2396 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_TRUE )
2397 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2398 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2401 result |= atoi(CTEXT(n));
2403 // don't return on true value -- keep evaluating for mission log purposes
2407 while (Sexp_nodes[n].rest != -1) {
2408 result |= eval_sexp(Sexp_nodes[n].rest);
2409 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_TRUE )
2410 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2411 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2413 n = Sexp_nodes[n].rest;
2414 // don't return on true value -- keep evaluating for mission log purposes
2421 return SEXP_KNOWN_FALSE;
2426 // this function does the 'and' operator. It will short circuit evaluation *but* it will still
2427 // evaluate other members of the and construct. I do this because I need events in the mission log
2428 // to get marked as essential for goal purposes, and evaluation is pretty much the only way
2431 int all_true, result;
2436 if (Sexp_nodes[n].first != -1) {
2437 result &= eval_sexp( CAR(n) );
2438 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2439 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2440 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be true
2443 result &= atoi(CTEXT(n));
2445 // don't short circuit -- evaluate everything for purposes of marking mission log
2449 while (Sexp_nodes[n].rest != -1) {
2452 new_result = eval_sexp( CDR(n) );
2453 result &= new_result;
2454 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2455 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2456 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2458 // don't short circuit -- evaluate everything for purposes of marking mission log
2462 n = Sexp_nodes[n].rest;
2467 return SEXP_KNOWN_TRUE;
2472 // this version of the 'and' operator determines whether or not it's arguments become true
2473 // in the order in which they are specified in the when statement. Should be a simple matter of
2474 // seeing if anything evaluates to true later than something that evalueated to false
2475 int sexp_and_in_sequence(int n)
2480 all_true = 1; // represents whether or not all nodes we have seen so far are true
2482 if (Sexp_nodes[n].first != -1) {
2483 result &= eval_sexp( CAR(n) );
2484 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2485 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2486 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if value is true, mark our all_true variable for later checking
2489 result &= atoi(CTEXT(n));
2491 // a little test -- if the previous sexpressions was true, then mark the node itself as always
2492 // true. I did this because of the distance function. It might become true, then when waiting for
2493 // the second evalation, it might become false, rendering this function false. So, when one becomes
2494 // true -- mark it true forever.
2496 Sexp_nodes[Sexp_nodes[n].first].value = SEXP_KNOWN_TRUE;
2498 while (Sexp_nodes[n].rest != -1) {
2501 next_result = eval_sexp( CDR(n) );
2502 if ( next_result && !result ) // if current result is true, and our running result is false, thngs didn't become true in order
2503 return SEXP_KNOWN_FALSE;
2504 result &= next_result;
2505 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2506 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2507 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2509 // see comment above for explanation of next lines
2511 Sexp_nodes[Sexp_nodes[n].rest].value = SEXP_KNOWN_TRUE;
2512 n = Sexp_nodes[n].rest;
2517 return SEXP_KNOWN_TRUE;
2522 // for these four basic boolean operations (not, <, >, and =), we have special cases that we must deal
2523 // with. We have sexpressions operators that might return a NAN type return value (such as the distance
2524 // between two ships when one of the ships is destroyed or departed). These operations need to check for
2525 // this special NAN value and adjust their return types accordingly. NAN values represent false return values
2526 int sexp_not( int n )
2531 if (Sexp_nodes[n].first != -1) {
2532 result = eval_sexp( CAR(n) );
2533 if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_FALSE )
2534 return SEXP_KNOWN_TRUE; // not KNOWN_FALSE == KNOWN_TRUE;
2535 else if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_TRUE ) // not KNOWN_TRUE == KNOWN_FALSE
2536 return SEXP_KNOWN_FALSE;
2537 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) // not NAN == TRUE (I think)
2539 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2542 result = atoi(CTEXT(n));
2552 exp1 = eval_sexp( n );
2553 exp2 = eval_sexp( CDR(n) );
2555 // check for the NAN value
2556 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2558 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2559 return SEXP_KNOWN_FALSE;
2570 exp1 = eval_sexp( n );
2571 exp2 = eval_sexp( CDR(n) );
2573 // check for the NAN value
2574 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2576 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2577 return SEXP_KNOWN_FALSE;
2584 int sexp_equal(int n)
2588 exp1 = eval_sexp( n );
2589 exp2 = eval_sexp( CDR(n) );
2591 // check for the NAN value
2592 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2594 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2595 return SEXP_KNOWN_FALSE;
2602 // Evaluate if given ship is destroyed.
2603 // Return true if the ship in the expression has been destroyed.
2604 int sexp_is_destroyed(int n, fix *latest_time)
2607 int count, num_destroyed, wing_index;
2619 if (sexp_query_has_yet_to_arrive(name))
2620 return SEXP_CANT_EVAL;
2622 // check to see if this ship/wing has departed. If so, then function is known false
2623 if ( mission_log_get_time (LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time (LOG_WING_DEPART, name, NULL, NULL) )
2624 return SEXP_KNOWN_FALSE;
2626 // check the mission log. If ship/wing not destroyed, immediately return 0.
2627 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) || mission_log_get_time(LOG_WING_DESTROYED, name, NULL, &time) || mission_log_get_time(LOG_SELF_DESTRUCT, name, NULL, &time)) {
2629 if ( latest_time && (time > *latest_time) )
2630 *latest_time = time;
2632 // ship or wing isn't destroyed -- add to directive count
2633 if ( (wing_index = wing_name_lookup( name, 1 )) >= 0 ) {
2634 Directive_count += Wings[wing_index].current_count;
2639 // move to next ship/wing in list
2643 // special case to mark a directive for destroy wing objectives true after a short amount
2644 // of time when there are more waves for this wing.
2645 if ( (count == 1) && (wing_index >= 0) && (Directive_count == 0) ) {
2646 if ( Wings[wing_index].current_wave < Wings[wing_index].num_waves )
2647 Directive_count = DIRECTIVE_WING_ZERO;
2650 if ( count == num_destroyed )
2651 return SEXP_KNOWN_TRUE;
2657 // return true if the subsystem of the given ship has been destroyed or not
2658 int sexp_is_subsystem_destroyed(int n)
2660 char *ship_name, *subsys_name;
2664 ship_name = CTEXT(n);
2665 subsys_name = CTEXT(CDR(n));
2667 if (sexp_query_has_yet_to_arrive(ship_name))
2668 return SEXP_CANT_EVAL;
2670 // if the ship has departed, no way to destroy it's subsystem.
2671 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2672 return SEXP_KNOWN_FALSE;
2674 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, NULL) )
2675 return SEXP_KNOWN_TRUE;
2681 // determines if a ship has docked
2682 int sexp_has_docked(int n)
2684 char *docker = CTEXT(n);
2685 char *dockee = CTEXT(CDR(n));
2686 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
2688 if (sexp_query_has_yet_to_arrive(docker))
2689 return SEXP_CANT_EVAL;
2691 if (sexp_query_has_yet_to_arrive(dockee))
2692 return SEXP_CANT_EVAL;
2694 Assert ( count > 0 );
2695 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2696 return SEXP_KNOWN_FALSE;
2698 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, NULL) )
2701 return SEXP_KNOWN_TRUE;
2704 // determines if a ship has undocked
2705 int sexp_has_undocked(int n)
2707 char *docker = CTEXT(n);
2708 char *dockee = CTEXT(CDR(n));
2709 int count = atoi(CTEXT(CDR(CDR(n))));
2711 if (sexp_query_has_yet_to_arrive(docker))
2712 return SEXP_CANT_EVAL;
2714 if (sexp_query_has_yet_to_arrive(dockee))
2715 return SEXP_CANT_EVAL;
2717 Assert ( count > 0 );
2718 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, NULL) ) {
2719 // if either ship destroyed before they dock, then sexp is known false
2720 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2721 return SEXP_KNOWN_FALSE;
2726 return SEXP_KNOWN_TRUE;
2729 // determines if a ship has arrived onto the scene
2730 int sexp_has_arrived(int n, fix *latest_time)
2733 int count, num_arrived;
2741 // if there is no log entry for this ship/wing for arrival, sexpression is false
2742 if ( mission_log_get_time(LOG_SHIP_ARRIVE, name, NULL, &time) || mission_log_get_time(LOG_WING_ARRIVE, name, NULL, &time) ) {
2744 if ( latest_time && (time > *latest_time) )
2745 *latest_time = time;
2750 if ( count == num_arrived )
2751 return SEXP_KNOWN_TRUE;
2756 // determines if a ship/wing has departed
2757 int sexp_has_departed(int n, fix *latest_time)
2760 int count, num_departed;
2769 if (sexp_query_has_yet_to_arrive(name))
2770 return SEXP_CANT_EVAL;
2772 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
2773 // the sexpression is not true.
2774 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) || mission_log_get_time(LOG_WING_DESTROYED, name, NULL, NULL))
2775 return SEXP_KNOWN_FALSE;
2776 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_WING_DEPART, name, NULL, &time) ) {
2778 if ( latest_time && (time > *latest_time) )
2779 *latest_time = time;
2784 if ( count == num_departed )
2785 return SEXP_KNOWN_TRUE;
2790 // determines if ships are disabled
2791 int sexp_is_disabled( int n, fix *latest_time )
2794 int count, num_disabled;
2803 if (sexp_query_has_yet_to_arrive(name))
2804 return SEXP_CANT_EVAL;
2806 // if ship/wing destroyed, sexpression is known false. Also, if there is no disable log entry, then
2807 // the sexpression is not true.
2808 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2809 return SEXP_KNOWN_FALSE;
2810 else if ( mission_log_get_time(LOG_SHIP_DISABLED, name, NULL, &time) ) {
2812 if ( latest_time && (time > *latest_time) )
2813 *latest_time = time;
2818 if ( count == num_disabled )
2819 return SEXP_KNOWN_TRUE;
2824 // function to determine if a ship is done flying waypoints
2825 int sexp_are_waypoints_done( int n )
2827 char *ship_name, *waypoint_name;
2829 ship_name = CTEXT(n);
2830 waypoint_name = CTEXT(CDR(n));
2832 if (sexp_query_has_yet_to_arrive(ship_name))
2833 return SEXP_CANT_EVAL;
2835 // a destroyed or departed ship will never reach their goal -- return known false
2836 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
2837 return SEXP_KNOWN_FALSE;
2838 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
2839 return SEXP_KNOWN_FALSE;
2841 // now check the log for the waypoints done entry
2842 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, NULL) )
2843 return SEXP_KNOWN_TRUE;
2849 // determines if ships are disarmed
2850 int sexp_is_disarmed( int n, fix *latest_time )
2853 int count, num_disarmed;
2862 if (sexp_query_has_yet_to_arrive(name))
2863 return SEXP_CANT_EVAL;
2865 // if ship/wing destroyed, sexpression is known false. Also, if there is no disarm log entry, then
2866 // the sexpression is not true.
2867 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2868 return SEXP_KNOWN_FALSE;
2869 else if ( mission_log_get_time(LOG_SHIP_DISARMED, name, NULL, &time) ) {
2871 if ( latest_time && (time > *latest_time) )
2872 *latest_time = time;
2877 if ( count == num_disarmed )
2878 return SEXP_KNOWN_TRUE;
2883 // the following functions are similar to the above objective functions but return true/false
2884 // if N seconds have elasped after the corresponding function is true.
2885 int sexp_is_destroyed_delay(int n)
2894 delay = i2f(num_eval(n));
2896 // check value of is_destroyed function. KNOWN_FALSE should be returned immediately
2897 val = sexp_is_destroyed( CDR(n), &time );
2898 if ( val == SEXP_KNOWN_FALSE )
2901 if ( val == SEXP_CANT_EVAL )
2902 return SEXP_CANT_EVAL;
2906 if ( (Missiontime - time) >= delay )
2907 return SEXP_KNOWN_TRUE;
2913 int sexp_is_subsystem_destroyed_delay( int n )
2915 char *ship_name, *subsys_name;
2920 ship_name = CTEXT(n);
2921 subsys_name = CTEXT(CDR(n));
2922 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
2924 if (sexp_query_has_yet_to_arrive(ship_name))
2925 return SEXP_CANT_EVAL;
2927 // if the ship has departed, no way to destroy it's subsystem.
2928 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2929 return SEXP_KNOWN_FALSE;
2931 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, &time) ) {
2932 if ( (Missiontime - time) >= delay )
2933 return SEXP_KNOWN_TRUE;
2939 int sexp_is_disabled_delay( int n )
2947 delay = i2f(atoi(CTEXT(n)));
2949 // check value of is_disable for known false and return immediately if it is.
2950 val = sexp_is_disabled( CDR(n), &time );
2951 if ( val == SEXP_KNOWN_FALSE )
2954 if ( val == SEXP_CANT_EVAL )
2955 return SEXP_CANT_EVAL;
2958 if ( (Missiontime - time) >= delay )
2959 return SEXP_KNOWN_TRUE;
2965 int sexp_is_disarmed_delay( int n )
2973 delay = i2f(atoi(CTEXT(n)));
2975 // check value of is_disarmed for a known false value and return that immediately if it is
2976 val = sexp_is_disarmed( CDR(n), &time );
2977 if ( val == SEXP_KNOWN_FALSE )
2980 if ( val == SEXP_CANT_EVAL )
2981 return SEXP_CANT_EVAL;
2984 if ( (Missiontime - time) >= delay )
2985 return SEXP_KNOWN_TRUE;
2991 int sexp_has_docked_delay( int n )
2993 char *docker = CTEXT(n);
2994 char *dockee = CTEXT(CDR(n));
2995 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
2996 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
2999 Assert ( count > 0 );
3000 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
3001 return SEXP_KNOWN_FALSE;
3003 if (sexp_query_has_yet_to_arrive(docker))
3004 return SEXP_CANT_EVAL;
3006 if (sexp_query_has_yet_to_arrive(dockee))
3007 return SEXP_CANT_EVAL;
3009 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) )
3012 if ( (Missiontime - time) >= delay )
3013 return SEXP_KNOWN_TRUE;
3018 int sexp_has_undocked_delay( int n )
3020 char *docker = CTEXT(n);
3021 char *dockee = CTEXT(CDR(n));
3022 int count = atoi(CTEXT(CDR(CDR(n))));
3023 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
3026 if (sexp_query_has_yet_to_arrive(docker))
3027 return SEXP_CANT_EVAL;
3029 if (sexp_query_has_yet_to_arrive(dockee))
3030 return SEXP_CANT_EVAL;
3032 Assert ( count > 0 );
3033 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ) {
3034 // if either ship destroyed before they dock, then sexp is known false
3035 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
3036 return SEXP_KNOWN_FALSE;
3041 if ( (Missiontime - time) >= delay )
3042 return SEXP_KNOWN_TRUE;
3047 int sexp_has_arrived_delay( int n )
3055 delay = i2f(atoi(CTEXT(n)));
3057 // check return value from arrived function. if can never arrive, then return that value here as well
3058 val = sexp_has_arrived( CDR(n), &time );
3059 if ( val == SEXP_KNOWN_FALSE )
3062 if ( val == SEXP_CANT_EVAL )
3063 return SEXP_CANT_EVAL;
3066 if ( (Missiontime - time) >= delay )
3067 return SEXP_KNOWN_TRUE;
3073 int sexp_has_departed_delay( int n )
3081 delay = i2f(atoi(CTEXT(n)));
3083 // must first check to see if the departed function could ever be true/false or is true or false.
3084 // if it can never be true, return that value
3085 val = sexp_has_departed( CDR(n), &time);
3086 if ( val == SEXP_KNOWN_FALSE )
3089 if ( val == SEXP_CANT_EVAL )
3090 return SEXP_CANT_EVAL;
3093 if ( (Missiontime - time) >= delay )
3094 return SEXP_KNOWN_TRUE;
3100 // function to determine if a ship is done flying waypoints after N seconds
3101 int sexp_are_waypoints_done_delay( int n )
3103 char *ship_name, *waypoint_name;
3106 ship_name = CTEXT(n);
3107 waypoint_name = CTEXT(CDR(n));
3108 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
3110 if (sexp_query_has_yet_to_arrive(ship_name))
3111 return SEXP_CANT_EVAL;
3113 // a destroyed or departed ship will never reach their goal -- return known false
3115 // Not checking the entries below. Ships which warp out after reaching their goal (or getting
3116 // destroyed after their goal), but after reaching their waypoints, may have this goal incorrectly
3119 // now check the log for the waypoints done entry
3120 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, &time) ) {
3121 if ( (Missiontime - time) >= delay )
3122 return SEXP_KNOWN_TRUE;
3124 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
3125 return SEXP_KNOWN_FALSE;
3126 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
3127 return SEXP_KNOWN_FALSE;
3133 // function to determine is all of a given ship type are destroyed
3134 int sexp_ship_type_destroyed( int n )
3139 percent = atoi(CTEXT(n));
3140 shiptype = CTEXT(CDR(n));
3142 for ( type = 0; type < MAX_SHIP_TYPE_COUNTS; type++ ) {
3143 if ( !stricmp( Ship_type_names[type], shiptype) )
3147 // bogus if we reach the end of this array!!!!
3148 if ( type == MAX_SHIP_TYPE_COUNTS ) {
3153 if ( Ship_counts[type].total == 0 )
3156 // determine if the percentage of killed/total is >= percentage given in the expression
3157 if ( (Ship_counts[type].killed * 100 / Ship_counts[type].total) >= percent)
3158 return SEXP_KNOWN_TRUE;
3164 // following are time based functions
3165 int sexp_has_time_elapsed(int n)
3167 int time = num_eval(n);
3169 if ( f2i(Missiontime) >= time )
3170 return SEXP_KNOWN_TRUE;
3175 // next function returns the time into the mission
3176 int sexp_mission_time()
3178 return f2i(Missiontime);
3181 // returns percent of length of distance to special warpout plane
3182 int sexp_special_warp_dist( int n)
3188 ship_name = CTEXT(n);
3190 // check to see if either ship was destroyed or departed. If so, then make this node known
3192 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ) {
3193 return SEXP_NAN_FOREVER;
3197 shipnum = ship_name_lookup(ship_name);
3202 // check that ship has warpout_objnum
3203 if (Ships[shipnum].special_warp_objnum == -1) {
3207 Assert( (Ships[shipnum].special_warp_objnum >= 0) && (Ships[shipnum].special_warp_objnum < MAX_OBJECTS));
3208 if ( (Ships[shipnum].special_warp_objnum < 0) && (Ships[shipnum].special_warp_objnum >= MAX_OBJECTS) ) {
3212 // check the special warpout device is valid
3214 object *ship_objp = &Objects[Ships[shipnum].objnum];
3215 object *warp_objp = &Objects[Ships[shipnum].special_warp_objnum];
3216 if (warp_objp->type == OBJ_SHIP) {
3217 if (Ship_info[Ships[warp_objp->instance].ship_info_index].flags & SIF_KNOSSOS_DEVICE) {
3226 // check if within 45 degree half-angle cone of facing
3227 float dot = fl_abs(vm_vec_dotprod(&warp_objp->orient.fvec, &ship_objp->orient.fvec));
3234 float dist = fvi_ray_plane(&hit_pt, &warp_objp->pos, &warp_objp->orient.fvec, &ship_objp->pos, &ship_objp->orient.fvec, 0.0f);
3235 polymodel *pm = model_get(Ships[shipnum].modelnum);
3238 // return as a percent of length
3239 return (int) (100.0f * dist / ship_get_length(&Ships[shipnum]));
3243 int sexp_time_destroyed(int n)
3247 if ( !mission_log_get_time( LOG_SHIP_DESTROYED, CTEXT(n), NULL, &time) ){ // returns 0 when not found
3254 int sexp_time_wing_destroyed(int n)
3258 if ( !mission_log_get_time( LOG_WING_DESTROYED, CTEXT(n), NULL, &time) ){
3265 int sexp_time_docked(int n)
3268 char *docker = CTEXT(n);
3269 char *dockee = CTEXT(CDR(n));
3270 int count = atoi(CTEXT(CDR(CDR(n))));
3272 Assert ( count > 0 );
3273 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) ){
3280 int sexp_time_undocked(int n)
3283 char *docker = CTEXT(n);
3284 char *dockee = CTEXT(CDR(n));
3285 int count = atoi(CTEXT(CDR(CDR(n))));
3287 Assert ( count > 0 );
3288 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ){
3295 int sexp_time_ship_arrived(int n)
3300 if ( !mission_log_get_time( LOG_SHIP_ARRIVE, CTEXT(n), NULL, &time ) ){
3307 int sexp_time_wing_arrived(int n)
3312 if ( !mission_log_get_time( LOG_WING_ARRIVE, CTEXT(n), NULL, &time ) ){
3319 int sexp_time_ship_departed(int n)
3324 if ( !mission_log_get_time( LOG_SHIP_DEPART, CTEXT(n), NULL, &time ) ){
3331 int sexp_time_wing_departed(int n)
3336 if ( !mission_log_get_time( LOG_WING_DEPART, CTEXT(n), NULL, &time ) ){
3343 // function to return the remaining shields as a percentage of the given ship.
3344 int sexp_shields_left(int n)
3346 int shipnum, percent;
3349 shipname = CTEXT(n);
3351 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3352 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3353 return SEXP_NAN_FOREVER;
3356 shipnum = ship_name_lookup( shipname );
3357 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3361 // now return the amount of shields left as a percentage of the whole.
3362 percent = (int)(get_shield_strength(&Objects[Ships[shipnum].objnum]) / Ship_info[Ships[shipnum].ship_info_index].shields * 100.0f);
3366 // function to return the remaining hits left as a percentage of the whole. This hit amount counts for
3367 // all hits on the ship (hull + subsystems). Use hits_left_hull to find hull hits remaining.
3368 int sexp_hits_left(int n)
3370 int shipnum, percent;
3373 shipname = CTEXT(n);
3375 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3376 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3377 return SEXP_NAN_FOREVER;
3380 shipnum = ship_name_lookup( shipname );
3381 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3385 // now return the amount of hits left as a percentage of the whole. Subtract the percentage from 100
3386 // since we are working with total hit points taken, not total remaining.
3387 ship *shipp = &Ships[shipnum];
3388 ship_info *sip = &Ship_info[shipp->ship_info_index];
3389 object *objp = &Objects[shipp->objnum];
3390 percent = (int) (100.0f * objp->hull_strength / sip->initial_hull_strength);
3394 // is ship visible on radar
3395 // returns 0 - not visible
3396 // returns 1 - marginally targetable (jiggly on radar)
3397 // returns 2 - fully targetable
3398 int sexp_is_ship_visible(int n)
3402 int ship_is_visible = 0;
3404 // if multiplayer, bail
3405 if (Game_mode & GM_MULTIPLAYER) {
3406 return SEXP_NAN_FOREVER;
3409 shipname = CTEXT(n);
3411 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3412 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3413 return SEXP_NAN_FOREVER;
3416 shipnum = ship_name_lookup( shipname );
3417 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3421 // get ship's *radar* visiblity
3422 if (Player_ship != NULL) {
3423 if (ship_is_visible_by_team(shipnum, Player_ship->team)) {
3424 ship_is_visible = 2;
3428 // only check awacs level if ship is not visible by team
3429 if (Player_ship != NULL && !ship_is_visible) {
3430 float awacs_level = awacs_get_level(&Objects[Ships[shipnum].objnum], Player_ship);
3431 if (awacs_level >= 1.0f) {
3432 ship_is_visible = 2;
3433 } else if (awacs_level > 0) {
3434 ship_is_visible = 1;
3438 return ship_is_visible;
3441 // get multi team v team score
3442 // if not multi team v team return 0
3443 // if invalid team return 0
3444 int sexp_team_score(int node)
3447 if (Game_mode & GM_MULTIPLAYER) {
3448 if (Netgame.type_flags & NG_TYPE_TEAM) {
3450 int team = atoi(CTEXT(node));
3453 return Multi_team0_score;
3454 } else if (team == 2) {
3455 return Multi_team1_score;
3457 // invalid team index
3468 // function to return the remaining hits left on a subsystem as a percentage of thw whole.
3469 int sexp_hits_left_subsystem(int n)
3471 int shipnum, percent, type;
3475 shipname = CTEXT(n);
3477 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3478 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3479 return SEXP_NAN_FOREVER;
3482 shipnum = ship_name_lookup( shipname );
3483 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3487 subsys_name = CTEXT(CDR(n));
3488 type = ai_get_subsystem_type( subsys_name );
3489 if ( (type >= 0) && (type < SUBSYSTEM_MAX) ) {
3490 // return as a percentage the hits remaining on the subsystem as a whole (i.e. for 3 engines,
3491 // we are returning the sum of the hits on the 3 engines)
3492 if (type == SUBSYSTEM_UNKNOWN) {
3493 // find the ship subsystem by searching ship's subsys_list
3495 ss = GET_FIRST( &Ships[shipnum].subsys_list );
3496 while ( ss != END_OF_LIST( &Ships[shipnum].subsys_list ) ) {
3498 if ( !stricmp(ss->system_info->subobj_name, subsys_name)) {
3499 percent = (int) (ss->current_hits / ss->system_info->max_hits * 100.0f);
3503 ss = GET_NEXT( ss );
3505 // we reached end of ship subsys list without finding subsys_name
3509 percent = (int)(ship_get_subsystem_strength(&Ships[shipnum],type) * 100.0f);
3513 return SEXP_NAN; // if for some strange reason, the type field of the subsystem is bogus
3516 int sexp_determine_team(char *subj)
3520 if (!stricmp(subj, "<any friendly>")){
3521 team = TEAM_FRIENDLY;
3522 } else if (!stricmp(subj, "<any hostile>")){
3523 team = TEAM_HOSTILE;
3524 } else if (!stricmp(subj, "<any neutral>")){
3525 team = TEAM_NEUTRAL;
3526 } else if (!stricmp(subj, "<any unknown>")){
3527 team = TEAM_UNKNOWN;
3528 } else if (!stricmp(subj, "<any traitor>")){
3529 team = TEAM_TRAITOR;
3535 // returns the distance between two objects. If a wing is specificed as one (or both) or the arguments
3536 // to this function, we are looking for the closest distance
3537 int sexp_distance(int n)
3539 int i, team, obj, dist, dist_min = 0, inited = 0;
3540 char *sname1, *sname2;
3545 sname2 = CTEXT(CDR(n));
3547 // check to see if either ship was destroyed or departed. If so, then make this node known
3549 if ( mission_log_get_time(LOG_SHIP_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname1, NULL, NULL) ||
3550 mission_log_get_time(LOG_SHIP_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname2, NULL, NULL) )
3551 return SEXP_NAN_FOREVER;
3553 // one of the names might be the name of a wing. Check to see if the wing is detroyed or departed
3554 if ( mission_log_get_time(LOG_WING_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname1, NULL, NULL) ||
3555 mission_log_get_time(LOG_WING_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname2, NULL, NULL) )
3556 return SEXP_NAN_FOREVER;
3558 team = sexp_determine_team(sname1);
3559 if (team) { // we have a team type, so check all ships of that type
3560 so = GET_FIRST(&Ship_obj_list);
3561 while (so != END_OF_LIST(&Ship_obj_list)) {
3562 if (Ships[Objects[so->objnum].instance].team == team) {
3564 dist = sexp_distance2(obj, sname2);
3565 if (dist != SEXP_NAN) {
3566 if (!inited || (dist < dist_min)) {
3576 if (!inited) // no objects were checked
3582 // at this point, we must have a wing, ship or point for a subj
3583 obj = ship_name_lookup(sname1);
3585 return sexp_distance2(Ships[obj].objnum, sname2);
3587 // at this point, we must have a wing or point for a subj
3588 obj = waypoint_lookup(sname1);
3590 return sexp_distance2(obj, sname2);
3592 // at this point, we must have a wing for a subj
3593 obj = wing_name_lookup(sname1);
3595 return SEXP_NAN; // we apparently don't have anything legal
3597 wingp = &Wings[obj];
3598 for (i=0; i<wingp->current_count; i++) {
3599 obj = Ships[wingp->ship_index[i]].objnum;
3600 dist = sexp_distance2(obj, sname2);
3601 if (dist != SEXP_NAN) {
3602 if (!inited || (dist < dist_min)) {
3609 if (!inited) // no objects were checked
3615 // check distance between a given ship and a given subject (ship, wing, any <team>).
3616 int sexp_distance2(int obj1, char *subj)
3618 int i, team, obj2, dist, dist_min = 0, inited = 0;
3622 team = sexp_determine_team(subj);
3623 if (team) { // we have a team type, so check all ships of that type
3624 so = GET_FIRST(&Ship_obj_list);
3625 while (so != END_OF_LIST(&Ship_obj_list)) {
3626 if (Ships[Objects[so->objnum].instance].team == team) {
3628 dist = sexp_distance3(obj1, obj2);
3629 if (dist != SEXP_NAN) {
3630 if (!inited || (dist < dist_min)) {
3640 if (!inited) // no objects were checked
3646 // at this point, we must have a wing, ship or point for a subj
3647 obj2 = ship_name_lookup(subj);
3649 return sexp_distance3(obj1, Ships[obj2].objnum);
3651 // at this point, we must have a wing or point for a subj
3652 obj2 = waypoint_lookup(subj);
3654 return sexp_distance3(obj1, obj2);
3656 // at this point, we must have a wing for a subj
3657 obj2 = wing_name_lookup(subj);
3659 return SEXP_NAN; // we apparently don't have anything legal
3661 wingp = &Wings[obj2];
3662 for (i=0; i<wingp->current_count; i++) {
3663 obj2 = Ships[wingp->ship_index[i]].objnum;
3664 dist = sexp_distance3(obj1, obj2);
3665 if (dist != SEXP_NAN) {
3666 if (!inited || (dist < dist_min)) {
3673 if (!inited) // no objects were checked
3679 // check distance between two given objects
3680 int sexp_distance3(int obj1, int obj2)
3682 if ( (obj1 == -1) || (obj2 == -1) ) // if either object isn't present in the mission now
3683 return SEXP_NAN; // return a really small number
3685 if ( (Objects[obj1].type == OBJ_SHIP) && (Objects[obj2].type == OBJ_SHIP) ) {
3686 if (OBJ_INDEX(Player_obj) == obj1)
3687 return (int) hud_find_target_distance( &Objects[obj2], &Objects[obj1] );
3689 return (int) hud_find_target_distance( &Objects[obj1], &Objects[obj2] );
3692 return (int) vm_vec_dist_quick( &Objects[obj1].pos, &Objects[obj2].pos );
3696 // funciton to determine when the last meaningful order was given to one or more ships. Returns
3697 // true or false depending on whether or not a meaningful order was received
3698 int sexp_last_order_time( int n )
3705 time = i2f(atoi(CTEXT(n)));
3706 Assert ( time >= 0 );
3711 instance = ship_name_lookup(name);
3712 if ( instance != -1 ) {
3713 aigp = Ai_info[Ships[instance].ai_index].goals;
3715 instance = wing_name_lookup(name);
3716 if ( instance == -1 ) // if we cannot find ship or wing, return 0
3718 aigp = Wings[instance].ai_goals;
3721 // with the ship, check the ai_goals structure for this ship and determine if there are any
3722 // orders which are < time seconds since current mission time
3723 for ( i = 0; i < MAX_AI_GOALS; i++ ) {
3726 mode = aigp->ai_mode;
3727 if ( (mode != AI_GOAL_NONE) && (mode != AI_GOAL_WARP) )
3728 if ( (aigp->time + time) > Missiontime )
3732 if ( i == MAX_AI_GOALS )
3741 // sexpression to return the number of players in the mission
3742 int sexp_num_players()
3748 for ( objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
3749 if ( (objp->type == OBJ_SHIP) && (objp->flags & OF_PLAYER_SHIP) )
3756 // expression to determine if the current skill level of the game is at least
3757 // the skill level given in the sexpression
3758 int sexp_skill_level_at_least( int n )
3763 level_name = CTEXT(n);
3764 for (i = 0; i < NUM_SKILL_LEVELS; i++ ) {
3765 if ( !stricmp(level_name, Skill_level_names(i, 0)) ) {
3766 if ( Game_skill_level >= i ){
3774 // return 0 if not found!!!
3778 int sexp_was_promotion_granted(int n)
3780 if (Player->flags & PLAYER_FLAGS_PROMOTED)
3786 int sexp_was_medal_granted(int n)
3792 if (Player->stats.m_medal_earned >= 0)
3798 medal_name = CTEXT(n);
3799 for (i=0; i<NUM_MEDALS; i++) {
3800 if (!stricmp(medal_name, Medals[i].name))
3804 if ( (i < NUM_MEDALS) && (Player->stats.m_medal_earned == i) )
3810 // function which returns true if the percentage of ships (and ships in wings) departed is at
3811 // least the percentage given. what determine if we should check destroyed or departed status
3812 int sexp_percent_ships_depart_destroy(int n, int what)
3818 percent = atoi(CTEXT(n));
3822 // iterate through the rest of the ships/wings in the list and tally the departures and the
3824 for ( n = CDR(n); n != -1; n = CDR(n) ) {
3829 wingnum = wing_name_lookup( name, 1 );
3830 if ( wingnum != -1 ) {
3831 // for wings, we can increment the total by the total number of ships that we expect for
3832 // this wing, and the departures by the number of departures stored for this wing
3833 total += (Wings[wingnum].wave_count * Wings[wingnum].num_waves);
3834 if ( what == OP_PERCENT_SHIPS_DEPARTED )
3835 count += Wings[wingnum].total_departed;
3836 else if ( what == OP_PERCENT_SHIPS_DESTROYED )
3837 count += Wings[wingnum].total_destroyed;
3839 Int3(); // this would be very bogus!
3841 // must be a ship, so increment the total by 1, then determine if this ship has departed
3843 if ( what == OP_PERCENT_SHIPS_DEPARTED ) {
3844 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) )
3846 } else if ( what == OP_PERCENT_SHIPS_DESTROYED ) {
3847 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3850 Int3(); // this would be very bogus as well.
3855 // now, look at the percentage
3856 if ( ((count * 100) / total) >= percent )
3857 return SEXP_KNOWN_TRUE;
3862 // function to tell is a list of ships has departed from within a radius of a given jump node.
3863 // returns true N seconds after the list of ships have departed
3864 int sexp_depart_node_delay( int n )
3866 int delay, count, num_departed;
3867 char *jump_node_name, *name;
3868 fix latest_time, this_time;
3870 delay = atoi( CTEXT(n) );
3872 jump_node_name = CTEXT(n);
3874 // iterate through the list of ships
3883 if (sexp_query_has_yet_to_arrive(name))
3884 return SEXP_CANT_EVAL;
3886 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
3887 // the sexpression is not true.
3888 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3889 return SEXP_KNOWN_FALSE;
3890 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, jump_node_name, &this_time) ) {
3892 if ( this_time > latest_time )
3893 latest_time = this_time;
3898 if ( (count == num_departed) && ((Missiontime - latest_time) >= delay) )
3899 return SEXP_KNOWN_TRUE;
3904 // sexpression which returns true when the listed ships/wings have all been destroyed or
3906 int sexp_destroyed_departed_delay( int n )
3909 fix delay, latest_time;
3913 delay = i2f(atoi(CTEXT(n)));
3916 count = 0; // number destroyed or departed
3917 total = 0; // total number of ships/wings to check
3926 // for wings, check the WF_GONE flag to see if there are no more ships in this wing to arrive.
3927 wingnum = wing_name_lookup(name, 1);
3928 if ( wingnum != -1 ) {
3929 if ( Wings[wingnum].flags & WF_WING_GONE ) {
3930 // be sure to get the latest time of one of these
3931 if ( Wings[wingnum].time_gone > latest_time ){
3932 time_gone = Wings[wingnum].time_gone;
3936 } else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time_gone) ) {
3938 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time_gone) ) {
3942 // check our latest time
3943 if ( time_gone > latest_time ){
3944 latest_time = time_gone;
3950 if ( (count == total) && (Missiontime > (latest_time + delay)) )
3951 return SEXP_KNOWN_TRUE;
3956 int sexp_special_warpout_name( int node )
3958 int shipnum, knossos_num;
3959 char *ship_name, *knossos;
3961 ship_name = CTEXT(node);
3962 knossos = CTEXT(CDR(node));
3964 // check to see if either ship was destroyed or departed. If so, then make this node known
3966 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ||
3967 mission_log_get_time(LOG_SHIP_DESTROYED, knossos, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, knossos, NULL, NULL) )
3968 return SEXP_NAN_FOREVER;
3971 shipnum = ship_name_lookup(ship_name);
3977 knossos_num = ship_name_lookup(knossos);
3978 if (knossos_num < 0) {
3982 // set special warpout objnum
3983 Ships[shipnum].special_warp_objnum = knossos_num;
3988 // function which determines if N seconds have elpased since all discovery of all cargo
3990 int sexp_is_cargo_known( int n, int check_delay )
3992 int count, shipnum, num_known, delay;
4000 // get the delay value (if there is one)
4002 if ( check_delay ) {
4003 delay = atoi(CTEXT(n) );
4015 // see if we have already checked this entry
4016 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4023 // see if the ship has already exited the mission (either through departure or destruction). If so,
4024 // grab the status of whether the cargo is known from this list
4025 exited_index = ship_find_exited_ship_by_name( name );
4026 if (exited_index != -1 ) {
4027 if ( !(Ships_exited[exited_index].flags & SEF_CARGO_KNOWN) )
4028 return SEXP_KNOWN_FALSE;
4030 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4031 // but won't cause huge delays.
4032 time_known = Missiontime - Ships_exited[exited_index].time;
4033 if ( f2i(time_known) >= delay )
4037 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4038 // is yet to arrive.
4039 shipnum = ship_name_lookup( name );
4040 if ( shipnum != -1 ) {
4041 if ( Ships[shipnum].flags & SF_CARGO_REVEALED ) {
4042 time_known = Missiontime - Ships[shipnum].time_cargo_revealed;
4043 if ( f2i(time_known) >= delay )
4050 // if cargo is known, mark our variable and this sexpression.
4053 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4059 Directive_count += count - num_known;
4060 if ( count == num_known )
4061 return SEXP_KNOWN_TRUE;
4066 int sexp_has_been_tagged_delay(int n)
4068 int count, shipnum, num_known, delay;
4076 // get the delay value
4077 delay = atoi(CTEXT(n) );
4089 // see if we have already checked this entry
4090 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4097 // see if the ship has already exited the mission (either through departure or destruction). If so,
4098 // grab the status of whether the cargo is known from this list
4099 exited_index = ship_find_exited_ship_by_name( name );
4100 if (exited_index != -1 ) {
4101 if ( !(Ships_exited[exited_index].flags & SEF_BEEN_TAGGED) )
4102 return SEXP_KNOWN_FALSE;
4104 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4105 // but won't cause huge delays.
4106 time_known = Missiontime - Ships_exited[exited_index].time;
4107 if ( f2i(time_known) >= delay )
4111 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4112 // is yet to arrive.
4113 shipnum = ship_name_lookup( name );
4114 if ( shipnum != -1 ) {
4115 if ( Ships[shipnum].time_first_tagged != 0 ) {
4116 time_known = Missiontime - Ships[shipnum].time_first_tagged;
4117 if ( f2i(time_known) >= delay )
4124 // if cargo is known, mark our variable and this sexpression.
4127 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4133 Directive_count += count - num_known;
4134 if ( count == num_known )
4135 return SEXP_KNOWN_TRUE;
4140 int sexp_cap_subsys_cargo_known_delay(int n)
4142 int delay, count, delta_time, num_known;
4143 char *ship_name, *subsys_name;
4149 delay = atoi(CTEXT(n));
4153 ship_name = CTEXT(n);
4165 // see if we have already checked this entry
4166 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4170 subsys_name = CTEXT(n);
4172 logged = mission_log_get_time(LOG_CAP_SUBSYS_CARGO_REVEALED, ship_name, subsys_name, &time_known);
4174 delta_time = f2i(Missiontime - time_known);
4175 if (delta_time >= delay) {
4180 // if (exited or destroyed) and not logged, known false
4181 // otherwise, still out there and cargo not yet known
4182 if ( (mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL )) && !logged ) {
4183 return SEXP_KNOWN_FALSE;
4189 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4195 Directive_count += count - num_known;
4196 if ( count == num_known )
4197 return SEXP_KNOWN_TRUE;
4203 // return object index of waypoint or -1 if no such waypoint
4204 int waypoint_lookup(char *name)
4210 ptr = GET_FIRST(&obj_used_list);
4211 while (ptr != END_OF_LIST(&obj_used_list)) {
4212 if (ptr->type == OBJ_WAYPOINT) {
4214 sprintf(buf, "%s:%d", Waypoint_lists[i / 65536].name, (i & 0xffff) + 1);
4215 if ( !stricmp(buf, name) )
4216 return OBJ_INDEX(ptr);
4219 ptr = GET_NEXT(ptr);
4225 // conditional sexpressions follow
4227 // eval_when evaluates the when conditional
4228 int eval_when(int n)
4232 Assert( n >= 0 ); // must have valid sexp index
4235 val = eval_sexp(cond); // get the value of the the conditional
4236 if ( val ) { // if the value is true, perform the actions is the 'then' part of the if
4240 while ( actions != -1 ) {
4243 val = eval_sexp(exp); // these sexp evaled only for side effects
4244 actions = CDR(actions);
4248 if (Sexp_nodes[cond].value == SEXP_KNOWN_FALSE)
4249 return SEXP_KNOWN_FALSE; // no need to waste time on this anymore
4251 if (val == SEXP_KNOWN_FALSE)
4252 return 0; // can't return known false, as this would bypass future actions under the when
4257 // eval_cond() evaluates the cond conditional
4258 int eval_cond( int n )
4260 int cond = 0, node, val = 0;
4266 val = eval_sexp(cond);
4268 // if the conditional evaluated to true, then we must evaluate the rest of the expression returning
4269 // the value of this evaluation
4274 actions = CDR(node);
4275 while (actions >= 0) {
4278 val = eval_sexp(exp); // these sexp evaled only for side effects
4280 actions = CDR(actions);
4286 // move onto the next cond clause
4293 int sexp_is_iff( int n )
4295 char *ship_name, *iff;
4300 // iff value is the first parameter, second is a list of one or more ships to check to see if the
4301 // iff value matches
4303 if ( !stricmp(iff, "friendly") )
4304 team = TEAM_FRIENDLY;
4305 else if ( !stricmp(iff, "hostile") )
4306 team = TEAM_HOSTILE;
4307 else if ( !stricmp(iff, "neutral") )
4308 team = TEAM_NEUTRAL;
4309 else if ( !stricmp(iff, "unknown") )
4310 team = TEAM_UNKNOWN;
4311 else if ( !stricmp(iff, "traitor") )
4312 team = TEAM_TRAITOR;
4315 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", iff));
4316 team = TEAM_HOSTILE;
4321 ship_name = CTEXT(n);
4322 // find the ship and check to be sure that it is still around.
4323 num = ship_name_lookup(ship_name);
4324 if ( num < 0 ) // if the ship is gone, can't check it's iff.
4327 // if the team doesn't match the team specified, return 0 immediately
4328 if ( Ships[num].team != team)
4337 void sexp_change_iff( int n )
4339 char *ship_name, *new_iff;
4344 if ( !stricmp(new_iff, "friendly") )
4345 new_team = TEAM_FRIENDLY;
4346 else if ( !stricmp(new_iff, "hostile") )
4347 new_team = TEAM_HOSTILE;
4348 else if ( !stricmp(new_iff, "neutral") )
4349 new_team = TEAM_NEUTRAL;
4350 else if ( !stricmp(new_iff, "unknown") )
4351 new_team = TEAM_UNKNOWN;
4352 else if ( !stricmp(new_iff, "traitor") )
4353 new_team = TEAM_TRAITOR;
4355 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", new_iff));
4356 new_team = TEAM_HOSTILE;
4362 ship_name = CTEXT(n);
4364 // find the ship and check to be sure that it is still around.
4365 num = ship_name_lookup(ship_name);
4366 if ( num >= 0 ) { // only change iff if we found the ship
4367 Ships[num].team = new_team;
4369 // send a network packet if we need to
4370 if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Ships[num].objnum >= 0)){
4371 send_change_iff_packet(Objects[Ships[num].objnum].net_signature, new_team);
4380 // following routine adds an ai goal to a ship structure. The sexpression index
4381 // passed in should be an ai-goal of the proper form. The code in MissionGoal should
4382 // check the syntax.
4384 void sexp_add_ship_goal( int n )
4390 ship_name = CTEXT(n);
4391 num = ship_name_lookup(ship_name);
4392 if ( num < 0 ) // ship not around anymore???? then forget it!
4396 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4399 // identical to above, except add a wing
4400 void sexp_add_wing_goal( int n )
4406 wing_name = CTEXT(n);
4407 num = wing_name_lookup(wing_name);
4408 if ( num < 0 ) // ship not around anymore???? then forget it!
4412 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4415 // sexp_add_goal adds a goal to the specified entiry (ships and wings have unique names between
4417 void sexp_add_goal( int n )
4426 // first, look for ship name -- if found, then add ship goal. else look for wing name -- if
4427 // found, add wing goal
4428 if ( (num = ship_name_lookup(name)) != -1 )
4429 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4430 else if ( (num = wing_name_lookup(name)) != -1 )
4431 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4434 // clears out all ai goals for a ship
4435 void sexp_clear_ship_goals( int n )
4441 ship_name = CTEXT(n);
4442 num = ship_name_lookup(ship_name);
4443 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4446 // clears out ai goals for a wing
4447 void sexp_clear_wing_goals( int n )
4453 wing_name = CTEXT(n);
4454 num = wing_name_lookup(wing_name);
4457 ai_clear_wing_goals( num );
4460 // this function clears all ai goals for the given ship or wing
4461 void sexp_clear_goals( int n )
4469 if ( (num = ship_name_lookup(name)) != -1 )
4470 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4471 else if ( (num = wing_name_lookup(name)) != -1 )
4472 ai_clear_wing_goals( num );
4478 // this function get called by send-message or send-message random with the name of the message, sender,
4480 void sexp_send_one_message( char *name, char *who_from, char *priority, int group, int delay )
4482 int ipriority, num, ship_index, source;
4489 // determine the priority of the message
4490 if ( !stricmp(priority, "low") )
4491 ipriority = MESSAGE_PRIORITY_LOW;
4492 else if ( !stricmp(priority, "normal") )
4493 ipriority = MESSAGE_PRIORITY_NORMAL;
4494 else if ( !stricmp(priority, "high") )
4495 ipriority = MESSAGE_PRIORITY_HIGH;
4498 ipriority = MESSAGE_PRIORITY_NORMAL;
4501 // check to see if the 'who_from' string is a ship that had been destroyed or departed. If so,
4502 // then don't send the message. We must look at 'who_from' to determine what to look for. who_from
4503 // may be any allied person, any wingman, a wingman from a specific wing, or a specific ship
4506 source = MESSAGE_SOURCE_COMMAND;
4507 if ( who_from[0] == '#' ) {
4508 message_send_unique_to_player( name, &(who_from[1]), MESSAGE_SOURCE_SPECIAL, ipriority, group, delay );
4510 } else if (!stricmp(who_from, "<any allied>")) {
4511 //Int3(); // no longer supported
4513 } else if ( (num = wing_name_lookup(who_from)) != -1 ) {
4514 // message from a wing
4515 // this will be an invalid case soon
4517 // choose wing leader to speak for wing (hence "1" at end of ship_get_random_ship_in_wing)
4518 ship_index = ship_get_random_ship_in_wing( num, SHIP_GET_NO_PLAYERS, 1 );
4519 if ( ship_index == -1 ) {
4520 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4524 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_SHIP_DEPART, who_from, NULL, NULL)
4525 || mission_log_get_time(LOG_WING_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_WING_DEPART, who_from, NULL, NULL) ) {
4526 // getting into this if statement means that the ship or wing (sender) is no longer in the mission
4527 // if message is high priority, make it come from Terran Command
4528 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4531 source = MESSAGE_SOURCE_COMMAND;
4533 } else if ( !stricmp(who_from, "<any wingman>") || (wing_name_lookup(who_from) != -1) ) {
4534 source = MESSAGE_SOURCE_WINGMAN;
4536 // Message from a apecific ship
4537 // bail if not high priority, otherwise reroute to command
4538 source = MESSAGE_SOURCE_SHIP;
4539 ship_index = ship_name_lookup(who_from);
4540 if ( ship_index == -1 ) {
4541 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4543 source = MESSAGE_SOURCE_COMMAND;
4547 if ( ship_index == -1 ){
4550 shipp = &Ships[ship_index];
4553 message_send_unique_to_player( name, shipp, source, ipriority, group, delay );
4556 void sexp_send_message( int n )
4558 char *name, *who_from, *priority, *tmp;
4565 who_from = CTEXT(n);
4566 priority = CTEXT(CDR(n));
4567 name = CTEXT(CDR(CDR(n)));
4569 // a temporary check to see if the name field matched a priority since I am in the process
4570 // of reordering the arguments
4571 if ( !stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high") ) {
4577 sexp_send_one_message( name, who_from, priority, 0, 0 );
4580 void sexp_send_message_list( int n )
4582 char *name, *who_from, *priority;
4589 // send a bunch of messages
4592 who_from = CTEXT(n);
4597 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4600 priority = CTEXT(n);
4605 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4613 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4616 delay += atoi(CTEXT(n));
4619 sexp_send_one_message(name, who_from, priority, 1, delay);
4626 void sexp_send_random_message( int n )
4628 char *name, *who_from, *priority;
4629 int temp, num_messages, message_num;
4632 who_from = CTEXT(n);
4633 priority = CTEXT(CDR(n));
4639 // count the number of messages that we have
4647 Assert ( num_messages >= 1 );
4649 // get a random message, and pass the parameters to send_one_message
4650 message_num = myrand() % num_messages;
4653 if ( message_num == 0 )
4658 Assert (n != -1); // should have found the message!!!
4661 sexp_send_one_message( name, who_from, priority, 0, 0 );
4664 void sexp_self_destruct( int n )
4670 // get the ship name and be sure that it is still in the mission. Destroy it if we find it
4671 ship_name = CTEXT(n);
4672 shipnum = ship_name_lookup( ship_name );
4673 if ( shipnum == -1 )
4675 ship_self_destruct( &Objects[Ships[shipnum].objnum] );
4681 void sexp_next_mission( int n )
4686 mission_name = CTEXT(n);
4687 for (i = 0; i < Campaign.num_missions; i++) {
4688 if ( !stricmp(Campaign.missions[i].name, mission_name) ) {
4689 Campaign.next_mission = i;
4693 Error(LOCATION, "Mission name %s not found in campaign file for next-mission command", mission_name);
4696 // function to deal with the end-of-campaign sexpression.
4697 void sexp_end_of_campaign( int n )
4699 // this is really a do-nothing sexpression. It is pretty much a placeholder to allow
4700 // campaigns to have repeat-mission branches at the end of the campaign. By not setting
4701 // anything in this function, the higher level campaign code will see this as end-of-campaign
4702 // since next_mission isn't set to anything. (To be safe, we'll set to -1).
4703 Campaign.next_mission = -1;
4706 // sexpression to end everything. One parameter is the movie to play when this is over.
4707 void sexp_end_campaign( int n )
4709 // post and event to move us to the end-of-campaign state. There we will play a movie, then
4710 // go to debriefing.
4711 // gameseq_post_event( GS_EVENT_END_CAMPAIGN );
4713 // in FS2 our ending is a bit wacky. we'll just flag the mission as having ended the campaign
4714 Campaign_ended_in_mission = 1;
4717 // sabotage subsystem reduces the strength of a subsystem by the given percentage. If it is reduced to
4718 // below 0%, then the hits of the subsystem are set to 0
4719 void sexp_sabotage_subsystem( int n )
4721 char *shipname, *subsystem;
4722 int percentage, shipnum, index;
4723 float sabotage_hits;
4727 shipname = CTEXT(n);
4728 subsystem = CTEXT(CDR(n));
4729 percentage = atoi(CTEXT(CDR(CDR(n))));
4731 shipnum = ship_name_lookup(shipname);
4733 // if no ship, then return immediately.
4734 if ( shipnum == -1 )
4736 shipp = &Ships[shipnum];
4738 // see if we are dealing with the HULL
4739 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4743 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4744 sabotage_hits = ihs * ((float)percentage / 100.0f);
4745 objp = &Objects[shipp->objnum];
4746 objp->hull_strength -= sabotage_hits;
4748 // self destruct the ship if <= 0.
4749 if ( objp->hull_strength <= 0.0f )
4750 ship_self_destruct( objp );
4754 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4755 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4757 index = ship_get_subsys_index(shipp, subsystem);
4758 if ( index == -1 ) {
4759 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for sabotage subsystem\n", subsystem, shipp->ship_name));
4763 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4764 // set the strength to the given percentage if current strength is > given percentage
4765 ss = ship_get_indexed_subsys( shipp, index );
4766 sabotage_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4767 ss->current_hits -= sabotage_hits;
4768 if ( ss->current_hits < 0.0f )
4769 ss->current_hits = 0.0f;
4770 ship_recalc_subsys_strength( shipp );
4773 // repair_subsystem adds some percentage of hits to a subsystem. Anything repaired about 100% is
4775 void sexp_repair_subsystem( int n )
4777 char *shipname, *subsystem;
4778 int percentage, shipnum, index;
4783 shipname = CTEXT(n);
4784 subsystem = CTEXT(CDR(n));
4785 shipnum = ship_name_lookup(shipname);
4787 // if no ship, then return immediately.
4788 if ( shipnum == -1 ) {
4791 shipp = &Ships[shipnum];
4793 // check if we've got a number or an op
4794 if ( CAR(CDR(CDR(n))) != -1) {
4795 percentage = eval_sexp( CAR(CDR(CDR(n))) );
4797 percentage = atoi(CTEXT(CDR(CDR(n))));
4800 // see if we are dealing with the HULL
4801 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4805 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4806 repair_hits = ihs * ((float)percentage / 100.0f);
4807 objp = &Objects[shipp->objnum];
4808 objp->hull_strength += repair_hits;
4809 if ( objp->hull_strength > ihs )
4810 objp->hull_strength = ihs;
4814 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4815 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4817 index = ship_get_subsys_index(shipp, subsystem);
4818 if ( index == -1 ) {
4819 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4823 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4824 // set the strength to the given percentage if current strength is < given percentage
4825 ss = ship_get_indexed_subsys( shipp, index );
4826 repair_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4827 ss->current_hits += repair_hits;
4828 if ( ss->current_hits > ss->system_info->max_hits )
4829 ss->current_hits = ss->system_info->max_hits;
4830 ship_recalc_subsys_strength( shipp );
4833 // sexpression code to set a subsystem of a ship at a specific percentage
4834 void sexp_set_subsystem_strength( int n )
4836 char *shipname, *subsystem;
4837 int percentage, shipnum, index;
4841 shipname = CTEXT(n);
4842 subsystem = CTEXT(CDR(n));
4843 percentage = num_eval(CDR(CDR(n)));
4845 shipnum = ship_name_lookup(shipname);
4847 // if no ship, then return immediately.
4848 if ( shipnum == -1 )
4850 shipp = &Ships[shipnum];
4852 if ( percentage > 100 ) {
4853 nprintf(("Warning", "percentage for set_subsystem_strength > 100 -- setting to 100\n"));
4855 } else if ( percentage < 0 ) {
4856 nprintf(("Werning", "percantage for set_subsystem_strength < 0 -- setting to 0\n"));
4860 // see if we are dealing with the HULL
4861 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4865 objp = &Objects[shipp->objnum];
4867 // destroy the ship if percentage is 0
4868 if ( percentage == 0 ) {
4869 ship_self_destruct( objp );
4871 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4872 objp->hull_strength = ihs * ((float)percentage / 100.0f);
4878 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4879 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4881 index = ship_get_subsys_index(shipp, subsystem);
4882 if ( index == -1 ) {
4883 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4887 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4888 // set the strength to the given percentage if current strength is < given percentage
4889 ss = ship_get_indexed_subsys( shipp, index );
4891 // maybe blow up subsys
4892 if (ss->current_hits > 0) {
4893 if (percentage < 1) {
4894 do_subobj_destroyed_stuff(shipp, ss, NULL);
4899 ss->current_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4901 ship_recalc_subsys_strength( shipp );
4904 // function which changes the validity of a goal. The flag paramater tells us whether to mark the goals
4905 // as valid or invalid
4906 void sexp_change_goal_validity( int n, int flag )
4913 mission_goal_mark_valid( name );
4915 mission_goal_mark_invalid( name );
4921 // function to transfer cargo from one ship to another
4922 void sexp_transfer_cargo( int n )
4924 char *shipname1, *shipname2;
4925 int shipnum1, shipnum2, i;
4928 shipname1 = CTEXT(n);
4929 shipname2 = CTEXT(CDR(n));
4931 // find the ships -- if neither in the mission, the abort
4932 shipnum1 = ship_name_lookup(shipname1);
4933 shipnum2 = ship_name_lookup(shipname2);
4934 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4937 // we must be sure that these two objects are indeed docked
4938 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
4939 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
4940 Int3(); // you are trying to transfer cargo between two ships not docked
4944 if ( !stricmp(Cargo_names[Ships[shipnum1].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4945 Int3(); // you are transfering no cargo!!!!
4949 // transfer cargo from ship1 to ship2
4951 // Don't give warning for large ships (cruiser on up)
4952 if (! (Ship_info[Ships[shipnum2].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
4953 if ( stricmp(Cargo_names[Ships[shipnum2].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4954 Warning(LOCATION, "Transfering cargo to %s which already\nhas cargo %s.\nCargo will be replaced", Ships[shipnum2].ship_name, Cargo_names[Ships[shipnum2].cargo1 & CARGO_INDEX_MASK] );
4958 Ships[shipnum2].cargo1 = char(Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
4960 if ( !(Ships[shipnum1].cargo1 & CARGO_NO_DEPLETE) ) {
4961 // need to set ship1's cargo to nothing. scan the cargo_names array looking for the string nothing.
4962 // add it if not found
4963 for (i = 0; i < Num_cargo; i++ ) {
4964 if ( !stricmp(Cargo_names[i], "nothing") ) {
4965 Ships[shipnum1].cargo1 = char(i);
4969 strcpy(Cargo_names[i], "Nothing");
4974 // this function exchanges cargo between two ships
4975 void sexp_exchange_cargo( int n )
4977 char *shipname1, *shipname2;
4978 int shipnum1, shipnum2, temp;
4981 shipname1 = CTEXT(n);
4982 shipname2 = CTEXT(CDR(n));
4984 // find the ships -- if neither in the mission, the abort
4985 shipnum1 = ship_name_lookup(shipname1);
4986 shipnum2 = ship_name_lookup(shipname2);
4987 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4990 // we must be sure that these two objects are indeed docked
4991 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
4992 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
4993 Int3(); // you are trying to transfer cargo between two ships not docked
4997 temp = (Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
4998 Ships[shipnum1].cargo1 = char(Ships[shipnum2].cargo1 & CARGO_INDEX_MASK);
4999 Ships[shipnum2].cargo1 = char(temp);
5002 void sexp_cap_waypont_speed(int n)
5008 shipname = CTEXT(n);
5009 speed = atoi(CTEXT(CDR(n)));
5011 shipnum = ship_name_lookup(shipname);
5013 if (shipnum == -1) {
5014 Int3(); // trying to set waypoint speed of ship not already in game
5018 // cap speed to range (-1, 127) to store within char
5027 Ai_info[Ships[shipnum].ai_index].waypoint_speed_cap = (char) speed;
5030 // this function causes a ship to jettison its cargo
5031 void sexp_jettison_cargo( int n )
5034 int jettison_delay, ship_index;
5037 shipname = CTEXT(n);
5038 jettison_delay = atoi(CTEXT(CDR(n)));
5041 ship_index = ship_name_lookup(shipname);
5047 ship_jettison_cargo(&Ships[ship_index]);
5050 void sexp_cargo_no_deplete( int n )
5053 int ship_index, no_deplete = 1;
5056 shipname = CTEXT(n);
5059 ship_index = ship_name_lookup(shipname);
5064 if ( !(Ship_info[Ships[ship_index].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
5065 Warning(LOCATION, "Trying to make non BIG or HUGE ship %s with non-depletable cargo.\n", Ships[ship_index].ship_name);
5070 no_deplete = atoi(CTEXT(CDR(n)));
5071 Assert((no_deplete == 0) || (no_deplete == 1));
5072 if ( (no_deplete != 0) && (no_deplete != 1) ) {
5078 Ships[ship_index].cargo1 |= CARGO_NO_DEPLETE;
5080 Ships[ship_index].cargo1 &= (~CARGO_NO_DEPLETE);
5085 // sexpression to end the mission after N seconds!
5086 void sexp_end_mission_delay( int n )
5090 //delay = atoi(CTEXT(n));
5091 //mission_parse_set_end_time( delay );
5092 mprintf(("Not ending mission -- end-mission sexpression no longer works!\n"));
5095 // funciton to toggle the status bit for the AI code which tells the AI if it is a good time
5096 // to rearm. The status being set means good time. Status not being set (unset), means bad time.
5097 // designers must implement this.
5098 void sexp_good_time_to_rearm( int n )
5103 team_name = CTEXT(n);
5104 time = atoi(CTEXT(CDR(n))); // this is the time for how long a good rearm is active -- in seconds
5105 for ( i = 0; i < Num_team_names; i++ ) {
5106 if ( !stricmp(team_name, Team_names[i]) ) {
5110 ai_set_rearm_status( team, time );
5115 // function which grants promotion to the player
5116 void sexp_grant_promotion()
5118 // short circuit multiplayer for now until we figure out what to do.
5119 if ( Game_mode & GM_MULTIPLAYER )
5122 // set a bit to tell player should get promoted at the end of the mission. I suppose the other
5123 // thing that we could do would be to set the players score to at least the amount of
5124 // points for the next level, but this way is better I think.
5125 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5126 Player->flags |= PLAYER_FLAGS_PROMOTED;
5130 // function which gives the named medal to the players in the mission
5131 void sexp_grant_medal( int n )
5136 // don't give medals in normal gameplay when not in campaign mode
5137 if ( (Game_mode & GM_NORMAL) && !(Game_mode & GM_CAMPAIGN_MODE) )
5140 Assert(Player->stats.m_medal_earned < 0); // Mission has problems. Tried to grant 2 medals in 1 mission.
5141 medal_name = CTEXT(n);
5142 for (i = 0; i < NUM_MEDALS; i++ ) {
5143 if ( !stricmp(medal_name, Medals[i].name) )
5147 if ( i < NUM_MEDALS ) {
5148 Player->stats.m_medal_earned = i;
5149 if ( Game_mode & GM_MULTIPLAYER ) {
5150 for ( j = 0; j < MAX_PLAYERS; j++ ) {
5151 if ( MULTI_CONNECTED(Net_players[j]) ) {
5152 Net_players[j].player->stats.m_medal_earned = i;
5159 void sexp_tech_add_ship(int node)
5165 // this function doesn't mean anything when not in campaign mode
5166 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5171 i = ship_info_lookup(name);
5173 Ship_info[i].flags |= SIF_IN_TECH_DATABASE;
5175 Error(LOCATION, "Ship class \"%s\" invalid", name);
5181 void sexp_tech_add_weapon(int node)
5187 // this function doesn't mean anything when not in campaign mode
5188 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5193 i = weapon_info_lookup(name);
5195 Weapon_info[i].wi_flags |= WIF_IN_TECH_DATABASE;
5197 Error(LOCATION, "Ship class \"%s\" invalid", name);
5203 // function to set variables needed to grant a new ship/weapon to the player during the course
5205 void sexp_allow_ship( int n )
5210 // this function doesn't mean anything when not in campaign mode
5211 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5214 // get the name of the ship and lookup up the ship_info index for it
5216 sindex = ship_info_lookup( name );
5220 // now we have a valid index --
5221 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_SHIP, sindex );
5224 void sexp_allow_weapon( int n )
5229 // this function doesn't mean anything when not in campaign mode
5230 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5233 // get the name of the weapon and lookup up the weapon_info index for it
5235 sindex = weapon_info_lookup( name );
5239 // now we have a valid index --
5240 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_WEAPON, sindex );
5243 // functions to deal with breaking/fixing the warp engines on ships/wings. should_break is true when
5244 // we are breaking the warp drive (can be repaired). The parameter is 0 when is getting broken (i.e.
5245 // can be fixed by repair). The repair parameter tells us whether we are clearing the destroyed or broken
5246 // flag (1), or setting them (0).
5247 void sexp_deal_with_warp( int n, int should_break, int nix )
5252 for ( ; n != -1; n = CDR(n) ) {
5254 index = ship_name_lookup(name);
5256 // check to see if ship destroyed or departed. In either case, do nothing.
5257 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
5260 // we can only operate on ships which are in the mission
5261 if ( index != -1 ) {
5263 // set the flag value accoring to whether we are destroying the warp or just breaking it
5265 flag = SF_WARP_BROKEN;
5267 flag = SF_WARP_NEVER;
5270 Ships[index].flags |= flag;
5272 Ships[index].flags &= ~flag;
5275 // maybe this ship has yet to arrive. Get a possible parse object and set the flag if found
5278 pobjp = mission_parse_get_arrival_ship( name );
5280 if ( pobjp == NULL ) {
5281 Int3(); // warning, find allender -- should be impossible
5286 flag = P_SF_WARP_BROKEN;
5288 flag = P_SF_WARP_BROKEN;
5291 pobjp->flags |= flag;
5293 pobjp->flags &= ~flag;
5299 // function which is used to tell the AI when it is okay to fire certain secondary
5300 // weapons at other ships.
5301 void sexp_good_secondary_time( int n )
5303 char *team_name, *weapon_name, *ship_name;
5304 int num_weapons, weapon_index, team, i;
5306 team_name = CTEXT(n);
5307 num_weapons = atoi(CTEXT(CDR(n)));
5308 weapon_name = CTEXT(CDR(CDR(n)));
5309 ship_name = CTEXT(CDR(CDR(CDR(n))));
5311 weapon_index = weapon_info_lookup(weapon_name);
5312 if ( weapon_index == -1 ) {
5313 nprintf(("Warning", "couldn't find weapon %s for good-secondary-time\n", weapon_name));
5317 // get the team type from the team_name
5318 for ( i = 0; i < Num_team_names; i++ ) {
5319 if ( !stricmp(Team_names[i], team_name) )
5322 if ( i == Num_team_names ) {
5323 nprintf(("Warning", "couldn't find team %s for good-secondary-time\n", team_name ));
5326 team = (1<<i); // this is the magic formula to get to a team type.
5328 // see if the ship has departed or has been destroyed. If so, then we don't need to set up the
5330 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5333 ai_good_secondary_time( team, weapon_index, num_weapons, ship_name );
5337 // function to deal with getting status of goals for previous missions (in the current campaign).
5338 // the status parameter is used to tell this function if we are looking for a goal_satisfied, goal_failed,
5339 // or goal incomplete event
5340 int sexp_previous_goal_status( int n, int status )
5342 char rval = 0, *mission_name;
5344 int i, mission_num, default_value = 0, use_defaults = 1;
5346 mission_name = CTEXT(n);
5347 goal_name = CTEXT(CDR(n));
5349 // check for possible next optional argument
5352 default_value = eval_sexp(n);
5355 // try to find the given mission name in the current list of missions in the campaign.
5356 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5357 i = mission_campaign_find_mission( mission_name );
5360 // if mission not found, assume that goal was false (so previous-goal-false returns true)
5361 nprintf(("General", "Couldn't find mission name %s in current campaign's list of missions.\nReturning %s for goal-status function.", mission_name, (status==GOAL_COMPLETE)?"false":"true"));
5362 if ( status == GOAL_COMPLETE )
5363 rval = SEXP_KNOWN_FALSE;
5365 rval = SEXP_KNOWN_TRUE;
5368 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5371 // now try and find the goal this mission
5373 for (i = 0; i < Campaign.missions[mission_num].num_goals; i++) {
5374 if ( !stricmp(Campaign.missions[mission_num].goals[i].name, goal_name) )
5378 if ( i == Campaign.missions[mission_num].num_goals ) {
5379 Warning(LOCATION, "Couldn't find goal name %s in mission %s.\nReturning %s for goal-true function.", goal_name, mission_name, (status==GOAL_COMPLETE)?"false":"true");
5380 if ( status == GOAL_COMPLETE )
5381 rval = SEXP_KNOWN_FALSE;
5383 rval = SEXP_KNOWN_TRUE;
5386 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5387 if ( Campaign.missions[mission_num].goals[i].status == status )
5388 rval = SEXP_KNOWN_TRUE;
5390 rval = SEXP_KNOWN_FALSE;
5398 // when not in campaign mode, always return KNOWN_TRUE when looking for goal complete, and KNOWN_FALSE
5401 if ( default_value )
5402 rval = SEXP_KNOWN_TRUE;
5404 rval = SEXP_KNOWN_FALSE;
5406 if ( status == GOAL_COMPLETE )
5407 rval = SEXP_KNOWN_TRUE;
5409 rval = SEXP_KNOWN_FALSE;
5416 // sexpression which gets the status of an event from a previous mission. Like the above function but
5417 // dealing with events instead of goals. Again, the status parameter tells the code if we are looking
5418 // for an event_true, event_false, or event_incomplete status
5419 int sexp_previous_event_status( int n, int status )
5421 char rval = 0, *mission_name;
5423 int i, mission_num, default_value = 0, use_defaults = 1;
5425 mission_name = CTEXT(n);
5426 name = CTEXT(CDR(n));
5428 // check for possible optional parameter
5431 default_value = eval_sexp(n);
5434 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5435 // following function returns -1 when mission isn't found.
5436 i = mission_campaign_find_mission( mission_name );
5438 // if the mission name wasn't found -- make this return FALSE for the event status.
5440 nprintf(("General", "Couldn't find mission name %s in current campaign's list of missions.\nReturning %s for event-status function.", mission_name, (status==EVENT_SATISFIED)?"false":"true"));
5441 if ( status == EVENT_SATISFIED ) {
5442 rval = SEXP_KNOWN_FALSE;
5444 rval = SEXP_KNOWN_TRUE;
5448 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5451 // now try and find the goal this mission
5453 for (i = 0; i < Campaign.missions[mission_num].num_events; i++) {
5454 if ( !stricmp(Campaign.missions[mission_num].events[i].name, name) )
5458 if ( i == Campaign.missions[mission_num].num_events ) {
5459 Warning(LOCATION, "Couldn't find event name %s in mission %s.\nReturning %s for event_status function.", name, mission_name, (status==EVENT_SATISFIED)?"false":"true");
5460 if ( status == EVENT_SATISFIED )
5461 rval = SEXP_KNOWN_FALSE;
5463 rval = SEXP_KNOWN_TRUE;
5466 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5467 if ( Campaign.missions[mission_num].events[i].status == status )
5468 rval = SEXP_KNOWN_TRUE;
5470 rval = SEXP_KNOWN_FALSE;
5479 if ( default_value )
5480 rval = SEXP_KNOWN_TRUE;
5482 rval = SEXP_KNOWN_FALSE;
5484 if ( status == EVENT_SATISFIED )
5485 rval = SEXP_KNOWN_TRUE;
5487 rval = SEXP_KNOWN_FALSE;
5494 // function to return the status of an event in the current mission. The passed parameter indicates
5495 // if we are checking whether the event is true or the event is false.
5496 int sexp_event_status( int n, int want_true )
5502 for (i = 0; i < Num_mission_events; i++ ) {
5503 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5504 if ( !stricmp(Mission_events[i].name, name) ) {
5505 result = Mission_events[i].result;
5506 if (Mission_events[i].formula < 0) {
5507 if ( (want_true && result) || (!want_true && !result) )
5508 return SEXP_KNOWN_TRUE;
5510 return SEXP_KNOWN_FALSE;
5513 if ( (want_true && result) || (!want_true && !result) )
5524 // function to return the status of an event N seconds after the event is true or false. Similar
5525 // to above function but waits N seconds before returning true
5526 int sexp_event_delay_status( int n, int want_true )
5533 delay = i2f(num_eval(CDR(n)));
5534 for (i = 0; i < Num_mission_events; i++ ) {
5535 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5536 if ( !stricmp(Mission_events[i].name, name) ) {
5537 if ( (fix) Mission_events[i].timestamp + delay >= Missiontime )
5540 result = Mission_events[i].result;
5541 if (Mission_events[i].formula < 0) {
5542 if ( (want_true && result) || (!want_true && !result) )
5543 return SEXP_KNOWN_TRUE;
5545 return SEXP_KNOWN_FALSE;
5548 if ( want_true && result ) //) || (!want_true && !result) )
5559 // function which returns true if the given event is still incomplete
5560 int sexp_event_incomplete( int n )
5567 for (i = 0; i < Num_mission_events; i++ ) {
5568 if ( !stricmp(Mission_events[i].name, name ) ) {
5569 // if the formula is still >= 0 (meaning it is still getting eval'ed), then
5570 // the event is incomplete
5571 if ( Mission_events[i].formula != -1 )
5574 return SEXP_KNOWN_FALSE;
5581 // function to return the status of an goal N seconds after the goal is true or false. Similar
5582 // to above function but operates on goals instead of events
5583 int sexp_goal_delay_status( int n, int want_true )
5589 delay = i2f(num_eval(CDR(n)));
5592 // if we are looking for a goal true entry and we find a false, then return known false here
5593 if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, NULL) )
5594 return SEXP_KNOWN_FALSE;
5595 else if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, &time) ) {
5596 if ( (Missiontime - time) >= delay )
5597 return SEXP_KNOWN_TRUE;
5600 // if we are looking for a goal false entry and we find a true, then return known false here
5601 if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, NULL) )
5602 return SEXP_KNOWN_FALSE;
5603 else if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, &time) ) {
5604 if ( (Missiontime - time) >= delay )
5605 return SEXP_KNOWN_TRUE;
5612 // function which returns true if the given goal is still incomplete
5613 int sexp_goal_incomplete( int n )
5619 if ( mission_log_get_time( LOG_GOAL_SATISFIED, name, NULL, NULL) || mission_log_get_time( LOG_GOAL_FAILED, name, NULL, NULL) )
5620 return SEXP_KNOWN_FALSE;
5626 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5627 // or cleared (flag==0)
5628 void sexp_protect_ships( int n, int flag )
5634 ship_name = CTEXT(n);
5636 // check to see if ship destroyed or departed. In either case, do nothing.
5637 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5640 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5641 // in a list until created
5642 num = ship_name_lookup(ship_name);
5645 Objects[Ships[num].objnum].flags |= OF_PROTECTED;
5647 Objects[Ships[num].objnum].flags &= ~OF_PROTECTED;
5649 p_object *parse_obj;
5651 parse_obj = mission_parse_get_arrival_ship( ship_name );
5654 parse_obj->flags |= P_OF_PROTECTED;
5656 parse_obj->flags &= ~P_OF_PROTECTED;
5660 Int3(); // get allender -- could be a potential problem here
5669 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5670 // or cleared (flag==0)
5671 void sexp_beam_protect_ships( int n, int flag )
5677 ship_name = CTEXT(n);
5679 // check to see if ship destroyed or departed. In either case, do nothing.
5680 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5683 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5684 // in a list until created
5685 num = ship_name_lookup(ship_name);
5688 Objects[Ships[num].objnum].flags |= OF_BEAM_PROTECTED;
5690 Objects[Ships[num].objnum].flags &= ~OF_BEAM_PROTECTED;
5692 p_object *parse_obj;
5694 parse_obj = mission_parse_get_arrival_ship( ship_name );
5697 parse_obj->flags |= P_OF_BEAM_PROTECTED;
5699 parse_obj->flags &= ~P_OF_BEAM_PROTECTED;
5703 Int3(); // get allender -- could be a potential problem here
5712 // sexpression to make ships "visible" and "invisible" to sensors. The visible parameter is true
5713 // when making ships visible, false otherwise
5714 void sexp_ships_visible( int n, int visible )
5719 for ( ; n != -1; n = CDR(n) ) {
5720 ship_name = CTEXT(n);
5722 // check to see if ship destroyed or departed. In either case, do nothing.
5723 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5726 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5727 // in a list until created
5728 num = ship_name_lookup(ship_name);
5731 Ships[num].flags |= SF_HIDDEN_FROM_SENSORS;
5733 Ships[num].flags &= ~SF_HIDDEN_FROM_SENSORS;
5734 if (Ships[num].flags & SF_ESCORT) {
5735 // SEND add escort request
5736 hud_add_ship_to_escort(Ships[num].objnum, 1);
5741 p_object *parse_obj;
5743 parse_obj = mission_parse_get_arrival_ship( ship_name );
5746 parse_obj->flags |= P_SF_HIDDEN_FROM_SENSORS;
5748 parse_obj->flags &= ~P_SF_HIDDEN_FROM_SENSORS;
5752 Int3(); // get allender -- could be a potential problem here
5759 // sexpression to toggle invulnerability flag of ships.
5760 void sexp_ships_invulnerable( int n, int invulnerable )
5766 for ( ; n != -1; n = CDR(n) ) {
5767 ship_name = CTEXT(n);
5769 // check to see if ship destroyed or departed. In either case, do nothing.
5770 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5773 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5774 // in a list until created
5775 num = ship_name_lookup(ship_name);
5777 objp = &Objects[Ships[num].objnum];
5779 objp->flags |= OF_INVULNERABLE;
5781 objp->flags &= ~OF_INVULNERABLE;
5783 p_object *parse_obj;
5785 parse_obj = mission_parse_get_arrival_ship( ship_name );
5788 parse_obj->flags |= P_SF_INVULNERABLE;
5790 parse_obj->flags &= ~P_SF_INVULNERABLE;
5794 Int3(); // get allender -- could be a potential problem here
5801 // sexpression to toggle KEEP ALIVE flag of ship object
5802 void sexp_ships_guardian( int n, int guardian )
5808 for ( ; n != -1; n = CDR(n) ) {
5809 ship_name = CTEXT(n);
5811 // check to see if ship destroyed or departed. In either case, do nothing.
5812 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5815 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5816 // in a list until created
5817 num = ship_name_lookup(ship_name);
5819 objp = &Objects[Ships[num].objnum];
5821 objp->flags |= OF_GUARDIAN;
5823 objp->flags &= ~OF_GUARDIAN;
5825 p_object *parse_obj;
5827 parse_obj = mission_parse_get_arrival_ship( ship_name );
5830 parse_obj->flags |= P_SF_GUARDIAN;
5832 parse_obj->flags &= ~P_SF_GUARDIAN;
5836 Int3(); // get allender -- could be a potential problem here
5843 // make ship vanish without a trace (and what its docked to)
5844 void ship_vanished(int);
5845 void sexp_ship_vanish( int n )
5848 object *objp, *docked_objp;
5851 // if MULTIPLAYER bail
5852 if (Game_mode & GM_MULTIPLAYER) {
5856 for ( ; n != -1; n = CDR(n) ) {
5857 ship_name = CTEXT(n);
5859 // check to see if ship destroyed or departed. In either case, do nothing.
5860 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5863 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5864 // in a list until created
5865 num = ship_name_lookup(ship_name);
5867 objp = &Objects[Ships[num].objnum];
5870 docked_objp = ai_find_docked_object( objp );
5873 objp->flags |= OF_SHOULD_BE_DEAD;
5880 docked_objp->flags |= OF_SHOULD_BE_DEAD;
5883 ship_vanished(docked_objp->instance);
5889 int sexp_key_pressed(int node)
5894 z = translate_key_to_index(CTEXT(node));
5899 if (!Control_config[z].used){
5907 t = atoi(CTEXT(CDR(node)));
5908 return timestamp_has_time_elapsed(Control_config[z].used, t * 1000);
5911 void sexp_key_reset(int node)
5916 z = translate_key_to_index(CTEXT(node));
5918 Control_config[z].used = 0;
5921 int sexp_targeted(int node)
5926 z = ship_query_state(CTEXT(node));
5928 return SEXP_KNOWN_FALSE; // ship isn't around, nor will it ever be
5929 } else if (z == -1) {
5930 return SEXP_CANT_EVAL;
5933 z = ship_name_lookup(CTEXT(node), 1);
5934 if ((z < 0) || !Player_ai || (Ships[z].objnum != Player_ai->target_objnum)){
5938 if (CDR(node) >= 0) {
5939 z = atoi(CTEXT(CDR(node))) * 1000;
5940 if (!timestamp_has_time_elapsed(Players_target_timestamp, z)){
5944 if (CDR(CDR(node)) >= 0) {
5945 ptr = Player_ai->targeted_subsys;
5946 if (!ptr || stricmp(ptr->system_info->subobj_name, CTEXT(CDR(CDR(node))))){
5955 int sexp_speed(int node)
5957 if (Training_context & TRAINING_CONTEXT_SPEED) {
5958 if (Training_context_speed_set){
5959 if (timestamp_has_time_elapsed(Training_context_speed_timestamp, atoi(CTEXT(node)) * 1000)){
5960 return SEXP_KNOWN_TRUE;
5968 int sexp_secondaries_depleted(int node)
5970 int sindex, num_banks, num_depleted_banks;
5974 sindex = ship_name_lookup(CTEXT(node));
5979 shipp = &Ships[sindex];
5980 if (shipp->objnum < 0) {
5984 // get num secondary banks
5985 num_banks = shipp->weapons.num_secondary_banks;
5986 num_depleted_banks = 0;
5988 // get number of depleted banks
5989 for (int idx=0; idx<num_banks; idx++) {
5990 if (shipp->weapons.secondary_bank_ammo[idx] == 0) {
5991 num_depleted_banks++;
5995 // are they all depleted?
5996 return (num_depleted_banks == num_banks);
5999 int sexp_facing(int node)
6005 if (ship_query_state(CTEXT(node)) < 0){
6006 return SEXP_KNOWN_FALSE;
6009 sh = ship_name_lookup(CTEXT(node));
6010 if ((sh < 0) || !Player_obj){
6014 obj = Ships[sh].objnum;
6015 v1 = Player_obj->orient.fvec;
6016 vm_vec_normalize(&v1);
6017 vm_vec_sub(&v2, &Objects[obj].pos, &Player_obj->pos);
6018 vm_vec_normalize(&v2);
6019 a1 = vm_vec_dotprod(&v1, &v2);
6020 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6028 // is ship facing first waypoint in waypoint path
6029 int sexp_facing2(int node)
6035 // bail if Player_obj is not good
6037 return SEXP_CANT_EVAL;
6041 v1 = Player_obj->orient.fvec;
6042 vm_vec_normalize(&v1);
6044 // get waypoint name
6045 char *waypoint_name = CTEXT(node);
6047 // get position of first waypoint
6049 for (i=0; i<Num_waypoint_lists; i++) {
6050 if (!stricmp(waypoint_name, Waypoint_lists[i].name)) {
6056 if (wp_index == -1) {
6057 return SEXP_CANT_EVAL;
6060 // Waypoint_lists[wp_index].waypoints[0]
6062 vm_vec_sub(&v2, &Waypoint_lists[wp_index].waypoints[0], &Player_obj->pos);
6063 vm_vec_normalize(&v2);
6064 a1 = vm_vec_dotprod(&v1, &v2);
6065 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6073 int sexp_order(int node)
6078 int sexp_waypoint_missed()
6080 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6081 if (Training_context_at_waypoint > Training_context_goal_waypoint){
6089 int sexp_waypoint_twice()
6091 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6092 if (Training_context_at_waypoint < Training_context_goal_waypoint - 1){
6100 int sexp_path_flown()
6102 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6103 if (Training_context_goal_waypoint == Waypoint_lists[Training_context_path].count){
6111 void sexp_send_training_message(int node)
6113 int t = -1, delay = 0;
6120 Assert(Event_index >= 0);
6122 if ((CDR(node) >= 0) && (CDR(CDR(node)) >= 0)) {
6123 delay = atoi(CTEXT(CDR(CDR(node)))) * 1000;
6124 t = CDR(CDR(CDR(node)));
6130 if ((Mission_events[Event_index].repeat_count > 1) || (CDR(node) < 0)){
6131 message_training_que(CTEXT(node), timestamp(delay), t);
6133 message_training_que(CTEXT(CDR(node)), timestamp(delay), t);
6136 // if (Training_msg_method)
6137 // gameseq_post_event(GS_EVENT_TRAINING_PAUSE);
6140 int sexp_shield_recharge_pct(int node)
6144 // get the firing ship
6145 sindex = ship_name_lookup(CTEXT(node));
6149 if(Ships[sindex].objnum < 0){
6153 // shield recharge pct
6154 return (int)(100.0f * Energy_levels[Ships[sindex].shield_recharge_index]);
6157 int sexp_engine_recharge_pct(int node)
6161 // get the firing ship
6162 sindex = ship_name_lookup(CTEXT(node));
6166 if(Ships[sindex].objnum < 0){
6170 // shield recharge pct
6171 return (int)(100.0f * Energy_levels[Ships[sindex].engine_recharge_index]);
6174 int sexp_weapon_recharge_pct(int node)
6178 // get the firing ship
6179 sindex = ship_name_lookup(CTEXT(node));
6183 if(Ships[sindex].objnum < 0){
6187 // shield recharge pct
6188 return (int)(100.0f * Energy_levels[Ships[sindex].weapon_recharge_index]);
6191 int sexp_shield_quad_low(int node)
6194 float max_quad, check;
6199 sindex = ship_name_lookup(CTEXT(node));
6203 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6206 if((Ships[sindex].ship_info_index < 0) || (Ships[sindex].ship_info_index >= Num_ship_types)){
6209 objp = &Objects[Ships[sindex].objnum];
6210 sip = &Ship_info[Ships[sindex].ship_info_index];
6211 if(!(sip->flags & SIF_SMALL_SHIP)){
6214 max_quad = sip->shields / (float)MAX_SHIELD_SECTIONS;
6217 check = (float)atoi(CTEXT(CDR(node)));
6219 // check his quadrants
6220 for(idx=0; idx<MAX_SHIELD_SECTIONS; idx++){
6221 if( ((objp->shields[idx] / max_quad) * 100.0f) <= check ){
6230 int sexp_secondary_ammo_pct(int node)
6239 sindex = ship_name_lookup(CTEXT(node));
6243 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6246 shipp = &Ships[sindex];
6249 check = atoi(CTEXT(CDR(node)));
6251 // bogus check? (3 == cumulative sum of all banks)
6252 if((check != 3) && (check > shipp->weapons.num_secondary_banks)){
6258 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6259 ret_sum[idx] = (int)(((float)shipp->weapons.secondary_bank_ammo[idx] / (float)shipp->weapons.secondary_bank_start_ammo[idx]) * 100.0f);
6264 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6265 ret += ret_sum[idx];
6267 ret = (int)((float)ret / (float)shipp->weapons.num_secondary_banks);
6269 ret = (int)(((float)shipp->weapons.secondary_bank_ammo[check] / (float)shipp->weapons.secondary_bank_start_ammo[check]) * 100.0f);
6276 void sexp_beam_fire(int node)
6279 beam_fire_info fire_info;
6283 memset(&fire_info, 0, sizeof(beam_fire_info));
6284 fire_info.accuracy = 0.000001f; // this will guarantee a hit
6286 // get the firing ship
6287 sindex = ship_name_lookup(CTEXT(node));
6291 if(Ships[sindex].objnum < 0){
6294 fire_info.shooter = &Objects[Ships[sindex].objnum];
6296 // get the subsystem
6297 fire_info.turret = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6298 if(fire_info.turret == NULL){
6303 sindex = ship_name_lookup(CTEXT(CDR(CDR(node))));
6307 if(Ships[sindex].objnum < 0){
6310 fire_info.target = &Objects[Ships[sindex].objnum];
6312 // see if the optional subsystem can be found
6313 fire_info.target_subsys = NULL;
6314 fire_info.target_subsys = ship_get_subsys(&Ships[sindex], CTEXT(CDR(CDR(CDR(node)))));
6316 // if it has no primary weapons
6317 if(fire_info.turret->weapons.num_primary_banks <= 0){
6322 // if the turret is destroyed
6323 if(fire_info.turret->current_hits <= 0.0f){
6327 // hmm, this could be wacky. Let's just simply select the first beam weapon in the turret
6328 fire_info.beam_info_index = -1;
6329 for(idx=0; idx<fire_info.turret->weapons.num_primary_banks; idx++){
6330 // store the weapon info index
6331 if(Weapon_info[fire_info.turret->weapons.primary_bank_weapons[idx]].wi_flags & WIF_BEAM){
6332 fire_info.beam_info_index = fire_info.turret->weapons.primary_bank_weapons[idx];
6337 if(fire_info.beam_info_index != -1){
6338 beam_fire(&fire_info);
6340 // it would appear the turret doesn't have any beam weapons, dumbass
6345 void sexp_beam_free(int node)
6348 ship_subsys *turret = NULL;
6350 // get the firing ship
6351 sindex = ship_name_lookup(CTEXT(node));
6355 if(Ships[sindex].objnum < 0){
6361 // get the subsystem
6362 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6367 // flag it as beam free :)
6368 turret->weapons.flags |= SW_FLAG_BEAM_FREE;
6369 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6376 void sexp_beam_free_all(int node)
6378 ship_subsys *subsys;
6381 // get the firing ship
6382 sindex = ship_name_lookup(CTEXT(node));
6386 if(Ships[sindex].objnum < 0){
6390 // free all beam weapons
6391 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6392 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6393 // just mark all turrets as beam free
6394 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6395 subsys->weapons.flags |= SW_FLAG_BEAM_FREE;
6396 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6400 subsys = GET_NEXT(subsys);
6404 void sexp_beam_lock(int node)
6407 ship_subsys *turret = NULL;
6409 // get the firing ship
6410 sindex = ship_name_lookup(CTEXT(node));
6414 if(Ships[sindex].objnum < 0){
6420 // get the subsystem
6421 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6426 // flag it as not beam free
6427 turret->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6434 void sexp_beam_lock_all(int node)
6436 ship_subsys *subsys;
6439 // get the firing ship
6440 sindex = ship_name_lookup(CTEXT(node));
6444 if(Ships[sindex].objnum < 0){
6448 // free all beam weapons
6449 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6450 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6451 // just mark all turrets as not beam free
6452 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6453 subsys->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6457 subsys = GET_NEXT(subsys);
6461 void sexp_turret_free(int node)
6464 ship_subsys *turret = NULL;
6466 // get the firing ship
6467 sindex = ship_name_lookup(CTEXT(node));
6471 if(Ships[sindex].objnum < 0){
6477 // get the subsystem
6478 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6483 // flag turret as no longer locked :)
6484 turret->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6485 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6492 void sexp_turret_free_all(int node)
6494 ship_subsys *subsys;
6497 // get the firing ship
6498 sindex = ship_name_lookup(CTEXT(node));
6502 if(Ships[sindex].objnum < 0){
6507 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6508 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6509 // just mark all turrets as free
6510 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6511 subsys->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6512 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6516 subsys = GET_NEXT(subsys);
6520 void sexp_turret_lock(int node)
6523 ship_subsys *turret = NULL;
6525 // get the firing ship
6526 sindex = ship_name_lookup(CTEXT(node));
6530 if(Ships[sindex].objnum < 0){
6536 // get the subsystem
6537 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6542 // flag turret as locked
6543 turret->weapons.flags |= SW_FLAG_TURRET_LOCK;
6550 void sexp_turret_lock_all(int node)
6552 ship_subsys *subsys;
6555 // get the firing ship
6556 sindex = ship_name_lookup(CTEXT(node));
6560 if(Ships[sindex].objnum < 0){
6565 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6566 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6567 // just mark all turrets as locked
6568 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6569 subsys->weapons.flags |= SW_FLAG_TURRET_LOCK;
6573 subsys = GET_NEXT(subsys);
6577 void sexp_turret_tagged_only_all(int node)
6579 ship_subsys *subsys;
6582 // get the firing ship
6583 sindex = ship_name_lookup(CTEXT(node));
6587 if(Ships[sindex].objnum < 0){
6591 // mark all turrets to only target tagged ships
6592 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6593 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6594 // just mark all turrets as locked
6595 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6596 subsys->weapons.flags |= SW_FLAG_TAGGED_ONLY;
6600 subsys = GET_NEXT(subsys);
6604 void sexp_turret_tagged_clear_all(int node)
6606 ship_subsys *subsys;
6609 // get the firing ship
6610 sindex = ship_name_lookup(CTEXT(node));
6614 if(Ships[sindex].objnum < 0){
6618 // mark all turrets so not restricted to only tagged ships
6619 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6620 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6621 // just mark all turrets as locked
6622 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6623 subsys->weapons.flags &= (~SW_FLAG_TAGGED_ONLY);
6627 subsys = GET_NEXT(subsys);
6631 void sexp_add_remove_escort(int node)
6637 // get the firing ship
6638 sindex = ship_name_lookup(CTEXT(node));
6642 if(Ships[sindex].objnum < 0){
6646 // determine whether to add or remove it
6647 whee = CTEXT(CDR(node));
6648 flag = atoi(CTEXT(CDR(node)));
6652 hud_add_ship_to_escort(Ships[sindex].objnum, 1);
6654 hud_remove_ship_from_escort(Ships[sindex].objnum);
6658 void sexp_awacs_set_radius(int node)
6663 // get the firing ship
6664 sindex = ship_name_lookup(CTEXT(node));
6668 if(Ships[sindex].objnum < 0){
6672 // get the awacs subsystem
6673 awacs = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6678 // make sure this _is_ an awacs subsystem
6679 Assert(awacs->system_info->flags & MSS_FLAG_AWACS);
6680 if(awacs->system_info->flags & MSS_FLAG_AWACS){
6684 // set the new awacs radius
6685 awacs->awacs_radius = (float)atoi(CTEXT(CDR(CDR(node))));
6688 int sexp_is_tagged(int node)
6692 // get the firing ship
6693 sindex = ship_name_lookup(CTEXT(node));
6697 if(Ships[sindex].objnum < 0){
6700 if(Ships[sindex].tag_left > 0.0f){
6708 int sexp_num_kills(int node)
6710 int sindex, np_index;
6713 // get the ship we're interested in
6714 sindex = ship_name_lookup(CTEXT(node));
6718 if(Ships[sindex].objnum < 0){
6722 // in multiplayer, search through all players
6723 if(Game_mode & GM_MULTIPLAYER){
6724 // try and find the player
6725 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6726 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6727 p = Net_players[np_index].player;
6730 // if we're in single player, we're only concerned with ourself
6733 if(Player_obj == &Objects[Ships[sindex].objnum]){
6738 // now, if we have a valid player, return his kills
6740 return p->stats.m_kill_count_ok;
6747 int sexp_num_type_kills(int node)
6749 int sindex, np_index, st_index;
6753 // get the ship we're interested in
6754 sindex = ship_name_lookup(CTEXT(node));
6758 if(Ships[sindex].objnum < 0){
6762 // in multiplayer, search through all players
6763 if(Game_mode & GM_MULTIPLAYER){
6764 // try and find the player
6765 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6766 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6767 p = Net_players[np_index].player;
6770 // if we're in single player, we're only concerned with ourself
6773 if(Player_obj == &Objects[Ships[sindex].objnum]){
6783 // lookup ship type name
6784 st_index = ship_type_name_lookup(CTEXT(CDR(node)));
6791 for(idx=0; idx<Num_ship_types; idx++){
6792 if((p->stats.m_okKills[idx] > 0) && (Ship_info[idx].flags & Ship_type_flags[st_index])){
6793 total += p->stats.m_okKills[idx];
6801 int sexp_num_class_kills(int node)
6803 int sindex, np_index, si_index;
6806 // get the ship we're interested in
6807 sindex = ship_name_lookup(CTEXT(node));
6811 if(Ships[sindex].objnum < 0){
6815 // in multiplayer, search through all players
6816 if(Game_mode & GM_MULTIPLAYER){
6817 // try and find the player
6818 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6819 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6820 p = Net_players[np_index].player;
6823 // if we're in single player, we're only concerned with ourself
6826 if(Player_obj == &Objects[Ships[sindex].objnum]){
6836 // get the ship type we're looking for
6837 si_index = ship_info_lookup(CTEXT(CDR(node)));
6838 if((si_index < 0) || (si_index > Num_ship_types)){
6843 return p->stats.m_okKills[si_index];
6846 void sexp_subsys_set_random(int node)
6848 int sindex, low, high, n, idx, rand, exclusion_list[MAX_MODEL_SUBSYSTEMS];
6849 ship_subsys *subsys;
6853 sindex = ship_name_lookup(CTEXT(node));
6857 if(Ships[sindex].objnum < 0){
6860 shipp = &Ships[sindex];
6863 low = num_eval(CDR(node));
6869 high = num_eval(CDR(CDR(node)));
6879 n = CDR(CDR(CDR(node)));
6881 // init exclusion list
6882 memset(exclusion_list, 0, sizeof(int) * Ship_info[shipp->ship_info_index].n_subsystems);
6884 // get exclusion list
6886 int exclude_index = ship_get_subsys_index(shipp, CTEXT(n), 0);
6887 if (exclude_index >= 0) {
6888 exclusion_list[exclude_index] = 1;
6894 // apply to all others
6895 for (idx=0; idx<Ship_info[shipp->ship_info_index].n_subsystems; idx++) {
6896 if ( exclusion_list[idx] == 0 ) {
6897 // get non excluded subsystem
6898 subsys = ship_get_indexed_subsys(shipp, idx, NULL);
6900 // randomize its hit points
6901 rand = rand_internal(low, high);
6902 subsys->current_hits = 0.01f * rand * subsys->system_info->max_hits;
6907 void sexp_supernova_start(int node)
6909 supernova_start(atoi(CTEXT(node)));
6912 int sexp_is_secondary_selected(int node)
6919 sindex = ship_name_lookup(CTEXT(node));
6923 if(Ships[sindex].objnum < 0){
6926 shipp = &Ships[sindex];
6929 bank = atoi(CTEXT(CDR(node)));
6930 if(bank >= shipp->weapons.num_secondary_banks){
6934 // is this the bank currently selected
6935 if(bank == shipp->weapons.current_secondary_bank){
6943 int sexp_is_primary_selected(int node)
6950 sindex = ship_name_lookup(CTEXT(node));
6954 if(Ships[sindex].objnum < 0){
6957 shipp = &Ships[sindex];
6960 bank = atoi(CTEXT(CDR(node)));
6961 if(bank >= shipp->weapons.num_primary_banks){
6965 // is this the bank currently selected
6966 if(bank == shipp->weapons.current_primary_bank){
6974 #define RIGHT_QUAD 0
6975 #define FRONT_QUAD 1
6979 // Return SEXP_TRUE if quadrant quadnum is near max.
6980 int shield_quad_near_max(int quadnum)
6982 float remaining = 0.0f;
6983 for (int i=0; i<MAX_SHIELD_SECTIONS; i++) {
6987 remaining += Player_obj->shields[i];
6990 if ((remaining < 2.0f) || (Player_obj->shields[quadnum] > Ship_info[Player_ship->ship_info_index].shields/MAX_SHIELD_SECTIONS - 5.0f)) {
6997 // Return truth value for special SEXP.
6998 // Used in training#5, perhaps in other missions.
6999 int process_special_sexps(int index)
7002 case 0: // Ship "Freighter 1" is aspect locked by player.
7003 if (Player_ai->target_objnum != -1) {
7004 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
7005 if (Player_ai->current_target_is_locked)
7011 case 1: // Fired Interceptors
7013 for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
7014 if (objp->type == OBJ_WEAPON) {
7015 if (!stricmp(Weapon_info[Weapons[objp->instance].weapon_info_index].name, "Interceptor#weak")) {
7016 int target = Weapons[objp->instance].target_num;
7018 if (Objects[target].type == OBJ_SHIP) {
7019 if (!(stricmp(Ships[Objects[target].instance].ship_name, "Freighter 1")))
7027 case 2: // Ship "Freighter 1", subsystem "Weapons" is aspect locked by player.
7028 if (Player_ai->target_objnum != -1) {
7029 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
7030 if (!(stricmp(Player_ai->targeted_subsys->system_info->name, "Weapons"))) {
7031 if (Player_ai->current_target_is_locked){
7039 case 3: // Player ship suffering shield damage on front.
7040 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7041 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7044 case 4: // Player ship suffering much damage.
7045 nprintf(("AI", "Frame %i\n", Framecount));
7046 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7047 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7048 if (Player_obj->shields[FRONT_QUAD] < 2.0f)
7053 case 5: // Player's shield is quick repaired
7054 nprintf(("AI", "Frame %i, recharged to %7.3f\n", Framecount, Player_obj->shields[FRONT_QUAD]));
7056 apply_damage_to_shield(Player_obj, FRONT_QUAD, -flFrametime*200.0f);
7058 if (Player_obj->shields[FRONT_QUAD] > Ship_info[Player_ship->ship_info_index].shields/4.0f)
7059 Player_obj->shields[FRONT_QUAD] = Ship_info[Player_ship->ship_info_index].shields/4.0f;
7061 //hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7062 if (Player_obj->shields[FRONT_QUAD] > Player_obj->shields[(FRONT_QUAD+1)%4] - 2.0f)
7067 case 6: // 3 of player's shield quadrants are reduced to 0.
7068 Player_obj->shields[1] = 1.0f;
7069 Player_obj->shields[2] = 1.0f;
7070 Player_obj->shields[3] = 1.0f;
7071 //apply_damage_to_shield(Player_obj, FRONT_QUAD, 1.0f);
7072 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7074 case 7: // Make sure front quadrant has been maximized, or close to it.
7075 if (shield_quad_near_max(FRONT_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7078 case 8: // Make sure rear quadrant has been maximized, or close to it.
7079 if (shield_quad_near_max(REAR_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7082 case 9: // Zero left and right quadrants in preparation for maximizing rear quadrant.
7083 Player_obj->shields[LEFT_QUAD] = 0.0f;
7084 Player_obj->shields[RIGHT_QUAD] = 0.0f;
7085 hud_shield_quadrant_hit(Player_obj, LEFT_QUAD);
7089 case 10: // Return true if player is low on Interceptors.
7090 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 8)
7096 case 11: // Return true if player has plenty of Interceptors.
7097 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] >= 8)
7103 case 12: // Return true if player is low on Interceptors.
7104 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 4)
7110 case 13: // Zero front shield quadrant. Added for Jim Boone on August 26, 1999 by MK.
7111 Player_obj->shields[FRONT_QUAD] = 0.0f;
7112 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7116 case 100: // Return true if player is out of countermeasures.
7117 if (Player_ship->cmeasure_count <= 0)
7123 Int3(); // Unsupported node type.
7129 // custom sexp operator for handling misc training stuff
7130 int sexp_special_training_check(int node)
7134 num = atoi(CTEXT(node));
7135 if (num == SPECIAL_CHECK_TRAINING_FAILURE)
7136 return Training_failure ? SEXP_TRUE : SEXP_FALSE;
7138 // To MK: do whatever you want with this number here.
7139 rtn = process_special_sexps(atoi(CTEXT(node)));
7144 // sexpression to flash a hud gauge. gauge name is text valud of node
7145 void sexp_flash_hud_gauge( int node )
7151 for (i = 0; i < NUM_HUD_GAUGES; i++ ) {
7152 if ( !stricmp(HUD_gauge_text[i], name) ) {
7153 hud_gauge_start_flash(i); // call HUD function to flash gauge
7159 void sexp_set_training_context_fly_path(int node)
7163 for (i=0; i<Num_waypoint_lists; i++)
7164 if (!stricmp(CTEXT(node), Waypoint_lists[i].name))
7167 if (i < Num_waypoint_lists) {
7168 Training_context |= TRAINING_CONTEXT_FLY_PATH;
7169 Training_context_path = i;
7170 Training_context_distance = (float) atof(CTEXT(CDR(node)));
7171 Training_context_goal_waypoint = 0;
7172 Training_context_at_waypoint = -1;
7176 void sexp_set_training_context_speed(int node)
7178 Training_context |= TRAINING_CONTEXT_SPEED;
7179 Training_context_speed_min = atoi(CTEXT(node));
7180 Training_context_speed_max = atoi(CTEXT(CDR(node)));
7181 Training_context_speed_set = 0;
7184 // high-level sexpression evaluator
7185 int eval_sexp(int cur_node)
7187 int node, type, sexp_val = UNINITIALIZED;
7188 if (cur_node == -1) // empty list, i.e. sexp: ( )
7191 Assert(cur_node >= 0); // we have special sexp nodes <= -1!!! MWA
7192 // which should be intercepted before we get here. HOFFOSS
7193 type = SEXP_NODE_TYPE(cur_node);
7194 Assert( (type == SEXP_LIST) || (type == SEXP_ATOM) );
7196 // trap known true and known false sexpressions. We don't trap on SEXP_NAN sexpressions since
7197 // they may yet evaluate to true or false.
7199 if (Sexp_nodes[cur_node].value == SEXP_KNOWN_TRUE)
7201 else if (Sexp_nodes[cur_node].value == SEXP_KNOWN_FALSE)
7204 if (Sexp_nodes[cur_node].first != -1) {
7205 node = CAR(cur_node);
7206 sexp_val = eval_sexp(node);
7207 Sexp_nodes[cur_node].value = Sexp_nodes[node].value; // higher level node gets node value
7213 node = CDR(cur_node); // makes reading the next bit of code a little easier.
7215 op_num = find_operator(CTEXT(cur_node));
7217 // arithmetic operators will always return just their value
7219 sexp_val = add_sexps( node );
7223 sexp_val = sub_sexps( node );
7227 sexp_val = mul_sexps( node );
7231 sexp_val = mod_sexps( node );
7235 sexp_val = div_sexps( node );
7239 sexp_val = rand_sexp( node );
7242 // boolean operators can have one of the special sexp values (known true, known false, unknown)
7244 sexp_val = SEXP_KNOWN_TRUE;
7248 sexp_val = SEXP_KNOWN_FALSE;
7252 sexp_val = sexp_or( node );
7256 sexp_val = sexp_and( node );
7259 case OP_AND_IN_SEQUENCE:
7260 sexp_val = sexp_and_in_sequence( node );
7263 case OP_GREATER_THAN:
7264 sexp_val = sexp_gt( node );
7268 sexp_val = sexp_lt( node );
7272 sexp_val = sexp_equal( node );
7276 sexp_val = sexp_is_iff( node );
7280 sexp_val = sexp_not( node );
7283 case OP_PREVIOUS_GOAL_TRUE:
7284 sexp_val = sexp_previous_goal_status( node, GOAL_COMPLETE );
7287 case OP_PREVIOUS_GOAL_FALSE:
7288 sexp_val = sexp_previous_goal_status( node, GOAL_FAILED );
7291 case OP_PREVIOUS_GOAL_INCOMPLETE:
7292 sexp_val = sexp_previous_goal_status( node, GOAL_INCOMPLETE );
7295 case OP_PREVIOUS_EVENT_TRUE:
7296 sexp_val = sexp_previous_event_status( node, EVENT_SATISFIED );
7299 case OP_PREVIOUS_EVENT_FALSE:
7300 sexp_val = sexp_previous_event_status( node, EVENT_FAILED );
7303 case OP_PREVIOUS_EVENT_INCOMPLETE:
7304 sexp_val = sexp_previous_event_status( node, EVENT_INCOMPLETE );
7308 case OP_EVENT_FALSE:
7309 sexp_val = sexp_event_status( node, (op_num == OP_EVENT_TRUE?1:0) );
7310 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7311 Sexp_useful_number = 0; // indicate sexp isn't current yet
7315 case OP_EVENT_TRUE_DELAY:
7316 case OP_EVENT_FALSE_DELAY:
7317 sexp_val = sexp_event_delay_status( node, (op_num == OP_EVENT_TRUE_DELAY?1:0) );
7318 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7319 Sexp_useful_number = 0; // indicate sexp isn't current yet
7322 case OP_GOAL_TRUE_DELAY:
7323 case OP_GOAL_FALSE_DELAY:
7324 sexp_val = sexp_goal_delay_status( node, (op_num == OP_GOAL_TRUE_DELAY?1:0) );
7327 case OP_EVENT_INCOMPLETE:
7328 sexp_val = sexp_event_incomplete( node );
7329 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7330 Sexp_useful_number = 0; // indicate sexp isn't current yet
7333 case OP_GOAL_INCOMPLETE:
7334 sexp_val = sexp_goal_incomplete( node );
7337 // destroy type sexpressions
7338 case OP_IS_DESTROYED:
7339 sexp_val = sexp_is_destroyed( node, NULL );
7342 case OP_IS_SUBSYSTEM_DESTROYED:
7343 sexp_val = sexp_is_subsystem_destroyed( node );
7347 sexp_val = sexp_has_docked( node );
7350 case OP_HAS_ARRIVED:
7351 sexp_val = sexp_has_arrived( node, NULL );
7354 case OP_HAS_DEPARTED:
7355 sexp_val = sexp_has_departed( node, NULL );
7358 case OP_HAS_UNDOCKED:
7359 sexp_val = sexp_has_undocked( node );
7362 case OP_IS_DISABLED:
7363 sexp_val = sexp_is_disabled( node, NULL );
7366 case OP_IS_DISARMED:
7367 sexp_val = sexp_is_disarmed( node, NULL );
7370 case OP_WAYPOINTS_DONE:
7371 sexp_val = sexp_are_waypoints_done( node );
7374 // objective operators that use a delay
7375 case OP_IS_DESTROYED_DELAY:
7376 sexp_val = sexp_is_destroyed_delay( node );
7379 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
7380 sexp_val = sexp_is_subsystem_destroyed_delay( node );
7383 case OP_HAS_DOCKED_DELAY:
7384 sexp_val = sexp_has_docked_delay( node );
7387 case OP_HAS_ARRIVED_DELAY:
7388 sexp_val = sexp_has_arrived_delay( node );
7391 case OP_HAS_DEPARTED_DELAY:
7392 sexp_val = sexp_has_departed_delay( node );
7395 case OP_HAS_UNDOCKED_DELAY:
7396 sexp_val = sexp_has_undocked_delay( node );
7399 case OP_IS_DISABLED_DELAY:
7400 sexp_val = sexp_is_disabled_delay( node );
7403 case OP_IS_DISARMED_DELAY:
7404 sexp_val = sexp_is_disarmed_delay( node );
7407 case OP_WAYPOINTS_DONE_DELAY:
7408 sexp_val = sexp_are_waypoints_done_delay( node );
7411 case OP_SHIP_TYPE_DESTROYED:
7412 sexp_val = sexp_ship_type_destroyed( node );
7415 // time based sexpressions
7416 case OP_HAS_TIME_ELAPSED:
7417 sexp_val = sexp_has_time_elapsed( node );
7420 case OP_MODIFY_VARIABLE:
7421 sexp_modify_variable( node );
7422 sexp_val = 1; // 1 means only do once.
7425 case OP_TIME_SHIP_DESTROYED:
7426 sexp_val = sexp_time_destroyed( node );
7429 case OP_TIME_WING_DESTROYED:
7430 sexp_val = sexp_time_wing_destroyed( node );
7433 case OP_TIME_SHIP_ARRIVED:
7434 sexp_val = sexp_time_ship_arrived( node );
7437 case OP_TIME_WING_ARRIVED:
7438 sexp_val = sexp_time_wing_arrived( node );
7441 case OP_TIME_SHIP_DEPARTED:
7442 sexp_val = sexp_time_ship_departed( node );
7445 case OP_TIME_WING_DEPARTED:
7446 sexp_val = sexp_time_wing_departed( node );
7449 case OP_MISSION_TIME:
7450 sexp_val = sexp_mission_time();
7453 case OP_TIME_DOCKED:
7454 sexp_val = sexp_time_docked( node );
7457 case OP_TIME_UNDOCKED:
7458 sexp_val = sexp_time_undocked( node );
7461 // info based sexpressions (like shields, hits
7462 case OP_SHIELDS_LEFT:
7463 sexp_val = sexp_shields_left( node );
7467 sexp_val = sexp_hits_left( node );
7470 case OP_SPECIAL_WARP_DISTANCE:
7471 sexp_val = sexp_special_warp_dist( node );
7474 case OP_HITS_LEFT_SUBSYSTEM:
7475 sexp_val = sexp_hits_left_subsystem( node );
7479 sexp_val = sexp_distance( node );
7482 case OP_IS_SHIP_VISIBLE:
7483 sexp_val = sexp_is_ship_visible( node );
7487 sexp_val = sexp_team_score( node );
7490 case OP_LAST_ORDER_TIME:
7491 sexp_val = sexp_last_order_time( node );
7494 case OP_NUM_PLAYERS:
7495 sexp_val = sexp_num_players();
7498 case OP_SKILL_LEVEL_AT_LEAST:
7499 sexp_val = sexp_skill_level_at_least( node );
7502 case OP_IS_CARGO_KNOWN:
7503 case OP_CARGO_KNOWN_DELAY:
7504 sexp_val = sexp_is_cargo_known( node, (op_num==OP_IS_CARGO_KNOWN)?0:1 );
7507 case OP_HAS_BEEN_TAGGED_DELAY:
7508 sexp_val = sexp_has_been_tagged_delay(node);
7511 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
7512 sexp_val = sexp_cap_subsys_cargo_known_delay(node);
7515 case OP_WAS_PROMOTION_GRANTED:
7516 sexp_val = sexp_was_promotion_granted(node);
7519 case OP_WAS_MEDAL_GRANTED:
7520 sexp_val = sexp_was_medal_granted(node);
7523 case OP_PERCENT_SHIPS_DEPARTED:
7524 case OP_PERCENT_SHIPS_DESTROYED:
7525 sexp_val = sexp_percent_ships_depart_destroy(node, op_num);
7528 case OP_DEPART_NODE_DELAY:
7529 sexp_val = sexp_depart_node_delay( node );
7532 case OP_DESTROYED_DEPARTED_DELAY:
7533 sexp_val = sexp_destroyed_departed_delay( node );
7536 // conditional sexpressions
7538 sexp_val = eval_when( node );
7542 sexp_val = eval_cond( node );
7545 // sexpressions with side effects
7547 sexp_change_iff( node );
7551 case OP_ADD_SHIP_GOAL:
7552 sexp_add_ship_goal( node );
7556 case OP_ADD_WING_GOAL:
7557 sexp_add_wing_goal( node );
7562 sexp_add_goal( node );
7566 case OP_CLEAR_SHIP_GOALS:
7567 sexp_clear_ship_goals( node );
7571 case OP_CLEAR_WING_GOALS:
7572 sexp_clear_wing_goals( node );
7576 case OP_CLEAR_GOALS:
7577 sexp_clear_goals( node );
7581 case OP_PROTECT_SHIP:
7582 case OP_UNPROTECT_SHIP:
7583 sexp_protect_ships( node, (op_num==OP_PROTECT_SHIP?1:0) );
7587 case OP_BEAM_PROTECT_SHIP:
7588 case OP_BEAM_UNPROTECT_SHIP:
7589 sexp_beam_protect_ships( node, (op_num==OP_BEAM_PROTECT_SHIP?1:0) );
7593 case OP_SHIP_INVISIBLE:
7594 case OP_SHIP_VISIBLE:
7595 sexp_ships_visible( node, (op_num==OP_SHIP_VISIBLE?1:0) );
7599 case OP_SHIP_VULNERABLE:
7600 case OP_SHIP_INVULNERABLE:
7601 sexp_ships_invulnerable( node, (op_num==OP_SHIP_INVULNERABLE?1:0) );
7605 case OP_SHIP_GUARDIAN:
7606 case OP_SHIP_NO_GUARDIAN:
7607 sexp_ships_guardian( node, (op_num==OP_SHIP_GUARDIAN?1:0) );
7611 case OP_SHIP_VANISH:
7612 sexp_ship_vanish( node );
7616 case OP_SEND_MESSAGE:
7617 sexp_send_message( node );
7621 case OP_SEND_MESSAGE_LIST:
7622 sexp_send_message_list( node );
7626 case OP_SEND_RANDOM_MESSAGE:
7627 sexp_send_random_message( node );
7631 case OP_SELF_DESTRUCT:
7632 sexp_self_destruct( node );
7636 case OP_NEXT_MISSION:
7637 sexp_next_mission( node );
7641 case OP_END_OF_CAMPAIGN:
7642 sexp_end_of_campaign( node );
7646 case OP_END_CAMPAIGN:
7647 sexp_end_campaign( node );
7651 case OP_SABOTAGE_SUBSYSTEM:
7652 sexp_sabotage_subsystem( node );
7656 case OP_REPAIR_SUBSYSTEM:
7657 sexp_repair_subsystem( node );
7661 case OP_SET_SUBSYSTEM_STRNGTH:
7662 sexp_set_subsystem_strength( node );
7666 case OP_INVALIDATE_GOAL:
7667 case OP_VALIDATE_GOAL:
7668 sexp_change_goal_validity( node, (op_num==OP_INVALIDATE_GOAL?0:1) );
7672 case OP_TRANSFER_CARGO:
7673 sexp_transfer_cargo( node );
7677 case OP_EXCHANGE_CARGO:
7678 sexp_exchange_cargo( node );
7683 case OP_JETTISON_CARGO:
7684 sexp_jettison_cargo( node );
7688 case OP_CARGO_NO_DEPLETE:
7689 sexp_cargo_no_deplete( node );
7693 case OP_SET_SPECIAL_WARPOUT_NAME:
7694 sexp_special_warpout_name( node );
7698 case OP_END_MISSION_DELAY:
7699 sexp_end_mission_delay( node );
7703 // sexpressions for setting flag for good/bad time for someone to reasm
7704 case OP_GOOD_REARM_TIME:
7705 sexp_good_time_to_rearm( node );
7709 case OP_GRANT_PROMOTION:
7710 sexp_grant_promotion();
7714 case OP_GRANT_MEDAL:
7715 sexp_grant_medal( node );
7719 case OP_WARP_BROKEN:
7720 case OP_WARP_NOT_BROKEN:
7721 sexp_deal_with_warp( node, 0, op_num==OP_WARP_BROKEN?1:0 );
7726 case OP_WARP_ALLOWED:
7727 sexp_deal_with_warp( node, 1, op_num==OP_WARP_NEVER?1:0 );
7731 case OP_GOOD_SECONDARY_TIME:
7732 sexp_good_secondary_time( node );
7736 // sexpressions to allow shpis/weapons during the course of a mission
7738 sexp_allow_ship( node );
7742 case OP_ALLOW_WEAPON:
7743 sexp_allow_weapon( node );
7747 case OP_TECH_ADD_SHIP:
7748 sexp_tech_add_ship(node);
7752 case OP_TECH_ADD_WEAPON:
7753 sexp_tech_add_weapon(node);
7757 // in the case of a red_alert mission, simply call the red alert function to close
7758 // the current campaign's mission and move forward to the next mission
7760 red_alert_start_mission();
7764 // training operators
7765 case OP_KEY_PRESSED:
7766 sexp_val = sexp_key_pressed(node);
7769 case OP_SPECIAL_CHECK:
7770 sexp_val = sexp_special_training_check(node);
7774 sexp_key_reset(node);
7775 sexp_val = SEXP_KNOWN_TRUE; // only do it first time in repeating events.
7779 sexp_val = sexp_targeted(node);
7783 sexp_val = sexp_speed(node);
7786 case OP_SECONDARIES_DEPLETED:
7787 sexp_val = sexp_secondaries_depleted(node);
7791 sexp_val = sexp_facing(node);
7795 sexp_val = sexp_facing2(node);
7799 sexp_val = sexp_order(node);
7802 case OP_WAYPOINT_MISSED:
7803 sexp_val = sexp_waypoint_missed();
7806 case OP_WAYPOINT_TWICE:
7807 sexp_val = sexp_waypoint_twice();
7811 sexp_val = sexp_path_flown();
7814 case OP_TRAINING_MSG:
7815 sexp_send_training_message(node);
7819 case OP_FLASH_HUD_GAUGE:
7820 sexp_flash_hud_gauge(node);
7824 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
7825 sexp_set_training_context_fly_path(node);
7829 case OP_SET_TRAINING_CONTEXT_SPEED:
7830 sexp_set_training_context_speed(node);
7834 // debugging operators
7840 case 0: // zero represents a non-operator
7841 return atoi(CTEXT(cur_node));
7848 sexp_beam_fire(node);
7853 sexp_val = sexp_is_tagged(node);
7857 sexp_val = sexp_num_kills(node);
7860 case OP_NUM_TYPE_KILLS:
7861 sexp_val = sexp_num_type_kills(node);
7864 case OP_NUM_CLASS_KILLS:
7865 sexp_val = sexp_num_class_kills(node);
7870 sexp_beam_free(node);
7873 case OP_BEAM_FREE_ALL:
7875 sexp_beam_free_all(node);
7880 sexp_beam_lock(node);
7883 case OP_BEAM_LOCK_ALL:
7885 sexp_beam_lock_all(node);
7888 case OP_TURRET_FREE:
7890 sexp_turret_free(node);
7893 case OP_TURRET_FREE_ALL:
7895 sexp_turret_free_all(node);
7898 case OP_TURRET_LOCK:
7900 sexp_turret_lock(node);
7903 case OP_TURRET_LOCK_ALL:
7905 sexp_turret_lock_all(node);
7908 case OP_ADD_REMOVE_ESCORT:
7910 sexp_add_remove_escort(node);
7913 case OP_AWACS_SET_RADIUS:
7915 sexp_awacs_set_radius(node);
7918 case OP_CAP_WAYPOINT_SPEED:
7920 sexp_cap_waypont_speed(node);
7923 case OP_TURRET_TAGGED_ONLY_ALL:
7925 sexp_turret_tagged_only_all(node);
7928 case OP_TURRET_TAGGED_CLEAR_ALL:
7930 sexp_turret_tagged_clear_all(node);
7933 case OP_SUBSYS_SET_RANDOM:
7935 sexp_subsys_set_random(node);
7938 case OP_SUPERNOVA_START:
7940 sexp_supernova_start(node);
7943 case OP_SHIELD_RECHARGE_PCT:
7944 sexp_val = sexp_shield_recharge_pct(node);
7947 case OP_ENGINE_RECHARGE_PCT:
7948 sexp_val = sexp_engine_recharge_pct(node);
7951 case OP_WEAPON_RECHARGE_PCT:
7952 sexp_val = sexp_weapon_recharge_pct(node);
7955 case OP_SHIELD_QUAD_LOW:
7956 sexp_val = sexp_shield_quad_low(node);
7959 case OP_SECONDARY_AMMO_PCT:
7960 sexp_val = sexp_secondary_ammo_pct(node);
7963 case OP_IS_SECONDARY_SELECTED:
7964 sexp_val = sexp_is_secondary_selected(node);
7967 case OP_IS_PRIMARY_SELECTED:
7968 sexp_val = sexp_is_primary_selected(node);
7972 Error(LOCATION, "Looking for SEXP operator, found '%s'.\n", CTEXT(cur_node));
7976 Assert(sexp_val != UNINITIALIZED);
7978 // if we haven't returned, check the sexp value of the sexpression evaluation. A special
7979 // value of known true or known false means that we should set the sexp.value field for
7980 // short circuit eval (and return that special value as well).
7981 if (sexp_val == SEXP_KNOWN_TRUE) {
7982 Sexp_nodes[cur_node].value = SEXP_KNOWN_TRUE;
7986 if (sexp_val == SEXP_KNOWN_FALSE) {
7987 Sexp_nodes[cur_node].value = SEXP_KNOWN_FALSE;
7991 if ( sexp_val == SEXP_NAN ) {
7992 Sexp_nodes[cur_node].value = SEXP_NAN; // not a number values are false I would suspect
7996 if ( sexp_val == SEXP_NAN_FOREVER ) {
7997 Sexp_nodes[cur_node].value = SEXP_NAN_FOREVER;
8001 if ( sexp_val == SEXP_CANT_EVAL ) {
8002 Sexp_nodes[cur_node].value = SEXP_CANT_EVAL;
8003 Sexp_useful_number = 0; // indicate sexp isn't current yet
8007 if ( Sexp_nodes[cur_node].value == SEXP_NAN ) { // if we had a nan, but now don't, reset the value
8008 Sexp_nodes[cur_node].value = SEXP_UNKNOWN;
8013 Sexp_nodes[cur_node].value = SEXP_TRUE;
8015 Sexp_nodes[cur_node].value = SEXP_FALSE;
8022 // Still a debug-level system.
8023 // get_sexp_main reads and builds the internal representation for a
8024 // symbolic expression.
8026 // Mp points at first character in expression.
8027 // The symbolic expression is built in Sexp_nodes beginning at node 0.
8031 char token[TOKEN_LENGTH];
8034 ignore_white_space();
8037 if (!strncmp(Mp, "( )", 3))
8042 start_node = get_sexp(token);
8043 // only need to check syntax if we have a operator
8044 if ( /*Sexp_nodes[start_node].subtype != SEXP_ATOM_OPERATOR ||*/ Fred_running || (start_node == -1))
8050 op = identify_operator(CTEXT(start_node));
8052 Error (LOCATION, "Can't find operator %s in operator list\n.", CTEXT(start_node) );
8062 while (*Mp != '#') {
8064 diag_printf("\n----------------\n");
8065 ignore_white_space();
8070 // returns the data type returned by an operator
8071 int query_operator_return_type(int op)
8075 Assert(op >= 0 && op < Num_operators);
8076 op = Operators[op].value;
8084 case OP_AND_IN_SEQUENCE:
8088 case OP_GREATER_THAN:
8090 case OP_IS_DESTROYED:
8091 case OP_IS_SUBSYSTEM_DESTROYED:
8092 case OP_IS_DISABLED:
8093 case OP_IS_DISARMED:
8095 case OP_HAS_UNDOCKED:
8096 case OP_HAS_ARRIVED:
8097 case OP_HAS_DEPARTED:
8098 case OP_IS_DESTROYED_DELAY:
8099 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8100 case OP_IS_DISABLED_DELAY:
8101 case OP_IS_DISARMED_DELAY:
8102 case OP_HAS_DOCKED_DELAY:
8103 case OP_HAS_UNDOCKED_DELAY:
8104 case OP_HAS_ARRIVED_DELAY:
8105 case OP_HAS_DEPARTED_DELAY:
8107 case OP_HAS_TIME_ELAPSED:
8108 case OP_GOAL_INCOMPLETE:
8109 case OP_GOAL_TRUE_DELAY:
8110 case OP_GOAL_FALSE_DELAY:
8111 case OP_EVENT_INCOMPLETE:
8112 case OP_EVENT_TRUE_DELAY:
8113 case OP_EVENT_FALSE_DELAY:
8114 case OP_PREVIOUS_EVENT_TRUE:
8115 case OP_PREVIOUS_EVENT_FALSE:
8116 case OP_PREVIOUS_EVENT_INCOMPLETE:
8117 case OP_PREVIOUS_GOAL_TRUE:
8118 case OP_PREVIOUS_GOAL_FALSE:
8119 case OP_PREVIOUS_GOAL_INCOMPLETE:
8120 case OP_WAYPOINTS_DONE:
8121 case OP_WAYPOINTS_DONE_DELAY:
8122 case OP_SHIP_TYPE_DESTROYED:
8123 case OP_LAST_ORDER_TIME:
8124 case OP_KEY_PRESSED:
8130 case OP_WAYPOINT_MISSED:
8131 case OP_WAYPOINT_TWICE:
8134 case OP_EVENT_FALSE:
8135 case OP_SKILL_LEVEL_AT_LEAST:
8136 case OP_IS_CARGO_KNOWN:
8137 case OP_HAS_BEEN_TAGGED_DELAY:
8138 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8139 case OP_CARGO_KNOWN_DELAY:
8140 case OP_WAS_PROMOTION_GRANTED:
8141 case OP_WAS_MEDAL_GRANTED:
8142 case OP_PERCENT_SHIPS_DEPARTED:
8143 case OP_PERCENT_SHIPS_DESTROYED:
8144 case OP_DEPART_NODE_DELAY:
8145 case OP_DESTROYED_DEPARTED_DELAY:
8146 case OP_SPECIAL_CHECK:
8148 case OP_SECONDARIES_DEPLETED:
8149 case OP_SHIELD_QUAD_LOW:
8150 case OP_IS_SECONDARY_SELECTED:
8151 case OP_IS_PRIMARY_SELECTED:
8160 case OP_TIME_SHIP_DESTROYED:
8161 case OP_TIME_SHIP_ARRIVED:
8162 case OP_TIME_SHIP_DEPARTED:
8163 case OP_TIME_WING_DESTROYED:
8164 case OP_TIME_WING_ARRIVED:
8165 case OP_TIME_WING_DEPARTED:
8166 case OP_MISSION_TIME:
8167 case OP_TIME_DOCKED:
8168 case OP_TIME_UNDOCKED:
8169 case OP_SHIELDS_LEFT:
8171 case OP_HITS_LEFT_SUBSYSTEM:
8173 case OP_NUM_PLAYERS:
8175 case OP_NUM_TYPE_KILLS:
8176 case OP_NUM_CLASS_KILLS:
8177 case OP_SHIELD_RECHARGE_PCT:
8178 case OP_ENGINE_RECHARGE_PCT:
8179 case OP_WEAPON_RECHARGE_PCT:
8180 case OP_SECONDARY_AMMO_PCT:
8181 case OP_SPECIAL_WARP_DISTANCE:
8182 case OP_IS_SHIP_VISIBLE:
8184 return OPR_POSITIVE;
8189 case OP_CLEAR_SHIP_GOALS:
8190 case OP_CLEAR_WING_GOALS:
8191 case OP_CLEAR_GOALS:
8192 case OP_ADD_SHIP_GOAL:
8193 case OP_ADD_WING_GOAL:
8195 case OP_PROTECT_SHIP:
8196 case OP_UNPROTECT_SHIP:
8197 case OP_BEAM_PROTECT_SHIP:
8198 case OP_BEAM_UNPROTECT_SHIP:
8202 case OP_SEND_MESSAGE:
8203 case OP_SELF_DESTRUCT:
8204 case OP_NEXT_MISSION:
8205 case OP_END_CAMPAIGN:
8206 case OP_END_OF_CAMPAIGN:
8207 case OP_SABOTAGE_SUBSYSTEM:
8208 case OP_REPAIR_SUBSYSTEM:
8209 case OP_INVALIDATE_GOAL:
8210 case OP_VALIDATE_GOAL:
8211 case OP_SEND_RANDOM_MESSAGE:
8212 case OP_TRANSFER_CARGO:
8213 case OP_EXCHANGE_CARGO:
8214 case OP_JETTISON_CARGO:
8215 case OP_CARGO_NO_DEPLETE:
8217 case OP_TRAINING_MSG:
8218 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8219 case OP_SET_TRAINING_CONTEXT_SPEED:
8220 case OP_END_MISSION_DELAY:
8221 case OP_SET_SUBSYSTEM_STRNGTH:
8222 case OP_GOOD_REARM_TIME:
8223 case OP_GRANT_PROMOTION:
8224 case OP_GRANT_MEDAL:
8226 case OP_ALLOW_WEAPON:
8227 case OP_TECH_ADD_SHIP:
8228 case OP_TECH_ADD_WEAPON:
8229 case OP_WARP_BROKEN:
8230 case OP_WARP_NOT_BROKEN:
8232 case OP_WARP_ALLOWED:
8233 case OP_FLASH_HUD_GAUGE:
8234 case OP_GOOD_SECONDARY_TIME:
8235 case OP_SHIP_VISIBLE:
8236 case OP_SHIP_INVISIBLE:
8237 case OP_SHIP_VULNERABLE:
8238 case OP_SHIP_INVULNERABLE:
8239 case OP_SHIP_GUARDIAN:
8240 case OP_SHIP_VANISH:
8241 case OP_SHIP_NO_GUARDIAN:
8243 case OP_MODIFY_VARIABLE:
8246 case OP_BEAM_FREE_ALL:
8248 case OP_BEAM_LOCK_ALL:
8249 case OP_TURRET_FREE:
8250 case OP_TURRET_FREE_ALL:
8251 case OP_TURRET_LOCK:
8252 case OP_TURRET_LOCK_ALL:
8253 case OP_ADD_REMOVE_ESCORT:
8254 case OP_AWACS_SET_RADIUS:
8255 case OP_SEND_MESSAGE_LIST:
8256 case OP_CAP_WAYPOINT_SPEED:
8257 case OP_TURRET_TAGGED_ONLY_ALL:
8258 case OP_TURRET_TAGGED_CLEAR_ALL:
8259 case OP_SUBSYS_SET_RANDOM:
8260 case OP_SUPERNOVA_START:
8261 case OP_SET_SPECIAL_WARPOUT_NAME:
8267 case OP_AI_WARP: // this particular operator is obsolete
8268 case OP_AI_WARP_OUT:
8269 case OP_AI_WAYPOINTS:
8270 case OP_AI_WAYPOINTS_ONCE:
8271 case OP_AI_DESTROY_SUBSYS:
8272 case OP_AI_CHASE_WING:
8273 case OP_AI_DISABLE_SHIP:
8274 case OP_AI_DISARM_SHIP:
8276 case OP_AI_GUARD_WING:
8277 case OP_AI_CHASE_ANY:
8278 case OP_AI_EVADE_SHIP:
8279 case OP_AI_STAY_NEAR_SHIP:
8280 case OP_AI_KEEP_SAFE_DISTANCE:
8282 case OP_AI_STAY_STILL:
8283 case OP_AI_PLAY_DEAD:
8293 // returns the data type of a specified argument to an operator. Argnum is 0 indexed.
8294 int query_operator_argument_type(int op, int argnum)
8300 Assert(index >= 0 && index < Num_operators);
8301 op = Operators[index].value;
8304 for (index=0; index<Num_operators; index++)
8305 if (Operators[index].value == op)
8308 Assert(index < Num_operators);
8311 if (argnum >= Operators[index].max)
8317 case OP_MISSION_TIME:
8320 case OP_WAYPOINT_MISSED:
8321 case OP_WAYPOINT_TWICE:
8323 case OP_GRANT_PROMOTION:
8324 case OP_WAS_PROMOTION_GRANTED:
8329 case OP_AND_IN_SEQUENCE:
8341 case OP_GREATER_THAN:
8343 case OP_HAS_TIME_ELAPSED:
8345 case OP_SET_TRAINING_CONTEXT_SPEED:
8346 case OP_END_MISSION_DELAY:
8347 case OP_SPECIAL_CHECK:
8348 case OP_AI_WARP_OUT:
8350 return OPF_POSITIVE;
8352 case OP_AI_WARP: // this operator is obsolete
8353 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8355 return OPF_WAYPOINT_PATH;
8359 case OP_AI_WAYPOINTS:
8360 case OP_AI_WAYPOINTS_ONCE:
8362 return OPF_WAYPOINT_PATH;
8364 return OPF_POSITIVE;
8366 case OP_IS_DISABLED:
8367 case OP_IS_DISARMED:
8368 case OP_TIME_SHIP_DESTROYED:
8369 case OP_TIME_SHIP_ARRIVED:
8370 case OP_TIME_SHIP_DEPARTED:
8371 case OP_SHIELDS_LEFT:
8373 case OP_CLEAR_SHIP_GOALS:
8374 case OP_PROTECT_SHIP:
8375 case OP_UNPROTECT_SHIP:
8376 case OP_BEAM_PROTECT_SHIP:
8377 case OP_BEAM_UNPROTECT_SHIP:
8378 case OP_TRANSFER_CARGO:
8379 case OP_EXCHANGE_CARGO:
8380 case OP_SHIP_INVISIBLE:
8381 case OP_SHIP_VISIBLE:
8382 case OP_SHIP_INVULNERABLE:
8383 case OP_SHIP_VULNERABLE:
8384 case OP_SHIP_GUARDIAN:
8385 case OP_SHIP_VANISH:
8386 case OP_SHIP_NO_GUARDIAN:
8387 case OP_SECONDARIES_DEPLETED:
8388 case OP_SPECIAL_WARP_DISTANCE:
8389 case OP_SET_SPECIAL_WARPOUT_NAME:
8390 case OP_IS_SHIP_VISIBLE:
8393 case OP_IS_DESTROYED:
8394 case OP_HAS_ARRIVED:
8395 case OP_HAS_DEPARTED:
8396 case OP_CLEAR_GOALS:
8397 return OPF_SHIP_WING;
8399 case OP_IS_DISABLED_DELAY:
8400 case OP_IS_DISARMED_DELAY:
8402 return OPF_POSITIVE;
8410 return OPF_POSITIVE;
8414 return OPF_WAYPOINT_PATH;
8416 return OPF_POSITIVE;
8421 return OPF_AI_ORDER;
8423 return OPF_SHIP_WING;
8425 case OP_IS_DESTROYED_DELAY:
8426 case OP_HAS_ARRIVED_DELAY:
8427 case OP_HAS_DEPARTED_DELAY:
8428 case OP_LAST_ORDER_TIME:
8430 return OPF_POSITIVE;
8432 return OPF_SHIP_WING;
8435 return OPF_SHIP_WING_POINT;
8437 case OP_MODIFY_VARIABLE:
8439 return OPF_VARIABLE_NAME;
8441 return OPF_AMBIGUOUS;
8445 case OP_HAS_UNDOCKED:
8446 case OP_HAS_DOCKED_DELAY:
8447 case OP_HAS_UNDOCKED_DELAY:
8448 case OP_TIME_DOCKED:
8449 case OP_TIME_UNDOCKED:
8453 return OPF_POSITIVE;
8455 case OP_TIME_WING_DESTROYED:
8456 case OP_TIME_WING_ARRIVED:
8457 case OP_TIME_WING_DEPARTED:
8458 case OP_CLEAR_WING_GOALS:
8461 case OP_IS_SUBSYSTEM_DESTROYED:
8462 case OP_HITS_LEFT_SUBSYSTEM:
8466 return OPF_SUBSYSTEM;
8471 else if (argnum == 1)
8472 return OPF_POSITIVE;
8474 return OPF_SUBSYSTEM;
8476 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8479 else if ( argnum == 1 )
8480 return OPF_SUBSYSTEM;
8482 return OPF_POSITIVE;
8491 case OP_ADD_SHIP_GOAL:
8497 case OP_ADD_WING_GOAL:
8505 return OPF_SHIP_WING;
8516 case OP_AI_DISABLE_SHIP:
8517 case OP_AI_DISARM_SHIP:
8518 case OP_AI_EVADE_SHIP:
8519 case OP_AI_STAY_NEAR_SHIP:
8524 return OPF_POSITIVE;
8529 return OPF_SHIP_WING;
8531 return OPF_POSITIVE;
8534 case OP_AI_KEEP_SAFE_DISTANCE:
8535 return OPF_POSITIVE;
8540 else if (argnum == 1)
8541 return OPF_DOCKER_POINT;
8542 else if (argnum == 2)
8543 return OPF_DOCKEE_POINT;
8545 return OPF_POSITIVE;
8547 case OP_AI_CHASE_WING:
8548 case OP_AI_GUARD_WING:
8552 return OPF_POSITIVE;
8554 case OP_AI_DESTROY_SUBSYS:
8557 else if (argnum == 1)
8558 return OPF_SUBSYSTEM;
8560 return OPF_POSITIVE;
8565 case OP_SEND_MESSAGE:
8566 case OP_SEND_RANDOM_MESSAGE:
8568 return OPF_WHO_FROM;
8569 else if ( argnum == 1 )
8570 return OPF_PRIORITY;
8574 case OP_SEND_MESSAGE_LIST:
8577 // every four, the value repeats
8582 return OPF_WHO_FROM;
8584 return OPF_PRIORITY;
8588 return OPF_POSITIVE;
8590 case OP_TRAINING_MSG:
8594 return OPF_POSITIVE;
8596 case OP_SELF_DESTRUCT:
8599 case OP_NEXT_MISSION:
8600 return OPF_MISSION_NAME;
8602 case OP_END_CAMPAIGN:
8603 case OP_END_OF_CAMPAIGN:
8606 case OP_PREVIOUS_GOAL_TRUE:
8607 case OP_PREVIOUS_GOAL_FALSE:
8609 return OPF_MISSION_NAME;
8610 else if (argnum == 1 )
8611 return OPF_GOAL_NAME;
8615 case OP_PREVIOUS_GOAL_INCOMPLETE:
8616 return OPF_GOAL_NAME;
8618 case OP_PREVIOUS_EVENT_TRUE:
8619 case OP_PREVIOUS_EVENT_FALSE:
8620 case OP_PREVIOUS_EVENT_INCOMPLETE:
8622 return OPF_MISSION_NAME;
8623 else if ( argnum == 1 )
8624 return OPF_EVENT_NAME;
8628 case OP_SABOTAGE_SUBSYSTEM:
8629 case OP_REPAIR_SUBSYSTEM:
8630 case OP_SET_SUBSYSTEM_STRNGTH:
8632 return OPF_SHIP_NOT_PLAYER;
8633 else if (argnum == 1 )
8634 return OPF_SUBSYSTEM;
8636 return OPF_POSITIVE;
8638 case OP_WAYPOINTS_DONE:
8640 return OPF_SHIP_WING;
8642 return OPF_WAYPOINT_PATH;
8644 case OP_WAYPOINTS_DONE_DELAY:
8646 return OPF_SHIP_WING;
8647 else if ( argnum == 1 )
8648 return OPF_WAYPOINT_PATH;
8650 return OPF_POSITIVE;
8652 case OP_INVALIDATE_GOAL:
8653 case OP_VALIDATE_GOAL:
8654 return OPF_GOAL_NAME;
8656 case OP_SHIP_TYPE_DESTROYED:
8658 return OPF_POSITIVE;
8660 return OPF_SHIP_TYPE;
8662 case OP_KEY_PRESSED:
8664 return OPF_KEYPRESS;
8666 return OPF_POSITIVE;
8669 return OPF_KEYPRESS;
8672 case OP_EVENT_FALSE:
8673 return OPF_EVENT_NAME;
8675 case OP_EVENT_INCOMPLETE:
8676 case OP_EVENT_TRUE_DELAY:
8677 case OP_EVENT_FALSE_DELAY:
8679 return OPF_EVENT_NAME;
8681 return OPF_POSITIVE;
8683 case OP_GOAL_INCOMPLETE:
8684 case OP_GOAL_TRUE_DELAY:
8685 case OP_GOAL_FALSE_DELAY:
8687 return OPF_GOAL_NAME;
8689 return OPF_POSITIVE;
8691 case OP_AI_PLAY_DEAD:
8692 case OP_AI_CHASE_ANY:
8693 return OPF_POSITIVE;
8695 case OP_AI_STAY_STILL:
8697 return OPF_SHIP_POINT;
8699 return OPF_POSITIVE;
8701 case OP_GOOD_REARM_TIME:
8705 return OPF_POSITIVE;
8707 case OP_NUM_PLAYERS:
8708 return OPF_POSITIVE;
8710 case OP_SKILL_LEVEL_AT_LEAST:
8711 return OPF_SKILL_LEVEL;
8713 case OP_GRANT_MEDAL:
8714 case OP_WAS_MEDAL_GRANTED:
8715 return OPF_MEDAL_NAME;
8717 case OP_IS_CARGO_KNOWN:
8720 case OP_CARGO_KNOWN_DELAY:
8722 return OPF_POSITIVE;
8726 case OP_HAS_BEEN_TAGGED_DELAY:
8727 if ( argnum == 0 ) {
8728 return OPF_POSITIVE;
8733 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8734 if ( argnum == 0 ) {
8735 return OPF_POSITIVE;
8736 } else if ( argnum == 1 ) {
8739 return OPF_SUBSYSTEM;
8743 case OP_TECH_ADD_SHIP:
8744 return OPF_SHIP_CLASS_NAME;
8746 case OP_ALLOW_WEAPON:
8747 case OP_TECH_ADD_WEAPON:
8748 return OPF_WEAPON_NAME;
8750 case OP_WARP_BROKEN:
8751 case OP_WARP_NOT_BROKEN:
8753 case OP_WARP_ALLOWED:
8756 case OP_FLASH_HUD_GAUGE:
8757 return OPF_HUD_GAUGE_NAME;
8759 case OP_GOOD_SECONDARY_TIME:
8762 else if ( argnum == 1 )
8763 return OPF_POSITIVE;
8764 else if ( argnum == 2 )
8765 return OPF_HUGE_WEAPON;
8769 case OP_PERCENT_SHIPS_DEPARTED:
8770 case OP_PERCENT_SHIPS_DESTROYED:
8772 return OPF_POSITIVE;
8774 return OPF_SHIP_WING;
8778 case OP_DEPART_NODE_DELAY:
8780 return OPF_POSITIVE;
8781 } else if ( argnum == 1 ){
8782 return OPF_JUMP_NODE_NAME;
8787 case OP_DESTROYED_DEPARTED_DELAY:
8789 return OPF_POSITIVE;
8791 return OPF_SHIP_WING;
8794 case OP_JETTISON_CARGO:
8798 return OPF_POSITIVE;
8801 case OP_CARGO_NO_DEPLETE:
8813 return OPF_SUBSYSTEM;
8817 return OPF_SUBSYSTEM;
8826 case OP_NUM_TYPE_KILLS:
8830 return OPF_SHIP_TYPE;
8833 case OP_NUM_CLASS_KILLS:
8837 return OPF_SHIP_CLASS_NAME;
8842 case OP_TURRET_FREE:
8843 case OP_TURRET_LOCK:
8847 return OPF_SUBSYSTEM;
8850 case OP_BEAM_FREE_ALL:
8851 case OP_BEAM_LOCK_ALL:
8852 case OP_TURRET_FREE_ALL:
8853 case OP_TURRET_LOCK_ALL:
8854 case OP_TURRET_TAGGED_ONLY_ALL:
8855 case OP_TURRET_TAGGED_CLEAR_ALL:
8858 case OP_ADD_REMOVE_ESCORT:
8865 case OP_AWACS_SET_RADIUS:
8868 } else if(argnum == 1){
8869 return OPF_AWACS_SUBSYSTEM;
8874 case OP_CAP_WAYPOINT_SPEED:
8881 case OP_SUBSYS_SET_RANDOM:
8884 } else if (argnum == 1 || argnum == 2) {
8887 return OPF_SUBSYSTEM;
8890 case OP_SUPERNOVA_START:
8893 case OP_SHIELD_RECHARGE_PCT:
8894 case OP_WEAPON_RECHARGE_PCT:
8895 case OP_ENGINE_RECHARGE_PCT:
8898 case OP_SHIELD_QUAD_LOW:
8905 case OP_SECONDARY_AMMO_PCT:
8912 case OP_IS_SECONDARY_SELECTED:
8913 case OP_IS_PRIMARY_SELECTED:
8927 void update_block_names(const char *old_name, const char *new_name)
8931 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
8932 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
8933 if ( !stricmp(old_name, Sexp_variables[i].variable_name) ) {
8934 strcpy(Sexp_variables[i].variable_name, new_name);
8940 // DA: 1/7/99 Used to rename ships and waypoints, not variables
8941 // Strictly used in FRED
8942 void update_sexp_references(char *old_name, char *new_name)
8946 // update_block_names
8947 update_block_names(old_name, new_name);
8949 Assert(strlen(new_name) < TOKEN_LENGTH);
8950 for (i=0; i<MAX_SEXP_NODES; i++){
8951 if ((SEXP_NODE_TYPE(i) == SEXP_ATOM) && (Sexp_nodes[i].subtype == SEXP_ATOM_STRING)){
8952 if (!stricmp(CTEXT(i), old_name)){
8953 strcpy(CTEXT(i), new_name);
8959 // DA: 1/7/99 Used to rename event names, goal names, not variables
8960 // Strictly used in FRED
8961 void update_sexp_references(char *old_name, char *new_name, int format)
8965 Assert(strlen(new_name) < TOKEN_LENGTH);
8966 for (i=0; i<MAX_SEXP_NODES; i++){
8967 if (is_sexp_top_level(i)){
8968 update_sexp_references(old_name, new_name, format, i);
8973 // DA: 1/7/99 Used to rename event names, goal names, not variables
8974 // recursive function to update references to a certain type of data
8975 void update_sexp_references(char *old_name, char *new_name, int format, int node)
8983 if ((SEXP_NODE_TYPE(node) == SEXP_LIST) && (Sexp_nodes[node].subtype == SEXP_ATOM_LIST)) {
8984 if (Sexp_nodes[node].first){
8985 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].first);
8987 if (Sexp_nodes[node].rest){
8988 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].rest);
8994 if (SEXP_NODE_TYPE(node) != SEXP_ATOM){
8998 if (Sexp_nodes[node].subtype != SEXP_ATOM_OPERATOR){
9002 op = identify_operator(CTEXT(node));
9003 Assert(Sexp_nodes[node].first < 0);
9004 n = Sexp_nodes[node].rest;
9007 if (SEXP_NODE_TYPE(n) == SEXP_LIST){
9008 update_sexp_references(old_name, new_name, format, Sexp_nodes[n].first);
9010 Assert((SEXP_NODE_TYPE(n) == SEXP_ATOM) && ((Sexp_nodes[n].subtype == SEXP_ATOM_NUMBER) || (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)));
9011 if (query_operator_argument_type(op, i) == format) {
9012 if (!stricmp(CTEXT(n), old_name)){
9013 strcpy(CTEXT(n), new_name);
9018 n = Sexp_nodes[n].rest;
9023 int query_referenced_in_sexp(int mode, char *name, int *node)
9027 for (n=0; n<MAX_SEXP_NODES; n++){
9028 if ((SEXP_NODE_TYPE(n) == SEXP_ATOM) && (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)){
9029 if (!stricmp(CTEXT(n), name)){
9035 if (n == MAX_SEXP_NODES){
9043 // so we know it's being used somewhere.. Time to find out where..
9044 for (i=0; i<MAX_SHIPS; i++)
9045 if (Ships[i].objnum >= 0) {
9046 if (query_node_in_sexp(n, Ships[i].arrival_cue)){
9047 return i | SRC_SHIP_ARRIVAL;
9049 if (query_node_in_sexp(n, Ships[i].departure_cue)){
9050 return i | SRC_SHIP_DEPARTURE;
9054 for (i=0; i<MAX_WINGS; i++){
9055 if (Wings[i].wave_count) {
9056 if (query_node_in_sexp(n, Wings[i].arrival_cue)){
9057 return i | SRC_WING_ARRIVAL;
9059 if (query_node_in_sexp(n, Wings[i].departure_cue)){
9060 return i | SRC_WING_DEPARTURE;
9065 for (i=0; i<Num_mission_events; i++){
9066 if (query_node_in_sexp(n, Mission_events[i].formula)){
9067 return i | SRC_EVENT;
9071 for (i=0; i<Num_goals; i++){
9072 if (query_node_in_sexp(n, Mission_goals[i].formula)){
9073 return i | SRC_MISSION_GOAL;
9077 for (j=0; j<Num_teams; j++) {
9078 for (i=0; i<Debriefings[j].num_stages; i++) {
9079 if (query_node_in_sexp(n, Debriefings[j].stages[i].formula)){
9080 return i | SRC_DEBRIEFING;
9085 for (j=0; j<Num_teams; j++) {
9086 for (i=0; i<Briefings[j].num_stages; i++) {
9087 if (query_node_in_sexp(n, Briefings[j].stages[i].formula)){
9088 return i | SRC_BRIEFING;
9096 int verify_vector(char *text)
9101 for (i=0; i<Num_waypoint_lists; i++) {
9102 len = strlen(str = Waypoint_lists[i].name);
9103 if (!strnicmp(str, text, len)){
9104 if (!text[len] || text[len] == ':'){
9110 if (i < Num_waypoint_lists) {
9112 return 0; // a valid waypoint path
9115 str = &text[len + 1];
9117 if (!isdigit(*str++)){
9118 return -1; // not a valid number
9122 z = atoi(&text[len + 1]);
9123 if (z < 1 || z > Waypoint_lists[i].count){
9124 return -1; // out of range
9127 return 0; // a valid waypoint
9131 if (text[0] != '(' || text[len - 1] != ')'){
9143 if (validate_float(&str)){
9148 if (validate_float(&str)){
9153 if (validate_float(&str)){
9171 void skip_white(char **str)
9173 if ((**str == ' ') || (**str == '\t')){
9178 int validate_float(char **str)
9180 int count = 0, dot = 0;
9182 while (isdigit(**str) || **str == '.') {
9202 // check if operator return type opr is a valid match for operator argument type opf
9203 int sexp_query_type_match(int opf, int opr)
9207 return ((opr == OPR_NUMBER) || (opr == OPR_POSITIVE));
9210 return (opr == OPR_POSITIVE);
9213 return (opr == OPR_BOOL);
9216 return (opr == OPR_NULL);
9219 return (opr == OPR_AI_GOAL);
9225 char *sexp_error_message(int num)
9228 case SEXP_CHECK_NONOP_ARGS:
9229 return "Data shouldn't have arguments";
9231 case SEXP_CHECK_OP_EXPTECTED:
9232 return "Operator expected instead of data";
9234 case SEXP_CHECK_UNKNOWN_OP:
9235 return "Unrecognized operator";
9237 case SEXP_CHECK_TYPE_MISMATCH:
9238 return "Argument type mismatch";
9240 case SEXP_CHECK_BAD_ARG_COUNT:
9241 return "Argument count is illegal";
9243 case SEXP_CHECK_UNKNOWN_TYPE:
9244 return "Unknown operator argument type";
9246 case SEXP_CHECK_INVALID_NUM:
9247 return "Not a number";
9249 case SEXP_CHECK_INVALID_SHIP:
9250 return "Invalid ship name";
9252 case SEXP_CHECK_INVALID_WING:
9253 return "Invalid wing name";
9255 case SEXP_CHECK_INVALID_SUBSYS:
9256 return "Invalid subsystem name";
9258 case SEXP_CHECK_INVALID_IFF:
9259 return "Invalid team name";
9261 case SEXP_CHECK_INVALID_POINT:
9262 return "Invalid point";
9264 case SEXP_CHECK_NEGATIVE_NUM:
9265 return "Negative number not allowed";
9267 case SEXP_CHECK_INVALID_SHIP_WING:
9268 return "Invalid ship/wing name";
9270 case SEXP_CHECK_INVALID_SHIP_TYPE:
9271 return "Invalid ship type";
9273 case SEXP_CHECK_UNKNOWN_MESSAGE:
9274 return "Invalid message name";
9276 case SEXP_CHECK_INVALID_PRIORITY:
9277 return "Invalid priority";
9279 case SEXP_CHECK_INVALID_MISSION_NAME:
9280 return "Invalid mission filename";
9282 case SEXP_CHECK_INVALID_GOAL_NAME:
9283 return "Invalid goal name";
9285 case SEXP_CHECK_INVALID_LEVEL:
9286 return "Mission level too low in tree";
9288 case SEXP_CHECK_INVALID_MSG_SOURCE:
9289 return "Invalid message source";
9291 case SEXP_CHECK_INVALID_DOCKER_POINT:
9292 return "Invalid docker point";
9294 case SEXP_CHECK_INVALID_DOCKEE_POINT:
9295 return "Invalid dockee point";
9297 case SEXP_CHECK_ORDER_NOT_ALLOWED:
9298 return "Ship not allowed to have this order";
9300 case SEXP_CHECK_DOCKING_NOT_ALLOWED:
9301 return "Ship can't dock with target ship";
9303 case SEXP_CHECK_NUM_RANGE_INVALID:
9304 return "Number is out of range";
9306 case SEXP_CHECK_INVALID_EVENT_NAME:
9307 return "Event name is invalid (not known)";
9309 case SEXP_CHECK_INVALID_SKILL_LEVEL:
9310 return "Skill level name is invalid (not known)";
9312 case SEXP_CHECK_INVALID_MEDAL_NAME:
9313 return "Invalid medal name";
9315 case SEXP_CHECK_INVALID_WEAPON_NAME:
9316 return "Invalid weapon name";
9318 case SEXP_CHECK_INVALID_SHIP_CLASS_NAME:
9319 return "Invalid ship class name";
9321 case SEXP_CHECK_INVALID_GAUGE_NAME:
9322 return "Invalid HUD gauges name";
9324 case SEXP_CHECK_INVALID_JUMP_NODE:
9325 return "Invalid Jump Node name";
9328 sprintf(Sexp_error_text, "Sexp error code %d", num);
9329 return Sexp_error_text;
9332 int query_sexp_ai_goal_valid(int sexp_ai_goal, int ship)
9336 for (op=0; op<Num_operators; op++)
9337 if (Operators[op].value == sexp_ai_goal)
9340 Assert(op < Num_operators);
9341 for (i=0; i<Num_sexp_ai_goal_links; i++)
9342 if (Sexp_ai_goal_links[i].op_code == sexp_ai_goal)
9345 Assert(i < Num_sexp_ai_goal_links);
9346 return ai_query_goal_valid(ship, Sexp_ai_goal_links[i].ai_goal);
9349 // Takes an Sexp_node.text pointing to a variable (of form "Sexp_variables[xx]=string" or "Sexp_variables[xx]=number")
9350 // and returns the index into the Sexp_variables array of the actual value
9351 int extract_sexp_variable_index(int node)
9353 char *text = Sexp_nodes[node].text;
9359 start_index = text + 15;
9360 Assert(isdigit(*start_index));
9364 while ( *start_index != ']' ) {
9365 char_index[len++] = *(start_index++);
9370 char_index[len] = 0; // append null termination to string
9372 variable_index = atoi(char_index);
9373 Assert( (variable_index >= 0) && (variable_index < MAX_SEXP_VARIABLES) );
9375 return variable_index;
9379 // wrapper around Sexp_node[xx].text for normal and variable
9382 if (Sexp_nodes[n].type & SEXP_FLAG_VARIABLE) {
9383 int sexp_variable_index;
9385 sexp_variable_index = get_index_sexp_variable_name(Sexp_nodes[n].text);
9386 Assert(sexp_variable_index != -1);
9388 // sexp_variable_index = extract_sexp_variable_index(n);
9389 sexp_variable_index = atoi(Sexp_nodes[n].text);
9391 // Reference a Sexp_variable
9392 // string format -- "Sexp_variables[xx]=number" or "Sexp_variables[xx]=string", where xx is the index
9394 Assert( !(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NOT_USED) );
9395 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9397 return Sexp_variables[sexp_variable_index].text;
9399 return Sexp_nodes[n].text;
9404 // Set all Sexp_variables to type uninitialized
9405 void init_sexp_vars()
9407 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9408 // if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) )
9409 // Sexp_nodes[i].type = SEXP_NOT_USED;
9410 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9415 // Adds an Sexp_variable to be used in a mission.
9416 // This should be called from within mission parse.
9417 int sexp_add_variable(const char *text, const char *var_name, int type, int index)
9419 // if index == -1, find next open slot
9421 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9422 if (Sexp_variables[i].type == SEXP_VARIABLE_NOT_USED) {
9428 Assert( (index >= 0) && (index < MAX_SEXP_VARIABLES) );
9432 strcpy(Sexp_variables[index].text, text);
9433 strcpy(Sexp_variables[index].variable_name, var_name);
9434 Sexp_variables[index].type &= ~SEXP_VARIABLE_NOT_USED;
9435 Sexp_variables[index].type = (type | SEXP_VARIABLE_SET);
9442 // Modifies and Sexp_variable to be used in a mission
9443 // This should be called in mission when an sexp_variable is to be modified
9444 void sexp_modify_variable(char *text, int index)
9446 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9447 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9448 Assert( !MULTIPLAYER_CLIENT );
9450 strcpy(Sexp_variables[index].text, text);
9451 Sexp_variables[index].type |= SEXP_VARIABLE_MODIFIED;
9453 // do multi_callback_here
9456 void sexp_modify_variable(int n)
9458 int sexp_variable_index;
9460 // char *new_char_var;
9461 char number_as_str[TOKEN_LENGTH];
9463 // Only do single player of multi host
9464 if ( MULTIPLAYER_CLIENT ) {
9470 // get sexp_variable index
9471 Assert(Sexp_nodes[n].first == -1);
9472 sexp_variable_index = atoi(Sexp_nodes[n].text);
9474 // verify variable set
9475 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9477 if (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NUMBER) {
9478 // get new numerical value
9479 new_number = eval_sexp(Sexp_nodes[n].rest);
9481 sprintf(number_as_str, "%d", new_number);
9482 sexp_modify_variable(number_as_str, sexp_variable_index);
9485 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);
9487 char *new_text = Sexp_nodes[Sexp_nodes[n].rest].text;
9488 sexp_modify_variable(new_text, sexp_variable_index);
9495 // Different type needed for Fred (1) allow modification of type (2) no callback required
9496 void sexp_fred_modify_variable(const char *text, const char *var_name, int index, int type)
9498 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9499 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9500 Assert( (type & SEXP_VARIABLE_NUMBER) || (type & SEXP_VARIABLE_STRING) );
9502 strcpy(Sexp_variables[index].text, text);
9503 strcpy(Sexp_variables[index].variable_name, var_name);
9504 Sexp_variables[index].type = (SEXP_VARIABLE_SET | SEXP_VARIABLE_MODIFIED | type);
9507 // return index of sexp_variable_name, -1 if not found
9508 int get_index_sexp_variable_name(const char* temp_name)
9510 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9511 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
9512 // check case sensitive
9513 if ( !strcmp(Sexp_variables[i].variable_name, temp_name) ) {
9524 // counts number of sexp_variables that are set
9525 int sexp_variable_count()
9529 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9530 if ( (Sexp_variables[i].type & SEXP_VARIABLE_SET) && !(Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) ) {
9539 // deletes sexp_variable from active
9540 void sexp_variable_delete(int index)
9542 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9544 Sexp_variables[index].type = SEXP_VARIABLE_NOT_USED;
9547 int sexp_var_compare(const void *var1, const void *var2)
9550 sexp_variable *sexp_var1, *sexp_var2;
9552 sexp_var1 = (sexp_variable*) var1;
9553 sexp_var2 = (sexp_variable*) var2;
9555 set1 = sexp_var1->type & SEXP_VARIABLE_SET;
9556 set2 = sexp_var2->type & SEXP_VARIABLE_SET;
9558 if (!set1 && !set2) {
9560 } else if (set1 && !set2) {
9562 } else if (!set1 && set2) {
9565 return stricmp( sexp_var1->variable_name, sexp_var2->variable_name);
9569 // Count number of variables in block
9570 int sexp_variable_block_count()
9573 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9574 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
9582 // Sort sexp_variable list lexigraphically, with set before unset
9583 void sexp_variable_sort()
9585 qsort( (void *)Sexp_variables, (size_t)(MAX_SEXP_VARIABLES - sexp_variable_block_count()), sizeof(sexp_variable), sexp_var_compare );
9588 int sexp_variable_allocate_block(const char* block_name, int block_type)
9590 int num_blocks, block_count, var_count, start;
9591 block_count = sexp_variable_block_count();
9592 var_count = sexp_variable_count();
9594 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9595 num_blocks = BLOCK_EXP_SIZE;
9597 Int3(); // add new block type here with size
9601 if (block_count + var_count > (MAX_SEXP_VARIABLES - num_blocks)) {
9602 // not enough free space
9606 // squeeze all variables to front of array
9607 sexp_variable_sort();
9609 // squeeze all block to end of array
9610 sexp_variable_condense_block();
9612 start = MAX_SEXP_VARIABLES - block_count - num_blocks;
9614 for (int idx=start; idx<start+num_blocks; idx++) {
9615 Assert(Sexp_variables[idx].type == SEXP_VARIABLE_NOT_USED);
9616 Sexp_variables[idx].type = SEXP_VARIABLE_BLOCK | block_type;
9617 strcpy(Sexp_variables[idx].variable_name, block_name);
9623 // squeeze all blocks to top of array
9624 void sexp_variable_condense_block()
9626 int temp_idx, idx, var_count;
9628 var_count = sexp_variable_count();
9629 temp_idx = MAX_SEXP_VARIABLES-1;
9631 for (idx=MAX_SEXP_VARIABLES-1; idx>var_count-1; idx--) {
9632 if (Sexp_variables[idx].type & SEXP_VARIABLE_BLOCK) {
9633 if (temp_idx > idx) {
9634 Sexp_variables[temp_idx] = Sexp_variables[idx];
9635 Sexp_variables[idx].type = SEXP_VARIABLE_NOT_USED;
9643 void sexp_variable_block_free(const char *ship_name, int start_index, int block_type)
9647 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9648 num_blocks = BLOCK_EXP_SIZE;
9650 Int3(); // new type of block
9654 for (int i=start_index; i<(start_index + num_blocks); i++) {
9655 Assert(!stricmp(Sexp_variables[i].variable_name, ship_name));
9657 Assert(Sexp_variables[i].type & block_type);
9659 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9662 sexp_variable_condense_block();
9665 // evaluate number which may result from an operator or may be text
9666 // only useful for first operand
9667 int num_eval(int node)
9669 if ( CAR(node) != -1 ) {
9670 return eval_sexp(CAR(node));
9672 return atoi(CTEXT(node));