forked from rofl0r/openbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenborscript.c
10055 lines (9193 loc) · 259 KB
/
openborscript.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 licence, see LICENSE in OpenBOR root for details.
*
* Copyright (c) 2004 - 2011 OpenBOR Team
*/
/* This file include all script methods used by openbor engine
Notice: Make sure to null *pretvar when you about to return E_FAIL,
Or the engine might crash.
Notice: Every new ScriptVariant must be initialized when you first alloc it by
ScriptVariant_Init immediately, memset it all to zero should also work by now,
unless VT_EMPTY is changed.
If you want to reset a ScriptVariant to empty, you must use ScriptVariant_Clear instead.
ScriptVariant_Init or memset must be called only ONCE, later you should use ScriptVariant_Clear.
Besure to call ScriptVariant_Clear if you want to use free to delete those variants.
If you want to copy a ScriptVariant from another, use ScriptVariant_Copy instead of assignment,
not because it is faster, but this method is neccessary for string types.
If you want to change types of an ScriptVariant, use ScriptVariant_ChangeType, don't change vt directly.
*/
#include "data.h"
#include "openborscript.h"
#include "openbor.h"
#include "soundmix.h"
#include "globals.h"
#include "ImportCache.h"
#include "models.h"
#include "commands.h"
// Define macro for string mapping
#define MAPSTRINGS(VAR, LIST, MAXINDEX, FAILMSG) \
if(VAR->vt == VT_STR) { \
propname = (char*)StrCache_Get(VAR->strVal); \
prop = searchList(LIST, propname, MAXINDEX); \
if(prop >= 0) { \
ScriptVariant_ChangeType(VAR, VT_INTEGER); \
VAR->lVal = prop; \
} else { printf(FAILMSG, propname); } \
}
extern int MAX_WALL_HEIGHT;
extern int current_palette;
extern s_player player[4];
extern s_savedata savedata;
extern s_savelevel savelevel[MAX_DIFFICULTIES];
extern s_savescore savescore;
extern s_level *level;
extern entity *self;
extern int noshare;
extern int credits;
extern char musicname[128];
extern float musicfade[2];
extern int musicloop;
extern u32 musicoffset;
extern int models_cached;
extern unsigned char *blendings[MAX_BLENDINGS];
extern int current_palette;
s_variantnode **global_var_list = NULL;
Script *pcurrentscript = NULL; //used by local script functions
List theFunctionList;
static List scriptheap;
static s_spawn_entry spawnentry;
static s_drawmethod drawmethod;
int max_global_var_index = -1;
ScriptVariant *indexed_var_list = NULL;
int max_global_vars = MAX_GLOBAL_VAR;
int max_indexed_vars = 0;
int max_entity_vars = 0;
int max_script_vars = 0;
//this function should be called before all script methods, for once
void Script_Global_Init() {
ptrdiff_t i;
size_t csize, psize;
if(max_global_vars > 0) {
psize = (sizeof(s_variantnode *) * max_global_vars);
csize = psize + (sizeof(s_variantnode) * max_global_vars);
global_var_list = malloc(csize);
assert(global_var_list != NULL);
memset(global_var_list, 0, csize);
for(i = 0; i < max_global_vars; i++) {
global_var_list[i] =
(s_variantnode *) (((char *) global_var_list) + psize + (i * sizeof(s_variantnode)));
}
}
/*
for(i=0; i<max_global_vars; i++)
{
global_var_list[i] = malloc(sizeof(s_variantnode));
assert(global_var_list[i] != NULL);
memset(global_var_list[i], 0, sizeof(s_variantnode));
} */
max_global_var_index = -1;
memset(&spawnentry, 0, sizeof(s_spawn_entry)); //clear up the spawn entry
drawmethod = plainmethod;
if(max_indexed_vars > 0) {
csize = sizeof(ScriptVariant) * (max_indexed_vars + 1);
indexed_var_list = (ScriptVariant *) malloc(csize);
assert(indexed_var_list != NULL);
memset(indexed_var_list, 0, csize);
}
List_Init(&theFunctionList);
Script_LoadSystemFunctions();
List_Init(&scriptheap);
ImportCache_Init();
}
//this function should only be called when the engine is shutting down
void Script_Global_Clear() {
int i, size;
List_Clear(&theFunctionList);
// dump all un-freed variants
size = List_GetSize(&scriptheap);
if(size > 0)
printf("\nWarning: %d script variants are not freed, dumping...\n", size);
for(i = 0, List_Reset(&scriptheap); i < size; List_GotoNext(&scriptheap), i++) {
printf("%s\n", List_GetName(&scriptheap));
free(List_Retrieve(&scriptheap));
}
List_Clear(&scriptheap);
// clear the global list
if(global_var_list) {
for(i = 0; i < max_global_vars; i++) {
if(global_var_list[i] != NULL) {
ScriptVariant_Clear(&(global_var_list[i]->value));
//free(global_var_list[i]);
}
//global_var_list[i] = NULL;
}
free(global_var_list);
global_var_list = NULL;
}
if(indexed_var_list) {
for(i = 0; i < max_indexed_vars; i++)
ScriptVariant_Clear(indexed_var_list + i);
free(indexed_var_list);
}
indexed_var_list = NULL;
max_global_var_index = -1;
memset(&spawnentry, 0, sizeof(s_spawn_entry)); //clear up the spawn entry
StrCache_Clear();
ImportCache_Clear();
}
ScriptVariant *Script_Get_Global_Variant(char *theName) {
int i;
if(!theName || !theName[0])
return NULL;
for(i = 0; i <= max_global_var_index; i++) {
if(!global_var_list[i]->owner && strcmp(theName, global_var_list[i]->key) == 0)
return &(global_var_list[i]->value);
}
return NULL;
}
// local function
int _set_var(char *theName, ScriptVariant * var, Script * owner) {
int i;
s_variantnode *tempnode;
if(!theName[0] || !theName || (owner && !owner->initialized))
return 0;
// search the name
for(i = 0; i <= max_global_var_index; i++) {
if(global_var_list[i]->owner == owner && !strcmp(theName, global_var_list[i]->key)) {
if(var->vt != VT_EMPTY)
ScriptVariant_Copy(&(global_var_list[i]->value), var);
else // set to null, so remove this value
{
/// re-adjust bounds, swap with last node
if(i != max_global_var_index) {
tempnode = global_var_list[i];
global_var_list[i] = global_var_list[max_global_var_index];
global_var_list[max_global_var_index] = tempnode;
}
max_global_var_index--;
}
return 1;
}
}
if(var->vt == VT_EMPTY)
return 1;
// all slots are taken
if(max_global_var_index >= max_global_vars - 1)
return 0;
// so out of bounds, find another slot
else {
++max_global_var_index;
ScriptVariant_Copy(&(global_var_list[max_global_var_index]->value), var);
global_var_list[max_global_var_index]->owner = owner;
strcpy(global_var_list[max_global_var_index]->key, theName);
return 1;
}
} // end of _set_var
int Script_Set_Global_Variant(char *theName, ScriptVariant * var) {
return _set_var(theName, var, NULL);
}
void Script_Local_Clear() {
int i;
s_variantnode *tempnode;
if(!pcurrentscript)
return;
for(i = 0; i <= max_global_var_index; i++) {
if(global_var_list[i]->owner == pcurrentscript) {
if(i != max_global_var_index) {
tempnode = global_var_list[i];
global_var_list[i] = global_var_list[max_global_var_index];
global_var_list[max_global_var_index] = tempnode;
}
max_global_var_index--;
}
}
if(pcurrentscript->vars)
for(i = 0; i < max_script_vars; i++)
ScriptVariant_Clear(pcurrentscript->vars + i);
}
ScriptVariant *Script_Get_Local_Variant(char *theName) {
int i;
if(!pcurrentscript || !pcurrentscript->initialized || !theName || !theName[0])
return NULL;
for(i = 0; i <= max_global_var_index; i++) {
if(global_var_list[i]->owner == pcurrentscript && strcmp(theName, global_var_list[i]->key) == 0)
return &(global_var_list[i]->value);
}
return NULL;
}
int Script_Set_Local_Variant(char *theName, ScriptVariant * var) {
if(!pcurrentscript)
return 0;
return _set_var(theName, var, pcurrentscript);
}
Script *alloc_script() {
int i;
Script *pscript = (Script *) malloc(sizeof(Script));
memset(pscript, 0, sizeof(Script));
if(max_script_vars > 0) {
pscript->vars = (ScriptVariant *) malloc(sizeof(ScriptVariant) * max_script_vars);
for(i = 0; i < max_script_vars; i++)
ScriptVariant_Init(pscript->vars + i);
}
return pscript;
}
void Script_Init(Script * pscript, char *theName, int first) {
int i;
if(first) {
memset(pscript, 0, sizeof(Script));
if(max_script_vars > 0) {
pscript->vars = (ScriptVariant *) malloc(sizeof(ScriptVariant) * max_script_vars);
for(i = 0; i < max_script_vars; i++)
ScriptVariant_Init(pscript->vars + i);
}
}
if(!theName || !theName[0])
return; // if no name specified, only alloc the variants
pcurrentscript = pscript; //used by local script functions
pscript->pinterpreter = (Interpreter *) malloc(sizeof(Interpreter));
Interpreter_Init(pscript->pinterpreter, theName, &theFunctionList);
pscript->interpreterowner = 1; // this is the owner, important
pscript->initialized = 1;
}
//safe copy method
void Script_Copy(Script * pdest, Script * psrc, int localclear) {
if(!psrc->initialized)
return;
if(pdest->initialized)
Script_Clear(pdest, localclear);
pdest->pinterpreter = psrc->pinterpreter;
pdest->interpreterowner = 0; // dont own it
pdest->initialized = psrc->initialized; //just copy, it should be 1
}
void Script_Clear(Script * pscript, int localclear) {
Script *temp;
int i;
ScriptVariant *pvars;
if(localclear == 2 && pscript->vars) {
for(i = 0; i < max_script_vars; i++) {
ScriptVariant_Clear(pscript->vars + i);
}
free(pscript->vars);
pscript->vars = NULL;
}
if(!pscript->initialized)
return;
temp = pcurrentscript;
pcurrentscript = pscript; //used by local script functions
//if it is the owner, free the interpreter
if(pscript->pinterpreter && pscript->interpreterowner) {
Interpreter_Clear(pscript->pinterpreter);
free(pscript->pinterpreter);
pscript->pinterpreter = NULL;
}
if(localclear)
Script_Local_Clear();
pvars = pscript->vars; // in game clear(localclear!=2) just keep this value
memset(pscript, 0, sizeof(Script));
pscript->vars = pvars; // copy it back
pcurrentscript = temp;
}
//append part of the script
//Because the script might not be initialized in 1 time.
int Script_AppendText(Script * pscript, char *text, char *path) {
int success;
pcurrentscript = pscript; //used by local script functions
//printf(text);
Interpreter_Reset(pscript->pinterpreter);
success = SUCCEEDED(Interpreter_ParseText(pscript->pinterpreter, text, 1, path));
return success;
}
//return name of function from pointer to function
const char *Script_GetFunctionName(void *functionRef) {
if(functionRef == ((void *) system_isempty))
return "isempty";
else if(functionRef == ((void *) system_NULL))
return "NULL";
else if(functionRef == ((void *) system_rand))
return "rand";
else if(functionRef == ((void *) system_maxglobalvarindex))
return "maxglobalvarindex";
else if(functionRef == ((void *) system_getglobalvar))
return "getglobalvar";
else if(functionRef == ((void *) system_setglobalvar))
return "setglobalvar";
else if(functionRef == ((void *) system_getlocalvar))
return "getlocalvar";
else if(functionRef == ((void *) system_setlocalvar))
return "setlocalvar";
else if(functionRef == ((void *) system_clearglobalvar))
return "clearglobalvar";
else if(functionRef == ((void *) system_clearindexedvar))
return "clearindexedvar";
else if(functionRef == ((void *) system_clearlocalvar))
return "clearlocalvar";
else if(functionRef == ((void *) system_free))
return "free";
else if(functionRef == ((void *) openbor_systemvariant))
return "openborvariant";
else if(functionRef == ((void *) openbor_changesystemvariant))
return "changeopenborvariant";
else if(functionRef == ((void *) openbor_drawstring))
return "drawstring";
else if(functionRef == ((void *) openbor_drawstringtoscreen))
return "drawstringtoscreen";
else if(functionRef == ((void *) openbor_log))
return "log";
else if(functionRef == ((void *) openbor_drawbox))
return "drawbox";
else if(functionRef == ((void *) openbor_drawboxtoscreen))
return "drawboxtoscreen";
else if(functionRef == ((void *) openbor_drawline))
return "drawline";
else if(functionRef == ((void *) openbor_drawlinetoscreen))
return "drawlinetoscreen";
else if(functionRef == ((void *) openbor_drawsprite))
return "drawsprite";
else if(functionRef == ((void *) openbor_drawspritetoscreen))
return "drawspritetoscreen";
else if(functionRef == ((void *) openbor_drawdot))
return "drawdot";
else if(functionRef == ((void *) openbor_drawdottoscreen))
return "drawdottoscreen";
else if(functionRef == ((void *) openbor_drawscreen))
return "drawscreen";
else if(functionRef == ((void *) openbor_changeplayerproperty))
return "changeplayerproperty";
else if(functionRef == ((void *) openbor_changeentityproperty))
return "changeentityproperty";
else if(functionRef == ((void *) openbor_getplayerproperty))
return "getplayerproperty";
else if(functionRef == ((void *) openbor_getentityproperty))
return "getentityproperty";
else if(functionRef == ((void *) openbor_tossentity))
return "tossentity";
else if(functionRef == ((void *) openbor_clearspawnentry))
return "clearspawnentry";
else if(functionRef == ((void *) openbor_setspawnentry))
return "setspawnentry";
else if(functionRef == ((void *) openbor_spawn))
return "spawn";
else if(functionRef == ((void *) openbor_projectile))
return "projectile";
else if(functionRef == ((void *) openbor_transconst))
return "openborconstant";
else if(functionRef == ((void *) openbor_playmusic))
return "playmusic";
else if(functionRef == ((void *) openbor_fademusic))
return "fademusic";
else if(functionRef == ((void *) openbor_setmusicvolume))
return "setmusicvolume";
else if(functionRef == ((void *) openbor_setmusictempo))
return "setmusictempo";
else if(functionRef == ((void *) openbor_pausemusic))
return "pausemusic";
else if(functionRef == ((void *) openbor_playsample))
return "playsample";
else if(functionRef == ((void *) openbor_loadsample))
return "loadsample";
else if(functionRef == ((void *) openbor_unloadsample))
return "unloadsample";
else if(functionRef == ((void *) openbor_fadeout))
return "fadeout";
else if(functionRef == ((void *) openbor_playerkeys))
return "playerkeys";
else if(functionRef == ((void *) openbor_changepalette))
return "changepalette";
else if(functionRef == ((void *) openbor_damageentity))
return "damageentity";
else if(functionRef == ((void *) openbor_killentity))
return "killentity";
else if(functionRef == ((void *) openbor_findtarget))
return "findtarget";
else if(functionRef == ((void *) openbor_checkrange))
return "checkrange";
else if(functionRef == ((void *) openbor_gettextobjproperty))
return "gettextobjproperty";
else if(functionRef == ((void *) openbor_changetextobjproperty))
return "changetextobjproperty";
else if(functionRef == ((void *) openbor_settextobj))
return "settextobj";
else if(functionRef == ((void *) openbor_cleartextobj))
return "cleartextobj";
else if(functionRef == ((void *) openbor_getbglayerproperty))
return "getbglayerproperty";
else if(functionRef == ((void *) openbor_changebglayerproperty))
return "changebglayerproperty";
else if(functionRef == ((void *) openbor_getfglayerproperty))
return "getfglayerproperty";
else if(functionRef == ((void *) openbor_changefglayerproperty))
return "changefglayerproperty";
else if(functionRef == ((void *) openbor_getlevelproperty))
return "getlevelproperty";
else if(functionRef == ((void *) openbor_changelevelproperty))
return "changelevelproperty";
else if(functionRef == ((void *) openbor_checkhole))
return "checkhole";
else if(functionRef == ((void *) openbor_checkwall))
return "checkwall";
else if(functionRef == ((void *) openbor_checkplatformbelow))
return "checkplatformbelow";
else if(functionRef == ((void *) openbor_openfilestream))
return "openfilestream";
else if(functionRef == ((void *) openbor_getfilestreamline))
return "getfilestreamline";
else if(functionRef == ((void *) openbor_getfilestreamargument))
return "getfilestreamargument";
else if(functionRef == ((void *) openbor_filestreamnextline))
return "filestreamnextline";
else if(functionRef == ((void *) openbor_getfilestreamposition))
return "getfilestreamposition";
else if(functionRef == ((void *) openbor_setfilestreamposition))
return "setfilestreamposition";
else if(functionRef == ((void *) openbor_filestreamappend))
return "filestreamappend";
else if(functionRef == ((void *) openbor_createfilestream))
return "createfilestream";
else if(functionRef == ((void *) openbor_savefilestream))
return "savefilestream";
else if(functionRef == ((void *) openbor_getindexedvar))
return "getindexedvar";
else if(functionRef == ((void *) openbor_setindexedvar))
return "setindexedvar";
else if(functionRef == ((void *) openbor_getscriptvar))
return "getscriptvar";
else if(functionRef == ((void *) openbor_setscriptvar))
return "setscriptvar";
else if(functionRef == ((void *) openbor_getentityvar))
return "getentityvar";
else if(functionRef == ((void *) openbor_setentityvar))
return "setentityvar";
else if(functionRef == ((void *) openbor_jumptobranch))
return "jumptobranch";
else if(functionRef == ((void *) openbor_changelight))
return "changelight";
else if(functionRef == ((void *) openbor_changeshadowcolor))
return "changeshadowcolor";
else if(functionRef == ((void *) openbor_bindentity))
return "bindentity";
else if(functionRef == ((void *) openbor_allocscreen))
return "allocscreen";
else if(functionRef == ((void *) openbor_clearscreen))
return "clearscreen";
else if(functionRef == ((void *) openbor_setdrawmethod))
return "setdrawmethod";
else if(functionRef == ((void *) openbor_updateframe))
return "updateframe";
else if(functionRef == ((void *) openbor_performattack))
return "performattack";
else if(functionRef == ((void *) openbor_setidle))
return "setidle";
else if(functionRef == ((void *) openbor_getentity))
return "getentity";
else if(functionRef == ((void *) openbor_loadmodel))
return "loadmodel";
else if(functionRef == ((void *) openbor_loadsprite))
return "loadsprite";
else if(functionRef == ((void *) openbor_playgif))
return "playgif";
else if(functionRef == ((void *) openbor_strinfirst))
return "strinfirst";
else if(functionRef == ((void *) openbor_strinlast))
return "strinlast";
else if(functionRef == ((void *) openbor_strleft))
return "strleft";
else if(functionRef == ((void *) openbor_strlength))
return "strlength";
else if(functionRef == ((void *) openbor_strright))
return "strright";
else if(functionRef == ((void *) openbor_getmodelproperty))
return "getmodelproperty";
else if(functionRef == ((void *) openbor_changemodelproperty))
return "changemodelproperty";
else if(functionRef == ((void *) openbor_rgbcolor))
return "rgbcolor";
else
return "<unknown function>";
}
//return string mapping function corresponding to a given function
void *Script_GetStringMapFunction(void *functionRef) {
if(functionRef == ((void *) openbor_systemvariant))
return (void *) mapstrings_systemvariant;
else if(functionRef == ((void *) openbor_changesystemvariant))
return (void *) mapstrings_changesystemvariant;
else if(functionRef == ((void *) openbor_getentityproperty))
return (void *) mapstrings_getentityproperty;
else if(functionRef == ((void *) openbor_changeentityproperty))
return (void *) mapstrings_changeentityproperty;
else if(functionRef == ((void *) openbor_getplayerproperty))
return (void *) mapstrings_getplayerproperty;
else if(functionRef == ((void *) openbor_changeplayerproperty))
return (void *) mapstrings_changeplayerproperty;
else if(functionRef == ((void *) openbor_setspawnentry))
return (void *) mapstrings_setspawnentry;
else if(functionRef == ((void *) openbor_transconst))
return (void *) mapstrings_transconst;
else if(functionRef == ((void *) openbor_playerkeys))
return (void *) mapstrings_playerkeys;
else if(functionRef == ((void *) openbor_gettextobjproperty))
return (void *) mapstrings_gettextobjproperty;
else if(functionRef == ((void *) openbor_changetextobjproperty))
return (void *) mapstrings_changetextobjproperty;
else if(functionRef == ((void *) openbor_getbglayerproperty))
return (void *) mapstrings_getbglayerproperty;
else if(functionRef == ((void *) openbor_changebglayerproperty))
return (void *) mapstrings_changebglayerproperty;
else if(functionRef == ((void *) openbor_getfglayerproperty))
return (void *) mapstrings_getfglayerproperty;
else if(functionRef == ((void *) openbor_changefglayerproperty))
return (void *) mapstrings_changefglayerproperty;
else
return NULL;
}
/* Replace string constants with enum constants at compile time to speed up
script execution. */
int Script_MapStringConstants(Script * pscript) {
Interpreter *pinterpreter = pscript->pinterpreter;
Instruction *pInstruction, *pInstruction2;
ScriptVariant **params;
//ScriptVariant* var;
void (*pMapstrings) (ScriptVariant **, int);
int i, j, k, size, paramCount;
size = pinterpreter->theSolidListOfInstructionList->size;
for(i = 0; i < size; i++) {
pInstruction = (Instruction *) (pinterpreter->theSolidListOfInstructionList->solidlist[i]);
if(pInstruction->functionRef) {
params =
pInstruction->theSolidListOfRefList ? (ScriptVariant **) pInstruction->
theSolidListOfRefList->solidlist : NULL;
paramCount = (int) pInstruction->theRef->lVal;
// Get the pointer to the correct mapstrings function, if one exists.
pMapstrings = Script_GetStringMapFunction(pInstruction->functionRef);
if(pMapstrings) {
// Call the mapstrings function.
pMapstrings(params, paramCount);
// Find the instruction containing each constant and update its value.
for(j = 0; j < paramCount; j++) {
for(k = i; k > 0; k--) {
pInstruction2 =
(Instruction *) (pinterpreter->theSolidListOfInstructionList->
solidlist[k]);
if(pInstruction2->theVal2 == params[j]) {
ScriptVariant_Copy(pInstruction2->theVal,
pInstruction2->theVal2);
break;
}
}
if(k < 0)
return 0;
}
}
}
}
return 1;
}
// replaces the entire instruction list with a new instruction list after optimization
int Script_ReplaceInstructionList(Interpreter * pInterpreter, List * newList) {
int i, j, newSize = List_GetSize(newList);
Instruction *pInstruction, *pTarget;
Instruction **oldList = (Instruction **) pInterpreter->theSolidListOfInstructionList->solidlist;
SolidList *nl = SolidListFromList(newList);
char buf[256];
for(i = 0; i < newSize; i++) {
pInstruction = (Instruction *) nl->solidlist[i];
if(pInstruction->theJumpTargetIndex >= 0) {
pTarget = (Instruction *) oldList[pInstruction->theJumpTargetIndex];
for(j = 0; j < newSize; j++) {
if(nl->solidlist[j] == pTarget) {
pInstruction->theJumpTargetIndex = j;
break;
}
}
// if the jump target isn't found, it must have been removed in an optimization - whoops!
if(j == newSize) {
Instruction_ToString(pTarget, buf);
printf("Error: jump target %i (%s) not found - overzealous optimization!\n",
pInstruction->theJumpTargetIndex, buf);
return 0;
}
}
}
// replace new list with old list
List_Clear(&(pInterpreter->theInstructionList));
freeSolidList(pInterpreter->theSolidListOfInstructionList);
pInterpreter->theSolidListOfInstructionList = nl;
return 1;
}
// prints lots of debugging stuff about optimizations that can be made
void Script_LowerConstants(Script * pscript) {
Interpreter *pinterpreter = pscript->pinterpreter;
Instruction *pInstruction, *pInstruction2;
List *newInstructionList = malloc(sizeof(List));
int i, j, size;
List_Init(newInstructionList);
size = pinterpreter->theSolidListOfInstructionList->size;
for(i = 0; i < size; i++) {
pInstruction = (Instruction *) (pinterpreter->theSolidListOfInstructionList->solidlist[i]);
if(pInstruction->OpCode == DATA) {
int numRefs = 0;
for(j = 0; j < size; j++) {
pInstruction2 =
(Instruction *) (pinterpreter->theSolidListOfInstructionList->solidlist[j]);
if(pInstruction2->OpCode == LOAD && pInstruction2->theRef == pInstruction->theVal)
numRefs++;
}
//printf("Variable declared, %i references\n", numRefs, pInstruction->theToken->theSource);
//printf("DATA (theVal=0x%08X, theRef=0x%08X)\n", pInstruction->theVal, pInstruction->theRef);
if(numRefs > 0)
List_InsertAfter(newInstructionList, pInstruction, NULL);
else {
printf("Unused variable\n");
free(pInstruction);
}
} else
List_InsertAfter(newInstructionList, pInstruction, NULL);
#define ISCONST(x) ((x) && ((x->OpCode==CONSTINT)||(x->OpCode==CONSTSTR)||(x->OpCode==CONSTDBL)))
// Look for constant binary ops
if(pInstruction->OpCode == ADD) {
Instruction *pSrc1 = NULL, *pSrc2 = NULL;
char buf[1024], buf2[1024], buf3[1024];
for(j = 0; j < size; j++) {
Instruction *tmp =
(Instruction *) (pinterpreter->theSolidListOfInstructionList->solidlist[j]);
if(tmp->theVal == pInstruction->theRef || tmp->theVal2 == pInstruction->theRef)
pSrc1 = tmp;
if(tmp->theVal == pInstruction->theRef2 || tmp->theVal2 == pInstruction->theRef2)
pSrc2 = tmp;
}
if(ISCONST(pSrc1) && ISCONST(pSrc2)) {
ScriptVariant *sum = ScriptVariant_Add(pSrc1->theVal2, pSrc2->theVal2);
ScriptVariant_ToString(pSrc1->theVal2, buf);
ScriptVariant_ToString(pSrc2->theVal2, buf2);
ScriptVariant_ToString(sum, buf3);
//printf("ADD 0x%08X: %s + %s = %s\n", pInstruction, buf, buf2, buf3);
}
#if 0
else if(pSrc1 && pSrc2) {
Instruction_ToString(pSrc1, buf);
Instruction_ToString(pSrc2, buf2);
printf("ADD 0x%08X: %s + %s\n", pInstruction, buf, buf2);
} else
printf("ADD 0x%08X: Non-constant addition?\n");
#endif
}
#undef ISCONST
}
// replace old instruction list with optimized one (TODO: enable when this function actually does optimizations)
//Script_ReplaceInstructionList(pinterpreter, &newInstructionList);
}
// detect unused functions in scripts (to save memory)
int Script_DetectUnusedFunctions(Script * pScript) {
Interpreter *pInterpreter = pScript->pinterpreter;
Instruction *pInstruction, *pInstruction2, **instructionList =
(Instruction **) pInterpreter->theSolidListOfInstructionList->solidlist;
List newInstructionList;
int i, size = List_GetSize(&(pInterpreter->theInstructionList));
int res;
List_Init(&newInstructionList);
for(i = 0; i < size; i++) {
pInstruction = instructionList[i];
// detect function declarations (FIXME: should have an opcode for function declarations other than NOOP)
if(pInstruction->OpCode == NOOP && pInstruction->theToken
&& strlen(pInstruction->theToken->theSource) > 0) {
int j, numCalls = 0;
// find all calls to this function
for(j = 0; j < size; j++) {
pInstruction2 = instructionList[j];
if(pInstruction2->OpCode == CALL && pInstruction2->theJumpTargetIndex == i)
numCalls++;
}
if(numCalls == 0 && strcmp(pInstruction->theToken->theSource, "main") != 0) {
printf("Unused function %s()\n", pInstruction->theToken->theSource);
while(instructionList[i]->OpCode != RET) // skip function without adding to new instruction list
{
free(instructionList[i++]);
if(i >= size) {
List_Clear(&newInstructionList);
return 0;
} // this shouldn't happen!
}
free(instructionList[i]); // free the final RET instruction too
} else
List_InsertAfter(&newInstructionList, pInstruction, NULL);
} else
List_InsertAfter(&newInstructionList, pInstruction, NULL);
List_InsertAfter(&newInstructionList, pInstruction, NULL);
//if(pInstruction->theToken) {free(pInstruction->theToken); pInstruction->theToken=NULL;} // TODO: move somewhere else
}
res = Script_ReplaceInstructionList(pInterpreter, &newInstructionList);
List_Clear(&newInstructionList);
return res;
}
//should be called only once after parsing text
int Script_Compile(Script * pscript) {
int result;
if(!pscript || !pscript->pinterpreter)
return 1;
//Interpreter_OutputPCode(pscript->pinterpreter, "code");
result = SUCCEEDED(Interpreter_CompileInstructions(pscript->pinterpreter));
if(!result) {
Script_Clear(pscript, 1);
shutdown(1, "Can't compile script!\n");
}
result = Script_MapStringConstants(pscript);
if(!result) {
Script_Clear(pscript, 1);
shutdown(1, "Can't compile script!\n");
}
//result = Script_DetectUnusedFunctions(pscript);
//if(!result) {Script_Clear(pscript, 1);shutdown(1, "Script optimization failed!\n");}
//Script_LowerConstants(pscript);
return result;
}
int Script_IsInitialized(Script * pscript) {
if(pscript && pscript->initialized)
pcurrentscript = pscript; //used by local script functions
return pscript->initialized;
}
//execute the script
int Script_Execute(Script * pscript) {
int result = S_OK;
Script *temp = pcurrentscript;
pcurrentscript = pscript; //used by local script functions
Interpreter_Reset(pscript->pinterpreter);
result = (int) SUCCEEDED(Interpreter_EvaluateImmediate(pscript->pinterpreter));
pcurrentscript = temp;
if(!result)
shutdown(1, "There's an exception while executing script '%s'.\n",
pscript->pinterpreter->theSymbolTable.name);
return result;
}
#ifndef COMPILED_SCRIPT
//this method is for debug purpose
int Script_Call(Script * pscript, char *method, ScriptVariant * pretvar) {
pcurrentscript = pscript; //used by local script functions
Interpreter_Reset(pscript->pinterpreter);
return (int) SUCCEEDED(Interpreter_Call(pscript->pinterpreter, method, pretvar));
}
#endif
//used by Script_Global_Init
void Script_LoadSystemFunctions() {
//printf("Loading system script functions....");
//load system functions if we need
List_Reset(&theFunctionList);
List_InsertAfter(&theFunctionList, (void *) system_isempty, "isempty");
List_InsertAfter(&theFunctionList, (void *) system_NULL, "NULL");
List_InsertAfter(&theFunctionList, (void *) system_rand, "rand");
List_InsertAfter(&theFunctionList, (void *) system_maxglobalvarindex, "maxglobalvarindex");
List_InsertAfter(&theFunctionList, (void *) system_getglobalvar, "getglobalvar");
List_InsertAfter(&theFunctionList, (void *) system_setglobalvar, "setglobalvar");
List_InsertAfter(&theFunctionList, (void *) system_getlocalvar, "getlocalvar");
List_InsertAfter(&theFunctionList, (void *) system_setlocalvar, "setlocalvar");
List_InsertAfter(&theFunctionList, (void *) system_clearglobalvar, "clearglobalvar");
List_InsertAfter(&theFunctionList, (void *) system_clearindexedvar, "clearindexedvar");
List_InsertAfter(&theFunctionList, (void *) system_clearlocalvar, "clearlocalvar");
List_InsertAfter(&theFunctionList, (void *) system_free, "free");
List_InsertAfter(&theFunctionList, (void *) openbor_systemvariant, "openborvariant");
List_InsertAfter(&theFunctionList, (void *) openbor_changesystemvariant, "changeopenborvariant");
List_InsertAfter(&theFunctionList, (void *) openbor_drawstring, "drawstring");
List_InsertAfter(&theFunctionList, (void *) openbor_drawstringtoscreen, "drawstringtoscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_log, "log");
List_InsertAfter(&theFunctionList, (void *) openbor_drawbox, "drawbox");
List_InsertAfter(&theFunctionList, (void *) openbor_drawboxtoscreen, "drawboxtoscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_drawline, "drawline");
List_InsertAfter(&theFunctionList, (void *) openbor_drawlinetoscreen, "drawlinetoscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_drawsprite, "drawsprite");
List_InsertAfter(&theFunctionList, (void *) openbor_drawspritetoscreen, "drawspritetoscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_drawdot, "drawdot");
List_InsertAfter(&theFunctionList, (void *) openbor_drawdottoscreen, "drawdottoscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_drawscreen, "drawscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_changeplayerproperty, "changeplayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changeentityproperty, "changeentityproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_getplayerproperty, "getplayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_getentityproperty, "getentityproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_tossentity, "tossentity");
List_InsertAfter(&theFunctionList, (void *) openbor_clearspawnentry, "clearspawnentry");
List_InsertAfter(&theFunctionList, (void *) openbor_setspawnentry, "setspawnentry");
List_InsertAfter(&theFunctionList, (void *) openbor_spawn, "spawn");
List_InsertAfter(&theFunctionList, (void *) openbor_projectile, "projectile");
List_InsertAfter(&theFunctionList, (void *) openbor_transconst, "openborconstant");
List_InsertAfter(&theFunctionList, (void *) openbor_playmusic, "playmusic");
List_InsertAfter(&theFunctionList, (void *) openbor_fademusic, "fademusic");
List_InsertAfter(&theFunctionList, (void *) openbor_setmusicvolume, "setmusicvolume");
List_InsertAfter(&theFunctionList, (void *) openbor_setmusictempo, "setmusictempo");
List_InsertAfter(&theFunctionList, (void *) openbor_pausemusic, "pausemusic");
List_InsertAfter(&theFunctionList, (void *) openbor_playsample, "playsample");
List_InsertAfter(&theFunctionList, (void *) openbor_loadsample, "loadsample");
List_InsertAfter(&theFunctionList, (void *) openbor_unloadsample, "unloadsample");
List_InsertAfter(&theFunctionList, (void *) openbor_fadeout, "fadeout");
List_InsertAfter(&theFunctionList, (void *) openbor_playerkeys, "playerkeys");
List_InsertAfter(&theFunctionList, (void *) openbor_changepalette, "changepalette");
List_InsertAfter(&theFunctionList, (void *) openbor_damageentity, "damageentity");
List_InsertAfter(&theFunctionList, (void *) openbor_killentity, "killentity");
List_InsertAfter(&theFunctionList, (void *) openbor_findtarget, "findtarget");
List_InsertAfter(&theFunctionList, (void *) openbor_checkrange, "checkrange");
List_InsertAfter(&theFunctionList, (void *) openbor_gettextobjproperty, "gettextobjproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changetextobjproperty, "changetextobjproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_settextobj, "settextobj");
List_InsertAfter(&theFunctionList, (void *) openbor_cleartextobj, "cleartextobj");
List_InsertAfter(&theFunctionList, (void *) openbor_getbglayerproperty, "getbglayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changebglayerproperty, "changebglayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_getfglayerproperty, "getfglayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changefglayerproperty, "changefglayerproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_getlevelproperty, "getlevelproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changelevelproperty, "changelevelproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_checkhole, "checkhole");
List_InsertAfter(&theFunctionList, (void *) openbor_checkwall, "checkwall");
List_InsertAfter(&theFunctionList, (void *) openbor_checkplatformbelow, "checkplatformbelow");
List_InsertAfter(&theFunctionList, (void *) openbor_openfilestream, "openfilestream");
List_InsertAfter(&theFunctionList, (void *) openbor_getfilestreamline, "getfilestreamline");
List_InsertAfter(&theFunctionList, (void *) openbor_getfilestreamargument, "getfilestreamargument");
List_InsertAfter(&theFunctionList, (void *) openbor_filestreamnextline, "filestreamnextline");
List_InsertAfter(&theFunctionList, (void *) openbor_getfilestreamposition, "getfilestreamposition");
List_InsertAfter(&theFunctionList, (void *) openbor_setfilestreamposition, "setfilestreamposition");
List_InsertAfter(&theFunctionList, (void *) openbor_filestreamappend, "filestreamappend");
List_InsertAfter(&theFunctionList, (void *) openbor_createfilestream, "createfilestream");
List_InsertAfter(&theFunctionList, (void *) openbor_savefilestream, "savefilestream");
List_InsertAfter(&theFunctionList, (void *) openbor_getindexedvar, "getindexedvar");
List_InsertAfter(&theFunctionList, (void *) openbor_setindexedvar, "setindexedvar");
List_InsertAfter(&theFunctionList, (void *) openbor_getscriptvar, "getscriptvar");
List_InsertAfter(&theFunctionList, (void *) openbor_setscriptvar, "setscriptvar");
List_InsertAfter(&theFunctionList, (void *) openbor_getentityvar, "getentityvar");
List_InsertAfter(&theFunctionList, (void *) openbor_setentityvar, "setentityvar");
List_InsertAfter(&theFunctionList, (void *) openbor_jumptobranch, "jumptobranch");
List_InsertAfter(&theFunctionList, (void *) openbor_changelight, "changelight");
List_InsertAfter(&theFunctionList, (void *) openbor_changeshadowcolor, "changeshadowcolor");
List_InsertAfter(&theFunctionList, (void *) openbor_bindentity, "bindentity");
List_InsertAfter(&theFunctionList, (void *) openbor_allocscreen, "allocscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_clearscreen, "clearscreen");
List_InsertAfter(&theFunctionList, (void *) openbor_setdrawmethod, "setdrawmethod");
List_InsertAfter(&theFunctionList, (void *) openbor_updateframe, "updateframe");
List_InsertAfter(&theFunctionList, (void *) openbor_performattack, "performattack");
List_InsertAfter(&theFunctionList, (void *) openbor_setidle, "setidle");
List_InsertAfter(&theFunctionList, (void *) openbor_getentity, "getentity");
List_InsertAfter(&theFunctionList, (void *) openbor_loadmodel, "loadmodel");
List_InsertAfter(&theFunctionList, (void *) openbor_loadsprite, "loadsprite");
List_InsertAfter(&theFunctionList, (void *) openbor_playgif, "playgif");
List_InsertAfter(&theFunctionList, (void *) openbor_strinfirst, "strinfirst");
List_InsertAfter(&theFunctionList, (void *) openbor_strinlast, "strinlast");
List_InsertAfter(&theFunctionList, (void *) openbor_strleft, "strleft");
List_InsertAfter(&theFunctionList, (void *) openbor_strlength, "strlength");
List_InsertAfter(&theFunctionList, (void *) openbor_strright, "strright");
List_InsertAfter(&theFunctionList, (void *) openbor_getmodelproperty, "getmodelproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_changemodelproperty, "changemodelproperty");
List_InsertAfter(&theFunctionList, (void *) openbor_rgbcolor, "rgbcolor");
//printf("Done!\n");
}
//////////////////////////////////////////////////////////
//////////// system functions
//////////////////////////////////////////////////////////
//isempty(var);
s32 system_isempty(ScriptVariant ** varlist, ScriptVariant ** pretvar, int paramCount) {
*pretvar = NULL;
if(paramCount != 1)
return E_FAIL;
ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
(*pretvar)->lVal = (s32) ((varlist[0])->vt == VT_EMPTY);
return S_OK;
}
//NULL();
s32 system_NULL(ScriptVariant ** varlist, ScriptVariant ** pretvar, int paramCount) {
ScriptVariant_Clear(*pretvar);
return S_OK;
}
s32 system_rand(ScriptVariant ** varlist, ScriptVariant ** pretvar, int paramCount) {
ScriptVariant_ChangeType(*pretvar, VT_INTEGER);
(*pretvar)->lVal = (s32) rand32();
return S_OK;
}
//getglobalvar(varname);
s32 system_getglobalvar(ScriptVariant ** varlist, ScriptVariant ** pretvar, int paramCount) {
ScriptVariant *ptmpvar;
if(paramCount != 1) {
*pretvar = NULL;
return E_FAIL;
}
if(varlist[0]->vt != VT_STR) {
printf("Function getglobalvar must have a string parameter.\n");
*pretvar = NULL;
return E_FAIL;
}
ptmpvar = Script_Get_Global_Variant(StrCache_Get(varlist[0]->strVal));
if(ptmpvar)
ScriptVariant_Copy(*pretvar, ptmpvar);
else
ScriptVariant_ChangeType(*pretvar, VT_EMPTY);
return S_OK;
}