-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathmission.cpp
More file actions
3262 lines (2665 loc) · 106 KB
/
mission.cpp
File metadata and controls
3262 lines (2665 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
//
#include "mission.h"
#include "globalincs/linklist.h"
#include "freespace.h"
#include "asteroid/asteroid.h"
#include "debris/debris.h"
#include "executor/GameStateExecutionContext.h"
#include "executor/global_executors.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/eventmusic.h"
#include "hud/hudescort.h"
#include "hud/hudmessage.h"
#include "hud/hudsquadmsg.h"
#include "iff_defs/iff_defs.h"
#include "mission/missioncampaign.h"
#include "mission/missiongoals.h"
#include "mission/missionload.h"
#include "mission/missionlog.h"
#include "mission/missionmessage.h"
#include "mission/missiontraining.h"
#include "missionui/missionbrief.h"
#include "missionui/missioncmdbrief.h"
#include "missionui/redalert.h"
#include "nebula/neb.h"
#include "nebula/neblightning.h"
#include "object/objcollide.h"
#include "parse/parselo.h"
#include "parse/sexp.h"
#include "parse/sexp/DynamicSEXP.h"
#include "parse/sexp/LuaSEXP.h"
#include "parse/sexp/LuaAISEXP.h"
#include "parse/sexp/sexp_lookup.h"
#include "playerman/player.h"
#include "prop/prop.h"
#include "scripting/api/LuaPromise.h"
#include "scripting/api/objs/LuaEnum.h"
#include "scripting/api/objs/LuaSEXP.h"
#include "scripting/api/objs/luaaisexp.h"
#include "scripting/api/objs/asteroid.h"
#include "scripting/api/objs/animation_handle.h"
#include "scripting/api/objs/background_element.h"
#include "scripting/api/objs/beam.h"
#include "scripting/api/objs/comm_order.h"
#include "scripting/api/objs/debris.h"
#include "scripting/api/objs/enums.h"
#include "scripting/api/objs/event.h"
#include "scripting/api/objs/fireball.h"
#include "scripting/api/objs/fireballclass.h"
#include "scripting/api/objs/goal.h"
#include "scripting/api/objs/message.h"
#include "scripting/api/objs/model.h"
#include "scripting/api/objs/modelinstance.h"
#include "scripting/api/objs/object.h"
#include "scripting/api/objs/parse_object.h"
#include "scripting/api/objs/promise.h"
#include "scripting/api/objs/prop.h"
#include "scripting/api/objs/propclass.h"
#include "scripting/api/objs/sexpvar.h"
#include "scripting/api/objs/ship_registry_entry.h"
#include "scripting/api/objs/ship.h"
#include "scripting/api/objs/shipclass.h"
#include "scripting/api/objs/sound.h"
#include "scripting/api/objs/team.h"
#include "scripting/api/objs/vecmath.h"
#include "scripting/api/objs/volumetric.h"
#include "scripting/api/objs/waypoint.h"
#include "scripting/api/objs/weapon.h"
#include "scripting/api/objs/weaponclass.h"
#include "scripting/api/objs/wing.h"
#include "scripting/lua/LuaConvert.h"
#include "scripting/lua/LuaFunction.h"
#include "scripting/lua/LuaTable.h"
#include "scripting/global_hooks.h"
#include "scripting/scripting.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "starfield/starfield.h"
#include "weapon/beam.h"
#include "weapon/weapon.h"
#include <utility>
extern bool Ships_inited;
extern bool Game_shudder_perpetual;
extern bool Game_shudder_everywhere;
extern TIMESTAMP Game_shudder_time;
extern int Game_shudder_total;
extern float Game_shudder_intensity;
constexpr int COUNT_OBJECTS = -1000;
// Returns the indexth object subclass
template <typename A>
int object_subclass_at_index(A& object_subclass_array, size_t array_size, int index)
{
int count = 0;
for (size_t i = 0; i < array_size; ++i)
{
int objnum = object_subclass_array[i].objnum;
if (objnum < 0 || objnum >= MAX_OBJECTS)
continue;
if (Objects[objnum].flags[Object::Object_Flags::Should_be_dead])
continue;
++count;
if (count == index)
{
return object_subclass_array[i].objnum;
}
}
if (index == COUNT_OBJECTS)
return count;
else
return -1;
}
// Counts the number of indexable objects
template <typename A>
int object_subclass_count(A& object_subclass_array, size_t array_size)
{
return object_subclass_at_index(object_subclass_array, array_size, COUNT_OBJECTS);
}
// Overload for a vector of std::optional objects
template <typename T>
int object_subclass_at_index(const SCP_vector<std::optional<T>>& vec, int index)
{
int count = 0;
for (const auto& opt_obj : vec) {
if (!opt_obj.has_value()) {
continue;
}
const T& obj = *opt_obj;
int objnum = obj.objnum;
if (objnum < 0 || objnum >= MAX_OBJECTS)
continue;
if (Objects[objnum].flags[Object::Object_Flags::Should_be_dead])
continue;
++count;
if (count == index) {
return obj.objnum;
}
}
if (index == COUNT_OBJECTS)
return count;
else
return -1;
}
template <typename A>
int object_subclass_count(A& object_subclass_array)
{
return object_subclass_at_index(object_subclass_array, COUNT_OBJECTS);
}
namespace scripting {
namespace api {
//**********LIBRARY: Mission
ADE_LIB(l_Mission, "Mission", "mn", "Mission library");
// for use in creating faster metadata systems, use in conjunction with getSignature()
ADE_FUNC(getObjectFromSignature, l_Mission, "number Signature", "Gets a handle of an object from its signature", "object", "Handle of object with signaure, invalid handle if signature is not in use")
{
int sig = -1;
int objnum;
if(!ade_get_args(L, "i", &sig))
return ade_set_error(L, "o", l_Object.Set(object_h()));
if (sig == -1) {
return ade_set_error(L, "o", l_Object.Set(object_h()));
}
objnum = obj_get_by_signature(sig);
return ade_set_object_with_breed(L, objnum);
}
ADE_FUNC(evaluateSEXP, l_Mission, "string", "Runs the defined SEXP script, and returns the result as a boolean", "boolean", "true if the SEXP returned SEXP_TRUE or SEXP_KNOWN_TRUE; false if the SEXP returned anything else (even a number)")
{
const char* s;
int r_val;
if(!ade_get_args(L, "s", &s))
return ADE_RETURN_FALSE;
r_val = run_sexp(s);
if (r_val == SEXP_TRUE)
return ADE_RETURN_TRUE;
else
return ADE_RETURN_FALSE;
}
ADE_FUNC(evaluateNumericSEXP, l_Mission, "string", "Runs the defined SEXP script, and returns the result as a number", "number", "the value of the SEXP result (or NaN if the SEXP returned SEXP_NAN or SEXP_NAN_FOREVER)")
{
const char* s;
int r_val;
bool got_nan;
if (!ade_get_args(L, "s", &s))
return ade_set_args(L, "i", 0);
r_val = run_sexp(s, true, &got_nan);
if (got_nan)
return ade_set_args(L, "f", std::numeric_limits<float>::quiet_NaN());
else
return ade_set_args(L, "i", r_val);
}
ADE_FUNC(runSEXP, l_Mission, "string", "Runs the defined SEXP script within a `when` operator", "boolean", "if the operation was successful")
{
const char* s;
int r_val;
SCP_string buf;
if(!ade_get_args(L, "s", &s))
return ADE_RETURN_FALSE;
while (is_white_space(*s))
s++;
if (*s != '(')
{
static bool Warned_about_runSEXP_parentheses = false;
if (!Warned_about_runSEXP_parentheses)
{
Warned_about_runSEXP_parentheses = true;
Warning(LOCATION, "Invalid SEXP syntax: SEXPs must be surrounded by parentheses. For backwards compatibility, the string has been enclosed in parentheses. This may not be correct in all use cases.\n\nrunSEXP string:\n%s\n", s);
}
// this is the old sexp handling method, which is incorrect
sprintf(buf, "( when ( true ) ( %s ) )", s);
}
else
{
// this is correct usage
sprintf(buf, "( when ( true ) %s )", s);
}
r_val = run_sexp(buf.c_str());
if (r_val == SEXP_TRUE)
return ADE_RETURN_TRUE;
else
return ADE_RETURN_FALSE;
}
//****SUBLIBRARY: Mission/Asteroids
ADE_LIB_DERIV(l_Mission_Asteroids, "Asteroids", NULL, "Asteroids in the mission", l_Mission);
ADE_INDEXER(l_Mission_Asteroids, "number Index", "Gets asteroid", "asteroid", "Asteroid handle, or invalid handle if invalid index specified")
{
int idx = -1;
if( !ade_get_args(L, "*i", &idx) ) {
return ade_set_error( L, "o", l_Asteroid.Set( object_h() ) );
}
int objnum = -1;
if (idx > 0)
objnum = object_subclass_at_index(Asteroids, MAX_ASTEROIDS, idx);
return ade_set_args(L, "o", l_Asteroid.Set( object_h(objnum) ) );
}
ADE_FUNC(__len, l_Mission_Asteroids, NULL,
"Number of asteroids in mission. Note that the value returned is only good until an asteroid is destroyed, and so cannot be relied on for more than one frame.",
"number",
"Number of asteroids in the mission, or 0 if asteroids are not enabled")
{
if(Asteroids_enabled) {
return ade_set_args(L, "i", object_subclass_count(Asteroids, MAX_ASTEROIDS));
}
return ade_set_args(L, "i", 0);
}
//****SUBLIBRARY: Mission/Comm Items
ADE_LIB_DERIV(l_Mission_Comm_Items, "CommItems", nullptr, "Comm Items in the mission", l_Mission);
ADE_INDEXER(l_Mission_Comm_Items,
"number Index",
"Gets comm items",
"comm_item",
"Comm Item handle, or invalid handle if invalid index specified")
{
int idx;
if (!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "s", "");
// convert from lua index
idx--;
if ((idx < 0) || idx >= MAX_MENU_ITEMS)
return ade_set_args(L, "o", l_Comm_Item.Set(-1));
return ade_set_args(L, "o", l_Comm_Item.Set(idx));
}
ADE_FUNC(__len,
l_Mission_Comm_Items,
nullptr,
"Number of comm orders in mission currently. Note that the value will change when an order is selected or the open/closed state of the comm menu is changed.",
"number",
"Number of comm orders in the mission. 0 if comm menu is closed")
{
return ade_set_args(L, "i", Num_menu_items);
}
//****SUBLIBRARY: Mission/Debris
ADE_LIB_DERIV(l_Mission_Debris, "Debris", NULL, "debris in the mission", l_Mission);
ADE_INDEXER(l_Mission_Debris, "number Index", "Array of debris in the current mission", "debris", "Debris handle, or invalid debris handle if index wasn't valid")
{
int idx = -1;
if( !ade_get_args( L, "*i", &idx ) ) {
return ade_set_error(L, "o", l_Debris.Set(object_h()));
}
idx--; // Lua -> C
if( idx >= 0 && idx < (int)Debris.size() ) {
if (Debris[idx].objnum == -1) //Somehow accessed an invalid debris piece
return ade_set_error(L, "o", l_Debris.Set(object_h()));
return ade_set_args(L, "o", l_Debris.Set(object_h(Debris[idx].objnum)));
}
return ade_set_error(L, "o", l_Debris.Set(object_h()));
}
ADE_FUNC(__len, l_Mission_Debris, NULL,
"Number of debris pieces in the mission. "
"Note that the value returned is only good until a piece of debris is destroyed, and so cannot be relied on for more than one frame.",
"number",
"Current number of debris particles")
{
return ade_set_args(L, "i", (int)Debris.size());
}
//****SUBLIBRARY: Mission/EscortShips
ADE_LIB_DERIV(l_Mission_EscortShips, "EscortShips", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_EscortShips, "number Index", "Gets escort ship at specified index on escort list", "ship", "Specified ship, or invalid ship handle if invalid index")
{
int idx;
if(!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "o", l_Ship.Set(object_h()));
if(idx < 1 || idx > hud_escort_num_ships_on_list())
return ade_set_error(L, "o", l_Ship.Set(object_h()));
//Lua->FS2
idx--;
idx = hud_escort_return_objnum(idx);
if(idx < 0)
return ade_set_error(L, "o", l_Ship.Set(object_h()));
return ade_set_args(L, "o", l_Ship.Set(object_h(idx)));
}
ADE_FUNC(__len, l_Mission_EscortShips, NULL, "Current number of escort ships", "number", "Current number of escort ships")
{
return ade_set_args(L, "i", hud_escort_num_ships_on_list());
}
//****SUBLIBRARY: Mission/Events
ADE_LIB_DERIV(l_Mission_Events, "Events", nullptr, "Events", l_Mission);
ADE_INDEXER(l_Mission_Events, "number/string IndexOrName", "Indexes mission events list", "mission_event", "Event handle, or invalid event handle if index was invalid")
{
const char* s;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Event.Set(-1));
int i = mission_event_lookup(s);
if (i >= 0)
return ade_set_args(L, "o", l_Event.Set(i));
//Now try as a number
i = atoi(s);
//Lua-->FS2
i--;
if(!SCP_vector_inbounds(Mission_events, i))
return ade_set_error(L, "o", l_Event.Set(-1));
return ade_set_args(L, "o", l_Event.Set(i));
}
ADE_FUNC(__len, l_Mission_Events, nullptr, "Number of events in mission", "number", "Number of events in mission")
{
return ade_set_args(L, "i", static_cast<int>(Mission_events.size()));
}
//****SUBLIBRARY: Mission/Goals
ADE_LIB_DERIV(l_Mission_Goals, "Goals", nullptr, "Goals", l_Mission);
ADE_INDEXER(l_Mission_Goals, "number/string IndexOrName", "Indexes mission goals list", "mission_goal", "Goal handle, or invalid goal handle if index was invalid")
{
const char* s;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Goal.Set(-1));
int i = mission_goal_lookup(s);
if (i >= 0)
return ade_set_args(L, "o", l_Goal.Set(i));
//Now try as a number
i = atoi(s);
//Lua-->FS2
i--;
if(!SCP_vector_inbounds(Mission_goals, i))
return ade_set_error(L, "o", l_Goal.Set(-1));
return ade_set_args(L, "o", l_Goal.Set(i));
}
ADE_FUNC(__len, l_Mission_Goals, nullptr, "Number of goals in mission", "number", "Number of goals in mission")
{
return ade_set_args(L, "i", static_cast<int>(Mission_goals.size()));
}
//****SUBLIBRARY: Mission/SEXPVariables
ADE_LIB_DERIV(l_Mission_SEXPVariables, "SEXPVariables", NULL, "SEXP Variables", l_Mission);
ADE_INDEXER(l_Mission_SEXPVariables, "number/string IndexOrName", "Array of SEXP variables. Note that you can set a sexp variable using the array, eg \'SEXPVariables[\"newvariable\"] = \"newvalue\"\'", "sexpvariable", "Handle to SEXP variable, or invalid sexpvariable handle if index was invalid")
{
const char* name = nullptr;
const char* newval = nullptr;
if(!ade_get_args(L, "*s|s", &name, &newval))
return ade_set_error(L, "o", l_SEXPVariable.Set(sexpvar_h()));
int idx = get_index_sexp_variable_name(name);
if(idx < 0)
{
idx = atoi(name);
//Lua-->FS2
idx--;
}
if(idx < 0 || idx >= MAX_SEXP_VARIABLES)
{
if(ADE_SETTING_VAR && newval != NULL)
{
idx = sexp_add_variable(newval, name, lua_type(L, 2) == LUA_TNUMBER ? SEXP_VARIABLE_NUMBER : SEXP_VARIABLE_STRING);
}
//We have failed.
if(idx < 0)
{
return ade_set_error(L, "o", l_SEXPVariable.Set(sexpvar_h()));
}
}
else
{
if(ADE_SETTING_VAR && newval != NULL)
{
sexp_modify_variable(newval, idx, false);
}
}
return ade_set_args(L, "o", l_SEXPVariable.Set(sexpvar_h(idx)));
}
ADE_FUNC(__len, l_Mission_SEXPVariables, NULL, "Current number of SEXP variables", "number", "Counts number of loaded SEXP Variables. May be slow.")
{
return ade_set_args(L, "i", sexp_variable_count());
}
//****SUBLIBRARY: Mission/ShipRegistry
ADE_LIB_DERIV(l_Mission_ShipRegistry, "ShipRegistry", nullptr, "The mission's ship registry: all ships parsed, created, or exited that the mission knows about", l_Mission);
ADE_INDEXER(l_Mission_ShipRegistry, "number/string IndexOrName", "Gets ship registry entry", "ship_registry_entry", "Ship registry entry handle, or invalid handle if index was invalid")
{
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_ShipRegistryEntry.Set(-1));
int idx = ship_registry_get_index(name);
if (idx < 0)
{
idx = atoi(name); // will return 0 if not parseable
//Lua-->FS2
idx--;
}
return ade_set_args(L, "o", l_ShipRegistryEntry.Set(idx));
}
ADE_FUNC(__len, l_Mission_ShipRegistry, nullptr,
"Number of ship registry entries in the mission. The value returned is generally stable but will change if a ship is created using ship-create or if additional wing waves arrive.",
"number",
"Number of ship registry entries in the mission")
{
return ade_set_args(L, "i", (int)Ship_registry.size());
}
//****SUBLIBRARY: Mission/Ships
ADE_LIB_DERIV(l_Mission_Ships, "Ships", NULL, "Ships in the mission", l_Mission);
ADE_INDEXER(l_Mission_Ships, "number/string IndexOrName", "Gets ship", "ship", "Ship handle, or invalid ship handle if index was invalid")
{
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_Ship.Set(object_h()));
int objnum = -1;
auto entry = ship_registry_get(name);
if (entry)
{
if (entry->has_objp())
objnum = entry->objnum;
}
else
{
int idx = atoi(name);
if (idx > 0)
objnum = object_subclass_at_index(Ships, MAX_SHIPS, idx);
}
return ade_set_args(L, "o", l_Ship.Set(object_h(objnum)));
}
ADE_FUNC(__len, l_Mission_Ships, NULL,
"Number of ships in the mission. "
"This function is somewhat slow, and should be set to a variable for use in looping situations. "
"Note that the value returned is only good until a ship is destroyed, and so cannot be relied on for more than one frame.",
"number",
"Number of ships in the mission, or 0 if ships haven't been initialized yet")
{
return ade_set_args(L, "i", object_subclass_count(Ships, MAX_SHIPS));
}
//****SUBLIBRARY: Mission/ParsedShips
ADE_LIB_DERIV(l_Mission_ParsedShips, "ParsedShips", nullptr, "Parsed ships (aka parse objects) in the mission", l_Mission);
ADE_INDEXER(l_Mission_ParsedShips, "number/string IndexOrName", "Gets parsed ship", "parse_object", "Parsed ship handle, or invalid handle if index was invalid")
{
const char *name;
if (!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_ParseObject.Set(parse_object_h(nullptr)));
auto ship_entry = ship_registry_get(name);
if (ship_entry)
{
return ade_set_args(L, "o", l_ParseObject.Set(parse_object_h(ship_entry->p_objp_or_null())));
}
else
{
auto idx = atoi(name);
if (idx > 0)
{
idx--; // Lua -> C++
if (idx < static_cast<int>(Parse_objects.size()))
{
return ade_set_args(L, "o", l_ParseObject.Set(parse_object_h(&Parse_objects[idx])));
}
}
}
return ade_set_error(L, "o", l_ParseObject.Set(parse_object_h(nullptr)));
}
ADE_FUNC(__len, l_Mission_ParsedShips, NULL,
"Number of parsed ships in the mission. This function is quick and the value returned can be relied on to be stable for the entire mission.",
"number",
"Number of parsed ships in the most recently loaded mission, or 0 if no mission has been parsed yet")
{
return ade_set_args(L, "i", static_cast<int>(Parse_objects.size()));
}
//****SUBLIBRARY: Mission/Props
ADE_LIB_DERIV(l_Mission_Props, "Props", nullptr, "Props in the mission", l_Mission);
ADE_INDEXER(l_Mission_Props, "number/string IndexOrName", "Gets prop", "prop", "Prop handle, or invalid prop handle if index was invalid")
{
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_Prop.Set(object_h()));
int idx = prop_name_lookup(name);
if (idx >= 0)
{
return ade_set_args(L, "o", l_Prop.Set(object_h(&Objects[prop_id_lookup(idx)->objnum])));
}
else
{
idx = atoi(name);
int objnum = -1;
if (idx > 0)
objnum = object_subclass_at_index(Props, idx);
return ade_set_args(L, "o", l_Prop.Set(object_h(objnum)));
}
}
ADE_FUNC(__len, l_Mission_Props, nullptr,
"Number of props in the mission. "
"This function is somewhat slow, and should be set to a variable for use in looping situations. "
"Note that props can be vanished, and so this value cannot be relied on for more than one frame.",
"number",
"Number of props in the mission, or 0 if props haven't been initialized yet")
{
return ade_set_args(L, "i", object_subclass_count(Props));
}
//****SUBLIBRARY: Mission/Waypoints
ADE_LIB_DERIV(l_Mission_Waypoints, "Waypoints", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Waypoints, "number Index", "Array of waypoints in the current mission", "waypoint", "Waypoint handle, or invalid waypoint handle if index was invalid")
{
int idx;
if(!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "o", l_Waypoint.Set(object_h()));
//Remember, Lua indices start at 0.
int count=0;
object *ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list))
{
if (ptr->type == OBJ_WAYPOINT && !ptr->flags[Object::Object_Flags::Should_be_dead])
count++;
if(count == idx) {
return ade_set_args(L, "o", l_Waypoint.Set(object_h(ptr)));
}
ptr = GET_NEXT(ptr);
}
return ade_set_error(L, "o", l_Waypoint.Set(object_h()));
}
ADE_FUNC(__len, l_Mission_Waypoints, NULL, "Gets number of waypoints in mission. Note that this is only accurate for one frame.", "number", "Number of waypoints in the mission")
{
int count=0;
object *ptr = GET_FIRST(&obj_used_list);
while (ptr != END_OF_LIST(&obj_used_list))
{
if (ptr->type == OBJ_WAYPOINT && !ptr->flags[Object::Object_Flags::Should_be_dead])
count++;
ptr = GET_NEXT(ptr);
}
return ade_set_args(L, "i", count);
}
//****SUBLIBRARY: Mission/WaypointLists
ADE_LIB_DERIV(l_Mission_WaypointLists, "WaypointLists", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_WaypointLists, "number/string IndexOrWaypointListName", "Array of waypoint lists", "waypointlist", "Gets waypointlist handle")
{
waypointlist_h wpl;
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_WaypointList.Set(waypointlist_h()));
wpl = waypointlist_h(name);
if (!wpl.isValid()) {
char* end_ptr;
auto idx = (int)strtol(name, &end_ptr, 10);
if (end_ptr != name && idx >= 1) {
// The string is a valid number and the number has a valid value
wpl = waypointlist_h(find_waypoint_list_at_index(idx - 1));
}
}
if (wpl.isValid()) {
return ade_set_args(L, "o", l_WaypointList.Set(wpl));
}
return ade_set_error(L, "o", l_WaypointList.Set(waypointlist_h()));
}
ADE_FUNC(__len, l_Mission_WaypointLists, NULL, "Number of waypoint lists in mission. Note that this is only accurate for one frame.", "number", "Number of waypoint lists in the mission")
{
return ade_set_args(L, "i", Waypoint_lists.size());
}
//****SUBLIBRARY: Mission/Weapons
ADE_LIB_DERIV(l_Mission_Weapons, "Weapons", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Weapons, "number Index", "Gets handle to a weapon object in the mission.", "weapon", "Weapon handle, or invalid weapon handle if index is invalid")
{
int idx;
if(!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "o", l_Weapon.Set(object_h()));
int objnum = -1;
if (idx > 0)
objnum = object_subclass_at_index(Weapons, MAX_WEAPONS, idx);
return ade_set_args(L, "o", l_Weapon.Set(object_h(objnum)));
}
ADE_FUNC(__len, l_Mission_Weapons, NULL, "Number of weapon objects in mission. Note that this is only accurate for one frame.", "number", "Number of weapon objects in mission")
{
return ade_set_args(L, "i", object_subclass_count(Weapons, MAX_WEAPONS));
}
//****SUBLIBRARY: Mission/Beams
ADE_LIB_DERIV(l_Mission_Beams, "Beams", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Beams, "number Index", "Gets handle to a beam object in the mission.", "beam", "Beam handle, or invalid beam handle if index is invalid")
{
int idx;
if(!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "o", l_Beam.Set(object_h()));
int objnum = -1;
if (idx > 0)
objnum = object_subclass_at_index(Beams, MAX_BEAMS, idx);
return ade_set_args(L, "o", l_Beam.Set(object_h(objnum)));
}
ADE_FUNC(__len, l_Mission_Beams, NULL, "Number of beam objects in mission. Note that this is only accurate for one frame.", "number", "Number of beam objects in mission")
{
return ade_set_args(L, "i", object_subclass_count(Beams, MAX_BEAMS));
}
//****SUBLIBRARY: Mission/Wings
ADE_LIB_DERIV(l_Mission_Wings, "Wings", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Wings, "number/string IndexOrWingName", "Wings in the mission", "wing", "Wing handle, or invalid wing handle if index or name was invalid")
{
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_Wing.Set(-1));
//MageKing17 - Make the count-ignoring version of the lookup and leave checking if the wing has any ships to the scripter
int idx = wing_lookup(name);
if(idx < 0)
{
idx = atoi(name);
if(idx < 1 || idx > Num_wings)
return ade_set_error(L, "o", l_Wing.Set(-1));
idx--; //Lua->FS2
}
return ade_set_args(L, "o", l_Wing.Set(idx));
}
ADE_FUNC(__len, l_Mission_Wings, NULL, "Number of wings in mission", "number", "Number of wings in mission")
{
return ade_set_args(L, "i", Num_wings);
}
//****SUBLIBRARY: Mission/Teams
ADE_LIB_DERIV(l_Mission_Teams, "Teams", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Teams, "number/string IndexOrTeamName", "Teams in the mission", "team", "Team handle or invalid team handle if the requested team could not be found")
{
const char* name;
if(!ade_get_args(L, "*s", &name))
return ade_set_error(L, "o", l_Team.Set(-1));
int idx = iff_lookup(name);
if(idx < 0)
{
idx = atoi(name);
idx--; //Lua->FS2
}
if(idx < 0 || idx >= (int)Iff_info.size())
return ade_set_error(L, "o", l_Team.Set(-1));
return ade_set_args(L, "o", l_Team.Set(idx));
}
ADE_FUNC(__len, l_Mission_Teams, NULL, "Number of teams in mission", "number", "Number of teams in mission")
{
return ade_set_args(L, "i", Iff_info.size());
}
//****SUBLIBRARY: Mission/Messages
ADE_LIB_DERIV(l_Mission_Messages, "Messages", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Messages, "number/string IndexOrMessageName", "Messages of the mission", "message", "Message handle or invalid handle on error")
{
int idx = -1;
if (lua_isnumber(L, 2))
{
if (!ade_get_args(L, "*i", &idx))
return ade_set_args(L, "o", l_Message.Set(-1));
idx--; // Lua --> FS2
idx += Num_builtin_messages;
}
else
{
const char* name = nullptr;
if (!ade_get_args(L, "*s", &name))
return ade_set_args(L, "o", l_Message.Set(-1));
if (name == NULL)
return ade_set_args(L, "o", l_Message.Set(-1));
for (int i = Num_builtin_messages; i < (int) Messages.size(); i++)
{
if (!stricmp(Messages[i].name, name))
{
idx = i;
break;
}
}
}
if (idx < Num_builtin_messages || idx >= (int) Messages.size())
return ade_set_args(L, "o", l_Message.Set(-1));
else
return ade_set_args(L, "o", l_Message.Set(idx));
}
ADE_FUNC(__len, l_Mission_Messages, NULL, "Number of messages in the mission", "number", "Number of messages in mission")
{
return ade_set_args(L, "i", (int) Messages.size() - Num_builtin_messages);
}
//****SUBLIBRARY: Mission/BuiltinMessages
ADE_LIB_DERIV(l_Mission_BuiltinMessages, "BuiltinMessages", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_BuiltinMessages, "number/string IndexOrMessageName", "Built-in messages of the mission", "message", "Message handle or invalid handle on error")
{
int idx = -1;
if (lua_isnumber(L, 2))
{
if (!ade_get_args(L, "*i", &idx))
return ade_set_args(L, "o", l_Message.Set(-1));
idx--; // Lua --> FS2
}
else
{
const char* name = nullptr;
if (!ade_get_args(L, "*s", &name))
return ade_set_args(L, "o", l_Message.Set(-1));
if (name == NULL)
return ade_set_args(L, "o", l_Message.Set(-1));
for (int i = 0; i < Num_builtin_messages; i++)
{
if (!stricmp(Messages[i].name, name))
{
idx = i;
break;
}
}
}
if (idx < 0 || idx >= Num_builtin_messages)
return ade_set_args(L, "o", l_Message.Set(-1));
else
return ade_set_args(L, "o", l_Message.Set(idx));
}
ADE_FUNC(__len, l_Mission_BuiltinMessages, NULL, "Number of built-in messages in the mission", "number", "Number of messages in mission")
{
return ade_set_args(L, "i", Num_builtin_messages);
}
//****SUBLIBRARY: Mission/Personas
ADE_LIB_DERIV(l_Mission_Personas, "Personas", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Personas, "number/string IndexOrName", "Personas of the mission", "persona", "Persona handle or invalid handle on error")
{
int idx = -1;
if (lua_isnumber(L, 2))
{
if (!ade_get_args(L, "*i", &idx))
return ade_set_args(L, "o", l_Persona.Set(-1));
idx--; // Lua --> FS2
}
else
{
const char* name = nullptr;
if (!ade_get_args(L, "*s", &name))
return ade_set_args(L, "o", l_Persona.Set(-1));
if (name == NULL)
return ade_set_args(L, "o", l_Persona.Set(-1));
idx = message_persona_name_lookup(name);
}
if (idx < 0 || idx >= (int)Personas.size())
return ade_set_args(L, "o", l_Persona.Set(-1));
else
return ade_set_args(L, "o", l_Persona.Set(idx));
}
ADE_FUNC(__len, l_Mission_Personas, NULL, "Number of personas in the mission", "number", "Number of messages in mission")
{
return ade_set_args(L, "i", (int)Personas.size());
}
//****SUBLIBRARY: Mission/Fireballs
ADE_LIB_DERIV(l_Mission_Fireballs, "Fireballs", NULL, NULL, l_Mission);
ADE_INDEXER(l_Mission_Fireballs, "number Index", "Gets handle to a fireball object in the mission.", "fireball", "Fireball handle, or invalid fireball handle if index is invalid")
{
int idx;
if (!ade_get_args(L, "*i", &idx))
return ade_set_error(L, "o", l_Fireball.Set(object_h()));
int objnum = -1;
if (idx > 0)
objnum = object_subclass_at_index(Fireballs, Fireballs.size(), idx);
return ade_set_args(L, "o", l_Fireball.Set(object_h(objnum)));
}
ADE_FUNC(__len, l_Mission_Fireballs, NULL, "Number of fireball objects in mission. Note that this is only accurate for one frame.", "number", "Number of fireball objects in mission")
{
return ade_set_args(L, "i", object_subclass_count(Fireballs, Fireballs.size()));
}
ADE_FUNC(addMessage, l_Mission, "string name, string text, [persona persona]", "Adds a message", "message", "The new message or invalid handle on error")
{
const char* name = nullptr;
const char* text = nullptr;
int personaIdx = -1;
if (!ade_get_args(L, "ss|o", &name, &text, l_Persona.Get(&personaIdx)))
return ade_set_error(L, "o", l_Message.Set(-1));
if (name == NULL || text == NULL)
return ade_set_error(L, "o", l_Message.Set(-1));
if (personaIdx < 0 || personaIdx >= (int)Personas.size())
personaIdx = -1;
add_message(name, text, personaIdx, 0);
return ade_set_error(L, "o", l_Message.Set((int) Messages.size() - 1));
}
int sendMessage_sub(lua_State* L, const void* sender, int messageSource, int messageIdx, float delay, enum_h* ehp)
{
if (messageIdx < 0 || messageIdx >= (int)Messages.size())
return ADE_RETURN_FALSE;
if (messageIdx < Num_builtin_messages)
{
LuaError(L, "Cannot send built-in messages!");
return ADE_RETURN_FALSE;
}
if (delay < 0.0f)
{
LuaError(L, "Invalid negative delay of %f!", delay);
return ADE_RETURN_FALSE;
}
int priority = MESSAGE_PRIORITY_NORMAL;
if (ehp != nullptr)
{
switch (ehp->index)
{
case LE_MESSAGE_PRIORITY_HIGH:
priority = MESSAGE_PRIORITY_HIGH;