2 * $Logfile: /Freespace2/code/parse/SEXP.CPP $
7 * main sexpression generator
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 66 9/13/99 11:38a Andsager
15 * Stupid switch for sexp_team_score. Don't forget *break*
17 * 65 9/09/99 11:40p Dave
18 * Handle an Assert() in beam code. Added supernova sounds. Play the right
19 * 2 end movies properly, based upon what the player did in the mission.
21 * 64 9/07/99 1:05a Andsager
22 * Added team-score sexp for multi team vs team missions
24 * 63 9/06/99 9:46p Jefff
25 * skip mission support
27 * 62 8/29/99 2:33p Andsager
28 * Fix bug in sexp_is_ship_visible()
30 * 61 8/27/99 9:07p Dave
31 * LOD explosions. Improved beam weapon accuracy.
33 * 60 8/27/99 4:07p Andsager
34 * Add is-ship-visible sexp. Make ship-vanish sexp SINGLE player only
36 * 59 8/26/99 4:21p Mikek
37 * Add new special-check for JimB, zeroing front shield quadrant.
39 * 58 8/24/99 4:25p Andsager
40 * Add ship-vanish sexp
42 * 57 8/23/99 6:05p Johnson
43 * Oops. Self destruct log should also go into the is-destroyed
46 * 56 8/23/99 3:23p Johnson
47 * Fix bug in sexp_facing2 when Player_obj does not yet existl
49 * 55 8/19/99 3:30p Andsager
50 * Make sexp_special_warp_dist check if ship if facing knossos device
52 * 54 8/19/99 9:20a Andsager
53 * Enable flashing for all guages
55 * 53 8/19/99 12:31a Andsager
56 * Add support guage as one that can flash
58 * 52 8/18/99 12:09p Andsager
59 * Add debug if message has no anim for message. Make messages come from
62 * 51 8/16/99 10:04p Andsager
63 * Add special-warp-dist and special-warpout-name sexp for Knossos device
66 * 50 8/09/99 3:32p Andsager
67 * Make has_time_elapsed work with arithmatic args.
69 * 49 8/09/99 2:00p Dave
72 * 48 8/02/99 4:26p Dave
73 * Added 2 new sexpressions.
75 * 47 7/31/99 2:30p Dave
76 * Added nifty mission message debug viewing keys.
78 * 46 7/28/99 1:36p Andsager
79 * Modify cargo1 to include flag CARGO_NO_DEPLETE. Add sexp
80 * cargo-no-deplete (only for BIG / HUGE). Modify ship struct to pack
83 * 45 7/24/99 4:56p Dave
84 * Added 3 new sexpressions.
86 * 44 7/21/99 8:10p Dave
87 * First run of supernova effect.
89 * 43 7/20/99 9:19p Andsager
90 * Added facing waypoint sexp
92 * 42 7/20/99 9:54a Andsager
93 * Add subsys-set-random sexp
95 * 41 7/19/99 12:02p Andsager
96 * Allow AWACS on any ship subsystem. Fix sexp_set_subsystem_strength to
97 * only blow up subsystem if its strength is > 0
99 * 40 7/15/99 9:22p Andsager
100 * modify secondaries_depleted sexp to report true only when all
103 * 39 7/13/99 3:37p Andsager
104 * Add secondaries-depleted sexp
106 * 38 7/08/99 12:06p Andsager
107 * Add turret-tagged-only and turret-tagged-clear sexp.
109 * 37 6/28/99 4:51p Andsager
110 * Add ship-guardian sexp (does not allow ship to be killed)
112 * 36 6/25/99 2:51p Andsager
113 * Make event_delay, goal_delay, and subsys strength work with sexp_rand
115 * 35 6/23/99 5:51p Andsager
116 * Add waypoint-cap-speed. Checkin stealth ai - inactive.
118 * 34 6/16/99 10:21a Dave
119 * Added send-message-list sexpression.
121 * 33 6/15/99 2:53p Andsager
122 * Fixed subsystem checking for AWACS type subsystems.
124 * 32 6/01/99 8:35p Dave
125 * Finished lockarm weapons. Added proper supercap weapons/damage. Added
126 * awacs-set-radius sexpression.
128 * 31 5/27/99 12:14p Andsager
129 * Some fixes for live debris when more than one subsys on ship with live
130 * debris. Set subsys strength (when 0) blows off subsystem.
131 * sexp_hits_left_subsystem works for SUBSYSTEM_UNKNOWN.
133 * 30 5/24/99 11:28a Dave
134 * Sexpression for adding/removing ships from the hud escort list.
136 * 29 4/28/99 9:33a Andsager
137 * Add turret-free and turret-lock (and -all) sexp. Stargger start time
138 * of beam weapons beam-free and beam-free-all.
140 * 28 4/26/99 2:14p Andsager
141 * Add beam-protect-ship and beam-unprotect-ship sexp.
143 * 27 4/23/99 12:01p Johnson
144 * Added SIF_HUGE_SHIP
146 * 26 4/23/99 9:45a Andsager
147 * Modify rand_sexp to return the same value each time it is called on a
148 * particular node. This prevents random time delay defaulting to min.
150 * 25 4/21/99 6:15p Dave
151 * Did some serious housecleaning in the beam code. Made it ready to go
152 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
153 * a handy macro for recalculating collision pairs for a given object.
155 * 24 4/19/99 2:28p Johnson
156 * DA: Use ship_info flags, not ship_flags for ship type. DOH!!
158 * 23 4/19/99 11:46a Johnson
161 * 22 4/19/99 11:36a Johnson
162 * Don't display multiple warning messges when large ship gets more than 1
165 * 21 4/02/99 9:55a Dave
166 * Added a few more options in the weapons.tbl for beam weapons. Attempt
167 * at putting "pain" packets into multiplayer.
169 * 20 3/31/99 2:22p Johnson
170 * Fix sexp_repair_subsystem() to accept sexp increments
172 * 19 3/28/99 5:58p Dave
173 * Added early demo code. Make objects move. Nice and framerate
174 * independant, but not much else. Don't use yet unless you're me :)
176 * 18 3/20/99 3:46p Dave
177 * Added support for model-based background nebulae. Added 3 new
180 * 17 3/08/99 7:03p Dave
181 * First run of new object update system. Looks very promising.
183 * 16 3/04/99 6:09p Dave
184 * Added in sexpressions for firing beams and checking for if a ship is
187 * 15 3/04/99 9:22a Andsager
188 * Make escort list work with ship-is-visible. When not visible, dump,
189 * when becoming visible, maybe add.
191 * 14 2/26/99 6:01p Andsager
192 * Add sexp has-been-tagged-delay and cap-subsys-cargo-known-delay
194 * 13 2/26/99 4:14p Dave
195 * Put in the ability to have multiple shockwaves for ships.
197 * 12 2/11/99 5:22p Andsager
198 * Fixed bugs, generalized block Sexp_variables
200 * 11 2/11/99 2:15p Andsager
201 * Add ship explosion modification to FRED
203 * 10 1/25/99 4:29p Andsager
204 * Modify sexp_modify_variable() to handle type string. added quick out
205 * for multiplayer_client
207 * 9 1/25/99 8:11a Andsager
208 * Add sexp_modify_variable(). Changed syntax checking to allow, adding
209 * operator return type ambiguous
211 * 8 1/24/99 11:37p Dave
212 * First full rev of beam weapons. Very customizable. Removed some bogus
213 * Int3()'s in low level net code.
215 * 7 1/20/99 9:37a Andsager
217 * 6 1/19/99 3:57p Andsager
218 * Round 2 of variables
220 * 5 1/07/99 1:52p Andsager
221 * Initial check in of Sexp_variables
223 * 4 11/05/98 5:55p Dave
224 * Big pass at reducing #includes
226 * 3 10/13/98 9:29a Dave
227 * Started neatening up freespace.h. Many variables renamed and
228 * reorganized. Added AlphaColors.[h,cpp]
230 * 2 10/07/98 10:53a Dave
233 * 1 10/07/98 10:50a Dave
235 * 321 9/20/98 7:20p Dave
236 * Added CHANGE_IFF packet.
238 * 320 9/16/98 10:42a Hoffoss
239 * Added sexp node counting to fsm files for end user designers.
241 * 319 7/06/98 4:31p Hoffoss
242 * Fixed bug in sexp parsing when formula is ( ).
244 * 318 6/09/98 5:15p Lawrance
245 * French/German localization
247 * 317 5/26/98 11:54a Allender
248 * fix multiplayer problems and sexpression crash
250 * 316 5/23/98 3:16p Allender
251 * work on object update packet optimizations (a new updating system).
252 * Don't allow medals/promotions/badges when playing singple player
253 * missions through the simulator
255 * 315 5/20/98 1:34a Allender
256 * fixed observer problems. Removed some code offending BoundsCheccker.
257 * Fixed medals for multiplayer
259 * 314 5/17/98 12:46p Mike
260 * Change special-check functions for cueing rearm suggestion.
262 * 313 5/14/98 6:29p Hoffoss
263 * Fixed some warnings a release rebuild all turned up.
265 * 312 5/14/98 10:15a Allender
266 * add optional argument to prevous-goal/event operators to specify what
267 * sexpression should return when being played as a single mission
269 * 311 5/08/98 10:16a Lawrance
270 * Add new "ship attacking count" gauge
272 * 310 4/29/98 11:39p Allender
273 * fixed nasty problem with is-event-false always returning true right
279 // Parse a symbolic expression.
280 // These are identical to Lisp functions.
281 // It uses a very baggy format, allocating 16 characters per token, regardless
282 // of how many are used.
294 #include "freespace.h"
297 #include "missionlog.h"
298 #include "missionparse.h" // for p_object definition
299 #include "missionmessage.h"
300 #include "missiontraining.h"
302 #include "linklist.h"
303 #include "model.h" // for subsystem types
305 #include "missioncampaign.h"
306 #include "missiongoals.h"
307 #include "controlsconfig.h"
310 #include "gamesequence.h"
314 #include "hudmessage.h"
316 #include "redalert.h"
317 #include "jumpnode.h"
318 #include "hudshield.h"
320 #include "multimsgs.h"
321 #include "multiutil.h"
322 #include "hudescort.h"
324 #include "supernova.h"
328 #include "multi_team.h"
333 sexp_oper Operators[] = {
334 // Operator, Identity, Min / Max arguments
335 { "+", OP_PLUS, 2, INT_MAX },
336 { "-", OP_MINUS, 2, INT_MAX },
337 { "*", OP_MUL, 2, INT_MAX },
338 { "/", OP_DIV, 2, INT_MAX },
339 { "mod", OP_MOD, 2, INT_MAX },
340 { "rand", OP_RAND, 2, 2 },
342 { "true", OP_TRUE, 0, 0, },
343 { "false", OP_FALSE, 0, 0, },
344 { "and", OP_AND, 2, INT_MAX, },
345 { "and-in-sequence", OP_AND_IN_SEQUENCE, 2, INT_MAX, },
346 { "or", OP_OR, 2, INT_MAX, },
347 { "not", OP_NOT, 1, 1, },
348 { "=", OP_EQUALS, 2, INT_MAX, },
349 { ">", OP_GREATER_THAN, 2, 2, },
350 { "<", OP_LESS_THAN, 2, 2, },
351 { "is-iff", OP_IS_IFF, 2, INT_MAX, },
352 { "has-time-elapsed", OP_HAS_TIME_ELAPSED, 1, 1, },
353 { "modify-variable", OP_MODIFY_VARIABLE, 2, 2, },
355 { "is-goal-incomplete", OP_GOAL_INCOMPLETE, 1, 1, },
356 { "is-goal-true-delay", OP_GOAL_TRUE_DELAY, 2, 2, },
357 { "is-goal-false-delay", OP_GOAL_FALSE_DELAY, 2, 2, },
358 { "is-event-incomplete", OP_EVENT_INCOMPLETE, 1, 1, },
359 { "is-event-true-delay", OP_EVENT_TRUE_DELAY, 2, 2, },
360 { "is-event-false-delay", OP_EVENT_FALSE_DELAY, 2, 2, },
361 { "is-previous-goal-incomplete", OP_PREVIOUS_GOAL_INCOMPLETE, 2, 3, },
362 { "is-previous-goal-true", OP_PREVIOUS_GOAL_TRUE, 2, 3, },
363 { "is-previous-goal-false", OP_PREVIOUS_GOAL_FALSE, 2, 3, },
364 { "is-previous-event-incomplete", OP_PREVIOUS_EVENT_INCOMPLETE, 2, 3, },
365 { "is-previous-event-true", OP_PREVIOUS_EVENT_TRUE, 2, 3, },
366 { "is-previous-event-false", OP_PREVIOUS_EVENT_FALSE, 2, 3, },
368 { "is-destroyed", OP_IS_DESTROYED, 1, INT_MAX, },
369 { "is-subsystem-destroyed", OP_IS_SUBSYSTEM_DESTROYED, 2, 2, },
370 { "is-disabled", OP_IS_DISABLED, 1, INT_MAX, },
371 { "is-disarmed", OP_IS_DISARMED, 1, INT_MAX, },
372 { "has-docked", OP_HAS_DOCKED, 3, 3, },
373 { "has-undocked", OP_HAS_UNDOCKED, 3, 3, },
374 { "has-arrived", OP_HAS_ARRIVED, 1, INT_MAX, },
375 { "has-departed", OP_HAS_DEPARTED, 1, INT_MAX, },
376 { "is-destroyed-delay", OP_IS_DESTROYED_DELAY, 2, INT_MAX, },
377 { "is-subsystem-destroyed-delay", OP_IS_SUBSYSTEM_DESTROYED_DELAY, 3, 3, },
378 { "is-disabled-delay", OP_IS_DISABLED_DELAY, 2, INT_MAX, },
379 { "is-disarmed-delay", OP_IS_DISARMED_DELAY, 2, INT_MAX, },
380 { "has-docked-delay", OP_HAS_DOCKED_DELAY, 4, 4, },
381 { "has-undocked-delay", OP_HAS_UNDOCKED_DELAY, 4, 4, },
382 { "has-arrived-delay", OP_HAS_ARRIVED_DELAY, 2, INT_MAX, },
383 { "has-departed-delay", OP_HAS_DEPARTED_DELAY, 2, INT_MAX, },
384 { "are-waypoints-done", OP_WAYPOINTS_DONE, 2, 2, },
385 { "are-waypoints-done-delay", OP_WAYPOINTS_DONE_DELAY, 3, 3, },
386 { "ship-type-destroyed", OP_SHIP_TYPE_DESTROYED, 2, 2, },
387 { "is-event-true", OP_EVENT_TRUE, 1, 1, },
388 { "is-event-false", OP_EVENT_FALSE, 1, 1, },
389 { "is-cargo-known", OP_IS_CARGO_KNOWN, 1, INT_MAX, },
390 { "was-promotion-granted", OP_WAS_PROMOTION_GRANTED, 0, 1, },
391 { "was-medal-granted", OP_WAS_MEDAL_GRANTED, 0, 1, },
392 { "percent-ships-departed", OP_PERCENT_SHIPS_DEPARTED, 2, INT_MAX },
393 { "percent-ships-destroyed", OP_PERCENT_SHIPS_DESTROYED, 2, INT_MAX },
394 { "is-cargo-known-delay", OP_CARGO_KNOWN_DELAY, 2, INT_MAX, },
395 { "cap-subsys-cargo-known-delay", OP_CAP_SUBSYS_CARGO_KNOWN_DELAY, 3, INT_MAX, },
396 { "has-been-tagged-delay", OP_HAS_BEEN_TAGGED_DELAY, 2, INT_MAX, },
397 { "depart-node-delay", OP_DEPART_NODE_DELAY, 3, INT_MAX, },
398 { "destroyed-or-departed-delay", OP_DESTROYED_DEPARTED_DELAY, 2, INT_MAX, },
399 { "is_tagged", OP_IS_TAGGED, 1, 1 },
400 { "num_kills", OP_NUM_KILLS, 1, 1 },
401 { "num_type_kills", OP_NUM_TYPE_KILLS, 2, 2 },
402 { "num_class_kills", OP_NUM_CLASS_KILLS, 2, 2 },
403 { "shield-recharge-pct", OP_SHIELD_RECHARGE_PCT, 1, 1 },
404 { "engine-recharge-pct", OP_ENGINE_RECHARGE_PCT, 1, 1 },
405 { "weapon-recharge-pct", OP_WEAPON_RECHARGE_PCT, 1, 1 },
406 { "shield-quad-low", OP_SHIELD_QUAD_LOW, 2, 2 },
407 { "secondary-ammo-pct", OP_SECONDARY_AMMO_PCT, 2, 2 },
408 { "is-secondary-selected", OP_IS_SECONDARY_SELECTED, 2, 2 },
409 { "is-primary-selected", OP_IS_PRIMARY_SELECTED, 2, 2 },
411 { "time-ship-destroyed", OP_TIME_SHIP_DESTROYED, 1, 1, },
412 { "time-ship-arrived", OP_TIME_SHIP_ARRIVED, 1, 1, },
413 { "time-ship-departed", OP_TIME_SHIP_DEPARTED, 1, 1, },
414 { "time-wing-destroyed", OP_TIME_WING_DESTROYED, 1, 1, },
415 { "time-wing-arrived", OP_TIME_WING_ARRIVED, 1, 1, },
416 { "time-wing-departed", OP_TIME_WING_DEPARTED, 1, 1, },
417 { "mission-time", OP_MISSION_TIME, 0, 0, },
418 { "time-docked", OP_TIME_DOCKED, 3, 3, },
419 { "time-undocked", OP_TIME_UNDOCKED, 3, 3, },
421 { "shields-left", OP_SHIELDS_LEFT, 1, 1, },
422 { "hits-left", OP_HITS_LEFT, 1, 1, },
423 { "hits-left-subsystem", OP_HITS_LEFT_SUBSYSTEM, 2, 2, },
424 { "distance", OP_DISTANCE, 2, 2, },
425 { "is-ship-visible", OP_IS_SHIP_VISIBLE, 1, 1, },
426 { "team-score", OP_TEAM_SCORE, 1, 1, },
427 { "time-elapsed-last-order", OP_LAST_ORDER_TIME, 2, 2, /*INT_MAX*/ },
428 { "skill-level-at-least", OP_SKILL_LEVEL_AT_LEAST, 1, 1, },
429 { "num-players", OP_NUM_PLAYERS, 0, 0, },
430 { "special-warp-dist", OP_SPECIAL_WARP_DISTANCE, 1, 1, },
432 { "special-warpout-name", OP_SET_SPECIAL_WARPOUT_NAME, 2, 2 },
434 { "do-nothing", OP_NOP, 0, 0, },
435 { "when", OP_WHEN, 2, INT_MAX, },
436 { "cond", OP_COND, 1, INT_MAX, },
438 { "add-goal", OP_ADD_GOAL, 2, 2, },
439 { "add-ship-goal", OP_ADD_SHIP_GOAL, 2, 2, },
440 { "add-wing-goal", OP_ADD_WING_GOAL, 2, 2, },
441 { "clear-goals", OP_CLEAR_GOALS, 1, INT_MAX, },
442 { "clear-ship-goals", OP_CLEAR_SHIP_GOALS, 1, 1, },
443 { "clear-wing-goals", OP_CLEAR_WING_GOALS, 1, 1, },
444 { "change-iff", OP_CHANGE_IFF, 2, INT_MAX, },
445 { "protect-ship", OP_PROTECT_SHIP, 1, INT_MAX, },
446 { "unprotect-ship", OP_UNPROTECT_SHIP, 1, INT_MAX, },
447 { "beam-protect-ship", OP_BEAM_PROTECT_SHIP, 1, INT_MAX, },
448 { "beam-unprotect-ship", OP_BEAM_UNPROTECT_SHIP, 1, INT_MAX, },
449 { "send-message", OP_SEND_MESSAGE, 3, 3, },
450 { "self-destruct", OP_SELF_DESTRUCT, 1, INT_MAX, },
451 { "next-mission", OP_NEXT_MISSION, 1, 1, },
452 { "end-campaign", OP_END_CAMPAIGN, 0, 0, },
453 { "end-of-campaign", OP_END_OF_CAMPAIGN, 0, 0, },
454 { "sabotage-subsystem", OP_SABOTAGE_SUBSYSTEM, 3, 3, },
455 { "repair-subsystem", OP_REPAIR_SUBSYSTEM, 3, 3, },
456 { "set-subsystem-strength", OP_SET_SUBSYSTEM_STRNGTH, 3, 3, },
457 { "invalidate-goal", OP_INVALIDATE_GOAL, 1, INT_MAX, },
458 { "validate-goal", OP_VALIDATE_GOAL, 1, INT_MAX, },
459 { "send-random-message", OP_SEND_RANDOM_MESSAGE, 3, INT_MAX, },
460 { "transfer-cargo", OP_TRANSFER_CARGO, 2, 2, },
461 { "exchange-cargo", OP_EXCHANGE_CARGO, 2, 2, },
462 { "end-misison-delay", OP_END_MISSION_DELAY, 1, 1, },
463 { "good-rearm-time", OP_GOOD_REARM_TIME, 2, 2, },
464 { "grant-promotion", OP_GRANT_PROMOTION, 0, 0, },
465 { "grant-medal", OP_GRANT_MEDAL, 1, 1, },
466 { "allow-ship", OP_ALLOW_SHIP, 1, 1, },
467 { "allow-weapon", OP_ALLOW_WEAPON, 1, 1, },
468 { "break-warp", OP_WARP_BROKEN, 1, INT_MAX, },
469 { "fix-warp", OP_WARP_NOT_BROKEN, 1, INT_MAX, },
470 { "never-warp", OP_WARP_NEVER, 1, INT_MAX, },
471 { "allow-warp", OP_WARP_ALLOWED, 1, INT_MAX, },
472 { "good-secondary-time", OP_GOOD_SECONDARY_TIME, 4, 4, },
473 { "ship-invisible", OP_SHIP_INVISIBLE, 1, INT_MAX },
474 { "ship-visible", OP_SHIP_VISIBLE, 1, INT_MAX },
475 { "ship-invulnerable", OP_SHIP_INVULNERABLE, 1, INT_MAX },
476 { "ship-vulnerable", OP_SHIP_VULNERABLE, 1, INT_MAX },
477 { "red-alert", OP_RED_ALERT, 0, 0 },
478 { "tech-add-ships", OP_TECH_ADD_SHIP, 1, INT_MAX },
479 { "tech-add-weapons", OP_TECH_ADD_WEAPON, 1, INT_MAX },
480 { "jettison-cargo-delay", OP_JETTISON_CARGO, 2, 2 },
481 { "fire-beam", OP_BEAM_FIRE, 3, 4 },
482 { "beam-free", OP_BEAM_FREE, 2, INT_MAX },
483 { "beam-free-all", OP_BEAM_FREE_ALL, 1, 1 },
484 { "beam-lock", OP_BEAM_LOCK, 2, INT_MAX },
485 { "beam-lock-all", OP_BEAM_LOCK_ALL, 1, 1 },
486 { "turret-free", OP_TURRET_FREE, 2, INT_MAX },
487 { "turret-free-all", OP_TURRET_FREE_ALL, 1, 1 },
488 { "turret-lock", OP_TURRET_LOCK, 2, INT_MAX },
489 { "turret-lock-all", OP_TURRET_LOCK_ALL, 1, 1 },
490 { "add-remove-escort", OP_ADD_REMOVE_ESCORT, 2, 2 },
491 { "awacs-set-radius", OP_AWACS_SET_RADIUS, 3, 3 },
492 { "send-message-list", OP_SEND_MESSAGE_LIST, 4, INT_MAX },
493 { "cap-waypoint-speed", OP_CAP_WAYPOINT_SPEED, 2, 2 },
494 { "ship-guardian", OP_SHIP_GUARDIAN, 1, INT_MAX },
495 { "ship-no-guardian", OP_SHIP_NO_GUARDIAN, 1, INT_MAX },
496 { "ship-vanish", OP_SHIP_VANISH, 1, INT_MAX },
497 { "turret-tagged-only", OP_TURRET_TAGGED_ONLY_ALL, 1, 1 },
498 { "turret-tagged-clear", OP_TURRET_TAGGED_CLEAR_ALL, 1, 1 },
499 { "subsys-set-random", OP_SUBSYS_SET_RANDOM, 3, INT_MAX },
500 { "supernova-start", OP_SUPERNOVA_START, 1, 1 },
501 { "cargo-no-deplete", OP_CARGO_NO_DEPLETE, 1, 2 },
503 { "error", OP_INT3, 0, 0 },
505 { "ai-chase", OP_AI_CHASE, 2, 2, },
506 { "ai-chase-wing", OP_AI_CHASE_WING, 2, 2, },
507 { "ai-dock", OP_AI_DOCK, 4, 4, },
508 { "ai-undock", OP_AI_UNDOCK, 1, 1, },
509 { "ai-warp", OP_AI_WARP, 2, 2, },
510 { "ai-warp-out", OP_AI_WARP_OUT, 1, 1, },
511 { "ai-waypoints", OP_AI_WAYPOINTS, 2, 2, },
512 { "ai-waypoints-once", OP_AI_WAYPOINTS_ONCE, 2, 2, },
513 { "ai-destroy-subsystem", OP_AI_DESTROY_SUBSYS, 3, 3, },
514 { "ai-disable-ship", OP_AI_DISABLE_SHIP, 2, 2, },
515 { "ai-disarm-ship", OP_AI_DISARM_SHIP, 2, 2, },
516 { "ai-guard", OP_AI_GUARD, 2, 2, },
517 { "ai-chase-any", OP_AI_CHASE_ANY, 1, 1, },
518 { "ai-guard-wing", OP_AI_GUARD_WING, 2, 2, },
519 { "ai-evade-ship", OP_AI_EVADE_SHIP, 2, 2, },
520 { "ai-stay-near-ship", OP_AI_STAY_NEAR_SHIP, 2, 2, },
521 { "ai-keep-safe-distance", OP_AI_KEEP_SAFE_DISTANCE, 1, 1, },
522 { "ai-ignore", OP_AI_IGNORE, 2, 2, },
523 { "ai-stay-still", OP_AI_STAY_STILL, 2, 2, },
524 { "ai-play-dead", OP_AI_PLAY_DEAD, 1, 1, },
526 { "goals", OP_GOALS_ID, 1, INT_MAX, },
528 { "key-pressed", OP_KEY_PRESSED, 1, 2, },
529 { "key-reset", OP_KEY_RESET, 1, INT_MAX, },
530 { "targeted", OP_TARGETED, 1, 3, },
531 { "speed", OP_SPEED, 1, 1, },
532 { "facing", OP_FACING, 2, 2, },
533 { "facing-waypoint", OP_FACING2, 2, 2, },
534 { "order", OP_ORDER, 2, 2, },
535 { "waypoint-missed", OP_WAYPOINT_MISSED, 0, 0, },
536 { "waypoint-twice", OP_WAYPOINT_TWICE, 0, 0, },
537 { "path-flown", OP_PATH_FLOWN, 0, 0, },
538 { "training-msg", OP_TRAINING_MSG, 1, 4, },
539 { "flash-hud-gauge", OP_FLASH_HUD_GAUGE, 1, 1, },
540 { "special-check", OP_SPECIAL_CHECK, 1, 1, },
541 { "secondaries-depleted", OP_SECONDARIES_DEPLETED, 1, 1, },
543 { "set-training-context-fly-path", OP_SET_TRAINING_CONTEXT_FLY_PATH, 2, 2, },
544 { "set-training-context-speed", OP_SET_TRAINING_CONTEXT_SPEED, 2, 2, },
547 sexp_ai_goal_link Sexp_ai_goal_links[] = {
548 { AI_GOAL_CHASE, OP_AI_CHASE },
549 { AI_GOAL_CHASE_WING, OP_AI_CHASE_WING },
550 { AI_GOAL_DOCK, OP_AI_DOCK },
551 { AI_GOAL_UNDOCK, OP_AI_UNDOCK },
552 { AI_GOAL_WARP, OP_AI_WARP_OUT },
553 { AI_GOAL_WARP, OP_AI_WARP },
554 { AI_GOAL_WAYPOINTS, OP_AI_WAYPOINTS },
555 { AI_GOAL_WAYPOINTS_ONCE, OP_AI_WAYPOINTS_ONCE },
556 { AI_GOAL_DESTROY_SUBSYSTEM, OP_AI_DESTROY_SUBSYS },
557 { AI_GOAL_DISABLE_SHIP, OP_AI_DISABLE_SHIP },
558 { AI_GOAL_DISARM_SHIP, OP_AI_DISARM_SHIP },
559 { AI_GOAL_GUARD, OP_AI_GUARD },
560 { AI_GOAL_CHASE_ANY, OP_AI_CHASE_ANY },
561 { AI_GOAL_GUARD_WING, OP_AI_GUARD_WING },
562 { AI_GOAL_EVADE_SHIP, OP_AI_EVADE_SHIP },
563 { AI_GOAL_STAY_NEAR_SHIP, OP_AI_STAY_NEAR_SHIP },
564 { AI_GOAL_KEEP_SAFE_DISTANCE, OP_AI_KEEP_SAFE_DISTANCE },
565 { AI_GOAL_IGNORE, OP_AI_IGNORE },
566 { AI_GOAL_STAY_STILL, OP_AI_STAY_STILL },
567 { AI_GOAL_PLAY_DEAD, OP_AI_PLAY_DEAD },
570 char *HUD_gauge_text[NUM_HUD_GAUGES] =
582 "TARGET_MONITOR_EXTRA_DATA",
583 "TARGET_SHIELD_ICON",
584 "PLAYER_SHIELD_ICON",
592 "AFTERBURNER_ENERGY",
594 "WEAPON_LINKING_GAUGE",
596 "OFFSCREEN_INDICATOR",
600 "MISSILE_WARNING_ARROW",
602 "OBJECTIVES_NOTIFY_GAUGE",
606 "ATTACKING TARGET COUNT",
615 int Sexp_useful_number; // a variable to pass useful info in from external modules
616 int Locked_sexp_true, Locked_sexp_false;
617 int Num_operators = sizeof(Operators) / sizeof(sexp_oper);
618 int Num_sexp_ai_goal_links = sizeof(Sexp_ai_goal_links) / sizeof(sexp_ai_goal_link);
620 int Sexp_clipboard = -1; // used by Fred
621 int Training_context = 0;
622 int Training_context_speed_set;
623 int Training_context_speed_min;
624 int Training_context_speed_max;
625 int Training_context_speed_timestamp;
626 int Training_context_path;
627 int Training_context_goal_waypoint;
628 int Training_context_at_waypoint;
629 float Training_context_distance;
630 char Sexp_error_text[MAX_SEXP_TEXT];
631 char *Sexp_string; //[1024] = {0};
632 sexp_node Sexp_nodes[MAX_SEXP_NODES];
633 sexp_variable Sexp_variables[MAX_SEXP_VARIABLES];
635 int Players_target = UNINITIALIZED;
636 ship_subsys *Players_targeted_subsys;
637 int Players_target_timestamp;
639 int get_sexp(char *token);
640 int eval_sexp(int cur_node);
641 void build_extended_sexp_string(int cur_node, int level, int mode);
642 void update_sexp_references(char *old_name, char *new_name, int format, int node);
643 int sexp_determine_team(char *subj);
644 int sexp_distance2(int obj1, char *subj);
645 int sexp_distance3(int obj1, int obj2);
646 int extract_sexp_variable_index(int node);
647 void init_sexp_vars();
648 int num_eval(int node);
654 for (i=0; i<MAX_SEXP_NODES; i++) {
655 if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) ){
656 Sexp_nodes[i].type = SEXP_NOT_USED;
662 Locked_sexp_false = Locked_sexp_true = -1;
663 Locked_sexp_false = alloc_sexp("false", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
664 Assert(Locked_sexp_false != -1);
665 Sexp_nodes[Locked_sexp_false].type = SEXP_ATOM; // fix bypassing value
666 Locked_sexp_true = alloc_sexp("true", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
667 Assert(Locked_sexp_true != -1);
668 Sexp_nodes[Locked_sexp_true].type = SEXP_ATOM; // fix bypassing value
671 // allocates an sexp node.
672 int alloc_sexp(char *text, int type, int subtype, int first, int rest)
676 i = find_operator(text);
677 if ((i == OP_TRUE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
678 return Locked_sexp_true;
679 } else if ((i == OP_FALSE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
680 return Locked_sexp_false;
683 i = find_free_sexp();
684 Assert(i != Locked_sexp_true);
685 Assert(i != Locked_sexp_false);
686 if (i == MAX_SEXP_NODES){
690 Assert(strlen(text) < TOKEN_LENGTH);
691 strcpy(Sexp_nodes[i].text, text);
693 Sexp_nodes[i].type = type;
694 Sexp_nodes[i].subtype = subtype;
695 Sexp_nodes[i].first = first;
696 Sexp_nodes[i].rest = rest;
697 Sexp_nodes[i].value = SEXP_UNKNOWN;
703 int count_free_sexp_nodes()
707 for (i=0; i<MAX_SEXP_NODES; i++) {
708 if (Sexp_nodes[i].type == SEXP_NOT_USED){
710 } else if (Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT){
715 if (MAX_SEXP_NODES - f > Sexp_hwm) {
716 nprintf(("Sexp", "Sexp nodes: Free=%d, Used=%d, Persistent=%d\n", f, MAX_SEXP_NODES - f, p));
717 Sexp_hwm = MAX_SEXP_NODES - f;
723 // find the next free sexp and return it's index.
728 for (i=0; i<MAX_SEXP_NODES; i++){
729 if (Sexp_nodes[i].type == SEXP_NOT_USED){
735 //count_free_sexp_nodes();
738 Assert(i != MAX_SEXP_NODES); // time to raise the limit..
739 if (i == MAX_SEXP_NODES){
746 // sexp_mark_persistent() marks a whole sexp tree with the persistent flag so that it won't
747 // get re-used between missions
748 void sexp_mark_persistent( int n )
754 // total hack because of the true/false locked sexps -- we should make those persistent as well
755 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
759 Assert( !(Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT) );
760 Sexp_nodes[n].type |= SEXP_FLAG_PERSISTENT;
762 sexp_mark_persistent(Sexp_nodes[n].first);
763 sexp_mark_persistent(Sexp_nodes[n].rest);
767 // sexp_unmark_persistent() removes the persistent flag from all nodes in the tree
768 void sexp_unmark_persistent( int n )
774 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
778 Assert( Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT );
779 Sexp_nodes[n].type &= ~SEXP_FLAG_PERSISTENT;
781 sexp_unmark_persistent(Sexp_nodes[n].first);
782 sexp_unmark_persistent(Sexp_nodes[n].rest);
785 // just frees up the specified sexp node, Leaves link chains untouched.
786 int free_one_sexp(int num)
788 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
789 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
791 if ((num == Locked_sexp_true) || (num == Locked_sexp_false)){
795 Sexp_nodes[num].type = SEXP_NOT_USED;
799 // frees a used sexp node, so it can be reused later. Should only be called on
800 // an atom or a list, and not an operator. If on a list, the list and everything
801 // in it will be freed (including the operator).
802 int free_sexp(int num)
804 int i, rest, count = 0;
806 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
807 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
809 if ((num == Locked_sexp_true) || (num == Locked_sexp_false) || (num == -1) ){
813 Sexp_nodes[num].type = SEXP_NOT_USED;
816 i = Sexp_nodes[num].first;
818 count += free_sexp(i);
819 i = Sexp_nodes[i].rest;
822 rest = Sexp_nodes[num].rest;
823 for (i=0; i<MAX_SEXP_NODES; i++) {
824 if (Sexp_nodes[i].first == num){
825 Sexp_nodes[i].first = rest;
828 if (Sexp_nodes[i].rest == num){
829 Sexp_nodes[i].rest = rest;
833 return count; // total elements freed up.
836 // used to free up an entire sexp tree. Because the root node is an operator, instead of
837 // a list, we can't simply call free_sexp(). This function should only be called on the
838 // root node of an sexp, otherwise the linking will get screwed up.
839 int free_sexp2(int num)
843 if ((num == -1) || (num == Locked_sexp_true) || (num == Locked_sexp_false)){
847 i = Sexp_nodes[num].rest;
849 count += free_sexp(i);
850 i = Sexp_nodes[i].rest;
853 count += free_sexp(num);
857 // This function resets the status of all the nodes in a tree, forcing them to all be
859 void flush_sexp_tree(int node)
865 Sexp_nodes[node].value = SEXP_UNKNOWN;
866 flush_sexp_tree(Sexp_nodes[node].first);
867 flush_sexp_tree(Sexp_nodes[node].rest);
870 int verify_sexp_tree(int node)
876 if ((Sexp_nodes[node].type == SEXP_NOT_USED) ||
877 (Sexp_nodes[node].first == node) ||
878 (Sexp_nodes[node].rest == node)) {
879 Error(LOCATION, "Sexp node is corrupt");
883 if (Sexp_nodes[node].first != -1){
884 verify_sexp_tree(Sexp_nodes[node].first);
886 if (Sexp_nodes[node].rest != -1){
887 verify_sexp_tree(Sexp_nodes[node].rest);
893 int dup_sexp_chain(int node)
895 int cur, first, rest;
901 // TODO - CASE OF SEXP VARIABLES - ONLY 1 COPY OF VARIABLE
902 first = dup_sexp_chain(Sexp_nodes[node].first);
903 rest = dup_sexp_chain(Sexp_nodes[node].rest);
904 cur = alloc_sexp(Sexp_nodes[node].text, Sexp_nodes[node].type, Sexp_nodes[node].subtype, first, rest);
918 // returns 1 if they are the same, 0 if different
919 int cmp_sexp_chains(int node1, int node2)
921 if ((node1 == -1) && (node2 == -1)){
925 if ((node1 == -1) || (node2 == -1)){
929 // DA: 1/7/99 Need to check the actual Sexp_node.text, not possible variable, which can be equal
930 if (stricmp(Sexp_nodes[node1].text, Sexp_nodes[node2].text)){
934 if (!cmp_sexp_chains(Sexp_nodes[node1].first, Sexp_nodes[node2].first)){
938 if (!cmp_sexp_chains(Sexp_nodes[node1].rest, Sexp_nodes[node2].rest)){
945 // determine if an sexp node is within the given sexp chain.
946 int query_node_in_sexp(int node, int sexp)
955 if (query_node_in_sexp(node, Sexp_nodes[sexp].first)){
958 if (query_node_in_sexp(node, Sexp_nodes[sexp].rest)){
965 // find the index of the list associated with an operator
966 int find_sexp_list(int num)
970 for (i=0; i<MAX_SEXP_NODES; i++){
971 if (Sexp_nodes[i].first == num){
979 // find index of operator that item is an argument of.
980 int find_parent_operator(int num)
984 if (Sexp_nodes[num].subtype == SEXP_ATOM_OPERATOR){
985 num = find_sexp_list(num);
988 while (Sexp_nodes[num].subtype != SEXP_ATOM_OPERATOR) {
989 for (i=0; i<MAX_SEXP_NODES; i++){
990 if (Sexp_nodes[i].rest == num){
995 if (i == MAX_SEXP_NODES){
996 return -1; // not found, probably at top node already.
1005 // function to determine if an sexpression node is the top level node of an sexpression tree. Top
1006 // level nodes do not have their node id in anyone elses first or rest index
1007 int is_sexp_top_level( int node )
1011 if ( Sexp_nodes[node].type == SEXP_NOT_USED ){
1015 for (i = 0; i < MAX_SEXP_NODES; i++ ) {
1016 if ( (Sexp_nodes[i].type == SEXP_NOT_USED) || ( i == node ) ){ // don't check myself or unused nodes
1019 if ( (Sexp_nodes[i].first == node) || (Sexp_nodes[i].rest == node) ){
1027 int identify_operator(char *token)
1031 for (i=0; i<Num_operators; i++){
1032 if (!stricmp(token, Operators[i].text)){
1040 int find_operator(char *token)
1044 for (i=0; i<Num_operators; i++){
1045 if (!stricmp(token, Operators[i].text)){
1046 return Operators[i].value;
1053 int query_sexp_args_count(int index)
1057 while (Sexp_nodes[index].rest != -1){
1059 index = Sexp_nodes[index].rest;
1065 // returns 0 if ok, negative if there's an error in expression..
1066 // See the returns types in sexp.h
1068 int check_sexp_syntax(int index, int return_type, int recursive, int *bad_index, int mode)
1070 int i = 0, z, t, type, argnum = 0, count, op, type2 = 0, op2;
1073 Assert(index >= 0 && index < MAX_SEXP_NODES);
1074 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1075 if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER && return_type == OPR_BOOL) {
1076 // special case Mark seems to want supported
1077 Assert(Sexp_nodes[index].first == -1); // only lists should have a first pointer
1078 if (Sexp_nodes[index].rest != -1) // anything after the number?
1079 return SEXP_CHECK_NONOP_ARGS; // if so, it's a syntax error
1084 op_index = index; // save the index of the operator since we need to get to other args.
1086 *bad_index = op_index;
1088 if (Sexp_nodes[op_index].subtype != SEXP_ATOM_OPERATOR)
1089 return SEXP_CHECK_OP_EXPTECTED; // not an operator, which it should always be
1091 op = identify_operator(CTEXT(op_index));
1093 return SEXP_CHECK_UNKNOWN_OP; // unrecognized operator
1095 //special case - OPR_AMBIGUOUS matches all
1096 if (return_type != OPR_AMBIGUOUS) {
1097 if (query_operator_return_type(op) != return_type) {
1098 return SEXP_CHECK_TYPE_MISMATCH;
1102 count = query_sexp_args_count(op_index);
1103 if (count < Operators[op].min || count > Operators[op].max)
1104 return SEXP_CHECK_BAD_ARG_COUNT; // incorrect number of arguments
1106 index = Sexp_nodes[op_index].rest;
1107 while (index != -1) {
1108 type = query_operator_argument_type(op, argnum);
1109 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1113 if (Sexp_nodes[index].subtype == SEXP_ATOM_LIST) {
1114 i = Sexp_nodes[index].first;
1118 // be sure to check to see if this node is a list of stuff and not an actual operator type
1119 // thing. (i.e. in the case of a cond statement, the conditional will fall into this if
1120 // statement. MORE TO DO HERE!!!!
1121 if (Sexp_nodes[i].subtype == SEXP_ATOM_LIST)
1124 op2 = identify_operator(CTEXT(i));
1126 return SEXP_CHECK_UNKNOWN_OP;
1128 type2 = query_operator_return_type(op2);
1151 // special case for modify-variable"
1157 return SEXP_CHECK_UNKNOWN_TYPE; // no other return types available
1160 if ((z = check_sexp_syntax(i, t, recursive, bad_index)) != 0) {
1165 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER) {
1168 type2 = OPR_POSITIVE;
1175 if (type == OPF_BOOL) // allow numbers to be used where boolean is required.
1180 return SEXP_CHECK_INVALID_NUM; // not a valid number
1185 i = atoi(CTEXT(index));
1186 z = find_operator(CTEXT(op_index));
1187 if ( (z == OP_HAS_DOCKED_DELAY) || (z == OP_HAS_UNDOCKED_DELAY) )
1188 if ( (argnum == 2) && (i < 1) )
1189 return SEXP_CHECK_NUM_RANGE_INVALID;
1191 z = identify_operator(CTEXT(op_index));
1192 if ( (query_operator_return_type(z) == OPR_AI_GOAL) && (argnum == Operators[op].min - 1) )
1193 if ( (i < 0) || (i > 89) )
1194 return SEXP_CHECK_NUM_RANGE_INVALID;
1196 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_STRING) {
1197 type2 = SEXP_ATOM_STRING;
1205 if ((type2 != OPR_NUMBER) && (type2 != OPR_POSITIVE)){
1206 return SEXP_CHECK_TYPE_MISMATCH;
1212 if (type2 == OPR_NUMBER){
1213 return SEXP_CHECK_NEGATIVE_NUM;
1216 if (type2 != OPR_POSITIVE){
1217 return SEXP_CHECK_TYPE_MISMATCH;
1222 case OPF_SHIP_NOT_PLAYER:
1223 if (type2 != SEXP_ATOM_STRING){
1224 return SEXP_CHECK_TYPE_MISMATCH;
1227 if (ship_name_lookup(CTEXT(index), 0) < 0) {
1228 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))){ // == 0 when still on arrival list
1229 return SEXP_CHECK_INVALID_SHIP;
1236 case OPF_SHIP_POINT:
1237 if (type2 != SEXP_ATOM_STRING){
1238 return SEXP_CHECK_TYPE_MISMATCH;
1241 if (ship_name_lookup(CTEXT(index), 1) < 0) {
1242 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1243 if (type == OPF_SHIP){ // return invalid ship if not also looking for point
1244 return SEXP_CHECK_INVALID_SHIP;
1247 if (waypoint_lookup(CTEXT(index)) < 0){
1248 if (verify_vector(CTEXT(index))){ // verify return non-zero on invalid point
1249 return SEXP_CHECK_INVALID_POINT;
1258 if (type2 != SEXP_ATOM_STRING){
1259 return SEXP_CHECK_TYPE_MISMATCH;
1262 if (wing_name_lookup(CTEXT(index), 1) < 0){
1263 return SEXP_CHECK_INVALID_WING;
1269 case OPF_SHIP_WING_POINT:
1270 if ( type2 != SEXP_ATOM_STRING ){
1271 return SEXP_CHECK_TYPE_MISMATCH;
1274 if ((ship_name_lookup(CTEXT(index), 1) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) {
1275 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1276 if (type != OPF_SHIP_WING_POINT){ // return invalid if not also looking for point
1277 return SEXP_CHECK_INVALID_SHIP_WING;
1280 if (waypoint_lookup(CTEXT(index)) < 0){
1281 if (verify_vector(CTEXT(index))){ // non-zero on verify vector mean invalid!
1282 if (!sexp_determine_team(CTEXT(index))){
1283 return SEXP_CHECK_INVALID_POINT;
1292 case OPF_AWACS_SUBSYSTEM:
1293 case OPF_SUBSYSTEM: {
1295 int shipnum,ship_class, i;
1298 if (type2 != SEXP_ATOM_STRING){
1299 return SEXP_CHECK_TYPE_MISMATCH;
1302 // we must get the model of the ship that is part of this sexpression and find a subsystem
1303 // with that name. This code assumes that the ship *always* preceeds the subsystem in an sexpression.
1304 // if this current fact is ever not the case, the next lines of code must be changes to get the correct
1306 switch(Operators[identify_operator(CTEXT(op_index))].value){
1307 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
1308 ship_index = Sexp_nodes[Sexp_nodes[op_index].rest].rest;
1313 ship_index = Sexp_nodes[op_index].rest;
1315 ship_index = Sexp_nodes[Sexp_nodes[Sexp_nodes[op_index].rest].rest].rest;
1320 ship_index = Sexp_nodes[op_index].rest;
1324 shipname = CTEXT(ship_index);
1325 shipnum = ship_name_lookup(shipname);
1327 ship_class = Ships[shipnum].ship_info_index;
1329 // must try to find the ship in the ship_arrival_list
1330 p_object *parse_obj;
1332 parse_obj = mission_parse_get_arrival_ship( shipname );
1333 if ( parse_obj == NULL ){
1334 return SEXP_CHECK_INVALID_SHIP;
1337 ship_class = parse_obj->ship_class;
1340 // check for the special "hull" value
1341 if ( (Operators[op].value == OP_SABOTAGE_SUBSYSTEM) || (Operators[op].value == OP_REPAIR_SUBSYSTEM) || (Operators[op].value == OP_SET_SUBSYSTEM_STRNGTH) ) {
1342 if ( !stricmp( CTEXT(index), SEXP_HULL_STRING) ){
1347 for (i=0; i<Ship_info[ship_class].n_subsystems; i++){
1348 if (!stricmp(Ship_info[ship_class].subsystems[i].subobj_name, CTEXT(index))){
1353 if (i == Ship_info[ship_class].n_subsystems){
1354 return SEXP_CHECK_INVALID_SUBSYS;
1357 // if we're checking for an AWACS subsystem and this is not an awacs subsystem
1359 if((type == OPF_AWACS_SUBSYSTEM) && !(Ship_info[ship_class].subsystems[i].flags & MSS_FLAG_AWACS)){
1360 return SEXP_CHECK_INVALID_SUBSYS;
1368 if (waypoint_lookup(CTEXT(index)) < 0){
1369 if (verify_vector(CTEXT(index))){
1370 return SEXP_CHECK_INVALID_POINT;
1374 if (type2 != SEXP_ATOM_STRING){
1375 return SEXP_CHECK_TYPE_MISMATCH;
1381 if (type2 == SEXP_ATOM_STRING){
1382 for (i=0; i<Num_team_names; i++){
1383 if (!stricmp(Team_names[i], CTEXT(index))){
1389 if (i == Num_team_names){
1390 return SEXP_CHECK_INVALID_IFF;
1393 if (type2 != SEXP_ATOM_STRING){
1394 return SEXP_CHECK_TYPE_MISMATCH;
1400 if (type2 != OPR_BOOL){
1401 return SEXP_CHECK_TYPE_MISMATCH;
1407 if (type2 != OPR_NULL){
1408 return SEXP_CHECK_TYPE_MISMATCH;
1414 if ( type2 != SEXP_ATOM_STRING ){
1415 return SEXP_CHECK_TYPE_MISMATCH;
1421 if (type2 != OPR_AI_GOAL){
1422 return SEXP_CHECK_TYPE_MISMATCH;
1426 int ship_num, ship2, i, w = 0, z;
1428 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1430 w = wing_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1433 *bad_index = Sexp_nodes[op_index].rest;
1436 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1440 Assert(Sexp_nodes[index].subtype == SEXP_ATOM_LIST);
1441 z = Sexp_nodes[index].first;
1442 Assert(Sexp_nodes[z].subtype != SEXP_ATOM_LIST);
1443 z = find_operator(CTEXT(z));
1444 if (ship_num >= 0) {
1445 if (!query_sexp_ai_goal_valid(z, ship_num)){
1446 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1450 for (i=0; i<Wings[w].wave_count; i++){
1451 if (!query_sexp_ai_goal_valid(z, Wings[w].ship_index[i])){
1452 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1457 if ((z == OP_AI_DOCK) && (Sexp_nodes[index].rest >= 0)) {
1458 ship2 = ship_name_lookup(CTEXT(Sexp_nodes[index].rest));
1459 if ((ship_num < 0) || !ship_docking_valid(ship_num, ship2)){
1460 return SEXP_CHECK_DOCKING_NOT_ALLOWED;
1465 // we should check the syntax of the actual goal!!!!
1466 z = Sexp_nodes[index].first;
1467 if ((z = check_sexp_syntax(z, OPR_AI_GOAL, recursive, bad_index)) != 0){
1474 if (type2 != SEXP_ATOM_STRING){
1475 return SEXP_CHECK_TYPE_MISMATCH;
1478 for (i=0; i<MAX_SHIP_TYPE_COUNTS; i++){
1479 if (!stricmp( Ship_type_names[i], CTEXT(index))){
1484 if (i == MAX_SHIP_TYPE_COUNTS){
1485 return SEXP_CHECK_INVALID_SHIP_TYPE;
1490 case OPF_WAYPOINT_PATH:
1491 for (i=0; i<Num_waypoint_lists; i++){
1492 if (!stricmp(Waypoint_lists[i].name, CTEXT(index))){
1497 if (i == Num_waypoint_lists){
1498 return SEXP_CHECK_TYPE_MISMATCH;
1503 // code commented out because of order reversing of arguments to messaging code. Maybe
1504 // I'll comment it back in someday when older missions get fixed.
1505 if (type2 != SEXP_ATOM_STRING)
1506 return SEXP_CHECK_TYPE_MISMATCH;
1509 for (i=0; i<Num_messages; i++)
1510 if (!stricmp(Messages[i].name, CTEXT(index)))
1513 if (i == Num_messages)
1514 return SEXP_CHECK_UNKNOWN_MESSAGE;
1519 case OPF_PRIORITY: {
1520 // following code must be removed since I changed the order of the operators in the send-message
1521 // function to take priority second. Maybe someday, I'll require that all designers fix the
1523 if (type2 != SEXP_ATOM_STRING)
1524 return SEXP_CHECK_TYPE_MISMATCH;
1526 if (Fred_running) { // should still check in Fred though..
1529 name = CTEXT(index);
1530 if (!stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high"))
1533 return SEXP_CHECK_INVALID_PRIORITY;
1539 case OPF_MISSION_NAME:
1540 if (type2 != SEXP_ATOM_STRING)
1541 return SEXP_CHECK_TYPE_MISMATCH;
1544 if (mode == SEXP_MODE_CAMPAIGN) {
1545 for (i=0; i<Campaign.num_missions; i++)
1546 if (!stricmp(CTEXT(index), Campaign.missions[i].name)) {
1547 if ((i != Sexp_useful_number) && (Campaign.missions[i].level >= Campaign.missions[Sexp_useful_number].level))
1548 return SEXP_CHECK_INVALID_LEVEL;
1553 if (i == Campaign.num_missions)
1554 return SEXP_CHECK_INVALID_MISSION_NAME;
1557 // mwa -- put the following if statement to prevent Fred errors for possibly valid
1558 // conditions. We should do something else here!!!
1559 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE)
1560 || (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1563 if (!(*Mission_filename) || stricmp(Mission_filename, CTEXT(index)))
1564 return SEXP_CHECK_INVALID_MISSION_NAME;
1571 if (type2 != SEXP_ATOM_STRING)
1572 return SEXP_CHECK_TYPE_MISMATCH;
1574 // we only need to check the campaign list if running in Fred and are in campaign mode.
1575 // otherwise, check the set of current goals
1576 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1577 z = find_parent_operator(index);
1579 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1581 for (i=0; i<Campaign.num_missions; i++)
1582 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1585 // read the goal/event list from the mission file if both num_goals and num_events
1587 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1588 read_mission_goal_list(i);
1590 if (i < Campaign.num_missions) {
1591 for (t=0; t<Campaign.missions[i].num_goals; t++)
1592 if (!stricmp(CTEXT(index), Campaign.missions[i].goals[t].name))
1595 if (t == Campaign.missions[i].num_goals)
1596 return SEXP_CHECK_INVALID_GOAL_NAME;
1599 // MWA -- short circuit evaluation of these things for now.
1600 if ( (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1603 for (i=0; i<Num_goals; i++)
1604 if (!stricmp(CTEXT(index), Mission_goals[i].name))
1608 return SEXP_CHECK_INVALID_GOAL_NAME;
1613 case OPF_EVENT_NAME:
1614 if ( type2 != SEXP_ATOM_STRING )
1615 return SEXP_CHECK_TYPE_MISMATCH;
1617 // like above checking for goals, check events in the campaign only if in Fred
1618 // and only if in campaign mode. Otherwise, check the current set of events
1619 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1620 z = find_parent_operator(index);
1622 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1624 for (i=0; i<Campaign.num_missions; i++)
1625 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1628 // read the goal/event list from the mission file if both num_goals and num_events
1630 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1631 read_mission_goal_list(i);
1633 if (i < Campaign.num_missions) {
1634 for (t=0; t<Campaign.missions[i].num_events; t++)
1635 if (!stricmp(CTEXT(index), Campaign.missions[i].events[t].name))
1638 if (t == Campaign.missions[i].num_events)
1639 return SEXP_CHECK_INVALID_EVENT_NAME;
1642 // MWA -- short circuit evaluation of these things for now.
1643 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE) )
1646 for ( i = 0; i < Num_mission_events; i++ ) {
1647 if ( !stricmp(CTEXT(index), Mission_events[i].name) )
1650 if ( i == Num_mission_events )
1651 return SEXP_CHECK_INVALID_EVENT_NAME;
1655 case OPF_DOCKER_POINT:
1656 if (type2 != SEXP_ATOM_STRING)
1657 return SEXP_CHECK_TYPE_MISMATCH;
1660 int ship_num, model, i, z;
1662 z = find_parent_operator(op_index);
1663 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[z].rest));
1666 *bad_index = Sexp_nodes[z].rest;
1668 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1671 model = Ships[ship_num].modelnum;
1672 z = model_get_num_dock_points(model);
1674 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1678 return SEXP_CHECK_INVALID_DOCKER_POINT;
1683 case OPF_DOCKEE_POINT:
1684 if (type2 != SEXP_ATOM_STRING)
1685 return SEXP_CHECK_TYPE_MISMATCH;
1688 int ship_num, model, i, z;
1690 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1693 *bad_index = Sexp_nodes[op_index].rest;
1695 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1698 model = Ships[ship_num].modelnum;
1699 z = model_get_num_dock_points(model);
1701 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1705 return SEXP_CHECK_INVALID_DOCKEE_POINT;
1711 if (type2 != SEXP_ATOM_STRING)
1712 return SEXP_CHECK_TYPE_MISMATCH;
1714 if (*CTEXT(index) != '#') { // not a manual source?
1715 //if ( !stricmp(CTEXT(index), "<any allied>") )
1716 // return SEXP_CHECK_INVALID_MSG_SOURCE;
1718 if ( stricmp(CTEXT(index), "<any wingman>")) // not a special token?
1719 if ((ship_name_lookup(CTEXT(index)) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) // is it in the mission?
1720 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) // == 0 when still on arrival list
1721 return SEXP_CHECK_INVALID_MSG_SOURCE;
1727 if (type2 != SEXP_ATOM_STRING)
1728 return SEXP_CHECK_TYPE_MISMATCH;
1732 case OPF_SKILL_LEVEL:
1733 if ( type2 != SEXP_ATOM_STRING )
1734 return SEXP_CHECK_TYPE_MISMATCH;
1736 for (i = 0; i < NUM_SKILL_LEVELS; i++) {
1737 if ( !stricmp(CTEXT(index), Skill_level_names(i, 0)) )
1740 if ( i == NUM_SKILL_LEVELS )
1741 return SEXP_CHECK_INVALID_SKILL_LEVEL;
1744 case OPF_MEDAL_NAME:
1745 if ( type2 != SEXP_ATOM_STRING)
1746 return SEXP_CHECK_TYPE_MISMATCH;
1748 for (i = 0; i < NUM_MEDALS; i++) {
1749 if ( !stricmp(CTEXT(index), Medals[i].name) )
1753 if ( i == NUM_MEDALS )
1754 return SEXP_CHECK_INVALID_MEDAL_NAME;
1757 case OPF_HUGE_WEAPON:
1758 case OPF_WEAPON_NAME:
1759 if ( type2 != SEXP_ATOM_STRING )
1760 return SEXP_CHECK_TYPE_MISMATCH;
1762 for (i = 0; i < Num_weapon_types; i++ ) {
1763 if ( !stricmp(CTEXT(index), Weapon_info[i].name) )
1767 if ( i == Num_weapon_types )
1768 return SEXP_CHECK_INVALID_WEAPON_NAME;
1770 // we need to be sure that for huge weapons, the WIF_HUGE flag is set
1771 if ( type == OPF_HUGE_WEAPON ) {
1772 if ( !(Weapon_info[i].wi_flags & WIF_HUGE) )
1773 return SEXP_CHECK_INVALID_WEAPON_NAME;
1778 case OPF_SHIP_CLASS_NAME:
1779 if ( type2 != SEXP_ATOM_STRING )
1780 return SEXP_CHECK_TYPE_MISMATCH;
1782 for (i = 0; i < Num_ship_types; i++ ) {
1783 if ( !stricmp(CTEXT(index), Ship_info[i].name) )
1787 if ( i == Num_ship_types )
1788 return SEXP_CHECK_INVALID_SHIP_CLASS_NAME;
1791 case OPF_HUD_GAUGE_NAME:
1792 if ( type2 != SEXP_ATOM_STRING )
1793 return SEXP_CHECK_TYPE_MISMATCH;
1795 for ( i = 0; i < NUM_HUD_GAUGES; i++ ) {
1796 if ( !stricmp(CTEXT(index), HUD_gauge_text[i]) )
1800 // if we reached the end of the list, then the name is invalid
1801 if ( i == NUM_HUD_GAUGES )
1802 return SEXP_CHECK_INVALID_GAUGE_NAME;
1806 case OPF_JUMP_NODE_NAME:
1807 if ( type2 != SEXP_ATOM_STRING )
1808 return SEXP_CHECK_TYPE_MISMATCH;
1810 for ( i = 0; i < Num_jump_nodes; i++ ) {
1811 if ( !stricmp(Jump_nodes[i].name, CTEXT(index)) )
1815 if ( i == Num_jump_nodes )
1816 return SEXP_CHECK_INVALID_JUMP_NODE;
1821 case OPF_VARIABLE_NAME:
1822 if ( Fred_running ) {
1823 if ( get_index_sexp_variable_name(Sexp_nodes[index].text) == -1) {
1824 return SEXP_CHECK_INVALID_VARIABLE;
1827 // if Fred not running anything goes
1831 // type checking for modify-variable
1832 // string or number -- anything goes
1836 Int3(); // currently unhandled argument format (so add it now)
1839 index = Sexp_nodes[index].rest;
1846 // Stuff a string (" chars ") in *str, return length.
1848 int get_string(char *str)
1852 len = strcspn(Mp + 1, "\"");
1853 strncpy(str, Mp + 1, len);
1861 // get text to stuff into Sexp_node in case of variable
1862 // if Fred_running - stuff Sexp_variables[].variable_name
1863 // otherwise - stuff index into Sexp_variables array.
1864 void get_sexp_text_for_variable(char *text, char *token)
1869 // get variable name (up to '['
1870 end_index = strcspn(token, "[");
1871 Assert( (end_index != 0) && (end_index < TOKEN_LENGTH-1) );
1872 strncpy(text, token, end_index);
1873 text[end_index] = '\0';
1875 if ( !Fred_running ) {
1876 // freespace - get index into Sexp_variables array
1877 sexp_var_index = get_index_sexp_variable_name(text);
1878 Assert(sexp_var_index != -1);
1879 sprintf(text, "%d", sexp_var_index);
1884 // returns the first sexp index of data this function allocates. (start of this sexp)
1885 // recursive function - always sets first and then rest
1886 int get_sexp(char *token)
1888 int start, node, last, len, op, count;
1889 char variable_text[TOKEN_LENGTH];
1891 // start - the node allocated in first instance of fuction
1892 // node - the node allocated in current instance of function
1893 // count - number of nodes allocated this instance of function [do we set last.rest or first]
1894 // variable - whether string or number is a variable referencing Sexp_variables
1900 ignore_white_space();
1901 while (*Mp != ')') {
1902 Assert(*Mp != EOF_CHAR);
1906 node = alloc_sexp("", SEXP_LIST, SEXP_ATOM_LIST, get_sexp(token), -1);
1908 } else if (*Mp == '\"') {
1910 len = strcspn(Mp + 1, "\"");
1912 Assert(Mp[len + 1] == '\"'); // hit EOF first (unterminated string)
1913 Assert(len < TOKEN_LENGTH); // token is too long.
1915 // check if string variable
1916 if ( *(Mp + 1) == SEXP_VARIABLE_CHAR ) {
1918 // reduce length by 1 for end \"
1919 int length = len - 1;
1920 Assert(length < 2*TOKEN_LENGTH+2);
1922 // start copying after skipping 1st char
1923 strncpy(token, Mp + 2, length);
1926 get_sexp_text_for_variable(variable_text, token);
1927 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_STRING, -1, -1);
1929 strncpy(token, Mp + 1, len);
1931 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_STRING, -1, -1);
1934 // bump past closing \" by 1 char
1938 // Sexp operator or number
1940 bool variable = false;
1941 while (*Mp != ')' && !is_white_space(*Mp)) {
1942 if ( (len == 0) && (*Mp == SEXP_VARIABLE_CHAR) ) {
1947 Assert(*Mp != EOF_CHAR);
1948 Assert(len < TOKEN_LENGTH - 1);
1949 token[len++] = *Mp++;
1954 op = identify_operator(token);
1956 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_OPERATOR, -1, -1);
1959 // convert token text for variable
1960 get_sexp_text_for_variable(variable_text, token);
1962 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_NUMBER, -1, -1);
1964 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_NUMBER, -1, -1);
1972 Sexp_nodes[last].rest = node;
1977 Assert(node != -1); // ran out of nodes. Time to raise the MAX!
1979 ignore_white_space();
1982 Mp++; // skip past the ')'
1987 // Stuffs a list of sexp variables
1988 int stuff_sexp_variable_list()
1991 char var_name[TOKEN_LENGTH];
1992 char default_value[TOKEN_LENGTH];
1993 char str_type[TOKEN_LENGTH];
1998 required_string("$Variables:");
1999 ignore_white_space();
2001 // check for start of list
2003 error_display(1, "Reading sexp variable list. Found [%c]. Expecting '('.\n", *Mp);
2004 longjmp(parse_abort, 6);
2008 ignore_white_space();
2010 while (*Mp != ')') {
2011 Assert(count < MAX_SEXP_VARIABLES);
2013 // get index - for debug
2015 ignore_gray_space();
2018 get_string(var_name);
2019 ignore_gray_space();
2021 // get default_value;
2022 get_string(default_value);
2023 ignore_gray_space();
2026 get_string(str_type);
2027 ignore_white_space();
2030 if (!stricmp(str_type, "number")) {
2031 type = SEXP_VARIABLE_NUMBER;
2032 } else if (!stricmp(str_type, "string")) {
2033 type = SEXP_VARIABLE_STRING;
2034 } else if (!stricmp(str_type, "block")) {
2035 type = SEXP_VARIABLE_BLOCK | SEXP_VARIABLE_BLOCK_EXP;
2037 type = SEXP_VARIABLE_UNKNOWN;
2043 // check if variable name already exists
2044 if ( (type == SEXP_VARIABLE_NUMBER) || (type == SEXP_VARIABLE_STRING) ) {
2045 Assert(get_index_sexp_variable_name(var_name) == -1);
2048 sexp_add_variable(default_value, var_name, type, index);
2057 void build_sexp_text_string(char *buffer, int node, int mode)
2059 if (Sexp_nodes[node].type & SEXP_FLAG_VARIABLE) {
2061 int sexp_variables_index = get_index_sexp_variable_name(Sexp_nodes[node].text);
2062 Assert(sexp_variables_index != -1);
2063 Assert( (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER) || (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING) );
2066 if (Sexp_nodes[node].subtype == SEXP_ATOM_NUMBER) {
2067 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER);
2069 // Error check - can be Fred or Freespace
2070 if (mode == SEXP_ERROR_CHECK_MODE) {
2071 if ( Fred_running ) {
2072 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2074 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2077 // Save as string - only Fred
2078 Assert(mode == SEXP_SAVE_MODE);
2079 sprintf(buffer, "@%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2083 Assert(Sexp_nodes[node].subtype == SEXP_ATOM_STRING);
2084 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING);
2086 // Error check - can be Fred or Freespace
2087 if (mode == SEXP_ERROR_CHECK_MODE) {
2088 if ( Fred_running ) {
2089 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2091 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2094 // Save as string - only Fred
2095 Assert(mode == SEXP_SAVE_MODE);
2096 sprintf(buffer, "\"@%s[%s]\" ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2101 if (Sexp_nodes[node].subtype == SEXP_ATOM_STRING) {
2102 sprintf(buffer, "\"%s\" ", CTEXT(node));
2104 sprintf(buffer, "%s ", CTEXT(node));
2111 int build_sexp_string(int cur_node, int level, int mode)
2114 int len, offset, node;
2116 Sexp_build_flag = 0;
2117 offset = strlen(Sexp_string);
2118 strcat(Sexp_string, "( ");
2120 while (node != -1) {
2121 Assert(node >= 0 && node < MAX_SEXP_NODES);
2122 if (Sexp_nodes[node].first == -1) {
2123 // build text to string
2124 build_sexp_text_string(pstr, node, mode);
2125 strcat(Sexp_string, pstr);
2128 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2131 node = Sexp_nodes[node].rest;
2134 strcat(Sexp_string, ") ");
2135 len = strlen(Sexp_string) - offset;
2137 Sexp_string[offset] = 0;
2138 build_extended_sexp_string(cur_node, level, mode);
2145 void build_extended_sexp_string(int cur_node, int level, int mode)
2148 int i, flag = 0, node;
2150 strcat(Sexp_string, "( ");
2152 while (node != -1) {
2153 if (flag) // not the first line?
2154 for (i=0; i<level + 1; i++)
2155 strcat(Sexp_string, " ");
2158 Assert(node >= 0 && node < MAX_SEXP_NODES);
2159 if (Sexp_nodes[node].first == -1) {
2160 build_sexp_text_string(pstr,node, mode);
2161 strcat(Sexp_string, pstr);
2164 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2167 strcat(Sexp_string, "\n");
2168 node = Sexp_nodes[node].rest;
2171 for (i=0; i<level; i++)
2172 strcat(Sexp_string, " ");
2174 strcat(Sexp_string, ")");
2177 void convert_sexp_to_string(int cur_node, char *outstr, int mode)
2179 Sexp_string = outstr;
2182 build_sexp_string(cur_node, 0, mode);
2184 strcpy(Sexp_string, "( )");
2187 // determine if the named ship or wing hasn't arrived yet (wing or ship must be on arrival list)
2188 int sexp_query_has_yet_to_arrive(char *name)
2192 if (ship_query_state(name) < 0)
2195 i = wing_name_lookup(name, 1);
2197 // has not arrived yet, and never will arrive
2198 if ((i >= 0) && (Wings[i].num_waves >= 0) && (Wings[i].flags & WF_NEVER_EXISTED)){
2202 // has not arrived yet
2203 if ((i >= 0) && (Wings[i].num_waves >= 0) && !Wings[i].total_arrived_count){
2210 // arithmetic functions
2211 int add_sexps(int n)
2217 sum = eval_sexp( CAR(n) );
2219 sum = atoi( CTEXT(n) );
2221 // be sure to check for the NAN value when doing arithmetic -- this value should
2222 // get propagated to the next highest function.
2223 if ( Sexp_nodes[CAR(n)].value == SEXP_NAN )
2225 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2226 return SEXP_NAN_FOREVER;
2228 while (CDR(n) != -1) {
2229 val = eval_sexp( CDR(n) );
2230 // be sure to check for the NAN value when doing arithmetic -- this value should
2231 // get propagated to the next highest function.
2232 if ( Sexp_nodes[CDR(n)].value == SEXP_NAN )
2234 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2235 return SEXP_NAN_FOREVER;
2244 int sub_sexps(int n)
2249 if (Sexp_nodes[n].first != -1)
2250 sum = eval_sexp(CAR(n));
2252 sum = atoi(CTEXT(n));
2254 while (CDR(n) != -1) {
2255 sum -= eval_sexp(CDR(n));
2263 int mul_sexps(int n)
2268 if (Sexp_nodes[n].first != -1)
2269 sum = eval_sexp(Sexp_nodes[n].first);
2271 sum = atoi(CTEXT(n));
2273 while (Sexp_nodes[n].rest != -1) {
2274 sum *= eval_sexp(Sexp_nodes[n].rest);
2275 n = Sexp_nodes[n].rest;
2282 int div_sexps(int n)
2287 if (Sexp_nodes[n].first != -1)
2288 sum = eval_sexp(Sexp_nodes[n].first);
2290 sum = atoi(CTEXT(n));
2292 while (Sexp_nodes[n].rest != -1) {
2293 sum /= eval_sexp(Sexp_nodes[n].rest);
2294 n = Sexp_nodes[n].rest;
2301 int mod_sexps(int n)
2306 if (Sexp_nodes[n].first != -1)
2307 sum = eval_sexp(Sexp_nodes[n].first);
2309 sum = atoi(CTEXT(n));
2311 while (Sexp_nodes[n].rest != -1) {
2312 sum = sum % eval_sexp(Sexp_nodes[n].rest);
2313 n = Sexp_nodes[n].rest;
2320 int rand_internal(int low, int high)
2324 // get diff - don't allow negative or zero
2330 return (low + rand() % (diff + 1));
2334 int rand_sexp(int n, int multiple=0)
2341 if (Sexp_nodes[n].value == SEXP_NUM_EVAL) {
2342 // don't regenerate new random number
2343 rand_num = atoi(CTEXT(n));
2346 // if (Sexp_nodes[n].first != -1) {
2347 // low = eval_sexp(Sexp_nodes[n].first);
2349 // low = atoi(CTEXT(n));
2354 high = num_eval(CDR(n));
2356 // get the random number
2357 rand_num = rand_internal(low, high);
2360 // set .value and .text so random number is generated only once.
2361 Sexp_nodes[n].value = SEXP_NUM_EVAL;
2362 sprintf(Sexp_nodes[n].text, "%d", rand_num);
2370 // boolean evaluation functions. Evaluate all sexpressions in the 'or' operator. Needed to mark
2371 // entries in the mission log as essential so when pruning the log, we know which entries we might
2375 int all_false, result;
2380 if (Sexp_nodes[n].first != -1) {
2381 result |= eval_sexp(Sexp_nodes[n].first);
2382 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_TRUE )
2383 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2384 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2387 result |= atoi(CTEXT(n));
2389 // don't return on true value -- keep evaluating for mission log purposes
2393 while (Sexp_nodes[n].rest != -1) {
2394 result |= eval_sexp(Sexp_nodes[n].rest);
2395 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_TRUE )
2396 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2397 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2399 n = Sexp_nodes[n].rest;
2400 // don't return on true value -- keep evaluating for mission log purposes
2407 return SEXP_KNOWN_FALSE;
2412 // this function does the 'and' operator. It will short circuit evaluation *but* it will still
2413 // evaluate other members of the and construct. I do this because I need events in the mission log
2414 // to get marked as essential for goal purposes, and evaluation is pretty much the only way
2417 int all_true, result;
2422 if (Sexp_nodes[n].first != -1) {
2423 result &= eval_sexp( CAR(n) );
2424 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2425 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2426 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be true
2429 result &= atoi(CTEXT(n));
2431 // don't short circuit -- evaluate everything for purposes of marking mission log
2435 while (Sexp_nodes[n].rest != -1) {
2438 new_result = eval_sexp( CDR(n) );
2439 result &= new_result;
2440 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2441 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2442 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2444 // don't short circuit -- evaluate everything for purposes of marking mission log
2448 n = Sexp_nodes[n].rest;
2453 return SEXP_KNOWN_TRUE;
2458 // this version of the 'and' operator determines whether or not it's arguments become true
2459 // in the order in which they are specified in the when statement. Should be a simple matter of
2460 // seeing if anything evaluates to true later than something that evalueated to false
2461 int sexp_and_in_sequence(int n)
2466 all_true = 1; // represents whether or not all nodes we have seen so far are true
2468 if (Sexp_nodes[n].first != -1) {
2469 result &= eval_sexp( CAR(n) );
2470 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2471 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2472 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if value is true, mark our all_true variable for later checking
2475 result &= atoi(CTEXT(n));
2477 // a little test -- if the previous sexpressions was true, then mark the node itself as always
2478 // true. I did this because of the distance function. It might become true, then when waiting for
2479 // the second evalation, it might become false, rendering this function false. So, when one becomes
2480 // true -- mark it true forever.
2482 Sexp_nodes[Sexp_nodes[n].first].value = SEXP_KNOWN_TRUE;
2484 while (Sexp_nodes[n].rest != -1) {
2487 next_result = eval_sexp( CDR(n) );
2488 if ( next_result && !result ) // if current result is true, and our running result is false, thngs didn't become true in order
2489 return SEXP_KNOWN_FALSE;
2490 result &= next_result;
2491 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2492 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2493 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2495 // see comment above for explanation of next lines
2497 Sexp_nodes[Sexp_nodes[n].rest].value = SEXP_KNOWN_TRUE;
2498 n = Sexp_nodes[n].rest;
2503 return SEXP_KNOWN_TRUE;
2508 // for these four basic boolean operations (not, <, >, and =), we have special cases that we must deal
2509 // with. We have sexpressions operators that might return a NAN type return value (such as the distance
2510 // between two ships when one of the ships is destroyed or departed). These operations need to check for
2511 // this special NAN value and adjust their return types accordingly. NAN values represent false return values
2512 int sexp_not( int n )
2517 if (Sexp_nodes[n].first != -1) {
2518 result = eval_sexp( CAR(n) );
2519 if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_FALSE )
2520 return SEXP_KNOWN_TRUE; // not KNOWN_FALSE == KNOWN_TRUE;
2521 else if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_TRUE ) // not KNOWN_TRUE == KNOWN_FALSE
2522 return SEXP_KNOWN_FALSE;
2523 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) // not NAN == TRUE (I think)
2525 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2528 result = atoi(CTEXT(n));
2538 exp1 = eval_sexp( n );
2539 exp2 = eval_sexp( CDR(n) );
2541 // check for the NAN value
2542 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2544 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2545 return SEXP_KNOWN_FALSE;
2556 exp1 = eval_sexp( n );
2557 exp2 = eval_sexp( CDR(n) );
2559 // check for the NAN value
2560 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2562 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2563 return SEXP_KNOWN_FALSE;
2570 int sexp_equal(int n)
2574 exp1 = eval_sexp( n );
2575 exp2 = eval_sexp( CDR(n) );
2577 // check for the NAN value
2578 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2580 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2581 return SEXP_KNOWN_FALSE;
2588 // Evaluate if given ship is destroyed.
2589 // Return true if the ship in the expression has been destroyed.
2590 int sexp_is_destroyed(int n, fix *latest_time)
2593 int count, num_destroyed, wing_index;
2605 if (sexp_query_has_yet_to_arrive(name))
2606 return SEXP_CANT_EVAL;
2608 // check to see if this ship/wing has departed. If so, then function is known false
2609 if ( mission_log_get_time (LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time (LOG_WING_DEPART, name, NULL, NULL) )
2610 return SEXP_KNOWN_FALSE;
2612 // check the mission log. If ship/wing not destroyed, immediately return 0.
2613 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)) {
2615 if ( latest_time && (time > *latest_time) )
2616 *latest_time = time;
2618 // ship or wing isn't destroyed -- add to directive count
2619 if ( (wing_index = wing_name_lookup( name, 1 )) >= 0 ) {
2620 Directive_count += Wings[wing_index].current_count;
2625 // move to next ship/wing in list
2629 // special case to mark a directive for destroy wing objectives true after a short amount
2630 // of time when there are more waves for this wing.
2631 if ( (count == 1) && (wing_index >= 0) && (Directive_count == 0) ) {
2632 if ( Wings[wing_index].current_wave < Wings[wing_index].num_waves )
2633 Directive_count = DIRECTIVE_WING_ZERO;
2636 if ( count == num_destroyed )
2637 return SEXP_KNOWN_TRUE;
2643 // return true if the subsystem of the given ship has been destroyed or not
2644 int sexp_is_subsystem_destroyed(int n)
2646 char *ship_name, *subsys_name;
2650 ship_name = CTEXT(n);
2651 subsys_name = CTEXT(CDR(n));
2653 if (sexp_query_has_yet_to_arrive(ship_name))
2654 return SEXP_CANT_EVAL;
2656 // if the ship has departed, no way to destroy it's subsystem.
2657 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2658 return SEXP_KNOWN_FALSE;
2660 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, NULL) )
2661 return SEXP_KNOWN_TRUE;
2667 // determines if a ship has docked
2668 int sexp_has_docked(int n)
2670 char *docker = CTEXT(n);
2671 char *dockee = CTEXT(CDR(n));
2672 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
2674 if (sexp_query_has_yet_to_arrive(docker))
2675 return SEXP_CANT_EVAL;
2677 if (sexp_query_has_yet_to_arrive(dockee))
2678 return SEXP_CANT_EVAL;
2680 Assert ( count > 0 );
2681 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2682 return SEXP_KNOWN_FALSE;
2684 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, NULL) )
2687 return SEXP_KNOWN_TRUE;
2690 // determines if a ship has undocked
2691 int sexp_has_undocked(int n)
2693 char *docker = CTEXT(n);
2694 char *dockee = CTEXT(CDR(n));
2695 int count = atoi(CTEXT(CDR(CDR(n))));
2697 if (sexp_query_has_yet_to_arrive(docker))
2698 return SEXP_CANT_EVAL;
2700 if (sexp_query_has_yet_to_arrive(dockee))
2701 return SEXP_CANT_EVAL;
2703 Assert ( count > 0 );
2704 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, NULL) ) {
2705 // if either ship destroyed before they dock, then sexp is known false
2706 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2707 return SEXP_KNOWN_FALSE;
2712 return SEXP_KNOWN_TRUE;
2715 // determines if a ship has arrived onto the scene
2716 int sexp_has_arrived(int n, fix *latest_time)
2719 int count, num_arrived;
2727 // if there is no log entry for this ship/wing for arrival, sexpression is false
2728 if ( mission_log_get_time(LOG_SHIP_ARRIVE, name, NULL, &time) || mission_log_get_time(LOG_WING_ARRIVE, name, NULL, &time) ) {
2730 if ( latest_time && (time > *latest_time) )
2731 *latest_time = time;
2736 if ( count == num_arrived )
2737 return SEXP_KNOWN_TRUE;
2742 // determines if a ship/wing has departed
2743 int sexp_has_departed(int n, fix *latest_time)
2746 int count, num_departed;
2755 if (sexp_query_has_yet_to_arrive(name))
2756 return SEXP_CANT_EVAL;
2758 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
2759 // the sexpression is not true.
2760 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) || mission_log_get_time(LOG_WING_DESTROYED, name, NULL, NULL))
2761 return SEXP_KNOWN_FALSE;
2762 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_WING_DEPART, name, NULL, &time) ) {
2764 if ( latest_time && (time > *latest_time) )
2765 *latest_time = time;
2770 if ( count == num_departed )
2771 return SEXP_KNOWN_TRUE;
2776 // determines if ships are disabled
2777 int sexp_is_disabled( int n, fix *latest_time )
2780 int count, num_disabled;
2789 if (sexp_query_has_yet_to_arrive(name))
2790 return SEXP_CANT_EVAL;
2792 // if ship/wing destroyed, sexpression is known false. Also, if there is no disable log entry, then
2793 // the sexpression is not true.
2794 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2795 return SEXP_KNOWN_FALSE;
2796 else if ( mission_log_get_time(LOG_SHIP_DISABLED, name, NULL, &time) ) {
2798 if ( latest_time && (time > *latest_time) )
2799 *latest_time = time;
2804 if ( count == num_disabled )
2805 return SEXP_KNOWN_TRUE;
2810 // function to determine if a ship is done flying waypoints
2811 int sexp_are_waypoints_done( int n )
2813 char *ship_name, *waypoint_name;
2815 ship_name = CTEXT(n);
2816 waypoint_name = CTEXT(CDR(n));
2818 if (sexp_query_has_yet_to_arrive(ship_name))
2819 return SEXP_CANT_EVAL;
2821 // a destroyed or departed ship will never reach their goal -- return known false
2822 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
2823 return SEXP_KNOWN_FALSE;
2824 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
2825 return SEXP_KNOWN_FALSE;
2827 // now check the log for the waypoints done entry
2828 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, NULL) )
2829 return SEXP_KNOWN_TRUE;
2835 // determines if ships are disarmed
2836 int sexp_is_disarmed( int n, fix *latest_time )
2839 int count, num_disarmed;
2848 if (sexp_query_has_yet_to_arrive(name))
2849 return SEXP_CANT_EVAL;
2851 // if ship/wing destroyed, sexpression is known false. Also, if there is no disarm log entry, then
2852 // the sexpression is not true.
2853 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2854 return SEXP_KNOWN_FALSE;
2855 else if ( mission_log_get_time(LOG_SHIP_DISARMED, name, NULL, &time) ) {
2857 if ( latest_time && (time > *latest_time) )
2858 *latest_time = time;
2863 if ( count == num_disarmed )
2864 return SEXP_KNOWN_TRUE;
2869 // the following functions are similar to the above objective functions but return true/false
2870 // if N seconds have elasped after the corresponding function is true.
2871 int sexp_is_destroyed_delay(int n)
2880 delay = i2f(num_eval(n));
2882 // check value of is_destroyed function. KNOWN_FALSE should be returned immediately
2883 val = sexp_is_destroyed( CDR(n), &time );
2884 if ( val == SEXP_KNOWN_FALSE )
2887 if ( val == SEXP_CANT_EVAL )
2888 return SEXP_CANT_EVAL;
2892 if ( (Missiontime - time) >= delay )
2893 return SEXP_KNOWN_TRUE;
2899 int sexp_is_subsystem_destroyed_delay( int n )
2901 char *ship_name, *subsys_name;
2906 ship_name = CTEXT(n);
2907 subsys_name = CTEXT(CDR(n));
2908 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
2910 if (sexp_query_has_yet_to_arrive(ship_name))
2911 return SEXP_CANT_EVAL;
2913 // if the ship has departed, no way to destroy it's subsystem.
2914 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2915 return SEXP_KNOWN_FALSE;
2917 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, &time) ) {
2918 if ( (Missiontime - time) >= delay )
2919 return SEXP_KNOWN_TRUE;
2925 int sexp_is_disabled_delay( int n )
2933 delay = i2f(atoi(CTEXT(n)));
2935 // check value of is_disable for known false and return immediately if it is.
2936 val = sexp_is_disabled( CDR(n), &time );
2937 if ( val == SEXP_KNOWN_FALSE )
2940 if ( val == SEXP_CANT_EVAL )
2941 return SEXP_CANT_EVAL;
2944 if ( (Missiontime - time) >= delay )
2945 return SEXP_KNOWN_TRUE;
2951 int sexp_is_disarmed_delay( int n )
2959 delay = i2f(atoi(CTEXT(n)));
2961 // check value of is_disarmed for a known false value and return that immediately if it is
2962 val = sexp_is_disarmed( CDR(n), &time );
2963 if ( val == SEXP_KNOWN_FALSE )
2966 if ( val == SEXP_CANT_EVAL )
2967 return SEXP_CANT_EVAL;
2970 if ( (Missiontime - time) >= delay )
2971 return SEXP_KNOWN_TRUE;
2977 int sexp_has_docked_delay( int n )
2979 char *docker = CTEXT(n);
2980 char *dockee = CTEXT(CDR(n));
2981 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
2982 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
2985 Assert ( count > 0 );
2986 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2987 return SEXP_KNOWN_FALSE;
2989 if (sexp_query_has_yet_to_arrive(docker))
2990 return SEXP_CANT_EVAL;
2992 if (sexp_query_has_yet_to_arrive(dockee))
2993 return SEXP_CANT_EVAL;
2995 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) )
2998 if ( (Missiontime - time) >= delay )
2999 return SEXP_KNOWN_TRUE;
3004 int sexp_has_undocked_delay( int n )
3006 char *docker = CTEXT(n);
3007 char *dockee = CTEXT(CDR(n));
3008 int count = atoi(CTEXT(CDR(CDR(n))));
3009 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
3012 if (sexp_query_has_yet_to_arrive(docker))
3013 return SEXP_CANT_EVAL;
3015 if (sexp_query_has_yet_to_arrive(dockee))
3016 return SEXP_CANT_EVAL;
3018 Assert ( count > 0 );
3019 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ) {
3020 // if either ship destroyed before they dock, then sexp is known false
3021 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
3022 return SEXP_KNOWN_FALSE;
3027 if ( (Missiontime - time) >= delay )
3028 return SEXP_KNOWN_TRUE;
3033 int sexp_has_arrived_delay( int n )
3041 delay = i2f(atoi(CTEXT(n)));
3043 // check return value from arrived function. if can never arrive, then return that value here as well
3044 val = sexp_has_arrived( CDR(n), &time );
3045 if ( val == SEXP_KNOWN_FALSE )
3048 if ( val == SEXP_CANT_EVAL )
3049 return SEXP_CANT_EVAL;
3052 if ( (Missiontime - time) >= delay )
3053 return SEXP_KNOWN_TRUE;
3059 int sexp_has_departed_delay( int n )
3067 delay = i2f(atoi(CTEXT(n)));
3069 // must first check to see if the departed function could ever be true/false or is true or false.
3070 // if it can never be true, return that value
3071 val = sexp_has_departed( CDR(n), &time);
3072 if ( val == SEXP_KNOWN_FALSE )
3075 if ( val == SEXP_CANT_EVAL )
3076 return SEXP_CANT_EVAL;
3079 if ( (Missiontime - time) >= delay )
3080 return SEXP_KNOWN_TRUE;
3086 // function to determine if a ship is done flying waypoints after N seconds
3087 int sexp_are_waypoints_done_delay( int n )
3089 char *ship_name, *waypoint_name;
3092 ship_name = CTEXT(n);
3093 waypoint_name = CTEXT(CDR(n));
3094 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
3096 if (sexp_query_has_yet_to_arrive(ship_name))
3097 return SEXP_CANT_EVAL;
3099 // a destroyed or departed ship will never reach their goal -- return known false
3101 // Not checking the entries below. Ships which warp out after reaching their goal (or getting
3102 // destroyed after their goal), but after reaching their waypoints, may have this goal incorrectly
3105 // now check the log for the waypoints done entry
3106 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, &time) ) {
3107 if ( (Missiontime - time) >= delay )
3108 return SEXP_KNOWN_TRUE;
3110 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
3111 return SEXP_KNOWN_FALSE;
3112 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
3113 return SEXP_KNOWN_FALSE;
3119 // function to determine is all of a given ship type are destroyed
3120 int sexp_ship_type_destroyed( int n )
3125 percent = atoi(CTEXT(n));
3126 shiptype = CTEXT(CDR(n));
3128 for ( type = 0; type < MAX_SHIP_TYPE_COUNTS; type++ ) {
3129 if ( !stricmp( Ship_type_names[type], shiptype) )
3133 // bogus if we reach the end of this array!!!!
3134 if ( type == MAX_SHIP_TYPE_COUNTS ) {
3139 if ( Ship_counts[type].total == 0 )
3142 // determine if the percentage of killed/total is >= percentage given in the expression
3143 if ( (Ship_counts[type].killed * 100 / Ship_counts[type].total) >= percent)
3144 return SEXP_KNOWN_TRUE;
3150 // following are time based functions
3151 int sexp_has_time_elapsed(int n)
3153 int time = num_eval(n);
3155 if ( f2i(Missiontime) >= time )
3156 return SEXP_KNOWN_TRUE;
3161 // next function returns the time into the mission
3162 int sexp_mission_time()
3164 return f2i(Missiontime);
3167 // returns percent of length of distance to special warpout plane
3168 int sexp_special_warp_dist( int n)
3174 ship_name = CTEXT(n);
3176 // check to see if either ship was destroyed or departed. If so, then make this node known
3178 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ) {
3179 return SEXP_NAN_FOREVER;
3183 shipnum = ship_name_lookup(ship_name);
3188 // check that ship has warpout_objnum
3189 if (Ships[shipnum].special_warp_objnum == -1) {
3193 Assert( (Ships[shipnum].special_warp_objnum >= 0) && (Ships[shipnum].special_warp_objnum < MAX_OBJECTS));
3194 if ( (Ships[shipnum].special_warp_objnum < 0) && (Ships[shipnum].special_warp_objnum >= MAX_OBJECTS) ) {
3198 // check the special warpout device is valid
3200 object *ship_objp = &Objects[Ships[shipnum].objnum];
3201 object *warp_objp = &Objects[Ships[shipnum].special_warp_objnum];
3202 if (warp_objp->type == OBJ_SHIP) {
3203 if (Ship_info[Ships[warp_objp->instance].ship_info_index].flags & SIF_KNOSSOS_DEVICE) {
3212 // check if within 45 degree half-angle cone of facing
3213 float dot = fl_abs(vm_vec_dotprod(&warp_objp->orient.fvec, &ship_objp->orient.fvec));
3220 float dist = fvi_ray_plane(&hit_pt, &warp_objp->pos, &warp_objp->orient.fvec, &ship_objp->pos, &ship_objp->orient.fvec, 0.0f);
3221 polymodel *pm = model_get(Ships[shipnum].modelnum);
3224 // return as a percent of length
3225 return (int) (100.0f * dist / ship_get_length(&Ships[shipnum]));
3229 int sexp_time_destroyed(int n)
3233 if ( !mission_log_get_time( LOG_SHIP_DESTROYED, CTEXT(n), NULL, &time) ){ // returns 0 when not found
3240 int sexp_time_wing_destroyed(int n)
3244 if ( !mission_log_get_time( LOG_WING_DESTROYED, CTEXT(n), NULL, &time) ){
3251 int sexp_time_docked(int n)
3254 char *docker = CTEXT(n);
3255 char *dockee = CTEXT(CDR(n));
3256 int count = atoi(CTEXT(CDR(CDR(n))));
3258 Assert ( count > 0 );
3259 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) ){
3266 int sexp_time_undocked(int n)
3269 char *docker = CTEXT(n);
3270 char *dockee = CTEXT(CDR(n));
3271 int count = atoi(CTEXT(CDR(CDR(n))));
3273 Assert ( count > 0 );
3274 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ){
3281 int sexp_time_ship_arrived(int n)
3286 if ( !mission_log_get_time( LOG_SHIP_ARRIVE, CTEXT(n), NULL, &time ) ){
3293 int sexp_time_wing_arrived(int n)
3298 if ( !mission_log_get_time( LOG_WING_ARRIVE, CTEXT(n), NULL, &time ) ){
3305 int sexp_time_ship_departed(int n)
3310 if ( !mission_log_get_time( LOG_SHIP_DEPART, CTEXT(n), NULL, &time ) ){
3317 int sexp_time_wing_departed(int n)
3322 if ( !mission_log_get_time( LOG_WING_DEPART, CTEXT(n), NULL, &time ) ){
3329 // function to return the remaining shields as a percentage of the given ship.
3330 int sexp_shields_left(int n)
3332 int shipnum, percent;
3335 shipname = CTEXT(n);
3337 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3338 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3339 return SEXP_NAN_FOREVER;
3342 shipnum = ship_name_lookup( shipname );
3343 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3347 // now return the amount of shields left as a percentage of the whole.
3348 percent = (int)(get_shield_strength(&Objects[Ships[shipnum].objnum]) / Ship_info[Ships[shipnum].ship_info_index].shields * 100.0f);
3352 // function to return the remaining hits left as a percentage of the whole. This hit amount counts for
3353 // all hits on the ship (hull + subsystems). Use hits_left_hull to find hull hits remaining.
3354 int sexp_hits_left(int n)
3356 int shipnum, percent;
3359 shipname = CTEXT(n);
3361 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3362 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3363 return SEXP_NAN_FOREVER;
3366 shipnum = ship_name_lookup( shipname );
3367 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3371 // now return the amount of hits left as a percentage of the whole. Subtract the percentage from 100
3372 // since we are working with total hit points taken, not total remaining.
3373 ship *shipp = &Ships[shipnum];
3374 ship_info *sip = &Ship_info[shipp->ship_info_index];
3375 object *objp = &Objects[shipp->objnum];
3376 percent = (int) (100.0f * objp->hull_strength / sip->initial_hull_strength);
3380 // is ship visible on radar
3381 // returns 0 - not visible
3382 // returns 1 - marginally targetable (jiggly on radar)
3383 // returns 2 - fully targetable
3384 int sexp_is_ship_visible(int n)
3388 int ship_is_visible = 0;
3390 // if multiplayer, bail
3391 if (Game_mode & GM_MULTIPLAYER) {
3392 return SEXP_NAN_FOREVER;
3395 shipname = CTEXT(n);
3397 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3398 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3399 return SEXP_NAN_FOREVER;
3402 shipnum = ship_name_lookup( shipname );
3403 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3407 // get ship's *radar* visiblity
3408 if (Player_ship != NULL) {
3409 if (ship_is_visible_by_team(shipnum, Player_ship->team)) {
3410 ship_is_visible = 2;
3414 // only check awacs level if ship is not visible by team
3415 if (Player_ship != NULL && !ship_is_visible) {
3416 float awacs_level = awacs_get_level(&Objects[Ships[shipnum].objnum], Player_ship);
3417 if (awacs_level >= 1.0f) {
3418 ship_is_visible = 2;
3419 } else if (awacs_level > 0) {
3420 ship_is_visible = 1;
3424 return ship_is_visible;
3427 // get multi team v team score
3428 // if not multi team v team return 0
3429 // if invalid team return 0
3430 int sexp_team_score(int node)
3433 if (Game_mode & GM_MULTIPLAYER) {
3434 if (Netgame.type_flags & NG_TYPE_TEAM) {
3436 int team = atoi(CTEXT(node));
3439 return Multi_team0_score;
3440 } else if (team == 2) {
3441 return Multi_team1_score;
3443 // invalid team index
3454 // function to return the remaining hits left on a subsystem as a percentage of thw whole.
3455 int sexp_hits_left_subsystem(int n)
3457 int shipnum, percent, type;
3461 shipname = CTEXT(n);
3463 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3464 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3465 return SEXP_NAN_FOREVER;
3468 shipnum = ship_name_lookup( shipname );
3469 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3473 subsys_name = CTEXT(CDR(n));
3474 type = ai_get_subsystem_type( subsys_name );
3475 if ( (type >= 0) && (type < SUBSYSTEM_MAX) ) {
3476 // return as a percentage the hits remaining on the subsystem as a whole (i.e. for 3 engines,
3477 // we are returning the sum of the hits on the 3 engines)
3478 if (type == SUBSYSTEM_UNKNOWN) {
3479 // find the ship subsystem by searching ship's subsys_list
3481 ss = GET_FIRST( &Ships[shipnum].subsys_list );
3482 while ( ss != END_OF_LIST( &Ships[shipnum].subsys_list ) ) {
3484 if ( !stricmp(ss->system_info->subobj_name, subsys_name)) {
3485 percent = (int) (ss->current_hits / ss->system_info->max_hits * 100.0f);
3489 ss = GET_NEXT( ss );
3491 // we reached end of ship subsys list without finding subsys_name
3495 percent = (int)(ship_get_subsystem_strength(&Ships[shipnum],type) * 100.0f);
3499 return SEXP_NAN; // if for some strange reason, the type field of the subsystem is bogus
3502 int sexp_determine_team(char *subj)
3506 if (!stricmp(subj, "<any friendly>")){
3507 team = TEAM_FRIENDLY;
3508 } else if (!stricmp(subj, "<any hostile>")){
3509 team = TEAM_HOSTILE;
3510 } else if (!stricmp(subj, "<any neutral>")){
3511 team = TEAM_NEUTRAL;
3512 } else if (!stricmp(subj, "<any unknown>")){
3513 team = TEAM_UNKNOWN;
3514 } else if (!stricmp(subj, "<any traitor>")){
3515 team = TEAM_TRAITOR;
3521 // returns the distance between two objects. If a wing is specificed as one (or both) or the arguments
3522 // to this function, we are looking for the closest distance
3523 int sexp_distance(int n)
3525 int i, team, obj, dist, dist_min = 0, inited = 0;
3526 char *sname1, *sname2;
3531 sname2 = CTEXT(CDR(n));
3533 // check to see if either ship was destroyed or departed. If so, then make this node known
3535 if ( mission_log_get_time(LOG_SHIP_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname1, NULL, NULL) ||
3536 mission_log_get_time(LOG_SHIP_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname2, NULL, NULL) )
3537 return SEXP_NAN_FOREVER;
3539 // one of the names might be the name of a wing. Check to see if the wing is detroyed or departed
3540 if ( mission_log_get_time(LOG_WING_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname1, NULL, NULL) ||
3541 mission_log_get_time(LOG_WING_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname2, NULL, NULL) )
3542 return SEXP_NAN_FOREVER;
3544 team = sexp_determine_team(sname1);
3545 if (team) { // we have a team type, so check all ships of that type
3546 so = GET_FIRST(&Ship_obj_list);
3547 while (so != END_OF_LIST(&Ship_obj_list)) {
3548 if (Ships[Objects[so->objnum].instance].team == team) {
3550 dist = sexp_distance2(obj, sname2);
3551 if (dist != SEXP_NAN) {
3552 if (!inited || (dist < dist_min)) {
3562 if (!inited) // no objects were checked
3568 // at this point, we must have a wing, ship or point for a subj
3569 obj = ship_name_lookup(sname1);
3571 return sexp_distance2(Ships[obj].objnum, sname2);
3573 // at this point, we must have a wing or point for a subj
3574 obj = waypoint_lookup(sname1);
3576 return sexp_distance2(obj, sname2);
3578 // at this point, we must have a wing for a subj
3579 obj = wing_name_lookup(sname1);
3581 return SEXP_NAN; // we apparently don't have anything legal
3583 wingp = &Wings[obj];
3584 for (i=0; i<wingp->current_count; i++) {
3585 obj = Ships[wingp->ship_index[i]].objnum;
3586 dist = sexp_distance2(obj, sname2);
3587 if (dist != SEXP_NAN) {
3588 if (!inited || (dist < dist_min)) {
3595 if (!inited) // no objects were checked
3601 // check distance between a given ship and a given subject (ship, wing, any <team>).
3602 int sexp_distance2(int obj1, char *subj)
3604 int i, team, obj2, dist, dist_min = 0, inited = 0;
3608 team = sexp_determine_team(subj);
3609 if (team) { // we have a team type, so check all ships of that type
3610 so = GET_FIRST(&Ship_obj_list);
3611 while (so != END_OF_LIST(&Ship_obj_list)) {
3612 if (Ships[Objects[so->objnum].instance].team == team) {
3614 dist = sexp_distance3(obj1, obj2);
3615 if (dist != SEXP_NAN) {
3616 if (!inited || (dist < dist_min)) {
3626 if (!inited) // no objects were checked
3632 // at this point, we must have a wing, ship or point for a subj
3633 obj2 = ship_name_lookup(subj);
3635 return sexp_distance3(obj1, Ships[obj2].objnum);
3637 // at this point, we must have a wing or point for a subj
3638 obj2 = waypoint_lookup(subj);
3640 return sexp_distance3(obj1, obj2);
3642 // at this point, we must have a wing for a subj
3643 obj2 = wing_name_lookup(subj);
3645 return SEXP_NAN; // we apparently don't have anything legal
3647 wingp = &Wings[obj2];
3648 for (i=0; i<wingp->current_count; i++) {
3649 obj2 = Ships[wingp->ship_index[i]].objnum;
3650 dist = sexp_distance3(obj1, obj2);
3651 if (dist != SEXP_NAN) {
3652 if (!inited || (dist < dist_min)) {
3659 if (!inited) // no objects were checked
3665 // check distance between two given objects
3666 int sexp_distance3(int obj1, int obj2)
3668 if ( (obj1 == -1) || (obj2 == -1) ) // if either object isn't present in the mission now
3669 return SEXP_NAN; // return a really small number
3671 if ( (Objects[obj1].type == OBJ_SHIP) && (Objects[obj2].type == OBJ_SHIP) ) {
3672 if (OBJ_INDEX(Player_obj) == obj1)
3673 return (int) hud_find_target_distance( &Objects[obj2], &Objects[obj1] );
3675 return (int) hud_find_target_distance( &Objects[obj1], &Objects[obj2] );
3678 return (int) vm_vec_dist_quick( &Objects[obj1].pos, &Objects[obj2].pos );
3682 // funciton to determine when the last meaningful order was given to one or more ships. Returns
3683 // true or false depending on whether or not a meaningful order was received
3684 int sexp_last_order_time( int n )
3691 time = i2f(atoi(CTEXT(n)));
3692 Assert ( time >= 0 );
3697 instance = ship_name_lookup(name);
3698 if ( instance != -1 ) {
3699 aigp = Ai_info[Ships[instance].ai_index].goals;
3701 instance = wing_name_lookup(name);
3702 if ( instance == -1 ) // if we cannot find ship or wing, return 0
3704 aigp = Wings[instance].ai_goals;
3707 // with the ship, check the ai_goals structure for this ship and determine if there are any
3708 // orders which are < time seconds since current mission time
3709 for ( i = 0; i < MAX_AI_GOALS; i++ ) {
3712 mode = aigp->ai_mode;
3713 if ( (mode != AI_GOAL_NONE) && (mode != AI_GOAL_WARP) )
3714 if ( (aigp->time + time) > Missiontime )
3718 if ( i == MAX_AI_GOALS )
3727 // sexpression to return the number of players in the mission
3728 int sexp_num_players()
3734 for ( objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
3735 if ( (objp->type == OBJ_SHIP) && (objp->flags & OF_PLAYER_SHIP) )
3742 // expression to determine if the current skill level of the game is at least
3743 // the skill level given in the sexpression
3744 int sexp_skill_level_at_least( int n )
3749 level_name = CTEXT(n);
3750 for (i = 0; i < NUM_SKILL_LEVELS; i++ ) {
3751 if ( !stricmp(level_name, Skill_level_names(i, 0)) ) {
3752 if ( Game_skill_level >= i ){
3760 // return 0 if not found!!!
3764 int sexp_was_promotion_granted(int n)
3766 if (Player->flags & PLAYER_FLAGS_PROMOTED)
3772 int sexp_was_medal_granted(int n)
3778 if (Player->stats.m_medal_earned >= 0)
3784 medal_name = CTEXT(n);
3785 for (i=0; i<NUM_MEDALS; i++) {
3786 if (!stricmp(medal_name, Medals[i].name))
3790 if ( (i < NUM_MEDALS) && (Player->stats.m_medal_earned == i) )
3796 // function which returns true if the percentage of ships (and ships in wings) departed is at
3797 // least the percentage given. what determine if we should check destroyed or departed status
3798 int sexp_percent_ships_depart_destroy(int n, int what)
3804 percent = atoi(CTEXT(n));
3808 // iterate through the rest of the ships/wings in the list and tally the departures and the
3810 for ( n = CDR(n); n != -1; n = CDR(n) ) {
3815 wingnum = wing_name_lookup( name, 1 );
3816 if ( wingnum != -1 ) {
3817 // for wings, we can increment the total by the total number of ships that we expect for
3818 // this wing, and the departures by the number of departures stored for this wing
3819 total += (Wings[wingnum].wave_count * Wings[wingnum].num_waves);
3820 if ( what == OP_PERCENT_SHIPS_DEPARTED )
3821 count += Wings[wingnum].total_departed;
3822 else if ( what == OP_PERCENT_SHIPS_DESTROYED )
3823 count += Wings[wingnum].total_destroyed;
3825 Int3(); // this would be very bogus!
3827 // must be a ship, so increment the total by 1, then determine if this ship has departed
3829 if ( what == OP_PERCENT_SHIPS_DEPARTED ) {
3830 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) )
3832 } else if ( what == OP_PERCENT_SHIPS_DESTROYED ) {
3833 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3836 Int3(); // this would be very bogus as well.
3841 // now, look at the percentage
3842 if ( ((count * 100) / total) >= percent )
3843 return SEXP_KNOWN_TRUE;
3848 // function to tell is a list of ships has departed from within a radius of a given jump node.
3849 // returns true N seconds after the list of ships have departed
3850 int sexp_depart_node_delay( int n )
3852 int delay, count, num_departed;
3853 char *jump_node_name, *name;
3854 fix latest_time, this_time;
3856 delay = atoi( CTEXT(n) );
3858 jump_node_name = CTEXT(n);
3860 // iterate through the list of ships
3869 if (sexp_query_has_yet_to_arrive(name))
3870 return SEXP_CANT_EVAL;
3872 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
3873 // the sexpression is not true.
3874 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3875 return SEXP_KNOWN_FALSE;
3876 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, jump_node_name, &this_time) ) {
3878 if ( this_time > latest_time )
3879 latest_time = this_time;
3884 if ( (count == num_departed) && ((Missiontime - latest_time) >= delay) )
3885 return SEXP_KNOWN_TRUE;
3890 // sexpression which returns true when the listed ships/wings have all been destroyed or
3892 int sexp_destroyed_departed_delay( int n )
3895 fix delay, latest_time;
3899 delay = i2f(atoi(CTEXT(n)));
3902 count = 0; // number destroyed or departed
3903 total = 0; // total number of ships/wings to check
3912 // for wings, check the WF_GONE flag to see if there are no more ships in this wing to arrive.
3913 wingnum = wing_name_lookup(name, 1);
3914 if ( wingnum != -1 ) {
3915 if ( Wings[wingnum].flags & WF_WING_GONE ) {
3916 // be sure to get the latest time of one of these
3917 if ( Wings[wingnum].time_gone > latest_time ){
3918 time_gone = Wings[wingnum].time_gone;
3922 } else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time_gone) ) {
3924 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time_gone) ) {
3928 // check our latest time
3929 if ( time_gone > latest_time ){
3930 latest_time = time_gone;
3936 if ( (count == total) && (Missiontime > (latest_time + delay)) )
3937 return SEXP_KNOWN_TRUE;
3942 int sexp_special_warpout_name( int node )
3944 int shipnum, knossos_num;
3945 char *ship_name, *knossos;
3947 ship_name = CTEXT(node);
3948 knossos = CTEXT(CDR(node));
3950 // check to see if either ship was destroyed or departed. If so, then make this node known
3952 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ||
3953 mission_log_get_time(LOG_SHIP_DESTROYED, knossos, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, knossos, NULL, NULL) )
3954 return SEXP_NAN_FOREVER;
3957 shipnum = ship_name_lookup(ship_name);
3963 knossos_num = ship_name_lookup(knossos);
3964 if (knossos_num < 0) {
3968 // set special warpout objnum
3969 Ships[shipnum].special_warp_objnum = knossos_num;
3974 // function which determines if N seconds have elpased since all discovery of all cargo
3976 int sexp_is_cargo_known( int n, int check_delay )
3978 int count, shipnum, num_known, delay;
3986 // get the delay value (if there is one)
3988 if ( check_delay ) {
3989 delay = atoi(CTEXT(n) );
4001 // see if we have already checked this entry
4002 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4009 // see if the ship has already exited the mission (either through departure or destruction). If so,
4010 // grab the status of whether the cargo is known from this list
4011 exited_index = ship_find_exited_ship_by_name( name );
4012 if (exited_index != -1 ) {
4013 if ( !(Ships_exited[exited_index].flags & SEF_CARGO_KNOWN) )
4014 return SEXP_KNOWN_FALSE;
4016 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4017 // but won't cause huge delays.
4018 time_known = Missiontime - Ships_exited[exited_index].time;
4019 if ( f2i(time_known) >= delay )
4023 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4024 // is yet to arrive.
4025 shipnum = ship_name_lookup( name );
4026 if ( shipnum != -1 ) {
4027 if ( Ships[shipnum].flags & SF_CARGO_REVEALED ) {
4028 time_known = Missiontime - Ships[shipnum].time_cargo_revealed;
4029 if ( f2i(time_known) >= delay )
4036 // if cargo is known, mark our variable and this sexpression.
4039 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4045 Directive_count += count - num_known;
4046 if ( count == num_known )
4047 return SEXP_KNOWN_TRUE;
4052 int sexp_has_been_tagged_delay(int n)
4054 int count, shipnum, num_known, delay;
4062 // get the delay value
4063 delay = atoi(CTEXT(n) );
4075 // see if we have already checked this entry
4076 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4083 // see if the ship has already exited the mission (either through departure or destruction). If so,
4084 // grab the status of whether the cargo is known from this list
4085 exited_index = ship_find_exited_ship_by_name( name );
4086 if (exited_index != -1 ) {
4087 if ( !(Ships_exited[exited_index].flags & SEF_BEEN_TAGGED) )
4088 return SEXP_KNOWN_FALSE;
4090 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4091 // but won't cause huge delays.
4092 time_known = Missiontime - Ships_exited[exited_index].time;
4093 if ( f2i(time_known) >= delay )
4097 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4098 // is yet to arrive.
4099 shipnum = ship_name_lookup( name );
4100 if ( shipnum != -1 ) {
4101 if ( Ships[shipnum].time_first_tagged != 0 ) {
4102 time_known = Missiontime - Ships[shipnum].time_first_tagged;
4103 if ( f2i(time_known) >= delay )
4110 // if cargo is known, mark our variable and this sexpression.
4113 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4119 Directive_count += count - num_known;
4120 if ( count == num_known )
4121 return SEXP_KNOWN_TRUE;
4126 int sexp_cap_subsys_cargo_known_delay(int n)
4128 int delay, count, delta_time, num_known;
4129 char *ship_name, *subsys_name;
4135 delay = atoi(CTEXT(n));
4139 ship_name = CTEXT(n);
4151 // see if we have already checked this entry
4152 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4156 subsys_name = CTEXT(n);
4158 logged = mission_log_get_time(LOG_CAP_SUBSYS_CARGO_REVEALED, ship_name, subsys_name, &time_known);
4160 delta_time = f2i(Missiontime - time_known);
4161 if (delta_time >= delay) {
4166 // if (exited or destroyed) and not logged, known false
4167 // otherwise, still out there and cargo not yet known
4168 if ( (mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL )) && !logged ) {
4169 return SEXP_KNOWN_FALSE;
4175 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4181 Directive_count += count - num_known;
4182 if ( count == num_known )
4183 return SEXP_KNOWN_TRUE;
4189 // return object index of waypoint or -1 if no such waypoint
4190 int waypoint_lookup(char *name)
4196 ptr = GET_FIRST(&obj_used_list);
4197 while (ptr != END_OF_LIST(&obj_used_list)) {
4198 if (ptr->type == OBJ_WAYPOINT) {
4200 sprintf(buf, "%s:%d", Waypoint_lists[i / 65536].name, (i & 0xffff) + 1);
4201 if ( !stricmp(buf, name) )
4202 return OBJ_INDEX(ptr);
4205 ptr = GET_NEXT(ptr);
4211 // conditional sexpressions follow
4213 // eval_when evaluates the when conditional
4214 int eval_when(int n)
4218 Assert( n >= 0 ); // must have valid sexp index
4221 val = eval_sexp(cond); // get the value of the the conditional
4222 if ( val ) { // if the value is true, perform the actions is the 'then' part of the if
4226 while ( actions != -1 ) {
4229 val = eval_sexp(exp); // these sexp evaled only for side effects
4230 actions = CDR(actions);
4234 if (Sexp_nodes[cond].value == SEXP_KNOWN_FALSE)
4235 return SEXP_KNOWN_FALSE; // no need to waste time on this anymore
4237 if (val == SEXP_KNOWN_FALSE)
4238 return 0; // can't return known false, as this would bypass future actions under the when
4243 // eval_cond() evaluates the cond conditional
4244 int eval_cond( int n )
4246 int cond = 0, node, val = 0;
4252 val = eval_sexp(cond);
4254 // if the conditional evaluated to true, then we must evaluate the rest of the expression returning
4255 // the value of this evaluation
4260 actions = CDR(node);
4261 while (actions >= 0) {
4264 val = eval_sexp(exp); // these sexp evaled only for side effects
4266 actions = CDR(actions);
4272 // move onto the next cond clause
4279 int sexp_is_iff( int n )
4281 char *ship_name, *iff;
4286 // iff value is the first parameter, second is a list of one or more ships to check to see if the
4287 // iff value matches
4289 if ( !stricmp(iff, "friendly") )
4290 team = TEAM_FRIENDLY;
4291 else if ( !stricmp(iff, "hostile") )
4292 team = TEAM_HOSTILE;
4293 else if ( !stricmp(iff, "neutral") )
4294 team = TEAM_NEUTRAL;
4295 else if ( !stricmp(iff, "unknown") )
4296 team = TEAM_UNKNOWN;
4297 else if ( !stricmp(iff, "traitor") )
4298 team = TEAM_TRAITOR;
4301 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", iff));
4302 team = TEAM_HOSTILE;
4307 ship_name = CTEXT(n);
4308 // find the ship and check to be sure that it is still around.
4309 num = ship_name_lookup(ship_name);
4310 if ( num < 0 ) // if the ship is gone, can't check it's iff.
4313 // if the team doesn't match the team specified, return 0 immediately
4314 if ( Ships[num].team != team)
4323 void sexp_change_iff( int n )
4325 char *ship_name, *new_iff;
4330 if ( !stricmp(new_iff, "friendly") )
4331 new_team = TEAM_FRIENDLY;
4332 else if ( !stricmp(new_iff, "hostile") )
4333 new_team = TEAM_HOSTILE;
4334 else if ( !stricmp(new_iff, "neutral") )
4335 new_team = TEAM_NEUTRAL;
4336 else if ( !stricmp(new_iff, "unknown") )
4337 new_team = TEAM_UNKNOWN;
4338 else if ( !stricmp(new_iff, "traitor") )
4339 new_team = TEAM_TRAITOR;
4341 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", new_iff));
4342 new_team = TEAM_HOSTILE;
4348 ship_name = CTEXT(n);
4350 // find the ship and check to be sure that it is still around.
4351 num = ship_name_lookup(ship_name);
4352 if ( num >= 0 ) { // only change iff if we found the ship
4353 Ships[num].team = new_team;
4355 // send a network packet if we need to
4356 if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Ships[num].objnum >= 0)){
4357 send_change_iff_packet(Objects[Ships[num].objnum].net_signature, new_team);
4366 // following routine adds an ai goal to a ship structure. The sexpression index
4367 // passed in should be an ai-goal of the proper form. The code in MissionGoal should
4368 // check the syntax.
4370 void sexp_add_ship_goal( int n )
4376 ship_name = CTEXT(n);
4377 num = ship_name_lookup(ship_name);
4378 if ( num < 0 ) // ship not around anymore???? then forget it!
4382 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4385 // identical to above, except add a wing
4386 void sexp_add_wing_goal( int n )
4392 wing_name = CTEXT(n);
4393 num = wing_name_lookup(wing_name);
4394 if ( num < 0 ) // ship not around anymore???? then forget it!
4398 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4401 // sexp_add_goal adds a goal to the specified entiry (ships and wings have unique names between
4403 void sexp_add_goal( int n )
4412 // first, look for ship name -- if found, then add ship goal. else look for wing name -- if
4413 // found, add wing goal
4414 if ( (num = ship_name_lookup(name)) != -1 )
4415 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4416 else if ( (num = wing_name_lookup(name)) != -1 )
4417 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4420 // clears out all ai goals for a ship
4421 void sexp_clear_ship_goals( int n )
4427 ship_name = CTEXT(n);
4428 num = ship_name_lookup(ship_name);
4429 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4432 // clears out ai goals for a wing
4433 void sexp_clear_wing_goals( int n )
4439 wing_name = CTEXT(n);
4440 num = wing_name_lookup(wing_name);
4443 ai_clear_wing_goals( num );
4446 // this function clears all ai goals for the given ship or wing
4447 void sexp_clear_goals( int n )
4455 if ( (num = ship_name_lookup(name)) != -1 )
4456 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4457 else if ( (num = wing_name_lookup(name)) != -1 )
4458 ai_clear_wing_goals( num );
4464 // this function get called by send-message or send-message random with the name of the message, sender,
4466 void sexp_send_one_message( char *name, char *who_from, char *priority, int group, int delay )
4468 int ipriority, num, ship_index, source;
4475 // determine the priority of the message
4476 if ( !stricmp(priority, "low") )
4477 ipriority = MESSAGE_PRIORITY_LOW;
4478 else if ( !stricmp(priority, "normal") )
4479 ipriority = MESSAGE_PRIORITY_NORMAL;
4480 else if ( !stricmp(priority, "high") )
4481 ipriority = MESSAGE_PRIORITY_HIGH;
4484 ipriority = MESSAGE_PRIORITY_NORMAL;
4487 // check to see if the 'who_from' string is a ship that had been destroyed or departed. If so,
4488 // then don't send the message. We must look at 'who_from' to determine what to look for. who_from
4489 // may be any allied person, any wingman, a wingman from a specific wing, or a specific ship
4492 source = MESSAGE_SOURCE_COMMAND;
4493 if ( who_from[0] == '#' ) {
4494 message_send_unique_to_player( name, &(who_from[1]), MESSAGE_SOURCE_SPECIAL, ipriority, group, delay );
4496 } else if (!stricmp(who_from, "<any allied>")) {
4497 //Int3(); // no longer supported
4499 } else if ( (num = wing_name_lookup(who_from)) != -1 ) {
4500 // message from a wing
4501 // this will be an invalid case soon
4503 // choose wing leader to speak for wing (hence "1" at end of ship_get_random_ship_in_wing)
4504 ship_index = ship_get_random_ship_in_wing( num, SHIP_GET_NO_PLAYERS, 1 );
4505 if ( ship_index == -1 ) {
4506 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4510 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_SHIP_DEPART, who_from, NULL, NULL)
4511 || mission_log_get_time(LOG_WING_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_WING_DEPART, who_from, NULL, NULL) ) {
4512 // getting into this if statement means that the ship or wing (sender) is no longer in the mission
4513 // if message is high priority, make it come from Terran Command
4514 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4517 source = MESSAGE_SOURCE_COMMAND;
4519 } else if ( !stricmp(who_from, "<any wingman>") || (wing_name_lookup(who_from) != -1) ) {
4520 source = MESSAGE_SOURCE_WINGMAN;
4522 // Message from a apecific ship
4523 // bail if not high priority, otherwise reroute to command
4524 source = MESSAGE_SOURCE_SHIP;
4525 ship_index = ship_name_lookup(who_from);
4526 if ( ship_index == -1 ) {
4527 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4529 source = MESSAGE_SOURCE_COMMAND;
4533 if ( ship_index == -1 ){
4536 shipp = &Ships[ship_index];
4539 message_send_unique_to_player( name, shipp, source, ipriority, group, delay );
4542 void sexp_send_message( int n )
4544 char *name, *who_from, *priority, *tmp;
4551 who_from = CTEXT(n);
4552 priority = CTEXT(CDR(n));
4553 name = CTEXT(CDR(CDR(n)));
4555 // a temporary check to see if the name field matched a priority since I am in the process
4556 // of reordering the arguments
4557 if ( !stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high") ) {
4563 sexp_send_one_message( name, who_from, priority, 0, 0 );
4566 void sexp_send_message_list( int n )
4568 char *name, *who_from, *priority;
4575 // send a bunch of messages
4578 who_from = CTEXT(n);
4583 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4586 priority = CTEXT(n);
4591 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4599 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4602 delay += atoi(CTEXT(n));
4605 sexp_send_one_message(name, who_from, priority, 1, delay);
4612 void sexp_send_random_message( int n )
4614 char *name, *who_from, *priority;
4615 int temp, num_messages, message_num;
4618 who_from = CTEXT(n);
4619 priority = CTEXT(CDR(n));
4625 // count the number of messages that we have
4633 Assert ( num_messages >= 1 );
4635 // get a random message, and pass the parameters to send_one_message
4636 message_num = myrand() % num_messages;
4639 if ( message_num == 0 )
4644 Assert (n != -1); // should have found the message!!!
4647 sexp_send_one_message( name, who_from, priority, 0, 0 );
4650 void sexp_self_destruct( int n )
4656 // get the ship name and be sure that it is still in the mission. Destroy it if we find it
4657 ship_name = CTEXT(n);
4658 shipnum = ship_name_lookup( ship_name );
4659 if ( shipnum == -1 )
4661 ship_self_destruct( &Objects[Ships[shipnum].objnum] );
4667 void sexp_next_mission( int n )
4672 mission_name = CTEXT(n);
4673 for (i = 0; i < Campaign.num_missions; i++) {
4674 if ( !stricmp(Campaign.missions[i].name, mission_name) ) {
4675 Campaign.next_mission = i;
4679 Error(LOCATION, "Mission name %s not found in campaign file for next-mission command", mission_name);
4682 // function to deal with the end-of-campaign sexpression.
4683 void sexp_end_of_campaign( int n )
4685 // this is really a do-nothing sexpression. It is pretty much a placeholder to allow
4686 // campaigns to have repeat-mission branches at the end of the campaign. By not setting
4687 // anything in this function, the higher level campaign code will see this as end-of-campaign
4688 // since next_mission isn't set to anything. (To be safe, we'll set to -1).
4689 Campaign.next_mission = -1;
4692 // sexpression to end everything. One parameter is the movie to play when this is over.
4693 void sexp_end_campaign( int n )
4695 // post and event to move us to the end-of-campaign state. There we will play a movie, then
4696 // go to debriefing.
4697 // gameseq_post_event( GS_EVENT_END_CAMPAIGN );
4699 // in FS2 our ending is a bit wacky. we'll just flag the mission as having ended the campaign
4700 Campaign_ended_in_mission = 1;
4703 // sabotage subsystem reduces the strength of a subsystem by the given percentage. If it is reduced to
4704 // below 0%, then the hits of the subsystem are set to 0
4705 void sexp_sabotage_subsystem( int n )
4707 char *shipname, *subsystem;
4708 int percentage, shipnum, index;
4709 float sabotage_hits;
4713 shipname = CTEXT(n);
4714 subsystem = CTEXT(CDR(n));
4715 percentage = atoi(CTEXT(CDR(CDR(n))));
4717 shipnum = ship_name_lookup(shipname);
4719 // if no ship, then return immediately.
4720 if ( shipnum == -1 )
4722 shipp = &Ships[shipnum];
4724 // see if we are dealing with the HULL
4725 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4729 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4730 sabotage_hits = ihs * ((float)percentage / 100.0f);
4731 objp = &Objects[shipp->objnum];
4732 objp->hull_strength -= sabotage_hits;
4734 // self destruct the ship if <= 0.
4735 if ( objp->hull_strength <= 0.0f )
4736 ship_self_destruct( objp );
4740 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4741 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4743 index = ship_get_subsys_index(shipp, subsystem);
4744 if ( index == -1 ) {
4745 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for sabotage subsystem\n", subsystem, shipp->ship_name));
4749 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4750 // set the strength to the given percentage if current strength is > given percentage
4751 ss = ship_get_indexed_subsys( shipp, index );
4752 sabotage_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4753 ss->current_hits -= sabotage_hits;
4754 if ( ss->current_hits < 0.0f )
4755 ss->current_hits = 0.0f;
4756 ship_recalc_subsys_strength( shipp );
4759 // repair_subsystem adds some percentage of hits to a subsystem. Anything repaired about 100% is
4761 void sexp_repair_subsystem( int n )
4763 char *shipname, *subsystem;
4764 int percentage, shipnum, index;
4769 shipname = CTEXT(n);
4770 subsystem = CTEXT(CDR(n));
4771 shipnum = ship_name_lookup(shipname);
4773 // if no ship, then return immediately.
4774 if ( shipnum == -1 ) {
4777 shipp = &Ships[shipnum];
4779 // check if we've got a number or an op
4780 if ( CAR(CDR(CDR(n))) != -1) {
4781 percentage = eval_sexp( CAR(CDR(CDR(n))) );
4783 percentage = atoi(CTEXT(CDR(CDR(n))));
4786 // see if we are dealing with the HULL
4787 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4791 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4792 repair_hits = ihs * ((float)percentage / 100.0f);
4793 objp = &Objects[shipp->objnum];
4794 objp->hull_strength += repair_hits;
4795 if ( objp->hull_strength > ihs )
4796 objp->hull_strength = ihs;
4800 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4801 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4803 index = ship_get_subsys_index(shipp, subsystem);
4804 if ( index == -1 ) {
4805 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4809 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4810 // set the strength to the given percentage if current strength is < given percentage
4811 ss = ship_get_indexed_subsys( shipp, index );
4812 repair_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4813 ss->current_hits += repair_hits;
4814 if ( ss->current_hits > ss->system_info->max_hits )
4815 ss->current_hits = ss->system_info->max_hits;
4816 ship_recalc_subsys_strength( shipp );
4819 // sexpression code to set a subsystem of a ship at a specific percentage
4820 void sexp_set_subsystem_strength( int n )
4822 char *shipname, *subsystem;
4823 int percentage, shipnum, index;
4827 shipname = CTEXT(n);
4828 subsystem = CTEXT(CDR(n));
4829 percentage = num_eval(CDR(CDR(n)));
4831 shipnum = ship_name_lookup(shipname);
4833 // if no ship, then return immediately.
4834 if ( shipnum == -1 )
4836 shipp = &Ships[shipnum];
4838 if ( percentage > 100 ) {
4839 nprintf(("Warning", "percentage for set_subsystem_strength > 100 -- setting to 100\n"));
4841 } else if ( percentage < 0 ) {
4842 nprintf(("Werning", "percantage for set_subsystem_strength < 0 -- setting to 0\n"));
4846 // see if we are dealing with the HULL
4847 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4851 objp = &Objects[shipp->objnum];
4853 // destroy the ship if percentage is 0
4854 if ( percentage == 0 ) {
4855 ship_self_destruct( objp );
4857 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4858 objp->hull_strength = ihs * ((float)percentage / 100.0f);
4864 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4865 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4867 index = ship_get_subsys_index(shipp, subsystem);
4868 if ( index == -1 ) {
4869 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4873 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4874 // set the strength to the given percentage if current strength is < given percentage
4875 ss = ship_get_indexed_subsys( shipp, index );
4877 // maybe blow up subsys
4878 if (ss->current_hits > 0) {
4879 if (percentage < 1) {
4880 do_subobj_destroyed_stuff(shipp, ss, NULL);
4885 ss->current_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4887 ship_recalc_subsys_strength( shipp );
4890 // function which changes the validity of a goal. The flag paramater tells us whether to mark the goals
4891 // as valid or invalid
4892 void sexp_change_goal_validity( int n, int flag )
4899 mission_goal_mark_valid( name );
4901 mission_goal_mark_invalid( name );
4907 // function to transfer cargo from one ship to another
4908 void sexp_transfer_cargo( int n )
4910 char *shipname1, *shipname2;
4911 int shipnum1, shipnum2, i;
4914 shipname1 = CTEXT(n);
4915 shipname2 = CTEXT(CDR(n));
4917 // find the ships -- if neither in the mission, the abort
4918 shipnum1 = ship_name_lookup(shipname1);
4919 shipnum2 = ship_name_lookup(shipname2);
4920 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4923 // we must be sure that these two objects are indeed docked
4924 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
4925 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
4926 Int3(); // you are trying to transfer cargo between two ships not docked
4930 if ( !stricmp(Cargo_names[Ships[shipnum1].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4931 Int3(); // you are transfering no cargo!!!!
4935 // transfer cargo from ship1 to ship2
4937 // Don't give warning for large ships (cruiser on up)
4938 if (! (Ship_info[Ships[shipnum2].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
4939 if ( stricmp(Cargo_names[Ships[shipnum2].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4940 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] );
4944 Ships[shipnum2].cargo1 = char(Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
4946 if ( !(Ships[shipnum1].cargo1 & CARGO_NO_DEPLETE) ) {
4947 // need to set ship1's cargo to nothing. scan the cargo_names array looking for the string nothing.
4948 // add it if not found
4949 for (i = 0; i < Num_cargo; i++ ) {
4950 if ( !stricmp(Cargo_names[i], "nothing") ) {
4951 Ships[shipnum1].cargo1 = char(i);
4955 strcpy(Cargo_names[i], "Nothing");
4960 // this function exchanges cargo between two ships
4961 void sexp_exchange_cargo( int n )
4963 char *shipname1, *shipname2;
4964 int shipnum1, shipnum2, temp;
4967 shipname1 = CTEXT(n);
4968 shipname2 = CTEXT(CDR(n));
4970 // find the ships -- if neither in the mission, the abort
4971 shipnum1 = ship_name_lookup(shipname1);
4972 shipnum2 = ship_name_lookup(shipname2);
4973 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4976 // we must be sure that these two objects are indeed docked
4977 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
4978 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
4979 Int3(); // you are trying to transfer cargo between two ships not docked
4983 temp = (Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
4984 Ships[shipnum1].cargo1 = char(Ships[shipnum2].cargo1 & CARGO_INDEX_MASK);
4985 Ships[shipnum2].cargo1 = char(temp);
4988 void sexp_cap_waypont_speed(int n)
4994 shipname = CTEXT(n);
4995 speed = atoi(CTEXT(CDR(n)));
4997 shipnum = ship_name_lookup(shipname);
4999 if (shipnum == -1) {
5000 Int3(); // trying to set waypoint speed of ship not already in game
5004 // cap speed to range (-1, 127) to store within char
5013 Ai_info[Ships[shipnum].ai_index].waypoint_speed_cap = (char) speed;
5016 // this function causes a ship to jettison its cargo
5017 void sexp_jettison_cargo( int n )
5020 int jettison_delay, ship_index;
5023 shipname = CTEXT(n);
5024 jettison_delay = atoi(CTEXT(CDR(n)));
5027 ship_index = ship_name_lookup(shipname);
5033 ship_jettison_cargo(&Ships[ship_index]);
5036 void sexp_cargo_no_deplete( int n )
5039 int ship_index, no_deplete = 1;
5042 shipname = CTEXT(n);
5045 ship_index = ship_name_lookup(shipname);
5050 if ( !(Ship_info[Ships[ship_index].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
5051 Warning(LOCATION, "Trying to make non BIG or HUGE ship %s with non-depletable cargo.\n", Ships[ship_index].ship_name);
5056 no_deplete = atoi(CTEXT(CDR(n)));
5057 Assert((no_deplete == 0) || (no_deplete == 1));
5058 if ( (no_deplete != 0) && (no_deplete != 1) ) {
5064 Ships[ship_index].cargo1 |= CARGO_NO_DEPLETE;
5066 Ships[ship_index].cargo1 &= (~CARGO_NO_DEPLETE);
5071 // sexpression to end the mission after N seconds!
5072 void sexp_end_mission_delay( int n )
5076 //delay = atoi(CTEXT(n));
5077 //mission_parse_set_end_time( delay );
5078 mprintf(("Not ending mission -- end-mission sexpression no longer works!\n"));
5081 // funciton to toggle the status bit for the AI code which tells the AI if it is a good time
5082 // to rearm. The status being set means good time. Status not being set (unset), means bad time.
5083 // designers must implement this.
5084 void sexp_good_time_to_rearm( int n )
5089 team_name = CTEXT(n);
5090 time = atoi(CTEXT(CDR(n))); // this is the time for how long a good rearm is active -- in seconds
5091 for ( i = 0; i < Num_team_names; i++ ) {
5092 if ( !stricmp(team_name, Team_names[i]) ) {
5096 ai_set_rearm_status( team, time );
5101 // function which grants promotion to the player
5102 void sexp_grant_promotion()
5104 // short circuit multiplayer for now until we figure out what to do.
5105 if ( Game_mode & GM_MULTIPLAYER )
5108 // set a bit to tell player should get promoted at the end of the mission. I suppose the other
5109 // thing that we could do would be to set the players score to at least the amount of
5110 // points for the next level, but this way is better I think.
5111 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5112 Player->flags |= PLAYER_FLAGS_PROMOTED;
5116 // function which gives the named medal to the players in the mission
5117 void sexp_grant_medal( int n )
5122 // don't give medals in normal gameplay when not in campaign mode
5123 if ( (Game_mode & GM_NORMAL) && !(Game_mode & GM_CAMPAIGN_MODE) )
5126 Assert(Player->stats.m_medal_earned < 0); // Mission has problems. Tried to grant 2 medals in 1 mission.
5127 medal_name = CTEXT(n);
5128 for (i = 0; i < NUM_MEDALS; i++ ) {
5129 if ( !stricmp(medal_name, Medals[i].name) )
5133 if ( i < NUM_MEDALS ) {
5134 Player->stats.m_medal_earned = i;
5135 if ( Game_mode & GM_MULTIPLAYER ) {
5136 for ( j = 0; j < MAX_PLAYERS; j++ ) {
5137 if ( MULTI_CONNECTED(Net_players[j]) ) {
5138 Net_players[j].player->stats.m_medal_earned = i;
5145 void sexp_tech_add_ship(int node)
5151 // this function doesn't mean anything when not in campaign mode
5152 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5157 i = ship_info_lookup(name);
5159 Ship_info[i].flags |= SIF_IN_TECH_DATABASE;
5161 Error(LOCATION, "Ship class \"%s\" invalid", name);
5167 void sexp_tech_add_weapon(int node)
5173 // this function doesn't mean anything when not in campaign mode
5174 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5179 i = weapon_info_lookup(name);
5181 Weapon_info[i].wi_flags |= WIF_IN_TECH_DATABASE;
5183 Error(LOCATION, "Ship class \"%s\" invalid", name);
5189 // function to set variables needed to grant a new ship/weapon to the player during the course
5191 void sexp_allow_ship( int n )
5196 // this function doesn't mean anything when not in campaign mode
5197 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5200 // get the name of the ship and lookup up the ship_info index for it
5202 sindex = ship_info_lookup( name );
5206 // now we have a valid index --
5207 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_SHIP, sindex );
5210 void sexp_allow_weapon( int n )
5215 // this function doesn't mean anything when not in campaign mode
5216 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5219 // get the name of the weapon and lookup up the weapon_info index for it
5221 sindex = weapon_info_lookup( name );
5225 // now we have a valid index --
5226 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_WEAPON, sindex );
5229 // functions to deal with breaking/fixing the warp engines on ships/wings. should_break is true when
5230 // we are breaking the warp drive (can be repaired). The parameter is 0 when is getting broken (i.e.
5231 // can be fixed by repair). The repair parameter tells us whether we are clearing the destroyed or broken
5232 // flag (1), or setting them (0).
5233 void sexp_deal_with_warp( int n, int should_break, int nix )
5238 for ( ; n != -1; n = CDR(n) ) {
5240 index = ship_name_lookup(name);
5242 // check to see if ship destroyed or departed. In either case, do nothing.
5243 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
5246 // we can only operate on ships which are in the mission
5247 if ( index != -1 ) {
5249 // set the flag value accoring to whether we are destroying the warp or just breaking it
5251 flag = SF_WARP_BROKEN;
5253 flag = SF_WARP_NEVER;
5256 Ships[index].flags |= flag;
5258 Ships[index].flags &= ~flag;
5261 // maybe this ship has yet to arrive. Get a possible parse object and set the flag if found
5264 pobjp = mission_parse_get_arrival_ship( name );
5266 if ( pobjp == NULL ) {
5267 Int3(); // warning, find allender -- should be impossible
5272 flag = P_SF_WARP_BROKEN;
5274 flag = P_SF_WARP_BROKEN;
5277 pobjp->flags |= flag;
5279 pobjp->flags &= ~flag;
5285 // function which is used to tell the AI when it is okay to fire certain secondary
5286 // weapons at other ships.
5287 void sexp_good_secondary_time( int n )
5289 char *team_name, *weapon_name, *ship_name;
5290 int num_weapons, weapon_index, team, i;
5292 team_name = CTEXT(n);
5293 num_weapons = atoi(CTEXT(CDR(n)));
5294 weapon_name = CTEXT(CDR(CDR(n)));
5295 ship_name = CTEXT(CDR(CDR(CDR(n))));
5297 weapon_index = weapon_info_lookup(weapon_name);
5298 if ( weapon_index == -1 ) {
5299 nprintf(("Warning", "couldn't find weapon %s for good-secondary-time\n", weapon_name));
5303 // get the team type from the team_name
5304 for ( i = 0; i < Num_team_names; i++ ) {
5305 if ( !stricmp(Team_names[i], team_name) )
5308 if ( i == Num_team_names ) {
5309 nprintf(("Warning", "couldn't find team %s for good-secondary-time\n", team_name ));
5312 team = (1<<i); // this is the magic formula to get to a team type.
5314 // see if the ship has departed or has been destroyed. If so, then we don't need to set up the
5316 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5319 ai_good_secondary_time( team, weapon_index, num_weapons, ship_name );
5323 // function to deal with getting status of goals for previous missions (in the current campaign).
5324 // the status parameter is used to tell this function if we are looking for a goal_satisfied, goal_failed,
5325 // or goal incomplete event
5326 int sexp_previous_goal_status( int n, int status )
5328 char rval = 0, *mission_name;
5330 int i, mission_num, default_value = 0, use_defaults = 1;
5332 mission_name = CTEXT(n);
5333 goal_name = CTEXT(CDR(n));
5335 // check for possible next optional argument
5338 default_value = eval_sexp(n);
5341 // try to find the given mission name in the current list of missions in the campaign.
5342 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5343 i = mission_campaign_find_mission( mission_name );
5346 // if mission not found, assume that goal was false (so previous-goal-false returns true)
5347 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"));
5348 if ( status == GOAL_COMPLETE )
5349 rval = SEXP_KNOWN_FALSE;
5351 rval = SEXP_KNOWN_TRUE;
5354 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5357 // now try and find the goal this mission
5359 for (i = 0; i < Campaign.missions[mission_num].num_goals; i++) {
5360 if ( !stricmp(Campaign.missions[mission_num].goals[i].name, goal_name) )
5364 if ( i == Campaign.missions[mission_num].num_goals ) {
5365 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");
5366 if ( status == GOAL_COMPLETE )
5367 rval = SEXP_KNOWN_FALSE;
5369 rval = SEXP_KNOWN_TRUE;
5372 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5373 if ( Campaign.missions[mission_num].goals[i].status == status )
5374 rval = SEXP_KNOWN_TRUE;
5376 rval = SEXP_KNOWN_FALSE;
5384 // when not in campaign mode, always return KNOWN_TRUE when looking for goal complete, and KNOWN_FALSE
5387 if ( default_value )
5388 rval = SEXP_KNOWN_TRUE;
5390 rval = SEXP_KNOWN_FALSE;
5392 if ( status == GOAL_COMPLETE )
5393 rval = SEXP_KNOWN_TRUE;
5395 rval = SEXP_KNOWN_FALSE;
5402 // sexpression which gets the status of an event from a previous mission. Like the above function but
5403 // dealing with events instead of goals. Again, the status parameter tells the code if we are looking
5404 // for an event_true, event_false, or event_incomplete status
5405 int sexp_previous_event_status( int n, int status )
5407 char rval = 0, *mission_name;
5409 int i, mission_num, default_value = 0, use_defaults = 1;
5411 mission_name = CTEXT(n);
5412 name = CTEXT(CDR(n));
5414 // check for possible optional parameter
5417 default_value = eval_sexp(n);
5420 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5421 // following function returns -1 when mission isn't found.
5422 i = mission_campaign_find_mission( mission_name );
5424 // if the mission name wasn't found -- make this return FALSE for the event status.
5426 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"));
5427 if ( status == EVENT_SATISFIED ) {
5428 rval = SEXP_KNOWN_FALSE;
5430 rval = SEXP_KNOWN_TRUE;
5434 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5437 // now try and find the goal this mission
5439 for (i = 0; i < Campaign.missions[mission_num].num_events; i++) {
5440 if ( !stricmp(Campaign.missions[mission_num].events[i].name, name) )
5444 if ( i == Campaign.missions[mission_num].num_events ) {
5445 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");
5446 if ( status == EVENT_SATISFIED )
5447 rval = SEXP_KNOWN_FALSE;
5449 rval = SEXP_KNOWN_TRUE;
5452 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5453 if ( Campaign.missions[mission_num].events[i].status == status )
5454 rval = SEXP_KNOWN_TRUE;
5456 rval = SEXP_KNOWN_FALSE;
5465 if ( default_value )
5466 rval = SEXP_KNOWN_TRUE;
5468 rval = SEXP_KNOWN_FALSE;
5470 if ( status == EVENT_SATISFIED )
5471 rval = SEXP_KNOWN_TRUE;
5473 rval = SEXP_KNOWN_FALSE;
5480 // function to return the status of an event in the current mission. The passed parameter indicates
5481 // if we are checking whether the event is true or the event is false.
5482 int sexp_event_status( int n, int want_true )
5488 for (i = 0; i < Num_mission_events; i++ ) {
5489 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5490 if ( !stricmp(Mission_events[i].name, name) ) {
5491 result = Mission_events[i].result;
5492 if (Mission_events[i].formula < 0) {
5493 if ( (want_true && result) || (!want_true && !result) )
5494 return SEXP_KNOWN_TRUE;
5496 return SEXP_KNOWN_FALSE;
5499 if ( (want_true && result) || (!want_true && !result) )
5510 // function to return the status of an event N seconds after the event is true or false. Similar
5511 // to above function but waits N seconds before returning true
5512 int sexp_event_delay_status( int n, int want_true )
5519 delay = i2f(num_eval(CDR(n)));
5520 for (i = 0; i < Num_mission_events; i++ ) {
5521 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5522 if ( !stricmp(Mission_events[i].name, name) ) {
5523 if ( (fix) Mission_events[i].timestamp + delay >= Missiontime )
5526 result = Mission_events[i].result;
5527 if (Mission_events[i].formula < 0) {
5528 if ( (want_true && result) || (!want_true && !result) )
5529 return SEXP_KNOWN_TRUE;
5531 return SEXP_KNOWN_FALSE;
5534 if ( want_true && result ) //) || (!want_true && !result) )
5545 // function which returns true if the given event is still incomplete
5546 int sexp_event_incomplete( int n )
5553 for (i = 0; i < Num_mission_events; i++ ) {
5554 if ( !stricmp(Mission_events[i].name, name ) ) {
5555 // if the formula is still >= 0 (meaning it is still getting eval'ed), then
5556 // the event is incomplete
5557 if ( Mission_events[i].formula != -1 )
5560 return SEXP_KNOWN_FALSE;
5567 // function to return the status of an goal N seconds after the goal is true or false. Similar
5568 // to above function but operates on goals instead of events
5569 int sexp_goal_delay_status( int n, int want_true )
5575 delay = i2f(num_eval(CDR(n)));
5578 // if we are looking for a goal true entry and we find a false, then return known false here
5579 if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, NULL) )
5580 return SEXP_KNOWN_FALSE;
5581 else if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, &time) ) {
5582 if ( (Missiontime - time) >= delay )
5583 return SEXP_KNOWN_TRUE;
5586 // if we are looking for a goal false entry and we find a true, then return known false here
5587 if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, NULL) )
5588 return SEXP_KNOWN_FALSE;
5589 else if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, &time) ) {
5590 if ( (Missiontime - time) >= delay )
5591 return SEXP_KNOWN_TRUE;
5598 // function which returns true if the given goal is still incomplete
5599 int sexp_goal_incomplete( int n )
5605 if ( mission_log_get_time( LOG_GOAL_SATISFIED, name, NULL, NULL) || mission_log_get_time( LOG_GOAL_FAILED, name, NULL, NULL) )
5606 return SEXP_KNOWN_FALSE;
5612 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5613 // or cleared (flag==0)
5614 void sexp_protect_ships( int n, int flag )
5620 ship_name = CTEXT(n);
5622 // check to see if ship destroyed or departed. In either case, do nothing.
5623 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5626 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5627 // in a list until created
5628 num = ship_name_lookup(ship_name);
5631 Objects[Ships[num].objnum].flags |= OF_PROTECTED;
5633 Objects[Ships[num].objnum].flags &= ~OF_PROTECTED;
5635 p_object *parse_obj;
5637 parse_obj = mission_parse_get_arrival_ship( ship_name );
5640 parse_obj->flags |= P_OF_PROTECTED;
5642 parse_obj->flags &= ~P_OF_PROTECTED;
5646 Int3(); // get allender -- could be a potential problem here
5655 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5656 // or cleared (flag==0)
5657 void sexp_beam_protect_ships( int n, int flag )
5663 ship_name = CTEXT(n);
5665 // check to see if ship destroyed or departed. In either case, do nothing.
5666 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5669 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5670 // in a list until created
5671 num = ship_name_lookup(ship_name);
5674 Objects[Ships[num].objnum].flags |= OF_BEAM_PROTECTED;
5676 Objects[Ships[num].objnum].flags &= ~OF_BEAM_PROTECTED;
5678 p_object *parse_obj;
5680 parse_obj = mission_parse_get_arrival_ship( ship_name );
5683 parse_obj->flags |= P_OF_BEAM_PROTECTED;
5685 parse_obj->flags &= ~P_OF_BEAM_PROTECTED;
5689 Int3(); // get allender -- could be a potential problem here
5698 // sexpression to make ships "visible" and "invisible" to sensors. The visible parameter is true
5699 // when making ships visible, false otherwise
5700 void sexp_ships_visible( int n, int visible )
5705 for ( ; n != -1; n = CDR(n) ) {
5706 ship_name = CTEXT(n);
5708 // check to see if ship destroyed or departed. In either case, do nothing.
5709 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5712 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5713 // in a list until created
5714 num = ship_name_lookup(ship_name);
5717 Ships[num].flags |= SF_HIDDEN_FROM_SENSORS;
5719 Ships[num].flags &= ~SF_HIDDEN_FROM_SENSORS;
5720 if (Ships[num].flags & SF_ESCORT) {
5721 // SEND add escort request
5722 hud_add_ship_to_escort(Ships[num].objnum, 1);
5727 p_object *parse_obj;
5729 parse_obj = mission_parse_get_arrival_ship( ship_name );
5732 parse_obj->flags |= P_SF_HIDDEN_FROM_SENSORS;
5734 parse_obj->flags &= ~P_SF_HIDDEN_FROM_SENSORS;
5738 Int3(); // get allender -- could be a potential problem here
5745 // sexpression to toggle invulnerability flag of ships.
5746 void sexp_ships_invulnerable( int n, int invulnerable )
5752 for ( ; n != -1; n = CDR(n) ) {
5753 ship_name = CTEXT(n);
5755 // check to see if ship destroyed or departed. In either case, do nothing.
5756 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5759 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5760 // in a list until created
5761 num = ship_name_lookup(ship_name);
5763 objp = &Objects[Ships[num].objnum];
5765 objp->flags |= OF_INVULNERABLE;
5767 objp->flags &= ~OF_INVULNERABLE;
5769 p_object *parse_obj;
5771 parse_obj = mission_parse_get_arrival_ship( ship_name );
5774 parse_obj->flags |= P_SF_INVULNERABLE;
5776 parse_obj->flags &= ~P_SF_INVULNERABLE;
5780 Int3(); // get allender -- could be a potential problem here
5787 // sexpression to toggle KEEP ALIVE flag of ship object
5788 void sexp_ships_guardian( int n, int guardian )
5794 for ( ; n != -1; n = CDR(n) ) {
5795 ship_name = CTEXT(n);
5797 // check to see if ship destroyed or departed. In either case, do nothing.
5798 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5801 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5802 // in a list until created
5803 num = ship_name_lookup(ship_name);
5805 objp = &Objects[Ships[num].objnum];
5807 objp->flags |= OF_GUARDIAN;
5809 objp->flags &= ~OF_GUARDIAN;
5811 p_object *parse_obj;
5813 parse_obj = mission_parse_get_arrival_ship( ship_name );
5816 parse_obj->flags |= P_SF_GUARDIAN;
5818 parse_obj->flags &= ~P_SF_GUARDIAN;
5822 Int3(); // get allender -- could be a potential problem here
5829 // make ship vanish without a trace (and what its docked to)
5830 void ship_vanished(int);
5831 void sexp_ship_vanish( int n )
5834 object *objp, *docked_objp;
5837 // if MULTIPLAYER bail
5838 if (Game_mode & GM_MULTIPLAYER) {
5842 for ( ; n != -1; n = CDR(n) ) {
5843 ship_name = CTEXT(n);
5845 // check to see if ship destroyed or departed. In either case, do nothing.
5846 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5849 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5850 // in a list until created
5851 num = ship_name_lookup(ship_name);
5853 objp = &Objects[Ships[num].objnum];
5856 docked_objp = ai_find_docked_object( objp );
5859 objp->flags |= OF_SHOULD_BE_DEAD;
5866 docked_objp->flags |= OF_SHOULD_BE_DEAD;
5869 ship_vanished(docked_objp->instance);
5875 int sexp_key_pressed(int node)
5880 z = translate_key_to_index(CTEXT(node));
5885 if (!Control_config[z].used){
5893 t = atoi(CTEXT(CDR(node)));
5894 return timestamp_has_time_elapsed(Control_config[z].used, t * 1000);
5897 void sexp_key_reset(int node)
5902 z = translate_key_to_index(CTEXT(node));
5904 Control_config[z].used = 0;
5907 int sexp_targeted(int node)
5912 z = ship_query_state(CTEXT(node));
5914 return SEXP_KNOWN_FALSE; // ship isn't around, nor will it ever be
5915 } else if (z == -1) {
5916 return SEXP_CANT_EVAL;
5919 z = ship_name_lookup(CTEXT(node), 1);
5920 if ((z < 0) || !Player_ai || (Ships[z].objnum != Player_ai->target_objnum)){
5924 if (CDR(node) >= 0) {
5925 z = atoi(CTEXT(CDR(node))) * 1000;
5926 if (!timestamp_has_time_elapsed(Players_target_timestamp, z)){
5930 if (CDR(CDR(node)) >= 0) {
5931 ptr = Player_ai->targeted_subsys;
5932 if (!ptr || stricmp(ptr->system_info->subobj_name, CTEXT(CDR(CDR(node))))){
5941 int sexp_speed(int node)
5943 if (Training_context & TRAINING_CONTEXT_SPEED) {
5944 if (Training_context_speed_set){
5945 if (timestamp_has_time_elapsed(Training_context_speed_timestamp, atoi(CTEXT(node)) * 1000)){
5946 return SEXP_KNOWN_TRUE;
5954 int sexp_secondaries_depleted(int node)
5956 int sindex, num_banks, num_depleted_banks;
5960 sindex = ship_name_lookup(CTEXT(node));
5965 shipp = &Ships[sindex];
5966 if (shipp->objnum < 0) {
5970 // get num secondary banks
5971 num_banks = shipp->weapons.num_secondary_banks;
5972 num_depleted_banks = 0;
5974 // get number of depleted banks
5975 for (int idx=0; idx<num_banks; idx++) {
5976 if (shipp->weapons.secondary_bank_ammo[idx] == 0) {
5977 num_depleted_banks++;
5981 // are they all depleted?
5982 return (num_depleted_banks == num_banks);
5985 int sexp_facing(int node)
5991 if (ship_query_state(CTEXT(node)) < 0){
5992 return SEXP_KNOWN_FALSE;
5995 sh = ship_name_lookup(CTEXT(node));
5996 if ((sh < 0) || !Player_obj){
6000 obj = Ships[sh].objnum;
6001 v1 = Player_obj->orient.fvec;
6002 vm_vec_normalize(&v1);
6003 vm_vec_sub(&v2, &Objects[obj].pos, &Player_obj->pos);
6004 vm_vec_normalize(&v2);
6005 a1 = vm_vec_dotprod(&v1, &v2);
6006 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6014 // is ship facing first waypoint in waypoint path
6015 int sexp_facing2(int node)
6021 // bail if Player_obj is not good
6023 return SEXP_CANT_EVAL;
6027 v1 = Player_obj->orient.fvec;
6028 vm_vec_normalize(&v1);
6030 // get waypoint name
6031 char *waypoint_name = CTEXT(node);
6033 // get position of first waypoint
6035 for (i=0; i<Num_waypoint_lists; i++) {
6036 if (!stricmp(waypoint_name, Waypoint_lists[i].name)) {
6042 if (wp_index == -1) {
6043 return SEXP_CANT_EVAL;
6046 // Waypoint_lists[wp_index].waypoints[0]
6048 vm_vec_sub(&v2, &Waypoint_lists[wp_index].waypoints[0], &Player_obj->pos);
6049 vm_vec_normalize(&v2);
6050 a1 = vm_vec_dotprod(&v1, &v2);
6051 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6059 int sexp_order(int node)
6064 int sexp_waypoint_missed()
6066 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6067 if (Training_context_at_waypoint > Training_context_goal_waypoint){
6075 int sexp_waypoint_twice()
6077 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6078 if (Training_context_at_waypoint < Training_context_goal_waypoint - 1){
6086 int sexp_path_flown()
6088 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6089 if (Training_context_goal_waypoint == Waypoint_lists[Training_context_path].count){
6097 void sexp_send_training_message(int node)
6099 int t = -1, delay = 0;
6106 Assert(Event_index >= 0);
6108 if ((CDR(node) >= 0) && (CDR(CDR(node)) >= 0)) {
6109 delay = atoi(CTEXT(CDR(CDR(node)))) * 1000;
6110 t = CDR(CDR(CDR(node)));
6116 if ((Mission_events[Event_index].repeat_count > 1) || (CDR(node) < 0)){
6117 message_training_que(CTEXT(node), timestamp(delay), t);
6119 message_training_que(CTEXT(CDR(node)), timestamp(delay), t);
6122 // if (Training_msg_method)
6123 // gameseq_post_event(GS_EVENT_TRAINING_PAUSE);
6126 int sexp_shield_recharge_pct(int node)
6130 // get the firing ship
6131 sindex = ship_name_lookup(CTEXT(node));
6135 if(Ships[sindex].objnum < 0){
6139 // shield recharge pct
6140 return (int)(100.0f * Energy_levels[Ships[sindex].shield_recharge_index]);
6143 int sexp_engine_recharge_pct(int node)
6147 // get the firing ship
6148 sindex = ship_name_lookup(CTEXT(node));
6152 if(Ships[sindex].objnum < 0){
6156 // shield recharge pct
6157 return (int)(100.0f * Energy_levels[Ships[sindex].engine_recharge_index]);
6160 int sexp_weapon_recharge_pct(int node)
6164 // get the firing ship
6165 sindex = ship_name_lookup(CTEXT(node));
6169 if(Ships[sindex].objnum < 0){
6173 // shield recharge pct
6174 return (int)(100.0f * Energy_levels[Ships[sindex].weapon_recharge_index]);
6177 int sexp_shield_quad_low(int node)
6180 float max_quad, check;
6185 sindex = ship_name_lookup(CTEXT(node));
6189 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6192 if((Ships[sindex].ship_info_index < 0) || (Ships[sindex].ship_info_index >= Num_ship_types)){
6195 objp = &Objects[Ships[sindex].objnum];
6196 sip = &Ship_info[Ships[sindex].ship_info_index];
6197 if(!(sip->flags & SIF_SMALL_SHIP)){
6200 max_quad = sip->shields / (float)MAX_SHIELD_SECTIONS;
6203 check = (float)atoi(CTEXT(CDR(node)));
6205 // check his quadrants
6206 for(idx=0; idx<MAX_SHIELD_SECTIONS; idx++){
6207 if( ((objp->shields[idx] / max_quad) * 100.0f) <= check ){
6216 int sexp_secondary_ammo_pct(int node)
6225 sindex = ship_name_lookup(CTEXT(node));
6229 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6232 shipp = &Ships[sindex];
6235 check = atoi(CTEXT(CDR(node)));
6237 // bogus check? (3 == cumulative sum of all banks)
6238 if((check != 3) && (check > shipp->weapons.num_secondary_banks)){
6244 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6245 ret_sum[idx] = (int)(((float)shipp->weapons.secondary_bank_ammo[idx] / (float)shipp->weapons.secondary_bank_start_ammo[idx]) * 100.0f);
6250 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6251 ret += ret_sum[idx];
6253 ret = (int)((float)ret / (float)shipp->weapons.num_secondary_banks);
6255 ret = (int)(((float)shipp->weapons.secondary_bank_ammo[check] / (float)shipp->weapons.secondary_bank_start_ammo[check]) * 100.0f);
6262 void sexp_beam_fire(int node)
6265 beam_fire_info fire_info;
6269 memset(&fire_info, 0, sizeof(beam_fire_info));
6270 fire_info.accuracy = 0.000001f; // this will guarantee a hit
6272 // get the firing ship
6273 sindex = ship_name_lookup(CTEXT(node));
6277 if(Ships[sindex].objnum < 0){
6280 fire_info.shooter = &Objects[Ships[sindex].objnum];
6282 // get the subsystem
6283 fire_info.turret = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6284 if(fire_info.turret == NULL){
6289 sindex = ship_name_lookup(CTEXT(CDR(CDR(node))));
6293 if(Ships[sindex].objnum < 0){
6296 fire_info.target = &Objects[Ships[sindex].objnum];
6298 // see if the optional subsystem can be found
6299 fire_info.target_subsys = NULL;
6300 fire_info.target_subsys = ship_get_subsys(&Ships[sindex], CTEXT(CDR(CDR(CDR(node)))));
6302 // if it has no primary weapons
6303 if(fire_info.turret->weapons.num_primary_banks <= 0){
6308 // if the turret is destroyed
6309 if(fire_info.turret->current_hits <= 0.0f){
6313 // hmm, this could be wacky. Let's just simply select the first beam weapon in the turret
6314 fire_info.beam_info_index = -1;
6315 for(idx=0; idx<fire_info.turret->weapons.num_primary_banks; idx++){
6316 // store the weapon info index
6317 if(Weapon_info[fire_info.turret->weapons.primary_bank_weapons[idx]].wi_flags & WIF_BEAM){
6318 fire_info.beam_info_index = fire_info.turret->weapons.primary_bank_weapons[idx];
6323 if(fire_info.beam_info_index != -1){
6324 beam_fire(&fire_info);
6326 // it would appear the turret doesn't have any beam weapons, dumbass
6331 void sexp_beam_free(int node)
6334 ship_subsys *turret = NULL;
6336 // get the firing ship
6337 sindex = ship_name_lookup(CTEXT(node));
6341 if(Ships[sindex].objnum < 0){
6347 // get the subsystem
6348 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6353 // flag it as beam free :)
6354 turret->weapons.flags |= SW_FLAG_BEAM_FREE;
6355 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6362 void sexp_beam_free_all(int node)
6364 ship_subsys *subsys;
6367 // get the firing ship
6368 sindex = ship_name_lookup(CTEXT(node));
6372 if(Ships[sindex].objnum < 0){
6376 // free all beam weapons
6377 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6378 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6379 // just mark all turrets as beam free
6380 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6381 subsys->weapons.flags |= SW_FLAG_BEAM_FREE;
6382 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6386 subsys = GET_NEXT(subsys);
6390 void sexp_beam_lock(int node)
6393 ship_subsys *turret = NULL;
6395 // get the firing ship
6396 sindex = ship_name_lookup(CTEXT(node));
6400 if(Ships[sindex].objnum < 0){
6406 // get the subsystem
6407 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6412 // flag it as not beam free
6413 turret->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6420 void sexp_beam_lock_all(int node)
6422 ship_subsys *subsys;
6425 // get the firing ship
6426 sindex = ship_name_lookup(CTEXT(node));
6430 if(Ships[sindex].objnum < 0){
6434 // free all beam weapons
6435 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6436 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6437 // just mark all turrets as not beam free
6438 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6439 subsys->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6443 subsys = GET_NEXT(subsys);
6447 void sexp_turret_free(int node)
6450 ship_subsys *turret = NULL;
6452 // get the firing ship
6453 sindex = ship_name_lookup(CTEXT(node));
6457 if(Ships[sindex].objnum < 0){
6463 // get the subsystem
6464 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6469 // flag turret as no longer locked :)
6470 turret->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6471 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6478 void sexp_turret_free_all(int node)
6480 ship_subsys *subsys;
6483 // get the firing ship
6484 sindex = ship_name_lookup(CTEXT(node));
6488 if(Ships[sindex].objnum < 0){
6493 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6494 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6495 // just mark all turrets as free
6496 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6497 subsys->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6498 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6502 subsys = GET_NEXT(subsys);
6506 void sexp_turret_lock(int node)
6509 ship_subsys *turret = NULL;
6511 // get the firing ship
6512 sindex = ship_name_lookup(CTEXT(node));
6516 if(Ships[sindex].objnum < 0){
6522 // get the subsystem
6523 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6528 // flag turret as locked
6529 turret->weapons.flags |= SW_FLAG_TURRET_LOCK;
6536 void sexp_turret_lock_all(int node)
6538 ship_subsys *subsys;
6541 // get the firing ship
6542 sindex = ship_name_lookup(CTEXT(node));
6546 if(Ships[sindex].objnum < 0){
6551 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6552 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6553 // just mark all turrets as locked
6554 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6555 subsys->weapons.flags |= SW_FLAG_TURRET_LOCK;
6559 subsys = GET_NEXT(subsys);
6563 void sexp_turret_tagged_only_all(int node)
6565 ship_subsys *subsys;
6568 // get the firing ship
6569 sindex = ship_name_lookup(CTEXT(node));
6573 if(Ships[sindex].objnum < 0){
6577 // mark all turrets to only target tagged ships
6578 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6579 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6580 // just mark all turrets as locked
6581 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6582 subsys->weapons.flags |= SW_FLAG_TAGGED_ONLY;
6586 subsys = GET_NEXT(subsys);
6590 void sexp_turret_tagged_clear_all(int node)
6592 ship_subsys *subsys;
6595 // get the firing ship
6596 sindex = ship_name_lookup(CTEXT(node));
6600 if(Ships[sindex].objnum < 0){
6604 // mark all turrets so not restricted to only tagged ships
6605 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6606 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6607 // just mark all turrets as locked
6608 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6609 subsys->weapons.flags &= (~SW_FLAG_TAGGED_ONLY);
6613 subsys = GET_NEXT(subsys);
6617 void sexp_add_remove_escort(int node)
6623 // get the firing ship
6624 sindex = ship_name_lookup(CTEXT(node));
6628 if(Ships[sindex].objnum < 0){
6632 // determine whether to add or remove it
6633 whee = CTEXT(CDR(node));
6634 flag = atoi(CTEXT(CDR(node)));
6638 hud_add_ship_to_escort(Ships[sindex].objnum, 1);
6640 hud_remove_ship_from_escort(Ships[sindex].objnum);
6644 void sexp_awacs_set_radius(int node)
6649 // get the firing ship
6650 sindex = ship_name_lookup(CTEXT(node));
6654 if(Ships[sindex].objnum < 0){
6658 // get the awacs subsystem
6659 awacs = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6664 // make sure this _is_ an awacs subsystem
6665 Assert(awacs->system_info->flags & MSS_FLAG_AWACS);
6666 if(awacs->system_info->flags & MSS_FLAG_AWACS){
6670 // set the new awacs radius
6671 awacs->awacs_radius = (float)atoi(CTEXT(CDR(CDR(node))));
6674 int sexp_is_tagged(int node)
6678 // get the firing ship
6679 sindex = ship_name_lookup(CTEXT(node));
6683 if(Ships[sindex].objnum < 0){
6686 if(Ships[sindex].tag_left > 0.0f){
6694 int sexp_num_kills(int node)
6696 int sindex, np_index;
6699 // get the ship we're interested in
6700 sindex = ship_name_lookup(CTEXT(node));
6704 if(Ships[sindex].objnum < 0){
6708 // in multiplayer, search through all players
6709 if(Game_mode & GM_MULTIPLAYER){
6710 // try and find the player
6711 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6712 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6713 p = Net_players[np_index].player;
6716 // if we're in single player, we're only concerned with ourself
6719 if(Player_obj == &Objects[Ships[sindex].objnum]){
6724 // now, if we have a valid player, return his kills
6726 return p->stats.m_kill_count_ok;
6733 int sexp_num_type_kills(int node)
6735 int sindex, np_index, st_index;
6739 // get the ship we're interested in
6740 sindex = ship_name_lookup(CTEXT(node));
6744 if(Ships[sindex].objnum < 0){
6748 // in multiplayer, search through all players
6749 if(Game_mode & GM_MULTIPLAYER){
6750 // try and find the player
6751 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6752 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6753 p = Net_players[np_index].player;
6756 // if we're in single player, we're only concerned with ourself
6759 if(Player_obj == &Objects[Ships[sindex].objnum]){
6769 // lookup ship type name
6770 st_index = ship_type_name_lookup(CTEXT(CDR(node)));
6777 for(idx=0; idx<Num_ship_types; idx++){
6778 if((p->stats.m_okKills[idx] > 0) && (Ship_info[idx].flags & Ship_type_flags[st_index])){
6779 total += p->stats.m_okKills[idx];
6787 int sexp_num_class_kills(int node)
6789 int sindex, np_index, si_index;
6792 // get the ship we're interested in
6793 sindex = ship_name_lookup(CTEXT(node));
6797 if(Ships[sindex].objnum < 0){
6801 // in multiplayer, search through all players
6802 if(Game_mode & GM_MULTIPLAYER){
6803 // try and find the player
6804 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6805 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6806 p = Net_players[np_index].player;
6809 // if we're in single player, we're only concerned with ourself
6812 if(Player_obj == &Objects[Ships[sindex].objnum]){
6822 // get the ship type we're looking for
6823 si_index = ship_info_lookup(CTEXT(CDR(node)));
6824 if((si_index < 0) || (si_index > Num_ship_types)){
6829 return p->stats.m_okKills[si_index];
6832 void sexp_subsys_set_random(int node)
6834 int sindex, low, high, n, idx, rand, exclusion_list[MAX_MODEL_SUBSYSTEMS];
6835 ship_subsys *subsys;
6839 sindex = ship_name_lookup(CTEXT(node));
6843 if(Ships[sindex].objnum < 0){
6846 shipp = &Ships[sindex];
6849 low = num_eval(CDR(node));
6855 high = num_eval(CDR(CDR(node)));
6865 n = CDR(CDR(CDR(node)));
6867 // init exclusion list
6868 memset(exclusion_list, 0, sizeof(int) * Ship_info[shipp->ship_info_index].n_subsystems);
6870 // get exclusion list
6872 int exclude_index = ship_get_subsys_index(shipp, CTEXT(n), 0);
6873 if (exclude_index >= 0) {
6874 exclusion_list[exclude_index] = 1;
6880 // apply to all others
6881 for (idx=0; idx<Ship_info[shipp->ship_info_index].n_subsystems; idx++) {
6882 if ( exclusion_list[idx] == 0 ) {
6883 // get non excluded subsystem
6884 subsys = ship_get_indexed_subsys(shipp, idx, NULL);
6886 // randomize its hit points
6887 rand = rand_internal(low, high);
6888 subsys->current_hits = 0.01f * rand * subsys->system_info->max_hits;
6893 void sexp_supernova_start(int node)
6895 supernova_start(atoi(CTEXT(node)));
6898 int sexp_is_secondary_selected(int node)
6905 sindex = ship_name_lookup(CTEXT(node));
6909 if(Ships[sindex].objnum < 0){
6912 shipp = &Ships[sindex];
6915 bank = atoi(CTEXT(CDR(node)));
6916 if(bank >= shipp->weapons.num_secondary_banks){
6920 // is this the bank currently selected
6921 if(bank == shipp->weapons.current_secondary_bank){
6929 int sexp_is_primary_selected(int node)
6936 sindex = ship_name_lookup(CTEXT(node));
6940 if(Ships[sindex].objnum < 0){
6943 shipp = &Ships[sindex];
6946 bank = atoi(CTEXT(CDR(node)));
6947 if(bank >= shipp->weapons.num_primary_banks){
6951 // is this the bank currently selected
6952 if(bank == shipp->weapons.current_primary_bank){
6960 #define RIGHT_QUAD 0
6961 #define FRONT_QUAD 1
6965 // Return SEXP_TRUE if quadrant quadnum is near max.
6966 int shield_quad_near_max(int quadnum)
6968 float remaining = 0.0f;
6969 for (int i=0; i<MAX_SHIELD_SECTIONS; i++) {
6973 remaining += Player_obj->shields[i];
6976 if ((remaining < 2.0f) || (Player_obj->shields[quadnum] > Ship_info[Player_ship->ship_info_index].shields/MAX_SHIELD_SECTIONS - 5.0f)) {
6983 // Return truth value for special SEXP.
6984 // Used in training#5, perhaps in other missions.
6985 int process_special_sexps(int index)
6988 case 0: // Ship "Freighter 1" is aspect locked by player.
6989 if (Player_ai->target_objnum != -1) {
6990 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
6991 if (Player_ai->current_target_is_locked)
6997 case 1: // Fired Interceptors
6999 for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
7000 if (objp->type == OBJ_WEAPON) {
7001 if (!stricmp(Weapon_info[Weapons[objp->instance].weapon_info_index].name, "Interceptor#weak")) {
7002 int target = Weapons[objp->instance].target_num;
7004 if (Objects[target].type == OBJ_SHIP) {
7005 if (!(stricmp(Ships[Objects[target].instance].ship_name, "Freighter 1")))
7013 case 2: // Ship "Freighter 1", subsystem "Weapons" is aspect locked by player.
7014 if (Player_ai->target_objnum != -1) {
7015 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
7016 if (!(stricmp(Player_ai->targeted_subsys->system_info->name, "Weapons"))) {
7017 if (Player_ai->current_target_is_locked){
7025 case 3: // Player ship suffering shield damage on front.
7026 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7027 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7030 case 4: // Player ship suffering much damage.
7031 nprintf(("AI", "Frame %i\n", Framecount));
7032 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7033 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7034 if (Player_obj->shields[FRONT_QUAD] < 2.0f)
7039 case 5: // Player's shield is quick repaired
7040 nprintf(("AI", "Frame %i, recharged to %7.3f\n", Framecount, Player_obj->shields[FRONT_QUAD]));
7042 apply_damage_to_shield(Player_obj, FRONT_QUAD, -flFrametime*200.0f);
7044 if (Player_obj->shields[FRONT_QUAD] > Ship_info[Player_ship->ship_info_index].shields/4.0f)
7045 Player_obj->shields[FRONT_QUAD] = Ship_info[Player_ship->ship_info_index].shields/4.0f;
7047 //hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7048 if (Player_obj->shields[FRONT_QUAD] > Player_obj->shields[(FRONT_QUAD+1)%4] - 2.0f)
7053 case 6: // 3 of player's shield quadrants are reduced to 0.
7054 Player_obj->shields[1] = 1.0f;
7055 Player_obj->shields[2] = 1.0f;
7056 Player_obj->shields[3] = 1.0f;
7057 //apply_damage_to_shield(Player_obj, FRONT_QUAD, 1.0f);
7058 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7060 case 7: // Make sure front quadrant has been maximized, or close to it.
7061 if (shield_quad_near_max(FRONT_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7064 case 8: // Make sure rear quadrant has been maximized, or close to it.
7065 if (shield_quad_near_max(REAR_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7068 case 9: // Zero left and right quadrants in preparation for maximizing rear quadrant.
7069 Player_obj->shields[LEFT_QUAD] = 0.0f;
7070 Player_obj->shields[RIGHT_QUAD] = 0.0f;
7071 hud_shield_quadrant_hit(Player_obj, LEFT_QUAD);
7075 case 10: // Return true if player is low on Interceptors.
7076 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 8)
7082 case 11: // Return true if player has plenty of Interceptors.
7083 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] >= 8)
7089 case 12: // Return true if player is low on Interceptors.
7090 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 4)
7096 case 13: // Zero front shield quadrant. Added for Jim Boone on August 26, 1999 by MK.
7097 Player_obj->shields[FRONT_QUAD] = 0.0f;
7098 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7102 case 100: // Return true if player is out of countermeasures.
7103 if (Player_ship->cmeasure_count <= 0)
7109 Int3(); // Unsupported node type.
7115 // custom sexp operator for handling misc training stuff
7116 int sexp_special_training_check(int node)
7120 num = atoi(CTEXT(node));
7121 if (num == SPECIAL_CHECK_TRAINING_FAILURE)
7122 return Training_failure ? SEXP_TRUE : SEXP_FALSE;
7124 // To MK: do whatever you want with this number here.
7125 rtn = process_special_sexps(atoi(CTEXT(node)));
7130 // sexpression to flash a hud gauge. gauge name is text valud of node
7131 void sexp_flash_hud_gauge( int node )
7137 for (i = 0; i < NUM_HUD_GAUGES; i++ ) {
7138 if ( !stricmp(HUD_gauge_text[i], name) ) {
7139 hud_gauge_start_flash(i); // call HUD function to flash gauge
7145 void sexp_set_training_context_fly_path(int node)
7149 for (i=0; i<Num_waypoint_lists; i++)
7150 if (!stricmp(CTEXT(node), Waypoint_lists[i].name))
7153 if (i < Num_waypoint_lists) {
7154 Training_context |= TRAINING_CONTEXT_FLY_PATH;
7155 Training_context_path = i;
7156 Training_context_distance = (float) atof(CTEXT(CDR(node)));
7157 Training_context_goal_waypoint = 0;
7158 Training_context_at_waypoint = -1;
7162 void sexp_set_training_context_speed(int node)
7164 Training_context |= TRAINING_CONTEXT_SPEED;
7165 Training_context_speed_min = atoi(CTEXT(node));
7166 Training_context_speed_max = atoi(CTEXT(CDR(node)));
7167 Training_context_speed_set = 0;
7170 // high-level sexpression evaluator
7171 int eval_sexp(int cur_node)
7173 int node, type, sexp_val = UNINITIALIZED;
7174 if (cur_node == -1) // empty list, i.e. sexp: ( )
7177 Assert(cur_node >= 0); // we have special sexp nodes <= -1!!! MWA
7178 // which should be intercepted before we get here. HOFFOSS
7179 type = SEXP_NODE_TYPE(cur_node);
7180 Assert( (type == SEXP_LIST) || (type == SEXP_ATOM) );
7182 // trap known true and known false sexpressions. We don't trap on SEXP_NAN sexpressions since
7183 // they may yet evaluate to true or false.
7185 if (Sexp_nodes[cur_node].value == SEXP_KNOWN_TRUE)
7187 else if (Sexp_nodes[cur_node].value == SEXP_KNOWN_FALSE)
7190 if (Sexp_nodes[cur_node].first != -1) {
7191 node = CAR(cur_node);
7192 sexp_val = eval_sexp(node);
7193 Sexp_nodes[cur_node].value = Sexp_nodes[node].value; // higher level node gets node value
7199 node = CDR(cur_node); // makes reading the next bit of code a little easier.
7201 op_num = find_operator(CTEXT(cur_node));
7203 // arithmetic operators will always return just their value
7205 sexp_val = add_sexps( node );
7209 sexp_val = sub_sexps( node );
7213 sexp_val = mul_sexps( node );
7217 sexp_val = mod_sexps( node );
7221 sexp_val = div_sexps( node );
7225 sexp_val = rand_sexp( node );
7228 // boolean operators can have one of the special sexp values (known true, known false, unknown)
7230 sexp_val = SEXP_KNOWN_TRUE;
7234 sexp_val = SEXP_KNOWN_FALSE;
7238 sexp_val = sexp_or( node );
7242 sexp_val = sexp_and( node );
7245 case OP_AND_IN_SEQUENCE:
7246 sexp_val = sexp_and_in_sequence( node );
7249 case OP_GREATER_THAN:
7250 sexp_val = sexp_gt( node );
7254 sexp_val = sexp_lt( node );
7258 sexp_val = sexp_equal( node );
7262 sexp_val = sexp_is_iff( node );
7266 sexp_val = sexp_not( node );
7269 case OP_PREVIOUS_GOAL_TRUE:
7270 sexp_val = sexp_previous_goal_status( node, GOAL_COMPLETE );
7273 case OP_PREVIOUS_GOAL_FALSE:
7274 sexp_val = sexp_previous_goal_status( node, GOAL_FAILED );
7277 case OP_PREVIOUS_GOAL_INCOMPLETE:
7278 sexp_val = sexp_previous_goal_status( node, GOAL_INCOMPLETE );
7281 case OP_PREVIOUS_EVENT_TRUE:
7282 sexp_val = sexp_previous_event_status( node, EVENT_SATISFIED );
7285 case OP_PREVIOUS_EVENT_FALSE:
7286 sexp_val = sexp_previous_event_status( node, EVENT_FAILED );
7289 case OP_PREVIOUS_EVENT_INCOMPLETE:
7290 sexp_val = sexp_previous_event_status( node, EVENT_INCOMPLETE );
7294 case OP_EVENT_FALSE:
7295 sexp_val = sexp_event_status( node, (op_num == OP_EVENT_TRUE?1:0) );
7296 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7297 Sexp_useful_number = 0; // indicate sexp isn't current yet
7301 case OP_EVENT_TRUE_DELAY:
7302 case OP_EVENT_FALSE_DELAY:
7303 sexp_val = sexp_event_delay_status( node, (op_num == OP_EVENT_TRUE_DELAY?1:0) );
7304 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7305 Sexp_useful_number = 0; // indicate sexp isn't current yet
7308 case OP_GOAL_TRUE_DELAY:
7309 case OP_GOAL_FALSE_DELAY:
7310 sexp_val = sexp_goal_delay_status( node, (op_num == OP_GOAL_TRUE_DELAY?1:0) );
7313 case OP_EVENT_INCOMPLETE:
7314 sexp_val = sexp_event_incomplete( node );
7315 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7316 Sexp_useful_number = 0; // indicate sexp isn't current yet
7319 case OP_GOAL_INCOMPLETE:
7320 sexp_val = sexp_goal_incomplete( node );
7323 // destroy type sexpressions
7324 case OP_IS_DESTROYED:
7325 sexp_val = sexp_is_destroyed( node, NULL );
7328 case OP_IS_SUBSYSTEM_DESTROYED:
7329 sexp_val = sexp_is_subsystem_destroyed( node );
7333 sexp_val = sexp_has_docked( node );
7336 case OP_HAS_ARRIVED:
7337 sexp_val = sexp_has_arrived( node, NULL );
7340 case OP_HAS_DEPARTED:
7341 sexp_val = sexp_has_departed( node, NULL );
7344 case OP_HAS_UNDOCKED:
7345 sexp_val = sexp_has_undocked( node );
7348 case OP_IS_DISABLED:
7349 sexp_val = sexp_is_disabled( node, NULL );
7352 case OP_IS_DISARMED:
7353 sexp_val = sexp_is_disarmed( node, NULL );
7356 case OP_WAYPOINTS_DONE:
7357 sexp_val = sexp_are_waypoints_done( node );
7360 // objective operators that use a delay
7361 case OP_IS_DESTROYED_DELAY:
7362 sexp_val = sexp_is_destroyed_delay( node );
7365 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
7366 sexp_val = sexp_is_subsystem_destroyed_delay( node );
7369 case OP_HAS_DOCKED_DELAY:
7370 sexp_val = sexp_has_docked_delay( node );
7373 case OP_HAS_ARRIVED_DELAY:
7374 sexp_val = sexp_has_arrived_delay( node );
7377 case OP_HAS_DEPARTED_DELAY:
7378 sexp_val = sexp_has_departed_delay( node );
7381 case OP_HAS_UNDOCKED_DELAY:
7382 sexp_val = sexp_has_undocked_delay( node );
7385 case OP_IS_DISABLED_DELAY:
7386 sexp_val = sexp_is_disabled_delay( node );
7389 case OP_IS_DISARMED_DELAY:
7390 sexp_val = sexp_is_disarmed_delay( node );
7393 case OP_WAYPOINTS_DONE_DELAY:
7394 sexp_val = sexp_are_waypoints_done_delay( node );
7397 case OP_SHIP_TYPE_DESTROYED:
7398 sexp_val = sexp_ship_type_destroyed( node );
7401 // time based sexpressions
7402 case OP_HAS_TIME_ELAPSED:
7403 sexp_val = sexp_has_time_elapsed( node );
7406 case OP_MODIFY_VARIABLE:
7407 sexp_modify_variable( node );
7408 sexp_val = 1; // 1 means only do once.
7411 case OP_TIME_SHIP_DESTROYED:
7412 sexp_val = sexp_time_destroyed( node );
7415 case OP_TIME_WING_DESTROYED:
7416 sexp_val = sexp_time_wing_destroyed( node );
7419 case OP_TIME_SHIP_ARRIVED:
7420 sexp_val = sexp_time_ship_arrived( node );
7423 case OP_TIME_WING_ARRIVED:
7424 sexp_val = sexp_time_wing_arrived( node );
7427 case OP_TIME_SHIP_DEPARTED:
7428 sexp_val = sexp_time_ship_departed( node );
7431 case OP_TIME_WING_DEPARTED:
7432 sexp_val = sexp_time_wing_departed( node );
7435 case OP_MISSION_TIME:
7436 sexp_val = sexp_mission_time();
7439 case OP_TIME_DOCKED:
7440 sexp_val = sexp_time_docked( node );
7443 case OP_TIME_UNDOCKED:
7444 sexp_val = sexp_time_undocked( node );
7447 // info based sexpressions (like shields, hits
7448 case OP_SHIELDS_LEFT:
7449 sexp_val = sexp_shields_left( node );
7453 sexp_val = sexp_hits_left( node );
7456 case OP_SPECIAL_WARP_DISTANCE:
7457 sexp_val = sexp_special_warp_dist( node );
7460 case OP_HITS_LEFT_SUBSYSTEM:
7461 sexp_val = sexp_hits_left_subsystem( node );
7465 sexp_val = sexp_distance( node );
7468 case OP_IS_SHIP_VISIBLE:
7469 sexp_val = sexp_is_ship_visible( node );
7473 sexp_val = sexp_team_score( node );
7476 case OP_LAST_ORDER_TIME:
7477 sexp_val = sexp_last_order_time( node );
7480 case OP_NUM_PLAYERS:
7481 sexp_val = sexp_num_players();
7484 case OP_SKILL_LEVEL_AT_LEAST:
7485 sexp_val = sexp_skill_level_at_least( node );
7488 case OP_IS_CARGO_KNOWN:
7489 case OP_CARGO_KNOWN_DELAY:
7490 sexp_val = sexp_is_cargo_known( node, (op_num==OP_IS_CARGO_KNOWN)?0:1 );
7493 case OP_HAS_BEEN_TAGGED_DELAY:
7494 sexp_val = sexp_has_been_tagged_delay(node);
7497 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
7498 sexp_val = sexp_cap_subsys_cargo_known_delay(node);
7501 case OP_WAS_PROMOTION_GRANTED:
7502 sexp_val = sexp_was_promotion_granted(node);
7505 case OP_WAS_MEDAL_GRANTED:
7506 sexp_val = sexp_was_medal_granted(node);
7509 case OP_PERCENT_SHIPS_DEPARTED:
7510 case OP_PERCENT_SHIPS_DESTROYED:
7511 sexp_val = sexp_percent_ships_depart_destroy(node, op_num);
7514 case OP_DEPART_NODE_DELAY:
7515 sexp_val = sexp_depart_node_delay( node );
7518 case OP_DESTROYED_DEPARTED_DELAY:
7519 sexp_val = sexp_destroyed_departed_delay( node );
7522 // conditional sexpressions
7524 sexp_val = eval_when( node );
7528 sexp_val = eval_cond( node );
7531 // sexpressions with side effects
7533 sexp_change_iff( node );
7537 case OP_ADD_SHIP_GOAL:
7538 sexp_add_ship_goal( node );
7542 case OP_ADD_WING_GOAL:
7543 sexp_add_wing_goal( node );
7548 sexp_add_goal( node );
7552 case OP_CLEAR_SHIP_GOALS:
7553 sexp_clear_ship_goals( node );
7557 case OP_CLEAR_WING_GOALS:
7558 sexp_clear_wing_goals( node );
7562 case OP_CLEAR_GOALS:
7563 sexp_clear_goals( node );
7567 case OP_PROTECT_SHIP:
7568 case OP_UNPROTECT_SHIP:
7569 sexp_protect_ships( node, (op_num==OP_PROTECT_SHIP?1:0) );
7573 case OP_BEAM_PROTECT_SHIP:
7574 case OP_BEAM_UNPROTECT_SHIP:
7575 sexp_beam_protect_ships( node, (op_num==OP_BEAM_PROTECT_SHIP?1:0) );
7579 case OP_SHIP_INVISIBLE:
7580 case OP_SHIP_VISIBLE:
7581 sexp_ships_visible( node, (op_num==OP_SHIP_VISIBLE?1:0) );
7585 case OP_SHIP_VULNERABLE:
7586 case OP_SHIP_INVULNERABLE:
7587 sexp_ships_invulnerable( node, (op_num==OP_SHIP_INVULNERABLE?1:0) );
7591 case OP_SHIP_GUARDIAN:
7592 case OP_SHIP_NO_GUARDIAN:
7593 sexp_ships_guardian( node, (op_num==OP_SHIP_GUARDIAN?1:0) );
7597 case OP_SHIP_VANISH:
7598 sexp_ship_vanish( node );
7602 case OP_SEND_MESSAGE:
7603 sexp_send_message( node );
7607 case OP_SEND_MESSAGE_LIST:
7608 sexp_send_message_list( node );
7612 case OP_SEND_RANDOM_MESSAGE:
7613 sexp_send_random_message( node );
7617 case OP_SELF_DESTRUCT:
7618 sexp_self_destruct( node );
7622 case OP_NEXT_MISSION:
7623 sexp_next_mission( node );
7627 case OP_END_OF_CAMPAIGN:
7628 sexp_end_of_campaign( node );
7632 case OP_END_CAMPAIGN:
7633 sexp_end_campaign( node );
7637 case OP_SABOTAGE_SUBSYSTEM:
7638 sexp_sabotage_subsystem( node );
7642 case OP_REPAIR_SUBSYSTEM:
7643 sexp_repair_subsystem( node );
7647 case OP_SET_SUBSYSTEM_STRNGTH:
7648 sexp_set_subsystem_strength( node );
7652 case OP_INVALIDATE_GOAL:
7653 case OP_VALIDATE_GOAL:
7654 sexp_change_goal_validity( node, (op_num==OP_INVALIDATE_GOAL?0:1) );
7658 case OP_TRANSFER_CARGO:
7659 sexp_transfer_cargo( node );
7663 case OP_EXCHANGE_CARGO:
7664 sexp_exchange_cargo( node );
7669 case OP_JETTISON_CARGO:
7670 sexp_jettison_cargo( node );
7674 case OP_CARGO_NO_DEPLETE:
7675 sexp_cargo_no_deplete( node );
7679 case OP_SET_SPECIAL_WARPOUT_NAME:
7680 sexp_special_warpout_name( node );
7684 case OP_END_MISSION_DELAY:
7685 sexp_end_mission_delay( node );
7689 // sexpressions for setting flag for good/bad time for someone to reasm
7690 case OP_GOOD_REARM_TIME:
7691 sexp_good_time_to_rearm( node );
7695 case OP_GRANT_PROMOTION:
7696 sexp_grant_promotion();
7700 case OP_GRANT_MEDAL:
7701 sexp_grant_medal( node );
7705 case OP_WARP_BROKEN:
7706 case OP_WARP_NOT_BROKEN:
7707 sexp_deal_with_warp( node, 0, op_num==OP_WARP_BROKEN?1:0 );
7712 case OP_WARP_ALLOWED:
7713 sexp_deal_with_warp( node, 1, op_num==OP_WARP_NEVER?1:0 );
7717 case OP_GOOD_SECONDARY_TIME:
7718 sexp_good_secondary_time( node );
7722 // sexpressions to allow shpis/weapons during the course of a mission
7724 sexp_allow_ship( node );
7728 case OP_ALLOW_WEAPON:
7729 sexp_allow_weapon( node );
7733 case OP_TECH_ADD_SHIP:
7734 sexp_tech_add_ship(node);
7738 case OP_TECH_ADD_WEAPON:
7739 sexp_tech_add_weapon(node);
7743 // in the case of a red_alert mission, simply call the red alert function to close
7744 // the current campaign's mission and move forward to the next mission
7746 red_alert_start_mission();
7750 // training operators
7751 case OP_KEY_PRESSED:
7752 sexp_val = sexp_key_pressed(node);
7755 case OP_SPECIAL_CHECK:
7756 sexp_val = sexp_special_training_check(node);
7760 sexp_key_reset(node);
7761 sexp_val = SEXP_KNOWN_TRUE; // only do it first time in repeating events.
7765 sexp_val = sexp_targeted(node);
7769 sexp_val = sexp_speed(node);
7772 case OP_SECONDARIES_DEPLETED:
7773 sexp_val = sexp_secondaries_depleted(node);
7777 sexp_val = sexp_facing(node);
7781 sexp_val = sexp_facing2(node);
7785 sexp_val = sexp_order(node);
7788 case OP_WAYPOINT_MISSED:
7789 sexp_val = sexp_waypoint_missed();
7792 case OP_WAYPOINT_TWICE:
7793 sexp_val = sexp_waypoint_twice();
7797 sexp_val = sexp_path_flown();
7800 case OP_TRAINING_MSG:
7801 sexp_send_training_message(node);
7805 case OP_FLASH_HUD_GAUGE:
7806 sexp_flash_hud_gauge(node);
7810 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
7811 sexp_set_training_context_fly_path(node);
7815 case OP_SET_TRAINING_CONTEXT_SPEED:
7816 sexp_set_training_context_speed(node);
7820 // debugging operators
7826 case 0: // zero represents a non-operator
7827 return atoi(CTEXT(cur_node));
7834 sexp_beam_fire(node);
7839 sexp_val = sexp_is_tagged(node);
7843 sexp_val = sexp_num_kills(node);
7846 case OP_NUM_TYPE_KILLS:
7847 sexp_val = sexp_num_type_kills(node);
7850 case OP_NUM_CLASS_KILLS:
7851 sexp_val = sexp_num_class_kills(node);
7856 sexp_beam_free(node);
7859 case OP_BEAM_FREE_ALL:
7861 sexp_beam_free_all(node);
7866 sexp_beam_lock(node);
7869 case OP_BEAM_LOCK_ALL:
7871 sexp_beam_lock_all(node);
7874 case OP_TURRET_FREE:
7876 sexp_turret_free(node);
7879 case OP_TURRET_FREE_ALL:
7881 sexp_turret_free_all(node);
7884 case OP_TURRET_LOCK:
7886 sexp_turret_lock(node);
7889 case OP_TURRET_LOCK_ALL:
7891 sexp_turret_lock_all(node);
7894 case OP_ADD_REMOVE_ESCORT:
7896 sexp_add_remove_escort(node);
7899 case OP_AWACS_SET_RADIUS:
7901 sexp_awacs_set_radius(node);
7904 case OP_CAP_WAYPOINT_SPEED:
7906 sexp_cap_waypont_speed(node);
7909 case OP_TURRET_TAGGED_ONLY_ALL:
7911 sexp_turret_tagged_only_all(node);
7914 case OP_TURRET_TAGGED_CLEAR_ALL:
7916 sexp_turret_tagged_clear_all(node);
7919 case OP_SUBSYS_SET_RANDOM:
7921 sexp_subsys_set_random(node);
7924 case OP_SUPERNOVA_START:
7926 sexp_supernova_start(node);
7929 case OP_SHIELD_RECHARGE_PCT:
7930 sexp_val = sexp_shield_recharge_pct(node);
7933 case OP_ENGINE_RECHARGE_PCT:
7934 sexp_val = sexp_engine_recharge_pct(node);
7937 case OP_WEAPON_RECHARGE_PCT:
7938 sexp_val = sexp_weapon_recharge_pct(node);
7941 case OP_SHIELD_QUAD_LOW:
7942 sexp_val = sexp_shield_quad_low(node);
7945 case OP_SECONDARY_AMMO_PCT:
7946 sexp_val = sexp_secondary_ammo_pct(node);
7949 case OP_IS_SECONDARY_SELECTED:
7950 sexp_val = sexp_is_secondary_selected(node);
7953 case OP_IS_PRIMARY_SELECTED:
7954 sexp_val = sexp_is_primary_selected(node);
7958 Error(LOCATION, "Looking for SEXP operator, found '%s'.\n", CTEXT(cur_node));
7962 Assert(sexp_val != UNINITIALIZED);
7964 // if we haven't returned, check the sexp value of the sexpression evaluation. A special
7965 // value of known true or known false means that we should set the sexp.value field for
7966 // short circuit eval (and return that special value as well).
7967 if (sexp_val == SEXP_KNOWN_TRUE) {
7968 Sexp_nodes[cur_node].value = SEXP_KNOWN_TRUE;
7972 if (sexp_val == SEXP_KNOWN_FALSE) {
7973 Sexp_nodes[cur_node].value = SEXP_KNOWN_FALSE;
7977 if ( sexp_val == SEXP_NAN ) {
7978 Sexp_nodes[cur_node].value = SEXP_NAN; // not a number values are false I would suspect
7982 if ( sexp_val == SEXP_NAN_FOREVER ) {
7983 Sexp_nodes[cur_node].value = SEXP_NAN_FOREVER;
7987 if ( sexp_val == SEXP_CANT_EVAL ) {
7988 Sexp_nodes[cur_node].value = SEXP_CANT_EVAL;
7989 Sexp_useful_number = 0; // indicate sexp isn't current yet
7993 if ( Sexp_nodes[cur_node].value == SEXP_NAN ) { // if we had a nan, but now don't, reset the value
7994 Sexp_nodes[cur_node].value = SEXP_UNKNOWN;
7999 Sexp_nodes[cur_node].value = SEXP_TRUE;
8001 Sexp_nodes[cur_node].value = SEXP_FALSE;
8008 // Still a debug-level system.
8009 // get_sexp_main reads and builds the internal representation for a
8010 // symbolic expression.
8012 // Mp points at first character in expression.
8013 // The symbolic expression is built in Sexp_nodes beginning at node 0.
8017 char token[TOKEN_LENGTH];
8020 ignore_white_space();
8023 if (!strncmp(Mp, "( )", 3))
8028 start_node = get_sexp(token);
8029 // only need to check syntax if we have a operator
8030 if ( /*Sexp_nodes[start_node].subtype != SEXP_ATOM_OPERATOR ||*/ Fred_running || (start_node == -1))
8036 op = identify_operator(CTEXT(start_node));
8038 Error (LOCATION, "Can't find operator %s in operator list\n.", CTEXT(start_node) );
8048 while (*Mp != '#') {
8050 diag_printf("\n----------------\n");
8051 ignore_white_space();
8056 // returns the data type returned by an operator
8057 int query_operator_return_type(int op)
8061 Assert(op >= 0 && op < Num_operators);
8062 op = Operators[op].value;
8070 case OP_AND_IN_SEQUENCE:
8074 case OP_GREATER_THAN:
8076 case OP_IS_DESTROYED:
8077 case OP_IS_SUBSYSTEM_DESTROYED:
8078 case OP_IS_DISABLED:
8079 case OP_IS_DISARMED:
8081 case OP_HAS_UNDOCKED:
8082 case OP_HAS_ARRIVED:
8083 case OP_HAS_DEPARTED:
8084 case OP_IS_DESTROYED_DELAY:
8085 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8086 case OP_IS_DISABLED_DELAY:
8087 case OP_IS_DISARMED_DELAY:
8088 case OP_HAS_DOCKED_DELAY:
8089 case OP_HAS_UNDOCKED_DELAY:
8090 case OP_HAS_ARRIVED_DELAY:
8091 case OP_HAS_DEPARTED_DELAY:
8093 case OP_HAS_TIME_ELAPSED:
8094 case OP_GOAL_INCOMPLETE:
8095 case OP_GOAL_TRUE_DELAY:
8096 case OP_GOAL_FALSE_DELAY:
8097 case OP_EVENT_INCOMPLETE:
8098 case OP_EVENT_TRUE_DELAY:
8099 case OP_EVENT_FALSE_DELAY:
8100 case OP_PREVIOUS_EVENT_TRUE:
8101 case OP_PREVIOUS_EVENT_FALSE:
8102 case OP_PREVIOUS_EVENT_INCOMPLETE:
8103 case OP_PREVIOUS_GOAL_TRUE:
8104 case OP_PREVIOUS_GOAL_FALSE:
8105 case OP_PREVIOUS_GOAL_INCOMPLETE:
8106 case OP_WAYPOINTS_DONE:
8107 case OP_WAYPOINTS_DONE_DELAY:
8108 case OP_SHIP_TYPE_DESTROYED:
8109 case OP_LAST_ORDER_TIME:
8110 case OP_KEY_PRESSED:
8116 case OP_WAYPOINT_MISSED:
8117 case OP_WAYPOINT_TWICE:
8120 case OP_EVENT_FALSE:
8121 case OP_SKILL_LEVEL_AT_LEAST:
8122 case OP_IS_CARGO_KNOWN:
8123 case OP_HAS_BEEN_TAGGED_DELAY:
8124 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8125 case OP_CARGO_KNOWN_DELAY:
8126 case OP_WAS_PROMOTION_GRANTED:
8127 case OP_WAS_MEDAL_GRANTED:
8128 case OP_PERCENT_SHIPS_DEPARTED:
8129 case OP_PERCENT_SHIPS_DESTROYED:
8130 case OP_DEPART_NODE_DELAY:
8131 case OP_DESTROYED_DEPARTED_DELAY:
8132 case OP_SPECIAL_CHECK:
8134 case OP_SECONDARIES_DEPLETED:
8135 case OP_SHIELD_QUAD_LOW:
8136 case OP_IS_SECONDARY_SELECTED:
8137 case OP_IS_PRIMARY_SELECTED:
8146 case OP_TIME_SHIP_DESTROYED:
8147 case OP_TIME_SHIP_ARRIVED:
8148 case OP_TIME_SHIP_DEPARTED:
8149 case OP_TIME_WING_DESTROYED:
8150 case OP_TIME_WING_ARRIVED:
8151 case OP_TIME_WING_DEPARTED:
8152 case OP_MISSION_TIME:
8153 case OP_TIME_DOCKED:
8154 case OP_TIME_UNDOCKED:
8155 case OP_SHIELDS_LEFT:
8157 case OP_HITS_LEFT_SUBSYSTEM:
8159 case OP_NUM_PLAYERS:
8161 case OP_NUM_TYPE_KILLS:
8162 case OP_NUM_CLASS_KILLS:
8163 case OP_SHIELD_RECHARGE_PCT:
8164 case OP_ENGINE_RECHARGE_PCT:
8165 case OP_WEAPON_RECHARGE_PCT:
8166 case OP_SECONDARY_AMMO_PCT:
8167 case OP_SPECIAL_WARP_DISTANCE:
8168 case OP_IS_SHIP_VISIBLE:
8170 return OPR_POSITIVE;
8175 case OP_CLEAR_SHIP_GOALS:
8176 case OP_CLEAR_WING_GOALS:
8177 case OP_CLEAR_GOALS:
8178 case OP_ADD_SHIP_GOAL:
8179 case OP_ADD_WING_GOAL:
8181 case OP_PROTECT_SHIP:
8182 case OP_UNPROTECT_SHIP:
8183 case OP_BEAM_PROTECT_SHIP:
8184 case OP_BEAM_UNPROTECT_SHIP:
8188 case OP_SEND_MESSAGE:
8189 case OP_SELF_DESTRUCT:
8190 case OP_NEXT_MISSION:
8191 case OP_END_CAMPAIGN:
8192 case OP_END_OF_CAMPAIGN:
8193 case OP_SABOTAGE_SUBSYSTEM:
8194 case OP_REPAIR_SUBSYSTEM:
8195 case OP_INVALIDATE_GOAL:
8196 case OP_VALIDATE_GOAL:
8197 case OP_SEND_RANDOM_MESSAGE:
8198 case OP_TRANSFER_CARGO:
8199 case OP_EXCHANGE_CARGO:
8200 case OP_JETTISON_CARGO:
8201 case OP_CARGO_NO_DEPLETE:
8203 case OP_TRAINING_MSG:
8204 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8205 case OP_SET_TRAINING_CONTEXT_SPEED:
8206 case OP_END_MISSION_DELAY:
8207 case OP_SET_SUBSYSTEM_STRNGTH:
8208 case OP_GOOD_REARM_TIME:
8209 case OP_GRANT_PROMOTION:
8210 case OP_GRANT_MEDAL:
8212 case OP_ALLOW_WEAPON:
8213 case OP_TECH_ADD_SHIP:
8214 case OP_TECH_ADD_WEAPON:
8215 case OP_WARP_BROKEN:
8216 case OP_WARP_NOT_BROKEN:
8218 case OP_WARP_ALLOWED:
8219 case OP_FLASH_HUD_GAUGE:
8220 case OP_GOOD_SECONDARY_TIME:
8221 case OP_SHIP_VISIBLE:
8222 case OP_SHIP_INVISIBLE:
8223 case OP_SHIP_VULNERABLE:
8224 case OP_SHIP_INVULNERABLE:
8225 case OP_SHIP_GUARDIAN:
8226 case OP_SHIP_VANISH:
8227 case OP_SHIP_NO_GUARDIAN:
8229 case OP_MODIFY_VARIABLE:
8232 case OP_BEAM_FREE_ALL:
8234 case OP_BEAM_LOCK_ALL:
8235 case OP_TURRET_FREE:
8236 case OP_TURRET_FREE_ALL:
8237 case OP_TURRET_LOCK:
8238 case OP_TURRET_LOCK_ALL:
8239 case OP_ADD_REMOVE_ESCORT:
8240 case OP_AWACS_SET_RADIUS:
8241 case OP_SEND_MESSAGE_LIST:
8242 case OP_CAP_WAYPOINT_SPEED:
8243 case OP_TURRET_TAGGED_ONLY_ALL:
8244 case OP_TURRET_TAGGED_CLEAR_ALL:
8245 case OP_SUBSYS_SET_RANDOM:
8246 case OP_SUPERNOVA_START:
8247 case OP_SET_SPECIAL_WARPOUT_NAME:
8253 case OP_AI_WARP: // this particular operator is obsolete
8254 case OP_AI_WARP_OUT:
8255 case OP_AI_WAYPOINTS:
8256 case OP_AI_WAYPOINTS_ONCE:
8257 case OP_AI_DESTROY_SUBSYS:
8258 case OP_AI_CHASE_WING:
8259 case OP_AI_DISABLE_SHIP:
8260 case OP_AI_DISARM_SHIP:
8262 case OP_AI_GUARD_WING:
8263 case OP_AI_CHASE_ANY:
8264 case OP_AI_EVADE_SHIP:
8265 case OP_AI_STAY_NEAR_SHIP:
8266 case OP_AI_KEEP_SAFE_DISTANCE:
8268 case OP_AI_STAY_STILL:
8269 case OP_AI_PLAY_DEAD:
8279 // returns the data type of a specified argument to an operator. Argnum is 0 indexed.
8280 int query_operator_argument_type(int op, int argnum)
8286 Assert(index >= 0 && index < Num_operators);
8287 op = Operators[index].value;
8290 for (index=0; index<Num_operators; index++)
8291 if (Operators[index].value == op)
8294 Assert(index < Num_operators);
8297 if (argnum >= Operators[index].max)
8303 case OP_MISSION_TIME:
8306 case OP_WAYPOINT_MISSED:
8307 case OP_WAYPOINT_TWICE:
8309 case OP_GRANT_PROMOTION:
8310 case OP_WAS_PROMOTION_GRANTED:
8315 case OP_AND_IN_SEQUENCE:
8327 case OP_GREATER_THAN:
8329 case OP_HAS_TIME_ELAPSED:
8331 case OP_SET_TRAINING_CONTEXT_SPEED:
8332 case OP_END_MISSION_DELAY:
8333 case OP_SPECIAL_CHECK:
8334 case OP_AI_WARP_OUT:
8336 return OPF_POSITIVE;
8338 case OP_AI_WARP: // this operator is obsolete
8339 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8341 return OPF_WAYPOINT_PATH;
8345 case OP_AI_WAYPOINTS:
8346 case OP_AI_WAYPOINTS_ONCE:
8348 return OPF_WAYPOINT_PATH;
8350 return OPF_POSITIVE;
8352 case OP_IS_DISABLED:
8353 case OP_IS_DISARMED:
8354 case OP_TIME_SHIP_DESTROYED:
8355 case OP_TIME_SHIP_ARRIVED:
8356 case OP_TIME_SHIP_DEPARTED:
8357 case OP_SHIELDS_LEFT:
8359 case OP_CLEAR_SHIP_GOALS:
8360 case OP_PROTECT_SHIP:
8361 case OP_UNPROTECT_SHIP:
8362 case OP_BEAM_PROTECT_SHIP:
8363 case OP_BEAM_UNPROTECT_SHIP:
8364 case OP_TRANSFER_CARGO:
8365 case OP_EXCHANGE_CARGO:
8366 case OP_SHIP_INVISIBLE:
8367 case OP_SHIP_VISIBLE:
8368 case OP_SHIP_INVULNERABLE:
8369 case OP_SHIP_VULNERABLE:
8370 case OP_SHIP_GUARDIAN:
8371 case OP_SHIP_VANISH:
8372 case OP_SHIP_NO_GUARDIAN:
8373 case OP_SECONDARIES_DEPLETED:
8374 case OP_SPECIAL_WARP_DISTANCE:
8375 case OP_SET_SPECIAL_WARPOUT_NAME:
8376 case OP_IS_SHIP_VISIBLE:
8379 case OP_IS_DESTROYED:
8380 case OP_HAS_ARRIVED:
8381 case OP_HAS_DEPARTED:
8382 case OP_CLEAR_GOALS:
8383 return OPF_SHIP_WING;
8385 case OP_IS_DISABLED_DELAY:
8386 case OP_IS_DISARMED_DELAY:
8388 return OPF_POSITIVE;
8396 return OPF_POSITIVE;
8400 return OPF_WAYPOINT_PATH;
8402 return OPF_POSITIVE;
8407 return OPF_AI_ORDER;
8409 return OPF_SHIP_WING;
8411 case OP_IS_DESTROYED_DELAY:
8412 case OP_HAS_ARRIVED_DELAY:
8413 case OP_HAS_DEPARTED_DELAY:
8414 case OP_LAST_ORDER_TIME:
8416 return OPF_POSITIVE;
8418 return OPF_SHIP_WING;
8421 return OPF_SHIP_WING_POINT;
8423 case OP_MODIFY_VARIABLE:
8425 return OPF_VARIABLE_NAME;
8427 return OPF_AMBIGUOUS;
8431 case OP_HAS_UNDOCKED:
8432 case OP_HAS_DOCKED_DELAY:
8433 case OP_HAS_UNDOCKED_DELAY:
8434 case OP_TIME_DOCKED:
8435 case OP_TIME_UNDOCKED:
8439 return OPF_POSITIVE;
8441 case OP_TIME_WING_DESTROYED:
8442 case OP_TIME_WING_ARRIVED:
8443 case OP_TIME_WING_DEPARTED:
8444 case OP_CLEAR_WING_GOALS:
8447 case OP_IS_SUBSYSTEM_DESTROYED:
8448 case OP_HITS_LEFT_SUBSYSTEM:
8452 return OPF_SUBSYSTEM;
8457 else if (argnum == 1)
8458 return OPF_POSITIVE;
8460 return OPF_SUBSYSTEM;
8462 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8465 else if ( argnum == 1 )
8466 return OPF_SUBSYSTEM;
8468 return OPF_POSITIVE;
8477 case OP_ADD_SHIP_GOAL:
8483 case OP_ADD_WING_GOAL:
8491 return OPF_SHIP_WING;
8502 case OP_AI_DISABLE_SHIP:
8503 case OP_AI_DISARM_SHIP:
8504 case OP_AI_EVADE_SHIP:
8505 case OP_AI_STAY_NEAR_SHIP:
8510 return OPF_POSITIVE;
8515 return OPF_SHIP_WING;
8517 return OPF_POSITIVE;
8520 case OP_AI_KEEP_SAFE_DISTANCE:
8521 return OPF_POSITIVE;
8526 else if (argnum == 1)
8527 return OPF_DOCKER_POINT;
8528 else if (argnum == 2)
8529 return OPF_DOCKEE_POINT;
8531 return OPF_POSITIVE;
8533 case OP_AI_CHASE_WING:
8534 case OP_AI_GUARD_WING:
8538 return OPF_POSITIVE;
8540 case OP_AI_DESTROY_SUBSYS:
8543 else if (argnum == 1)
8544 return OPF_SUBSYSTEM;
8546 return OPF_POSITIVE;
8551 case OP_SEND_MESSAGE:
8552 case OP_SEND_RANDOM_MESSAGE:
8554 return OPF_WHO_FROM;
8555 else if ( argnum == 1 )
8556 return OPF_PRIORITY;
8560 case OP_SEND_MESSAGE_LIST:
8563 // every four, the value repeats
8568 return OPF_WHO_FROM;
8570 return OPF_PRIORITY;
8574 return OPF_POSITIVE;
8576 case OP_TRAINING_MSG:
8580 return OPF_POSITIVE;
8582 case OP_SELF_DESTRUCT:
8585 case OP_NEXT_MISSION:
8586 return OPF_MISSION_NAME;
8588 case OP_END_CAMPAIGN:
8589 case OP_END_OF_CAMPAIGN:
8592 case OP_PREVIOUS_GOAL_TRUE:
8593 case OP_PREVIOUS_GOAL_FALSE:
8595 return OPF_MISSION_NAME;
8596 else if (argnum == 1 )
8597 return OPF_GOAL_NAME;
8601 case OP_PREVIOUS_GOAL_INCOMPLETE:
8602 return OPF_GOAL_NAME;
8604 case OP_PREVIOUS_EVENT_TRUE:
8605 case OP_PREVIOUS_EVENT_FALSE:
8606 case OP_PREVIOUS_EVENT_INCOMPLETE:
8608 return OPF_MISSION_NAME;
8609 else if ( argnum == 1 )
8610 return OPF_EVENT_NAME;
8614 case OP_SABOTAGE_SUBSYSTEM:
8615 case OP_REPAIR_SUBSYSTEM:
8616 case OP_SET_SUBSYSTEM_STRNGTH:
8618 return OPF_SHIP_NOT_PLAYER;
8619 else if (argnum == 1 )
8620 return OPF_SUBSYSTEM;
8622 return OPF_POSITIVE;
8624 case OP_WAYPOINTS_DONE:
8626 return OPF_SHIP_WING;
8628 return OPF_WAYPOINT_PATH;
8630 case OP_WAYPOINTS_DONE_DELAY:
8632 return OPF_SHIP_WING;
8633 else if ( argnum == 1 )
8634 return OPF_WAYPOINT_PATH;
8636 return OPF_POSITIVE;
8638 case OP_INVALIDATE_GOAL:
8639 case OP_VALIDATE_GOAL:
8640 return OPF_GOAL_NAME;
8642 case OP_SHIP_TYPE_DESTROYED:
8644 return OPF_POSITIVE;
8646 return OPF_SHIP_TYPE;
8648 case OP_KEY_PRESSED:
8650 return OPF_KEYPRESS;
8652 return OPF_POSITIVE;
8655 return OPF_KEYPRESS;
8658 case OP_EVENT_FALSE:
8659 return OPF_EVENT_NAME;
8661 case OP_EVENT_INCOMPLETE:
8662 case OP_EVENT_TRUE_DELAY:
8663 case OP_EVENT_FALSE_DELAY:
8665 return OPF_EVENT_NAME;
8667 return OPF_POSITIVE;
8669 case OP_GOAL_INCOMPLETE:
8670 case OP_GOAL_TRUE_DELAY:
8671 case OP_GOAL_FALSE_DELAY:
8673 return OPF_GOAL_NAME;
8675 return OPF_POSITIVE;
8677 case OP_AI_PLAY_DEAD:
8678 case OP_AI_CHASE_ANY:
8679 return OPF_POSITIVE;
8681 case OP_AI_STAY_STILL:
8683 return OPF_SHIP_POINT;
8685 return OPF_POSITIVE;
8687 case OP_GOOD_REARM_TIME:
8691 return OPF_POSITIVE;
8693 case OP_NUM_PLAYERS:
8694 return OPF_POSITIVE;
8696 case OP_SKILL_LEVEL_AT_LEAST:
8697 return OPF_SKILL_LEVEL;
8699 case OP_GRANT_MEDAL:
8700 case OP_WAS_MEDAL_GRANTED:
8701 return OPF_MEDAL_NAME;
8703 case OP_IS_CARGO_KNOWN:
8706 case OP_CARGO_KNOWN_DELAY:
8708 return OPF_POSITIVE;
8712 case OP_HAS_BEEN_TAGGED_DELAY:
8713 if ( argnum == 0 ) {
8714 return OPF_POSITIVE;
8719 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8720 if ( argnum == 0 ) {
8721 return OPF_POSITIVE;
8722 } else if ( argnum == 1 ) {
8725 return OPF_SUBSYSTEM;
8729 case OP_TECH_ADD_SHIP:
8730 return OPF_SHIP_CLASS_NAME;
8732 case OP_ALLOW_WEAPON:
8733 case OP_TECH_ADD_WEAPON:
8734 return OPF_WEAPON_NAME;
8736 case OP_WARP_BROKEN:
8737 case OP_WARP_NOT_BROKEN:
8739 case OP_WARP_ALLOWED:
8742 case OP_FLASH_HUD_GAUGE:
8743 return OPF_HUD_GAUGE_NAME;
8745 case OP_GOOD_SECONDARY_TIME:
8748 else if ( argnum == 1 )
8749 return OPF_POSITIVE;
8750 else if ( argnum == 2 )
8751 return OPF_HUGE_WEAPON;
8755 case OP_PERCENT_SHIPS_DEPARTED:
8756 case OP_PERCENT_SHIPS_DESTROYED:
8758 return OPF_POSITIVE;
8760 return OPF_SHIP_WING;
8764 case OP_DEPART_NODE_DELAY:
8766 return OPF_POSITIVE;
8767 } else if ( argnum == 1 ){
8768 return OPF_JUMP_NODE_NAME;
8773 case OP_DESTROYED_DEPARTED_DELAY:
8775 return OPF_POSITIVE;
8777 return OPF_SHIP_WING;
8780 case OP_JETTISON_CARGO:
8784 return OPF_POSITIVE;
8787 case OP_CARGO_NO_DEPLETE:
8799 return OPF_SUBSYSTEM;
8803 return OPF_SUBSYSTEM;
8812 case OP_NUM_TYPE_KILLS:
8816 return OPF_SHIP_TYPE;
8819 case OP_NUM_CLASS_KILLS:
8823 return OPF_SHIP_CLASS_NAME;
8828 case OP_TURRET_FREE:
8829 case OP_TURRET_LOCK:
8833 return OPF_SUBSYSTEM;
8836 case OP_BEAM_FREE_ALL:
8837 case OP_BEAM_LOCK_ALL:
8838 case OP_TURRET_FREE_ALL:
8839 case OP_TURRET_LOCK_ALL:
8840 case OP_TURRET_TAGGED_ONLY_ALL:
8841 case OP_TURRET_TAGGED_CLEAR_ALL:
8844 case OP_ADD_REMOVE_ESCORT:
8851 case OP_AWACS_SET_RADIUS:
8854 } else if(argnum == 1){
8855 return OPF_AWACS_SUBSYSTEM;
8860 case OP_CAP_WAYPOINT_SPEED:
8867 case OP_SUBSYS_SET_RANDOM:
8870 } else if (argnum == 1 || argnum == 2) {
8873 return OPF_SUBSYSTEM;
8876 case OP_SUPERNOVA_START:
8879 case OP_SHIELD_RECHARGE_PCT:
8880 case OP_WEAPON_RECHARGE_PCT:
8881 case OP_ENGINE_RECHARGE_PCT:
8884 case OP_SHIELD_QUAD_LOW:
8891 case OP_SECONDARY_AMMO_PCT:
8898 case OP_IS_SECONDARY_SELECTED:
8899 case OP_IS_PRIMARY_SELECTED:
8913 void update_block_names(const char *old_name, const char *new_name)
8917 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
8918 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
8919 if ( !stricmp(old_name, Sexp_variables[i].variable_name) ) {
8920 strcpy(Sexp_variables[i].variable_name, new_name);
8926 // DA: 1/7/99 Used to rename ships and waypoints, not variables
8927 // Strictly used in FRED
8928 void update_sexp_references(char *old_name, char *new_name)
8932 // update_block_names
8933 update_block_names(old_name, new_name);
8935 Assert(strlen(new_name) < TOKEN_LENGTH);
8936 for (i=0; i<MAX_SEXP_NODES; i++){
8937 if ((SEXP_NODE_TYPE(i) == SEXP_ATOM) && (Sexp_nodes[i].subtype == SEXP_ATOM_STRING)){
8938 if (!stricmp(CTEXT(i), old_name)){
8939 strcpy(CTEXT(i), new_name);
8945 // DA: 1/7/99 Used to rename event names, goal names, not variables
8946 // Strictly used in FRED
8947 void update_sexp_references(char *old_name, char *new_name, int format)
8951 Assert(strlen(new_name) < TOKEN_LENGTH);
8952 for (i=0; i<MAX_SEXP_NODES; i++){
8953 if (is_sexp_top_level(i)){
8954 update_sexp_references(old_name, new_name, format, i);
8959 // DA: 1/7/99 Used to rename event names, goal names, not variables
8960 // recursive function to update references to a certain type of data
8961 void update_sexp_references(char *old_name, char *new_name, int format, int node)
8969 if ((SEXP_NODE_TYPE(node) == SEXP_LIST) && (Sexp_nodes[node].subtype == SEXP_ATOM_LIST)) {
8970 if (Sexp_nodes[node].first){
8971 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].first);
8973 if (Sexp_nodes[node].rest){
8974 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].rest);
8980 if (SEXP_NODE_TYPE(node) != SEXP_ATOM){
8984 if (Sexp_nodes[node].subtype != SEXP_ATOM_OPERATOR){
8988 op = identify_operator(CTEXT(node));
8989 Assert(Sexp_nodes[node].first < 0);
8990 n = Sexp_nodes[node].rest;
8993 if (SEXP_NODE_TYPE(n) == SEXP_LIST){
8994 update_sexp_references(old_name, new_name, format, Sexp_nodes[n].first);
8996 Assert((SEXP_NODE_TYPE(n) == SEXP_ATOM) && ((Sexp_nodes[n].subtype == SEXP_ATOM_NUMBER) || (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)));
8997 if (query_operator_argument_type(op, i) == format) {
8998 if (!stricmp(CTEXT(n), old_name)){
8999 strcpy(CTEXT(n), new_name);
9004 n = Sexp_nodes[n].rest;
9009 int query_referenced_in_sexp(int mode, char *name, int *node)
9013 for (n=0; n<MAX_SEXP_NODES; n++){
9014 if ((SEXP_NODE_TYPE(n) == SEXP_ATOM) && (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)){
9015 if (!stricmp(CTEXT(n), name)){
9021 if (n == MAX_SEXP_NODES){
9029 // so we know it's being used somewhere.. Time to find out where..
9030 for (i=0; i<MAX_SHIPS; i++)
9031 if (Ships[i].objnum >= 0) {
9032 if (query_node_in_sexp(n, Ships[i].arrival_cue)){
9033 return i | SRC_SHIP_ARRIVAL;
9035 if (query_node_in_sexp(n, Ships[i].departure_cue)){
9036 return i | SRC_SHIP_DEPARTURE;
9040 for (i=0; i<MAX_WINGS; i++){
9041 if (Wings[i].wave_count) {
9042 if (query_node_in_sexp(n, Wings[i].arrival_cue)){
9043 return i | SRC_WING_ARRIVAL;
9045 if (query_node_in_sexp(n, Wings[i].departure_cue)){
9046 return i | SRC_WING_DEPARTURE;
9051 for (i=0; i<Num_mission_events; i++){
9052 if (query_node_in_sexp(n, Mission_events[i].formula)){
9053 return i | SRC_EVENT;
9057 for (i=0; i<Num_goals; i++){
9058 if (query_node_in_sexp(n, Mission_goals[i].formula)){
9059 return i | SRC_MISSION_GOAL;
9063 for (j=0; j<Num_teams; j++) {
9064 for (i=0; i<Debriefings[j].num_stages; i++) {
9065 if (query_node_in_sexp(n, Debriefings[j].stages[i].formula)){
9066 return i | SRC_DEBRIEFING;
9071 for (j=0; j<Num_teams; j++) {
9072 for (i=0; i<Briefings[j].num_stages; i++) {
9073 if (query_node_in_sexp(n, Briefings[j].stages[i].formula)){
9074 return i | SRC_BRIEFING;
9082 int verify_vector(char *text)
9087 for (i=0; i<Num_waypoint_lists; i++) {
9088 len = strlen(str = Waypoint_lists[i].name);
9089 if (!strnicmp(str, text, len)){
9090 if (!text[len] || text[len] == ':'){
9096 if (i < Num_waypoint_lists) {
9098 return 0; // a valid waypoint path
9101 str = &text[len + 1];
9103 if (!isdigit(*str++)){
9104 return -1; // not a valid number
9108 z = atoi(&text[len + 1]);
9109 if (z < 1 || z > Waypoint_lists[i].count){
9110 return -1; // out of range
9113 return 0; // a valid waypoint
9117 if (text[0] != '(' || text[len - 1] != ')'){
9129 if (validate_float(&str)){
9134 if (validate_float(&str)){
9139 if (validate_float(&str)){
9157 void skip_white(char **str)
9159 if ((**str == ' ') || (**str == '\t')){
9164 int validate_float(char **str)
9166 int count = 0, dot = 0;
9168 while (isdigit(**str) || **str == '.') {
9188 // check if operator return type opr is a valid match for operator argument type opf
9189 int sexp_query_type_match(int opf, int opr)
9193 return ((opr == OPR_NUMBER) || (opr == OPR_POSITIVE));
9196 return (opr == OPR_POSITIVE);
9199 return (opr == OPR_BOOL);
9202 return (opr == OPR_NULL);
9205 return (opr == OPR_AI_GOAL);
9211 char *sexp_error_message(int num)
9214 case SEXP_CHECK_NONOP_ARGS:
9215 return "Data shouldn't have arguments";
9217 case SEXP_CHECK_OP_EXPTECTED:
9218 return "Operator expected instead of data";
9220 case SEXP_CHECK_UNKNOWN_OP:
9221 return "Unrecognized operator";
9223 case SEXP_CHECK_TYPE_MISMATCH:
9224 return "Argument type mismatch";
9226 case SEXP_CHECK_BAD_ARG_COUNT:
9227 return "Argument count is illegal";
9229 case SEXP_CHECK_UNKNOWN_TYPE:
9230 return "Unknown operator argument type";
9232 case SEXP_CHECK_INVALID_NUM:
9233 return "Not a number";
9235 case SEXP_CHECK_INVALID_SHIP:
9236 return "Invalid ship name";
9238 case SEXP_CHECK_INVALID_WING:
9239 return "Invalid wing name";
9241 case SEXP_CHECK_INVALID_SUBSYS:
9242 return "Invalid subsystem name";
9244 case SEXP_CHECK_INVALID_IFF:
9245 return "Invalid team name";
9247 case SEXP_CHECK_INVALID_POINT:
9248 return "Invalid point";
9250 case SEXP_CHECK_NEGATIVE_NUM:
9251 return "Negative number not allowed";
9253 case SEXP_CHECK_INVALID_SHIP_WING:
9254 return "Invalid ship/wing name";
9256 case SEXP_CHECK_INVALID_SHIP_TYPE:
9257 return "Invalid ship type";
9259 case SEXP_CHECK_UNKNOWN_MESSAGE:
9260 return "Invalid message name";
9262 case SEXP_CHECK_INVALID_PRIORITY:
9263 return "Invalid priority";
9265 case SEXP_CHECK_INVALID_MISSION_NAME:
9266 return "Invalid mission filename";
9268 case SEXP_CHECK_INVALID_GOAL_NAME:
9269 return "Invalid goal name";
9271 case SEXP_CHECK_INVALID_LEVEL:
9272 return "Mission level too low in tree";
9274 case SEXP_CHECK_INVALID_MSG_SOURCE:
9275 return "Invalid message source";
9277 case SEXP_CHECK_INVALID_DOCKER_POINT:
9278 return "Invalid docker point";
9280 case SEXP_CHECK_INVALID_DOCKEE_POINT:
9281 return "Invalid dockee point";
9283 case SEXP_CHECK_ORDER_NOT_ALLOWED:
9284 return "Ship not allowed to have this order";
9286 case SEXP_CHECK_DOCKING_NOT_ALLOWED:
9287 return "Ship can't dock with target ship";
9289 case SEXP_CHECK_NUM_RANGE_INVALID:
9290 return "Number is out of range";
9292 case SEXP_CHECK_INVALID_EVENT_NAME:
9293 return "Event name is invalid (not known)";
9295 case SEXP_CHECK_INVALID_SKILL_LEVEL:
9296 return "Skill level name is invalid (not known)";
9298 case SEXP_CHECK_INVALID_MEDAL_NAME:
9299 return "Invalid medal name";
9301 case SEXP_CHECK_INVALID_WEAPON_NAME:
9302 return "Invalid weapon name";
9304 case SEXP_CHECK_INVALID_SHIP_CLASS_NAME:
9305 return "Invalid ship class name";
9307 case SEXP_CHECK_INVALID_GAUGE_NAME:
9308 return "Invalid HUD gauges name";
9310 case SEXP_CHECK_INVALID_JUMP_NODE:
9311 return "Invalid Jump Node name";
9314 sprintf(Sexp_error_text, "Sexp error code %d", num);
9315 return Sexp_error_text;
9318 int query_sexp_ai_goal_valid(int sexp_ai_goal, int ship)
9322 for (op=0; op<Num_operators; op++)
9323 if (Operators[op].value == sexp_ai_goal)
9326 Assert(op < Num_operators);
9327 for (i=0; i<Num_sexp_ai_goal_links; i++)
9328 if (Sexp_ai_goal_links[i].op_code == sexp_ai_goal)
9331 Assert(i < Num_sexp_ai_goal_links);
9332 return ai_query_goal_valid(ship, Sexp_ai_goal_links[i].ai_goal);
9335 // Takes an Sexp_node.text pointing to a variable (of form "Sexp_variables[xx]=string" or "Sexp_variables[xx]=number")
9336 // and returns the index into the Sexp_variables array of the actual value
9337 int extract_sexp_variable_index(int node)
9339 char *text = Sexp_nodes[node].text;
9345 start_index = text + 15;
9346 Assert(isdigit(*start_index));
9350 while ( *start_index != ']' ) {
9351 char_index[len++] = *(start_index++);
9356 char_index[len] = 0; // append null termination to string
9358 variable_index = atoi(char_index);
9359 Assert( (variable_index >= 0) && (variable_index < MAX_SEXP_VARIABLES) );
9361 return variable_index;
9365 // wrapper around Sexp_node[xx].text for normal and variable
9368 if (Sexp_nodes[n].type & SEXP_FLAG_VARIABLE) {
9369 int sexp_variable_index;
9371 sexp_variable_index = get_index_sexp_variable_name(Sexp_nodes[n].text);
9372 Assert(sexp_variable_index != -1);
9374 // sexp_variable_index = extract_sexp_variable_index(n);
9375 sexp_variable_index = atoi(Sexp_nodes[n].text);
9377 // Reference a Sexp_variable
9378 // string format -- "Sexp_variables[xx]=number" or "Sexp_variables[xx]=string", where xx is the index
9380 Assert( !(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NOT_USED) );
9381 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9383 return Sexp_variables[sexp_variable_index].text;
9385 return Sexp_nodes[n].text;
9390 // Set all Sexp_variables to type uninitialized
9391 void init_sexp_vars()
9393 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9394 // if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) )
9395 // Sexp_nodes[i].type = SEXP_NOT_USED;
9396 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9401 // Adds an Sexp_variable to be used in a mission.
9402 // This should be called from within mission parse.
9403 int sexp_add_variable(const char *text, const char *var_name, int type, int index)
9405 // if index == -1, find next open slot
9407 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9408 if (Sexp_variables[i].type == SEXP_VARIABLE_NOT_USED) {
9414 Assert( (index >= 0) && (index < MAX_SEXP_VARIABLES) );
9418 strcpy(Sexp_variables[index].text, text);
9419 strcpy(Sexp_variables[index].variable_name, var_name);
9420 Sexp_variables[index].type &= ~SEXP_VARIABLE_NOT_USED;
9421 Sexp_variables[index].type = (type | SEXP_VARIABLE_SET);
9428 // Modifies and Sexp_variable to be used in a mission
9429 // This should be called in mission when an sexp_variable is to be modified
9430 void sexp_modify_variable(char *text, int index)
9432 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9433 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9434 Assert( !MULTIPLAYER_CLIENT );
9436 strcpy(Sexp_variables[index].text, text);
9437 Sexp_variables[index].type |= SEXP_VARIABLE_MODIFIED;
9439 // do multi_callback_here
9442 void sexp_modify_variable(int n)
9444 int sexp_variable_index;
9446 // char *new_char_var;
9447 char number_as_str[TOKEN_LENGTH];
9449 // Only do single player of multi host
9450 if ( MULTIPLAYER_CLIENT ) {
9456 // get sexp_variable index
9457 Assert(Sexp_nodes[n].first == -1);
9458 sexp_variable_index = atoi(Sexp_nodes[n].text);
9460 // verify variable set
9461 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9463 if (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NUMBER) {
9464 // get new numerical value
9465 new_number = eval_sexp(Sexp_nodes[n].rest);
9467 sprintf(number_as_str, "%d", new_number);
9468 sexp_modify_variable(number_as_str, sexp_variable_index);
9471 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);
9473 char *new_text = Sexp_nodes[Sexp_nodes[n].rest].text;
9474 sexp_modify_variable(new_text, sexp_variable_index);
9481 // Different type needed for Fred (1) allow modification of type (2) no callback required
9482 void sexp_fred_modify_variable(const char *text, const char *var_name, int index, int type)
9484 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9485 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9486 Assert( (type & SEXP_VARIABLE_NUMBER) || (type & SEXP_VARIABLE_STRING) );
9488 strcpy(Sexp_variables[index].text, text);
9489 strcpy(Sexp_variables[index].variable_name, var_name);
9490 Sexp_variables[index].type = (SEXP_VARIABLE_SET | SEXP_VARIABLE_MODIFIED | type);
9493 // return index of sexp_variable_name, -1 if not found
9494 int get_index_sexp_variable_name(const char* temp_name)
9496 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9497 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
9498 // check case sensitive
9499 if ( !strcmp(Sexp_variables[i].variable_name, temp_name) ) {
9510 // counts number of sexp_variables that are set
9511 int sexp_variable_count()
9515 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9516 if ( (Sexp_variables[i].type & SEXP_VARIABLE_SET) && !(Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) ) {
9525 // deletes sexp_variable from active
9526 void sexp_variable_delete(int index)
9528 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9530 Sexp_variables[index].type = SEXP_VARIABLE_NOT_USED;
9533 int sexp_var_compare(const void *var1, const void *var2)
9536 sexp_variable *sexp_var1, *sexp_var2;
9538 sexp_var1 = (sexp_variable*) var1;
9539 sexp_var2 = (sexp_variable*) var2;
9541 set1 = sexp_var1->type & SEXP_VARIABLE_SET;
9542 set2 = sexp_var2->type & SEXP_VARIABLE_SET;
9544 if (!set1 && !set2) {
9546 } else if (set1 && !set2) {
9548 } else if (!set1 && set2) {
9551 return stricmp( sexp_var1->variable_name, sexp_var2->variable_name);
9555 // Count number of variables in block
9556 int sexp_variable_block_count()
9559 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9560 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
9568 // Sort sexp_variable list lexigraphically, with set before unset
9569 void sexp_variable_sort()
9571 qsort( (void *)Sexp_variables, (size_t)(MAX_SEXP_VARIABLES - sexp_variable_block_count()), sizeof(sexp_variable), sexp_var_compare );
9574 int sexp_variable_allocate_block(const char* block_name, int block_type)
9576 int num_blocks, block_count, var_count, start;
9577 block_count = sexp_variable_block_count();
9578 var_count = sexp_variable_count();
9580 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9581 num_blocks = BLOCK_EXP_SIZE;
9583 Int3(); // add new block type here with size
9587 if (block_count + var_count > (MAX_SEXP_VARIABLES - num_blocks)) {
9588 // not enough free space
9592 // squeeze all variables to front of array
9593 sexp_variable_sort();
9595 // squeeze all block to end of array
9596 sexp_variable_condense_block();
9598 start = MAX_SEXP_VARIABLES - block_count - num_blocks;
9600 for (int idx=start; idx<start+num_blocks; idx++) {
9601 Assert(Sexp_variables[idx].type == SEXP_VARIABLE_NOT_USED);
9602 Sexp_variables[idx].type = SEXP_VARIABLE_BLOCK | block_type;
9603 strcpy(Sexp_variables[idx].variable_name, block_name);
9609 // squeeze all blocks to top of array
9610 void sexp_variable_condense_block()
9612 int temp_idx, idx, var_count;
9614 var_count = sexp_variable_count();
9615 temp_idx = MAX_SEXP_VARIABLES-1;
9617 for (idx=MAX_SEXP_VARIABLES-1; idx>var_count-1; idx--) {
9618 if (Sexp_variables[idx].type & SEXP_VARIABLE_BLOCK) {
9619 if (temp_idx > idx) {
9620 Sexp_variables[temp_idx] = Sexp_variables[idx];
9621 Sexp_variables[idx].type = SEXP_VARIABLE_NOT_USED;
9629 void sexp_variable_block_free(const char *ship_name, int start_index, int block_type)
9633 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9634 num_blocks = BLOCK_EXP_SIZE;
9636 Int3(); // new type of block
9640 for (int i=start_index; i<(start_index + num_blocks); i++) {
9641 Assert(!stricmp(Sexp_variables[i].variable_name, ship_name));
9643 Assert(Sexp_variables[i].type & block_type);
9645 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9648 sexp_variable_condense_block();
9651 // evaluate number which may result from an operator or may be text
9652 // only useful for first operand
9653 int num_eval(int node)
9655 if ( CAR(node) != -1 ) {
9656 return eval_sexp(CAR(node));
9658 return atoi(CTEXT(node));