/
p_acs.cpp
10780 lines (9631 loc) · 252 KB
/
p_acs.cpp
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
/*
** p_acs.cpp
** General BEHAVIOR management and ACS execution environment
**
**---------------------------------------------------------------------------
** Copyright 1998-2012 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
** This code at one time made lots of little-endian assumptions.
** I think it should be fine on big-endian machines now, but I have no
** real way to test it.
*/
#include <assert.h>
#include "templates.h"
#include "doomdef.h"
#include "p_local.h"
#include "d_player.h"
#include "p_spec.h"
#include "p_acs.h"
#include "p_saveg.h"
#include "p_lnspec.h"
#include "p_enemy.h"
#include "c_console.h"
#include "c_dispatch.h"
#include "s_sndseq.h"
#include "sbar.h"
#include "a_sharedglobal.h"
#include "w_wad.h"
#include "r_sky.h"
#include "gstrings.h"
#include "gi.h"
#include "g_game.h"
#include "c_bind.h"
#include "cmdlib.h"
#include "m_png.h"
#include "p_setup.h"
#include "po_man.h"
#include "actorptrselect.h"
#include "serializer.h"
#include "decallib.h"
#include "p_terrain.h"
#include "version.h"
#include "p_effect.h"
#include "r_utility.h"
#include "a_morph.h"
#include "i_music.h"
#include "thingdef.h"
#include "g_levellocals.h"
#include "actorinlines.h"
#include "types.h"
#include "scriptutil.h"
// P-codes for ACS scripts
enum
{
/* 0*/ PCD_NOP,
PCD_TERMINATE,
PCD_SUSPEND,
PCD_PUSHNUMBER,
PCD_LSPEC1,
PCD_LSPEC2,
PCD_LSPEC3,
PCD_LSPEC4,
PCD_LSPEC5,
PCD_LSPEC1DIRECT,
/* 10*/ PCD_LSPEC2DIRECT,
PCD_LSPEC3DIRECT,
PCD_LSPEC4DIRECT,
PCD_LSPEC5DIRECT,
PCD_ADD,
PCD_SUBTRACT,
PCD_MULTIPLY,
PCD_DIVIDE,
PCD_MODULUS,
PCD_EQ,
/* 20*/ PCD_NE,
PCD_LT,
PCD_GT,
PCD_LE,
PCD_GE,
PCD_ASSIGNSCRIPTVAR,
PCD_ASSIGNMAPVAR,
PCD_ASSIGNWORLDVAR,
PCD_PUSHSCRIPTVAR,
PCD_PUSHMAPVAR,
/* 30*/ PCD_PUSHWORLDVAR,
PCD_ADDSCRIPTVAR,
PCD_ADDMAPVAR,
PCD_ADDWORLDVAR,
PCD_SUBSCRIPTVAR,
PCD_SUBMAPVAR,
PCD_SUBWORLDVAR,
PCD_MULSCRIPTVAR,
PCD_MULMAPVAR,
PCD_MULWORLDVAR,
/* 40*/ PCD_DIVSCRIPTVAR,
PCD_DIVMAPVAR,
PCD_DIVWORLDVAR,
PCD_MODSCRIPTVAR,
PCD_MODMAPVAR,
PCD_MODWORLDVAR,
PCD_INCSCRIPTVAR,
PCD_INCMAPVAR,
PCD_INCWORLDVAR,
PCD_DECSCRIPTVAR,
/* 50*/ PCD_DECMAPVAR,
PCD_DECWORLDVAR,
PCD_GOTO,
PCD_IFGOTO,
PCD_DROP,
PCD_DELAY,
PCD_DELAYDIRECT,
PCD_RANDOM,
PCD_RANDOMDIRECT,
PCD_THINGCOUNT,
/* 60*/ PCD_THINGCOUNTDIRECT,
PCD_TAGWAIT,
PCD_TAGWAITDIRECT,
PCD_POLYWAIT,
PCD_POLYWAITDIRECT,
PCD_CHANGEFLOOR,
PCD_CHANGEFLOORDIRECT,
PCD_CHANGECEILING,
PCD_CHANGECEILINGDIRECT,
PCD_RESTART,
/* 70*/ PCD_ANDLOGICAL,
PCD_ORLOGICAL,
PCD_ANDBITWISE,
PCD_ORBITWISE,
PCD_EORBITWISE,
PCD_NEGATELOGICAL,
PCD_LSHIFT,
PCD_RSHIFT,
PCD_UNARYMINUS,
PCD_IFNOTGOTO,
/* 80*/ PCD_LINESIDE,
PCD_SCRIPTWAIT,
PCD_SCRIPTWAITDIRECT,
PCD_CLEARLINESPECIAL,
PCD_CASEGOTO,
PCD_BEGINPRINT,
PCD_ENDPRINT,
PCD_PRINTSTRING,
PCD_PRINTNUMBER,
PCD_PRINTCHARACTER,
/* 90*/ PCD_PLAYERCOUNT,
PCD_GAMETYPE,
PCD_GAMESKILL,
PCD_TIMER,
PCD_SECTORSOUND,
PCD_AMBIENTSOUND,
PCD_SOUNDSEQUENCE,
PCD_SETLINETEXTURE,
PCD_SETLINEBLOCKING,
PCD_SETLINESPECIAL,
/*100*/ PCD_THINGSOUND,
PCD_ENDPRINTBOLD, // [RH] End of Hexen p-codes
PCD_ACTIVATORSOUND,
PCD_LOCALAMBIENTSOUND,
PCD_SETLINEMONSTERBLOCKING,
PCD_PLAYERBLUESKULL, // [BC] Start of new [Skull Tag] pcodes
PCD_PLAYERREDSKULL,
PCD_PLAYERYELLOWSKULL,
PCD_PLAYERMASTERSKULL,
PCD_PLAYERBLUECARD,
/*110*/ PCD_PLAYERREDCARD,
PCD_PLAYERYELLOWCARD,
PCD_PLAYERMASTERCARD,
PCD_PLAYERBLACKSKULL,
PCD_PLAYERSILVERSKULL,
PCD_PLAYERGOLDSKULL,
PCD_PLAYERBLACKCARD,
PCD_PLAYERSILVERCARD,
PCD_ISNETWORKGAME,
PCD_PLAYERTEAM,
/*120*/ PCD_PLAYERHEALTH,
PCD_PLAYERARMORPOINTS,
PCD_PLAYERFRAGS,
PCD_PLAYEREXPERT,
PCD_BLUETEAMCOUNT,
PCD_REDTEAMCOUNT,
PCD_BLUETEAMSCORE,
PCD_REDTEAMSCORE,
PCD_ISONEFLAGCTF,
PCD_LSPEC6, // These are never used. They should probably
/*130*/ PCD_LSPEC6DIRECT, // be given names like PCD_DUMMY.
PCD_PRINTNAME,
PCD_MUSICCHANGE,
PCD_CONSOLECOMMANDDIRECT,
PCD_CONSOLECOMMAND,
PCD_SINGLEPLAYER, // [RH] End of Skull Tag p-codes
PCD_FIXEDMUL,
PCD_FIXEDDIV,
PCD_SETGRAVITY,
PCD_SETGRAVITYDIRECT,
/*140*/ PCD_SETAIRCONTROL,
PCD_SETAIRCONTROLDIRECT,
PCD_CLEARINVENTORY,
PCD_GIVEINVENTORY,
PCD_GIVEINVENTORYDIRECT,
PCD_TAKEINVENTORY,
PCD_TAKEINVENTORYDIRECT,
PCD_CHECKINVENTORY,
PCD_CHECKINVENTORYDIRECT,
PCD_SPAWN,
/*150*/ PCD_SPAWNDIRECT,
PCD_SPAWNSPOT,
PCD_SPAWNSPOTDIRECT,
PCD_SETMUSIC,
PCD_SETMUSICDIRECT,
PCD_LOCALSETMUSIC,
PCD_LOCALSETMUSICDIRECT,
PCD_PRINTFIXED,
PCD_PRINTLOCALIZED,
PCD_MOREHUDMESSAGE,
/*160*/ PCD_OPTHUDMESSAGE,
PCD_ENDHUDMESSAGE,
PCD_ENDHUDMESSAGEBOLD,
PCD_SETSTYLE,
PCD_SETSTYLEDIRECT,
PCD_SETFONT,
PCD_SETFONTDIRECT,
PCD_PUSHBYTE,
PCD_LSPEC1DIRECTB,
PCD_LSPEC2DIRECTB,
/*170*/ PCD_LSPEC3DIRECTB,
PCD_LSPEC4DIRECTB,
PCD_LSPEC5DIRECTB,
PCD_DELAYDIRECTB,
PCD_RANDOMDIRECTB,
PCD_PUSHBYTES,
PCD_PUSH2BYTES,
PCD_PUSH3BYTES,
PCD_PUSH4BYTES,
PCD_PUSH5BYTES,
/*180*/ PCD_SETTHINGSPECIAL,
PCD_ASSIGNGLOBALVAR,
PCD_PUSHGLOBALVAR,
PCD_ADDGLOBALVAR,
PCD_SUBGLOBALVAR,
PCD_MULGLOBALVAR,
PCD_DIVGLOBALVAR,
PCD_MODGLOBALVAR,
PCD_INCGLOBALVAR,
PCD_DECGLOBALVAR,
/*190*/ PCD_FADETO,
PCD_FADERANGE,
PCD_CANCELFADE,
PCD_PLAYMOVIE,
PCD_SETFLOORTRIGGER,
PCD_SETCEILINGTRIGGER,
PCD_GETACTORX,
PCD_GETACTORY,
PCD_GETACTORZ,
PCD_STARTTRANSLATION,
/*200*/ PCD_TRANSLATIONRANGE1,
PCD_TRANSLATIONRANGE2,
PCD_ENDTRANSLATION,
PCD_CALL,
PCD_CALLDISCARD,
PCD_RETURNVOID,
PCD_RETURNVAL,
PCD_PUSHMAPARRAY,
PCD_ASSIGNMAPARRAY,
PCD_ADDMAPARRAY,
/*210*/ PCD_SUBMAPARRAY,
PCD_MULMAPARRAY,
PCD_DIVMAPARRAY,
PCD_MODMAPARRAY,
PCD_INCMAPARRAY,
PCD_DECMAPARRAY,
PCD_DUP,
PCD_SWAP,
PCD_WRITETOINI,
PCD_GETFROMINI,
/*220*/ PCD_SIN,
PCD_COS,
PCD_VECTORANGLE,
PCD_CHECKWEAPON,
PCD_SETWEAPON,
PCD_TAGSTRING,
PCD_PUSHWORLDARRAY,
PCD_ASSIGNWORLDARRAY,
PCD_ADDWORLDARRAY,
PCD_SUBWORLDARRAY,
/*230*/ PCD_MULWORLDARRAY,
PCD_DIVWORLDARRAY,
PCD_MODWORLDARRAY,
PCD_INCWORLDARRAY,
PCD_DECWORLDARRAY,
PCD_PUSHGLOBALARRAY,
PCD_ASSIGNGLOBALARRAY,
PCD_ADDGLOBALARRAY,
PCD_SUBGLOBALARRAY,
PCD_MULGLOBALARRAY,
/*240*/ PCD_DIVGLOBALARRAY,
PCD_MODGLOBALARRAY,
PCD_INCGLOBALARRAY,
PCD_DECGLOBALARRAY,
PCD_SETMARINEWEAPON,
PCD_SETACTORPROPERTY,
PCD_GETACTORPROPERTY,
PCD_PLAYERNUMBER,
PCD_ACTIVATORTID,
PCD_SETMARINESPRITE,
/*250*/ PCD_GETSCREENWIDTH,
PCD_GETSCREENHEIGHT,
PCD_THING_PROJECTILE2,
PCD_STRLEN,
PCD_SETHUDSIZE,
PCD_GETCVAR,
PCD_CASEGOTOSORTED,
PCD_SETRESULTVALUE,
PCD_GETLINEROWOFFSET,
PCD_GETACTORFLOORZ,
/*260*/ PCD_GETACTORANGLE,
PCD_GETSECTORFLOORZ,
PCD_GETSECTORCEILINGZ,
PCD_LSPEC5RESULT,
PCD_GETSIGILPIECES,
PCD_GETLEVELINFO,
PCD_CHANGESKY,
PCD_PLAYERINGAME,
PCD_PLAYERISBOT,
PCD_SETCAMERATOTEXTURE,
/*270*/ PCD_ENDLOG,
PCD_GETAMMOCAPACITY,
PCD_SETAMMOCAPACITY,
PCD_PRINTMAPCHARARRAY, // [JB] start of new p-codes
PCD_PRINTWORLDCHARARRAY,
PCD_PRINTGLOBALCHARARRAY, // [JB] end of new p-codes
PCD_SETACTORANGLE, // [GRB]
PCD_GRABINPUT, // Unused but acc defines them
PCD_SETMOUSEPOINTER, // "
PCD_MOVEMOUSEPOINTER, // "
/*280*/ PCD_SPAWNPROJECTILE,
PCD_GETSECTORLIGHTLEVEL,
PCD_GETACTORCEILINGZ,
PCD_SETACTORPOSITION,
PCD_CLEARACTORINVENTORY,
PCD_GIVEACTORINVENTORY,
PCD_TAKEACTORINVENTORY,
PCD_CHECKACTORINVENTORY,
PCD_THINGCOUNTNAME,
PCD_SPAWNSPOTFACING,
/*290*/ PCD_PLAYERCLASS, // [GRB]
//[MW] start my p-codes
PCD_ANDSCRIPTVAR,
PCD_ANDMAPVAR,
PCD_ANDWORLDVAR,
PCD_ANDGLOBALVAR,
PCD_ANDMAPARRAY,
PCD_ANDWORLDARRAY,
PCD_ANDGLOBALARRAY,
PCD_EORSCRIPTVAR,
PCD_EORMAPVAR,
/*300*/ PCD_EORWORLDVAR,
PCD_EORGLOBALVAR,
PCD_EORMAPARRAY,
PCD_EORWORLDARRAY,
PCD_EORGLOBALARRAY,
PCD_ORSCRIPTVAR,
PCD_ORMAPVAR,
PCD_ORWORLDVAR,
PCD_ORGLOBALVAR,
PCD_ORMAPARRAY,
/*310*/ PCD_ORWORLDARRAY,
PCD_ORGLOBALARRAY,
PCD_LSSCRIPTVAR,
PCD_LSMAPVAR,
PCD_LSWORLDVAR,
PCD_LSGLOBALVAR,
PCD_LSMAPARRAY,
PCD_LSWORLDARRAY,
PCD_LSGLOBALARRAY,
PCD_RSSCRIPTVAR,
/*320*/ PCD_RSMAPVAR,
PCD_RSWORLDVAR,
PCD_RSGLOBALVAR,
PCD_RSMAPARRAY,
PCD_RSWORLDARRAY,
PCD_RSGLOBALARRAY,
//[MW] end my p-codes
PCD_GETPLAYERINFO, // [GRB]
PCD_CHANGELEVEL,
PCD_SECTORDAMAGE,
PCD_REPLACETEXTURES,
/*330*/ PCD_NEGATEBINARY,
PCD_GETACTORPITCH,
PCD_SETACTORPITCH,
PCD_PRINTBIND,
PCD_SETACTORSTATE,
PCD_THINGDAMAGE2,
PCD_USEINVENTORY,
PCD_USEACTORINVENTORY,
PCD_CHECKACTORCEILINGTEXTURE,
PCD_CHECKACTORFLOORTEXTURE,
/*340*/ PCD_GETACTORLIGHTLEVEL,
PCD_SETMUGSHOTSTATE,
PCD_THINGCOUNTSECTOR,
PCD_THINGCOUNTNAMESECTOR,
PCD_CHECKPLAYERCAMERA, // [TN]
PCD_MORPHACTOR, // [MH]
PCD_UNMORPHACTOR, // [MH]
PCD_GETPLAYERINPUT,
PCD_CLASSIFYACTOR,
PCD_PRINTBINARY,
/*350*/ PCD_PRINTHEX,
PCD_CALLFUNC,
PCD_SAVESTRING, // [FDARI] create string (temporary)
PCD_PRINTMAPCHRANGE, // [FDARI] output range (print part of array)
PCD_PRINTWORLDCHRANGE,
PCD_PRINTGLOBALCHRANGE,
PCD_STRCPYTOMAPCHRANGE, // [FDARI] input range (copy string to all/part of array)
PCD_STRCPYTOWORLDCHRANGE,
PCD_STRCPYTOGLOBALCHRANGE,
PCD_PUSHFUNCTION, // from Eternity
/*360*/ PCD_CALLSTACK, // from Eternity
PCD_SCRIPTWAITNAMED,
PCD_TRANSLATIONRANGE3,
PCD_GOTOSTACK,
PCD_ASSIGNSCRIPTARRAY,
PCD_PUSHSCRIPTARRAY,
PCD_ADDSCRIPTARRAY,
PCD_SUBSCRIPTARRAY,
PCD_MULSCRIPTARRAY,
PCD_DIVSCRIPTARRAY,
/*370*/ PCD_MODSCRIPTARRAY,
PCD_INCSCRIPTARRAY,
PCD_DECSCRIPTARRAY,
PCD_ANDSCRIPTARRAY,
PCD_EORSCRIPTARRAY,
PCD_ORSCRIPTARRAY,
PCD_LSSCRIPTARRAY,
PCD_RSSCRIPTARRAY,
PCD_PRINTSCRIPTCHARARRAY,
PCD_PRINTSCRIPTCHRANGE,
/*380*/ PCD_STRCPYTOSCRIPTCHRANGE,
PCD_LSPEC5EX,
PCD_LSPEC5EXRESULT,
PCD_TRANSLATIONRANGE4,
PCD_TRANSLATIONRANGE5,
/*381*/ PCODE_COMMAND_COUNT
};
// Some constants used by ACS scripts
enum {
LINE_FRONT = 0,
LINE_BACK = 1
};
enum {
SIDE_FRONT = 0,
SIDE_BACK = 1
};
enum {
TEXTURE_TOP = 0,
TEXTURE_MIDDLE = 1,
TEXTURE_BOTTOM = 2
};
enum {
GAME_SINGLE_PLAYER = 0,
GAME_NET_COOPERATIVE = 1,
GAME_NET_DEATHMATCH = 2,
GAME_TITLE_MAP = 3
};
enum {
CLASS_FIGHTER = 0,
CLASS_CLERIC = 1,
CLASS_MAGE = 2
};
enum {
SKILL_VERY_EASY = 0,
SKILL_EASY = 1,
SKILL_NORMAL = 2,
SKILL_HARD = 3,
SKILL_VERY_HARD = 4
};
enum {
BLOCK_NOTHING = 0,
BLOCK_CREATURES = 1,
BLOCK_EVERYTHING = 2,
BLOCK_RAILING = 3,
BLOCK_PLAYERS = 4
};
enum {
LEVELINFO_PAR_TIME,
LEVELINFO_CLUSTERNUM,
LEVELINFO_LEVELNUM,
LEVELINFO_TOTAL_SECRETS,
LEVELINFO_FOUND_SECRETS,
LEVELINFO_TOTAL_ITEMS,
LEVELINFO_FOUND_ITEMS,
LEVELINFO_TOTAL_MONSTERS,
LEVELINFO_KILLED_MONSTERS,
LEVELINFO_SUCK_TIME
};
enum {
PLAYERINFO_TEAM,
PLAYERINFO_AIMDIST,
PLAYERINFO_COLOR,
PLAYERINFO_GENDER,
PLAYERINFO_NEVERSWITCH,
PLAYERINFO_MOVEBOB,
PLAYERINFO_STILLBOB,
PLAYERINFO_PLAYERCLASS,
PLAYERINFO_FOV,
PLAYERINFO_DESIREDFOV,
};
extern FILE *Logfile;
FRandom pr_acs ("ACS");
// I imagine this much stack space is probably overkill, but it could
// potentially get used with recursive functions.
#define STACK_SIZE 4096
#define CLAMPCOLOR(c) (EColorRange)((unsigned)(c) >= NUM_TEXT_COLORS ? CR_UNTRANSLATED : (c))
#define LANGREGIONMASK MAKE_ID(0,0,0xff,0xff)
// HUD message flags
#define HUDMSG_LOG (0x80000000)
#define HUDMSG_COLORSTRING (0x40000000)
#define HUDMSG_ADDBLEND (0x20000000)
#define HUDMSG_ALPHA (0x10000000)
#define HUDMSG_NOWRAP (0x08000000)
// HUD message layers; these are not flags
#define HUDMSG_LAYER_SHIFT 12
#define HUDMSG_LAYER_MASK (0x0000F000)
// See HUDMSGLayer enumerations in sbar.h
// HUD message visibility flags
#define HUDMSG_VISIBILITY_SHIFT 16
#define HUDMSG_VISIBILITY_MASK (0x00070000)
// See HUDMSG visibility enumerations in sbar.h
// LineAttack flags
#define FHF_NORANDOMPUFFZ 1
#define FHF_NOIMPACTDECAL 2
// SpawnDecal flags
#define SDF_ABSANGLE 1
#define SDF_PERMANENT 2
// GetArmorInfo
enum
{
ARMORINFO_CLASSNAME,
ARMORINFO_SAVEAMOUNT,
ARMORINFO_SAVEPERCENT,
ARMORINFO_MAXABSORB,
ARMORINFO_MAXFULLABSORB,
ARMORINFO_ACTUALSAVEAMOUNT,
};
// PickActor
// [JP] I've renamed these flags to something else to avoid confusion with the other PAF_ flags
enum
{
// PAF_FORCETID,
// PAF_RETURNTID
PICKAF_FORCETID = 1,
PICKAF_RETURNTID = 2,
};
// ACS specific conversion functions to and from fixed point.
// These should be used to convert from and to sctipt variables
// so that there is a clear distinction between leftover fixed point code
// and genuinely needed conversions.
inline double ACSToDouble(int acsval)
{
return acsval / 65536.;
}
inline float ACSToFloat(int acsval)
{
return acsval / 65536.f;
}
inline int DoubleToACS(double val)
{
return xs_Fix<16>::ToFix(val);
}
inline DAngle ACSToAngle(int acsval)
{
return acsval * (360. / 65536.);
}
inline int AngleToACS(DAngle ang)
{
return ang.BAMs() >> 16;
}
inline int PitchToACS(DAngle ang)
{
return int(ang.Normalized180().Degrees * (65536. / 360));
}
struct CallReturn
{
CallReturn(int pc, ScriptFunction *func, FBehavior *module, const ACSLocalVariables &locals, ACSLocalArrays *arrays, bool discard, unsigned int runaway)
: ReturnFunction(func),
ReturnModule(module),
ReturnLocals(locals),
ReturnArrays(arrays),
ReturnAddress(pc),
bDiscardResult(discard),
EntryInstrCount(runaway)
{}
ScriptFunction *ReturnFunction;
FBehavior *ReturnModule;
ACSLocalVariables ReturnLocals;
ACSLocalArrays *ReturnArrays;
int ReturnAddress;
int bDiscardResult;
unsigned int EntryInstrCount;
};
class DLevelScript : public DObject
{
DECLARE_CLASS(DLevelScript, DObject)
HAS_OBJECT_POINTERS
public:
enum EScriptState
{
SCRIPT_Running,
SCRIPT_Suspended,
SCRIPT_Delayed,
SCRIPT_TagWait,
SCRIPT_PolyWait,
SCRIPT_ScriptWaitPre,
SCRIPT_ScriptWait,
SCRIPT_PleaseRemove,
SCRIPT_DivideBy0,
SCRIPT_ModulusBy0,
};
DLevelScript(FLevelLocals *Level, AActor *who, line_t *where, int num, const ScriptPtr *code, FBehavior *module,
const int *args, int argcount, int flags);
~DLevelScript();
void Serialize(FSerializer &arc);
int RunScript();
PClass *GetClassForIndex(int index) const;
inline void SetState(EScriptState newstate) { state = newstate; }
inline EScriptState GetState() { return state; }
DLevelScript *GetNext() const { return next; }
void MarkLocalVarStrings() const
{
GlobalACSStrings.MarkStringArray(&Localvars[0], Localvars.Size());
}
void LockLocalVarStrings(int levelnum) const
{
GlobalACSStrings.LockStringArray(levelnum, &Localvars[0], Localvars.Size());
}
protected:
FLevelLocals *Level;
DLevelScript *next, *prev;
int script;
TArray<int32_t> Localvars;
int *pc;
EScriptState state;
int statedata;
TObjPtr<AActor*> activator;
line_t *activationline;
bool backSide;
FFont *activefont;
int hudwidth, hudheight;
int ClipRectLeft, ClipRectTop, ClipRectWidth, ClipRectHeight;
int WrapWidth;
bool HandleAspect;
FBehavior *activeBehavior;
int InModuleScriptNumber;
TArray<FString> ACS_StringBuilderStack;
inline void STRINGBUILDER_START(FString &Builder)
{
if (Builder.IsNotEmpty() || ACS_StringBuilderStack.Size())
{
ACS_StringBuilderStack.Push(Builder);
Builder = "";
}
}
inline void STRINGBUILDER_FINISH(FString &Builder)
{
if (!ACS_StringBuilderStack.Pop(Builder))
{
Builder = "";
}
}
void Link();
void Unlink();
void PutLast();
void PutFirst();
int Random(int min, int max);
int ThingCount(int type, int stringid, int tid, int tag);
void ChangeFlat(int tag, int name, bool floorOrCeiling);
int CountPlayers();
void SetLineTexture(int lineid, int side, int position, int name);
int DoSpawn(int type, const DVector3 &pos, int tid, DAngle angle, bool force);
int DoSpawn(int type, int x, int y, int z, int tid, int angle, bool force);
bool DoCheckActorTexture(int tid, AActor *activator, int string, bool floor);
int DoSpawnSpot(int type, int spot, int tid, int angle, bool forced);
int DoSpawnSpotFacing(int type, int spot, int tid, bool forced);
int DoClassifyActor(int tid);
int CallFunction(int argCount, int funcIndex, int32_t *args);
void DoFadeTo(int r, int g, int b, int a, int time);
void DoFadeRange(int r1, int g1, int b1, int a1,
int r2, int g2, int b2, int a2, int time);
void DoSetFont(int fontnum);
int DoSetMaster(AActor *self, AActor *master);
void SetActorProperty(int tid, int property, int value);
void DoSetActorProperty(AActor *actor, int property, int value);
int GetActorProperty(int tid, int property);
int CheckActorProperty(int tid, int property, int value);
int GetPlayerInput(int playernum, int inputnum);
AActor *SingleActorFromTID(int tid, AActor *defactor);
void SetUserVariable(AActor *self, FName varname, int index, int value);
int ScriptCall(AActor *activator, unsigned argc, int32_t *args);
void SetActorTeleFog(AActor *activator, int tid, const FString &telefogsrc, const FString &telefogdest);
int SwapActorTeleFog(AActor *activator, int tid);
void SetActorRoll(AActor *activator, int tid, int angle, bool interpolate);
void SetActorPitch(AActor *activator, int tid, int angle, bool interpolate);
void SetActorAngle(AActor *activator, int tid, int angle, bool interpolate);
void DoSetCVar(FBaseCVar *cvar, int value, bool is_string, bool force = false);
int SetUserCVar(int playernum, const char *cvarname, int value, bool is_string);
int SetCVar(AActor *activator, const char *cvarname, int value, bool is_string);
int LineFromID(int id);
int SideFromID(int id, int side);
private:
DLevelScript();
friend class DACSThinker;
};
static DLevelScript *P_GetScriptGoing (FLevelLocals *Level, AActor *who, line_t *where, int num, const ScriptPtr *code, FBehavior *module,
const int *args, int argcount, int flags);
struct FBehavior::ArrayInfo
{
uint32_t ArraySize;
int32_t *Elements;
};
//============================================================================
//
// uallong
//
// Read a possibly unaligned four-byte little endian integer from memory.
//
//============================================================================
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__x86_64__)
inline int uallong(const int &foo)
{
return foo;
}
#else
inline int uallong(const int &foo)
{
const unsigned char *bar = (const unsigned char *)&foo;
return bar[0] | (bar[1] << 8) | (bar[2] << 16) | (bar[3] << 24);
}
#endif
//============================================================================
//
// Global and world variables
//
//============================================================================
// ACS variables with world scope
static BoundsCheckingArray<int32_t, NUM_WORLDVARS> ACS_WorldVars;
static BoundsCheckingArray<FWorldGlobalArray, NUM_WORLDVARS> ACS_WorldArrays;
// ACS variables with global scope
BoundsCheckingArray<int32_t, NUM_GLOBALVARS> ACS_GlobalVars;
BoundsCheckingArray<FWorldGlobalArray, NUM_GLOBALVARS> ACS_GlobalArrays;
//----------------------------------------------------------------------------
//
// ACS stack manager
//
// This is needed so that the garbage collector has access to all active
// script stacks
//
//----------------------------------------------------------------------------
using FACSStackMemory = BoundsCheckingArray<int32_t, STACK_SIZE>;
struct FACSStack
{
FACSStackMemory buffer;
int sp;
FACSStack *next;
FACSStack *prev;
static FACSStack *head;
FACSStack();
~FACSStack();
};
FACSStack *FACSStack::head;
FACSStack::FACSStack()
{
sp = 0;
next = head;
prev = NULL;
head = this;
}
FACSStack::~FACSStack()
{
if (next != NULL) next->prev = prev;
if (prev == NULL)
{
head = next;
}
else
{
prev->next = next;
}
}
//----------------------------------------------------------------------------
//
// Global ACS strings (Formerly known as On the fly strings)
//
// This special string table is part of the global state. Programmatically
// generated strings (e.g. those returned by strparam) are stored here.
// PCD_TAGSTRING also now stores strings in this table instead of simply
// tagging strings with their library ID.
//
// Identical strings map to identical string identifiers.
//
// When the string table needs to grow to hold more strings, a garbage
// collection is first attempted to see if more room can be made to store
// strings without growing. A string is considered in use if any value
// in any of these variable blocks contains a valid ID in the global string
// table:
// * The active area of the ACS stack
// * All running scripts' local variables
// * All map variables
// * All world variables
// * All global variables
// It's not important whether or not they are really used as strings, only
// that they might be. A string is also considered in use if its lock count
// is non-zero, even if none of the above variable blocks referenced it.
//
// To keep track of local and map variables for nonresident maps in a hub,
// when a map's state is archived, all strings found in its local and map
// variables are locked. When a map is revisited in a hub, all strings found
// in its local and map variables are unlocked. Locking and unlocking are
// cumulative operations.
//
// What this all means is that:
// * Strings returned by strparam last indefinitely. No longer do they
// disappear at the end of the tic they were generated.
// * You can pass library strings around freely without having to worry
// about always having the same libraries loaded in the same order on
// every map that needs to use those strings.
//
//----------------------------------------------------------------------------
ACSStringPool GlobalACSStrings;
void ACSStringPool::PoolEntry::Lock(int levelnum)
{
if (Locks.Find(levelnum) == Locks.Size())
{
Locks.Push(levelnum);
}
}
void ACSStringPool::PoolEntry::Unlock(int levelnum)
{
auto ndx = Locks.Find(levelnum);
if (ndx < Locks.Size())
{
Locks.Delete(ndx);
}
}
ACSStringPool::ACSStringPool()
{
memset(PoolBuckets, 0xFF, sizeof(PoolBuckets));
FirstFreeEntry = 0;
}
//============================================================================
//
// ACSStringPool :: Clear
//
// Remove all strings from the pool.
//
//============================================================================
void ACSStringPool::Clear()
{
Pool.Clear();
memset(PoolBuckets, 0xFF, sizeof(PoolBuckets));
FirstFreeEntry = 0;
}
//============================================================================
//
// ACSStringPool :: AddString
//
// Returns a valid string identifier (including library ID) or -1 if we ran
// out of room. Identical strings will return identical values.
//
//============================================================================
int ACSStringPool::AddString(const char *str)
{
if (str == nullptr) str = "";
size_t len = strlen(str);
unsigned int h = SuperFastHash(str, len);
unsigned int bucketnum = h % NUM_BUCKETS;
int i = FindString(str, len, h, bucketnum);
if (i >= 0)
{
return i | STRPOOL_LIBRARYID_OR;
}
FString fstr(str);
return InsertString(fstr, h, bucketnum);
}
int ACSStringPool::AddString(FString &str)
{
unsigned int h = SuperFastHash(str.GetChars(), str.Len());
unsigned int bucketnum = h % NUM_BUCKETS;
int i = FindString(str, str.Len(), h, bucketnum);
if (i >= 0)
{
return i | STRPOOL_LIBRARYID_OR;
}
return InsertString(str, h, bucketnum);
}
//============================================================================
//
// ACSStringPool :: GetString
//
//============================================================================
const char *ACSStringPool::GetString(int strnum)
{
assert((strnum & LIBRARYID_MASK) == STRPOOL_LIBRARYID_OR);
strnum &= ~LIBRARYID_MASK;
if ((unsigned)strnum < Pool.Size() && Pool[strnum].Next != FREE_ENTRY)