forked from rofl0r/openbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenbor.c
20166 lines (18013 loc) · 615 KB
/
openbor.c
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
/*
* OpenBOR - http://www.LavaLit.com
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in OpenBOR root for details.
*
* Copyright (c) 2004 - 2011 OpenBOR Team
*/
/////////////////////////////////////////////////////////////////////////////
// Beats of Rage //
// Side-scrolling beat-'em-up //
/////////////////////////////////////////////////////////////////////////////
#include "debug.h"
#include "data.h"
#include "openbor.h"
#include "commands.h"
#include "models.h"
#include "movie.h"
#include "menus.h"
#include "source/strswitch/stringswitch.h"
#define GET_ARG(z) arglist.count > z ? arglist.args[z] : ""
#define GET_ARG_LEN(z) arglist.count > z ? arglist.arglen[z] : 0
#define GET_ARGP(z) arglist->count > z ? arglist->args[z] : ""
#define GET_ARGP_LEN(z) arglist->count > z ? arglist->arglen[z] : 0
#define GET_INT_ARG(z) getValidInt(GET_ARG(z), filename, command)
#define GET_FLOAT_ARG(z) getValidFloat(GET_ARG(z), filename, command)
#define GET_INT_ARGP(z) getValidInt(GET_ARGP(z), filename, command)
#define GET_FLOAT_ARGP(z) getValidFloat(GET_ARGP(z), filename, command)
static const char *E_OUT_OF_MEMORY = "Error: Could not allocate sufficient memory.\n";
static int DEFAULT_OFFSCREEN_KILL = 3000;
/////////////////////////////////////////////////////////////////////////////
// Global Variables //
/////////////////////////////////////////////////////////////////////////////
s_level_entry *levelorder[MAX_DIFFICULTIES][MAX_LEVELS];
s_level *level = NULL;
s_screen *vscreen = NULL;
s_screen *background = NULL;
s_screen *bgbuffer = NULL;
char bgbuffer_updated = 0;
s_bitmap *texture = NULL;
s_videomodes videomodes;
s_player_min_max_z_bgheight player_min_max_z_bgheight = {
160, 232, 160
};
char custom_button_names[CB_MAX][16];
char disabledkey[CB_MAX] = { 0 };
int quit_game = 0;
int sprite_map_max_items = 0;
int cache_map_max_items = 0;
int startup_done = 0; // startup is only called when a game is loaded. so when exitting from the menu we need a way to figure out which resources to free.
List *modelcmdlist = NULL;
List* modelsattackcmdlist = NULL;
List *modelstxtcmdlist = NULL;
List *levelcmdlist = NULL;
List *levelordercmdlist = NULL;
List *scriptConstantsCommandList = NULL;
char *custBkgrds = NULL;
char *custLevels = NULL;
char *custModels = NULL;
char rush_names[2][MAX_NAME_LEN];
char branch_name[MAX_NAME_LEN + 1]; // Used for branches
char set_names[MAX_DIFFICULTIES][MAX_NAME_LEN + 1];
unsigned char pal[MAX_PAL_SIZE] = { "" };
int blendfx[MAX_BLENDINGS] = { 0, 1, 0, 0, 0, 0 };
char blendfx_is_set = 0;
int fontmonospace[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
// move all blending effects here
unsigned char *blendings[MAX_BLENDINGS] = { NULL, NULL, NULL, NULL, NULL, NULL };
// function pointers to create the tables
palette_table_function blending_table_functions[MAX_BLENDINGS] =
{ palette_table_screen, palette_table_multiply, palette_table_overlay,
palette_table_hardlight, palette_table_dodge, palette_table_half
};
int current_set = 0;
int current_level = 0;
int current_stage = 1;
float bgtravelled;
int traveltime;
int texttime;
float advancex;
float advancey;
float scrolldx; // advancex changed previous loop
float scrolldy; // advancey .....................
float scrollminz; // Limit level z-scroll
float scrollmaxz;
float blockade; // Limit x scroll back
float lasthitx; //Last hit X location.
float lasthitz; //Last hit Z location.
float lasthita; //Last hit A location.
int lasthitt; //Last hit type.
int lasthitc; //Last hit confirm (i.e. if engine hit code will be used).
// used by gfx shadow
int light[2] = { 0, 0 };
int shadowalpha = BLEND_MULTIPLY + 1;
u32 interval = 0;
extern unsigned long seed;
s_samples samples = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,};
//------------------------------
int grab_attacks[5][2] = {
{ANI_GRABATTACK, ANI_GRABATTACK2},
{ANI_GRABFORWARD, ANI_GRABFORWARD2},
{ANI_GRABUP, ANI_GRABUP2},
{ANI_GRABDOWN, ANI_GRABDOWN2},
{ANI_GRABBACKWARD, ANI_GRABBACKWARD2}
};
// background cache to speed up in-game menus
#if WII
s_screen *bg_cache[MAX_CACHED_BACKGROUNDS] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
unsigned char bg_palette_cache[MAX_CACHED_BACKGROUNDS][MAX_PAL_SIZE];
#endif
int maxplayers[MAX_DIFFICULTIES] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
int ctrlmaxplayers[MAX_DIFFICULTIES] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned int num_levels[MAX_DIFFICULTIES];
unsigned int ifcomplete[MAX_DIFFICULTIES];
unsigned int num_difficulties;
unsigned int noshowhof[MAX_DIFFICULTIES];
unsigned int difflives[MAX_DIFFICULTIES]; // What too easy? change the # of lives players get
unsigned int custfade[MAX_DIFFICULTIES];
unsigned int diffcreds[MAX_DIFFICULTIES]; // What still to easy - lets see how they do without continues!
unsigned int diffoverlap[MAX_DIFFICULTIES]; // Music overlap
unsigned int typemp[MAX_DIFFICULTIES];
unsigned int continuescore[MAX_DIFFICULTIES]; //what to do with score if continue is used.
char *(*skipselect)[MAX_DIFFICULTIES][MAX_PLAYERS] = NULL; // skips select screen and automatically gives players models specified
int cansave_flag[MAX_DIFFICULTIES]; // 0, no save, 1 save level position 2 save all: lives/credits/hp/mp/also player
int cameratype = 0;
u32 go_time = 0;
u32 borTime = 0;
u32 newtime = 0;
unsigned char slowmotion[3] = { 0, 2, 0 }; // [0] = enable/disable; [1] = duration; [2] = counter;
int disablelog = 0;
#define PLOG(fmt, args...) do { if(!disablelog) fprintf(stdout, fmt, ## args); } while (0)
int currentspawnplayer = 0;
int MAX_WALL_HEIGHT = 1000; // Max wall height that an entity can be spawned on
int saveslot = 0;
int current_palette = 0;
int fade = 24;
int credits = 0;
int gosound = 0; // Used to prevent go sound playing too frequently,
int musicoverlap = 0;
int colorbars = 0;
int current_spawn = 0;
int level_completed = 0;
int nojoin = 0; // dont allow new hero to join in, use "Please Wait" instead of "Select Hero"
int groupmin = 0;
int groupmax = 0;
int selectScreen = 0; // Flag to determine if at select screen (used for setting animations)
int tospeedup = 0; // If set will speed the level back up after a boss hits the ground
int reached[4] = { 0, 0, 0, 0 }; // Used with TYPE_ENDLEVEL to determine which players have reached the point //4player
int noslowfx = 0; // Flag to determine if sound speed when hitting opponent slows or not
int equalairpause = 0; // If set to 1, there will be no extra pausetime for players who hit multiple enemies in midair
int hiscorebg = 0; // If set to 1, will look for a background image to display at the highscore screen
int completebg = 0; // If set to 1, will look for a background image to display at the showcomplete screen
s_loadingbar loadingbg[2] = { {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0} }; // If set to 1, will look for a background image to display at the loading screen
int loadingmusic = 0;
int unlockbg = 0; // If set to 1, will look for a different background image after defeating the game
int pause = 0;
int nopause = 0; // OX. If set to 1 , pausing the game will be disabled.
int noscreenshot = 0; // OX. If set to 1 , taking screenshots is disabled.
int endgame = 0;
int forcecheatsoff = 0;
int cheats = 0;
int livescheat = 0;
int creditscheat = 0;
int healthcheat = 0;
int keyscriptrate = 0;
int showtimeover = 0;
int sameplayer = 0; // 7-1-2005 flag to determine if players can use the same character
int PLAYER_LIVES = 3; // 7-1-2005 default setting for Lives
int CONTINUES = 5; // 7-1-2005 default setting for continues
int colourselect = 0; // 6-2-2005 Colour select is optional
int autoland = 0; // Default set to no autoland and landing is valid with u j combo
int ajspecial = 0; // Flag to determine if holding down attack and pressing jump executes special
int nolost = 0; // variable to control if drop weapon when grab a enemy by tails
int nocost = 0; // If set, special will not cost life unless an enemy is hit
int mpstrict = 0; // If current system will check all animation's energy cost when set new animations
int magic_type = 0; // use for restore mp by time by tails
entity *textbox = NULL;
entity *smartbomber = NULL;
int plife[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable player lifebar
int plifeX[4][3] = { {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1} }; // Used for customizable player lifebar 'x'
int plifeN[4][3] = { {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1} }; // Used for customizable player lifebar number of lives
int picon[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable player icon
int piconw[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable player weapon icons
int mpicon[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable magicbar player icon
int pnameJ[4][7] = { {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1} }; // Used for customizable player name, Select Hero, (Credits, Press Start, Game Over) when joining
int pscore[4][7] = { {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1}, {0, 0, 0, 0, 0, 0, -1} }; // Used for customizable player name, dash, score
int pshoot[4][3] = { {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1} }; // Used for customizable player shootnum
int prush[4][8] = { {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0} }; // Used for customizable player combo/rush system
int psmenu[4][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }; // Used for customizable player placement in select menu
char musicname[128] = { "" };
float musicfade[2] = { 0, 0 };
int musicloop = 0;
u32 musicoffset = 0;
int timeloc[6] = { 0, 0, 0, 0, 0, -1 }; // Used for customizable timeclock location/size
int timeicon = -1;
short timeicon_offsets[2] = { 0, 0 };
char timeicon_path[128] = { "" };
int bgicon = -1;
short bgicon_offsets[3] = { 0, 0, 0 };
char bgicon_path[128] = { "" };
int olicon = -1;
short olicon_offsets[3] = { 0, 0, 0 };
char olicon_path[128] = { "" };
int elife[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable enemy lifebar
int ename[4][3] = { {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1} }; // Used for customizable enemy name
int eicon[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable enemy icon
int scomplete[6] = { 0, 0, 0, 0, 0, 0 }; // Used for customizable Stage # Complete
int cbonus[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Used for customizable clear bonus
int lbonus[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Used for customizable life bonus
int rbonus[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Used for customizable rush bonus
int tscore[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Used for customizable total score
int scbonuses[4] = { 10000, 1000, 100, 0 }; //Stage complete bonus multipliers
int showrushbonus = 0;
int noshare = 0; // Used for when you want to keep p1 & p2 credits separate
int nodropen = 0; // Drop or not when spawning is now a modder option
int gfx_y_offset = 0;
int timeleft = 0;
int oldtime = 0; // One second back from time left.
int holez = 0; // Used for setting spawn points
int allow_secret_chars = 0;
unsigned int lifescore = 50000; // Number of points needed to earn a 1-up
unsigned int credscore = 0; // Number of points needed to earn a credit
int mpblock = 0; // Take chip damage from health or MP first?
int blockratio = 0; // Take half-damage while blocking?
int nochipdeath = 0; // Prevents entities from dying due to chip damage (damage while blocking)
int noaircancel = 0; // Now, you can make jumping attacks uncancellable!
int nomaxrushreset[5] = { 0, 0, 0, 0, 0 };
int mpbartext[4] = { -1, 0, 0, 0 }; // Array for adjusting MP status text (font, Xpos, Ypos, Display type).
int lbartext[4] = { -1, 0, 0, 0 }; // Array for adjusting HP status text (font, Xpos, Ypos, Display type).
int pmp[4][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; // Used for customizable player mpbar
int spdirection[4] = { 1, 0, 1, 0 }; // Used for Select Player Direction for select player screen
int bonus = 0; // Used for unlocking Bonus difficulties
int versusdamage = 2; // Used for setting mode. (ability to hit other players)
int same[MAX_DIFFICULTIES]; // ltb 1-13-05 sameplayer
int z_coords[3] = { 0, 0, 0 }; // Used for setting customizable walkable area
int rush[6] = { 0, 2, 3, 3, 3, 3 };
s_colors colors = {0};
int lifebarfgalpha = 0;
int lifebarbgalpha = 2;
int shadowsprites[6] = { -1, -1, -1, -1, -1, -1 };
int gosprite = -1;
int golsprite = -1;
int holesprite = -1;
unsigned int videoMode = 0;
int scoreformat = 0; // If set fill score values with 6 Zeros
// Funny neon lights
unsigned char neontable[MAX_PAL_SIZE];
unsigned int neon_time = 0;
s_panel panels[MAX_PANELS];
unsigned int panels_loaded = 0;
int panel_width = 0;
int panel_height = 0;
s_sprite *frontpanels[MAX_PANELS];
unsigned int frontpanels_loaded = 0;
unsigned int sprites_loaded = 0;
unsigned int anims_loaded = 0;
unsigned int models_loaded = 0;
unsigned int models_cached = 0;
entity *ent_list[MAX_ENTS];
entity *self;
int ent_count = 0; // log count of entites
int ent_max = 0;
int combodelay = GAME_SPEED / 2;
s_player player[4];
u32 bothkeys, bothnewkeys;
s_playercontrols playercontrols1;
s_playercontrols playercontrols2;
s_playercontrols playercontrols3;
s_playercontrols playercontrols4;
s_playercontrols *playercontrolpointers[] = { &playercontrols1, &playercontrols2, &playercontrols3, &playercontrols4 };
s_savelevel savelevel[MAX_DIFFICULTIES];
s_savescore savescore;
s_savedata savedata;
s_game_scripts game_scripts;
extern Script *pcurrentscript; //used by local script functions
void common_walkoff(void);
//-------------------------methods-------------------------------
#define DEFAULT_SHUTDOWN_MESSAGE \
"OpenBOR %s, Compile Date: " __DATE__ "\n" \
"Presented by Senile Team.\n" \
"This Version is unofficial and based on the Senile source code.\n" \
"\n" \
"Special thanks to SEGA and SNK.\n\n", \
VERSION
void leave_game(void) {
if(strcmp(packfile, MENU_PACK_FILENAME))
saveHighScoreFile();
shutdown(0, DEFAULT_SHUTDOWN_MESSAGE);
}
static void setDestIfDestNeg_int(int* dest, int source) {
if(*dest < 0) *dest = source;
}
static void setDestIfDestNeg_short(short* dest, short source) {
if(*dest < 0) *dest = source;
}
static void setDestIfDestNeg_char(char* dest, char source) {
if(*dest < 0) *dest = source;
}
void setDrawMethod(s_anim * a, ptrdiff_t index, s_drawmethod * m) {
assert(index >= 0);
assert(a != NULL);
assert(m != NULL);
assert(index < a->numframes);
a->drawmethods[index] = m;
}
s_drawmethod *getDrawMethod(s_anim * a, ptrdiff_t index) {
assert(index >= 0);
assert(a != NULL);
assert(index < a->numframes);
return a->drawmethods[index];
}
int isLoadingScreenTypeBg(loadingScreenType what) {
return (what & LSTYPE_BACKGROUND) == LSTYPE_BACKGROUND;
}
int isLoadingScreenTypeBar(loadingScreenType what) {
return (what & LSTYPE_BAR) == LSTYPE_BAR;
}
char *fill_s_loadingbar(s_loadingbar * s, char set, short bx, short by, short bsize, short tx, short ty, char tf,
int ms) {
switch (set) {
case 1:
s->set = (LSTYPE_BACKGROUND | LSTYPE_BAR);
break;
case 2:
s->set = LSTYPE_BACKGROUND;
break;
case 3:
s->set = LSTYPE_BAR;
break;
case 0:
s->set = LSTYPE_NONE;
break;
default:
s->set = LSTYPE_NONE;
printf("invalid loadingbg type %d!\n", set);
}
s->tf = tf;
s->bx = bx;
s->by = by;
s->bsize = bsize;
s->tx = tx;
s->ty = ty;
s->refreshMs = (ms ? ms : 100);
return NULL;
}
// returns: 1 - succeeded 0 - failed
int buffer_pakfile(char *filename, char **pbuffer, size_t * psize) {
int handle;
*psize = 0;
*pbuffer = NULL;
// Read file
PDEBUG("pakfile requested: %s.\n", filename); //ASDF
if((handle = openpackfile(filename, packfile)) < 0) {
PDEBUG("couldnt get handle!\n");
return 0;
}
*psize = seekpackfile(handle, 0, SEEK_END);
seekpackfile(handle, 0, SEEK_SET);
*pbuffer = (char *) malloc(*psize + 1);
if(*pbuffer == NULL) {
*psize = 0;
closepackfile(handle);
shutdown(1, "Can't create buffer for packfile '%s'", filename);
return 0;
}
if(readpackfile(handle, *pbuffer, *psize) != *psize) {
freeAndNull((void**) pbuffer);
*psize = 0;
closepackfile(handle);
shutdown(1, "Can't read from packfile '%s'", filename);
return 0;
}
(*pbuffer)[*psize] = 0; // Terminate string (important!)
closepackfile(handle);
return 1;
}
//this method is used by script engine, we move it here
// it will get a system property, put it in the ScriptVariant
// if failed return 0, otherwise return 1
int getsyspropertybyindex(ScriptVariant * var, int index) {
enum nameenum {
_e_branchname,
_e_count_enemies,
_e_count_entities,
_e_count_npcs,
_e_count_players,
_e_current_level,
_e_current_palette,
_e_current_set,
_e_current_stage,
_e_elapsed_time,
_e_ent_max,
_e_game_paused,
_e_game_speed,
_e_gfx_y_offset,
_e_hResolution,
_e_in_level,
_e_in_selectscreen,
_e_lasthita,
_e_lasthitc,
_e_lasthitt,
_e_lasthitx,
_e_lasthitz,
_e_levelheight,
_e_levelwidth,
_e_lightx,
_e_lightz,
_e_maxentityvars,
_e_maxglobalvars,
_e_maxindexedvars,
_e_maxplayers,
_e_maxscriptvars,
_e_models_cached,
_e_models_loaded,
_e_numpalettes,
_e_pixelformat,
_e_player,
_e_player1,
_e_player2,
_e_player3,
_e_player4,
_e_player_max_z,
_e_player_min_z,
_e_shadowalpha,
_e_shadowcolor,
_e_slowmotion,
_e_slowmotion_duration,
_e_totalram,
_e_freeram,
_e_usedram,
_e_vResolution,
_e_xpos,
_e_ypos,
_e_the_end,
};
if(!var)
return 0;
switch (index) {
case _e_lasthita: case _e_lasthitx: case _e_lasthitz:
case _e_xpos: case _e_ypos:
ScriptVariant_ChangeType(var, VT_DECIMAL);
switch(index) {
case _e_xpos: case _e_ypos:
if(!level)
return 0;
if(index == _e_xpos)
var->dblVal = (double) advancex;
else
var->dblVal = (double) advancey;
break;
case _e_lasthita: var->dblVal = (double) (lasthita); break;
case _e_lasthitx: var->dblVal = (double) (lasthitx); break;
case _e_lasthitz: var->dblVal = (double) (lasthitz); break;
default:
assert(0);
}
break;
case _e_branchname:
ScriptVariant_ChangeType(var, VT_STR);
strcpy(StrCache_Get(var->strVal), branch_name);
break;
case _e_player: case _e_player1: case _e_player2:
case _e_player3: case _e_player4:
ScriptVariant_ChangeType(var, VT_PTR);
switch(index) {
case _e_player: case _e_player1: var->ptrVal = (void*) player; break;
case _e_player2: var->ptrVal = (void*) (player + 1); break;
case _e_player3: var->ptrVal = (void*) (player + 2); break;
case _e_player4: var->ptrVal = (void*) (player + 3); break;
default: assert (0);
}
break;
case _e_count_enemies: case _e_count_players: case _e_count_npcs: case _e_count_entities:
case _e_ent_max: case _e_in_level: case _e_elapsed_time: case _e_in_selectscreen:
case _e_lasthitc: case _e_lasthitt: case _e_hResolution: case _e_vResolution:
case _e_current_set: case _e_current_level: case _e_current_palette: case _e_current_stage:
case _e_maxentityvars: case _e_maxglobalvars: case _e_maxindexedvars: case _e_maxplayers:
case _e_maxscriptvars: case _e_models_loaded: case _e_numpalettes: case _e_pixelformat:
case _e_player_max_z: case _e_player_min_z: case _e_lightx: case _e_lightz:
case _e_shadowalpha: case _e_slowmotion: case _e_slowmotion_duration: case _e_game_paused:
case _e_totalram: case _e_freeram: case _e_usedram:
ScriptVariant_ChangeType(var, VT_INTEGER);
switch (index) {
case _e_count_enemies: var->lVal = (s32) count_ents(TYPE_ENEMY); break;
case _e_count_players: var->lVal = (s32) count_ents(TYPE_PLAYER); break;
case _e_count_npcs: var->lVal = (s32) count_ents(TYPE_NPC); break;
case _e_count_entities: var->lVal = (s32) ent_count; break;
case _e_ent_max: var->lVal = (s32) ent_max; break;
case _e_in_level: var->lVal = (s32) (level == NULL); break;
case _e_elapsed_time: var->lVal = (s32) borTime; break;
case _e_in_selectscreen: var->lVal = (s32) (selectScreen); break;
case _e_lasthitc: var->lVal = (s32) (lasthitc); break;
case _e_lasthitt: var->lVal = (s32) (lasthitt); break;
case _e_hResolution: var->lVal = (s32) videomodes.hRes; break;
case _e_vResolution: var->lVal = (s32) videomodes.vRes; break;
case _e_current_set: var->lVal = (s32) (current_set); break;
case _e_current_level: var->lVal = (s32) (current_level); break;
case _e_current_palette: var->lVal = (s32) (current_palette); break;
case _e_current_stage: var->lVal = (s32) (current_stage); break;
case _e_maxentityvars: var->lVal = (s32) max_entity_vars; break;
case _e_maxglobalvars: var->lVal = (s32) max_global_vars; break;
case _e_maxindexedvars: var->lVal = (s32) max_indexed_vars; break;
case _e_maxplayers: var->lVal = (s32) maxplayers[current_set]; break;
case _e_maxscriptvars: var->lVal = (s32) max_script_vars; break;
case _e_models_cached: var->lVal = (s32) models_cached; break;
case _e_models_loaded: var->lVal = (s32) models_loaded; break;
case _e_numpalettes: var->lVal = (s32) (level->numpalettes); break;
case _e_pixelformat: var->lVal = (s32) pixelformat; break;
case _e_player_max_z: var->lVal = (s32) (PLAYER_MAX_Z); break;
case _e_player_min_z: var->lVal = (s32) (PLAYER_MIN_Z); break;
case _e_lightx: var->lVal = (s32) (light[0]); break;
case _e_lightz: var->lVal = (s32) (light[1]); break;
case _e_shadowalpha: var->lVal = (s32) shadowalpha; break;
case _e_shadowcolor: var->lVal = (s32) colors.shadow; break;
case _e_slowmotion: var->lVal = (s32) slowmotion[0]; break;
case _e_slowmotion_duration: var->lVal = (s32) slowmotion[1]; break;
case _e_game_paused: var->lVal = (s32) pause; break;
case _e_totalram: var->lVal = 64 * 1024; break;
case _e_freeram: var->lVal = 63 * 1024; break;
case _e_usedram: var->lVal = 1024; break;
default:
assert (0); break;
}
break;
case _e_game_speed: case _e_gfx_y_offset: case _e_levelwidth: case _e_levelheight:
if(!level)
return 0;
ScriptVariant_ChangeType(var, VT_INTEGER);
switch (index) {
case _e_game_speed: var->lVal = (s32) GAME_SPEED; break;
case _e_gfx_y_offset: var->lVal = (s32) gfx_y_offset; break;
case _e_levelwidth: var->lVal = (s32) (level->width); break;
case _e_levelheight: var->lVal = (s32) (panel_height); break;
default: assert(0); break;
}
break;
default:
// We use indices now, but players/modders don't need to be exposed
// to that implementation detail, so we write "name" and not "index".
printf("Unknown system property name.\n");
return 0;
}
return 1;
}
// change a system variant, used by script
int changesyspropertybyindex(int index, ScriptVariant * value) {
//char* tempstr = NULL;
s32 ltemp;
//double dbltemp;
// This enum is replicated in mapstrings_changesystemvariant in
// openborscript.c. If you change one, you must change the other as well!!!!
enum changesystemvariant_enum {
_csv_blockade,
_csv_elapsed_time,
_csv_lasthita,
_csv_lasthitc,
_csv_lasthitt,
_csv_lasthitx,
_csv_lasthitz,
_csv_levelpos,
_csv_scrollmaxz,
_csv_scrollminz,
_csv_slowmotion,
_csv_slowmotion_duration,
_csv_smartbomber,
_csv_textbox,
_csv_xpos,
_csv_ypos,
_csv_the_end,
};
switch (index) {
case _csv_elapsed_time: case _csv_levelpos: case _csv_xpos:
case _csv_ypos: case _csv_scrollminz: case _csv_scrollmaxz:
case _csv_blockade: case _csv_slowmotion: case _csv_slowmotion_duration:
case _csv_lasthitx: case _csv_lasthita: case _csv_lasthitc:
case _csv_lasthitz: case _csv_lasthitt:
if(SUCCEEDED(ScriptVariant_IntegerValue(value, <emp))) {
switch(index) {
case _csv_elapsed_time: borTime = (int) ltemp; break;
case _csv_levelpos: level->pos = (int) ltemp; break;
case _csv_xpos: advancex = (float) ltemp; break;
case _csv_ypos: advancey = (float) ltemp; break;
case _csv_scrollminz: scrollminz = (float) ltemp; break;
case _csv_scrollmaxz: scrollmaxz = (float) ltemp; break;
case _csv_blockade: blockade = (float) ltemp; break;
case _csv_slowmotion: slowmotion[0] = (unsigned char) ltemp; break;
case _csv_slowmotion_duration: slowmotion[1] = (unsigned char) ltemp; break;
case _csv_lasthitx: lasthitx = (float) ltemp; break;
case _csv_lasthita: lasthita = (float) ltemp; break;
case _csv_lasthitc: lasthitc = (int) ltemp; break;
case _csv_lasthitz: lasthitz = (float) ltemp; break;
case _csv_lasthitt: lasthitt = (int) ltemp; break;
default: assert(0);
}
}
break;
case _csv_smartbomber:
smartbomber = (entity *) value;
break;
case _csv_textbox:
textbox = (entity *) value;
break;
default:
return 0;
}
return 1;
}
int load_script(Script * script, char *file) {
size_t size = 0;
int failed = 0;
char *buf = NULL;
if(buffer_pakfile(file, &buf, &size) != 1)
return 0;
failed = !Script_AppendText(script, buf, file);
freeAndNull((void**) &buf);
// text loaded but parsing failed, shutdown
if(failed)
shutdown(1, "Failed to parse script file: '%s'!\n", file);
return !failed;
}
// this method is used by load_scripts, don't call it
void init_scripts() {
int i;
Script_Global_Init();
for (i = 0; i < script_and_path_and_name_itemcount; i++) {
Script_Init(script_and_path_and_name[i].script, script_and_path_and_name[i].name, 1);
}
}
// This method is called once when the engine starts, do not use it multiple times
// It should be calld after load_script_setting
void load_scripts() {
int i;
init_scripts();
//Script_Clear's second parameter set to 2, because the script fails to load,
//and will never have another chance to be loaded, so just clear the variable list in it
for (i = 0; i < script_and_path_and_name_itemcount; i++) {
if(!load_script(script_and_path_and_name[i].script, script_and_path_and_name[i].path))
Script_Clear(script_and_path_and_name[i].script, 2);
Script_Compile(script_and_path_and_name[i].script);
}
}
// This method is called once when the engine is shutting down, do not use it multiple times
void clear_scripts() {
int i;
//Script_Clear's second parameter set to 2, because the script fails to load,
//and will never have another chance to be loaded, so just clear the variable list in it
for(i = 0; i < script_and_path_and_name_itemcount; i++) {
Script_Clear(script_and_path_and_name[i].script, 2);
}
Script_Global_Clear();
}
void alloc_all_scripts(s_scripts * s) {
static const size_t scripts_membercount = sizeof(s_scripts) / sizeof(Script *);
size_t i;
for(i = 0; i < scripts_membercount; i++) {
(((Script **) s)[i]) = alloc_script();
}
}
void clear_all_scripts(s_scripts * s, int method) {
static const size_t scripts_membercount = sizeof(s_scripts) / sizeof(Script *);
size_t i;
Script **ps = (Script **) s;
for(i = 0; i < scripts_membercount; i++) {
Script_Clear(ps[i], method);
}
}
void free_all_scripts(s_scripts * s) {
static const size_t scripts_membercount = sizeof(s_scripts) / sizeof(Script *);
size_t i;
Script **ps = (Script **) s;
for(i = 0; i < scripts_membercount; i++) {
freeAndNull((void**) &ps[i]);
}
}
void copy_all_scripts(s_scripts * src, s_scripts * dest, int method) {
static const size_t scripts_membercount = sizeof(s_scripts) / sizeof(Script *);
size_t i;
Script **ps = (Script **) src;
Script **pd = (Script **) dest;
for(i = 0; i < scripts_membercount; i++) {
Script_Copy(pd[i], ps[i], method);
}
}
static const s_script_args_names script_args_names = {
.ent = "self",
.attacker = "attacker",
.drop = "drop",
.type = "attacktype",
.noblock = "noblock",
.guardcost = "guardcost",
.jugglecost = "jugglecost",
.pauseadd = "pauseadd",
.which = "which",
.atkid = "attackid",
.blocked = "blocked",
.animnum = "animnum",
.frame = "frame",
.player = "player",
.attacktype = "attacktype",
.reset = "reset",
.plane = "plane",
.height = "height",
.obstacle = "obstacle",
.time = "time",
.gotime = "gotime",
.damage = "damage",
.damagetaker = "damagetaker",
.other = "other",
};
static const s_script_args init_script_args_default = {
.ent = {VT_PTR, 0},
.attacker = {VT_PTR, 0},
.drop = {VT_INTEGER, 0},
.type = {VT_INTEGER, 0},
.noblock = {VT_INTEGER, 0},
.guardcost = {VT_INTEGER, 0},
.jugglecost = {VT_INTEGER, 0},
.pauseadd = {VT_INTEGER, 0},
.which = {VT_EMPTY, 0},
.atkid = {VT_EMPTY, 0},
.blocked = {VT_EMPTY, 0},
.animnum = {VT_EMPTY, 0},
.frame = {VT_EMPTY, 0},
.player = {VT_EMPTY, 0},
.attacktype = {VT_EMPTY, 0},
.reset = {VT_EMPTY, 0},
.plane = {VT_EMPTY, 0},
.height = {VT_EMPTY, 0},
.obstacle = {VT_EMPTY, 0},
.time = {VT_EMPTY, 0},
.gotime = {VT_EMPTY, 0},
.damage = {VT_INTEGER, 0},
.damagetaker = {VT_EMPTY, 0},
.other = {VT_EMPTY, 0},
};
static const s_script_args init_script_args_only_ent = {
.ent = {VT_PTR, 0},
.attacker = {VT_EMPTY, 0},
.drop = {VT_EMPTY, 0},
.type = {VT_EMPTY, 0},
.noblock = {VT_EMPTY, 0},
.guardcost = {VT_EMPTY, 0},
.jugglecost = {VT_EMPTY, 0},
.pauseadd = {VT_EMPTY, 0},
.which = {VT_EMPTY, 0},
.atkid = {VT_EMPTY, 0},
.blocked = {VT_EMPTY, 0},
.animnum = {VT_EMPTY, 0},
.frame = {VT_EMPTY, 0},
.player = {VT_EMPTY, 0},
.attacktype = {VT_EMPTY, 0},
.reset = {VT_EMPTY, 0},
.plane = {VT_EMPTY, 0},
.height = {VT_EMPTY, 0},
.obstacle = {VT_EMPTY, 0},
.time = {VT_EMPTY, 0},
.gotime = {VT_EMPTY, 0},
.damage = {VT_EMPTY, 0},
.damagetaker = {VT_EMPTY, 0},
.other = {VT_EMPTY, 0},
};
static void execute_script_default(s_script_args* args, Script* dest_script) {
ScriptVariant tempvar;
Script *ptempscript = pcurrentscript;
s_script_args_tuple* tuples = (s_script_args_tuple*) args;
char** names = (char**) &script_args_names;
unsigned i;
float tmp_float;
if(Script_IsInitialized(dest_script)) {
ScriptVariant_Init(&tempvar);
for(i = 0; i < s_script_args_membercount; i++) {
if(tuples[i].vt != VT_EMPTY) {
ScriptVariant_ChangeType(&tempvar, tuples[i].vt);
switch(tuples[i].vt) {
case VT_PTR:
tempvar.ptrVal = (void*) tuples[i].value;
break;
case VT_INTEGER:
tempvar.lVal = (s32) tuples[i].value;
break;
case VT_DECIMAL:
memcpy(&tmp_float, &tuples[i].value, sizeof(float));
tempvar.dblVal = (double) tmp_float;
break;
default:
assert(0);
}
Script_Set_Local_Variant(names[i], &tempvar);
}
}
Script_Execute(dest_script);
//clear to save variant space
ScriptVariant_Clear(&tempvar);
for(i = 0; i < s_script_args_membercount; i++) {
if(tuples[i].vt != VT_EMPTY)
Script_Set_Local_Variant(names[i], &tempvar);
}
}
pcurrentscript = ptempscript;
}
static void execute_takedamage_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.takedamage_script);
}
static void execute_onfall_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.onfall_script);
}
static void execute_ondeath_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.ondeath_script);
}
static void execute_didblock_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.didblock_script);
}
static void execute_ondoattack_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.ondoattack_script);
}
static void execute_didhit_script_i(s_script_args* args) {
execute_script_default(args, ((entity*) args->ent.value)->scripts.didhit_script);
}
void execute_takedamage_script(entity * ent, entity * other, int force, int drop, int type, int noblock, int guardcost,
int jugglecost, int pauseadd) {
s_script_args script_args = init_script_args_default;
script_args.ent.value = (intptr_t) ent;
script_args.attacker.value = (intptr_t) other;
script_args.damage.value = force;
script_args.drop.value = drop;
script_args.type.value = type;
script_args.noblock.value = noblock;
script_args.guardcost.value = guardcost;
script_args.jugglecost.value = jugglecost;
script_args.pauseadd.value = pauseadd;
execute_takedamage_script_i(&script_args);
}
void execute_ondeath_script(entity * ent, entity * other, int force, int drop, int type, int noblock, int guardcost,
int jugglecost, int pauseadd) {
s_script_args script_args = init_script_args_default;
script_args.ent.value = (intptr_t) ent;
script_args.attacker.value = (intptr_t) other;
script_args.damage.value = force;
script_args.drop.value = drop;
script_args.type.value = type;
script_args.noblock.value = noblock;
script_args.guardcost.value = guardcost;
script_args.jugglecost.value = jugglecost;
script_args.pauseadd.value = pauseadd;
execute_ondeath_script_i(&script_args);
}
void execute_onfall_script(entity * ent, entity * other, int force, int drop, int type, int noblock, int guardcost,
int jugglecost, int pauseadd) {
s_script_args script_args = init_script_args_default;
script_args.ent.value = (intptr_t) ent;
script_args.attacker.value = (intptr_t) other;
script_args.damage.value = force;
script_args.drop.value = drop;
script_args.type.value = type;
script_args.noblock.value = noblock;
script_args.guardcost.value = guardcost;
script_args.jugglecost.value = jugglecost;
script_args.pauseadd.value = pauseadd;
execute_onfall_script_i(&script_args);
}
void execute_didblock_script(entity * ent, entity * other, int force, int drop, int type, int noblock, int guardcost,
int jugglecost, int pauseadd) {
s_script_args script_args = init_script_args_default;
script_args.ent.value = (intptr_t) ent;
script_args.attacker.value = (intptr_t) other;
script_args.damage.value = force;
script_args.drop.value = drop;
script_args.type.value = type;
script_args.noblock.value = noblock;
script_args.guardcost.value = guardcost;
script_args.jugglecost.value = jugglecost;
script_args.pauseadd.value = pauseadd;
execute_didblock_script_i(&script_args);
}
void execute_ondoattack_script(entity * ent, entity * other, int force, int drop, int type, int noblock, int guardcost,
int jugglecost, int pauseadd, int iWhich, int iAtkID) {
s_script_args script_args = init_script_args_default;
script_args.ent.value = (intptr_t) ent;
script_args.attacker.vt = VT_EMPTY;
script_args.other.vt = VT_PTR;
script_args.other.value = (intptr_t) other;
/* yep, that one calls it "other", all the others "attacker" */
script_args.damage.value = force;