-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
zelda.cpp
6024 lines (5276 loc) · 168 KB
/
zelda.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
#include <memory>
#include <filesystem>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <ctype.h>
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include "base/qrs.h"
#include "base/dmap.h"
#include "base/cpool.h"
#include "base/packfile.h"
#include "base/msgstr.h"
#include "base/render.h"
#include "base/zc_alleg.h"
#include "base/misctypes.h"
#include <stdlib.h>
#include <al5_img.h>
#include <loadpng.h>
#include "zscriptversion.h"
#include "zcmusic.h"
#include "base/zdefs.h"
#include "zc/zelda.h"
#include "tiles.h"
#include "base/colors.h"
#include "pal.h"
#include "base/zsys.h"
#include "base/zapp.h"
#include "play_midi.h"
#include "qst.h"
#include "zc/matrix.h"
#include "jwin.h"
#include "base/jwinfsel.h"
#include "fontsdat.h"
#include "particles.h"
#include "gamedata.h"
#include "zc/ffscript.h"
#include "zc/jit.h"
#include "zc/script_debug.h"
#include "zc/combos.h"
#include "qst.h"
#include "base/util.h"
#include "drawing.h"
#include "dialog/alert.h"
#include "dialog/info.h"
#include "zc/replay.h"
#include "zc/cheats.h"
#include "zc/saves.h"
#include "base/zc_math.h"
#include <fmt/format.h>
#include <fmt/std.h>
#include "zc/render.h"
#include "zinfo.h"
using namespace util;
extern FFScript FFCore; //the core script engine.
extern bool epilepsyFlashReduction;
#include "zconsole/ConsoleLogger.h"
#ifndef _WIN32 //Unix
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
int32_t pt = 0;
char* ptname = NULL;
std::ostringstream lxconsole_oss;
#endif
extern ZModule zcm; //modules
extern zcmodule moduledata;
extern char runningItemScripts[256];
extern char modulepath[2048];
extern char zc_builddate[80];
extern char zc_aboutstr[80];
int32_t DMapEditorLastMaptileUsed = 0;
int32_t switch_type = 0; //Init here to avoid Linux building error in g++.
bool saved = true;
bool zqtesting_mode = false;
static std::string testingqst_name;
bool use_testingst_start = false;
static uint16_t testingqst_dmap = 0;
static uint8_t testingqst_screen = 0;
static uint8_t testingqst_retsqr = 0;
static std::string testingqst_init_data;
static bool replay_debug = false;
extern CConsoleLoggerEx zscript_coloured_console;
extern CConsoleLoggerEx coloured_console;
static zc_randgen drunk_rng;
#include "init.h"
#include <assert.h>
#include "zc/rendertarget.h"
#include "zconsole.h"
#include "base/win32.h"
#include "single_instance.h"
#include "zc/zeldadat.h"
#define LOGGAMELOOP 0
#ifdef _MSC_VER
#include <crtdbg.h>
#define stricmp _stricmp
#define getcwd _getcwd
#endif
#ifdef __EMSCRIPTEN__
#include "base/emscripten_utils.h"
#endif
// MSVC fix
#if _MSC_VER >= 1900
FILE _iob[] = { *stdin, *stdout, *stderr };
extern "C" FILE * __cdecl __iob_func(void) { return _iob; }
#endif
#if DEVLEVEL > 0
bool dev_logging = false;
bool dev_debug = false; //UNUSED
bool dev_timestmp = false;
#endif
ZCMUSIC *zcmusic = NULL;
ZCMIXER *zcmixer = NULL;
zinitdata zinit;
int32_t colordepth;
int32_t db=0;
int32_t detail_int[10]; //temporary holder for things you want to detail
int32_t lens_hint_item[MAXITEMS][2]= {{0,0},{0,0}}; //aclk, aframe
int32_t lens_hint_weapon[MAXWPNS][5] = {{0,0},{0,0}}; //aclk, aframe, dir, x, y
int32_t cheat_modifier_keys[4]; //two options each, default either control and either shift
int32_t strike_hint_counter=0;
uint8_t __isZQuest = 0; //shared functions can use this. -
int32_t strike_hint_timer=0;
int32_t strike_hint = 0;
int32_t slot_arg = 0, slot_arg2 = 0;
int32_t previous_DMap = -1;
CScriptDrawingCommands script_drawing_commands;
using std::string;
using std::pair;
int32_t zq_screen_w = 0, zq_screen_h = 0;
int32_t passive_subscreen_height=56;
int32_t original_playing_field_offset=56;
int32_t playing_field_offset=original_playing_field_offset;
int32_t passive_subscreen_offset=0;
extern int32_t directItemA;
extern int32_t directItemB;
extern int32_t directItemY;
extern int32_t directItemX;
extern int32_t hangcount;
extern bool kb_typing_mode;
bool is_compact = false;
bool standalone_mode=false;
char *standalone_quest=NULL;
std::string standalone_save_path;
bool disable_save_to_disk=false;
int32_t favorite_combos[MAXFAVORITECOMBOS] = {0};
byte favorite_combo_modes[MAXFAVORITECOMBOS] = {0};
void playLevelMusic();
//Prevent restarting during ending sequence from creating a rect clip
int32_t draw_screen_clip_rect_x1=0;
int32_t draw_screen_clip_rect_x2=255;
int32_t draw_screen_clip_rect_y1=0;
int32_t draw_screen_clip_rect_y2=223;
extern int32_t script_hero_sprite;
extern int32_t script_hero_cset;
extern int32_t script_hero_flip;
bool trip=false;
extern byte midi_suspended;
extern int32_t paused_midi_pos;
static std::atomic<bool> logic_counter;
void update_logic_counter()
{
logic_counter.store(true, std::memory_order_relaxed);
}
END_OF_FUNCTION(update_logic_counter)
bool doThrottle()
{
#ifdef ALLEGRO_MACOSX
int toggle_key = KEY_BACKQUOTE;
#else
int toggle_key = KEY_TILDE;
#endif
return (Throttlefps ^ (zc_get_system_key(toggle_key)!=0))
|| (get_qr(qr_NOFASTMODE) && !replay_is_replaying());
}
// https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/
static void preciseThrottle(double seconds)
{
static double estimate = 5e-3;
static double mean = 5e-3;
static double m2 = 0;
static int64_t count = 1;
while (seconds > estimate) {
auto start = std::chrono::high_resolution_clock::now();
rest(1);
auto end = std::chrono::high_resolution_clock::now();
double observed = (end - start).count() / 1e9;
seconds -= observed;
++count;
double delta = observed - mean;
mean += delta / count;
m2 += delta * (observed - mean);
double stddev = sqrt(m2 / (count - 1));
estimate = mean + stddev;
}
// spin lock
#ifdef __EMSCRIPTEN__
while (!logic_counter.load(std::memory_order_relaxed))
{
volatile int i = 0;
while (i < 10000000)
{
if (logic_counter.load(std::memory_order_relaxed)) return;
i += 1;
}
rest(1);
}
#else
while(!logic_counter.load(std::memory_order_relaxed));
#endif
}
void throttleFPS()
{
static auto last_time = std::chrono::high_resolution_clock::now();
if( doThrottle() || Paused)
{
if(zc_vsync == FALSE)
{
if (!logic_counter.load(std::memory_order_relaxed))
{
int freq = 60;
double target = 1.0 / freq;
auto now_time = std::chrono::high_resolution_clock::now();
double delta = (now_time - last_time).count() / 1e9;
if (delta < target)
preciseThrottle(target - delta);
}
}
else
{
vsync();
}
}
logic_counter.store(false, std::memory_order_relaxed);
last_time = std::chrono::high_resolution_clock::now();
}
int32_t onHelp()
{
// restore_mouse();
// doHelp(vc(15),vc(0));
return D_O_K;
}
int32_t d_dropdmaplist_proc(int32_t ,DIALOG *,int32_t)
{
return D_O_K;
}
static char dmap_str_buf[37];
int32_t dmap_list_size=MAXDMAPS;
bool dmap_list_zero=true;
const char *dmaplist(int32_t index, int32_t *list_size)
{
if(index>=0)
{
bound(index,0,dmap_list_size-1);
sprintf(dmap_str_buf,"%3d-%s",index+(dmap_list_zero?0:1), DMaps[index].name);
return dmap_str_buf;
}
*list_size=dmap_list_size;
return NULL;
}
int32_t startdmapxy[6] = {0,0,0,0,0,0};
/**********************************/
/******** Global Variables ********/
/**********************************/
int32_t curr_tb_page=0;
RGB_MAP rgb_table;
COLOR_MAP trans_table, trans_table2;
BITMAP *framebuf, *menu_bmp, *gui_bmp, *scrollbuf, *tmp_bmp, *tmp_scr, *screen2,
*msg_portrait_display_buf, *msg_txt_display_buf, *msg_bg_display_buf,
*pricesdisplaybuf, *tb_page[3], *temp_buf, *prim_bmp,
*script_menu_buf, *f6_menu_buf;
BITMAP *zcmouse[NUM_ZCMOUSE];
DATAFILE *datafile, *sfxdata, *fontsdata, *mididata;
size_t fontsdat_cnt = 0;
PALETTE RAMpal;
byte *colordata, *trashbuf;
//byte *tilebuf;
itemdata *itemsbuf;
wpndata *wpnsbuf;
comboclass *combo_class_buf;
guydata *guysbuf;
item_drop_object item_drop_sets[MAXITEMDROPSETS];
ZCHEATS zcheats;
byte use_cheats;
byte use_tiles;
char palnames[MAXLEVELS][PALNAMESIZE];
bool ewind_restart=false;
word msgclk = 0, msgstr = 0, enqueued_str = 0,
msgpos = 0, // screen position of the next character.
msgptr = 0, // position within the string of the next character.
msgcolour = 0, // colour to use for the displayed text.
msgspeed = 0, // delay between each character (5 = default).
msg_w = 0,
msg_h = 0,
msg_count = 0,
cursor_x = 0,
cursor_y = 0,
msg_xpos=0,
msg_ypos=0,
msgorig=0;
int16_t msg_margins[4] = {0};
byte msgstr_layer = 6;
int32_t prt_tile=0;
byte prt_cset=0, prt_x=0, prt_y=0, prt_tw=0, prt_th=0, msg_shdtype=0, msg_shdcol=0;
bool msg_onscreen = false, msg_active = false, msgspace = false;
BITMAP *msg_txt_bmp_buf = NULL, *msg_bg_bmp_buf = NULL, *msg_portrait_bmp_buf = NULL, *msg_menu_bmp_buf = NULL;
BITMAP *darkscr_bmp_curscr = NULL, *darkscr_bmp_scrollscr = NULL,
*darkscr_bmp_curscr_trans = NULL, *darkscr_bmp_scrollscr_trans = NULL;
BITMAP *lightbeam_bmp = NULL;
bool lightbeam_present;
FONT *msgfont;
word door_combo_set_count;
word introclk = 0, intropos = 0, dmapmsgclk = 0, linkedmsgclk = 0;
int16_t lensclk = 0;
int32_t lensid = 0; // Lens's item id. -1 if lens is off.
byte screengrid[22]={0};
byte screengrid_layer[2][22]={0};
byte ffcgrid[MAXFFCS/8]={0};
bool halt=false;
bool screenscrolling=false;
PALETTE tempbombpal;
bool usebombpal = false;
int32_t readsize = 0, writesize = 0;
bool fake_pack_writing=false;
SAMPLE customsfxdata[WAV_COUNT] = {0};
uint8_t customsfxflag[WAV_COUNT>>3] = {0};
int32_t sfxdat=1;
extern int32_t jwin_pal[jcMAX];
int32_t gui_colorset=99;
int32_t fullscreen = 0;
byte forceExit=0,zc_vsync=0;
byte zc_color_depth=8;
byte use_win32_proc=1, zasm_debugger = 0, zscript_debugger = 0; //windows-build configs
int32_t homescr,currscr,frame=0,currmap=0,dlevel,warpscr,worldscr,scrolling_scr=0,scrolling_map=0;
int32_t newscr_clk=0,opendoors=0,currdmap=0,fadeclk=-1,listpos=0;
int32_t lastentrance=0,lastentrance_dmap=0,prices[3]= {0},loadside = 0, Bwpn = -1, Awpn = -1, Xwpn = -1, Ywpn = -1;
int32_t digi_volume = 0,midi_volume = 0,sfx_volume = 0,emusic_volume = 0,currmidi = -1,hasitem = 0,whistleclk = 0,pan_style = 0;
bool analog_movement=true;
int32_t joystick_index=0,Akey = 0,Bkey = 0,Skey = 0,Lkey = 0,Rkey = 0,Pkey = 0,Exkey1 = 0,Exkey2 = 0,Exkey3 = 0,Exkey4 = 0,Abtn = 0,Bbtn = 0,Sbtn = 0,Mbtn = 0,Lbtn = 0,Rbtn = 0,Pbtn = 0,Exbtn1 = 0,Exbtn2 = 0,Exbtn3 = 0,Exbtn4 = 0,Quit=0;
uint32_t GameFlags=0;
int32_t js_stick_1_x_stick = 0, js_stick_1_x_axis = 0, js_stick_1_x_offset = 0;
int32_t js_stick_1_y_stick = 0, js_stick_1_y_axis = 0, js_stick_1_y_offset = 0;
int32_t js_stick_2_x_stick = 0, js_stick_2_x_axis = 0, js_stick_2_x_offset = 0;
int32_t js_stick_2_y_stick = 0, js_stick_2_y_axis = 0, js_stick_2_y_offset = 0;
int32_t DUkey = 0, DDkey = 0, DLkey = 0, DRkey = 0, DUbtn = 0, DDbtn = 0, DLbtn = 0, DRbtn = 0, ss_after = 0, ss_speed = 0, ss_density = 0, ss_enable = 0;
int32_t hs_startx = 0, hs_starty = 0, hs_xdist = 0, hs_ydist = 0, clockclk = 0, clock_zoras[eMAXGUYS]={0};
int32_t cheat_goto_dmap=0, cheat_goto_screen=0, currcset = 0, currspal6 = -1, currspal14 = -1;
int32_t gfc = 0, gfc2 = 0, pitx = 0, pity = 0, refill_what = 0, refill_why = 0, heart_beep_timer=0, new_enemy_tile_start=1580;
int32_t nets=1580, magicitem=-1,div_prot_item=-1, magiccastclk = 0, quakeclk=0, wavy=0, castx = 0, casty = 0, df_x = 0, df_y = 0, nl1_x = 0, nl1_y = 0, nl2_x = 0, nl2_y = 0;
int32_t magicdrainclk=0, conveyclk=3, memrequested=0;
byte newconveyorclk = 0;
float avgfps=0;
dword fps_secs=0;
bool cheats_execute_goto=false, cheats_execute_light=false;
int32_t checkx = 0, checky = 0;
int32_t loadlast=0;
int32_t skipcont=0;
bool monochrome = false; //GFX are monochrome.
bool palette_user_tinted = false;
bool show_layer_0=true, show_layer_1=true, show_layer_2=true, show_layer_3=true, show_layer_4=true, show_layer_5=true, show_layer_6=true,
//oveheard combos //pushblocks
show_layer_over=true, show_layer_push=true, show_sprites=true, show_ffcs=true, show_hitboxes=false, show_walkflags=false, show_ff_scripts=false, show_effectflags = false;
bool Throttlefps = true, MenuOpen = false, ClickToFreeze=false, Paused=false, Saving=false,
Advance=false, ShowFPS = true, Showpal=false, disableClickToFreeze=false, SaveDragResize=false,
DragAspect=false, SaveWinPos=false, scaleForceInteger=false, stretchGame=false;
double aspect_ratio = 0.75;
int window_min_width = 320, window_min_height = 240;
bool Playing, FrameSkip=false, TransLayers = true,clearConsoleOnLoad = true,clearConsoleOnReload = true;
bool GameLoaded = false;
bool __debug=false,debug_enabled = false;
bool refreshpal,blockpath = false,loaded_guys= false,freeze_guys= false,
loaded_enemies= false,drawguys= false,details=false,watch= false;
bool darkroom=false,naturaldark=false,BSZ= false; //,NEWSUBSCR;
bool down_control_states[controls::btnLast] = {false};
bool F12= false,F11= false, F5= false,keyI= false, keyQ= false,
SystemKeys=true,NESquit= false,volkeys= false,useCD=false,boughtsomething=false,
fixed_door=false, hookshot_used=false, hookshot_frozen=false,
pull_hero=false, hs_fix=false, hs_switcher=false,
cheat_superman=false, gofast=false, checkhero=true, didpit=false, heart_beep=true,
pausenow=false, castnext=false, add_df1asparkle= false, add_df1bsparkle= false, add_nl1asparkle= false, add_nl1bsparkle= false, add_nl2asparkle= false, add_nl2bsparkle= false,
is_on_conveyor= false, activated_timed_warp=false;
int32_t hooked_combopos = -1, switchhook_cost_item = -1;
int32_t is_conveyor_stunned = 0;
uint16_t hooked_layerbits = 0;
int32_t hooked_undercombos[14] = {0};
solid_object* switching_object = NULL;
byte COOLSCROLL = 0;
int32_t add_asparkle=0, add_bsparkle=0;
int32_t SnapshotFormat, NameEntryMode=0;
char zeldadat_sig[52]={0};
char sfxdat_sig[52]={0};
char fontsdat_sig[52]={0};
char cheat_goto_dmap_str[4]={0};
char cheat_goto_screen_str[3]={0};
int16_t visited[6]={0};
byte guygrid[176]={0};
byte guygridffc[MAXFFCS]={0};
mapscr tmpscr[2];
mapscr tmpscr2[6];
mapscr tmpscr3[6];
gamedata *game=NULL;
script_data *ffscripts[NUMSCRIPTFFC];
script_data *itemscripts[NUMSCRIPTITEM];
script_data *globalscripts[NUMSCRIPTGLOBAL];
script_data *genericscripts[NUMSCRIPTSGENERIC];
script_data *guyscripts[NUMSCRIPTGUYS];
script_data *wpnscripts[NUMSCRIPTWEAPONS];
script_data *lwpnscripts[NUMSCRIPTWEAPONS];
script_data *ewpnscripts[NUMSCRIPTWEAPONS];
script_data *playerscripts[NUMSCRIPTPLAYER];
script_data *screenscripts[NUMSCRIPTSCREEN];
script_data *dmapscripts[NUMSCRIPTSDMAP];
script_data *itemspritescripts[NUMSCRIPTSITEMSPRITE];
script_data *comboscripts[NUMSCRIPTSCOMBODATA];
script_data *subscreenscripts[NUMSCRIPTSSUBSCREEN];
ScriptOwner::ScriptOwner() : scriptType(ScriptType::None), ownerUID(0),
specOwned(false), specCleared(false)
{}
void ScriptOwner::clear()
{
scriptType = ScriptType::None;
ownerUID = 0;
specOwned = false;
specCleared = false;
}
//ZScript array storage
std::vector<ZScriptArray> globalRAM;
ZScriptArray localRAM[NUM_ZSCRIPT_ARRAYS];
std::map<int32_t,ZScriptArray> objectRAM;
ScriptOwner arrayOwner[NUM_ZSCRIPT_ARRAYS];
//script bitmap drawing
ZScriptDrawingRenderTarget* zscriptDrawingRenderTarget;
DebugConsole DebugConsole::singleton = DebugConsole();
ZASMSTackTrace ZASMSTackTrace::singleton = ZASMSTackTrace();
void setZScriptVersion(int32_t s_version)
{
ZScriptVersion::setVersion(s_version);
}
void initZScriptArrayRAM(bool firstplay)
{
for(word i = 0; i < NUM_ZSCRIPT_ARRAYS; i++)
{
localRAM[i].Clear();
arrayOwner[i].clear();
}
if(game->globalRAM.size() != 0)
game->globalRAM.clear();
if(firstplay)
{
//leave to global script ~Init to allocate global memory first time round
game->globalRAM.resize(getNumGlobalArrays());
}
else
{
//allocate from save file
game->globalRAM.resize(saves_get_current_slot()->game->globalRAM.size());
for(dword i = 0; i < game->globalRAM.size(); i++)
{
#ifdef _DEBUGARRAYALLOC
al_trace("Global Array: %i\n",i);
#endif
const ZScriptArray &from = saves_get_current_slot()->game->globalRAM[i];
ZScriptArray &to = game->globalRAM[i];
to.Resize(from.Size());
to.setValid(from.Valid());
for(dword j = 0; j < from.Size(); j++)
{
#ifdef _DEBUGARRAYALLOC
al_trace("Element: %i\nInit: %i, From save file: %i\n", j, to[j], from[j]);
#endif
to[j] = from[j];
}
}
}
}
void initZScriptGlobalRAM()
{
for (int32_t q = 0; q < NUMSCRIPTGLOBAL; ++q)
{
FFCore.reset_script_engine_data(ScriptType::Global, q);
}
}
void initZScriptGlobalScript(int32_t ID)
{
FFCore.reset_script_engine_data(ScriptType::Global, ID);
}
dword getNumGlobalArrays()
{
word scommand, pc = 0, ret = 0;
do
{
scommand = globalscripts[GLOBAL_SCRIPT_INIT]->zasm[pc].command;
if(scommand == ALLOCATEGMEMV || scommand == ALLOCATEGMEMR)
ret++;
pc++;
}
while(scommand != 0xFFFF);
return ret;
}
//movingblock mblock2; //mblock[4]?
//HeroClass Hero;
int32_t resx= 0,resy= 0;
// the number of horizontal or vertical pixels between the framebuffer (320x240) and the window.
// aka, the letterbox size.
int32_t scrx= 0,scry= 0;
int32_t window_width = 0, window_height = 0;
extern byte pause_in_background;
extern signed char pause_in_background_menu_init;
bool toogam=false;
bool ignoreSideview=false;
int32_t script_mouse_x;
int32_t script_mouse_y;
int32_t script_mouse_z;
int32_t script_mouse_b;
int32_t cheat = (DEVLEVEL > 1) ? 4 : 0; // 0 = none; 1,2,3,4 = cheat level
int32_t maxcheat = (DEVLEVEL > 1) ? 4 : 0;
int32_t mouse_down=0; // used to hold the last reading of 'gui_mouse_b()' status
int32_t idle_count=0, active_count=0;
// quest file data
zquestheader QHeader;
byte midi_flags[MIDIFLAGS_SIZE];
byte music_flags[MUSICFLAGS_SIZE];
int32_t msg_strings_size=0;
zcmap *ZCMaps;
byte *quest_file;
dword quest_map_pos[MAPSCRS*MAXMAPS2]={0};
char *qstpath=NULL;
char *qstdir=NULL;
// if set, the titlescreen will automatically create a new save with this quest.
std::string load_qstpath;
// if set, no file select is possible for the title screen, and only this quest apply to new saves.
std::string only_qstpath;
char header_version_nul_term[17];
volatile int32_t lastfps=0;
volatile int32_t framecnt=0;
volatile int32_t myvsync=0;
bool update_hw_pal = false;
PALETTE* hw_palette = NULL;
void update_hw_screen(bool force)
{
if (is_headless())
return;
#ifdef __EMSCRIPTEN__
force = true;
#endif
if(force || (!is_sys_pal && !doThrottle()) || myvsync)
{
zc_process_display_events();
resx = al_get_display_width(all_get_display());
resy = al_get_display_height(all_get_display());
if(update_hw_pal && hw_palette)
{
zc_set_palette(*hw_palette);
update_hw_pal = false;
}
framecnt++;
if (myvsync||force)
render_zc();
myvsync=0;
}
}
void myvsync_callback()
{
++myvsync;
}
END_OF_FUNCTION(myvsync_callback)
/*
enum { SAVESC_BACKGROUND, SAVESC_TEXT, SAVESC_USETILE,
SAVESC_CURSOR_CSET, SAVESC_CUR_SOUND, SAVESC_TEXT_CONTINUE_COLOUR,
SAVESC_TEXT_SAVE_COLOUR, SAVESC_TEXT_RETRY_COLOUR, SAVESC_TEXT_CONTINUE_FLASH,
SAVESC_TEXT_SAVE_FLASH, SAVESC_TEXT_RETRY_FLASH, SAVESC_MIDI,
SAVESC_CUR_FLIP, SAVESC_TEXT_DONTSAVE_COLOUR, SAVESC_TEXT_SAVEQUIT_COLOUR,
SAVESC_TEXT_SAVE2_COLOUR, SAVESC_TEXT_QUIT_COLOUR, SAVESC_TEXT_DONTSAVE_FLASH,
SAVESC_TEXT_SAVEQUIT_FLASH, SAVESC_TEXT_SAVE2_FLASH, SAVESC_TEXT_QUIT_FLASH,
SAVESC_EXTRA1, SAVESC_EXTRA2, SAVESC_EXTRA3,
SAVESC_LAST }; */
#define SAVESC_DEF_TILE 2
int32_t SaveScreenSettings[24] = {
BLACK, WHITE, SAVESC_DEF_TILE,
1, WAV_CHINK, QMisc.colors.msgtext,
QMisc.colors.msgtext, QMisc.colors.msgtext, QMisc.colors.caption,
QMisc.colors.caption, QMisc.colors.caption, -4,
0, QMisc.colors.msgtext, QMisc.colors.msgtext,
QMisc.colors.msgtext, QMisc.colors.msgtext, QMisc.colors.caption,
QMisc.colors.caption, QMisc.colors.caption, QMisc.colors.caption,
0, 0, 0 }; //BG, Text, Cursor CSet, Sound, UseTile, Misc
char SaveScreenText[7][32]={"CONTINUE", "SAVE", "RETRY", "DON'T SAVE", "SAVE AND QUIT", "SAVE", "QUIT" };
void ResetSaveScreenSettings()
{
SaveScreenSettings[SAVESC_TEXT_CONTINUE_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_SAVE_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_RETRY_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_CONTINUE_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_TEXT_SAVE_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_TEXT_RETRY_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_MIDI] = -4;
//SaveScreenSettings[SAVESC_BACKGROUND] = BLACK;
SaveScreenSettings[SAVESC_BACKGROUND] = 0; //Isle of Rebirth changed the game over background by changing color 0 of the palette; this needs to be respected!
SaveScreenSettings[SAVESC_TEXT] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_USETILE] = SAVESC_DEF_TILE;
SaveScreenSettings[SAVESC_CURSOR_CSET] = 1;
SaveScreenSettings[SAVESC_CUR_SOUND] = WAV_CHINK;
SaveScreenSettings[SAVESC_CUR_FLIP] = 0;
SaveScreenSettings[SAVESC_TEXT_DONTSAVE_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_SAVEQUIT_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_SAVE2_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_QUIT_COLOUR] = QMisc.colors.msgtext;
SaveScreenSettings[SAVESC_TEXT_DONTSAVE_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_TEXT_SAVEQUIT_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_TEXT_SAVE2_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_TEXT_QUIT_FLASH] = QMisc.colors.caption;
SaveScreenSettings[SAVESC_EXTRA1] = 0;
SaveScreenSettings[SAVESC_EXTRA2] = 0;
SaveScreenSettings[SAVESC_EXTRA3] = 0;
}
void SetSaveScreenSetting(int32_t indx, int32_t value)
{
switch(indx)
{
case SAVESC_BACKGROUND:
SaveScreenSettings[SAVESC_BACKGROUND] = vbound(value,0,255);
break;
case SAVESC_TEXT:
SaveScreenSettings[SAVESC_TEXT] = vbound(value,0,255);
break;
case SAVESC_USETILE:
SaveScreenSettings[SAVESC_USETILE] = vbound(value,0,NEWMAXTILES);
break;
case SAVESC_CURSOR_CSET:
SaveScreenSettings[SAVESC_CURSOR_CSET] = vbound(value,0,14);
break;
case SAVESC_CUR_SOUND:
SaveScreenSettings[SAVESC_CUR_SOUND] = vbound(value,0,255); //MAX_SOUNDS
break;
case SAVESC_TEXT_CONTINUE_COLOUR:
SaveScreenSettings[SAVESC_TEXT_CONTINUE_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVE_COLOUR:
SaveScreenSettings[SAVESC_TEXT_SAVE_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_RETRY_COLOUR:
SaveScreenSettings[SAVESC_TEXT_RETRY_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_CONTINUE_FLASH:
SaveScreenSettings[SAVESC_TEXT_CONTINUE_FLASH] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVE_FLASH:
SaveScreenSettings[SAVESC_TEXT_SAVE_FLASH] = vbound(value,0,255);
break;
case SAVESC_TEXT_RETRY_FLASH:
SaveScreenSettings[SAVESC_TEXT_RETRY_FLASH] = vbound(value,0,255);
break;
case SAVESC_MIDI:
SaveScreenSettings[SAVESC_MIDI] = vbound(value,0,255);
break;
case SAVESC_CUR_FLIP:
SaveScreenSettings[SAVESC_CUR_FLIP] = vbound(value,0,3);
break;
case SAVESC_TEXT_DONTSAVE_COLOUR:
SaveScreenSettings[SAVESC_TEXT_DONTSAVE_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVEQUIT_COLOUR:
SaveScreenSettings[SAVESC_TEXT_SAVEQUIT_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVE2_COLOUR:
SaveScreenSettings[SAVESC_TEXT_SAVE2_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_QUIT_COLOUR:
SaveScreenSettings[SAVESC_TEXT_QUIT_COLOUR] = vbound(value,0,255);
break;
case SAVESC_TEXT_DONTSAVE_FLASH:
SaveScreenSettings[SAVESC_TEXT_DONTSAVE_FLASH] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVEQUIT_FLASH:
SaveScreenSettings[SAVESC_TEXT_SAVEQUIT_FLASH] = vbound(value,0,255);
break;
case SAVESC_TEXT_SAVE2_FLASH:
SaveScreenSettings[SAVESC_TEXT_SAVE2_FLASH] = vbound(value,0,255);
break;
case SAVESC_TEXT_QUIT_FLASH:
SaveScreenSettings[SAVESC_TEXT_QUIT_FLASH] = vbound(value,0,255);
break;
default: break;
}
}
void ChangeSubscreenText(int32_t index, const char *f)
{
index = vbound(index, 0, SAVESC_END-1);
strncpy(SaveScreenText[index], f, 31);
SaveScreenText[index][31]='\0';
}
/**********************************/
/*********** Misc Data ************/
/**********************************/
const char startguy[8] = {-13,-13,-13,-14,-15,-18,-21,-40};
const char gambledat[12*6] =
{
20,-10,-10, 20,-10,-10, 20,-40,-10, 20,-10,-40,
50,-10,-10, 50,-10,-10, 50,-40,-10, 50,-10,-40,
-10,20,-10, -10,20,-10, -40,20,-10, -10,20,-40,
-10,50,-10, -10,50,-10, -40,50,-10, -10,50,-40,
-10,-10,20, -10,-10,20, -10,-40,20, -40,-10,20,
-10,-10,50, -10,-10,50, -10,-40,50, -40,-10,50
};
const byte stx[4][9] =
{
{ 48, 80, 80, 96,112,144,160,160,192},
{ 48, 80, 80, 96,128,144,160,160,192},
{ 80, 80,128,128,160,160,192,192,208},
{ 32, 48, 48, 80, 80,112,112,160,160}
};
const byte sty[4][9] =
{
{112, 64,128, 96, 80, 96, 64,128,112},
{ 48, 32, 96, 64, 80, 64, 32, 96, 48},
{ 32,128, 64, 96, 64, 96, 48,112, 80},
{ 80, 48,112, 64, 96, 64, 96, 32,128}
};
const byte ten_rupies_x[10] = {120,112,128,96,112,128,144,112,128,120};
const byte ten_rupies_y[10] = {49,65,65,81,81,81,81,97,97,113};
zctune tunes[MAXMIDIS] =
{
// (title) (s) (ls) (le) (l) (vol) (midi)(fmt)
zctune((char *)"Zelda - Dungeon", 0, -1, -1, 1, 176, NULL, 0),
zctune((char *)"Zelda - Ending", 0, 129, 225, 1, 160, NULL, 0),
zctune((char *)"Zelda - Game Over", 0, -1, -1, 1, 224, NULL, 0),
zctune((char *)"Zelda - Level 9", 0, -1, -1, 1, 255, NULL, 0),
zctune((char *)"Zelda - Overworld", 0, 17, -1, 1, 208, NULL, 0),
zctune((char *)"Zelda - Title", 0, -1, -1, 0, 168, NULL, 0),
zctune((char *)"Zelda - Triforce", 0, -1, -1, 0, 168, NULL, 0)
};
// emusic enhancedMusic[MAXMUSIC];
FONT *setmsgfont()
{
return get_zc_font(MsgStrings[msgstr].font);
}
void zc_trans_blit(BITMAP* dest, BITMAP* src, int32_t sx, int32_t sy, int32_t dx, int32_t dy, int32_t w, int32_t h)
{
for(int32_t tx = 0; tx < w; ++tx)
for(int32_t ty = 0; ty < h; ++ty)
{
int32_t c1 = src->line[sy+ty][sx+tx];
int32_t c2 = dest->line[dy+ty][dx+tx];
if(c1)
{
dest->line[dy+ty][dx+tx] = trans_table.data[c1][c2];
}
}
}
void msg_bg(MsgStr const& msg)
{
if(msg.tile == 0) return;
if(msg.stringflags & STRINGFLAG_FULLTILE)
{
draw_block_flip(msg_bg_bmp_buf,0,0,msg.tile,msg.cset,
(int32_t)ceil(msg.w/16.0),(int32_t)ceil(msg.h/16.0),0,true,false);
}
else
{
int32_t add = (get_qr(qr_STRING_FRAME_OLD_WIDTH_HEIGHT)!=0 ? 2 : 0);
frame2x2(msg_bg_bmp_buf,0,0,msg.tile,msg.cset,
(msg.w>>3)+add,(msg.h>>3)+add,0,true,0);
}
}
void msg_prt()
{
clear_bitmap(msg_portrait_bmp_buf);
if(prt_tile > 0 && prt_th > 0 && prt_tw > 0)
{
draw_block_flip(msg_portrait_bmp_buf,0,0,prt_tile,prt_cset,
prt_tw, prt_th, 0, true, false);
}
}
void blit_msgstr_bg(BITMAP* dest, int32_t sx, int32_t sy, int32_t dx, int32_t dy, int32_t w, int32_t h)
{
if(MsgStrings[msgstr].stringflags & STRINGFLAG_TRANS_BG)
{
BITMAP* subbmp = create_bitmap_ex(8,w,h);
if(subbmp)
{
color_map = &trans_table2;
clear_bitmap(subbmp);
masked_blit(msg_bg_display_buf, subbmp, sx, sy, 0, 0, w, h);
draw_trans_sprite(dest, subbmp, dx, dy);
destroy_bitmap(subbmp);
color_map = &trans_table;
}
//zc_trans_blit(dest, msg_bg_display_buf, sx, sy, dx, dy, w, h);
}
else
{
masked_blit(msg_bg_display_buf, dest, sx, sy, dx, dy, w, h);
}
}
void blit_msgstr_fg(BITMAP* dest, int32_t sx, int32_t sy, int32_t dx, int32_t dy, int32_t w, int32_t h)
{
if(MsgStrings[msgstr].stringflags & STRINGFLAG_TRANS_FG)
{
BITMAP* subbmp = create_bitmap_ex(8,w,h);
if(subbmp)
{
color_map = &trans_table2;
clear_bitmap(subbmp);
masked_blit(msg_txt_display_buf, subbmp, sx, sy, 0, 0, w, h);
draw_trans_sprite(dest, subbmp, dx, dy);
destroy_bitmap(subbmp);
color_map = &trans_table;
}
//zc_trans_blit(dest, msg_txt_display_buf, sx, sy, dx, dy, w, h);
}
else
{
masked_blit(msg_txt_display_buf, dest, sx, sy, dx, dy, w, h);
}
}
void blit_msgstr_prt(BITMAP* dest, int32_t sx, int32_t sy, int32_t dx, int32_t dy, int32_t w, int32_t h)
{
masked_blit(msg_portrait_display_buf, dest, sx, sy, dx, dy, w, h);
}
void clearmsgnext(int32_t str)
{
MsgStrings[str].nextstring = 0;
}
void clr_msg_data();
void donewmsg(int32_t str)
{
if(msg_onscreen || msg_active)
dismissmsg();
clr_msg_data();
//al_trace("donewmsg %d\n",str);
linkedmsgclk=0;
msg_active = true;
// Don't set msg_onscreen - not onscreen just yet
msgstr = str;
msgorig = msgstr;
msgfont = setmsgfont();
msgcolour=QMisc.colors.msgtext;
msgspeed=zinit.msg_speed;
msgstr_layer=MsgStrings[msgstr].drawlayer;
if(introclk==0 || (introclk>=72 && dmapmsgclk==0))
{
clear_bitmap(msg_bg_display_buf);
clear_bitmap(msg_txt_display_buf);
}
clear_bitmap(msg_bg_display_buf);
set_clip_state(msg_bg_display_buf, 1);
clear_bitmap(msg_portrait_display_buf);
set_clip_state(msg_portrait_display_buf, 1);
clear_bitmap(msg_txt_display_buf);
set_clip_state(msg_txt_display_buf, 1);
clear_bitmap(msg_txt_bmp_buf);
clear_bitmap(msg_menu_bmp_buf);
clear_bitmap(msg_bg_bmp_buf);
clear_bitmap(msg_portrait_bmp_buf);
msgclk=msgpos=msgptr=0;
msgspace=true;
msg_w=MsgStrings[msgstr].w;
msg_h=MsgStrings[msgstr].h;
msg_xpos=MsgStrings[msgstr].x;
msg_ypos=MsgStrings[msgstr].y;
prt_tile=MsgStrings[msgstr].portrait_tile;
prt_cset=MsgStrings[msgstr].portrait_cset;
prt_x=MsgStrings[msgstr].portrait_x;
prt_y=MsgStrings[msgstr].portrait_y;
prt_tw=MsgStrings[msgstr].portrait_tw;
prt_th=MsgStrings[msgstr].portrait_th;
msg_shdtype=MsgStrings[msgstr].shadow_type;
msg_shdcol=MsgStrings[msgstr].shadow_color;
msg_bg(MsgStrings[msgstr]);
msg_prt();
int16_t old_margins[4] = {8,0,8,-8};
int16_t const* copy_from = get_qr(qr_OLD_STRING_EDITOR_MARGINS) ? old_margins : MsgStrings[msgstr].margins;
for(auto q = 0; q < 4; ++q)
msg_margins[q] = copy_from[q];
cursor_x=msg_margins[left];
cursor_y=msg_margins[up];
}
// Called to make a message disappear
void dismissmsg()
{
linkedmsgclk=0;
msgstr = msgclk=msgpos=msgptr=0;
cursor_x=0;