2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/parse/SEXP.CPP $
15 * main sexpression generator
18 * Revision 1.5 2003/05/25 02:30:43 taylor
21 * Revision 1.4 2002/06/17 06:15:25 relnev
22 * ryan's struct patch (and cr removal)
24 * Revision 1.3 2002/06/09 04:41:25 relnev
25 * added copyright header
27 * Revision 1.2 2002/05/07 03:16:48 theoddone33
28 * The Great Newline Fix
30 * Revision 1.1.1.1 2002/05/03 03:28:10 root
34 * 66 9/13/99 11:38a Andsager
35 * Stupid switch for sexp_team_score. Don't forget *break*
37 * 65 9/09/99 11:40p Dave
38 * Handle an Assert() in beam code. Added supernova sounds. Play the right
39 * 2 end movies properly, based upon what the player did in the mission.
41 * 64 9/07/99 1:05a Andsager
42 * Added team-score sexp for multi team vs team missions
44 * 63 9/06/99 9:46p Jefff
45 * skip mission support
47 * 62 8/29/99 2:33p Andsager
48 * Fix bug in sexp_is_ship_visible()
50 * 61 8/27/99 9:07p Dave
51 * LOD explosions. Improved beam weapon accuracy.
53 * 60 8/27/99 4:07p Andsager
54 * Add is-ship-visible sexp. Make ship-vanish sexp SINGLE player only
56 * 59 8/26/99 4:21p Mikek
57 * Add new special-check for JimB, zeroing front shield quadrant.
59 * 58 8/24/99 4:25p Andsager
60 * Add ship-vanish sexp
62 * 57 8/23/99 6:05p Johnson
63 * Oops. Self destruct log should also go into the is-destroyed
66 * 56 8/23/99 3:23p Johnson
67 * Fix bug in sexp_facing2 when Player_obj does not yet existl
69 * 55 8/19/99 3:30p Andsager
70 * Make sexp_special_warp_dist check if ship if facing knossos device
72 * 54 8/19/99 9:20a Andsager
73 * Enable flashing for all guages
75 * 53 8/19/99 12:31a Andsager
76 * Add support guage as one that can flash
78 * 52 8/18/99 12:09p Andsager
79 * Add debug if message has no anim for message. Make messages come from
82 * 51 8/16/99 10:04p Andsager
83 * Add special-warp-dist and special-warpout-name sexp for Knossos device
86 * 50 8/09/99 3:32p Andsager
87 * Make has_time_elapsed work with arithmatic args.
89 * 49 8/09/99 2:00p Dave
92 * 48 8/02/99 4:26p Dave
93 * Added 2 new sexpressions.
95 * 47 7/31/99 2:30p Dave
96 * Added nifty mission message debug viewing keys.
98 * 46 7/28/99 1:36p Andsager
99 * Modify cargo1 to include flag CARGO_NO_DEPLETE. Add sexp
100 * cargo-no-deplete (only for BIG / HUGE). Modify ship struct to pack
103 * 45 7/24/99 4:56p Dave
104 * Added 3 new sexpressions.
106 * 44 7/21/99 8:10p Dave
107 * First run of supernova effect.
109 * 43 7/20/99 9:19p Andsager
110 * Added facing waypoint sexp
112 * 42 7/20/99 9:54a Andsager
113 * Add subsys-set-random sexp
115 * 41 7/19/99 12:02p Andsager
116 * Allow AWACS on any ship subsystem. Fix sexp_set_subsystem_strength to
117 * only blow up subsystem if its strength is > 0
119 * 40 7/15/99 9:22p Andsager
120 * modify secondaries_depleted sexp to report true only when all
123 * 39 7/13/99 3:37p Andsager
124 * Add secondaries-depleted sexp
126 * 38 7/08/99 12:06p Andsager
127 * Add turret-tagged-only and turret-tagged-clear sexp.
129 * 37 6/28/99 4:51p Andsager
130 * Add ship-guardian sexp (does not allow ship to be killed)
132 * 36 6/25/99 2:51p Andsager
133 * Make event_delay, goal_delay, and subsys strength work with sexp_rand
135 * 35 6/23/99 5:51p Andsager
136 * Add waypoint-cap-speed. Checkin stealth ai - inactive.
138 * 34 6/16/99 10:21a Dave
139 * Added send-message-list sexpression.
141 * 33 6/15/99 2:53p Andsager
142 * Fixed subsystem checking for AWACS type subsystems.
144 * 32 6/01/99 8:35p Dave
145 * Finished lockarm weapons. Added proper supercap weapons/damage. Added
146 * awacs-set-radius sexpression.
148 * 31 5/27/99 12:14p Andsager
149 * Some fixes for live debris when more than one subsys on ship with live
150 * debris. Set subsys strength (when 0) blows off subsystem.
151 * sexp_hits_left_subsystem works for SUBSYSTEM_UNKNOWN.
153 * 30 5/24/99 11:28a Dave
154 * Sexpression for adding/removing ships from the hud escort list.
156 * 29 4/28/99 9:33a Andsager
157 * Add turret-free and turret-lock (and -all) sexp. Stargger start time
158 * of beam weapons beam-free and beam-free-all.
160 * 28 4/26/99 2:14p Andsager
161 * Add beam-protect-ship and beam-unprotect-ship sexp.
163 * 27 4/23/99 12:01p Johnson
164 * Added SIF_HUGE_SHIP
166 * 26 4/23/99 9:45a Andsager
167 * Modify rand_sexp to return the same value each time it is called on a
168 * particular node. This prevents random time delay defaulting to min.
170 * 25 4/21/99 6:15p Dave
171 * Did some serious housecleaning in the beam code. Made it ready to go
172 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
173 * a handy macro for recalculating collision pairs for a given object.
175 * 24 4/19/99 2:28p Johnson
176 * DA: Use ship_info flags, not ship_flags for ship type. DOH!!
178 * 23 4/19/99 11:46a Johnson
181 * 22 4/19/99 11:36a Johnson
182 * Don't display multiple warning messges when large ship gets more than 1
185 * 21 4/02/99 9:55a Dave
186 * Added a few more options in the weapons.tbl for beam weapons. Attempt
187 * at putting "pain" packets into multiplayer.
189 * 20 3/31/99 2:22p Johnson
190 * Fix sexp_repair_subsystem() to accept sexp increments
192 * 19 3/28/99 5:58p Dave
193 * Added early demo code. Make objects move. Nice and framerate
194 * independant, but not much else. Don't use yet unless you're me :)
196 * 18 3/20/99 3:46p Dave
197 * Added support for model-based background nebulae. Added 3 new
200 * 17 3/08/99 7:03p Dave
201 * First run of new object update system. Looks very promising.
203 * 16 3/04/99 6:09p Dave
204 * Added in sexpressions for firing beams and checking for if a ship is
207 * 15 3/04/99 9:22a Andsager
208 * Make escort list work with ship-is-visible. When not visible, dump,
209 * when becoming visible, maybe add.
211 * 14 2/26/99 6:01p Andsager
212 * Add sexp has-been-tagged-delay and cap-subsys-cargo-known-delay
214 * 13 2/26/99 4:14p Dave
215 * Put in the ability to have multiple shockwaves for ships.
217 * 12 2/11/99 5:22p Andsager
218 * Fixed bugs, generalized block Sexp_variables
220 * 11 2/11/99 2:15p Andsager
221 * Add ship explosion modification to FRED
223 * 10 1/25/99 4:29p Andsager
224 * Modify sexp_modify_variable() to handle type string. added quick out
225 * for multiplayer_client
227 * 9 1/25/99 8:11a Andsager
228 * Add sexp_modify_variable(). Changed syntax checking to allow, adding
229 * operator return type ambiguous
231 * 8 1/24/99 11:37p Dave
232 * First full rev of beam weapons. Very customizable. Removed some bogus
233 * Int3()'s in low level net code.
235 * 7 1/20/99 9:37a Andsager
237 * 6 1/19/99 3:57p Andsager
238 * Round 2 of variables
240 * 5 1/07/99 1:52p Andsager
241 * Initial check in of Sexp_variables
243 * 4 11/05/98 5:55p Dave
244 * Big pass at reducing #includes
246 * 3 10/13/98 9:29a Dave
247 * Started neatening up freespace.h. Many variables renamed and
248 * reorganized. Added AlphaColors.[h,cpp]
250 * 2 10/07/98 10:53a Dave
253 * 1 10/07/98 10:50a Dave
255 * 321 9/20/98 7:20p Dave
256 * Added CHANGE_IFF packet.
258 * 320 9/16/98 10:42a Hoffoss
259 * Added sexp node counting to fsm files for end user designers.
261 * 319 7/06/98 4:31p Hoffoss
262 * Fixed bug in sexp parsing when formula is ( ).
264 * 318 6/09/98 5:15p Lawrance
265 * French/German localization
267 * 317 5/26/98 11:54a Allender
268 * fix multiplayer problems and sexpression crash
270 * 316 5/23/98 3:16p Allender
271 * work on object update packet optimizations (a new updating system).
272 * Don't allow medals/promotions/badges when playing singple player
273 * missions through the simulator
275 * 315 5/20/98 1:34a Allender
276 * fixed observer problems. Removed some code offending BoundsCheccker.
277 * Fixed medals for multiplayer
279 * 314 5/17/98 12:46p Mike
280 * Change special-check functions for cueing rearm suggestion.
282 * 313 5/14/98 6:29p Hoffoss
283 * Fixed some warnings a release rebuild all turned up.
285 * 312 5/14/98 10:15a Allender
286 * add optional argument to prevous-goal/event operators to specify what
287 * sexpression should return when being played as a single mission
289 * 311 5/08/98 10:16a Lawrance
290 * Add new "ship attacking count" gauge
292 * 310 4/29/98 11:39p Allender
293 * fixed nasty problem with is-event-false always returning true right
299 // Parse a symbolic expression.
300 // These are identical to Lisp functions.
301 // It uses a very baggy format, allocating 16 characters per token, regardless
302 // of how many are used.
314 #include "freespace.h"
317 #include "missionlog.h"
318 #include "missionparse.h" // for p_object definition
319 #include "missionmessage.h"
320 #include "missiontraining.h"
322 #include "linklist.h"
323 #include "model.h" // for subsystem types
325 #include "missioncampaign.h"
326 #include "missiongoals.h"
327 #include "controlsconfig.h"
330 #include "gamesequence.h"
334 #include "hudmessage.h"
336 #include "redalert.h"
337 #include "jumpnode.h"
338 #include "hudshield.h"
340 #include "multimsgs.h"
341 #include "multiutil.h"
342 #include "hudescort.h"
344 #include "supernova.h"
348 #include "multi_team.h"
353 sexp_oper Operators[] = {
354 // Operator, Identity, Min / Max arguments
355 { "+", OP_PLUS, 2, INT_MAX },
356 { "-", OP_MINUS, 2, INT_MAX },
357 { "*", OP_MUL, 2, INT_MAX },
358 { "/", OP_DIV, 2, INT_MAX },
359 { "mod", OP_MOD, 2, INT_MAX },
360 { "rand", OP_RAND, 2, 2 },
362 { "true", OP_TRUE, 0, 0, },
363 { "false", OP_FALSE, 0, 0, },
364 { "and", OP_AND, 2, INT_MAX, },
365 { "and-in-sequence", OP_AND_IN_SEQUENCE, 2, INT_MAX, },
366 { "or", OP_OR, 2, INT_MAX, },
367 { "not", OP_NOT, 1, 1, },
368 { "=", OP_EQUALS, 2, INT_MAX, },
369 { ">", OP_GREATER_THAN, 2, 2, },
370 { "<", OP_LESS_THAN, 2, 2, },
371 { "is-iff", OP_IS_IFF, 2, INT_MAX, },
372 { "has-time-elapsed", OP_HAS_TIME_ELAPSED, 1, 1, },
373 { "modify-variable", OP_MODIFY_VARIABLE, 2, 2, },
375 { "is-goal-incomplete", OP_GOAL_INCOMPLETE, 1, 1, },
376 { "is-goal-true-delay", OP_GOAL_TRUE_DELAY, 2, 2, },
377 { "is-goal-false-delay", OP_GOAL_FALSE_DELAY, 2, 2, },
378 { "is-event-incomplete", OP_EVENT_INCOMPLETE, 1, 1, },
379 { "is-event-true-delay", OP_EVENT_TRUE_DELAY, 2, 2, },
380 { "is-event-false-delay", OP_EVENT_FALSE_DELAY, 2, 2, },
381 { "is-previous-goal-incomplete", OP_PREVIOUS_GOAL_INCOMPLETE, 2, 3, },
382 { "is-previous-goal-true", OP_PREVIOUS_GOAL_TRUE, 2, 3, },
383 { "is-previous-goal-false", OP_PREVIOUS_GOAL_FALSE, 2, 3, },
384 { "is-previous-event-incomplete", OP_PREVIOUS_EVENT_INCOMPLETE, 2, 3, },
385 { "is-previous-event-true", OP_PREVIOUS_EVENT_TRUE, 2, 3, },
386 { "is-previous-event-false", OP_PREVIOUS_EVENT_FALSE, 2, 3, },
388 { "is-destroyed", OP_IS_DESTROYED, 1, INT_MAX, },
389 { "is-subsystem-destroyed", OP_IS_SUBSYSTEM_DESTROYED, 2, 2, },
390 { "is-disabled", OP_IS_DISABLED, 1, INT_MAX, },
391 { "is-disarmed", OP_IS_DISARMED, 1, INT_MAX, },
392 { "has-docked", OP_HAS_DOCKED, 3, 3, },
393 { "has-undocked", OP_HAS_UNDOCKED, 3, 3, },
394 { "has-arrived", OP_HAS_ARRIVED, 1, INT_MAX, },
395 { "has-departed", OP_HAS_DEPARTED, 1, INT_MAX, },
396 { "is-destroyed-delay", OP_IS_DESTROYED_DELAY, 2, INT_MAX, },
397 { "is-subsystem-destroyed-delay", OP_IS_SUBSYSTEM_DESTROYED_DELAY, 3, 3, },
398 { "is-disabled-delay", OP_IS_DISABLED_DELAY, 2, INT_MAX, },
399 { "is-disarmed-delay", OP_IS_DISARMED_DELAY, 2, INT_MAX, },
400 { "has-docked-delay", OP_HAS_DOCKED_DELAY, 4, 4, },
401 { "has-undocked-delay", OP_HAS_UNDOCKED_DELAY, 4, 4, },
402 { "has-arrived-delay", OP_HAS_ARRIVED_DELAY, 2, INT_MAX, },
403 { "has-departed-delay", OP_HAS_DEPARTED_DELAY, 2, INT_MAX, },
404 { "are-waypoints-done", OP_WAYPOINTS_DONE, 2, 2, },
405 { "are-waypoints-done-delay", OP_WAYPOINTS_DONE_DELAY, 3, 3, },
406 { "ship-type-destroyed", OP_SHIP_TYPE_DESTROYED, 2, 2, },
407 { "is-event-true", OP_EVENT_TRUE, 1, 1, },
408 { "is-event-false", OP_EVENT_FALSE, 1, 1, },
409 { "is-cargo-known", OP_IS_CARGO_KNOWN, 1, INT_MAX, },
410 { "was-promotion-granted", OP_WAS_PROMOTION_GRANTED, 0, 1, },
411 { "was-medal-granted", OP_WAS_MEDAL_GRANTED, 0, 1, },
412 { "percent-ships-departed", OP_PERCENT_SHIPS_DEPARTED, 2, INT_MAX },
413 { "percent-ships-destroyed", OP_PERCENT_SHIPS_DESTROYED, 2, INT_MAX },
414 { "is-cargo-known-delay", OP_CARGO_KNOWN_DELAY, 2, INT_MAX, },
415 { "cap-subsys-cargo-known-delay", OP_CAP_SUBSYS_CARGO_KNOWN_DELAY, 3, INT_MAX, },
416 { "has-been-tagged-delay", OP_HAS_BEEN_TAGGED_DELAY, 2, INT_MAX, },
417 { "depart-node-delay", OP_DEPART_NODE_DELAY, 3, INT_MAX, },
418 { "destroyed-or-departed-delay", OP_DESTROYED_DEPARTED_DELAY, 2, INT_MAX, },
419 { "is_tagged", OP_IS_TAGGED, 1, 1 },
420 { "num_kills", OP_NUM_KILLS, 1, 1 },
421 { "num_type_kills", OP_NUM_TYPE_KILLS, 2, 2 },
422 { "num_class_kills", OP_NUM_CLASS_KILLS, 2, 2 },
423 { "shield-recharge-pct", OP_SHIELD_RECHARGE_PCT, 1, 1 },
424 { "engine-recharge-pct", OP_ENGINE_RECHARGE_PCT, 1, 1 },
425 { "weapon-recharge-pct", OP_WEAPON_RECHARGE_PCT, 1, 1 },
426 { "shield-quad-low", OP_SHIELD_QUAD_LOW, 2, 2 },
427 { "secondary-ammo-pct", OP_SECONDARY_AMMO_PCT, 2, 2 },
428 { "is-secondary-selected", OP_IS_SECONDARY_SELECTED, 2, 2 },
429 { "is-primary-selected", OP_IS_PRIMARY_SELECTED, 2, 2 },
431 { "time-ship-destroyed", OP_TIME_SHIP_DESTROYED, 1, 1, },
432 { "time-ship-arrived", OP_TIME_SHIP_ARRIVED, 1, 1, },
433 { "time-ship-departed", OP_TIME_SHIP_DEPARTED, 1, 1, },
434 { "time-wing-destroyed", OP_TIME_WING_DESTROYED, 1, 1, },
435 { "time-wing-arrived", OP_TIME_WING_ARRIVED, 1, 1, },
436 { "time-wing-departed", OP_TIME_WING_DEPARTED, 1, 1, },
437 { "mission-time", OP_MISSION_TIME, 0, 0, },
438 { "time-docked", OP_TIME_DOCKED, 3, 3, },
439 { "time-undocked", OP_TIME_UNDOCKED, 3, 3, },
441 { "shields-left", OP_SHIELDS_LEFT, 1, 1, },
442 { "hits-left", OP_HITS_LEFT, 1, 1, },
443 { "hits-left-subsystem", OP_HITS_LEFT_SUBSYSTEM, 2, 2, },
444 { "distance", OP_DISTANCE, 2, 2, },
445 { "is-ship-visible", OP_IS_SHIP_VISIBLE, 1, 1, },
446 { "team-score", OP_TEAM_SCORE, 1, 1, },
447 { "time-elapsed-last-order", OP_LAST_ORDER_TIME, 2, 2, /*INT_MAX*/ },
448 { "skill-level-at-least", OP_SKILL_LEVEL_AT_LEAST, 1, 1, },
449 { "num-players", OP_NUM_PLAYERS, 0, 0, },
450 { "special-warp-dist", OP_SPECIAL_WARP_DISTANCE, 1, 1, },
452 { "special-warpout-name", OP_SET_SPECIAL_WARPOUT_NAME, 2, 2 },
454 { "do-nothing", OP_NOP, 0, 0, },
455 { "when", OP_WHEN, 2, INT_MAX, },
456 { "cond", OP_COND, 1, INT_MAX, },
458 { "add-goal", OP_ADD_GOAL, 2, 2, },
459 { "add-ship-goal", OP_ADD_SHIP_GOAL, 2, 2, },
460 { "add-wing-goal", OP_ADD_WING_GOAL, 2, 2, },
461 { "clear-goals", OP_CLEAR_GOALS, 1, INT_MAX, },
462 { "clear-ship-goals", OP_CLEAR_SHIP_GOALS, 1, 1, },
463 { "clear-wing-goals", OP_CLEAR_WING_GOALS, 1, 1, },
464 { "change-iff", OP_CHANGE_IFF, 2, INT_MAX, },
465 { "protect-ship", OP_PROTECT_SHIP, 1, INT_MAX, },
466 { "unprotect-ship", OP_UNPROTECT_SHIP, 1, INT_MAX, },
467 { "beam-protect-ship", OP_BEAM_PROTECT_SHIP, 1, INT_MAX, },
468 { "beam-unprotect-ship", OP_BEAM_UNPROTECT_SHIP, 1, INT_MAX, },
469 { "send-message", OP_SEND_MESSAGE, 3, 3, },
470 { "self-destruct", OP_SELF_DESTRUCT, 1, INT_MAX, },
471 { "next-mission", OP_NEXT_MISSION, 1, 1, },
472 { "end-campaign", OP_END_CAMPAIGN, 0, 0, },
473 { "end-of-campaign", OP_END_OF_CAMPAIGN, 0, 0, },
474 { "sabotage-subsystem", OP_SABOTAGE_SUBSYSTEM, 3, 3, },
475 { "repair-subsystem", OP_REPAIR_SUBSYSTEM, 3, 3, },
476 { "set-subsystem-strength", OP_SET_SUBSYSTEM_STRNGTH, 3, 3, },
477 { "invalidate-goal", OP_INVALIDATE_GOAL, 1, INT_MAX, },
478 { "validate-goal", OP_VALIDATE_GOAL, 1, INT_MAX, },
479 { "send-random-message", OP_SEND_RANDOM_MESSAGE, 3, INT_MAX, },
480 { "transfer-cargo", OP_TRANSFER_CARGO, 2, 2, },
481 { "exchange-cargo", OP_EXCHANGE_CARGO, 2, 2, },
482 { "end-misison-delay", OP_END_MISSION_DELAY, 1, 1, },
483 { "good-rearm-time", OP_GOOD_REARM_TIME, 2, 2, },
484 { "grant-promotion", OP_GRANT_PROMOTION, 0, 0, },
485 { "grant-medal", OP_GRANT_MEDAL, 1, 1, },
486 { "allow-ship", OP_ALLOW_SHIP, 1, 1, },
487 { "allow-weapon", OP_ALLOW_WEAPON, 1, 1, },
488 { "break-warp", OP_WARP_BROKEN, 1, INT_MAX, },
489 { "fix-warp", OP_WARP_NOT_BROKEN, 1, INT_MAX, },
490 { "never-warp", OP_WARP_NEVER, 1, INT_MAX, },
491 { "allow-warp", OP_WARP_ALLOWED, 1, INT_MAX, },
492 { "good-secondary-time", OP_GOOD_SECONDARY_TIME, 4, 4, },
493 { "ship-invisible", OP_SHIP_INVISIBLE, 1, INT_MAX },
494 { "ship-visible", OP_SHIP_VISIBLE, 1, INT_MAX },
495 { "ship-invulnerable", OP_SHIP_INVULNERABLE, 1, INT_MAX },
496 { "ship-vulnerable", OP_SHIP_VULNERABLE, 1, INT_MAX },
497 { "red-alert", OP_RED_ALERT, 0, 0 },
498 { "tech-add-ships", OP_TECH_ADD_SHIP, 1, INT_MAX },
499 { "tech-add-weapons", OP_TECH_ADD_WEAPON, 1, INT_MAX },
500 { "jettison-cargo-delay", OP_JETTISON_CARGO, 2, 2 },
501 { "fire-beam", OP_BEAM_FIRE, 3, 4 },
502 { "beam-free", OP_BEAM_FREE, 2, INT_MAX },
503 { "beam-free-all", OP_BEAM_FREE_ALL, 1, 1 },
504 { "beam-lock", OP_BEAM_LOCK, 2, INT_MAX },
505 { "beam-lock-all", OP_BEAM_LOCK_ALL, 1, 1 },
506 { "turret-free", OP_TURRET_FREE, 2, INT_MAX },
507 { "turret-free-all", OP_TURRET_FREE_ALL, 1, 1 },
508 { "turret-lock", OP_TURRET_LOCK, 2, INT_MAX },
509 { "turret-lock-all", OP_TURRET_LOCK_ALL, 1, 1 },
510 { "add-remove-escort", OP_ADD_REMOVE_ESCORT, 2, 2 },
511 { "awacs-set-radius", OP_AWACS_SET_RADIUS, 3, 3 },
512 { "send-message-list", OP_SEND_MESSAGE_LIST, 4, INT_MAX },
513 { "cap-waypoint-speed", OP_CAP_WAYPOINT_SPEED, 2, 2 },
514 { "ship-guardian", OP_SHIP_GUARDIAN, 1, INT_MAX },
515 { "ship-no-guardian", OP_SHIP_NO_GUARDIAN, 1, INT_MAX },
516 { "ship-vanish", OP_SHIP_VANISH, 1, INT_MAX },
517 { "turret-tagged-only", OP_TURRET_TAGGED_ONLY_ALL, 1, 1 },
518 { "turret-tagged-clear", OP_TURRET_TAGGED_CLEAR_ALL, 1, 1 },
519 { "subsys-set-random", OP_SUBSYS_SET_RANDOM, 3, INT_MAX },
520 { "supernova-start", OP_SUPERNOVA_START, 1, 1 },
521 { "cargo-no-deplete", OP_CARGO_NO_DEPLETE, 1, 2 },
523 { "error", OP_INT3, 0, 0 },
525 { "ai-chase", OP_AI_CHASE, 2, 2, },
526 { "ai-chase-wing", OP_AI_CHASE_WING, 2, 2, },
527 { "ai-dock", OP_AI_DOCK, 4, 4, },
528 { "ai-undock", OP_AI_UNDOCK, 1, 1, },
529 { "ai-warp", OP_AI_WARP, 2, 2, },
530 { "ai-warp-out", OP_AI_WARP_OUT, 1, 1, },
531 { "ai-waypoints", OP_AI_WAYPOINTS, 2, 2, },
532 { "ai-waypoints-once", OP_AI_WAYPOINTS_ONCE, 2, 2, },
533 { "ai-destroy-subsystem", OP_AI_DESTROY_SUBSYS, 3, 3, },
534 { "ai-disable-ship", OP_AI_DISABLE_SHIP, 2, 2, },
535 { "ai-disarm-ship", OP_AI_DISARM_SHIP, 2, 2, },
536 { "ai-guard", OP_AI_GUARD, 2, 2, },
537 { "ai-chase-any", OP_AI_CHASE_ANY, 1, 1, },
538 { "ai-guard-wing", OP_AI_GUARD_WING, 2, 2, },
539 { "ai-evade-ship", OP_AI_EVADE_SHIP, 2, 2, },
540 { "ai-stay-near-ship", OP_AI_STAY_NEAR_SHIP, 2, 2, },
541 { "ai-keep-safe-distance", OP_AI_KEEP_SAFE_DISTANCE, 1, 1, },
542 { "ai-ignore", OP_AI_IGNORE, 2, 2, },
543 { "ai-stay-still", OP_AI_STAY_STILL, 2, 2, },
544 { "ai-play-dead", OP_AI_PLAY_DEAD, 1, 1, },
546 { "goals", OP_GOALS_ID, 1, INT_MAX, },
548 { "key-pressed", OP_KEY_PRESSED, 1, 2, },
549 { "key-reset", OP_KEY_RESET, 1, INT_MAX, },
550 { "targeted", OP_TARGETED, 1, 3, },
551 { "speed", OP_SPEED, 1, 1, },
552 { "facing", OP_FACING, 2, 2, },
553 { "facing-waypoint", OP_FACING2, 2, 2, },
554 { "order", OP_ORDER, 2, 2, },
555 { "waypoint-missed", OP_WAYPOINT_MISSED, 0, 0, },
556 { "waypoint-twice", OP_WAYPOINT_TWICE, 0, 0, },
557 { "path-flown", OP_PATH_FLOWN, 0, 0, },
558 { "training-msg", OP_TRAINING_MSG, 1, 4, },
559 { "flash-hud-gauge", OP_FLASH_HUD_GAUGE, 1, 1, },
560 { "special-check", OP_SPECIAL_CHECK, 1, 1, },
561 { "secondaries-depleted", OP_SECONDARIES_DEPLETED, 1, 1, },
563 { "set-training-context-fly-path", OP_SET_TRAINING_CONTEXT_FLY_PATH, 2, 2, },
564 { "set-training-context-speed", OP_SET_TRAINING_CONTEXT_SPEED, 2, 2, },
567 sexp_ai_goal_link Sexp_ai_goal_links[] = {
568 { AI_GOAL_CHASE, OP_AI_CHASE },
569 { AI_GOAL_CHASE_WING, OP_AI_CHASE_WING },
570 { AI_GOAL_DOCK, OP_AI_DOCK },
571 { AI_GOAL_UNDOCK, OP_AI_UNDOCK },
572 { AI_GOAL_WARP, OP_AI_WARP_OUT },
573 { AI_GOAL_WARP, OP_AI_WARP },
574 { AI_GOAL_WAYPOINTS, OP_AI_WAYPOINTS },
575 { AI_GOAL_WAYPOINTS_ONCE, OP_AI_WAYPOINTS_ONCE },
576 { AI_GOAL_DESTROY_SUBSYSTEM, OP_AI_DESTROY_SUBSYS },
577 { AI_GOAL_DISABLE_SHIP, OP_AI_DISABLE_SHIP },
578 { AI_GOAL_DISARM_SHIP, OP_AI_DISARM_SHIP },
579 { AI_GOAL_GUARD, OP_AI_GUARD },
580 { AI_GOAL_CHASE_ANY, OP_AI_CHASE_ANY },
581 { AI_GOAL_GUARD_WING, OP_AI_GUARD_WING },
582 { AI_GOAL_EVADE_SHIP, OP_AI_EVADE_SHIP },
583 { AI_GOAL_STAY_NEAR_SHIP, OP_AI_STAY_NEAR_SHIP },
584 { AI_GOAL_KEEP_SAFE_DISTANCE, OP_AI_KEEP_SAFE_DISTANCE },
585 { AI_GOAL_IGNORE, OP_AI_IGNORE },
586 { AI_GOAL_STAY_STILL, OP_AI_STAY_STILL },
587 { AI_GOAL_PLAY_DEAD, OP_AI_PLAY_DEAD },
590 char *HUD_gauge_text[NUM_HUD_GAUGES] =
602 "TARGET_MONITOR_EXTRA_DATA",
603 "TARGET_SHIELD_ICON",
604 "PLAYER_SHIELD_ICON",
612 "AFTERBURNER_ENERGY",
614 "WEAPON_LINKING_GAUGE",
616 "OFFSCREEN_INDICATOR",
620 "MISSILE_WARNING_ARROW",
622 "OBJECTIVES_NOTIFY_GAUGE",
626 "ATTACKING TARGET COUNT",
635 int Sexp_useful_number; // a variable to pass useful info in from external modules
636 int Locked_sexp_true, Locked_sexp_false;
637 int Num_operators = sizeof(Operators) / sizeof(sexp_oper);
638 int Num_sexp_ai_goal_links = sizeof(Sexp_ai_goal_links) / sizeof(sexp_ai_goal_link);
640 int Sexp_clipboard = -1; // used by Fred
641 int Training_context = 0;
642 int Training_context_speed_set;
643 int Training_context_speed_min;
644 int Training_context_speed_max;
645 int Training_context_speed_timestamp;
646 int Training_context_path;
647 int Training_context_goal_waypoint;
648 int Training_context_at_waypoint;
649 float Training_context_distance;
650 char Sexp_error_text[MAX_SEXP_TEXT];
651 char *Sexp_string; //[1024] = {0};
652 sexp_node Sexp_nodes[MAX_SEXP_NODES];
653 sexp_variable Sexp_variables[MAX_SEXP_VARIABLES];
655 int Players_target = UNINITIALIZED;
656 ship_subsys *Players_targeted_subsys;
657 int Players_target_timestamp;
659 int get_sexp(char *token);
660 int eval_sexp(int cur_node);
661 void build_extended_sexp_string(int cur_node, int level, int mode);
662 void update_sexp_references(char *old_name, char *new_name, int format, int node);
663 int sexp_determine_team(char *subj);
664 int sexp_distance2(int obj1, char *subj);
665 int sexp_distance3(int obj1, int obj2);
666 int extract_sexp_variable_index(int node);
667 void init_sexp_vars();
668 int num_eval(int node);
674 for (i=0; i<MAX_SEXP_NODES; i++) {
675 if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) ){
676 Sexp_nodes[i].type = SEXP_NOT_USED;
682 Locked_sexp_false = Locked_sexp_true = -1;
683 Locked_sexp_false = alloc_sexp("false", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
684 Assert(Locked_sexp_false != -1);
685 Sexp_nodes[Locked_sexp_false].type = SEXP_ATOM; // fix bypassing value
686 Locked_sexp_true = alloc_sexp("true", SEXP_LIST, SEXP_ATOM_OPERATOR, -1, -1);
687 Assert(Locked_sexp_true != -1);
688 Sexp_nodes[Locked_sexp_true].type = SEXP_ATOM; // fix bypassing value
691 // allocates an sexp node.
692 int alloc_sexp(char *text, int type, int subtype, int first, int rest)
696 i = find_operator(text);
697 if ((i == OP_TRUE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
698 return Locked_sexp_true;
699 } else if ((i == OP_FALSE) && (type == SEXP_ATOM) && (subtype == SEXP_ATOM_OPERATOR)) {
700 return Locked_sexp_false;
703 i = find_free_sexp();
704 Assert(i != Locked_sexp_true);
705 Assert(i != Locked_sexp_false);
706 if (i == MAX_SEXP_NODES){
710 Assert(strlen(text) < TOKEN_LENGTH);
711 strcpy(Sexp_nodes[i].text, text);
713 Sexp_nodes[i].type = type;
714 Sexp_nodes[i].subtype = subtype;
715 Sexp_nodes[i].first = first;
716 Sexp_nodes[i].rest = rest;
717 Sexp_nodes[i].value = SEXP_UNKNOWN;
723 int count_free_sexp_nodes()
727 for (i=0; i<MAX_SEXP_NODES; i++) {
728 if (Sexp_nodes[i].type == SEXP_NOT_USED){
730 } else if (Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT){
735 if (MAX_SEXP_NODES - f > Sexp_hwm) {
736 nprintf(("Sexp", "Sexp nodes: Free=%d, Used=%d, Persistent=%d\n", f, MAX_SEXP_NODES - f, p));
737 Sexp_hwm = MAX_SEXP_NODES - f;
743 // find the next free sexp and return it's index.
748 for (i=0; i<MAX_SEXP_NODES; i++){
749 if (Sexp_nodes[i].type == SEXP_NOT_USED){
755 //count_free_sexp_nodes();
758 Assert(i != MAX_SEXP_NODES); // time to raise the limit..
759 if (i == MAX_SEXP_NODES){
766 // sexp_mark_persistent() marks a whole sexp tree with the persistent flag so that it won't
767 // get re-used between missions
768 void sexp_mark_persistent( int n )
774 // total hack because of the true/false locked sexps -- we should make those persistent as well
775 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
779 Assert( !(Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT) );
780 Sexp_nodes[n].type |= SEXP_FLAG_PERSISTENT;
782 sexp_mark_persistent(Sexp_nodes[n].first);
783 sexp_mark_persistent(Sexp_nodes[n].rest);
787 // sexp_unmark_persistent() removes the persistent flag from all nodes in the tree
788 void sexp_unmark_persistent( int n )
794 if ( (n == Locked_sexp_true) || (n == Locked_sexp_false) ){
798 Assert( Sexp_nodes[n].type & SEXP_FLAG_PERSISTENT );
799 Sexp_nodes[n].type &= ~SEXP_FLAG_PERSISTENT;
801 sexp_unmark_persistent(Sexp_nodes[n].first);
802 sexp_unmark_persistent(Sexp_nodes[n].rest);
805 // just frees up the specified sexp node, Leaves link chains untouched.
806 int free_one_sexp(int num)
808 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
809 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
811 if ((num == Locked_sexp_true) || (num == Locked_sexp_false)){
815 Sexp_nodes[num].type = SEXP_NOT_USED;
819 // frees a used sexp node, so it can be reused later. Should only be called on
820 // an atom or a list, and not an operator. If on a list, the list and everything
821 // in it will be freed (including the operator).
822 int free_sexp(int num)
824 int i, rest, count = 0;
826 Assert(Sexp_nodes[num].type != SEXP_NOT_USED); // make sure it is actually used
827 Assert( !(Sexp_nodes[num].type & SEXP_FLAG_PERSISTENT) );
829 if ((num == Locked_sexp_true) || (num == Locked_sexp_false) || (num == -1) ){
833 Sexp_nodes[num].type = SEXP_NOT_USED;
836 i = Sexp_nodes[num].first;
838 count += free_sexp(i);
839 i = Sexp_nodes[i].rest;
842 rest = Sexp_nodes[num].rest;
843 for (i=0; i<MAX_SEXP_NODES; i++) {
844 if (Sexp_nodes[i].first == num){
845 Sexp_nodes[i].first = rest;
848 if (Sexp_nodes[i].rest == num){
849 Sexp_nodes[i].rest = rest;
853 return count; // total elements freed up.
856 // used to free up an entire sexp tree. Because the root node is an operator, instead of
857 // a list, we can't simply call free_sexp(). This function should only be called on the
858 // root node of an sexp, otherwise the linking will get screwed up.
859 int free_sexp2(int num)
863 if ((num == -1) || (num == Locked_sexp_true) || (num == Locked_sexp_false)){
867 i = Sexp_nodes[num].rest;
869 count += free_sexp(i);
870 i = Sexp_nodes[i].rest;
873 count += free_sexp(num);
877 // This function resets the status of all the nodes in a tree, forcing them to all be
879 void flush_sexp_tree(int node)
885 Sexp_nodes[node].value = SEXP_UNKNOWN;
886 flush_sexp_tree(Sexp_nodes[node].first);
887 flush_sexp_tree(Sexp_nodes[node].rest);
890 int verify_sexp_tree(int node)
896 if ((Sexp_nodes[node].type == SEXP_NOT_USED) ||
897 (Sexp_nodes[node].first == node) ||
898 (Sexp_nodes[node].rest == node)) {
899 Error(LOCATION, "Sexp node is corrupt");
903 if (Sexp_nodes[node].first != -1){
904 verify_sexp_tree(Sexp_nodes[node].first);
906 if (Sexp_nodes[node].rest != -1){
907 verify_sexp_tree(Sexp_nodes[node].rest);
913 int dup_sexp_chain(int node)
915 int cur, first, rest;
921 // TODO - CASE OF SEXP VARIABLES - ONLY 1 COPY OF VARIABLE
922 first = dup_sexp_chain(Sexp_nodes[node].first);
923 rest = dup_sexp_chain(Sexp_nodes[node].rest);
924 cur = alloc_sexp(Sexp_nodes[node].text, Sexp_nodes[node].type, Sexp_nodes[node].subtype, first, rest);
938 // returns 1 if they are the same, 0 if different
939 int cmp_sexp_chains(int node1, int node2)
941 if ((node1 == -1) && (node2 == -1)){
945 if ((node1 == -1) || (node2 == -1)){
949 // DA: 1/7/99 Need to check the actual Sexp_node.text, not possible variable, which can be equal
950 if (stricmp(Sexp_nodes[node1].text, Sexp_nodes[node2].text)){
954 if (!cmp_sexp_chains(Sexp_nodes[node1].first, Sexp_nodes[node2].first)){
958 if (!cmp_sexp_chains(Sexp_nodes[node1].rest, Sexp_nodes[node2].rest)){
965 // determine if an sexp node is within the given sexp chain.
966 int query_node_in_sexp(int node, int sexp)
975 if (query_node_in_sexp(node, Sexp_nodes[sexp].first)){
978 if (query_node_in_sexp(node, Sexp_nodes[sexp].rest)){
985 // find the index of the list associated with an operator
986 int find_sexp_list(int num)
990 for (i=0; i<MAX_SEXP_NODES; i++){
991 if (Sexp_nodes[i].first == num){
999 // find index of operator that item is an argument of.
1000 int find_parent_operator(int num)
1004 if (Sexp_nodes[num].subtype == SEXP_ATOM_OPERATOR){
1005 num = find_sexp_list(num);
1008 while (Sexp_nodes[num].subtype != SEXP_ATOM_OPERATOR) {
1009 for (i=0; i<MAX_SEXP_NODES; i++){
1010 if (Sexp_nodes[i].rest == num){
1015 if (i == MAX_SEXP_NODES){
1016 return -1; // not found, probably at top node already.
1025 // function to determine if an sexpression node is the top level node of an sexpression tree. Top
1026 // level nodes do not have their node id in anyone elses first or rest index
1027 int is_sexp_top_level( int node )
1031 if ( Sexp_nodes[node].type == SEXP_NOT_USED ){
1035 for (i = 0; i < MAX_SEXP_NODES; i++ ) {
1036 if ( (Sexp_nodes[i].type == SEXP_NOT_USED) || ( i == node ) ){ // don't check myself or unused nodes
1039 if ( (Sexp_nodes[i].first == node) || (Sexp_nodes[i].rest == node) ){
1047 int identify_operator(char *token)
1051 for (i=0; i<Num_operators; i++){
1052 if (!stricmp(token, Operators[i].text)){
1060 int find_operator(char *token)
1064 for (i=0; i<Num_operators; i++){
1065 if (!stricmp(token, Operators[i].text)){
1066 return Operators[i].value;
1073 int query_sexp_args_count(int index)
1077 while (Sexp_nodes[index].rest != -1){
1079 index = Sexp_nodes[index].rest;
1085 // returns 0 if ok, negative if there's an error in expression..
1086 // See the returns types in sexp.h
1088 int check_sexp_syntax(int index, int return_type, int recursive, int *bad_index, int mode)
1090 int i = 0, z, t, type, argnum = 0, count, op, type2 = 0, op2;
1093 Assert(index >= 0 && index < MAX_SEXP_NODES);
1094 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1095 if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER && return_type == OPR_BOOL) {
1096 // special case Mark seems to want supported
1097 Assert(Sexp_nodes[index].first == -1); // only lists should have a first pointer
1098 if (Sexp_nodes[index].rest != -1) // anything after the number?
1099 return SEXP_CHECK_NONOP_ARGS; // if so, it's a syntax error
1104 op_index = index; // save the index of the operator since we need to get to other args.
1106 *bad_index = op_index;
1108 if (Sexp_nodes[op_index].subtype != SEXP_ATOM_OPERATOR)
1109 return SEXP_CHECK_OP_EXPTECTED; // not an operator, which it should always be
1111 op = identify_operator(CTEXT(op_index));
1113 return SEXP_CHECK_UNKNOWN_OP; // unrecognized operator
1115 //special case - OPR_AMBIGUOUS matches all
1116 if (return_type != OPR_AMBIGUOUS) {
1117 if (query_operator_return_type(op) != return_type) {
1118 return SEXP_CHECK_TYPE_MISMATCH;
1122 count = query_sexp_args_count(op_index);
1123 if (count < Operators[op].min || count > Operators[op].max)
1124 return SEXP_CHECK_BAD_ARG_COUNT; // incorrect number of arguments
1126 index = Sexp_nodes[op_index].rest;
1127 while (index != -1) {
1128 type = query_operator_argument_type(op, argnum);
1129 Assert(Sexp_nodes[index].type != SEXP_NOT_USED);
1133 if (Sexp_nodes[index].subtype == SEXP_ATOM_LIST) {
1134 i = Sexp_nodes[index].first;
1138 // be sure to check to see if this node is a list of stuff and not an actual operator type
1139 // thing. (i.e. in the case of a cond statement, the conditional will fall into this if
1140 // statement. MORE TO DO HERE!!!!
1141 if (Sexp_nodes[i].subtype == SEXP_ATOM_LIST)
1144 op2 = identify_operator(CTEXT(i));
1146 return SEXP_CHECK_UNKNOWN_OP;
1148 type2 = query_operator_return_type(op2);
1171 // special case for modify-variable"
1177 return SEXP_CHECK_UNKNOWN_TYPE; // no other return types available
1180 if ((z = check_sexp_syntax(i, t, recursive, bad_index)) != 0) {
1185 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_NUMBER) {
1188 type2 = OPR_POSITIVE;
1195 if (type == OPF_BOOL) // allow numbers to be used where boolean is required.
1200 return SEXP_CHECK_INVALID_NUM; // not a valid number
1205 i = atoi(CTEXT(index));
1206 z = find_operator(CTEXT(op_index));
1207 if ( (z == OP_HAS_DOCKED_DELAY) || (z == OP_HAS_UNDOCKED_DELAY) )
1208 if ( (argnum == 2) && (i < 1) )
1209 return SEXP_CHECK_NUM_RANGE_INVALID;
1211 z = identify_operator(CTEXT(op_index));
1212 if ( (query_operator_return_type(z) == OPR_AI_GOAL) && (argnum == Operators[op].min - 1) )
1213 if ( (i < 0) || (i > 89) )
1214 return SEXP_CHECK_NUM_RANGE_INVALID;
1216 } else if (Sexp_nodes[index].subtype == SEXP_ATOM_STRING) {
1217 type2 = SEXP_ATOM_STRING;
1225 if ((type2 != OPR_NUMBER) && (type2 != OPR_POSITIVE)){
1226 return SEXP_CHECK_TYPE_MISMATCH;
1232 if (type2 == OPR_NUMBER){
1233 return SEXP_CHECK_NEGATIVE_NUM;
1236 if (type2 != OPR_POSITIVE){
1237 return SEXP_CHECK_TYPE_MISMATCH;
1242 case OPF_SHIP_NOT_PLAYER:
1243 if (type2 != SEXP_ATOM_STRING){
1244 return SEXP_CHECK_TYPE_MISMATCH;
1247 if (ship_name_lookup(CTEXT(index), 0) < 0) {
1248 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))){ // == 0 when still on arrival list
1249 return SEXP_CHECK_INVALID_SHIP;
1256 case OPF_SHIP_POINT:
1257 if (type2 != SEXP_ATOM_STRING){
1258 return SEXP_CHECK_TYPE_MISMATCH;
1261 if (ship_name_lookup(CTEXT(index), 1) < 0) {
1262 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1263 if (type == OPF_SHIP){ // return invalid ship if not also looking for point
1264 return SEXP_CHECK_INVALID_SHIP;
1267 if (waypoint_lookup(CTEXT(index)) < 0){
1268 if (verify_vector(CTEXT(index))){ // verify return non-zero on invalid point
1269 return SEXP_CHECK_INVALID_POINT;
1278 if (type2 != SEXP_ATOM_STRING){
1279 return SEXP_CHECK_TYPE_MISMATCH;
1282 if (wing_name_lookup(CTEXT(index), 1) < 0){
1283 return SEXP_CHECK_INVALID_WING;
1289 case OPF_SHIP_WING_POINT:
1290 if ( type2 != SEXP_ATOM_STRING ){
1291 return SEXP_CHECK_TYPE_MISMATCH;
1294 if ((ship_name_lookup(CTEXT(index), 1) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) {
1295 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) { // == 0 when still on arrival list
1296 if (type != OPF_SHIP_WING_POINT){ // return invalid if not also looking for point
1297 return SEXP_CHECK_INVALID_SHIP_WING;
1300 if (waypoint_lookup(CTEXT(index)) < 0){
1301 if (verify_vector(CTEXT(index))){ // non-zero on verify vector mean invalid!
1302 if (!sexp_determine_team(CTEXT(index))){
1303 return SEXP_CHECK_INVALID_POINT;
1312 case OPF_AWACS_SUBSYSTEM:
1313 case OPF_SUBSYSTEM: {
1315 int shipnum,ship_class, i;
1318 if (type2 != SEXP_ATOM_STRING){
1319 return SEXP_CHECK_TYPE_MISMATCH;
1322 // we must get the model of the ship that is part of this sexpression and find a subsystem
1323 // with that name. This code assumes that the ship *always* preceeds the subsystem in an sexpression.
1324 // if this current fact is ever not the case, the next lines of code must be changes to get the correct
1326 switch(Operators[identify_operator(CTEXT(op_index))].value){
1327 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
1328 ship_index = Sexp_nodes[Sexp_nodes[op_index].rest].rest;
1333 ship_index = Sexp_nodes[op_index].rest;
1335 ship_index = Sexp_nodes[Sexp_nodes[Sexp_nodes[op_index].rest].rest].rest;
1340 ship_index = Sexp_nodes[op_index].rest;
1344 shipname = CTEXT(ship_index);
1345 shipnum = ship_name_lookup(shipname);
1347 ship_class = Ships[shipnum].ship_info_index;
1349 // must try to find the ship in the ship_arrival_list
1350 p_object *parse_obj;
1352 parse_obj = mission_parse_get_arrival_ship( shipname );
1353 if ( parse_obj == NULL ){
1354 return SEXP_CHECK_INVALID_SHIP;
1357 ship_class = parse_obj->ship_class;
1360 // check for the special "hull" value
1361 if ( (Operators[op].value == OP_SABOTAGE_SUBSYSTEM) || (Operators[op].value == OP_REPAIR_SUBSYSTEM) || (Operators[op].value == OP_SET_SUBSYSTEM_STRNGTH) ) {
1362 if ( !stricmp( CTEXT(index), SEXP_HULL_STRING) ){
1367 for (i=0; i<Ship_info[ship_class].n_subsystems; i++){
1368 if (!stricmp(Ship_info[ship_class].subsystems[i].subobj_name, CTEXT(index))){
1373 if (i == Ship_info[ship_class].n_subsystems){
1374 return SEXP_CHECK_INVALID_SUBSYS;
1377 // if we're checking for an AWACS subsystem and this is not an awacs subsystem
1379 if((type == OPF_AWACS_SUBSYSTEM) && !(Ship_info[ship_class].subsystems[i].flags & MSS_FLAG_AWACS)){
1380 return SEXP_CHECK_INVALID_SUBSYS;
1388 if (waypoint_lookup(CTEXT(index)) < 0){
1389 if (verify_vector(CTEXT(index))){
1390 return SEXP_CHECK_INVALID_POINT;
1394 if (type2 != SEXP_ATOM_STRING){
1395 return SEXP_CHECK_TYPE_MISMATCH;
1401 if (type2 == SEXP_ATOM_STRING){
1402 for (i=0; i<Num_team_names; i++){
1403 if (!stricmp(Team_names[i], CTEXT(index))){
1409 if (i == Num_team_names){
1410 return SEXP_CHECK_INVALID_IFF;
1413 if (type2 != SEXP_ATOM_STRING){
1414 return SEXP_CHECK_TYPE_MISMATCH;
1420 if (type2 != OPR_BOOL){
1421 return SEXP_CHECK_TYPE_MISMATCH;
1427 if (type2 != OPR_NULL){
1428 return SEXP_CHECK_TYPE_MISMATCH;
1434 if ( type2 != SEXP_ATOM_STRING ){
1435 return SEXP_CHECK_TYPE_MISMATCH;
1441 if (type2 != OPR_AI_GOAL){
1442 return SEXP_CHECK_TYPE_MISMATCH;
1446 int ship_num, ship2, i, w = 0, z;
1448 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1450 w = wing_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1453 *bad_index = Sexp_nodes[op_index].rest;
1456 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1460 Assert(Sexp_nodes[index].subtype == SEXP_ATOM_LIST);
1461 z = Sexp_nodes[index].first;
1462 Assert(Sexp_nodes[z].subtype != SEXP_ATOM_LIST);
1463 z = find_operator(CTEXT(z));
1464 if (ship_num >= 0) {
1465 if (!query_sexp_ai_goal_valid(z, ship_num)){
1466 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1470 for (i=0; i<Wings[w].wave_count; i++){
1471 if (!query_sexp_ai_goal_valid(z, Wings[w].ship_index[i])){
1472 return SEXP_CHECK_ORDER_NOT_ALLOWED;
1477 if ((z == OP_AI_DOCK) && (Sexp_nodes[index].rest >= 0)) {
1478 ship2 = ship_name_lookup(CTEXT(Sexp_nodes[index].rest));
1479 if ((ship_num < 0) || !ship_docking_valid(ship_num, ship2)){
1480 return SEXP_CHECK_DOCKING_NOT_ALLOWED;
1485 // we should check the syntax of the actual goal!!!!
1486 z = Sexp_nodes[index].first;
1487 if ((z = check_sexp_syntax(z, OPR_AI_GOAL, recursive, bad_index)) != 0){
1494 if (type2 != SEXP_ATOM_STRING){
1495 return SEXP_CHECK_TYPE_MISMATCH;
1498 for (i=0; i<MAX_SHIP_TYPE_COUNTS; i++){
1499 if (!stricmp( Ship_type_names[i], CTEXT(index))){
1504 if (i == MAX_SHIP_TYPE_COUNTS){
1505 return SEXP_CHECK_INVALID_SHIP_TYPE;
1510 case OPF_WAYPOINT_PATH:
1511 for (i=0; i<Num_waypoint_lists; i++){
1512 if (!stricmp(Waypoint_lists[i].name, CTEXT(index))){
1517 if (i == Num_waypoint_lists){
1518 return SEXP_CHECK_TYPE_MISMATCH;
1523 // code commented out because of order reversing of arguments to messaging code. Maybe
1524 // I'll comment it back in someday when older missions get fixed.
1525 if (type2 != SEXP_ATOM_STRING)
1526 return SEXP_CHECK_TYPE_MISMATCH;
1529 for (i=0; i<Num_messages; i++)
1530 if (!stricmp(Messages[i].name, CTEXT(index)))
1533 if (i == Num_messages)
1534 return SEXP_CHECK_UNKNOWN_MESSAGE;
1539 case OPF_PRIORITY: {
1540 // following code must be removed since I changed the order of the operators in the send-message
1541 // function to take priority second. Maybe someday, I'll require that all designers fix the
1543 if (type2 != SEXP_ATOM_STRING)
1544 return SEXP_CHECK_TYPE_MISMATCH;
1546 if (Fred_running) { // should still check in Fred though..
1549 name = CTEXT(index);
1550 if (!stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high"))
1553 return SEXP_CHECK_INVALID_PRIORITY;
1559 case OPF_MISSION_NAME:
1560 if (type2 != SEXP_ATOM_STRING)
1561 return SEXP_CHECK_TYPE_MISMATCH;
1564 if (mode == SEXP_MODE_CAMPAIGN) {
1565 for (i=0; i<Campaign.num_missions; i++)
1566 if (!stricmp(CTEXT(index), Campaign.missions[i].name)) {
1567 if ((i != Sexp_useful_number) && (Campaign.missions[i].level >= Campaign.missions[Sexp_useful_number].level))
1568 return SEXP_CHECK_INVALID_LEVEL;
1573 if (i == Campaign.num_missions)
1574 return SEXP_CHECK_INVALID_MISSION_NAME;
1577 // mwa -- put the following if statement to prevent Fred errors for possibly valid
1578 // conditions. We should do something else here!!!
1579 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE)
1580 || (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1583 if (!(*Mission_filename) || stricmp(Mission_filename, CTEXT(index)))
1584 return SEXP_CHECK_INVALID_MISSION_NAME;
1591 if (type2 != SEXP_ATOM_STRING)
1592 return SEXP_CHECK_TYPE_MISMATCH;
1594 // we only need to check the campaign list if running in Fred and are in campaign mode.
1595 // otherwise, check the set of current goals
1596 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1597 z = find_parent_operator(index);
1599 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1601 for (i=0; i<Campaign.num_missions; i++)
1602 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1605 // read the goal/event list from the mission file if both num_goals and num_events
1607 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1608 read_mission_goal_list(i);
1610 if (i < Campaign.num_missions) {
1611 for (t=0; t<Campaign.missions[i].num_goals; t++)
1612 if (!stricmp(CTEXT(index), Campaign.missions[i].goals[t].name))
1615 if (t == Campaign.missions[i].num_goals)
1616 return SEXP_CHECK_INVALID_GOAL_NAME;
1619 // MWA -- short circuit evaluation of these things for now.
1620 if ( (Operators[op].value == OP_PREVIOUS_GOAL_TRUE) || (Operators[op].value == OP_PREVIOUS_GOAL_FALSE) || (Operators[op].value == OP_PREVIOUS_GOAL_INCOMPLETE) )
1623 for (i=0; i<Num_goals; i++)
1624 if (!stricmp(CTEXT(index), Mission_goals[i].name))
1628 return SEXP_CHECK_INVALID_GOAL_NAME;
1633 case OPF_EVENT_NAME:
1634 if ( type2 != SEXP_ATOM_STRING )
1635 return SEXP_CHECK_TYPE_MISMATCH;
1637 // like above checking for goals, check events in the campaign only if in Fred
1638 // and only if in campaign mode. Otherwise, check the current set of events
1639 if ( Fred_running && (mode == SEXP_MODE_CAMPAIGN) ) {
1640 z = find_parent_operator(index);
1642 z = Sexp_nodes[z].rest; // first argument of operator should be mission name
1644 for (i=0; i<Campaign.num_missions; i++)
1645 if (!stricmp(CTEXT(z), Campaign.missions[i].name))
1648 // read the goal/event list from the mission file if both num_goals and num_events
1650 if ((Campaign.missions[i].num_goals <= 0) && (Campaign.missions[i].num_events <= 0) )
1651 read_mission_goal_list(i);
1653 if (i < Campaign.num_missions) {
1654 for (t=0; t<Campaign.missions[i].num_events; t++)
1655 if (!stricmp(CTEXT(index), Campaign.missions[i].events[t].name))
1658 if (t == Campaign.missions[i].num_events)
1659 return SEXP_CHECK_INVALID_EVENT_NAME;
1662 // MWA -- short circuit evaluation of these things for now.
1663 if ( (Operators[op].value == OP_PREVIOUS_EVENT_TRUE) || (Operators[op].value == OP_PREVIOUS_EVENT_FALSE) || (Operators[op].value == OP_PREVIOUS_EVENT_INCOMPLETE) )
1666 for ( i = 0; i < Num_mission_events; i++ ) {
1667 if ( !stricmp(CTEXT(index), Mission_events[i].name) )
1670 if ( i == Num_mission_events )
1671 return SEXP_CHECK_INVALID_EVENT_NAME;
1675 case OPF_DOCKER_POINT:
1676 if (type2 != SEXP_ATOM_STRING)
1677 return SEXP_CHECK_TYPE_MISMATCH;
1680 int ship_num, model, i, z;
1682 z = find_parent_operator(op_index);
1683 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[z].rest));
1686 *bad_index = Sexp_nodes[z].rest;
1688 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1691 model = Ships[ship_num].modelnum;
1692 z = model_get_num_dock_points(model);
1694 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1698 return SEXP_CHECK_INVALID_DOCKER_POINT;
1703 case OPF_DOCKEE_POINT:
1704 if (type2 != SEXP_ATOM_STRING)
1705 return SEXP_CHECK_TYPE_MISMATCH;
1708 int ship_num, model, i, z;
1710 ship_num = ship_name_lookup(CTEXT(Sexp_nodes[op_index].rest));
1713 *bad_index = Sexp_nodes[op_index].rest;
1715 return SEXP_CHECK_INVALID_SHIP; // should have already been caught earlier, but just in case..
1718 model = Ships[ship_num].modelnum;
1719 z = model_get_num_dock_points(model);
1721 if (!stricmp(CTEXT(index), model_get_dock_name(model, i)))
1725 return SEXP_CHECK_INVALID_DOCKEE_POINT;
1731 if (type2 != SEXP_ATOM_STRING)
1732 return SEXP_CHECK_TYPE_MISMATCH;
1734 if (*CTEXT(index) != '#') { // not a manual source?
1735 //if ( !stricmp(CTEXT(index), "<any allied>") )
1736 // return SEXP_CHECK_INVALID_MSG_SOURCE;
1738 if ( stricmp(CTEXT(index), "<any wingman>")) // not a special token?
1739 if ((ship_name_lookup(CTEXT(index)) < 0) && (wing_name_lookup(CTEXT(index), 1) < 0)) // is it in the mission?
1740 if (Fred_running || mission_parse_ship_arrived(CTEXT(index))) // == 0 when still on arrival list
1741 return SEXP_CHECK_INVALID_MSG_SOURCE;
1747 if (type2 != SEXP_ATOM_STRING)
1748 return SEXP_CHECK_TYPE_MISMATCH;
1752 case OPF_SKILL_LEVEL:
1753 if ( type2 != SEXP_ATOM_STRING )
1754 return SEXP_CHECK_TYPE_MISMATCH;
1756 for (i = 0; i < NUM_SKILL_LEVELS; i++) {
1757 if ( !stricmp(CTEXT(index), Skill_level_names(i, 0)) )
1760 if ( i == NUM_SKILL_LEVELS )
1761 return SEXP_CHECK_INVALID_SKILL_LEVEL;
1764 case OPF_MEDAL_NAME:
1765 if ( type2 != SEXP_ATOM_STRING)
1766 return SEXP_CHECK_TYPE_MISMATCH;
1768 for (i = 0; i < NUM_MEDALS; i++) {
1769 if ( !stricmp(CTEXT(index), Medals[i].name) )
1773 if ( i == NUM_MEDALS )
1774 return SEXP_CHECK_INVALID_MEDAL_NAME;
1777 case OPF_HUGE_WEAPON:
1778 case OPF_WEAPON_NAME:
1779 if ( type2 != SEXP_ATOM_STRING )
1780 return SEXP_CHECK_TYPE_MISMATCH;
1782 for (i = 0; i < Num_weapon_types; i++ ) {
1783 if ( !stricmp(CTEXT(index), Weapon_info[i].name) )
1787 if ( i == Num_weapon_types )
1788 return SEXP_CHECK_INVALID_WEAPON_NAME;
1790 // we need to be sure that for huge weapons, the WIF_HUGE flag is set
1791 if ( type == OPF_HUGE_WEAPON ) {
1792 if ( !(Weapon_info[i].wi_flags & WIF_HUGE) )
1793 return SEXP_CHECK_INVALID_WEAPON_NAME;
1798 case OPF_SHIP_CLASS_NAME:
1799 if ( type2 != SEXP_ATOM_STRING )
1800 return SEXP_CHECK_TYPE_MISMATCH;
1802 for (i = 0; i < Num_ship_types; i++ ) {
1803 if ( !stricmp(CTEXT(index), Ship_info[i].name) )
1807 if ( i == Num_ship_types )
1808 return SEXP_CHECK_INVALID_SHIP_CLASS_NAME;
1811 case OPF_HUD_GAUGE_NAME:
1812 if ( type2 != SEXP_ATOM_STRING )
1813 return SEXP_CHECK_TYPE_MISMATCH;
1815 for ( i = 0; i < NUM_HUD_GAUGES; i++ ) {
1816 if ( !stricmp(CTEXT(index), HUD_gauge_text[i]) )
1820 // if we reached the end of the list, then the name is invalid
1821 if ( i == NUM_HUD_GAUGES )
1822 return SEXP_CHECK_INVALID_GAUGE_NAME;
1826 case OPF_JUMP_NODE_NAME:
1827 if ( type2 != SEXP_ATOM_STRING )
1828 return SEXP_CHECK_TYPE_MISMATCH;
1830 for ( i = 0; i < Num_jump_nodes; i++ ) {
1831 if ( !stricmp(Jump_nodes[i].name, CTEXT(index)) )
1835 if ( i == Num_jump_nodes )
1836 return SEXP_CHECK_INVALID_JUMP_NODE;
1841 case OPF_VARIABLE_NAME:
1842 if ( Fred_running ) {
1843 if ( get_index_sexp_variable_name(Sexp_nodes[index].text) == -1) {
1844 return SEXP_CHECK_INVALID_VARIABLE;
1847 // if Fred not running anything goes
1851 // type checking for modify-variable
1852 // string or number -- anything goes
1856 Int3(); // currently unhandled argument format (so add it now)
1859 index = Sexp_nodes[index].rest;
1866 // Stuff a string (" chars ") in *str, return length.
1868 int get_string(char *str)
1872 len = strcspn(Mp + 1, "\"");
1873 strncpy(str, Mp + 1, len);
1881 // get text to stuff into Sexp_node in case of variable
1882 // if Fred_running - stuff Sexp_variables[].variable_name
1883 // otherwise - stuff index into Sexp_variables array.
1884 void get_sexp_text_for_variable(char *text, char *token)
1889 // get variable name (up to '['
1890 end_index = strcspn(token, "[");
1891 Assert( (end_index != 0) && (end_index < TOKEN_LENGTH-1) );
1892 strncpy(text, token, end_index);
1893 text[end_index] = '\0';
1895 if ( !Fred_running ) {
1896 // freespace - get index into Sexp_variables array
1897 sexp_var_index = get_index_sexp_variable_name(text);
1898 Assert(sexp_var_index != -1);
1899 sprintf(text, "%d", sexp_var_index);
1904 // returns the first sexp index of data this function allocates. (start of this sexp)
1905 // recursive function - always sets first and then rest
1906 int get_sexp(char *token)
1908 int start, node, last, len, op, count;
1909 char variable_text[TOKEN_LENGTH];
1911 // start - the node allocated in first instance of fuction
1912 // node - the node allocated in current instance of function
1913 // count - number of nodes allocated this instance of function [do we set last.rest or first]
1914 // variable - whether string or number is a variable referencing Sexp_variables
1920 ignore_white_space();
1921 while (*Mp != ')') {
1922 Assert(*Mp != EOF_CHAR);
1926 node = alloc_sexp("", SEXP_LIST, SEXP_ATOM_LIST, get_sexp(token), -1);
1928 } else if (*Mp == '\"') {
1930 len = strcspn(Mp + 1, "\"");
1932 Assert(Mp[len + 1] == '\"'); // hit EOF first (unterminated string)
1933 Assert(len < TOKEN_LENGTH); // token is too long.
1935 // check if string variable
1936 if ( *(Mp + 1) == SEXP_VARIABLE_CHAR ) {
1938 // reduce length by 1 for end \"
1939 int length = len - 1;
1940 Assert(length < 2*TOKEN_LENGTH+2);
1942 // start copying after skipping 1st char
1943 strncpy(token, Mp + 2, length);
1946 get_sexp_text_for_variable(variable_text, token);
1947 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_STRING, -1, -1);
1949 strncpy(token, Mp + 1, len);
1951 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_STRING, -1, -1);
1954 // bump past closing \" by 1 char
1958 // Sexp operator or number
1960 bool variable = false;
1961 while (*Mp != ')' && !is_white_space(*Mp)) {
1962 if ( (len == 0) && (*Mp == SEXP_VARIABLE_CHAR) ) {
1967 Assert(*Mp != EOF_CHAR);
1968 Assert(len < TOKEN_LENGTH - 1);
1969 token[len++] = *Mp++;
1974 op = identify_operator(token);
1976 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_OPERATOR, -1, -1);
1979 // convert token text for variable
1980 get_sexp_text_for_variable(variable_text, token);
1982 node = alloc_sexp(variable_text, (SEXP_ATOM | SEXP_FLAG_VARIABLE), SEXP_ATOM_NUMBER, -1, -1);
1984 node = alloc_sexp(token, SEXP_ATOM, SEXP_ATOM_NUMBER, -1, -1);
1992 Sexp_nodes[last].rest = node;
1997 Assert(node != -1); // ran out of nodes. Time to raise the MAX!
1999 ignore_white_space();
2002 Mp++; // skip past the ')'
2007 // Stuffs a list of sexp variables
2008 int stuff_sexp_variable_list()
2011 char var_name[TOKEN_LENGTH];
2012 char default_value[TOKEN_LENGTH];
2013 char str_type[TOKEN_LENGTH];
2018 required_string("$Variables:");
2019 ignore_white_space();
2021 // check for start of list
2023 error_display(1, "Reading sexp variable list. Found [%c]. Expecting '('.\n", *Mp);
2024 longjmp(parse_abort, 6);
2028 ignore_white_space();
2030 while (*Mp != ')') {
2031 Assert(count < MAX_SEXP_VARIABLES);
2033 // get index - for debug
2035 ignore_gray_space();
2038 get_string(var_name);
2039 ignore_gray_space();
2041 // get default_value;
2042 get_string(default_value);
2043 ignore_gray_space();
2046 get_string(str_type);
2047 ignore_white_space();
2050 if (!stricmp(str_type, "number")) {
2051 type = SEXP_VARIABLE_NUMBER;
2052 } else if (!stricmp(str_type, "string")) {
2053 type = SEXP_VARIABLE_STRING;
2054 } else if (!stricmp(str_type, "block")) {
2055 type = SEXP_VARIABLE_BLOCK | SEXP_VARIABLE_BLOCK_EXP;
2057 type = SEXP_VARIABLE_UNKNOWN;
2063 // check if variable name already exists
2064 if ( (type == SEXP_VARIABLE_NUMBER) || (type == SEXP_VARIABLE_STRING) ) {
2065 Assert(get_index_sexp_variable_name(var_name) == -1);
2068 sexp_add_variable(default_value, var_name, type, index);
2077 void build_sexp_text_string(char *buffer, int node, int mode)
2079 if (Sexp_nodes[node].type & SEXP_FLAG_VARIABLE) {
2081 int sexp_variables_index = get_index_sexp_variable_name(Sexp_nodes[node].text);
2082 Assert(sexp_variables_index != -1);
2083 Assert( (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER) || (Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING) );
2086 if (Sexp_nodes[node].subtype == SEXP_ATOM_NUMBER) {
2087 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_NUMBER);
2089 // Error check - can be Fred or Freespace
2090 if (mode == SEXP_ERROR_CHECK_MODE) {
2091 if ( Fred_running ) {
2092 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2094 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2097 // Save as string - only Fred
2098 Assert(mode == SEXP_SAVE_MODE);
2099 sprintf(buffer, "@%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2103 Assert(Sexp_nodes[node].subtype == SEXP_ATOM_STRING);
2104 Assert(Sexp_variables[sexp_variables_index].type & SEXP_VARIABLE_STRING);
2106 // Error check - can be Fred or Freespace
2107 if (mode == SEXP_ERROR_CHECK_MODE) {
2108 if ( Fred_running ) {
2109 sprintf(buffer, "%s[%s] ", Sexp_variables[sexp_variables_index].variable_name, Sexp_variables[sexp_variables_index].text);
2111 sprintf(buffer, "%s[%s] ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2114 // Save as string - only Fred
2115 Assert(mode == SEXP_SAVE_MODE);
2116 sprintf(buffer, "\"@%s[%s]\" ", Sexp_nodes[node].text, Sexp_variables[sexp_variables_index].text);
2121 if (Sexp_nodes[node].subtype == SEXP_ATOM_STRING) {
2122 sprintf(buffer, "\"%s\" ", CTEXT(node));
2124 sprintf(buffer, "%s ", CTEXT(node));
2131 int build_sexp_string(int cur_node, int level, int mode)
2134 int len, offset, node;
2136 Sexp_build_flag = 0;
2137 offset = strlen(Sexp_string);
2138 strcat(Sexp_string, "( ");
2140 while (node != -1) {
2141 Assert(node >= 0 && node < MAX_SEXP_NODES);
2142 if (Sexp_nodes[node].first == -1) {
2143 // build text to string
2144 build_sexp_text_string(pstr, node, mode);
2145 strcat(Sexp_string, pstr);
2148 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2151 node = Sexp_nodes[node].rest;
2154 strcat(Sexp_string, ") ");
2155 len = strlen(Sexp_string) - offset;
2157 Sexp_string[offset] = 0;
2158 build_extended_sexp_string(cur_node, level, mode);
2165 void build_extended_sexp_string(int cur_node, int level, int mode)
2168 int i, flag = 0, node;
2170 strcat(Sexp_string, "( ");
2172 while (node != -1) {
2173 if (flag) // not the first line?
2174 for (i=0; i<level + 1; i++)
2175 strcat(Sexp_string, " ");
2178 Assert(node >= 0 && node < MAX_SEXP_NODES);
2179 if (Sexp_nodes[node].first == -1) {
2180 build_sexp_text_string(pstr,node, mode);
2181 strcat(Sexp_string, pstr);
2184 build_sexp_string(Sexp_nodes[node].first, level + 1, mode);
2187 strcat(Sexp_string, "\n");
2188 node = Sexp_nodes[node].rest;
2191 for (i=0; i<level; i++)
2192 strcat(Sexp_string, " ");
2194 strcat(Sexp_string, ")");
2197 void convert_sexp_to_string(int cur_node, char *outstr, int mode)
2199 Sexp_string = outstr;
2202 build_sexp_string(cur_node, 0, mode);
2204 strcpy(Sexp_string, "( )");
2207 // determine if the named ship or wing hasn't arrived yet (wing or ship must be on arrival list)
2208 int sexp_query_has_yet_to_arrive(char *name)
2212 if (ship_query_state(name) < 0)
2215 i = wing_name_lookup(name, 1);
2217 // has not arrived yet, and never will arrive
2218 if ((i >= 0) && (Wings[i].num_waves >= 0) && (Wings[i].flags & WF_NEVER_EXISTED)){
2222 // has not arrived yet
2223 if ((i >= 0) && (Wings[i].num_waves >= 0) && !Wings[i].total_arrived_count){
2230 // arithmetic functions
2231 int add_sexps(int n)
2237 sum = eval_sexp( CAR(n) );
2239 sum = atoi( CTEXT(n) );
2241 // be sure to check for the NAN value when doing arithmetic -- this value should
2242 // get propagated to the next highest function.
2243 if ( Sexp_nodes[CAR(n)].value == SEXP_NAN )
2245 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2246 return SEXP_NAN_FOREVER;
2248 while (CDR(n) != -1) {
2249 val = eval_sexp( CDR(n) );
2250 // be sure to check for the NAN value when doing arithmetic -- this value should
2251 // get propagated to the next highest function.
2252 if ( Sexp_nodes[CDR(n)].value == SEXP_NAN )
2254 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2255 return SEXP_NAN_FOREVER;
2264 int sub_sexps(int n)
2269 if (Sexp_nodes[n].first != -1)
2270 sum = eval_sexp(CAR(n));
2272 sum = atoi(CTEXT(n));
2274 while (CDR(n) != -1) {
2275 sum -= eval_sexp(CDR(n));
2283 int mul_sexps(int n)
2288 if (Sexp_nodes[n].first != -1)
2289 sum = eval_sexp(Sexp_nodes[n].first);
2291 sum = atoi(CTEXT(n));
2293 while (Sexp_nodes[n].rest != -1) {
2294 sum *= eval_sexp(Sexp_nodes[n].rest);
2295 n = Sexp_nodes[n].rest;
2302 int div_sexps(int n)
2307 if (Sexp_nodes[n].first != -1)
2308 sum = eval_sexp(Sexp_nodes[n].first);
2310 sum = atoi(CTEXT(n));
2312 while (Sexp_nodes[n].rest != -1) {
2313 sum /= eval_sexp(Sexp_nodes[n].rest);
2314 n = Sexp_nodes[n].rest;
2321 int mod_sexps(int n)
2326 if (Sexp_nodes[n].first != -1)
2327 sum = eval_sexp(Sexp_nodes[n].first);
2329 sum = atoi(CTEXT(n));
2331 while (Sexp_nodes[n].rest != -1) {
2332 sum = sum % eval_sexp(Sexp_nodes[n].rest);
2333 n = Sexp_nodes[n].rest;
2340 int rand_internal(int low, int high)
2344 // get diff - don't allow negative or zero
2350 return (low + rand() % (diff + 1));
2354 int rand_sexp(int n, int multiple=0)
2361 if (Sexp_nodes[n].value == SEXP_NUM_EVAL) {
2362 // don't regenerate new random number
2363 rand_num = atoi(CTEXT(n));
2366 // if (Sexp_nodes[n].first != -1) {
2367 // low = eval_sexp(Sexp_nodes[n].first);
2369 // low = atoi(CTEXT(n));
2374 high = num_eval(CDR(n));
2376 // get the random number
2377 rand_num = rand_internal(low, high);
2380 // set .value and .text so random number is generated only once.
2381 Sexp_nodes[n].value = SEXP_NUM_EVAL;
2382 sprintf(Sexp_nodes[n].text, "%d", rand_num);
2390 // boolean evaluation functions. Evaluate all sexpressions in the 'or' operator. Needed to mark
2391 // entries in the mission log as essential so when pruning the log, we know which entries we might
2395 int all_false, result;
2400 if (Sexp_nodes[n].first != -1) {
2401 result |= eval_sexp(Sexp_nodes[n].first);
2402 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_TRUE )
2403 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2404 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2407 result |= atoi(CTEXT(n));
2409 // don't return on true value -- keep evaluating for mission log purposes
2413 while (Sexp_nodes[n].rest != -1) {
2414 result |= eval_sexp(Sexp_nodes[n].rest);
2415 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_TRUE )
2416 return SEXP_KNOWN_TRUE; // if one of the OR clauses is TRUE, whole clause is true
2417 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_FALSE ) // if the value is still unknown, they all can't be false
2419 n = Sexp_nodes[n].rest;
2420 // don't return on true value -- keep evaluating for mission log purposes
2427 return SEXP_KNOWN_FALSE;
2432 // this function does the 'and' operator. It will short circuit evaluation *but* it will still
2433 // evaluate other members of the and construct. I do this because I need events in the mission log
2434 // to get marked as essential for goal purposes, and evaluation is pretty much the only way
2437 int all_true, result;
2442 if (Sexp_nodes[n].first != -1) {
2443 result &= eval_sexp( CAR(n) );
2444 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2445 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2446 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be true
2449 result &= atoi(CTEXT(n));
2451 // don't short circuit -- evaluate everything for purposes of marking mission log
2455 while (Sexp_nodes[n].rest != -1) {
2458 new_result = eval_sexp( CDR(n) );
2459 result &= new_result;
2460 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2461 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2462 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2464 // don't short circuit -- evaluate everything for purposes of marking mission log
2468 n = Sexp_nodes[n].rest;
2473 return SEXP_KNOWN_TRUE;
2478 // this version of the 'and' operator determines whether or not it's arguments become true
2479 // in the order in which they are specified in the when statement. Should be a simple matter of
2480 // seeing if anything evaluates to true later than something that evalueated to false
2481 int sexp_and_in_sequence(int n)
2486 all_true = 1; // represents whether or not all nodes we have seen so far are true
2488 if (Sexp_nodes[n].first != -1) {
2489 result &= eval_sexp( CAR(n) );
2490 if ( Sexp_nodes[Sexp_nodes[n].first].value == SEXP_KNOWN_FALSE )
2491 return SEXP_KNOWN_FALSE; // if one of the AND clauses is FALSE, whole clause is false
2492 if ( Sexp_nodes[Sexp_nodes[n].first].value != SEXP_KNOWN_TRUE ) // if value is true, mark our all_true variable for later checking
2495 result &= atoi(CTEXT(n));
2497 // a little test -- if the previous sexpressions was true, then mark the node itself as always
2498 // true. I did this because of the distance function. It might become true, then when waiting for
2499 // the second evalation, it might become false, rendering this function false. So, when one becomes
2500 // true -- mark it true forever.
2502 Sexp_nodes[Sexp_nodes[n].first].value = SEXP_KNOWN_TRUE;
2504 while (Sexp_nodes[n].rest != -1) {
2507 next_result = eval_sexp( CDR(n) );
2508 if ( next_result && !result ) // if current result is true, and our running result is false, thngs didn't become true in order
2509 return SEXP_KNOWN_FALSE;
2510 result &= next_result;
2511 if ( Sexp_nodes[Sexp_nodes[n].rest].value == SEXP_KNOWN_FALSE )
2512 return SEXP_KNOWN_FALSE; // if one of the OR clauses is TRUE, whole clause is true
2513 if ( Sexp_nodes[Sexp_nodes[n].rest].value != SEXP_KNOWN_TRUE ) // if the value is still unknown, they all can't be false
2515 // see comment above for explanation of next lines
2517 Sexp_nodes[Sexp_nodes[n].rest].value = SEXP_KNOWN_TRUE;
2518 n = Sexp_nodes[n].rest;
2523 return SEXP_KNOWN_TRUE;
2528 // for these four basic boolean operations (not, <, >, and =), we have special cases that we must deal
2529 // with. We have sexpressions operators that might return a NAN type return value (such as the distance
2530 // between two ships when one of the ships is destroyed or departed). These operations need to check for
2531 // this special NAN value and adjust their return types accordingly. NAN values represent false return values
2532 int sexp_not( int n )
2537 if (Sexp_nodes[n].first != -1) {
2538 result = eval_sexp( CAR(n) );
2539 if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_FALSE )
2540 return SEXP_KNOWN_TRUE; // not KNOWN_FALSE == KNOWN_TRUE;
2541 else if ( Sexp_nodes[CAR(n)].value == SEXP_KNOWN_TRUE ) // not KNOWN_TRUE == KNOWN_FALSE
2542 return SEXP_KNOWN_FALSE;
2543 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) // not NAN == TRUE (I think)
2545 else if ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER )
2548 result = atoi(CTEXT(n));
2558 exp1 = eval_sexp( n );
2559 exp2 = eval_sexp( CDR(n) );
2561 // check for the NAN value
2562 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2564 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2565 return SEXP_KNOWN_FALSE;
2576 exp1 = eval_sexp( n );
2577 exp2 = eval_sexp( CDR(n) );
2579 // check for the NAN value
2580 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2582 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2583 return SEXP_KNOWN_FALSE;
2590 int sexp_equal(int n)
2594 exp1 = eval_sexp( n );
2595 exp2 = eval_sexp( CDR(n) );
2597 // check for the NAN value
2598 if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN ) )
2600 else if ( ( Sexp_nodes[CAR(n)].value == SEXP_NAN_FOREVER ) || ( Sexp_nodes[CDR(n)].value == SEXP_NAN_FOREVER ) )
2601 return SEXP_KNOWN_FALSE;
2608 // Evaluate if given ship is destroyed.
2609 // Return true if the ship in the expression has been destroyed.
2610 int sexp_is_destroyed(int n, fix *latest_time)
2613 int count, num_destroyed, wing_index;
2625 if (sexp_query_has_yet_to_arrive(name))
2626 return SEXP_CANT_EVAL;
2628 // check to see if this ship/wing has departed. If so, then function is known false
2629 if ( mission_log_get_time (LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time (LOG_WING_DEPART, name, NULL, NULL) )
2630 return SEXP_KNOWN_FALSE;
2632 // check the mission log. If ship/wing not destroyed, immediately return 0.
2633 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)) {
2635 if ( latest_time && (time > *latest_time) )
2636 *latest_time = time;
2638 // ship or wing isn't destroyed -- add to directive count
2639 if ( (wing_index = wing_name_lookup( name, 1 )) >= 0 ) {
2640 Directive_count += Wings[wing_index].current_count;
2645 // move to next ship/wing in list
2649 // special case to mark a directive for destroy wing objectives true after a short amount
2650 // of time when there are more waves for this wing.
2651 if ( (count == 1) && (wing_index >= 0) && (Directive_count == 0) ) {
2652 if ( Wings[wing_index].current_wave < Wings[wing_index].num_waves )
2653 Directive_count = DIRECTIVE_WING_ZERO;
2656 if ( count == num_destroyed )
2657 return SEXP_KNOWN_TRUE;
2663 // return true if the subsystem of the given ship has been destroyed or not
2664 int sexp_is_subsystem_destroyed(int n)
2666 char *ship_name, *subsys_name;
2670 ship_name = CTEXT(n);
2671 subsys_name = CTEXT(CDR(n));
2673 if (sexp_query_has_yet_to_arrive(ship_name))
2674 return SEXP_CANT_EVAL;
2676 // if the ship has departed, no way to destroy it's subsystem.
2677 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2678 return SEXP_KNOWN_FALSE;
2680 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, NULL) )
2681 return SEXP_KNOWN_TRUE;
2687 // determines if a ship has docked
2688 int sexp_has_docked(int n)
2690 char *docker = CTEXT(n);
2691 char *dockee = CTEXT(CDR(n));
2692 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
2694 if (sexp_query_has_yet_to_arrive(docker))
2695 return SEXP_CANT_EVAL;
2697 if (sexp_query_has_yet_to_arrive(dockee))
2698 return SEXP_CANT_EVAL;
2700 Assert ( count > 0 );
2701 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2702 return SEXP_KNOWN_FALSE;
2704 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, NULL) )
2707 return SEXP_KNOWN_TRUE;
2710 // determines if a ship has undocked
2711 int sexp_has_undocked(int n)
2713 char *docker = CTEXT(n);
2714 char *dockee = CTEXT(CDR(n));
2715 int count = atoi(CTEXT(CDR(CDR(n))));
2717 if (sexp_query_has_yet_to_arrive(docker))
2718 return SEXP_CANT_EVAL;
2720 if (sexp_query_has_yet_to_arrive(dockee))
2721 return SEXP_CANT_EVAL;
2723 Assert ( count > 0 );
2724 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, NULL) ) {
2725 // if either ship destroyed before they dock, then sexp is known false
2726 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
2727 return SEXP_KNOWN_FALSE;
2732 return SEXP_KNOWN_TRUE;
2735 // determines if a ship has arrived onto the scene
2736 int sexp_has_arrived(int n, fix *latest_time)
2739 int count, num_arrived;
2747 // if there is no log entry for this ship/wing for arrival, sexpression is false
2748 if ( mission_log_get_time(LOG_SHIP_ARRIVE, name, NULL, &time) || mission_log_get_time(LOG_WING_ARRIVE, name, NULL, &time) ) {
2750 if ( latest_time && (time > *latest_time) )
2751 *latest_time = time;
2756 if ( count == num_arrived )
2757 return SEXP_KNOWN_TRUE;
2762 // determines if a ship/wing has departed
2763 int sexp_has_departed(int n, fix *latest_time)
2766 int count, num_departed;
2775 if (sexp_query_has_yet_to_arrive(name))
2776 return SEXP_CANT_EVAL;
2778 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
2779 // the sexpression is not true.
2780 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) || mission_log_get_time(LOG_WING_DESTROYED, name, NULL, NULL))
2781 return SEXP_KNOWN_FALSE;
2782 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_WING_DEPART, name, NULL, &time) ) {
2784 if ( latest_time && (time > *latest_time) )
2785 *latest_time = time;
2790 if ( count == num_departed )
2791 return SEXP_KNOWN_TRUE;
2796 // determines if ships are disabled
2797 int sexp_is_disabled( int n, fix *latest_time )
2800 int count, num_disabled;
2809 if (sexp_query_has_yet_to_arrive(name))
2810 return SEXP_CANT_EVAL;
2812 // if ship/wing destroyed, sexpression is known false. Also, if there is no disable log entry, then
2813 // the sexpression is not true.
2814 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2815 return SEXP_KNOWN_FALSE;
2816 else if ( mission_log_get_time(LOG_SHIP_DISABLED, name, NULL, &time) ) {
2818 if ( latest_time && (time > *latest_time) )
2819 *latest_time = time;
2824 if ( count == num_disabled )
2825 return SEXP_KNOWN_TRUE;
2830 // function to determine if a ship is done flying waypoints
2831 int sexp_are_waypoints_done( int n )
2833 char *ship_name, *waypoint_name;
2835 ship_name = CTEXT(n);
2836 waypoint_name = CTEXT(CDR(n));
2838 if (sexp_query_has_yet_to_arrive(ship_name))
2839 return SEXP_CANT_EVAL;
2841 // a destroyed or departed ship will never reach their goal -- return known false
2842 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
2843 return SEXP_KNOWN_FALSE;
2844 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
2845 return SEXP_KNOWN_FALSE;
2847 // now check the log for the waypoints done entry
2848 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, NULL) )
2849 return SEXP_KNOWN_TRUE;
2855 // determines if ships are disarmed
2856 int sexp_is_disarmed( int n, fix *latest_time )
2859 int count, num_disarmed;
2868 if (sexp_query_has_yet_to_arrive(name))
2869 return SEXP_CANT_EVAL;
2871 // if ship/wing destroyed, sexpression is known false. Also, if there is no disarm log entry, then
2872 // the sexpression is not true.
2873 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time) )
2874 return SEXP_KNOWN_FALSE;
2875 else if ( mission_log_get_time(LOG_SHIP_DISARMED, name, NULL, &time) ) {
2877 if ( latest_time && (time > *latest_time) )
2878 *latest_time = time;
2883 if ( count == num_disarmed )
2884 return SEXP_KNOWN_TRUE;
2889 // the following functions are similar to the above objective functions but return true/false
2890 // if N seconds have elasped after the corresponding function is true.
2891 int sexp_is_destroyed_delay(int n)
2900 delay = i2f(num_eval(n));
2902 // check value of is_destroyed function. KNOWN_FALSE should be returned immediately
2903 val = sexp_is_destroyed( CDR(n), &time );
2904 if ( val == SEXP_KNOWN_FALSE )
2907 if ( val == SEXP_CANT_EVAL )
2908 return SEXP_CANT_EVAL;
2912 if ( (Missiontime - time) >= delay )
2913 return SEXP_KNOWN_TRUE;
2919 int sexp_is_subsystem_destroyed_delay( int n )
2921 char *ship_name, *subsys_name;
2926 ship_name = CTEXT(n);
2927 subsys_name = CTEXT(CDR(n));
2928 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
2930 if (sexp_query_has_yet_to_arrive(ship_name))
2931 return SEXP_CANT_EVAL;
2933 // if the ship has departed, no way to destroy it's subsystem.
2934 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ))
2935 return SEXP_KNOWN_FALSE;
2937 if ( mission_log_get_time(LOG_SHIP_SUBSYS_DESTROYED, ship_name, subsys_name, &time) ) {
2938 if ( (Missiontime - time) >= delay )
2939 return SEXP_KNOWN_TRUE;
2945 int sexp_is_disabled_delay( int n )
2953 delay = i2f(atoi(CTEXT(n)));
2955 // check value of is_disable for known false and return immediately if it is.
2956 val = sexp_is_disabled( CDR(n), &time );
2957 if ( val == SEXP_KNOWN_FALSE )
2960 if ( val == SEXP_CANT_EVAL )
2961 return SEXP_CANT_EVAL;
2964 if ( (Missiontime - time) >= delay )
2965 return SEXP_KNOWN_TRUE;
2971 int sexp_is_disarmed_delay( int n )
2979 delay = i2f(atoi(CTEXT(n)));
2981 // check value of is_disarmed for a known false value and return that immediately if it is
2982 val = sexp_is_disarmed( CDR(n), &time );
2983 if ( val == SEXP_KNOWN_FALSE )
2986 if ( val == SEXP_CANT_EVAL )
2987 return SEXP_CANT_EVAL;
2990 if ( (Missiontime - time) >= delay )
2991 return SEXP_KNOWN_TRUE;
2997 int sexp_has_docked_delay( int n )
2999 char *docker = CTEXT(n);
3000 char *dockee = CTEXT(CDR(n));
3001 int count = atoi(CTEXT(CDR(CDR(n)))); // count of times that we should look for
3002 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
3005 Assert ( count > 0 );
3006 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
3007 return SEXP_KNOWN_FALSE;
3009 if (sexp_query_has_yet_to_arrive(docker))
3010 return SEXP_CANT_EVAL;
3012 if (sexp_query_has_yet_to_arrive(dockee))
3013 return SEXP_CANT_EVAL;
3015 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) )
3018 if ( (Missiontime - time) >= delay )
3019 return SEXP_KNOWN_TRUE;
3024 int sexp_has_undocked_delay( int n )
3026 char *docker = CTEXT(n);
3027 char *dockee = CTEXT(CDR(n));
3028 int count = atoi(CTEXT(CDR(CDR(n))));
3029 fix delay = i2f(atoi(CTEXT(CDR(CDR(CDR(n))))));
3032 if (sexp_query_has_yet_to_arrive(docker))
3033 return SEXP_CANT_EVAL;
3035 if (sexp_query_has_yet_to_arrive(dockee))
3036 return SEXP_CANT_EVAL;
3038 Assert ( count > 0 );
3039 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ) {
3040 // if either ship destroyed before they dock, then sexp is known false
3041 if ( mission_log_get_time(LOG_SHIP_DESTROYED, docker, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, dockee, NULL, NULL) )
3042 return SEXP_KNOWN_FALSE;
3047 if ( (Missiontime - time) >= delay )
3048 return SEXP_KNOWN_TRUE;
3053 int sexp_has_arrived_delay( int n )
3061 delay = i2f(atoi(CTEXT(n)));
3063 // check return value from arrived function. if can never arrive, then return that value here as well
3064 val = sexp_has_arrived( CDR(n), &time );
3065 if ( val == SEXP_KNOWN_FALSE )
3068 if ( val == SEXP_CANT_EVAL )
3069 return SEXP_CANT_EVAL;
3072 if ( (Missiontime - time) >= delay )
3073 return SEXP_KNOWN_TRUE;
3079 int sexp_has_departed_delay( int n )
3087 delay = i2f(atoi(CTEXT(n)));
3089 // must first check to see if the departed function could ever be true/false or is true or false.
3090 // if it can never be true, return that value
3091 val = sexp_has_departed( CDR(n), &time);
3092 if ( val == SEXP_KNOWN_FALSE )
3095 if ( val == SEXP_CANT_EVAL )
3096 return SEXP_CANT_EVAL;
3099 if ( (Missiontime - time) >= delay )
3100 return SEXP_KNOWN_TRUE;
3106 // function to determine if a ship is done flying waypoints after N seconds
3107 int sexp_are_waypoints_done_delay( int n )
3109 char *ship_name, *waypoint_name;
3112 ship_name = CTEXT(n);
3113 waypoint_name = CTEXT(CDR(n));
3114 delay = i2f(atoi(CTEXT(CDR(CDR(n)))));
3116 if (sexp_query_has_yet_to_arrive(ship_name))
3117 return SEXP_CANT_EVAL;
3119 // a destroyed or departed ship will never reach their goal -- return known false
3121 // Not checking the entries below. Ships which warp out after reaching their goal (or getting
3122 // destroyed after their goal), but after reaching their waypoints, may have this goal incorrectly
3125 // now check the log for the waypoints done entry
3126 if ( mission_log_get_time(LOG_WAYPOINTS_DONE, ship_name, waypoint_name, &time) ) {
3127 if ( (Missiontime - time) >= delay )
3128 return SEXP_KNOWN_TRUE;
3130 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
3131 return SEXP_KNOWN_FALSE;
3132 else if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) )
3133 return SEXP_KNOWN_FALSE;
3139 // function to determine is all of a given ship type are destroyed
3140 int sexp_ship_type_destroyed( int n )
3145 percent = atoi(CTEXT(n));
3146 shiptype = CTEXT(CDR(n));
3148 for ( type = 0; type < MAX_SHIP_TYPE_COUNTS; type++ ) {
3149 if ( !stricmp( Ship_type_names[type], shiptype) )
3153 // bogus if we reach the end of this array!!!!
3154 if ( type == MAX_SHIP_TYPE_COUNTS ) {
3159 if ( Ship_counts[type].total == 0 )
3162 // determine if the percentage of killed/total is >= percentage given in the expression
3163 if ( (Ship_counts[type].killed * 100 / Ship_counts[type].total) >= percent)
3164 return SEXP_KNOWN_TRUE;
3170 // following are time based functions
3171 int sexp_has_time_elapsed(int n)
3173 int time = num_eval(n);
3175 if ( f2i(Missiontime) >= time )
3176 return SEXP_KNOWN_TRUE;
3181 // next function returns the time into the mission
3182 int sexp_mission_time()
3184 return f2i(Missiontime);
3187 // returns percent of length of distance to special warpout plane
3188 int sexp_special_warp_dist( int n)
3194 ship_name = CTEXT(n);
3196 // check to see if either ship was destroyed or departed. If so, then make this node known
3198 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ) {
3199 return SEXP_NAN_FOREVER;
3203 shipnum = ship_name_lookup(ship_name);
3208 // check that ship has warpout_objnum
3209 if (Ships[shipnum].special_warp_objnum == -1) {
3213 Assert( (Ships[shipnum].special_warp_objnum >= 0) && (Ships[shipnum].special_warp_objnum < MAX_OBJECTS));
3214 if ( (Ships[shipnum].special_warp_objnum < 0) && (Ships[shipnum].special_warp_objnum >= MAX_OBJECTS) ) {
3218 // check the special warpout device is valid
3220 object *ship_objp = &Objects[Ships[shipnum].objnum];
3221 object *warp_objp = &Objects[Ships[shipnum].special_warp_objnum];
3222 if (warp_objp->type == OBJ_SHIP) {
3223 if (Ship_info[Ships[warp_objp->instance].ship_info_index].flags & SIF_KNOSSOS_DEVICE) {
3232 // check if within 45 degree half-angle cone of facing
3233 float dot = fl_abs(vm_vec_dotprod(&warp_objp->orient.v.fvec, &ship_objp->orient.v.fvec));
3240 float dist = fvi_ray_plane(&hit_pt, &warp_objp->pos, &warp_objp->orient.v.fvec, &ship_objp->pos, &ship_objp->orient.v.fvec, 0.0f);
3241 polymodel *pm = model_get(Ships[shipnum].modelnum);
3242 dist += pm->mins.xyz.z;
3244 // return as a percent of length
3245 return (int) (100.0f * dist / ship_get_length(&Ships[shipnum]));
3249 int sexp_time_destroyed(int n)
3253 if ( !mission_log_get_time( LOG_SHIP_DESTROYED, CTEXT(n), NULL, &time) ){ // returns 0 when not found
3260 int sexp_time_wing_destroyed(int n)
3264 if ( !mission_log_get_time( LOG_WING_DESTROYED, CTEXT(n), NULL, &time) ){
3271 int sexp_time_docked(int n)
3274 char *docker = CTEXT(n);
3275 char *dockee = CTEXT(CDR(n));
3276 int count = atoi(CTEXT(CDR(CDR(n))));
3278 Assert ( count > 0 );
3279 if ( !mission_log_get_time_indexed(LOG_SHIP_DOCK, docker, dockee, count, &time) ){
3286 int sexp_time_undocked(int n)
3289 char *docker = CTEXT(n);
3290 char *dockee = CTEXT(CDR(n));
3291 int count = atoi(CTEXT(CDR(CDR(n))));
3293 Assert ( count > 0 );
3294 if ( !mission_log_get_time_indexed(LOG_SHIP_UNDOCK, docker, dockee, count, &time) ){
3301 int sexp_time_ship_arrived(int n)
3306 if ( !mission_log_get_time( LOG_SHIP_ARRIVE, CTEXT(n), NULL, &time ) ){
3313 int sexp_time_wing_arrived(int n)
3318 if ( !mission_log_get_time( LOG_WING_ARRIVE, CTEXT(n), NULL, &time ) ){
3325 int sexp_time_ship_departed(int n)
3330 if ( !mission_log_get_time( LOG_SHIP_DEPART, CTEXT(n), NULL, &time ) ){
3337 int sexp_time_wing_departed(int n)
3342 if ( !mission_log_get_time( LOG_WING_DEPART, CTEXT(n), NULL, &time ) ){
3349 // function to return the remaining shields as a percentage of the given ship.
3350 int sexp_shields_left(int n)
3352 int shipnum, percent;
3355 shipname = CTEXT(n);
3357 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3358 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3359 return SEXP_NAN_FOREVER;
3362 shipnum = ship_name_lookup( shipname );
3363 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3367 // now return the amount of shields left as a percentage of the whole.
3368 percent = (int)(get_shield_strength(&Objects[Ships[shipnum].objnum]) / Ship_info[Ships[shipnum].ship_info_index].shields * 100.0f);
3372 // function to return the remaining hits left as a percentage of the whole. This hit amount counts for
3373 // all hits on the ship (hull + subsystems). Use hits_left_hull to find hull hits remaining.
3374 int sexp_hits_left(int n)
3376 int shipnum, percent;
3379 shipname = CTEXT(n);
3381 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3382 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3383 return SEXP_NAN_FOREVER;
3386 shipnum = ship_name_lookup( shipname );
3387 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3391 // now return the amount of hits left as a percentage of the whole. Subtract the percentage from 100
3392 // since we are working with total hit points taken, not total remaining.
3393 ship *shipp = &Ships[shipnum];
3394 ship_info *sip = &Ship_info[shipp->ship_info_index];
3395 object *objp = &Objects[shipp->objnum];
3396 percent = (int) (100.0f * objp->hull_strength / sip->initial_hull_strength);
3400 // is ship visible on radar
3401 // returns 0 - not visible
3402 // returns 1 - marginally targetable (jiggly on radar)
3403 // returns 2 - fully targetable
3404 int sexp_is_ship_visible(int n)
3408 int ship_is_visible = 0;
3410 // if multiplayer, bail
3411 if (Game_mode & GM_MULTIPLAYER) {
3412 return SEXP_NAN_FOREVER;
3415 shipname = CTEXT(n);
3417 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3418 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3419 return SEXP_NAN_FOREVER;
3422 shipnum = ship_name_lookup( shipname );
3423 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3427 // get ship's *radar* visiblity
3428 if (Player_ship != NULL) {
3429 if (ship_is_visible_by_team(shipnum, Player_ship->team)) {
3430 ship_is_visible = 2;
3434 // only check awacs level if ship is not visible by team
3435 if (Player_ship != NULL && !ship_is_visible) {
3436 float awacs_level = awacs_get_level(&Objects[Ships[shipnum].objnum], Player_ship);
3437 if (awacs_level >= 1.0f) {
3438 ship_is_visible = 2;
3439 } else if (awacs_level > 0) {
3440 ship_is_visible = 1;
3444 return ship_is_visible;
3447 // get multi team v team score
3448 // if not multi team v team return 0
3449 // if invalid team return 0
3450 int sexp_team_score(int node)
3453 if (Game_mode & GM_MULTIPLAYER) {
3454 if (Netgame.type_flags & NG_TYPE_TEAM) {
3456 int team = atoi(CTEXT(node));
3459 return Multi_team0_score;
3460 } else if (team == 2) {
3461 return Multi_team1_score;
3463 // invalid team index
3474 // function to return the remaining hits left on a subsystem as a percentage of thw whole.
3475 int sexp_hits_left_subsystem(int n)
3477 int shipnum, percent, type;
3481 shipname = CTEXT(n);
3483 // if ship is gone or departed, cannot ever evaluate properly. Return NAN_FOREVER
3484 if ( mission_log_get_time(LOG_SHIP_DESTROYED, shipname, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, shipname, NULL, NULL) ){
3485 return SEXP_NAN_FOREVER;
3488 shipnum = ship_name_lookup( shipname );
3489 if ( shipnum == -1 ){ // hmm.. if true, must not have arrived yet
3493 subsys_name = CTEXT(CDR(n));
3494 type = ai_get_subsystem_type( subsys_name );
3495 if ( (type >= 0) && (type < SUBSYSTEM_MAX) ) {
3496 // return as a percentage the hits remaining on the subsystem as a whole (i.e. for 3 engines,
3497 // we are returning the sum of the hits on the 3 engines)
3498 if (type == SUBSYSTEM_UNKNOWN) {
3499 // find the ship subsystem by searching ship's subsys_list
3501 ss = GET_FIRST( &Ships[shipnum].subsys_list );
3502 while ( ss != END_OF_LIST( &Ships[shipnum].subsys_list ) ) {
3504 if ( !stricmp(ss->system_info->subobj_name, subsys_name)) {
3505 percent = (int) (ss->current_hits / ss->system_info->max_hits * 100.0f);
3509 ss = GET_NEXT( ss );
3511 // we reached end of ship subsys list without finding subsys_name
3515 percent = (int)(ship_get_subsystem_strength(&Ships[shipnum],type) * 100.0f);
3519 return SEXP_NAN; // if for some strange reason, the type field of the subsystem is bogus
3522 int sexp_determine_team(char *subj)
3526 if (!stricmp(subj, "<any friendly>")){
3527 team = TEAM_FRIENDLY;
3528 } else if (!stricmp(subj, "<any hostile>")){
3529 team = TEAM_HOSTILE;
3530 } else if (!stricmp(subj, "<any neutral>")){
3531 team = TEAM_NEUTRAL;
3532 } else if (!stricmp(subj, "<any unknown>")){
3533 team = TEAM_UNKNOWN;
3534 } else if (!stricmp(subj, "<any traitor>")){
3535 team = TEAM_TRAITOR;
3541 // returns the distance between two objects. If a wing is specificed as one (or both) or the arguments
3542 // to this function, we are looking for the closest distance
3543 int sexp_distance(int n)
3545 int i, team, obj, dist, dist_min = 0, inited = 0;
3546 char *sname1, *sname2;
3551 sname2 = CTEXT(CDR(n));
3553 // check to see if either ship was destroyed or departed. If so, then make this node known
3555 if ( mission_log_get_time(LOG_SHIP_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname1, NULL, NULL) ||
3556 mission_log_get_time(LOG_SHIP_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, sname2, NULL, NULL) )
3557 return SEXP_NAN_FOREVER;
3559 // one of the names might be the name of a wing. Check to see if the wing is detroyed or departed
3560 if ( mission_log_get_time(LOG_WING_DESTROYED, sname1, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname1, NULL, NULL) ||
3561 mission_log_get_time(LOG_WING_DESTROYED, sname2, NULL, NULL) || mission_log_get_time( LOG_WING_DEPART, sname2, NULL, NULL) )
3562 return SEXP_NAN_FOREVER;
3564 team = sexp_determine_team(sname1);
3565 if (team) { // we have a team type, so check all ships of that type
3566 so = GET_FIRST(&Ship_obj_list);
3567 while (so != END_OF_LIST(&Ship_obj_list)) {
3568 if (Ships[Objects[so->objnum].instance].team == team) {
3570 dist = sexp_distance2(obj, sname2);
3571 if (dist != SEXP_NAN) {
3572 if (!inited || (dist < dist_min)) {
3582 if (!inited) // no objects were checked
3588 // at this point, we must have a wing, ship or point for a subj
3589 obj = ship_name_lookup(sname1);
3591 return sexp_distance2(Ships[obj].objnum, sname2);
3593 // at this point, we must have a wing or point for a subj
3594 obj = waypoint_lookup(sname1);
3596 return sexp_distance2(obj, sname2);
3598 // at this point, we must have a wing for a subj
3599 obj = wing_name_lookup(sname1);
3601 return SEXP_NAN; // we apparently don't have anything legal
3603 wingp = &Wings[obj];
3604 for (i=0; i<wingp->current_count; i++) {
3605 obj = Ships[wingp->ship_index[i]].objnum;
3606 dist = sexp_distance2(obj, sname2);
3607 if (dist != SEXP_NAN) {
3608 if (!inited || (dist < dist_min)) {
3615 if (!inited) // no objects were checked
3621 // check distance between a given ship and a given subject (ship, wing, any <team>).
3622 int sexp_distance2(int obj1, char *subj)
3624 int i, team, obj2, dist, dist_min = 0, inited = 0;
3628 team = sexp_determine_team(subj);
3629 if (team) { // we have a team type, so check all ships of that type
3630 so = GET_FIRST(&Ship_obj_list);
3631 while (so != END_OF_LIST(&Ship_obj_list)) {
3632 if (Ships[Objects[so->objnum].instance].team == team) {
3634 dist = sexp_distance3(obj1, obj2);
3635 if (dist != SEXP_NAN) {
3636 if (!inited || (dist < dist_min)) {
3646 if (!inited) // no objects were checked
3652 // at this point, we must have a wing, ship or point for a subj
3653 obj2 = ship_name_lookup(subj);
3655 return sexp_distance3(obj1, Ships[obj2].objnum);
3657 // at this point, we must have a wing or point for a subj
3658 obj2 = waypoint_lookup(subj);
3660 return sexp_distance3(obj1, obj2);
3662 // at this point, we must have a wing for a subj
3663 obj2 = wing_name_lookup(subj);
3665 return SEXP_NAN; // we apparently don't have anything legal
3667 wingp = &Wings[obj2];
3668 for (i=0; i<wingp->current_count; i++) {
3669 obj2 = Ships[wingp->ship_index[i]].objnum;
3670 dist = sexp_distance3(obj1, obj2);
3671 if (dist != SEXP_NAN) {
3672 if (!inited || (dist < dist_min)) {
3679 if (!inited) // no objects were checked
3685 // check distance between two given objects
3686 int sexp_distance3(int obj1, int obj2)
3688 if ( (obj1 == -1) || (obj2 == -1) ) // if either object isn't present in the mission now
3689 return SEXP_NAN; // return a really small number
3691 if ( (Objects[obj1].type == OBJ_SHIP) && (Objects[obj2].type == OBJ_SHIP) ) {
3692 if (OBJ_INDEX(Player_obj) == obj1)
3693 return (int) hud_find_target_distance( &Objects[obj2], &Objects[obj1] );
3695 return (int) hud_find_target_distance( &Objects[obj1], &Objects[obj2] );
3698 return (int) vm_vec_dist_quick( &Objects[obj1].pos, &Objects[obj2].pos );
3702 // funciton to determine when the last meaningful order was given to one or more ships. Returns
3703 // true or false depending on whether or not a meaningful order was received
3704 int sexp_last_order_time( int n )
3711 time = i2f(atoi(CTEXT(n)));
3712 Assert ( time >= 0 );
3717 instance = ship_name_lookup(name);
3718 if ( instance != -1 ) {
3719 aigp = Ai_info[Ships[instance].ai_index].goals;
3721 instance = wing_name_lookup(name);
3722 if ( instance == -1 ) // if we cannot find ship or wing, return 0
3724 aigp = Wings[instance].ai_goals;
3727 // with the ship, check the ai_goals structure for this ship and determine if there are any
3728 // orders which are < time seconds since current mission time
3729 for ( i = 0; i < MAX_AI_GOALS; i++ ) {
3732 mode = aigp->ai_mode;
3733 if ( (mode != AI_GOAL_NONE) && (mode != AI_GOAL_WARP) )
3734 if ( (aigp->time + time) > Missiontime )
3738 if ( i == MAX_AI_GOALS )
3747 // sexpression to return the number of players in the mission
3748 int sexp_num_players()
3754 for ( objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
3755 if ( (objp->type == OBJ_SHIP) && (objp->flags & OF_PLAYER_SHIP) )
3762 // expression to determine if the current skill level of the game is at least
3763 // the skill level given in the sexpression
3764 int sexp_skill_level_at_least( int n )
3769 level_name = CTEXT(n);
3770 for (i = 0; i < NUM_SKILL_LEVELS; i++ ) {
3771 if ( !stricmp(level_name, Skill_level_names(i, 0)) ) {
3772 if ( Game_skill_level >= i ){
3780 // return 0 if not found!!!
3784 int sexp_was_promotion_granted(int n)
3786 if (Player->flags & PLAYER_FLAGS_PROMOTED)
3792 int sexp_was_medal_granted(int n)
3798 if (Player->stats.m_medal_earned >= 0)
3804 medal_name = CTEXT(n);
3805 for (i=0; i<NUM_MEDALS; i++) {
3806 if (!stricmp(medal_name, Medals[i].name))
3810 if ( (i < NUM_MEDALS) && (Player->stats.m_medal_earned == i) )
3816 // function which returns true if the percentage of ships (and ships in wings) departed is at
3817 // least the percentage given. what determine if we should check destroyed or departed status
3818 int sexp_percent_ships_depart_destroy(int n, int what)
3824 percent = atoi(CTEXT(n));
3828 // iterate through the rest of the ships/wings in the list and tally the departures and the
3830 for ( n = CDR(n); n != -1; n = CDR(n) ) {
3835 wingnum = wing_name_lookup( name, 1 );
3836 if ( wingnum != -1 ) {
3837 // for wings, we can increment the total by the total number of ships that we expect for
3838 // this wing, and the departures by the number of departures stored for this wing
3839 total += (Wings[wingnum].wave_count * Wings[wingnum].num_waves);
3840 if ( what == OP_PERCENT_SHIPS_DEPARTED )
3841 count += Wings[wingnum].total_departed;
3842 else if ( what == OP_PERCENT_SHIPS_DESTROYED )
3843 count += Wings[wingnum].total_destroyed;
3845 Int3(); // this would be very bogus!
3847 // must be a ship, so increment the total by 1, then determine if this ship has departed
3849 if ( what == OP_PERCENT_SHIPS_DEPARTED ) {
3850 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) )
3852 } else if ( what == OP_PERCENT_SHIPS_DESTROYED ) {
3853 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3856 Int3(); // this would be very bogus as well.
3861 // now, look at the percentage
3862 if ( ((count * 100) / total) >= percent )
3863 return SEXP_KNOWN_TRUE;
3868 // function to tell is a list of ships has departed from within a radius of a given jump node.
3869 // returns true N seconds after the list of ships have departed
3870 int sexp_depart_node_delay( int n )
3872 int delay, count, num_departed;
3873 char *jump_node_name, *name;
3874 fix latest_time, this_time;
3876 delay = atoi( CTEXT(n) );
3878 jump_node_name = CTEXT(n);
3880 // iterate through the list of ships
3889 if (sexp_query_has_yet_to_arrive(name))
3890 return SEXP_CANT_EVAL;
3892 // if ship/wing destroyed, sexpression is known false. Also, if there is no departure log entry, then
3893 // the sexpression is not true.
3894 if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
3895 return SEXP_KNOWN_FALSE;
3896 else if ( mission_log_get_time(LOG_SHIP_DEPART, name, jump_node_name, &this_time) ) {
3898 if ( this_time > latest_time )
3899 latest_time = this_time;
3904 if ( (count == num_departed) && ((Missiontime - latest_time) >= delay) )
3905 return SEXP_KNOWN_TRUE;
3910 // sexpression which returns true when the listed ships/wings have all been destroyed or
3912 int sexp_destroyed_departed_delay( int n )
3915 fix delay, latest_time;
3919 delay = i2f(atoi(CTEXT(n)));
3922 count = 0; // number destroyed or departed
3923 total = 0; // total number of ships/wings to check
3932 // for wings, check the WF_GONE flag to see if there are no more ships in this wing to arrive.
3933 wingnum = wing_name_lookup(name, 1);
3934 if ( wingnum != -1 ) {
3935 if ( Wings[wingnum].flags & WF_WING_GONE ) {
3936 // be sure to get the latest time of one of these
3937 if ( Wings[wingnum].time_gone > latest_time ){
3938 time_gone = Wings[wingnum].time_gone;
3942 } else if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, &time_gone) ) {
3944 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, &time_gone) ) {
3948 // check our latest time
3949 if ( time_gone > latest_time ){
3950 latest_time = time_gone;
3956 if ( (count == total) && (Missiontime > (latest_time + delay)) )
3957 return SEXP_KNOWN_TRUE;
3962 int sexp_special_warpout_name( int node )
3964 int shipnum, knossos_num;
3965 char *ship_name, *knossos;
3967 ship_name = CTEXT(node);
3968 knossos = CTEXT(CDR(node));
3970 // check to see if either ship was destroyed or departed. If so, then make this node known
3972 if ( mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, ship_name, NULL, NULL) ||
3973 mission_log_get_time(LOG_SHIP_DESTROYED, knossos, NULL, NULL) || mission_log_get_time( LOG_SHIP_DEPART, knossos, NULL, NULL) )
3974 return SEXP_NAN_FOREVER;
3977 shipnum = ship_name_lookup(ship_name);
3983 knossos_num = ship_name_lookup(knossos);
3984 if (knossos_num < 0) {
3988 // set special warpout objnum
3989 Ships[shipnum].special_warp_objnum = knossos_num;
3994 // function which determines if N seconds have elpased since all discovery of all cargo
3996 int sexp_is_cargo_known( int n, int check_delay )
3998 int count, shipnum, num_known, delay;
4006 // get the delay value (if there is one)
4008 if ( check_delay ) {
4009 delay = atoi(CTEXT(n) );
4021 // see if we have already checked this entry
4022 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4029 // see if the ship has already exited the mission (either through departure or destruction). If so,
4030 // grab the status of whether the cargo is known from this list
4031 exited_index = ship_find_exited_ship_by_name( name );
4032 if (exited_index != -1 ) {
4033 if ( !(Ships_exited[exited_index].flags & SEF_CARGO_KNOWN) )
4034 return SEXP_KNOWN_FALSE;
4036 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4037 // but won't cause huge delays.
4038 time_known = Missiontime - Ships_exited[exited_index].time;
4039 if ( f2i(time_known) >= delay )
4043 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4044 // is yet to arrive.
4045 shipnum = ship_name_lookup( name );
4046 if ( shipnum != -1 ) {
4047 if ( Ships[shipnum].flags & SF_CARGO_REVEALED ) {
4048 time_known = Missiontime - Ships[shipnum].time_cargo_revealed;
4049 if ( f2i(time_known) >= delay )
4056 // if cargo is known, mark our variable and this sexpression.
4059 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4065 Directive_count += count - num_known;
4066 if ( count == num_known )
4067 return SEXP_KNOWN_TRUE;
4072 int sexp_has_been_tagged_delay(int n)
4074 int count, shipnum, num_known, delay;
4082 // get the delay value
4083 delay = atoi(CTEXT(n) );
4095 // see if we have already checked this entry
4096 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4103 // see if the ship has already exited the mission (either through departure or destruction). If so,
4104 // grab the status of whether the cargo is known from this list
4105 exited_index = ship_find_exited_ship_by_name( name );
4106 if (exited_index != -1 ) {
4107 if ( !(Ships_exited[exited_index].flags & SEF_BEEN_TAGGED) )
4108 return SEXP_KNOWN_FALSE;
4110 // check the delay of when we found out. We use the ship died time which isn't entirely accurate
4111 // but won't cause huge delays.
4112 time_known = Missiontime - Ships_exited[exited_index].time;
4113 if ( f2i(time_known) >= delay )
4117 // otherwise, ship should still be in the mission. If ship_name_lookup returns -1, then ship
4118 // is yet to arrive.
4119 shipnum = ship_name_lookup( name );
4120 if ( shipnum != -1 ) {
4121 if ( Ships[shipnum].time_first_tagged != 0 ) {
4122 time_known = Missiontime - Ships[shipnum].time_first_tagged;
4123 if ( f2i(time_known) >= delay )
4130 // if cargo is known, mark our variable and this sexpression.
4133 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4139 Directive_count += count - num_known;
4140 if ( count == num_known )
4141 return SEXP_KNOWN_TRUE;
4146 int sexp_cap_subsys_cargo_known_delay(int n)
4148 int delay, count, delta_time, num_known;
4149 char *ship_name, *subsys_name;
4155 delay = atoi(CTEXT(n));
4159 ship_name = CTEXT(n);
4171 // see if we have already checked this entry
4172 if ( Sexp_nodes[n].value == SEXP_KNOWN_TRUE ) {
4176 subsys_name = CTEXT(n);
4178 logged = mission_log_get_time(LOG_CAP_SUBSYS_CARGO_REVEALED, ship_name, subsys_name, &time_known);
4180 delta_time = f2i(Missiontime - time_known);
4181 if (delta_time >= delay) {
4186 // if (exited or destroyed) and not logged, known false
4187 // otherwise, still out there and cargo not yet known
4188 if ( (mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL ) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL )) && !logged ) {
4189 return SEXP_KNOWN_FALSE;
4195 Sexp_nodes[n].value = SEXP_KNOWN_TRUE;
4201 Directive_count += count - num_known;
4202 if ( count == num_known )
4203 return SEXP_KNOWN_TRUE;
4209 // return object index of waypoint or -1 if no such waypoint
4210 int waypoint_lookup(char *name)
4216 ptr = GET_FIRST(&obj_used_list);
4217 while (ptr != END_OF_LIST(&obj_used_list)) {
4218 if (ptr->type == OBJ_WAYPOINT) {
4220 sprintf(buf, "%s:%d", Waypoint_lists[i / 65536].name, (i & 0xffff) + 1);
4221 if ( !stricmp(buf, name) )
4222 return OBJ_INDEX(ptr);
4225 ptr = GET_NEXT(ptr);
4231 // conditional sexpressions follow
4233 // eval_when evaluates the when conditional
4234 int eval_when(int n)
4238 Assert( n >= 0 ); // must have valid sexp index
4241 val = eval_sexp(cond); // get the value of the the conditional
4242 if ( val ) { // if the value is true, perform the actions is the 'then' part of the if
4246 while ( actions != -1 ) {
4249 val = eval_sexp(exp); // these sexp evaled only for side effects
4250 actions = CDR(actions);
4254 if (Sexp_nodes[cond].value == SEXP_KNOWN_FALSE)
4255 return SEXP_KNOWN_FALSE; // no need to waste time on this anymore
4257 if (val == SEXP_KNOWN_FALSE)
4258 return 0; // can't return known false, as this would bypass future actions under the when
4263 // eval_cond() evaluates the cond conditional
4264 int eval_cond( int n )
4266 int cond = 0, node, val = 0;
4272 val = eval_sexp(cond);
4274 // if the conditional evaluated to true, then we must evaluate the rest of the expression returning
4275 // the value of this evaluation
4280 actions = CDR(node);
4281 while (actions >= 0) {
4284 val = eval_sexp(exp); // these sexp evaled only for side effects
4286 actions = CDR(actions);
4292 // move onto the next cond clause
4299 int sexp_is_iff( int n )
4301 char *ship_name, *iff;
4306 // iff value is the first parameter, second is a list of one or more ships to check to see if the
4307 // iff value matches
4309 if ( !stricmp(iff, "friendly") )
4310 team = TEAM_FRIENDLY;
4311 else if ( !stricmp(iff, "hostile") )
4312 team = TEAM_HOSTILE;
4313 else if ( !stricmp(iff, "neutral") )
4314 team = TEAM_NEUTRAL;
4315 else if ( !stricmp(iff, "unknown") )
4316 team = TEAM_UNKNOWN;
4317 else if ( !stricmp(iff, "traitor") )
4318 team = TEAM_TRAITOR;
4321 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", iff));
4322 team = TEAM_HOSTILE;
4327 ship_name = CTEXT(n);
4328 // find the ship and check to be sure that it is still around.
4329 num = ship_name_lookup(ship_name);
4330 if ( num < 0 ) // if the ship is gone, can't check it's iff.
4333 // if the team doesn't match the team specified, return 0 immediately
4334 if ( Ships[num].team != team)
4343 void sexp_change_iff( int n )
4345 char *ship_name, *new_iff;
4350 if ( !stricmp(new_iff, "friendly") )
4351 new_team = TEAM_FRIENDLY;
4352 else if ( !stricmp(new_iff, "hostile") )
4353 new_team = TEAM_HOSTILE;
4354 else if ( !stricmp(new_iff, "neutral") )
4355 new_team = TEAM_NEUTRAL;
4356 else if ( !stricmp(new_iff, "unknown") )
4357 new_team = TEAM_UNKNOWN;
4358 else if ( !stricmp(new_iff, "traitor") )
4359 new_team = TEAM_TRAITOR;
4361 mprintf(("Warning: Team %s no longer supported. Just Friendly and Hostile.\n", new_iff));
4362 new_team = TEAM_HOSTILE;
4368 ship_name = CTEXT(n);
4370 // find the ship and check to be sure that it is still around.
4371 num = ship_name_lookup(ship_name);
4372 if ( num >= 0 ) { // only change iff if we found the ship
4373 Ships[num].team = new_team;
4375 // send a network packet if we need to
4376 if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Ships[num].objnum >= 0)){
4377 send_change_iff_packet(Objects[Ships[num].objnum].net_signature, new_team);
4386 // following routine adds an ai goal to a ship structure. The sexpression index
4387 // passed in should be an ai-goal of the proper form. The code in MissionGoal should
4388 // check the syntax.
4390 void sexp_add_ship_goal( int n )
4396 ship_name = CTEXT(n);
4397 num = ship_name_lookup(ship_name);
4398 if ( num < 0 ) // ship not around anymore???? then forget it!
4402 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4405 // identical to above, except add a wing
4406 void sexp_add_wing_goal( int n )
4412 wing_name = CTEXT(n);
4413 num = wing_name_lookup(wing_name);
4414 if ( num < 0 ) // ship not around anymore???? then forget it!
4418 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4421 // sexp_add_goal adds a goal to the specified entiry (ships and wings have unique names between
4423 void sexp_add_goal( int n )
4432 // first, look for ship name -- if found, then add ship goal. else look for wing name -- if
4433 // found, add wing goal
4434 if ( (num = ship_name_lookup(name)) != -1 )
4435 ai_add_ship_goal_sexp( sindex, AIG_TYPE_EVENT_SHIP, &(Ai_info[Ships[num].ai_index]) );
4436 else if ( (num = wing_name_lookup(name)) != -1 )
4437 ai_add_wing_goal_sexp( sindex, AIG_TYPE_EVENT_WING, num );
4440 // clears out all ai goals for a ship
4441 void sexp_clear_ship_goals( int n )
4447 ship_name = CTEXT(n);
4448 num = ship_name_lookup(ship_name);
4449 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4452 // clears out ai goals for a wing
4453 void sexp_clear_wing_goals( int n )
4459 wing_name = CTEXT(n);
4460 num = wing_name_lookup(wing_name);
4463 ai_clear_wing_goals( num );
4466 // this function clears all ai goals for the given ship or wing
4467 void sexp_clear_goals( int n )
4475 if ( (num = ship_name_lookup(name)) != -1 )
4476 ai_clear_ship_goals( &(Ai_info[Ships[num].ai_index]) );
4477 else if ( (num = wing_name_lookup(name)) != -1 )
4478 ai_clear_wing_goals( num );
4484 // this function get called by send-message or send-message random with the name of the message, sender,
4486 void sexp_send_one_message( char *name, char *who_from, char *priority, int group, int delay )
4488 int ipriority, num, ship_index, source;
4495 // determine the priority of the message
4496 if ( !stricmp(priority, "low") )
4497 ipriority = MESSAGE_PRIORITY_LOW;
4498 else if ( !stricmp(priority, "normal") )
4499 ipriority = MESSAGE_PRIORITY_NORMAL;
4500 else if ( !stricmp(priority, "high") )
4501 ipriority = MESSAGE_PRIORITY_HIGH;
4504 ipriority = MESSAGE_PRIORITY_NORMAL;
4507 // check to see if the 'who_from' string is a ship that had been destroyed or departed. If so,
4508 // then don't send the message. We must look at 'who_from' to determine what to look for. who_from
4509 // may be any allied person, any wingman, a wingman from a specific wing, or a specific ship
4512 source = MESSAGE_SOURCE_COMMAND;
4513 if ( who_from[0] == '#' ) {
4514 message_send_unique_to_player( name, &(who_from[1]), MESSAGE_SOURCE_SPECIAL, ipriority, group, delay );
4516 } else if (!stricmp(who_from, "<any allied>")) {
4517 //Int3(); // no longer supported
4519 } else if ( (num = wing_name_lookup(who_from)) != -1 ) {
4520 // message from a wing
4521 // this will be an invalid case soon
4523 // choose wing leader to speak for wing (hence "1" at end of ship_get_random_ship_in_wing)
4524 ship_index = ship_get_random_ship_in_wing( num, SHIP_GET_NO_PLAYERS, 1 );
4525 if ( ship_index == -1 ) {
4526 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4530 } else if ( mission_log_get_time(LOG_SHIP_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_SHIP_DEPART, who_from, NULL, NULL)
4531 || mission_log_get_time(LOG_WING_DESTROYED, who_from, NULL, NULL) || mission_log_get_time(LOG_WING_DEPART, who_from, NULL, NULL) ) {
4532 // getting into this if statement means that the ship or wing (sender) is no longer in the mission
4533 // if message is high priority, make it come from Terran Command
4534 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4537 source = MESSAGE_SOURCE_COMMAND;
4539 } else if ( !stricmp(who_from, "<any wingman>") || (wing_name_lookup(who_from) != -1) ) {
4540 source = MESSAGE_SOURCE_WINGMAN;
4542 // Message from a apecific ship
4543 // bail if not high priority, otherwise reroute to command
4544 source = MESSAGE_SOURCE_SHIP;
4545 ship_index = ship_name_lookup(who_from);
4546 if ( ship_index == -1 ) {
4547 if ( ipriority != MESSAGE_PRIORITY_HIGH )
4549 source = MESSAGE_SOURCE_COMMAND;
4553 if ( ship_index == -1 ){
4556 shipp = &Ships[ship_index];
4559 message_send_unique_to_player( name, shipp, source, ipriority, group, delay );
4562 void sexp_send_message( int n )
4564 char *name, *who_from, *priority, *tmp;
4571 who_from = CTEXT(n);
4572 priority = CTEXT(CDR(n));
4573 name = CTEXT(CDR(CDR(n)));
4575 // a temporary check to see if the name field matched a priority since I am in the process
4576 // of reordering the arguments
4577 if ( !stricmp(name, "low") || !stricmp(name, "normal") || !stricmp(name, "high") ) {
4583 sexp_send_one_message( name, who_from, priority, 0, 0 );
4586 void sexp_send_message_list( int n )
4588 char *name, *who_from, *priority;
4595 // send a bunch of messages
4598 who_from = CTEXT(n);
4603 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4606 priority = CTEXT(n);
4611 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4619 Warning(LOCATION, "Detected incomplete parameter list in sexp-send-message-list");
4622 delay += atoi(CTEXT(n));
4625 sexp_send_one_message(name, who_from, priority, 1, delay);
4632 void sexp_send_random_message( int n )
4634 char *name, *who_from, *priority;
4635 int temp, num_messages, message_num;
4638 who_from = CTEXT(n);
4639 priority = CTEXT(CDR(n));
4645 // count the number of messages that we have
4653 Assert ( num_messages >= 1 );
4655 // get a random message, and pass the parameters to send_one_message
4656 message_num = myrand() % num_messages;
4659 if ( message_num == 0 )
4664 Assert (n != -1); // should have found the message!!!
4667 sexp_send_one_message( name, who_from, priority, 0, 0 );
4670 void sexp_self_destruct( int n )
4676 // get the ship name and be sure that it is still in the mission. Destroy it if we find it
4677 ship_name = CTEXT(n);
4678 shipnum = ship_name_lookup( ship_name );
4679 if ( shipnum == -1 )
4681 ship_self_destruct( &Objects[Ships[shipnum].objnum] );
4687 void sexp_next_mission( int n )
4692 mission_name = CTEXT(n);
4693 for (i = 0; i < Campaign.num_missions; i++) {
4694 if ( !stricmp(Campaign.missions[i].name, mission_name) ) {
4695 Campaign.next_mission = i;
4699 Error(LOCATION, "Mission name %s not found in campaign file for next-mission command", mission_name);
4702 // function to deal with the end-of-campaign sexpression.
4703 void sexp_end_of_campaign( int n )
4705 // this is really a do-nothing sexpression. It is pretty much a placeholder to allow
4706 // campaigns to have repeat-mission branches at the end of the campaign. By not setting
4707 // anything in this function, the higher level campaign code will see this as end-of-campaign
4708 // since next_mission isn't set to anything. (To be safe, we'll set to -1).
4709 Campaign.next_mission = -1;
4712 // sexpression to end everything. One parameter is the movie to play when this is over.
4713 void sexp_end_campaign( int n )
4715 // post and event to move us to the end-of-campaign state. There we will play a movie, then
4716 // go to debriefing.
4718 // needed to actually end the game
4719 gameseq_post_event( GS_EVENT_END_CAMPAIGN );
4721 // in FS2 our ending is a bit wacky. we'll just flag the mission as having ended the campaign
4722 Campaign_ended_in_mission = 1;
4726 // sabotage subsystem reduces the strength of a subsystem by the given percentage. If it is reduced to
4727 // below 0%, then the hits of the subsystem are set to 0
4728 void sexp_sabotage_subsystem( int n )
4730 char *shipname, *subsystem;
4731 int percentage, shipnum, index;
4732 float sabotage_hits;
4736 shipname = CTEXT(n);
4737 subsystem = CTEXT(CDR(n));
4738 percentage = atoi(CTEXT(CDR(CDR(n))));
4740 shipnum = ship_name_lookup(shipname);
4742 // if no ship, then return immediately.
4743 if ( shipnum == -1 )
4745 shipp = &Ships[shipnum];
4747 // see if we are dealing with the HULL
4748 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4752 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4753 sabotage_hits = ihs * ((float)percentage / 100.0f);
4754 objp = &Objects[shipp->objnum];
4755 objp->hull_strength -= sabotage_hits;
4757 // self destruct the ship if <= 0.
4758 if ( objp->hull_strength <= 0.0f )
4759 ship_self_destruct( objp );
4763 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4764 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4766 index = ship_get_subsys_index(shipp, subsystem);
4767 if ( index == -1 ) {
4768 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for sabotage subsystem\n", subsystem, shipp->ship_name));
4772 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4773 // set the strength to the given percentage if current strength is > given percentage
4774 ss = ship_get_indexed_subsys( shipp, index );
4775 sabotage_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4776 ss->current_hits -= sabotage_hits;
4777 if ( ss->current_hits < 0.0f )
4778 ss->current_hits = 0.0f;
4779 ship_recalc_subsys_strength( shipp );
4782 // repair_subsystem adds some percentage of hits to a subsystem. Anything repaired about 100% is
4784 void sexp_repair_subsystem( int n )
4786 char *shipname, *subsystem;
4787 int percentage, shipnum, index;
4792 shipname = CTEXT(n);
4793 subsystem = CTEXT(CDR(n));
4794 shipnum = ship_name_lookup(shipname);
4796 // if no ship, then return immediately.
4797 if ( shipnum == -1 ) {
4800 shipp = &Ships[shipnum];
4802 // check if we've got a number or an op
4803 if ( CAR(CDR(CDR(n))) != -1) {
4804 percentage = eval_sexp( CAR(CDR(CDR(n))) );
4806 percentage = atoi(CTEXT(CDR(CDR(n))));
4809 // see if we are dealing with the HULL
4810 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4814 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4815 repair_hits = ihs * ((float)percentage / 100.0f);
4816 objp = &Objects[shipp->objnum];
4817 objp->hull_strength += repair_hits;
4818 if ( objp->hull_strength > ihs )
4819 objp->hull_strength = ihs;
4823 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4824 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4826 index = ship_get_subsys_index(shipp, subsystem);
4827 if ( index == -1 ) {
4828 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4832 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4833 // set the strength to the given percentage if current strength is < given percentage
4834 ss = ship_get_indexed_subsys( shipp, index );
4835 repair_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4836 ss->current_hits += repair_hits;
4837 if ( ss->current_hits > ss->system_info->max_hits )
4838 ss->current_hits = ss->system_info->max_hits;
4839 ship_recalc_subsys_strength( shipp );
4842 // sexpression code to set a subsystem of a ship at a specific percentage
4843 void sexp_set_subsystem_strength( int n )
4845 char *shipname, *subsystem;
4846 int percentage, shipnum, index;
4850 shipname = CTEXT(n);
4851 subsystem = CTEXT(CDR(n));
4852 percentage = num_eval(CDR(CDR(n)));
4854 shipnum = ship_name_lookup(shipname);
4856 // if no ship, then return immediately.
4857 if ( shipnum == -1 )
4859 shipp = &Ships[shipnum];
4861 if ( percentage > 100 ) {
4862 nprintf(("Warning", "percentage for set_subsystem_strength > 100 -- setting to 100\n"));
4864 } else if ( percentage < 0 ) {
4865 nprintf(("Werning", "percantage for set_subsystem_strength < 0 -- setting to 0\n"));
4869 // see if we are dealing with the HULL
4870 if ( !stricmp( subsystem, SEXP_HULL_STRING) ) {
4874 objp = &Objects[shipp->objnum];
4876 // destroy the ship if percentage is 0
4877 if ( percentage == 0 ) {
4878 ship_self_destruct( objp );
4880 ihs = Ship_info[shipp->ship_info_index].initial_hull_strength;
4881 objp->hull_strength = ihs * ((float)percentage / 100.0f);
4887 // now find the given subsystem on the ship. The subsystem should be an actual subsystem name
4888 // and not a generic type (generic type meaning SUBSYSTEM_ENGINE, etc).
4890 index = ship_get_subsys_index(shipp, subsystem);
4891 if ( index == -1 ) {
4892 nprintf(("Warning", "Couldn't find subsystem %s on ship %s for repair subsystem\n", subsystem, shipp->ship_name));
4896 // get the pointer to the subsystem. Check it's current hits against it's max hits, and
4897 // set the strength to the given percentage if current strength is < given percentage
4898 ss = ship_get_indexed_subsys( shipp, index );
4900 // maybe blow up subsys
4901 if (ss->current_hits > 0) {
4902 if (percentage < 1) {
4903 do_subobj_destroyed_stuff(shipp, ss, NULL);
4908 ss->current_hits = ss->system_info->max_hits * ((float)percentage / 100.0f);
4910 ship_recalc_subsys_strength( shipp );
4913 // function which changes the validity of a goal. The flag paramater tells us whether to mark the goals
4914 // as valid or invalid
4915 void sexp_change_goal_validity( int n, int flag )
4922 mission_goal_mark_valid( name );
4924 mission_goal_mark_invalid( name );
4930 // function to transfer cargo from one ship to another
4931 void sexp_transfer_cargo( int n )
4933 char *shipname1, *shipname2;
4934 int shipnum1, shipnum2, i;
4937 shipname1 = CTEXT(n);
4938 shipname2 = CTEXT(CDR(n));
4940 // find the ships -- if neither in the mission, the abort
4941 shipnum1 = ship_name_lookup(shipname1);
4942 shipnum2 = ship_name_lookup(shipname2);
4943 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4946 // we must be sure that these two objects are indeed docked
4947 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
4948 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
4949 Int3(); // you are trying to transfer cargo between two ships not docked
4953 if ( !stricmp(Cargo_names[Ships[shipnum1].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4954 Int3(); // you are transfering no cargo!!!!
4958 // transfer cargo from ship1 to ship2
4960 // Don't give warning for large ships (cruiser on up)
4961 if (! (Ship_info[Ships[shipnum2].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
4962 if ( stricmp(Cargo_names[Ships[shipnum2].cargo1 & CARGO_INDEX_MASK], "nothing") ) {
4963 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] );
4967 Ships[shipnum2].cargo1 = char(Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
4969 if ( !(Ships[shipnum1].cargo1 & CARGO_NO_DEPLETE) ) {
4970 // need to set ship1's cargo to nothing. scan the cargo_names array looking for the string nothing.
4971 // add it if not found
4972 for (i = 0; i < Num_cargo; i++ ) {
4973 if ( !stricmp(Cargo_names[i], "nothing") ) {
4974 Ships[shipnum1].cargo1 = char(i);
4978 strcpy(Cargo_names[i], "Nothing");
4983 // this function exchanges cargo between two ships
4984 void sexp_exchange_cargo( int n )
4986 char *shipname1, *shipname2;
4987 int shipnum1, shipnum2, temp;
4990 shipname1 = CTEXT(n);
4991 shipname2 = CTEXT(CDR(n));
4993 // find the ships -- if neither in the mission, the abort
4994 shipnum1 = ship_name_lookup(shipname1);
4995 shipnum2 = ship_name_lookup(shipname2);
4996 if ( (shipnum1 == -1) || (shipnum2 == -1) )
4999 // we must be sure that these two objects are indeed docked
5000 objp = ai_find_docked_object( &Objects[Ships[shipnum1].objnum] );
5001 if ( objp != &Objects[Ships[shipnum2].objnum] ) {
5002 Int3(); // you are trying to transfer cargo between two ships not docked
5006 temp = (Ships[shipnum1].cargo1 & CARGO_INDEX_MASK);
5007 Ships[shipnum1].cargo1 = char(Ships[shipnum2].cargo1 & CARGO_INDEX_MASK);
5008 Ships[shipnum2].cargo1 = char(temp);
5011 void sexp_cap_waypont_speed(int n)
5017 shipname = CTEXT(n);
5018 speed = atoi(CTEXT(CDR(n)));
5020 shipnum = ship_name_lookup(shipname);
5022 if (shipnum == -1) {
5023 Int3(); // trying to set waypoint speed of ship not already in game
5027 // cap speed to range (-1, 127) to store within char
5036 Ai_info[Ships[shipnum].ai_index].waypoint_speed_cap = (char) speed;
5039 // this function causes a ship to jettison its cargo
5040 void sexp_jettison_cargo( int n )
5043 int jettison_delay, ship_index;
5046 shipname = CTEXT(n);
5047 jettison_delay = atoi(CTEXT(CDR(n)));
5050 ship_index = ship_name_lookup(shipname);
5056 ship_jettison_cargo(&Ships[ship_index]);
5059 void sexp_cargo_no_deplete( int n )
5062 int ship_index, no_deplete = 1;
5065 shipname = CTEXT(n);
5068 ship_index = ship_name_lookup(shipname);
5073 if ( !(Ship_info[Ships[ship_index].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) ) {
5074 Warning(LOCATION, "Trying to make non BIG or HUGE ship %s with non-depletable cargo.\n", Ships[ship_index].ship_name);
5079 no_deplete = atoi(CTEXT(CDR(n)));
5080 Assert((no_deplete == 0) || (no_deplete == 1));
5081 if ( (no_deplete != 0) && (no_deplete != 1) ) {
5087 Ships[ship_index].cargo1 |= CARGO_NO_DEPLETE;
5089 Ships[ship_index].cargo1 &= (~CARGO_NO_DEPLETE);
5094 // sexpression to end the mission after N seconds!
5095 void sexp_end_mission_delay( int n )
5099 //delay = atoi(CTEXT(n));
5100 //mission_parse_set_end_time( delay );
5101 mprintf(("Not ending mission -- end-mission sexpression no longer works!\n"));
5104 // funciton to toggle the status bit for the AI code which tells the AI if it is a good time
5105 // to rearm. The status being set means good time. Status not being set (unset), means bad time.
5106 // designers must implement this.
5107 void sexp_good_time_to_rearm( int n )
5112 team_name = CTEXT(n);
5113 time = atoi(CTEXT(CDR(n))); // this is the time for how long a good rearm is active -- in seconds
5114 for ( i = 0; i < Num_team_names; i++ ) {
5115 if ( !stricmp(team_name, Team_names[i]) ) {
5119 ai_set_rearm_status( team, time );
5124 // function which grants promotion to the player
5125 void sexp_grant_promotion()
5127 // short circuit multiplayer for now until we figure out what to do.
5128 if ( Game_mode & GM_MULTIPLAYER )
5131 // set a bit to tell player should get promoted at the end of the mission. I suppose the other
5132 // thing that we could do would be to set the players score to at least the amount of
5133 // points for the next level, but this way is better I think.
5134 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5135 Player->flags |= PLAYER_FLAGS_PROMOTED;
5139 // function which gives the named medal to the players in the mission
5140 void sexp_grant_medal( int n )
5145 // don't give medals in normal gameplay when not in campaign mode
5146 if ( (Game_mode & GM_NORMAL) && !(Game_mode & GM_CAMPAIGN_MODE) )
5149 Assert(Player->stats.m_medal_earned < 0); // Mission has problems. Tried to grant 2 medals in 1 mission.
5150 medal_name = CTEXT(n);
5151 for (i = 0; i < NUM_MEDALS; i++ ) {
5152 if ( !stricmp(medal_name, Medals[i].name) )
5156 if ( i < NUM_MEDALS ) {
5157 Player->stats.m_medal_earned = i;
5158 if ( Game_mode & GM_MULTIPLAYER ) {
5159 for ( j = 0; j < MAX_PLAYERS; j++ ) {
5160 if ( MULTI_CONNECTED(Net_players[j]) ) {
5161 Net_players[j].player->stats.m_medal_earned = i;
5168 void sexp_tech_add_ship(int node)
5174 // this function doesn't mean anything when not in campaign mode
5175 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5180 i = ship_info_lookup(name);
5182 Ship_info[i].flags |= SIF_IN_TECH_DATABASE;
5184 Error(LOCATION, "Ship class \"%s\" invalid", name);
5190 void sexp_tech_add_weapon(int node)
5196 // this function doesn't mean anything when not in campaign mode
5197 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5202 i = weapon_info_lookup(name);
5204 Weapon_info[i].wi_flags |= WIF_IN_TECH_DATABASE;
5206 Error(LOCATION, "Ship class \"%s\" invalid", name);
5212 // function to set variables needed to grant a new ship/weapon to the player during the course
5214 void sexp_allow_ship( int n )
5219 // this function doesn't mean anything when not in campaign mode
5220 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5223 // get the name of the ship and lookup up the ship_info index for it
5225 sindex = ship_info_lookup( name );
5229 // now we have a valid index --
5230 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_SHIP, sindex );
5233 void sexp_allow_weapon( int n )
5238 // this function doesn't mean anything when not in campaign mode
5239 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
5242 // get the name of the weapon and lookup up the weapon_info index for it
5244 sindex = weapon_info_lookup( name );
5248 // now we have a valid index --
5249 mission_campaign_save_persistent( CAMPAIGN_PERSISTENT_WEAPON, sindex );
5252 // functions to deal with breaking/fixing the warp engines on ships/wings. should_break is true when
5253 // we are breaking the warp drive (can be repaired). The parameter is 0 when is getting broken (i.e.
5254 // can be fixed by repair). The repair parameter tells us whether we are clearing the destroyed or broken
5255 // flag (1), or setting them (0).
5256 void sexp_deal_with_warp( int n, int should_break, int nix )
5261 for ( ; n != -1; n = CDR(n) ) {
5263 index = ship_name_lookup(name);
5265 // check to see if ship destroyed or departed. In either case, do nothing.
5266 if ( mission_log_get_time(LOG_SHIP_DEPART, name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, name, NULL, NULL) )
5269 // we can only operate on ships which are in the mission
5270 if ( index != -1 ) {
5272 // set the flag value accoring to whether we are destroying the warp or just breaking it
5274 flag = SF_WARP_BROKEN;
5276 flag = SF_WARP_NEVER;
5279 Ships[index].flags |= flag;
5281 Ships[index].flags &= ~flag;
5284 // maybe this ship has yet to arrive. Get a possible parse object and set the flag if found
5287 pobjp = mission_parse_get_arrival_ship( name );
5289 if ( pobjp == NULL ) {
5290 Int3(); // warning, find allender -- should be impossible
5295 flag = P_SF_WARP_BROKEN;
5297 flag = P_SF_WARP_BROKEN;
5300 pobjp->flags |= flag;
5302 pobjp->flags &= ~flag;
5308 // function which is used to tell the AI when it is okay to fire certain secondary
5309 // weapons at other ships.
5310 void sexp_good_secondary_time( int n )
5312 char *team_name, *weapon_name, *ship_name;
5313 int num_weapons, weapon_index, team, i;
5315 team_name = CTEXT(n);
5316 num_weapons = atoi(CTEXT(CDR(n)));
5317 weapon_name = CTEXT(CDR(CDR(n)));
5318 ship_name = CTEXT(CDR(CDR(CDR(n))));
5320 weapon_index = weapon_info_lookup(weapon_name);
5321 if ( weapon_index == -1 ) {
5322 nprintf(("Warning", "couldn't find weapon %s for good-secondary-time\n", weapon_name));
5326 // get the team type from the team_name
5327 for ( i = 0; i < Num_team_names; i++ ) {
5328 if ( !stricmp(Team_names[i], team_name) )
5331 if ( i == Num_team_names ) {
5332 nprintf(("Warning", "couldn't find team %s for good-secondary-time\n", team_name ));
5335 team = (1<<i); // this is the magic formula to get to a team type.
5337 // see if the ship has departed or has been destroyed. If so, then we don't need to set up the
5339 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5342 ai_good_secondary_time( team, weapon_index, num_weapons, ship_name );
5346 // function to deal with getting status of goals for previous missions (in the current campaign).
5347 // the status parameter is used to tell this function if we are looking for a goal_satisfied, goal_failed,
5348 // or goal incomplete event
5349 int sexp_previous_goal_status( int n, int status )
5351 char rval = 0, *mission_name;
5353 int i, mission_num, default_value = 0, use_defaults = 1;
5355 mission_name = CTEXT(n);
5356 goal_name = CTEXT(CDR(n));
5358 // check for possible next optional argument
5361 default_value = eval_sexp(n);
5364 // try to find the given mission name in the current list of missions in the campaign.
5365 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5366 i = mission_campaign_find_mission( mission_name );
5369 // if mission not found, assume that goal was false (so previous-goal-false returns true)
5370 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"));
5371 if ( status == GOAL_COMPLETE )
5372 rval = SEXP_KNOWN_FALSE;
5374 rval = SEXP_KNOWN_TRUE;
5377 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5380 // now try and find the goal this mission
5382 for (i = 0; i < Campaign.missions[mission_num].num_goals; i++) {
5383 if ( !stricmp(Campaign.missions[mission_num].goals[i].name, goal_name) )
5387 if ( i == Campaign.missions[mission_num].num_goals ) {
5388 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");
5389 if ( status == GOAL_COMPLETE )
5390 rval = SEXP_KNOWN_FALSE;
5392 rval = SEXP_KNOWN_TRUE;
5395 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5396 if ( Campaign.missions[mission_num].goals[i].status == status )
5397 rval = SEXP_KNOWN_TRUE;
5399 rval = SEXP_KNOWN_FALSE;
5407 // when not in campaign mode, always return KNOWN_TRUE when looking for goal complete, and KNOWN_FALSE
5410 if ( default_value )
5411 rval = SEXP_KNOWN_TRUE;
5413 rval = SEXP_KNOWN_FALSE;
5415 if ( status == GOAL_COMPLETE )
5416 rval = SEXP_KNOWN_TRUE;
5418 rval = SEXP_KNOWN_FALSE;
5425 // sexpression which gets the status of an event from a previous mission. Like the above function but
5426 // dealing with events instead of goals. Again, the status parameter tells the code if we are looking
5427 // for an event_true, event_false, or event_incomplete status
5428 int sexp_previous_event_status( int n, int status )
5430 char rval = 0, *mission_name;
5432 int i, mission_num, default_value = 0, use_defaults = 1;
5434 mission_name = CTEXT(n);
5435 name = CTEXT(CDR(n));
5437 // check for possible optional parameter
5440 default_value = eval_sexp(n);
5443 if ( Game_mode & GM_CAMPAIGN_MODE ) {
5444 // following function returns -1 when mission isn't found.
5445 i = mission_campaign_find_mission( mission_name );
5447 // if the mission name wasn't found -- make this return FALSE for the event status.
5449 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"));
5450 if ( status == EVENT_SATISFIED ) {
5451 rval = SEXP_KNOWN_FALSE;
5453 rval = SEXP_KNOWN_TRUE;
5457 } else if (Campaign.missions[i].flags & CMISSION_FLAG_SKIPPED) {
5460 // now try and find the goal this mission
5462 for (i = 0; i < Campaign.missions[mission_num].num_events; i++) {
5463 if ( !stricmp(Campaign.missions[mission_num].events[i].name, name) )
5467 if ( i == Campaign.missions[mission_num].num_events ) {
5468 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");
5469 if ( status == EVENT_SATISFIED )
5470 rval = SEXP_KNOWN_FALSE;
5472 rval = SEXP_KNOWN_TRUE;
5475 // now return KNOWN_TRUE or KNOWN_FALSE based on the status field in the goal structure
5476 if ( Campaign.missions[mission_num].events[i].status == status )
5477 rval = SEXP_KNOWN_TRUE;
5479 rval = SEXP_KNOWN_FALSE;
5488 if ( default_value )
5489 rval = SEXP_KNOWN_TRUE;
5491 rval = SEXP_KNOWN_FALSE;
5493 if ( status == EVENT_SATISFIED )
5494 rval = SEXP_KNOWN_TRUE;
5496 rval = SEXP_KNOWN_FALSE;
5503 // function to return the status of an event in the current mission. The passed parameter indicates
5504 // if we are checking whether the event is true or the event is false.
5505 int sexp_event_status( int n, int want_true )
5511 for (i = 0; i < Num_mission_events; i++ ) {
5512 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5513 if ( !stricmp(Mission_events[i].name, name) ) {
5514 result = Mission_events[i].result;
5515 if (Mission_events[i].formula < 0) {
5516 if ( (want_true && result) || (!want_true && !result) )
5517 return SEXP_KNOWN_TRUE;
5519 return SEXP_KNOWN_FALSE;
5522 if ( (want_true && result) || (!want_true && !result) )
5533 // function to return the status of an event N seconds after the event is true or false. Similar
5534 // to above function but waits N seconds before returning true
5535 int sexp_event_delay_status( int n, int want_true )
5542 delay = i2f(num_eval(CDR(n)));
5543 for (i = 0; i < Num_mission_events; i++ ) {
5544 // look for the event name, check it's status. If formula is gone, we know the state won't ever change.
5545 if ( !stricmp(Mission_events[i].name, name) ) {
5546 if ( (fix) Mission_events[i].timestamp + delay >= Missiontime )
5549 result = Mission_events[i].result;
5550 if (Mission_events[i].formula < 0) {
5551 if ( (want_true && result) || (!want_true && !result) )
5552 return SEXP_KNOWN_TRUE;
5554 return SEXP_KNOWN_FALSE;
5557 if ( want_true && result ) //) || (!want_true && !result) )
5568 // function which returns true if the given event is still incomplete
5569 int sexp_event_incomplete( int n )
5576 for (i = 0; i < Num_mission_events; i++ ) {
5577 if ( !stricmp(Mission_events[i].name, name ) ) {
5578 // if the formula is still >= 0 (meaning it is still getting eval'ed), then
5579 // the event is incomplete
5580 if ( Mission_events[i].formula != -1 )
5583 return SEXP_KNOWN_FALSE;
5590 // function to return the status of an goal N seconds after the goal is true or false. Similar
5591 // to above function but operates on goals instead of events
5592 int sexp_goal_delay_status( int n, int want_true )
5598 delay = i2f(num_eval(CDR(n)));
5601 // if we are looking for a goal true entry and we find a false, then return known false here
5602 if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, NULL) )
5603 return SEXP_KNOWN_FALSE;
5604 else if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, &time) ) {
5605 if ( (Missiontime - time) >= delay )
5606 return SEXP_KNOWN_TRUE;
5609 // if we are looking for a goal false entry and we find a true, then return known false here
5610 if ( mission_log_get_time(LOG_GOAL_SATISFIED, name, NULL, NULL) )
5611 return SEXP_KNOWN_FALSE;
5612 else if ( mission_log_get_time(LOG_GOAL_FAILED, name, NULL, &time) ) {
5613 if ( (Missiontime - time) >= delay )
5614 return SEXP_KNOWN_TRUE;
5621 // function which returns true if the given goal is still incomplete
5622 int sexp_goal_incomplete( int n )
5628 if ( mission_log_get_time( LOG_GOAL_SATISFIED, name, NULL, NULL) || mission_log_get_time( LOG_GOAL_FAILED, name, NULL, NULL) )
5629 return SEXP_KNOWN_FALSE;
5635 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5636 // or cleared (flag==0)
5637 void sexp_protect_ships( int n, int flag )
5643 ship_name = CTEXT(n);
5645 // check to see if ship destroyed or departed. In either case, do nothing.
5646 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5649 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5650 // in a list until created
5651 num = ship_name_lookup(ship_name);
5654 Objects[Ships[num].objnum].flags |= OF_PROTECTED;
5656 Objects[Ships[num].objnum].flags &= ~OF_PROTECTED;
5658 p_object *parse_obj;
5660 parse_obj = mission_parse_get_arrival_ship( ship_name );
5663 parse_obj->flags |= P_OF_PROTECTED;
5665 parse_obj->flags &= ~P_OF_PROTECTED;
5669 Int3(); // get allender -- could be a potential problem here
5678 // protects/unprotects a ship. The flag tells us whether or not the protect bit should be set (flag==1)
5679 // or cleared (flag==0)
5680 void sexp_beam_protect_ships( int n, int flag )
5686 ship_name = CTEXT(n);
5688 // check to see if ship destroyed or departed. In either case, do nothing.
5689 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5692 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5693 // in a list until created
5694 num = ship_name_lookup(ship_name);
5697 Objects[Ships[num].objnum].flags |= OF_BEAM_PROTECTED;
5699 Objects[Ships[num].objnum].flags &= ~OF_BEAM_PROTECTED;
5701 p_object *parse_obj;
5703 parse_obj = mission_parse_get_arrival_ship( ship_name );
5706 parse_obj->flags |= P_OF_BEAM_PROTECTED;
5708 parse_obj->flags &= ~P_OF_BEAM_PROTECTED;
5712 Int3(); // get allender -- could be a potential problem here
5721 // sexpression to make ships "visible" and "invisible" to sensors. The visible parameter is true
5722 // when making ships visible, false otherwise
5723 void sexp_ships_visible( int n, int visible )
5728 for ( ; n != -1; n = CDR(n) ) {
5729 ship_name = CTEXT(n);
5731 // check to see if ship destroyed or departed. In either case, do nothing.
5732 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5735 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5736 // in a list until created
5737 num = ship_name_lookup(ship_name);
5740 Ships[num].flags |= SF_HIDDEN_FROM_SENSORS;
5742 Ships[num].flags &= ~SF_HIDDEN_FROM_SENSORS;
5743 if (Ships[num].flags & SF_ESCORT) {
5744 // SEND add escort request
5745 hud_add_ship_to_escort(Ships[num].objnum, 1);
5750 p_object *parse_obj;
5752 parse_obj = mission_parse_get_arrival_ship( ship_name );
5755 parse_obj->flags |= P_SF_HIDDEN_FROM_SENSORS;
5757 parse_obj->flags &= ~P_SF_HIDDEN_FROM_SENSORS;
5761 Int3(); // get allender -- could be a potential problem here
5768 // sexpression to toggle invulnerability flag of ships.
5769 void sexp_ships_invulnerable( int n, int invulnerable )
5775 for ( ; n != -1; n = CDR(n) ) {
5776 ship_name = CTEXT(n);
5778 // check to see if ship destroyed or departed. In either case, do nothing.
5779 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5782 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5783 // in a list until created
5784 num = ship_name_lookup(ship_name);
5786 objp = &Objects[Ships[num].objnum];
5788 objp->flags |= OF_INVULNERABLE;
5790 objp->flags &= ~OF_INVULNERABLE;
5792 p_object *parse_obj;
5794 parse_obj = mission_parse_get_arrival_ship( ship_name );
5797 parse_obj->flags |= P_SF_INVULNERABLE;
5799 parse_obj->flags &= ~P_SF_INVULNERABLE;
5803 Int3(); // get allender -- could be a potential problem here
5810 // sexpression to toggle KEEP ALIVE flag of ship object
5811 void sexp_ships_guardian( int n, int guardian )
5817 for ( ; n != -1; n = CDR(n) ) {
5818 ship_name = CTEXT(n);
5820 // check to see if ship destroyed or departed. In either case, do nothing.
5821 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5824 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5825 // in a list until created
5826 num = ship_name_lookup(ship_name);
5828 objp = &Objects[Ships[num].objnum];
5830 objp->flags |= OF_GUARDIAN;
5832 objp->flags &= ~OF_GUARDIAN;
5834 p_object *parse_obj;
5836 parse_obj = mission_parse_get_arrival_ship( ship_name );
5839 parse_obj->flags |= P_SF_GUARDIAN;
5841 parse_obj->flags &= ~P_SF_GUARDIAN;
5845 Int3(); // get allender -- could be a potential problem here
5852 // make ship vanish without a trace (and what its docked to)
5853 void ship_vanished(int);
5854 void sexp_ship_vanish( int n )
5857 object *objp, *docked_objp;
5860 // if MULTIPLAYER bail
5861 if (Game_mode & GM_MULTIPLAYER) {
5865 for ( ; n != -1; n = CDR(n) ) {
5866 ship_name = CTEXT(n);
5868 // check to see if ship destroyed or departed. In either case, do nothing.
5869 if ( mission_log_get_time(LOG_SHIP_DEPART, ship_name, NULL, NULL) || mission_log_get_time(LOG_SHIP_DESTROYED, ship_name, NULL, NULL) )
5872 // get the ship num. If we get a -1 for the number here, ship has yet to arrive. Store this ship
5873 // in a list until created
5874 num = ship_name_lookup(ship_name);
5876 objp = &Objects[Ships[num].objnum];
5879 docked_objp = ai_find_docked_object( objp );
5882 objp->flags |= OF_SHOULD_BE_DEAD;
5889 docked_objp->flags |= OF_SHOULD_BE_DEAD;
5892 ship_vanished(docked_objp->instance);
5898 int sexp_key_pressed(int node)
5903 z = translate_key_to_index(CTEXT(node));
5908 if (!Control_config[z].used){
5916 t = atoi(CTEXT(CDR(node)));
5917 return timestamp_has_time_elapsed(Control_config[z].used, t * 1000);
5920 void sexp_key_reset(int node)
5925 z = translate_key_to_index(CTEXT(node));
5927 Control_config[z].used = 0;
5930 int sexp_targeted(int node)
5935 z = ship_query_state(CTEXT(node));
5937 return SEXP_KNOWN_FALSE; // ship isn't around, nor will it ever be
5938 } else if (z == -1) {
5939 return SEXP_CANT_EVAL;
5942 z = ship_name_lookup(CTEXT(node), 1);
5943 if ((z < 0) || !Player_ai || (Ships[z].objnum != Player_ai->target_objnum)){
5947 if (CDR(node) >= 0) {
5948 z = atoi(CTEXT(CDR(node))) * 1000;
5949 if (!timestamp_has_time_elapsed(Players_target_timestamp, z)){
5953 if (CDR(CDR(node)) >= 0) {
5954 ptr = Player_ai->targeted_subsys;
5955 if (!ptr || stricmp(ptr->system_info->subobj_name, CTEXT(CDR(CDR(node))))){
5964 int sexp_speed(int node)
5966 if (Training_context & TRAINING_CONTEXT_SPEED) {
5967 if (Training_context_speed_set){
5968 if (timestamp_has_time_elapsed(Training_context_speed_timestamp, atoi(CTEXT(node)) * 1000)){
5969 return SEXP_KNOWN_TRUE;
5977 int sexp_secondaries_depleted(int node)
5979 int sindex, num_banks, num_depleted_banks;
5983 sindex = ship_name_lookup(CTEXT(node));
5988 shipp = &Ships[sindex];
5989 if (shipp->objnum < 0) {
5993 // get num secondary banks
5994 num_banks = shipp->weapons.num_secondary_banks;
5995 num_depleted_banks = 0;
5997 // get number of depleted banks
5998 for (int idx=0; idx<num_banks; idx++) {
5999 if (shipp->weapons.secondary_bank_ammo[idx] == 0) {
6000 num_depleted_banks++;
6004 // are they all depleted?
6005 return (num_depleted_banks == num_banks);
6008 int sexp_facing(int node)
6014 if (ship_query_state(CTEXT(node)) < 0){
6015 return SEXP_KNOWN_FALSE;
6018 sh = ship_name_lookup(CTEXT(node));
6019 if ((sh < 0) || !Player_obj){
6023 obj = Ships[sh].objnum;
6024 v1 = Player_obj->orient.v.fvec;
6025 vm_vec_normalize(&v1);
6026 vm_vec_sub(&v2, &Objects[obj].pos, &Player_obj->pos);
6027 vm_vec_normalize(&v2);
6028 a1 = vm_vec_dotprod(&v1, &v2);
6029 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6037 // is ship facing first waypoint in waypoint path
6038 int sexp_facing2(int node)
6044 // bail if Player_obj is not good
6046 return SEXP_CANT_EVAL;
6050 v1 = Player_obj->orient.v.fvec;
6051 vm_vec_normalize(&v1);
6053 // get waypoint name
6054 char *waypoint_name = CTEXT(node);
6056 // get position of first waypoint
6058 for (i=0; i<Num_waypoint_lists; i++) {
6059 if (!stricmp(waypoint_name, Waypoint_lists[i].name)) {
6065 if (wp_index == -1) {
6066 return SEXP_CANT_EVAL;
6069 // Waypoint_lists[wp_index].waypoints[0]
6071 vm_vec_sub(&v2, &Waypoint_lists[wp_index].waypoints[0], &Player_obj->pos);
6072 vm_vec_normalize(&v2);
6073 a1 = vm_vec_dotprod(&v1, &v2);
6074 a2 = (float) cos(ANG_TO_RAD(atof(CTEXT(CDR(node)))));
6082 int sexp_order(int node)
6087 int sexp_waypoint_missed()
6089 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6090 if (Training_context_at_waypoint > Training_context_goal_waypoint){
6098 int sexp_waypoint_twice()
6100 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6101 if (Training_context_at_waypoint < Training_context_goal_waypoint - 1){
6109 int sexp_path_flown()
6111 if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
6112 if (Training_context_goal_waypoint == Waypoint_lists[Training_context_path].count){
6120 void sexp_send_training_message(int node)
6122 int t = -1, delay = 0;
6129 Assert(Event_index >= 0);
6131 if ((CDR(node) >= 0) && (CDR(CDR(node)) >= 0)) {
6132 delay = atoi(CTEXT(CDR(CDR(node)))) * 1000;
6133 t = CDR(CDR(CDR(node)));
6139 if ((Mission_events[Event_index].repeat_count > 1) || (CDR(node) < 0)){
6140 message_training_que(CTEXT(node), timestamp(delay), t);
6142 message_training_que(CTEXT(CDR(node)), timestamp(delay), t);
6145 // if (Training_msg_method)
6146 // gameseq_post_event(GS_EVENT_TRAINING_PAUSE);
6149 int sexp_shield_recharge_pct(int node)
6153 // get the firing ship
6154 sindex = ship_name_lookup(CTEXT(node));
6158 if(Ships[sindex].objnum < 0){
6162 // shield recharge pct
6163 return (int)(100.0f * Energy_levels[Ships[sindex].shield_recharge_index]);
6166 int sexp_engine_recharge_pct(int node)
6170 // get the firing ship
6171 sindex = ship_name_lookup(CTEXT(node));
6175 if(Ships[sindex].objnum < 0){
6179 // shield recharge pct
6180 return (int)(100.0f * Energy_levels[Ships[sindex].engine_recharge_index]);
6183 int sexp_weapon_recharge_pct(int node)
6187 // get the firing ship
6188 sindex = ship_name_lookup(CTEXT(node));
6192 if(Ships[sindex].objnum < 0){
6196 // shield recharge pct
6197 return (int)(100.0f * Energy_levels[Ships[sindex].weapon_recharge_index]);
6200 int sexp_shield_quad_low(int node)
6203 float max_quad, check;
6208 sindex = ship_name_lookup(CTEXT(node));
6212 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6215 if((Ships[sindex].ship_info_index < 0) || (Ships[sindex].ship_info_index >= Num_ship_types)){
6218 objp = &Objects[Ships[sindex].objnum];
6219 sip = &Ship_info[Ships[sindex].ship_info_index];
6220 if(!(sip->flags & SIF_SMALL_SHIP)){
6223 max_quad = sip->shields / (float)MAX_SHIELD_SECTIONS;
6226 check = (float)atoi(CTEXT(CDR(node)));
6228 // check his quadrants
6229 for(idx=0; idx<MAX_SHIELD_SECTIONS; idx++){
6230 if( ((objp->shields[idx] / max_quad) * 100.0f) <= check ){
6239 int sexp_secondary_ammo_pct(int node)
6248 sindex = ship_name_lookup(CTEXT(node));
6252 if((Ships[sindex].objnum < 0) || (Ships[sindex].objnum >= MAX_OBJECTS)){
6255 shipp = &Ships[sindex];
6258 check = atoi(CTEXT(CDR(node)));
6260 // bogus check? (3 == cumulative sum of all banks)
6261 if((check != 3) && (check > shipp->weapons.num_secondary_banks)){
6267 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6268 ret_sum[idx] = (int)(((float)shipp->weapons.secondary_bank_ammo[idx] / (float)shipp->weapons.secondary_bank_start_ammo[idx]) * 100.0f);
6273 for(idx=0; idx<shipp->weapons.num_secondary_banks; idx++){
6274 ret += ret_sum[idx];
6276 ret = (int)((float)ret / (float)shipp->weapons.num_secondary_banks);
6278 ret = (int)(((float)shipp->weapons.secondary_bank_ammo[check] / (float)shipp->weapons.secondary_bank_start_ammo[check]) * 100.0f);
6285 void sexp_beam_fire(int node)
6288 beam_fire_info fire_info;
6292 memset(&fire_info, 0, sizeof(beam_fire_info));
6293 fire_info.accuracy = 0.000001f; // this will guarantee a hit
6295 // get the firing ship
6296 sindex = ship_name_lookup(CTEXT(node));
6300 if(Ships[sindex].objnum < 0){
6303 fire_info.shooter = &Objects[Ships[sindex].objnum];
6305 // get the subsystem
6306 fire_info.turret = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6307 if(fire_info.turret == NULL){
6312 sindex = ship_name_lookup(CTEXT(CDR(CDR(node))));
6316 if(Ships[sindex].objnum < 0){
6319 fire_info.target = &Objects[Ships[sindex].objnum];
6321 // see if the optional subsystem can be found
6322 fire_info.target_subsys = NULL;
6323 fire_info.target_subsys = ship_get_subsys(&Ships[sindex], CTEXT(CDR(CDR(CDR(node)))));
6325 // if it has no primary weapons
6326 if(fire_info.turret->weapons.num_primary_banks <= 0){
6331 // if the turret is destroyed
6332 if(fire_info.turret->current_hits <= 0.0f){
6336 // hmm, this could be wacky. Let's just simply select the first beam weapon in the turret
6337 fire_info.beam_info_index = -1;
6338 for(idx=0; idx<fire_info.turret->weapons.num_primary_banks; idx++){
6339 // store the weapon info index
6340 if(Weapon_info[fire_info.turret->weapons.primary_bank_weapons[idx]].wi_flags & WIF_BEAM){
6341 fire_info.beam_info_index = fire_info.turret->weapons.primary_bank_weapons[idx];
6346 if(fire_info.beam_info_index != -1){
6347 beam_fire(&fire_info);
6349 // it would appear the turret doesn't have any beam weapons, dumbass
6354 void sexp_beam_free(int node)
6357 ship_subsys *turret = NULL;
6359 // get the firing ship
6360 sindex = ship_name_lookup(CTEXT(node));
6364 if(Ships[sindex].objnum < 0){
6370 // get the subsystem
6371 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6376 // flag it as beam free :)
6377 turret->weapons.flags |= SW_FLAG_BEAM_FREE;
6378 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6385 void sexp_beam_free_all(int node)
6387 ship_subsys *subsys;
6390 // get the firing ship
6391 sindex = ship_name_lookup(CTEXT(node));
6395 if(Ships[sindex].objnum < 0){
6399 // free all beam weapons
6400 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6401 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6402 // just mark all turrets as beam free
6403 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6404 subsys->weapons.flags |= SW_FLAG_BEAM_FREE;
6405 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6409 subsys = GET_NEXT(subsys);
6413 void sexp_beam_lock(int node)
6416 ship_subsys *turret = NULL;
6418 // get the firing ship
6419 sindex = ship_name_lookup(CTEXT(node));
6423 if(Ships[sindex].objnum < 0){
6429 // get the subsystem
6430 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6435 // flag it as not beam free
6436 turret->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6443 void sexp_beam_lock_all(int node)
6445 ship_subsys *subsys;
6448 // get the firing ship
6449 sindex = ship_name_lookup(CTEXT(node));
6453 if(Ships[sindex].objnum < 0){
6457 // free all beam weapons
6458 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6459 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6460 // just mark all turrets as not beam free
6461 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6462 subsys->weapons.flags &= ~(SW_FLAG_BEAM_FREE);
6466 subsys = GET_NEXT(subsys);
6470 void sexp_turret_free(int node)
6473 ship_subsys *turret = NULL;
6475 // get the firing ship
6476 sindex = ship_name_lookup(CTEXT(node));
6480 if(Ships[sindex].objnum < 0){
6486 // get the subsystem
6487 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6492 // flag turret as no longer locked :)
6493 turret->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6494 turret->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6501 void sexp_turret_free_all(int node)
6503 ship_subsys *subsys;
6506 // get the firing ship
6507 sindex = ship_name_lookup(CTEXT(node));
6511 if(Ships[sindex].objnum < 0){
6516 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6517 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6518 // just mark all turrets as free
6519 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6520 subsys->weapons.flags &= (~SW_FLAG_TURRET_LOCK);
6521 subsys->turret_next_fire_stamp = timestamp((int) frand_range(50.0f, 4000.0f));
6525 subsys = GET_NEXT(subsys);
6529 void sexp_turret_lock(int node)
6532 ship_subsys *turret = NULL;
6534 // get the firing ship
6535 sindex = ship_name_lookup(CTEXT(node));
6539 if(Ships[sindex].objnum < 0){
6545 // get the subsystem
6546 turret = ship_get_subsys(&Ships[sindex], CTEXT(node));
6551 // flag turret as locked
6552 turret->weapons.flags |= SW_FLAG_TURRET_LOCK;
6559 void sexp_turret_lock_all(int node)
6561 ship_subsys *subsys;
6564 // get the firing ship
6565 sindex = ship_name_lookup(CTEXT(node));
6569 if(Ships[sindex].objnum < 0){
6574 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6575 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6576 // just mark all turrets as locked
6577 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6578 subsys->weapons.flags |= SW_FLAG_TURRET_LOCK;
6582 subsys = GET_NEXT(subsys);
6586 void sexp_turret_tagged_only_all(int node)
6588 ship_subsys *subsys;
6591 // get the firing ship
6592 sindex = ship_name_lookup(CTEXT(node));
6596 if(Ships[sindex].objnum < 0){
6600 // mark all turrets to only target tagged ships
6601 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6602 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6603 // just mark all turrets as locked
6604 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6605 subsys->weapons.flags |= SW_FLAG_TAGGED_ONLY;
6609 subsys = GET_NEXT(subsys);
6613 void sexp_turret_tagged_clear_all(int node)
6615 ship_subsys *subsys;
6618 // get the firing ship
6619 sindex = ship_name_lookup(CTEXT(node));
6623 if(Ships[sindex].objnum < 0){
6627 // mark all turrets so not restricted to only tagged ships
6628 subsys = GET_FIRST(&Ships[sindex].subsys_list);
6629 while(subsys != END_OF_LIST(&Ships[sindex].subsys_list)){
6630 // just mark all turrets as locked
6631 if(subsys->system_info->type == SUBSYSTEM_TURRET){
6632 subsys->weapons.flags &= (~SW_FLAG_TAGGED_ONLY);
6636 subsys = GET_NEXT(subsys);
6640 void sexp_add_remove_escort(int node)
6646 // get the firing ship
6647 sindex = ship_name_lookup(CTEXT(node));
6651 if(Ships[sindex].objnum < 0){
6655 // determine whether to add or remove it
6656 whee = CTEXT(CDR(node));
6657 flag = atoi(CTEXT(CDR(node)));
6661 hud_add_ship_to_escort(Ships[sindex].objnum, 1);
6663 hud_remove_ship_from_escort(Ships[sindex].objnum);
6667 void sexp_awacs_set_radius(int node)
6672 // get the firing ship
6673 sindex = ship_name_lookup(CTEXT(node));
6677 if(Ships[sindex].objnum < 0){
6681 // get the awacs subsystem
6682 awacs = ship_get_subsys(&Ships[sindex], CTEXT(CDR(node)));
6687 // make sure this _is_ an awacs subsystem
6688 Assert(awacs->system_info->flags & MSS_FLAG_AWACS);
6689 if(awacs->system_info->flags & MSS_FLAG_AWACS){
6693 // set the new awacs radius
6694 awacs->awacs_radius = (float)atoi(CTEXT(CDR(CDR(node))));
6697 int sexp_is_tagged(int node)
6701 // get the firing ship
6702 sindex = ship_name_lookup(CTEXT(node));
6706 if(Ships[sindex].objnum < 0){
6709 if(Ships[sindex].tag_left > 0.0f){
6717 int sexp_num_kills(int node)
6719 int sindex, np_index;
6722 // get the ship we're interested in
6723 sindex = ship_name_lookup(CTEXT(node));
6727 if(Ships[sindex].objnum < 0){
6731 // in multiplayer, search through all players
6732 if(Game_mode & GM_MULTIPLAYER){
6733 // try and find the player
6734 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6735 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6736 p = Net_players[np_index].player;
6739 // if we're in single player, we're only concerned with ourself
6742 if(Player_obj == &Objects[Ships[sindex].objnum]){
6747 // now, if we have a valid player, return his kills
6749 return p->stats.m_kill_count_ok;
6756 int sexp_num_type_kills(int node)
6758 int sindex, np_index, st_index;
6762 // get the ship we're interested in
6763 sindex = ship_name_lookup(CTEXT(node));
6767 if(Ships[sindex].objnum < 0){
6771 // in multiplayer, search through all players
6772 if(Game_mode & GM_MULTIPLAYER){
6773 // try and find the player
6774 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6775 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6776 p = Net_players[np_index].player;
6779 // if we're in single player, we're only concerned with ourself
6782 if(Player_obj == &Objects[Ships[sindex].objnum]){
6792 // lookup ship type name
6793 st_index = ship_type_name_lookup(CTEXT(CDR(node)));
6800 for(idx=0; idx<Num_ship_types; idx++){
6801 if((p->stats.m_okKills[idx] > 0) && (Ship_info[idx].flags & Ship_type_flags[st_index])){
6802 total += p->stats.m_okKills[idx];
6810 int sexp_num_class_kills(int node)
6812 int sindex, np_index, si_index;
6815 // get the ship we're interested in
6816 sindex = ship_name_lookup(CTEXT(node));
6820 if(Ships[sindex].objnum < 0){
6824 // in multiplayer, search through all players
6825 if(Game_mode & GM_MULTIPLAYER){
6826 // try and find the player
6827 np_index = multi_find_player_by_object(&Objects[Ships[sindex].objnum]);
6828 if((np_index >= 0) && (np_index < MAX_PLAYERS)){
6829 p = Net_players[np_index].player;
6832 // if we're in single player, we're only concerned with ourself
6835 if(Player_obj == &Objects[Ships[sindex].objnum]){
6845 // get the ship type we're looking for
6846 si_index = ship_info_lookup(CTEXT(CDR(node)));
6847 if((si_index < 0) || (si_index > Num_ship_types)){
6852 return p->stats.m_okKills[si_index];
6855 void sexp_subsys_set_random(int node)
6857 int sindex, low, high, n, idx, rand, exclusion_list[MAX_MODEL_SUBSYSTEMS];
6858 ship_subsys *subsys;
6862 sindex = ship_name_lookup(CTEXT(node));
6866 if(Ships[sindex].objnum < 0){
6869 shipp = &Ships[sindex];
6872 low = num_eval(CDR(node));
6878 high = num_eval(CDR(CDR(node)));
6888 n = CDR(CDR(CDR(node)));
6890 // init exclusion list
6891 memset(exclusion_list, 0, sizeof(int) * Ship_info[shipp->ship_info_index].n_subsystems);
6893 // get exclusion list
6895 int exclude_index = ship_get_subsys_index(shipp, CTEXT(n), 0);
6896 if (exclude_index >= 0) {
6897 exclusion_list[exclude_index] = 1;
6903 // apply to all others
6904 for (idx=0; idx<Ship_info[shipp->ship_info_index].n_subsystems; idx++) {
6905 if ( exclusion_list[idx] == 0 ) {
6906 // get non excluded subsystem
6907 subsys = ship_get_indexed_subsys(shipp, idx, NULL);
6909 // randomize its hit points
6910 rand = rand_internal(low, high);
6911 subsys->current_hits = 0.01f * rand * subsys->system_info->max_hits;
6916 void sexp_supernova_start(int node)
6918 supernova_start(atoi(CTEXT(node)));
6921 int sexp_is_secondary_selected(int node)
6928 sindex = ship_name_lookup(CTEXT(node));
6932 if(Ships[sindex].objnum < 0){
6935 shipp = &Ships[sindex];
6938 bank = atoi(CTEXT(CDR(node)));
6939 if(bank >= shipp->weapons.num_secondary_banks){
6943 // is this the bank currently selected
6944 if(bank == shipp->weapons.current_secondary_bank){
6952 int sexp_is_primary_selected(int node)
6959 sindex = ship_name_lookup(CTEXT(node));
6963 if(Ships[sindex].objnum < 0){
6966 shipp = &Ships[sindex];
6969 bank = atoi(CTEXT(CDR(node)));
6970 if(bank >= shipp->weapons.num_primary_banks){
6974 // is this the bank currently selected
6975 if(bank == shipp->weapons.current_primary_bank){
6983 #define RIGHT_QUAD 0
6984 #define FRONT_QUAD 1
6988 // Return SEXP_TRUE if quadrant quadnum is near max.
6989 int shield_quad_near_max(int quadnum)
6991 float remaining = 0.0f;
6992 for (int i=0; i<MAX_SHIELD_SECTIONS; i++) {
6996 remaining += Player_obj->shields[i];
6999 if ((remaining < 2.0f) || (Player_obj->shields[quadnum] > Ship_info[Player_ship->ship_info_index].shields/MAX_SHIELD_SECTIONS - 5.0f)) {
7006 // Return truth value for special SEXP.
7007 // Used in training#5, perhaps in other missions.
7008 int process_special_sexps(int index)
7011 case 0: // Ship "Freighter 1" is aspect locked by player.
7012 if (Player_ai->target_objnum != -1) {
7013 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
7014 if (Player_ai->current_target_is_locked)
7020 case 1: // Fired Interceptors
7022 for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
7023 if (objp->type == OBJ_WEAPON) {
7024 if (!stricmp(Weapon_info[Weapons[objp->instance].weapon_info_index].name, "Interceptor#weak")) {
7025 int target = Weapons[objp->instance].target_num;
7027 if (Objects[target].type == OBJ_SHIP) {
7028 if (!(stricmp(Ships[Objects[target].instance].ship_name, "Freighter 1")))
7036 case 2: // Ship "Freighter 1", subsystem "Weapons" is aspect locked by player.
7037 if (Player_ai->target_objnum != -1) {
7038 if (!(stricmp(Ships[Objects[Player_ai->target_objnum].instance].ship_name, "Freighter 1"))) {
7039 if (!(stricmp(Player_ai->targeted_subsys->system_info->name, "Weapons"))) {
7040 if (Player_ai->current_target_is_locked){
7048 case 3: // Player ship suffering shield damage on front.
7049 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7050 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7053 case 4: // Player ship suffering much damage.
7054 nprintf(("AI", "Frame %i\n", Framecount));
7055 apply_damage_to_shield(Player_obj, FRONT_QUAD, 10.0f);
7056 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7057 if (Player_obj->shields[FRONT_QUAD] < 2.0f)
7062 case 5: // Player's shield is quick repaired
7063 nprintf(("AI", "Frame %i, recharged to %7.3f\n", Framecount, Player_obj->shields[FRONT_QUAD]));
7065 apply_damage_to_shield(Player_obj, FRONT_QUAD, -flFrametime*200.0f);
7067 if (Player_obj->shields[FRONT_QUAD] > Ship_info[Player_ship->ship_info_index].shields/4.0f)
7068 Player_obj->shields[FRONT_QUAD] = Ship_info[Player_ship->ship_info_index].shields/4.0f;
7070 //hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7071 if (Player_obj->shields[FRONT_QUAD] > Player_obj->shields[(FRONT_QUAD+1)%4] - 2.0f)
7076 case 6: // 3 of player's shield quadrants are reduced to 0.
7077 Player_obj->shields[1] = 1.0f;
7078 Player_obj->shields[2] = 1.0f;
7079 Player_obj->shields[3] = 1.0f;
7080 //apply_damage_to_shield(Player_obj, FRONT_QUAD, 1.0f);
7081 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7083 case 7: // Make sure front quadrant has been maximized, or close to it.
7084 if (shield_quad_near_max(FRONT_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7087 case 8: // Make sure rear quadrant has been maximized, or close to it.
7088 if (shield_quad_near_max(REAR_QUAD)) return SEXP_TRUE; else return SEXP_FALSE;
7091 case 9: // Zero left and right quadrants in preparation for maximizing rear quadrant.
7092 Player_obj->shields[LEFT_QUAD] = 0.0f;
7093 Player_obj->shields[RIGHT_QUAD] = 0.0f;
7094 hud_shield_quadrant_hit(Player_obj, LEFT_QUAD);
7098 case 10: // Return true if player is low on Interceptors.
7099 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 8)
7105 case 11: // Return true if player has plenty of Interceptors.
7106 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] >= 8)
7112 case 12: // Return true if player is low on Interceptors.
7113 if (Player_ship->weapons.secondary_bank_ammo[0] + Player_ship->weapons.secondary_bank_ammo[1] < 4)
7119 case 13: // Zero front shield quadrant. Added for Jim Boone on August 26, 1999 by MK.
7120 Player_obj->shields[FRONT_QUAD] = 0.0f;
7121 hud_shield_quadrant_hit(Player_obj, FRONT_QUAD);
7125 case 100: // Return true if player is out of countermeasures.
7126 if (Player_ship->cmeasure_count <= 0)
7132 Int3(); // Unsupported node type.
7138 // custom sexp operator for handling misc training stuff
7139 int sexp_special_training_check(int node)
7143 num = atoi(CTEXT(node));
7144 if (num == SPECIAL_CHECK_TRAINING_FAILURE)
7145 return Training_failure ? SEXP_TRUE : SEXP_FALSE;
7147 // To MK: do whatever you want with this number here.
7148 rtn = process_special_sexps(atoi(CTEXT(node)));
7153 // sexpression to flash a hud gauge. gauge name is text valud of node
7154 void sexp_flash_hud_gauge( int node )
7160 for (i = 0; i < NUM_HUD_GAUGES; i++ ) {
7161 if ( !stricmp(HUD_gauge_text[i], name) ) {
7162 hud_gauge_start_flash(i); // call HUD function to flash gauge
7168 void sexp_set_training_context_fly_path(int node)
7172 for (i=0; i<Num_waypoint_lists; i++)
7173 if (!stricmp(CTEXT(node), Waypoint_lists[i].name))
7176 if (i < Num_waypoint_lists) {
7177 Training_context |= TRAINING_CONTEXT_FLY_PATH;
7178 Training_context_path = i;
7179 Training_context_distance = (float) atof(CTEXT(CDR(node)));
7180 Training_context_goal_waypoint = 0;
7181 Training_context_at_waypoint = -1;
7185 void sexp_set_training_context_speed(int node)
7187 Training_context |= TRAINING_CONTEXT_SPEED;
7188 Training_context_speed_min = atoi(CTEXT(node));
7189 Training_context_speed_max = atoi(CTEXT(CDR(node)));
7190 Training_context_speed_set = 0;
7193 // high-level sexpression evaluator
7194 int eval_sexp(int cur_node)
7196 int node, type, sexp_val = UNINITIALIZED;
7197 if (cur_node == -1) // empty list, i.e. sexp: ( )
7200 Assert(cur_node >= 0); // we have special sexp nodes <= -1!!! MWA
7201 // which should be intercepted before we get here. HOFFOSS
7202 type = SEXP_NODE_TYPE(cur_node);
7203 Assert( (type == SEXP_LIST) || (type == SEXP_ATOM) );
7205 // trap known true and known false sexpressions. We don't trap on SEXP_NAN sexpressions since
7206 // they may yet evaluate to true or false.
7208 if (Sexp_nodes[cur_node].value == SEXP_KNOWN_TRUE)
7210 else if (Sexp_nodes[cur_node].value == SEXP_KNOWN_FALSE)
7213 if (Sexp_nodes[cur_node].first != -1) {
7214 node = CAR(cur_node);
7215 sexp_val = eval_sexp(node);
7216 Sexp_nodes[cur_node].value = Sexp_nodes[node].value; // higher level node gets node value
7222 node = CDR(cur_node); // makes reading the next bit of code a little easier.
7224 op_num = find_operator(CTEXT(cur_node));
7226 // arithmetic operators will always return just their value
7228 sexp_val = add_sexps( node );
7232 sexp_val = sub_sexps( node );
7236 sexp_val = mul_sexps( node );
7240 sexp_val = mod_sexps( node );
7244 sexp_val = div_sexps( node );
7248 sexp_val = rand_sexp( node );
7251 // boolean operators can have one of the special sexp values (known true, known false, unknown)
7253 sexp_val = SEXP_KNOWN_TRUE;
7257 sexp_val = SEXP_KNOWN_FALSE;
7261 sexp_val = sexp_or( node );
7265 sexp_val = sexp_and( node );
7268 case OP_AND_IN_SEQUENCE:
7269 sexp_val = sexp_and_in_sequence( node );
7272 case OP_GREATER_THAN:
7273 sexp_val = sexp_gt( node );
7277 sexp_val = sexp_lt( node );
7281 sexp_val = sexp_equal( node );
7285 sexp_val = sexp_is_iff( node );
7289 sexp_val = sexp_not( node );
7292 case OP_PREVIOUS_GOAL_TRUE:
7293 sexp_val = sexp_previous_goal_status( node, GOAL_COMPLETE );
7296 case OP_PREVIOUS_GOAL_FALSE:
7297 sexp_val = sexp_previous_goal_status( node, GOAL_FAILED );
7300 case OP_PREVIOUS_GOAL_INCOMPLETE:
7301 sexp_val = sexp_previous_goal_status( node, GOAL_INCOMPLETE );
7304 case OP_PREVIOUS_EVENT_TRUE:
7305 sexp_val = sexp_previous_event_status( node, EVENT_SATISFIED );
7308 case OP_PREVIOUS_EVENT_FALSE:
7309 sexp_val = sexp_previous_event_status( node, EVENT_FAILED );
7312 case OP_PREVIOUS_EVENT_INCOMPLETE:
7313 sexp_val = sexp_previous_event_status( node, EVENT_INCOMPLETE );
7317 case OP_EVENT_FALSE:
7318 sexp_val = sexp_event_status( node, (op_num == OP_EVENT_TRUE?1:0) );
7319 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7320 Sexp_useful_number = 0; // indicate sexp isn't current yet
7324 case OP_EVENT_TRUE_DELAY:
7325 case OP_EVENT_FALSE_DELAY:
7326 sexp_val = sexp_event_delay_status( node, (op_num == OP_EVENT_TRUE_DELAY?1:0) );
7327 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7328 Sexp_useful_number = 0; // indicate sexp isn't current yet
7331 case OP_GOAL_TRUE_DELAY:
7332 case OP_GOAL_FALSE_DELAY:
7333 sexp_val = sexp_goal_delay_status( node, (op_num == OP_GOAL_TRUE_DELAY?1:0) );
7336 case OP_EVENT_INCOMPLETE:
7337 sexp_val = sexp_event_incomplete( node );
7338 if ((sexp_val != SEXP_TRUE) && (sexp_val != SEXP_KNOWN_TRUE))
7339 Sexp_useful_number = 0; // indicate sexp isn't current yet
7342 case OP_GOAL_INCOMPLETE:
7343 sexp_val = sexp_goal_incomplete( node );
7346 // destroy type sexpressions
7347 case OP_IS_DESTROYED:
7348 sexp_val = sexp_is_destroyed( node, NULL );
7351 case OP_IS_SUBSYSTEM_DESTROYED:
7352 sexp_val = sexp_is_subsystem_destroyed( node );
7356 sexp_val = sexp_has_docked( node );
7359 case OP_HAS_ARRIVED:
7360 sexp_val = sexp_has_arrived( node, NULL );
7363 case OP_HAS_DEPARTED:
7364 sexp_val = sexp_has_departed( node, NULL );
7367 case OP_HAS_UNDOCKED:
7368 sexp_val = sexp_has_undocked( node );
7371 case OP_IS_DISABLED:
7372 sexp_val = sexp_is_disabled( node, NULL );
7375 case OP_IS_DISARMED:
7376 sexp_val = sexp_is_disarmed( node, NULL );
7379 case OP_WAYPOINTS_DONE:
7380 sexp_val = sexp_are_waypoints_done( node );
7383 // objective operators that use a delay
7384 case OP_IS_DESTROYED_DELAY:
7385 sexp_val = sexp_is_destroyed_delay( node );
7388 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
7389 sexp_val = sexp_is_subsystem_destroyed_delay( node );
7392 case OP_HAS_DOCKED_DELAY:
7393 sexp_val = sexp_has_docked_delay( node );
7396 case OP_HAS_ARRIVED_DELAY:
7397 sexp_val = sexp_has_arrived_delay( node );
7400 case OP_HAS_DEPARTED_DELAY:
7401 sexp_val = sexp_has_departed_delay( node );
7404 case OP_HAS_UNDOCKED_DELAY:
7405 sexp_val = sexp_has_undocked_delay( node );
7408 case OP_IS_DISABLED_DELAY:
7409 sexp_val = sexp_is_disabled_delay( node );
7412 case OP_IS_DISARMED_DELAY:
7413 sexp_val = sexp_is_disarmed_delay( node );
7416 case OP_WAYPOINTS_DONE_DELAY:
7417 sexp_val = sexp_are_waypoints_done_delay( node );
7420 case OP_SHIP_TYPE_DESTROYED:
7421 sexp_val = sexp_ship_type_destroyed( node );
7424 // time based sexpressions
7425 case OP_HAS_TIME_ELAPSED:
7426 sexp_val = sexp_has_time_elapsed( node );
7429 case OP_MODIFY_VARIABLE:
7430 sexp_modify_variable( node );
7431 sexp_val = 1; // 1 means only do once.
7434 case OP_TIME_SHIP_DESTROYED:
7435 sexp_val = sexp_time_destroyed( node );
7438 case OP_TIME_WING_DESTROYED:
7439 sexp_val = sexp_time_wing_destroyed( node );
7442 case OP_TIME_SHIP_ARRIVED:
7443 sexp_val = sexp_time_ship_arrived( node );
7446 case OP_TIME_WING_ARRIVED:
7447 sexp_val = sexp_time_wing_arrived( node );
7450 case OP_TIME_SHIP_DEPARTED:
7451 sexp_val = sexp_time_ship_departed( node );
7454 case OP_TIME_WING_DEPARTED:
7455 sexp_val = sexp_time_wing_departed( node );
7458 case OP_MISSION_TIME:
7459 sexp_val = sexp_mission_time();
7462 case OP_TIME_DOCKED:
7463 sexp_val = sexp_time_docked( node );
7466 case OP_TIME_UNDOCKED:
7467 sexp_val = sexp_time_undocked( node );
7470 // info based sexpressions (like shields, hits
7471 case OP_SHIELDS_LEFT:
7472 sexp_val = sexp_shields_left( node );
7476 sexp_val = sexp_hits_left( node );
7479 case OP_SPECIAL_WARP_DISTANCE:
7480 sexp_val = sexp_special_warp_dist( node );
7483 case OP_HITS_LEFT_SUBSYSTEM:
7484 sexp_val = sexp_hits_left_subsystem( node );
7488 sexp_val = sexp_distance( node );
7491 case OP_IS_SHIP_VISIBLE:
7492 sexp_val = sexp_is_ship_visible( node );
7496 sexp_val = sexp_team_score( node );
7499 case OP_LAST_ORDER_TIME:
7500 sexp_val = sexp_last_order_time( node );
7503 case OP_NUM_PLAYERS:
7504 sexp_val = sexp_num_players();
7507 case OP_SKILL_LEVEL_AT_LEAST:
7508 sexp_val = sexp_skill_level_at_least( node );
7511 case OP_IS_CARGO_KNOWN:
7512 case OP_CARGO_KNOWN_DELAY:
7513 sexp_val = sexp_is_cargo_known( node, (op_num==OP_IS_CARGO_KNOWN)?0:1 );
7516 case OP_HAS_BEEN_TAGGED_DELAY:
7517 sexp_val = sexp_has_been_tagged_delay(node);
7520 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
7521 sexp_val = sexp_cap_subsys_cargo_known_delay(node);
7524 case OP_WAS_PROMOTION_GRANTED:
7525 sexp_val = sexp_was_promotion_granted(node);
7528 case OP_WAS_MEDAL_GRANTED:
7529 sexp_val = sexp_was_medal_granted(node);
7532 case OP_PERCENT_SHIPS_DEPARTED:
7533 case OP_PERCENT_SHIPS_DESTROYED:
7534 sexp_val = sexp_percent_ships_depart_destroy(node, op_num);
7537 case OP_DEPART_NODE_DELAY:
7538 sexp_val = sexp_depart_node_delay( node );
7541 case OP_DESTROYED_DEPARTED_DELAY:
7542 sexp_val = sexp_destroyed_departed_delay( node );
7545 // conditional sexpressions
7547 sexp_val = eval_when( node );
7551 sexp_val = eval_cond( node );
7554 // sexpressions with side effects
7556 sexp_change_iff( node );
7560 case OP_ADD_SHIP_GOAL:
7561 sexp_add_ship_goal( node );
7565 case OP_ADD_WING_GOAL:
7566 sexp_add_wing_goal( node );
7571 sexp_add_goal( node );
7575 case OP_CLEAR_SHIP_GOALS:
7576 sexp_clear_ship_goals( node );
7580 case OP_CLEAR_WING_GOALS:
7581 sexp_clear_wing_goals( node );
7585 case OP_CLEAR_GOALS:
7586 sexp_clear_goals( node );
7590 case OP_PROTECT_SHIP:
7591 case OP_UNPROTECT_SHIP:
7592 sexp_protect_ships( node, (op_num==OP_PROTECT_SHIP?1:0) );
7596 case OP_BEAM_PROTECT_SHIP:
7597 case OP_BEAM_UNPROTECT_SHIP:
7598 sexp_beam_protect_ships( node, (op_num==OP_BEAM_PROTECT_SHIP?1:0) );
7602 case OP_SHIP_INVISIBLE:
7603 case OP_SHIP_VISIBLE:
7604 sexp_ships_visible( node, (op_num==OP_SHIP_VISIBLE?1:0) );
7608 case OP_SHIP_VULNERABLE:
7609 case OP_SHIP_INVULNERABLE:
7610 sexp_ships_invulnerable( node, (op_num==OP_SHIP_INVULNERABLE?1:0) );
7614 case OP_SHIP_GUARDIAN:
7615 case OP_SHIP_NO_GUARDIAN:
7616 sexp_ships_guardian( node, (op_num==OP_SHIP_GUARDIAN?1:0) );
7620 case OP_SHIP_VANISH:
7621 sexp_ship_vanish( node );
7625 case OP_SEND_MESSAGE:
7626 sexp_send_message( node );
7630 case OP_SEND_MESSAGE_LIST:
7631 sexp_send_message_list( node );
7635 case OP_SEND_RANDOM_MESSAGE:
7636 sexp_send_random_message( node );
7640 case OP_SELF_DESTRUCT:
7641 sexp_self_destruct( node );
7645 case OP_NEXT_MISSION:
7646 sexp_next_mission( node );
7650 case OP_END_OF_CAMPAIGN:
7651 sexp_end_of_campaign( node );
7655 case OP_END_CAMPAIGN:
7656 sexp_end_campaign( node );
7660 case OP_SABOTAGE_SUBSYSTEM:
7661 sexp_sabotage_subsystem( node );
7665 case OP_REPAIR_SUBSYSTEM:
7666 sexp_repair_subsystem( node );
7670 case OP_SET_SUBSYSTEM_STRNGTH:
7671 sexp_set_subsystem_strength( node );
7675 case OP_INVALIDATE_GOAL:
7676 case OP_VALIDATE_GOAL:
7677 sexp_change_goal_validity( node, (op_num==OP_INVALIDATE_GOAL?0:1) );
7681 case OP_TRANSFER_CARGO:
7682 sexp_transfer_cargo( node );
7686 case OP_EXCHANGE_CARGO:
7687 sexp_exchange_cargo( node );
7692 case OP_JETTISON_CARGO:
7693 sexp_jettison_cargo( node );
7697 case OP_CARGO_NO_DEPLETE:
7698 sexp_cargo_no_deplete( node );
7702 case OP_SET_SPECIAL_WARPOUT_NAME:
7703 sexp_special_warpout_name( node );
7707 case OP_END_MISSION_DELAY:
7708 sexp_end_mission_delay( node );
7712 // sexpressions for setting flag for good/bad time for someone to reasm
7713 case OP_GOOD_REARM_TIME:
7714 sexp_good_time_to_rearm( node );
7718 case OP_GRANT_PROMOTION:
7719 sexp_grant_promotion();
7723 case OP_GRANT_MEDAL:
7724 sexp_grant_medal( node );
7728 case OP_WARP_BROKEN:
7729 case OP_WARP_NOT_BROKEN:
7730 sexp_deal_with_warp( node, 0, op_num==OP_WARP_BROKEN?1:0 );
7735 case OP_WARP_ALLOWED:
7736 sexp_deal_with_warp( node, 1, op_num==OP_WARP_NEVER?1:0 );
7740 case OP_GOOD_SECONDARY_TIME:
7741 sexp_good_secondary_time( node );
7745 // sexpressions to allow shpis/weapons during the course of a mission
7747 sexp_allow_ship( node );
7751 case OP_ALLOW_WEAPON:
7752 sexp_allow_weapon( node );
7756 case OP_TECH_ADD_SHIP:
7757 sexp_tech_add_ship(node);
7761 case OP_TECH_ADD_WEAPON:
7762 sexp_tech_add_weapon(node);
7766 // in the case of a red_alert mission, simply call the red alert function to close
7767 // the current campaign's mission and move forward to the next mission
7769 red_alert_start_mission();
7773 // training operators
7774 case OP_KEY_PRESSED:
7775 sexp_val = sexp_key_pressed(node);
7778 case OP_SPECIAL_CHECK:
7779 sexp_val = sexp_special_training_check(node);
7783 sexp_key_reset(node);
7784 sexp_val = SEXP_KNOWN_TRUE; // only do it first time in repeating events.
7788 sexp_val = sexp_targeted(node);
7792 sexp_val = sexp_speed(node);
7795 case OP_SECONDARIES_DEPLETED:
7796 sexp_val = sexp_secondaries_depleted(node);
7800 sexp_val = sexp_facing(node);
7804 sexp_val = sexp_facing2(node);
7808 sexp_val = sexp_order(node);
7811 case OP_WAYPOINT_MISSED:
7812 sexp_val = sexp_waypoint_missed();
7815 case OP_WAYPOINT_TWICE:
7816 sexp_val = sexp_waypoint_twice();
7820 sexp_val = sexp_path_flown();
7823 case OP_TRAINING_MSG:
7824 sexp_send_training_message(node);
7828 case OP_FLASH_HUD_GAUGE:
7829 sexp_flash_hud_gauge(node);
7833 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
7834 sexp_set_training_context_fly_path(node);
7838 case OP_SET_TRAINING_CONTEXT_SPEED:
7839 sexp_set_training_context_speed(node);
7843 // debugging operators
7849 case 0: // zero represents a non-operator
7850 return atoi(CTEXT(cur_node));
7857 sexp_beam_fire(node);
7862 sexp_val = sexp_is_tagged(node);
7866 sexp_val = sexp_num_kills(node);
7869 case OP_NUM_TYPE_KILLS:
7870 sexp_val = sexp_num_type_kills(node);
7873 case OP_NUM_CLASS_KILLS:
7874 sexp_val = sexp_num_class_kills(node);
7879 sexp_beam_free(node);
7882 case OP_BEAM_FREE_ALL:
7884 sexp_beam_free_all(node);
7889 sexp_beam_lock(node);
7892 case OP_BEAM_LOCK_ALL:
7894 sexp_beam_lock_all(node);
7897 case OP_TURRET_FREE:
7899 sexp_turret_free(node);
7902 case OP_TURRET_FREE_ALL:
7904 sexp_turret_free_all(node);
7907 case OP_TURRET_LOCK:
7909 sexp_turret_lock(node);
7912 case OP_TURRET_LOCK_ALL:
7914 sexp_turret_lock_all(node);
7917 case OP_ADD_REMOVE_ESCORT:
7919 sexp_add_remove_escort(node);
7922 case OP_AWACS_SET_RADIUS:
7924 sexp_awacs_set_radius(node);
7927 case OP_CAP_WAYPOINT_SPEED:
7929 sexp_cap_waypont_speed(node);
7932 case OP_TURRET_TAGGED_ONLY_ALL:
7934 sexp_turret_tagged_only_all(node);
7937 case OP_TURRET_TAGGED_CLEAR_ALL:
7939 sexp_turret_tagged_clear_all(node);
7942 case OP_SUBSYS_SET_RANDOM:
7944 sexp_subsys_set_random(node);
7947 case OP_SUPERNOVA_START:
7949 sexp_supernova_start(node);
7952 case OP_SHIELD_RECHARGE_PCT:
7953 sexp_val = sexp_shield_recharge_pct(node);
7956 case OP_ENGINE_RECHARGE_PCT:
7957 sexp_val = sexp_engine_recharge_pct(node);
7960 case OP_WEAPON_RECHARGE_PCT:
7961 sexp_val = sexp_weapon_recharge_pct(node);
7964 case OP_SHIELD_QUAD_LOW:
7965 sexp_val = sexp_shield_quad_low(node);
7968 case OP_SECONDARY_AMMO_PCT:
7969 sexp_val = sexp_secondary_ammo_pct(node);
7972 case OP_IS_SECONDARY_SELECTED:
7973 sexp_val = sexp_is_secondary_selected(node);
7976 case OP_IS_PRIMARY_SELECTED:
7977 sexp_val = sexp_is_primary_selected(node);
7981 Error(LOCATION, "Looking for SEXP operator, found '%s'.\n", CTEXT(cur_node));
7985 Assert(sexp_val != UNINITIALIZED);
7987 // if we haven't returned, check the sexp value of the sexpression evaluation. A special
7988 // value of known true or known false means that we should set the sexp.value field for
7989 // short circuit eval (and return that special value as well).
7990 if (sexp_val == SEXP_KNOWN_TRUE) {
7991 Sexp_nodes[cur_node].value = SEXP_KNOWN_TRUE;
7995 if (sexp_val == SEXP_KNOWN_FALSE) {
7996 Sexp_nodes[cur_node].value = SEXP_KNOWN_FALSE;
8000 if ( sexp_val == SEXP_NAN ) {
8001 Sexp_nodes[cur_node].value = SEXP_NAN; // not a number values are false I would suspect
8005 if ( sexp_val == SEXP_NAN_FOREVER ) {
8006 Sexp_nodes[cur_node].value = SEXP_NAN_FOREVER;
8010 if ( sexp_val == SEXP_CANT_EVAL ) {
8011 Sexp_nodes[cur_node].value = SEXP_CANT_EVAL;
8012 Sexp_useful_number = 0; // indicate sexp isn't current yet
8016 if ( Sexp_nodes[cur_node].value == SEXP_NAN ) { // if we had a nan, but now don't, reset the value
8017 Sexp_nodes[cur_node].value = SEXP_UNKNOWN;
8022 Sexp_nodes[cur_node].value = SEXP_TRUE;
8024 Sexp_nodes[cur_node].value = SEXP_FALSE;
8031 // Still a debug-level system.
8032 // get_sexp_main reads and builds the internal representation for a
8033 // symbolic expression.
8035 // Mp points at first character in expression.
8036 // The symbolic expression is built in Sexp_nodes beginning at node 0.
8040 char token[TOKEN_LENGTH];
8043 ignore_white_space();
8046 if (!strncmp(Mp, "( )", 3))
8051 start_node = get_sexp(token);
8052 // only need to check syntax if we have a operator
8053 if ( /*Sexp_nodes[start_node].subtype != SEXP_ATOM_OPERATOR ||*/ Fred_running || (start_node == -1))
8059 op = identify_operator(CTEXT(start_node));
8061 Error (LOCATION, "Can't find operator %s in operator list\n.", CTEXT(start_node) );
8071 while (*Mp != '#') {
8073 diag_printf("\n----------------\n");
8074 ignore_white_space();
8079 // returns the data type returned by an operator
8080 int query_operator_return_type(int op)
8084 Assert(op >= 0 && op < Num_operators);
8085 op = Operators[op].value;
8093 case OP_AND_IN_SEQUENCE:
8097 case OP_GREATER_THAN:
8099 case OP_IS_DESTROYED:
8100 case OP_IS_SUBSYSTEM_DESTROYED:
8101 case OP_IS_DISABLED:
8102 case OP_IS_DISARMED:
8104 case OP_HAS_UNDOCKED:
8105 case OP_HAS_ARRIVED:
8106 case OP_HAS_DEPARTED:
8107 case OP_IS_DESTROYED_DELAY:
8108 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8109 case OP_IS_DISABLED_DELAY:
8110 case OP_IS_DISARMED_DELAY:
8111 case OP_HAS_DOCKED_DELAY:
8112 case OP_HAS_UNDOCKED_DELAY:
8113 case OP_HAS_ARRIVED_DELAY:
8114 case OP_HAS_DEPARTED_DELAY:
8116 case OP_HAS_TIME_ELAPSED:
8117 case OP_GOAL_INCOMPLETE:
8118 case OP_GOAL_TRUE_DELAY:
8119 case OP_GOAL_FALSE_DELAY:
8120 case OP_EVENT_INCOMPLETE:
8121 case OP_EVENT_TRUE_DELAY:
8122 case OP_EVENT_FALSE_DELAY:
8123 case OP_PREVIOUS_EVENT_TRUE:
8124 case OP_PREVIOUS_EVENT_FALSE:
8125 case OP_PREVIOUS_EVENT_INCOMPLETE:
8126 case OP_PREVIOUS_GOAL_TRUE:
8127 case OP_PREVIOUS_GOAL_FALSE:
8128 case OP_PREVIOUS_GOAL_INCOMPLETE:
8129 case OP_WAYPOINTS_DONE:
8130 case OP_WAYPOINTS_DONE_DELAY:
8131 case OP_SHIP_TYPE_DESTROYED:
8132 case OP_LAST_ORDER_TIME:
8133 case OP_KEY_PRESSED:
8139 case OP_WAYPOINT_MISSED:
8140 case OP_WAYPOINT_TWICE:
8143 case OP_EVENT_FALSE:
8144 case OP_SKILL_LEVEL_AT_LEAST:
8145 case OP_IS_CARGO_KNOWN:
8146 case OP_HAS_BEEN_TAGGED_DELAY:
8147 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8148 case OP_CARGO_KNOWN_DELAY:
8149 case OP_WAS_PROMOTION_GRANTED:
8150 case OP_WAS_MEDAL_GRANTED:
8151 case OP_PERCENT_SHIPS_DEPARTED:
8152 case OP_PERCENT_SHIPS_DESTROYED:
8153 case OP_DEPART_NODE_DELAY:
8154 case OP_DESTROYED_DEPARTED_DELAY:
8155 case OP_SPECIAL_CHECK:
8157 case OP_SECONDARIES_DEPLETED:
8158 case OP_SHIELD_QUAD_LOW:
8159 case OP_IS_SECONDARY_SELECTED:
8160 case OP_IS_PRIMARY_SELECTED:
8169 case OP_TIME_SHIP_DESTROYED:
8170 case OP_TIME_SHIP_ARRIVED:
8171 case OP_TIME_SHIP_DEPARTED:
8172 case OP_TIME_WING_DESTROYED:
8173 case OP_TIME_WING_ARRIVED:
8174 case OP_TIME_WING_DEPARTED:
8175 case OP_MISSION_TIME:
8176 case OP_TIME_DOCKED:
8177 case OP_TIME_UNDOCKED:
8178 case OP_SHIELDS_LEFT:
8180 case OP_HITS_LEFT_SUBSYSTEM:
8182 case OP_NUM_PLAYERS:
8184 case OP_NUM_TYPE_KILLS:
8185 case OP_NUM_CLASS_KILLS:
8186 case OP_SHIELD_RECHARGE_PCT:
8187 case OP_ENGINE_RECHARGE_PCT:
8188 case OP_WEAPON_RECHARGE_PCT:
8189 case OP_SECONDARY_AMMO_PCT:
8190 case OP_SPECIAL_WARP_DISTANCE:
8191 case OP_IS_SHIP_VISIBLE:
8193 return OPR_POSITIVE;
8198 case OP_CLEAR_SHIP_GOALS:
8199 case OP_CLEAR_WING_GOALS:
8200 case OP_CLEAR_GOALS:
8201 case OP_ADD_SHIP_GOAL:
8202 case OP_ADD_WING_GOAL:
8204 case OP_PROTECT_SHIP:
8205 case OP_UNPROTECT_SHIP:
8206 case OP_BEAM_PROTECT_SHIP:
8207 case OP_BEAM_UNPROTECT_SHIP:
8211 case OP_SEND_MESSAGE:
8212 case OP_SELF_DESTRUCT:
8213 case OP_NEXT_MISSION:
8214 case OP_END_CAMPAIGN:
8215 case OP_END_OF_CAMPAIGN:
8216 case OP_SABOTAGE_SUBSYSTEM:
8217 case OP_REPAIR_SUBSYSTEM:
8218 case OP_INVALIDATE_GOAL:
8219 case OP_VALIDATE_GOAL:
8220 case OP_SEND_RANDOM_MESSAGE:
8221 case OP_TRANSFER_CARGO:
8222 case OP_EXCHANGE_CARGO:
8223 case OP_JETTISON_CARGO:
8224 case OP_CARGO_NO_DEPLETE:
8226 case OP_TRAINING_MSG:
8227 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8228 case OP_SET_TRAINING_CONTEXT_SPEED:
8229 case OP_END_MISSION_DELAY:
8230 case OP_SET_SUBSYSTEM_STRNGTH:
8231 case OP_GOOD_REARM_TIME:
8232 case OP_GRANT_PROMOTION:
8233 case OP_GRANT_MEDAL:
8235 case OP_ALLOW_WEAPON:
8236 case OP_TECH_ADD_SHIP:
8237 case OP_TECH_ADD_WEAPON:
8238 case OP_WARP_BROKEN:
8239 case OP_WARP_NOT_BROKEN:
8241 case OP_WARP_ALLOWED:
8242 case OP_FLASH_HUD_GAUGE:
8243 case OP_GOOD_SECONDARY_TIME:
8244 case OP_SHIP_VISIBLE:
8245 case OP_SHIP_INVISIBLE:
8246 case OP_SHIP_VULNERABLE:
8247 case OP_SHIP_INVULNERABLE:
8248 case OP_SHIP_GUARDIAN:
8249 case OP_SHIP_VANISH:
8250 case OP_SHIP_NO_GUARDIAN:
8252 case OP_MODIFY_VARIABLE:
8255 case OP_BEAM_FREE_ALL:
8257 case OP_BEAM_LOCK_ALL:
8258 case OP_TURRET_FREE:
8259 case OP_TURRET_FREE_ALL:
8260 case OP_TURRET_LOCK:
8261 case OP_TURRET_LOCK_ALL:
8262 case OP_ADD_REMOVE_ESCORT:
8263 case OP_AWACS_SET_RADIUS:
8264 case OP_SEND_MESSAGE_LIST:
8265 case OP_CAP_WAYPOINT_SPEED:
8266 case OP_TURRET_TAGGED_ONLY_ALL:
8267 case OP_TURRET_TAGGED_CLEAR_ALL:
8268 case OP_SUBSYS_SET_RANDOM:
8269 case OP_SUPERNOVA_START:
8270 case OP_SET_SPECIAL_WARPOUT_NAME:
8276 case OP_AI_WARP: // this particular operator is obsolete
8277 case OP_AI_WARP_OUT:
8278 case OP_AI_WAYPOINTS:
8279 case OP_AI_WAYPOINTS_ONCE:
8280 case OP_AI_DESTROY_SUBSYS:
8281 case OP_AI_CHASE_WING:
8282 case OP_AI_DISABLE_SHIP:
8283 case OP_AI_DISARM_SHIP:
8285 case OP_AI_GUARD_WING:
8286 case OP_AI_CHASE_ANY:
8287 case OP_AI_EVADE_SHIP:
8288 case OP_AI_STAY_NEAR_SHIP:
8289 case OP_AI_KEEP_SAFE_DISTANCE:
8291 case OP_AI_STAY_STILL:
8292 case OP_AI_PLAY_DEAD:
8302 // returns the data type of a specified argument to an operator. Argnum is 0 indexed.
8303 int query_operator_argument_type(int op, int argnum)
8309 Assert(index >= 0 && index < Num_operators);
8310 op = Operators[index].value;
8313 for (index=0; index<Num_operators; index++)
8314 if (Operators[index].value == op)
8317 Assert(index < Num_operators);
8320 if (argnum >= Operators[index].max)
8326 case OP_MISSION_TIME:
8329 case OP_WAYPOINT_MISSED:
8330 case OP_WAYPOINT_TWICE:
8332 case OP_GRANT_PROMOTION:
8333 case OP_WAS_PROMOTION_GRANTED:
8338 case OP_AND_IN_SEQUENCE:
8350 case OP_GREATER_THAN:
8352 case OP_HAS_TIME_ELAPSED:
8354 case OP_SET_TRAINING_CONTEXT_SPEED:
8355 case OP_END_MISSION_DELAY:
8356 case OP_SPECIAL_CHECK:
8357 case OP_AI_WARP_OUT:
8359 return OPF_POSITIVE;
8361 case OP_AI_WARP: // this operator is obsolete
8362 case OP_SET_TRAINING_CONTEXT_FLY_PATH:
8364 return OPF_WAYPOINT_PATH;
8368 case OP_AI_WAYPOINTS:
8369 case OP_AI_WAYPOINTS_ONCE:
8371 return OPF_WAYPOINT_PATH;
8373 return OPF_POSITIVE;
8375 case OP_IS_DISABLED:
8376 case OP_IS_DISARMED:
8377 case OP_TIME_SHIP_DESTROYED:
8378 case OP_TIME_SHIP_ARRIVED:
8379 case OP_TIME_SHIP_DEPARTED:
8380 case OP_SHIELDS_LEFT:
8382 case OP_CLEAR_SHIP_GOALS:
8383 case OP_PROTECT_SHIP:
8384 case OP_UNPROTECT_SHIP:
8385 case OP_BEAM_PROTECT_SHIP:
8386 case OP_BEAM_UNPROTECT_SHIP:
8387 case OP_TRANSFER_CARGO:
8388 case OP_EXCHANGE_CARGO:
8389 case OP_SHIP_INVISIBLE:
8390 case OP_SHIP_VISIBLE:
8391 case OP_SHIP_INVULNERABLE:
8392 case OP_SHIP_VULNERABLE:
8393 case OP_SHIP_GUARDIAN:
8394 case OP_SHIP_VANISH:
8395 case OP_SHIP_NO_GUARDIAN:
8396 case OP_SECONDARIES_DEPLETED:
8397 case OP_SPECIAL_WARP_DISTANCE:
8398 case OP_SET_SPECIAL_WARPOUT_NAME:
8399 case OP_IS_SHIP_VISIBLE:
8402 case OP_IS_DESTROYED:
8403 case OP_HAS_ARRIVED:
8404 case OP_HAS_DEPARTED:
8405 case OP_CLEAR_GOALS:
8406 return OPF_SHIP_WING;
8408 case OP_IS_DISABLED_DELAY:
8409 case OP_IS_DISARMED_DELAY:
8411 return OPF_POSITIVE;
8419 return OPF_POSITIVE;
8423 return OPF_WAYPOINT_PATH;
8425 return OPF_POSITIVE;
8430 return OPF_AI_ORDER;
8432 return OPF_SHIP_WING;
8434 case OP_IS_DESTROYED_DELAY:
8435 case OP_HAS_ARRIVED_DELAY:
8436 case OP_HAS_DEPARTED_DELAY:
8437 case OP_LAST_ORDER_TIME:
8439 return OPF_POSITIVE;
8441 return OPF_SHIP_WING;
8444 return OPF_SHIP_WING_POINT;
8446 case OP_MODIFY_VARIABLE:
8448 return OPF_VARIABLE_NAME;
8450 return OPF_AMBIGUOUS;
8454 case OP_HAS_UNDOCKED:
8455 case OP_HAS_DOCKED_DELAY:
8456 case OP_HAS_UNDOCKED_DELAY:
8457 case OP_TIME_DOCKED:
8458 case OP_TIME_UNDOCKED:
8462 return OPF_POSITIVE;
8464 case OP_TIME_WING_DESTROYED:
8465 case OP_TIME_WING_ARRIVED:
8466 case OP_TIME_WING_DEPARTED:
8467 case OP_CLEAR_WING_GOALS:
8470 case OP_IS_SUBSYSTEM_DESTROYED:
8471 case OP_HITS_LEFT_SUBSYSTEM:
8475 return OPF_SUBSYSTEM;
8480 else if (argnum == 1)
8481 return OPF_POSITIVE;
8483 return OPF_SUBSYSTEM;
8485 case OP_IS_SUBSYSTEM_DESTROYED_DELAY:
8488 else if ( argnum == 1 )
8489 return OPF_SUBSYSTEM;
8491 return OPF_POSITIVE;
8500 case OP_ADD_SHIP_GOAL:
8506 case OP_ADD_WING_GOAL:
8514 return OPF_SHIP_WING;
8525 case OP_AI_DISABLE_SHIP:
8526 case OP_AI_DISARM_SHIP:
8527 case OP_AI_EVADE_SHIP:
8528 case OP_AI_STAY_NEAR_SHIP:
8533 return OPF_POSITIVE;
8538 return OPF_SHIP_WING;
8540 return OPF_POSITIVE;
8543 case OP_AI_KEEP_SAFE_DISTANCE:
8544 return OPF_POSITIVE;
8549 else if (argnum == 1)
8550 return OPF_DOCKER_POINT;
8551 else if (argnum == 2)
8552 return OPF_DOCKEE_POINT;
8554 return OPF_POSITIVE;
8556 case OP_AI_CHASE_WING:
8557 case OP_AI_GUARD_WING:
8561 return OPF_POSITIVE;
8563 case OP_AI_DESTROY_SUBSYS:
8566 else if (argnum == 1)
8567 return OPF_SUBSYSTEM;
8569 return OPF_POSITIVE;
8574 case OP_SEND_MESSAGE:
8575 case OP_SEND_RANDOM_MESSAGE:
8577 return OPF_WHO_FROM;
8578 else if ( argnum == 1 )
8579 return OPF_PRIORITY;
8583 case OP_SEND_MESSAGE_LIST:
8586 // every four, the value repeats
8591 return OPF_WHO_FROM;
8593 return OPF_PRIORITY;
8597 return OPF_POSITIVE;
8599 case OP_TRAINING_MSG:
8603 return OPF_POSITIVE;
8605 case OP_SELF_DESTRUCT:
8608 case OP_NEXT_MISSION:
8609 return OPF_MISSION_NAME;
8611 case OP_END_CAMPAIGN:
8612 case OP_END_OF_CAMPAIGN:
8615 case OP_PREVIOUS_GOAL_TRUE:
8616 case OP_PREVIOUS_GOAL_FALSE:
8618 return OPF_MISSION_NAME;
8619 else if (argnum == 1 )
8620 return OPF_GOAL_NAME;
8624 case OP_PREVIOUS_GOAL_INCOMPLETE:
8625 return OPF_GOAL_NAME;
8627 case OP_PREVIOUS_EVENT_TRUE:
8628 case OP_PREVIOUS_EVENT_FALSE:
8629 case OP_PREVIOUS_EVENT_INCOMPLETE:
8631 return OPF_MISSION_NAME;
8632 else if ( argnum == 1 )
8633 return OPF_EVENT_NAME;
8637 case OP_SABOTAGE_SUBSYSTEM:
8638 case OP_REPAIR_SUBSYSTEM:
8639 case OP_SET_SUBSYSTEM_STRNGTH:
8641 return OPF_SHIP_NOT_PLAYER;
8642 else if (argnum == 1 )
8643 return OPF_SUBSYSTEM;
8645 return OPF_POSITIVE;
8647 case OP_WAYPOINTS_DONE:
8649 return OPF_SHIP_WING;
8651 return OPF_WAYPOINT_PATH;
8653 case OP_WAYPOINTS_DONE_DELAY:
8655 return OPF_SHIP_WING;
8656 else if ( argnum == 1 )
8657 return OPF_WAYPOINT_PATH;
8659 return OPF_POSITIVE;
8661 case OP_INVALIDATE_GOAL:
8662 case OP_VALIDATE_GOAL:
8663 return OPF_GOAL_NAME;
8665 case OP_SHIP_TYPE_DESTROYED:
8667 return OPF_POSITIVE;
8669 return OPF_SHIP_TYPE;
8671 case OP_KEY_PRESSED:
8673 return OPF_KEYPRESS;
8675 return OPF_POSITIVE;
8678 return OPF_KEYPRESS;
8681 case OP_EVENT_FALSE:
8682 return OPF_EVENT_NAME;
8684 case OP_EVENT_INCOMPLETE:
8685 case OP_EVENT_TRUE_DELAY:
8686 case OP_EVENT_FALSE_DELAY:
8688 return OPF_EVENT_NAME;
8690 return OPF_POSITIVE;
8692 case OP_GOAL_INCOMPLETE:
8693 case OP_GOAL_TRUE_DELAY:
8694 case OP_GOAL_FALSE_DELAY:
8696 return OPF_GOAL_NAME;
8698 return OPF_POSITIVE;
8700 case OP_AI_PLAY_DEAD:
8701 case OP_AI_CHASE_ANY:
8702 return OPF_POSITIVE;
8704 case OP_AI_STAY_STILL:
8706 return OPF_SHIP_POINT;
8708 return OPF_POSITIVE;
8710 case OP_GOOD_REARM_TIME:
8714 return OPF_POSITIVE;
8716 case OP_NUM_PLAYERS:
8717 return OPF_POSITIVE;
8719 case OP_SKILL_LEVEL_AT_LEAST:
8720 return OPF_SKILL_LEVEL;
8722 case OP_GRANT_MEDAL:
8723 case OP_WAS_MEDAL_GRANTED:
8724 return OPF_MEDAL_NAME;
8726 case OP_IS_CARGO_KNOWN:
8729 case OP_CARGO_KNOWN_DELAY:
8731 return OPF_POSITIVE;
8735 case OP_HAS_BEEN_TAGGED_DELAY:
8736 if ( argnum == 0 ) {
8737 return OPF_POSITIVE;
8742 case OP_CAP_SUBSYS_CARGO_KNOWN_DELAY:
8743 if ( argnum == 0 ) {
8744 return OPF_POSITIVE;
8745 } else if ( argnum == 1 ) {
8748 return OPF_SUBSYSTEM;
8752 case OP_TECH_ADD_SHIP:
8753 return OPF_SHIP_CLASS_NAME;
8755 case OP_ALLOW_WEAPON:
8756 case OP_TECH_ADD_WEAPON:
8757 return OPF_WEAPON_NAME;
8759 case OP_WARP_BROKEN:
8760 case OP_WARP_NOT_BROKEN:
8762 case OP_WARP_ALLOWED:
8765 case OP_FLASH_HUD_GAUGE:
8766 return OPF_HUD_GAUGE_NAME;
8768 case OP_GOOD_SECONDARY_TIME:
8771 else if ( argnum == 1 )
8772 return OPF_POSITIVE;
8773 else if ( argnum == 2 )
8774 return OPF_HUGE_WEAPON;
8778 case OP_PERCENT_SHIPS_DEPARTED:
8779 case OP_PERCENT_SHIPS_DESTROYED:
8781 return OPF_POSITIVE;
8783 return OPF_SHIP_WING;
8787 case OP_DEPART_NODE_DELAY:
8789 return OPF_POSITIVE;
8790 } else if ( argnum == 1 ){
8791 return OPF_JUMP_NODE_NAME;
8796 case OP_DESTROYED_DEPARTED_DELAY:
8798 return OPF_POSITIVE;
8800 return OPF_SHIP_WING;
8803 case OP_JETTISON_CARGO:
8807 return OPF_POSITIVE;
8810 case OP_CARGO_NO_DEPLETE:
8822 return OPF_SUBSYSTEM;
8826 return OPF_SUBSYSTEM;
8835 case OP_NUM_TYPE_KILLS:
8839 return OPF_SHIP_TYPE;
8842 case OP_NUM_CLASS_KILLS:
8846 return OPF_SHIP_CLASS_NAME;
8851 case OP_TURRET_FREE:
8852 case OP_TURRET_LOCK:
8856 return OPF_SUBSYSTEM;
8859 case OP_BEAM_FREE_ALL:
8860 case OP_BEAM_LOCK_ALL:
8861 case OP_TURRET_FREE_ALL:
8862 case OP_TURRET_LOCK_ALL:
8863 case OP_TURRET_TAGGED_ONLY_ALL:
8864 case OP_TURRET_TAGGED_CLEAR_ALL:
8867 case OP_ADD_REMOVE_ESCORT:
8874 case OP_AWACS_SET_RADIUS:
8877 } else if(argnum == 1){
8878 return OPF_AWACS_SUBSYSTEM;
8883 case OP_CAP_WAYPOINT_SPEED:
8890 case OP_SUBSYS_SET_RANDOM:
8893 } else if (argnum == 1 || argnum == 2) {
8896 return OPF_SUBSYSTEM;
8899 case OP_SUPERNOVA_START:
8902 case OP_SHIELD_RECHARGE_PCT:
8903 case OP_WEAPON_RECHARGE_PCT:
8904 case OP_ENGINE_RECHARGE_PCT:
8907 case OP_SHIELD_QUAD_LOW:
8914 case OP_SECONDARY_AMMO_PCT:
8921 case OP_IS_SECONDARY_SELECTED:
8922 case OP_IS_PRIMARY_SELECTED:
8936 void update_block_names(const char *old_name, const char *new_name)
8940 for (i=0; i<MAX_SEXP_VARIABLES; i++) {
8941 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
8942 if ( !stricmp(old_name, Sexp_variables[i].variable_name) ) {
8943 strcpy(Sexp_variables[i].variable_name, new_name);
8949 // DA: 1/7/99 Used to rename ships and waypoints, not variables
8950 // Strictly used in FRED
8951 void update_sexp_references(char *old_name, char *new_name)
8955 // update_block_names
8956 update_block_names(old_name, new_name);
8958 Assert(strlen(new_name) < TOKEN_LENGTH);
8959 for (i=0; i<MAX_SEXP_NODES; i++){
8960 if ((SEXP_NODE_TYPE(i) == SEXP_ATOM) && (Sexp_nodes[i].subtype == SEXP_ATOM_STRING)){
8961 if (!stricmp(CTEXT(i), old_name)){
8962 strcpy(CTEXT(i), new_name);
8968 // DA: 1/7/99 Used to rename event names, goal names, not variables
8969 // Strictly used in FRED
8970 void update_sexp_references(char *old_name, char *new_name, int format)
8974 Assert(strlen(new_name) < TOKEN_LENGTH);
8975 for (i=0; i<MAX_SEXP_NODES; i++){
8976 if (is_sexp_top_level(i)){
8977 update_sexp_references(old_name, new_name, format, i);
8982 // DA: 1/7/99 Used to rename event names, goal names, not variables
8983 // recursive function to update references to a certain type of data
8984 void update_sexp_references(char *old_name, char *new_name, int format, int node)
8992 if ((SEXP_NODE_TYPE(node) == SEXP_LIST) && (Sexp_nodes[node].subtype == SEXP_ATOM_LIST)) {
8993 if (Sexp_nodes[node].first){
8994 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].first);
8996 if (Sexp_nodes[node].rest){
8997 update_sexp_references(old_name, new_name, format, Sexp_nodes[node].rest);
9003 if (SEXP_NODE_TYPE(node) != SEXP_ATOM){
9007 if (Sexp_nodes[node].subtype != SEXP_ATOM_OPERATOR){
9011 op = identify_operator(CTEXT(node));
9012 Assert(Sexp_nodes[node].first < 0);
9013 n = Sexp_nodes[node].rest;
9016 if (SEXP_NODE_TYPE(n) == SEXP_LIST){
9017 update_sexp_references(old_name, new_name, format, Sexp_nodes[n].first);
9019 Assert((SEXP_NODE_TYPE(n) == SEXP_ATOM) && ((Sexp_nodes[n].subtype == SEXP_ATOM_NUMBER) || (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)));
9020 if (query_operator_argument_type(op, i) == format) {
9021 if (!stricmp(CTEXT(n), old_name)){
9022 strcpy(CTEXT(n), new_name);
9027 n = Sexp_nodes[n].rest;
9032 int query_referenced_in_sexp(int mode, char *name, int *node)
9036 for (n=0; n<MAX_SEXP_NODES; n++){
9037 if ((SEXP_NODE_TYPE(n) == SEXP_ATOM) && (Sexp_nodes[n].subtype == SEXP_ATOM_STRING)){
9038 if (!stricmp(CTEXT(n), name)){
9044 if (n == MAX_SEXP_NODES){
9052 // so we know it's being used somewhere.. Time to find out where..
9053 for (i=0; i<MAX_SHIPS; i++)
9054 if (Ships[i].objnum >= 0) {
9055 if (query_node_in_sexp(n, Ships[i].arrival_cue)){
9056 return i | SRC_SHIP_ARRIVAL;
9058 if (query_node_in_sexp(n, Ships[i].departure_cue)){
9059 return i | SRC_SHIP_DEPARTURE;
9063 for (i=0; i<MAX_WINGS; i++){
9064 if (Wings[i].wave_count) {
9065 if (query_node_in_sexp(n, Wings[i].arrival_cue)){
9066 return i | SRC_WING_ARRIVAL;
9068 if (query_node_in_sexp(n, Wings[i].departure_cue)){
9069 return i | SRC_WING_DEPARTURE;
9074 for (i=0; i<Num_mission_events; i++){
9075 if (query_node_in_sexp(n, Mission_events[i].formula)){
9076 return i | SRC_EVENT;
9080 for (i=0; i<Num_goals; i++){
9081 if (query_node_in_sexp(n, Mission_goals[i].formula)){
9082 return i | SRC_MISSION_GOAL;
9086 for (j=0; j<Num_teams; j++) {
9087 for (i=0; i<Debriefings[j].num_stages; i++) {
9088 if (query_node_in_sexp(n, Debriefings[j].stages[i].formula)){
9089 return i | SRC_DEBRIEFING;
9094 for (j=0; j<Num_teams; j++) {
9095 for (i=0; i<Briefings[j].num_stages; i++) {
9096 if (query_node_in_sexp(n, Briefings[j].stages[i].formula)){
9097 return i | SRC_BRIEFING;
9105 int verify_vector(char *text)
9110 for (i=0; i<Num_waypoint_lists; i++) {
9111 len = strlen(str = Waypoint_lists[i].name);
9112 if (!strnicmp(str, text, len)){
9113 if (!text[len] || text[len] == ':'){
9119 if (i < Num_waypoint_lists) {
9121 return 0; // a valid waypoint path
9124 str = &text[len + 1];
9126 if (!isdigit(*str++)){
9127 return -1; // not a valid number
9131 z = atoi(&text[len + 1]);
9132 if (z < 1 || z > Waypoint_lists[i].count){
9133 return -1; // out of range
9136 return 0; // a valid waypoint
9140 if (text[0] != '(' || text[len - 1] != ')'){
9152 if (validate_float(&str)){
9157 if (validate_float(&str)){
9162 if (validate_float(&str)){
9180 void skip_white(char **str)
9182 if ((**str == ' ') || (**str == '\t')){
9187 int validate_float(char **str)
9189 int count = 0, dot = 0;
9191 while (isdigit(**str) || **str == '.') {
9211 // check if operator return type opr is a valid match for operator argument type opf
9212 int sexp_query_type_match(int opf, int opr)
9216 return ((opr == OPR_NUMBER) || (opr == OPR_POSITIVE));
9219 return (opr == OPR_POSITIVE);
9222 return (opr == OPR_BOOL);
9225 return (opr == OPR_NULL);
9228 return (opr == OPR_AI_GOAL);
9234 char *sexp_error_message(int num)
9237 case SEXP_CHECK_NONOP_ARGS:
9238 return "Data shouldn't have arguments";
9240 case SEXP_CHECK_OP_EXPTECTED:
9241 return "Operator expected instead of data";
9243 case SEXP_CHECK_UNKNOWN_OP:
9244 return "Unrecognized operator";
9246 case SEXP_CHECK_TYPE_MISMATCH:
9247 return "Argument type mismatch";
9249 case SEXP_CHECK_BAD_ARG_COUNT:
9250 return "Argument count is illegal";
9252 case SEXP_CHECK_UNKNOWN_TYPE:
9253 return "Unknown operator argument type";
9255 case SEXP_CHECK_INVALID_NUM:
9256 return "Not a number";
9258 case SEXP_CHECK_INVALID_SHIP:
9259 return "Invalid ship name";
9261 case SEXP_CHECK_INVALID_WING:
9262 return "Invalid wing name";
9264 case SEXP_CHECK_INVALID_SUBSYS:
9265 return "Invalid subsystem name";
9267 case SEXP_CHECK_INVALID_IFF:
9268 return "Invalid team name";
9270 case SEXP_CHECK_INVALID_POINT:
9271 return "Invalid point";
9273 case SEXP_CHECK_NEGATIVE_NUM:
9274 return "Negative number not allowed";
9276 case SEXP_CHECK_INVALID_SHIP_WING:
9277 return "Invalid ship/wing name";
9279 case SEXP_CHECK_INVALID_SHIP_TYPE:
9280 return "Invalid ship type";
9282 case SEXP_CHECK_UNKNOWN_MESSAGE:
9283 return "Invalid message name";
9285 case SEXP_CHECK_INVALID_PRIORITY:
9286 return "Invalid priority";
9288 case SEXP_CHECK_INVALID_MISSION_NAME:
9289 return "Invalid mission filename";
9291 case SEXP_CHECK_INVALID_GOAL_NAME:
9292 return "Invalid goal name";
9294 case SEXP_CHECK_INVALID_LEVEL:
9295 return "Mission level too low in tree";
9297 case SEXP_CHECK_INVALID_MSG_SOURCE:
9298 return "Invalid message source";
9300 case SEXP_CHECK_INVALID_DOCKER_POINT:
9301 return "Invalid docker point";
9303 case SEXP_CHECK_INVALID_DOCKEE_POINT:
9304 return "Invalid dockee point";
9306 case SEXP_CHECK_ORDER_NOT_ALLOWED:
9307 return "Ship not allowed to have this order";
9309 case SEXP_CHECK_DOCKING_NOT_ALLOWED:
9310 return "Ship can't dock with target ship";
9312 case SEXP_CHECK_NUM_RANGE_INVALID:
9313 return "Number is out of range";
9315 case SEXP_CHECK_INVALID_EVENT_NAME:
9316 return "Event name is invalid (not known)";
9318 case SEXP_CHECK_INVALID_SKILL_LEVEL:
9319 return "Skill level name is invalid (not known)";
9321 case SEXP_CHECK_INVALID_MEDAL_NAME:
9322 return "Invalid medal name";
9324 case SEXP_CHECK_INVALID_WEAPON_NAME:
9325 return "Invalid weapon name";
9327 case SEXP_CHECK_INVALID_SHIP_CLASS_NAME:
9328 return "Invalid ship class name";
9330 case SEXP_CHECK_INVALID_GAUGE_NAME:
9331 return "Invalid HUD gauges name";
9333 case SEXP_CHECK_INVALID_JUMP_NODE:
9334 return "Invalid Jump Node name";
9337 sprintf(Sexp_error_text, "Sexp error code %d", num);
9338 return Sexp_error_text;
9341 int query_sexp_ai_goal_valid(int sexp_ai_goal, int ship)
9345 for (op=0; op<Num_operators; op++)
9346 if (Operators[op].value == sexp_ai_goal)
9349 Assert(op < Num_operators);
9350 for (i=0; i<Num_sexp_ai_goal_links; i++)
9351 if (Sexp_ai_goal_links[i].op_code == sexp_ai_goal)
9354 Assert(i < Num_sexp_ai_goal_links);
9355 return ai_query_goal_valid(ship, Sexp_ai_goal_links[i].ai_goal);
9358 // Takes an Sexp_node.text pointing to a variable (of form "Sexp_variables[xx]=string" or "Sexp_variables[xx]=number")
9359 // and returns the index into the Sexp_variables array of the actual value
9360 int extract_sexp_variable_index(int node)
9362 char *text = Sexp_nodes[node].text;
9368 start_index = text + 15;
9369 Assert(isdigit(*start_index));
9373 while ( *start_index != ']' ) {
9374 char_index[len++] = *(start_index++);
9379 char_index[len] = 0; // append null termination to string
9381 variable_index = atoi(char_index);
9382 Assert( (variable_index >= 0) && (variable_index < MAX_SEXP_VARIABLES) );
9384 return variable_index;
9388 // wrapper around Sexp_node[xx].text for normal and variable
9391 if (Sexp_nodes[n].type & SEXP_FLAG_VARIABLE) {
9392 int sexp_variable_index;
9394 sexp_variable_index = get_index_sexp_variable_name(Sexp_nodes[n].text);
9395 Assert(sexp_variable_index != -1);
9397 // sexp_variable_index = extract_sexp_variable_index(n);
9398 sexp_variable_index = atoi(Sexp_nodes[n].text);
9400 // Reference a Sexp_variable
9401 // string format -- "Sexp_variables[xx]=number" or "Sexp_variables[xx]=string", where xx is the index
9403 Assert( !(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NOT_USED) );
9404 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9406 return Sexp_variables[sexp_variable_index].text;
9408 return Sexp_nodes[n].text;
9413 // Set all Sexp_variables to type uninitialized
9414 void init_sexp_vars()
9416 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9417 // if ( !(Sexp_nodes[i].type & SEXP_FLAG_PERSISTENT) )
9418 // Sexp_nodes[i].type = SEXP_NOT_USED;
9419 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9424 // Adds an Sexp_variable to be used in a mission.
9425 // This should be called from within mission parse.
9426 int sexp_add_variable(const char *text, const char *var_name, int type, int index)
9428 // if index == -1, find next open slot
9430 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9431 if (Sexp_variables[i].type == SEXP_VARIABLE_NOT_USED) {
9437 Assert( (index >= 0) && (index < MAX_SEXP_VARIABLES) );
9441 strcpy(Sexp_variables[index].text, text);
9442 strcpy(Sexp_variables[index].variable_name, var_name);
9443 Sexp_variables[index].type &= ~SEXP_VARIABLE_NOT_USED;
9444 Sexp_variables[index].type = (type | SEXP_VARIABLE_SET);
9451 // Modifies and Sexp_variable to be used in a mission
9452 // This should be called in mission when an sexp_variable is to be modified
9453 void sexp_modify_variable(char *text, int index)
9455 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9456 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9457 Assert( !MULTIPLAYER_CLIENT );
9459 strcpy(Sexp_variables[index].text, text);
9460 Sexp_variables[index].type |= SEXP_VARIABLE_MODIFIED;
9462 // do multi_callback_here
9465 void sexp_modify_variable(int n)
9467 int sexp_variable_index;
9469 // char *new_char_var;
9470 char number_as_str[TOKEN_LENGTH];
9472 // Only do single player of multi host
9473 if ( MULTIPLAYER_CLIENT ) {
9479 // get sexp_variable index
9480 Assert(Sexp_nodes[n].first == -1);
9481 sexp_variable_index = atoi(Sexp_nodes[n].text);
9483 // verify variable set
9484 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_SET);
9486 if (Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_NUMBER) {
9487 // get new numerical value
9488 new_number = eval_sexp(Sexp_nodes[n].rest);
9490 sprintf(number_as_str, "%d", new_number);
9491 sexp_modify_variable(number_as_str, sexp_variable_index);
9494 Assert(Sexp_variables[sexp_variable_index].type & SEXP_VARIABLE_STRING);
9496 char *new_text = Sexp_nodes[Sexp_nodes[n].rest].text;
9497 sexp_modify_variable(new_text, sexp_variable_index);
9504 // Different type needed for Fred (1) allow modification of type (2) no callback required
9505 void sexp_fred_modify_variable(const char *text, const char *var_name, int index, int type)
9507 Assert(index >= 0 && index < MAX_SEXP_VARIABLES);
9508 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9509 Assert( (type & SEXP_VARIABLE_NUMBER) || (type & SEXP_VARIABLE_STRING) );
9511 strcpy(Sexp_variables[index].text, text);
9512 strcpy(Sexp_variables[index].variable_name, var_name);
9513 Sexp_variables[index].type = (SEXP_VARIABLE_SET | SEXP_VARIABLE_MODIFIED | type);
9516 // return index of sexp_variable_name, -1 if not found
9517 int get_index_sexp_variable_name(const char* temp_name)
9519 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9520 if (Sexp_variables[i].type & SEXP_VARIABLE_SET) {
9521 // check case sensitive
9522 if ( !strcmp(Sexp_variables[i].variable_name, temp_name) ) {
9533 // counts number of sexp_variables that are set
9534 int sexp_variable_count()
9538 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9539 if ( (Sexp_variables[i].type & SEXP_VARIABLE_SET) && !(Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) ) {
9548 // deletes sexp_variable from active
9549 void sexp_variable_delete(int index)
9551 Assert(Sexp_variables[index].type & SEXP_VARIABLE_SET);
9553 Sexp_variables[index].type = SEXP_VARIABLE_NOT_USED;
9556 int sexp_var_compare(const void *var1, const void *var2)
9559 sexp_variable *sexp_var1, *sexp_var2;
9561 sexp_var1 = (sexp_variable*) var1;
9562 sexp_var2 = (sexp_variable*) var2;
9564 set1 = sexp_var1->type & SEXP_VARIABLE_SET;
9565 set2 = sexp_var2->type & SEXP_VARIABLE_SET;
9567 if (!set1 && !set2) {
9569 } else if (set1 && !set2) {
9571 } else if (!set1 && set2) {
9574 return stricmp( sexp_var1->variable_name, sexp_var2->variable_name);
9578 // Count number of variables in block
9579 int sexp_variable_block_count()
9582 for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
9583 if (Sexp_variables[i].type & SEXP_VARIABLE_BLOCK) {
9591 // Sort sexp_variable list lexigraphically, with set before unset
9592 void sexp_variable_sort()
9594 qsort( (void *)Sexp_variables, (size_t)(MAX_SEXP_VARIABLES - sexp_variable_block_count()), sizeof(sexp_variable), sexp_var_compare );
9597 int sexp_variable_allocate_block(const char* block_name, int block_type)
9599 int num_blocks, block_count, var_count, start;
9600 block_count = sexp_variable_block_count();
9601 var_count = sexp_variable_count();
9603 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9604 num_blocks = BLOCK_EXP_SIZE;
9606 Int3(); // add new block type here with size
9610 if (block_count + var_count > (MAX_SEXP_VARIABLES - num_blocks)) {
9611 // not enough free space
9615 // squeeze all variables to front of array
9616 sexp_variable_sort();
9618 // squeeze all block to end of array
9619 sexp_variable_condense_block();
9621 start = MAX_SEXP_VARIABLES - block_count - num_blocks;
9623 for (int idx=start; idx<start+num_blocks; idx++) {
9624 Assert(Sexp_variables[idx].type == SEXP_VARIABLE_NOT_USED);
9625 Sexp_variables[idx].type = SEXP_VARIABLE_BLOCK | block_type;
9626 strcpy(Sexp_variables[idx].variable_name, block_name);
9632 // squeeze all blocks to top of array
9633 void sexp_variable_condense_block()
9635 int temp_idx, idx, var_count;
9637 var_count = sexp_variable_count();
9638 temp_idx = MAX_SEXP_VARIABLES-1;
9640 for (idx=MAX_SEXP_VARIABLES-1; idx>var_count-1; idx--) {
9641 if (Sexp_variables[idx].type & SEXP_VARIABLE_BLOCK) {
9642 if (temp_idx > idx) {
9643 Sexp_variables[temp_idx] = Sexp_variables[idx];
9644 Sexp_variables[idx].type = SEXP_VARIABLE_NOT_USED;
9652 void sexp_variable_block_free(const char *ship_name, int start_index, int block_type)
9656 if (block_type & SEXP_VARIABLE_BLOCK_EXP) {
9657 num_blocks = BLOCK_EXP_SIZE;
9659 Int3(); // new type of block
9663 for (int i=start_index; i<(start_index + num_blocks); i++) {
9664 Assert(!stricmp(Sexp_variables[i].variable_name, ship_name));
9666 Assert(Sexp_variables[i].type & block_type);
9668 Sexp_variables[i].type = SEXP_VARIABLE_NOT_USED;
9671 sexp_variable_condense_block();
9674 // evaluate number which may result from an operator or may be text
9675 // only useful for first operand
9676 int num_eval(int node)
9678 if ( CAR(node) != -1 ) {
9679 return eval_sexp(CAR(node));
9681 return atoi(CTEXT(node));