forked from NagyD/SDLPoP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg000.c
1786 lines (1654 loc) · 48.2 KB
/
seg000.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
/*
SDLPoP, a port/conversion of the DOS game Prince of Persia.
Copyright (C) 2013-2015 Dávid Nagy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The authors of this program may be contacted at http://forum.princed.org
*/
#include "common.h"
#include <fcntl.h>
#include <setjmp.h>
// data:009C
word cheats_enabled = 0;
// data:461E
dat_type * dathandle;
// data:4CE2
word need_full_redraw;
// data:4C08
word need_redraw_because_flipped;
// seg000:0000
void far pop_main() {
// debug only: check that the sequence table deobfuscation did not mess things up
#ifdef CHECK_SEQTABLE_MATCHES_ORIGINAL
check_seqtable_matches_original();
#endif
load_options();
apply_seqtbl_patches();
char sprintf_temp[100];
int i;
dathandle = open_dat("PRINCE.DAT", 0);
/*video_mode =*/ parse_grmode();
init_timer(60);
parse_cmdline_sound();
set_hc_pal();
current_target_surface = rect_sthg(onscreen_surface_, &screen_rect);
show_loading();
set_joy_mode();
cheats_enabled = check_param("megahit") != NULL;
#ifdef __DEBUG__
cheats_enabled = 1; // debug
#endif
draw_mode = check_param("draw") != NULL && cheats_enabled;
demo_mode = check_param("demo") != NULL;
init_copyprot_dialog();
#ifdef USE_REPLAY
init_record_replay();
#endif
if (cheats_enabled
#ifdef USE_REPLAY
|| recording
#endif
) {
for (i = 14; i >= 0; --i) {
snprintf(sprintf_temp, sizeof(sprintf_temp), "%d", i);
if (check_param(sprintf_temp)) {
start_level = i;
break;
}
}
}
init_game_main();
}
byte* level_var_palettes;
// seg000:024F
void __pascal far init_game_main() {
doorlink1_ad = /*&*/level.doorlinks1;
doorlink2_ad = /*&*/level.doorlinks2;
prandom(1);
if (graphics_mode == gmMcgaVga) {
// Guard palettes
guard_palettes = (byte*) load_from_opendats_alloc(10, "bin", NULL, NULL);
// (blood, hurt flash) #E00030 = red
set_pal(12, 0x38, 0x00, 0x0C, 1);
// (palace wall pattern) #C09850 = light brown
set_pal( 6, 0x30, 0x26, 0x14, 0);
// Level color variations (1.3)
level_var_palettes = load_from_opendats_alloc(20, "bin", NULL, NULL);
}
// PRINCE.DAT: sword
chtab_addrs[id_chtab_0_sword] = load_sprites_from_file(700, 1<<2, 1);
// PRINCE.DAT: flame, sword on floor, potion
chtab_addrs[id_chtab_1_flameswordpotion] = load_sprites_from_file(150, 1<<3, 1);
close_dat(dathandle);
load_sounds(0, 43);
load_opt_sounds(43, 56); //added
hof_read();
show_use_fixes_and_enhancements_prompt(); // added
start_game();
}
// data:02C2
word first_start = 1;
// data:4C38
jmp_buf setjmp_buf;
// seg000:0358
void __pascal far start_game() {
#ifdef USE_COPYPROT
word which_entry;
word pos;
word entry_used[40];
byte letts_used[26];
#endif
screen_updates_suspended = 0;
// Prevent filling of stack.
// start_game is called from many places to restart the game, for example:
// process_key, play_frame, draw_game_frame, play_level, control_kid, end_sequence, expired
if (first_start) {
first_start = 0;
setjmp(/*&*/setjmp_buf);
} else {
draw_rect(&screen_rect, 0);
show_quotes();
clear_screen_and_sounds();
longjmp(/*&*/setjmp_buf,-1);
}
release_title_images(); // added
free_optsnd_chtab(); // added
#ifdef USE_COPYPROT
copyprot_plac = prandom(13);
memset(&entry_used, 0, sizeof(entry_used));
memset(&letts_used, 0, sizeof(letts_used));
for (pos = 0; pos < 14; ++pos) {
do {
if (pos == copyprot_plac) {
which_entry = copyprot_idx = prandom(39);
} else {
which_entry = prandom(39);
}
} while (entry_used[which_entry] || letts_used[copyprot_letter[which_entry]-'A']);
cplevel_entr[pos] = which_entry;
entry_used[which_entry] = 1;
letts_used[copyprot_letter[which_entry]-'A'] = 1;
}
#endif
if (start_level == 0) {
show_title();
} else {
init_game(start_level);
}
}
#ifdef USE_QUICKSAVE
// All these functions return true on success, false otherwise.
FILE* quick_fp;
int process_save(void* data, size_t data_size) {
return fwrite(data, data_size, 1, quick_fp) == 1;
}
int process_load(void* data, size_t data_size) {
return fread(data, data_size, 1, quick_fp) == 1;
}
typedef int process_func_type(void* data, size_t data_size);
int quick_process(process_func_type process_func) {
int ok = 1;
#define process(x) ok = ok && process_func(&(x), sizeof(x))
// level
process(level);
process(checkpoint);
process(upside_down);
process(drawn_room);
process(current_level);
process(next_level);
process(mobs_count);
process(mobs);
process(trobs_count);
process(trobs);
process(leveldoor_open);
//process(exit_room_timer);
// kid
process(Kid);
process(hitp_curr);
process(hitp_max);
process(hitp_beg_lev);
process(grab_timer);
process(holding_sword);
process(united_with_shadow);
process(have_sword);
/*process(ctrl1_forward);
process(ctrl1_backward);
process(ctrl1_up);
process(ctrl1_down);
process(ctrl1_shift2);*/
process(kid_sword_strike);
process(pickup_obj_type);
process(offguard);
// guard
process(Guard);
process(Char);
process(Opp);
process(guardhp_curr);
process(guardhp_max);
process(demo_index);
process(demo_time);
process(curr_guard_color);
process(guard_notice_timer);
process(guard_skill);
process(shadow_initialized);
process(guard_refrac);
process(justblocked);
process(droppedout);
// collision
process(curr_row_coll_room);
process(curr_row_coll_flags);
process(below_row_coll_room);
process(below_row_coll_flags);
process(above_row_coll_room);
process(above_row_coll_flags);
process(prev_collision_row);
// flash
process(flash_color);
process(flash_time);
// sounds
process(need_level1_music);
process(is_screaming);
process(is_feather_fall);
process(last_loose_sound);
//process(next_sound);
//process(current_sound);
// random
process(random_seed);
// remaining time
process(rem_min);
process(rem_tick);
// saved controls
process(control_x);
process(control_y);
process(control_shift);
process(control_forward);
process(control_backward);
process(control_up);
process(control_down);
process(control_shift2);
process(ctrl1_forward);
process(ctrl1_backward);
process(ctrl1_up);
process(ctrl1_down);
process(ctrl1_shift2);
#undef process
return ok;
}
const char* quick_file = "QUICKSAVE.SAV";
const char quick_version[] = "V1.16b4 ";
char quick_control[] = "........";
int quick_save() {
int ok = 0;
quick_fp = fopen(quick_file, "wb");
if (quick_fp != NULL) {
process_save((void*) quick_version, COUNT(quick_version));
ok = quick_process(process_save);
fclose(quick_fp);
quick_fp = NULL;
}
return ok;
}
void restore_room_after_quick_load() {
int temp1 = curr_guard_color;
int temp2 = next_level;
reset_level_unused_fields(false);
load_lev_spr(current_level);
curr_guard_color = temp1;
next_level = temp2;
//need_full_redraw = 1;
different_room = 1;
next_room = drawn_room;
load_room_links();
//draw_level_first();
//gen_palace_wall_colors();
draw_game_frame(); // for falling
//redraw_screen(1); // for room_L
hitp_delta = guardhp_delta = 1; // force HP redraw
draw_hp();
loadkid_and_opp();
// Get rid of "press button" message if kid was dead before quickload.
text_time_total = text_time_remaining = 0;
//next_sound = current_sound = -1;
exit_room_timer = 0;
}
int quick_load() {
int ok = 0;
quick_fp = fopen(quick_file, "rb");
if (quick_fp != NULL) {
// check quicksave version is compatible
process_load(quick_control, COUNT(quick_control));
if (strcmp(quick_control, quick_version) != 0) {
fclose(quick_fp);
quick_fp = NULL;
return 0;
}
stop_sounds();
start_timer(timer_0, 5); // briefly display a black screen as a visual cue
draw_rect(&screen_rect, 0);
screen_updates_suspended = 0;
request_screen_update();
screen_updates_suspended = 1;
word old_rem_min = rem_min;
word old_rem_tick = rem_tick;
ok = quick_process(process_load);
fclose(quick_fp);
quick_fp = NULL;
restore_room_after_quick_load();
do_wait(timer_0);
screen_updates_suspended = 0;
request_screen_update();
#ifdef USE_QUICKLOAD_PENALTY
// Subtract one minute from the remaining time (if it is above 5 minutes)
if (options.enable_quicksave_penalty) {
int ticks_elapsed = 720 * (rem_min - old_rem_min) + (rem_tick - old_rem_tick);
// don't restore time at all if the elapsed time is between 0 and 1 minutes
if (ticks_elapsed > 0 && ticks_elapsed < 720) {
rem_min = old_rem_min;
rem_tick = old_rem_tick;
}
else {
if (rem_min == 6) rem_tick = 719; // crop to "5 minutes" exactly, if hitting the threshold in <1 minute
if (rem_min > 5) --rem_min;
}
}
#endif
}
return ok;
}
int need_quick_save = 0;
int need_quick_load = 0;
void check_quick_op() {
if (!options.enable_quicksave) return;
if (need_quick_save) {
if (!is_feather_fall && quick_save()) {
display_text_bottom("QUICKSAVE");
} else {
display_text_bottom("NO QUICKSAVE");
}
need_quick_save = 0;
text_time_total = 24;
text_time_remaining = 24;
}
if (need_quick_load) {
if (quick_load()) {
display_text_bottom("QUICKLOAD");
} else {
display_text_bottom("NO QUICKLOAD");
}
need_quick_load = 0;
text_time_total = 24;
text_time_remaining = 24;
}
}
#endif // USE_QUICKSAVE
Uint32 temp_shift_release_callback(Uint32 interval, void *param) {
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_LSHIFT]) key_states[SDL_SCANCODE_LSHIFT] = 1;
if (state[SDL_SCANCODE_RSHIFT]) key_states[SDL_SCANCODE_RSHIFT] = 1;
return 0; // causes the timer to be removed
}
// seg000:04CD
int __pascal far process_key() {
char sprintf_temp[80];
int key;
const char* answer_text;
word need_show_text;
need_show_text = 0;
key = key_test_quit();
if (start_level == 0) {
if (key || control_shift) {
#ifdef USE_QUICKSAVE
if (key == SDL_SCANCODE_F9) need_quick_load = 1;
#endif
#ifdef USE_REPLAY
if (key == SDL_SCANCODE_TAB) {
start_replay();
}
else
#endif
if (key == (SDL_SCANCODE_L | WITH_CTRL)) { // ctrl-L
if (!load_game()) return 0;
} else {
start_level = 1;
}
draw_rect(&screen_rect, 0);
#ifdef USE_FADE
if (is_global_fading) {
fade_palette_buffer->proc_restore_free(fade_palette_buffer);
is_global_fading = 0;
}
#endif
start_game();
}
}
// If the Kid died, enter or shift will restart the level.
if (rem_min != 0 && Kid.alive > 6 && (control_shift || key == SDL_SCANCODE_RETURN)) {
key = SDL_SCANCODE_A | WITH_CTRL; // ctrl-a
}
#ifdef USE_REPLAY
if (recording) key_press_while_recording(&key);
else if (replaying) key_press_while_replaying(&key);
#endif
if (key == 0) return 0;
if (is_keyboard_mode) clear_kbd_buf();
switch(key) {
case SDL_SCANCODE_ESCAPE: // esc
case SDL_SCANCODE_ESCAPE | WITH_SHIFT: // allow pause while grabbing
is_paused = 1;
break;
case SDL_SCANCODE_SPACE: // space
is_show_time = 1;
break;
case SDL_SCANCODE_A | WITH_CTRL: // ctrl-a
if (current_level != 15) {
stop_sounds();
is_restart_level = 1;
}
break;
case SDL_SCANCODE_G | WITH_CTRL: // ctrl-g
// CusPoP: first and last level where saving is allowed
// if (current_level > 2 && current_level < 14) { // original
if (current_level >= saving_allowed_first_level && current_level <= saving_allowed_last_level) {
save_game();
}
break;
case SDL_SCANCODE_J | WITH_CTRL: // ctrl-j
if ((sound_flags & sfDigi) && sound_mode == smTandy) {
answer_text = "JOYSTICK UNAVAILABLE";
} else {
if (set_joy_mode()) {
answer_text = "JOYSTICK MODE";
} else {
answer_text = "JOYSTICK NOT FOUND";
}
}
need_show_text = 1;
break;
case SDL_SCANCODE_K | WITH_CTRL: // ctrl-k
answer_text = "KEYBOARD MODE";
is_joyst_mode = 0;
is_keyboard_mode = 1;
need_show_text = 1;
break;
case SDL_SCANCODE_R | WITH_CTRL: // ctrl-r
start_level = 0;
start_game();
break;
case SDL_SCANCODE_S | WITH_CTRL: // ctrl-s
turn_sound_on_off((!is_sound_on) * 15);
answer_text = "SOUND OFF";
if (is_sound_on) {
answer_text = "SOUND ON";
}
//
need_show_text = 1;
break;
case SDL_SCANCODE_V | WITH_CTRL: // ctrl-v
answer_text = "PRINCE OF PERSIA V1.0";
need_show_text = 1;
break;
case SDL_SCANCODE_L | WITH_SHIFT: // shift-l
if (current_level <= 3 || cheats_enabled) {
// if shift is not released within the delay, the cutscene is skipped
Uint32 delay = 250;
key_states[SDL_SCANCODE_LSHIFT] = 0;
key_states[SDL_SCANCODE_RSHIFT] = 0;
SDL_TimerID timer;
timer = SDL_AddTimer(delay, temp_shift_release_callback, NULL);
if (timer == 0) {
sdlperror("SDL_AddTimer");
quit(1);
}
if (current_level == 14) {
next_level = 1;
} else {
if (current_level == 15 && cheats_enabled) {
#ifdef USE_COPYPROT
if (options.enable_copyprot) {
next_level = copyprot_level;
copyprot_level = -1;
}
#endif
} else {
next_level = current_level + 1;
if (!cheats_enabled && rem_min > 15) {
rem_min = 15;
rem_tick = 719;
}
}
}
stop_sounds();
}
break;
#ifdef USE_QUICKSAVE
case SDL_SCANCODE_F6:
case SDL_SCANCODE_F6 | WITH_SHIFT:
if (Kid.alive < 0) need_quick_save = 1;
break;
case SDL_SCANCODE_F9:
case SDL_SCANCODE_F9 | WITH_SHIFT:
need_quick_load = 1;
break;
#ifdef USE_REPLAY
case SDL_SCANCODE_TAB | WITH_CTRL:
case SDL_SCANCODE_TAB | WITH_CTRL | WITH_SHIFT:
if (recording) { // finished recording
stop_recording();
}
else { // should start recording
start_recording();
}
break;
#endif // USE_RECORD_REPLAY
#endif // USE_QUICKSAVE
}
if (cheats_enabled) {
switch (key) {
case SDL_SCANCODE_C: // c
snprintf(sprintf_temp, sizeof(sprintf_temp), "S%d L%d R%d A%d B%d", drawn_room, room_L, room_R, room_A, room_B);
answer_text = /*&*/sprintf_temp;
need_show_text = 1;
break;
case SDL_SCANCODE_C | WITH_SHIFT: // shift-c
snprintf(sprintf_temp, sizeof(sprintf_temp), "AL%d AR%d BL%d BR%d", room_AL, room_AR, room_BL, room_BR);
answer_text = /*&*/sprintf_temp;
need_show_text = 1;
break;
case SDL_SCANCODE_MINUS:
case SDL_SCANCODE_KP_MINUS: // '-' --> subtract time cheat
if (rem_min > 1) --rem_min;
text_time_total = 0;
text_time_remaining = 0;
is_show_time = 1;
break;
case SDL_SCANCODE_EQUALS | WITH_SHIFT: // '+'
case SDL_SCANCODE_KP_PLUS: // '+' --> add time cheat
++rem_min;
text_time_total = 0;
text_time_remaining = 0;
is_show_time = 1;
break;
case SDL_SCANCODE_R: // R --> revive kid cheat
if (Kid.alive > 0) {
resurrect_time = 20;
Kid.alive = -1;
erase_bottom_text(1);
}
break;
case SDL_SCANCODE_K: // K --> kill guard cheat
guardhp_delta = -guardhp_curr;
Guard.alive = 0;
break;
case SDL_SCANCODE_I | WITH_SHIFT: // shift+I --> invert cheat
toggle_upside();
break;
case SDL_SCANCODE_W | WITH_SHIFT: // shift+W --> feather fall cheat
feather_fall();
break;
case SDL_SCANCODE_H: // H --> view room to the left
draw_guard_hp(0, 10);
next_room = room_L;
break;
case SDL_SCANCODE_J: // J --> view room to the right
draw_guard_hp(0, 10);
next_room = room_R;
break;
case SDL_SCANCODE_U: // U --> view room above
draw_guard_hp(0, 10);
next_room = room_A;
break;
case SDL_SCANCODE_N: // N --> view room below
draw_guard_hp(0, 10);
next_room = room_B;
break;
case SDL_SCANCODE_B | WITH_SHIFT: // shift-b
is_blind_mode = !is_blind_mode;
if (is_blind_mode) {
draw_rect(&rect_top, 0);
} else {
need_full_redraw = 1;
}
break;
case SDL_SCANCODE_S | WITH_SHIFT: // shift-s
if (hitp_curr != hitp_max) {
play_sound(sound_33_small_potion); // small potion (cheat)
hitp_delta = 1;
flash_color = 4; // red
flash_time = 2;
}
break;
case SDL_SCANCODE_T | WITH_SHIFT: // shift-t
play_sound(sound_30_big_potion); // big potion (cheat)
flash_color = 4; // red
flash_time = 4;
add_life();
break;
#ifdef USE_DEBUG_CHEATS
case SDL_SCANCODE_T:
printf("Remaining minutes: %d\tticks:%d\n", rem_min, rem_tick);
snprintf(sprintf_temp, sizeof(sprintf_temp), "M:%d S:%d T:%d", rem_min, rem_tick / 12, rem_tick);
answer_text = sprintf_temp;
need_show_text = 1;
break;
#endif
}
}
if (need_show_text) {
display_text_bottom(answer_text);
text_time_total = 24;
text_time_remaining = 24;
}
return 1;
}
// seg000:08EB
void __pascal far play_frame() {
do_mobs();
process_trobs();
check_skel();
check_can_guard_see_kid();
// if level is restarted, return immediately
if (play_kid_frame()) return;
play_guard_frame();
if (0 == resurrect_time) {
check_sword_hurting();
check_sword_hurt();
}
check_sword_vs_sword();
do_delta_hp();
exit_room();
check_the_end();
check_guard_fallout();
if (current_level == 0) {
// Special event: level 0 running exit
if (Kid.room == 24) {
draw_rect(&screen_rect, 0);
start_level = 0;
need_quotes = 1;
start_game();
}
} else if(current_level == 6) {
// Special event: level 6 falling exit
if (roomleave_result == -2) {
Kid.y = -1;
stop_sounds();
++next_level;
}
} else if(current_level == 12) {
// Special event: level 12 running exit
if (Kid.room == 23) {
++next_level;
// Sounds must be stopped, because play_level_2() checks next_level only if there are no sounds playing.
stop_sounds();
seamless = 1;
}
}
show_time();
// expiring doesn't count on Jaffar/princess level
if (current_level < 13 && rem_min == 0) {
expired();
}
}
// seg000:09B6
void __pascal far draw_game_frame() {
short var_2;
if (need_full_redraw) {
redraw_screen(0);
need_full_redraw = 0;
} else {
if (different_room) {
drawn_room = next_room;
if (tbl_level_type[current_level]) {
gen_palace_wall_colors();
}
redraw_screen(1);
} else {
if (need_redraw_because_flipped) {
need_redraw_because_flipped = 0;
redraw_screen(0);
} else {
memset_near(&table_counts, 0, sizeof(table_counts));
draw_moving();
draw_tables();
if (is_blind_mode) {
draw_rect(&rect_top, 0);
}
if (upside_down) {
flip_screen(offscreen_surface);
}
while (drects_count--) {
copy_screen_rect(&drects[drects_count]);
}
if (upside_down) {
flip_screen(offscreen_surface);
}
drects_count = 0;
}
}
}
play_next_sound();
// Note: texts are identified by their total time!
if (text_time_remaining == 1) {
// If the text's is about to expire:
if (text_time_total == 36 || text_time_total == 288) {
// 36: died on demo/potions level
// 288: press button to continue
// In this case, restart the game.
start_level = 0;
need_quotes = 1;
start_game();
} else {
// Otherwise, just clear it.
erase_bottom_text(1);
}
} else {
if (text_time_remaining != 0 && text_time_total != 1188) {
// 1188: potions level (page/line/word) -- this one does not disappear
--text_time_remaining;
if (text_time_total == 288 && text_time_remaining < 72) {
// 288: press button to continue
// Blink the message:
var_2 = text_time_remaining % 12;
if (var_2 > 3) {
erase_bottom_text(0);
} else {
if (var_2 == 3) {
display_text_bottom("Press Button to Continue");
play_sound_from_buffer(sound_pointers[sound_38_blink]); // press button blink
}
}
}
}
}
}
// seg000:0B12
void __pascal far anim_tile_modif() {
word tilepos;
for (tilepos = 0; tilepos < 30; ++tilepos) {
switch (get_curr_tile(tilepos)) {
case tiles_10_potion:
start_anim_potion(drawn_room, tilepos);
break;
case tiles_19_torch:
case tiles_30_torch_with_debris:
start_anim_torch(drawn_room, tilepos);
break;
case tiles_22_sword:
start_anim_sword(drawn_room, tilepos);
break;
}
}
}
// seg000:0B72
void __pascal far load_sounds(int first,int last) {
dat_type* ibm_dat = NULL;
dat_type* digi1_dat = NULL;
// dat_type* digi2_dat = NULL;
dat_type* digi3_dat = NULL;
dat_type* midi_dat = NULL;
short current;
ibm_dat = open_dat("IBM_SND1.DAT", 0);
if (sound_flags & sfDigi) {
digi1_dat = open_dat("DIGISND1.DAT", 0);
// digi2_dat = open_dat("DIGISND2.DAT", 0);
digi3_dat = open_dat("DIGISND3.DAT", 0);
}
if (sound_flags & sfMidi) {
midi_dat = open_dat("MIDISND1.DAT", 0);
}
#ifdef USE_MIXER
load_sound_names();
#endif
for (current = first; current <= last; ++current) {
if (sound_pointers[current] != NULL) continue;
/*if (demo_mode) {
sound_pointers[current] = decompress_sound((sound_buffer_type*) load_from_opendats_alloc(current + 10000));
} else*/ {
//sound_pointers[current] = (sound_buffer_type*) load_from_opendats_alloc(current + 10000, "bin", NULL, NULL);
//printf("overwriting sound_pointers[%d] = %p\n", current, sound_pointers[current]);
sound_pointers[current] = load_sound(current);
}
}
if (midi_dat) close_dat(midi_dat);
if (digi1_dat) close_dat(digi1_dat);
// if (digi2_dat) close_dat(digi2_dat);
if (digi3_dat) close_dat(digi3_dat);
close_dat(ibm_dat);
}
// seg000:0C5E
void __pascal far load_opt_sounds(int first,int last) {
// stub
dat_type* ibm_dat = NULL;
dat_type* digi_dat = NULL;
dat_type* midi_dat = NULL;
short current;
ibm_dat = open_dat("IBM_SND2.DAT", 0);
if (sound_flags & sfDigi) {
digi_dat = open_dat("DIGISND2.DAT", 0);
}
if (sound_flags & sfMidi) {
midi_dat = open_dat("MIDISND2.DAT", 0);
}
for (current = first; current <= last; ++current) {
//We don't free sounds, so load only once.
if (sound_pointers[current] != NULL) continue;
/*if (demo_mode) {
sound_pointers[current] = decompress_sound((sound_buffer_type*) load_from_opendats_alloc(current + 10000));
} else*/ {
//sound_pointers[current] = (sound_buffer_type*) load_from_opendats_alloc(current + 10000, "bin", NULL, NULL);
//printf("overwriting sound_pointers[%d] = %p\n", current, sound_pointers[current]);
sound_pointers[current] = load_sound(current);
}
}
if (midi_dat) close_dat(midi_dat);
if (digi_dat) close_dat(digi_dat);
close_dat(ibm_dat);
}
// data:03BA
const char*const tbl_guard_dat[] = {"GUARD.DAT", "FAT.DAT", "SKEL.DAT", "VIZIER.DAT", "SHADOW.DAT"};
// data:03C4
const char*const tbl_envir_gr[] = {"", "C", "C", "E", "E", "V"};
// data:03D0
const char*const tbl_envir_ki[] = {"DUNGEON", "PALACE"};
// seg000:0D20
void __pascal far load_lev_spr(int level) {
dat_type* dathandle;
short guardtype;
char filename[20];
dathandle = NULL;
current_level = next_level = level;
draw_rect(&screen_rect, 0);
free_optsnd_chtab();
snprintf(filename, sizeof(filename), "%s%s.DAT",
tbl_envir_gr[graphics_mode],
tbl_envir_ki[tbl_level_type[current_level]]
);
load_chtab_from_file(id_chtab_6_environment, 200, filename, 1<<5);
load_more_opt_graf(filename);
guardtype = tbl_guard_type[current_level];
if (guardtype != -1) {
if (guardtype == 0) {
dathandle = open_dat(tbl_level_type[current_level] ? "GUARD1.DAT" : "GUARD2.DAT", 0);
}
load_chtab_from_file(id_chtab_5_guard, 750, tbl_guard_dat[guardtype], 1<<8);
if (dathandle) {
close_dat(dathandle);
}
}
curr_guard_color = 0;
load_chtab_from_file(id_chtab_7_environmentwall, 360, filename, 1<<6);
// Level colors (1.3)
if (graphics_mode == gmMcgaVga && level_var_palettes != NULL) {
int level_color = tbl_level_color[current_level];
if (level_color != 0) {
byte* env_pal = level_var_palettes + 0x30*(level_color-1);
byte* wall_pal = env_pal + 0x30 * tbl_level_type[current_level];
set_pal_arr(0x50, 0x10, (rgb_type*)env_pal, 1);
set_pal_arr(0x60, 0x10, (rgb_type*)wall_pal, 1);
set_chtab_palette(chtab_addrs[id_chtab_6_environment], env_pal, 0x10);
set_chtab_palette(chtab_addrs[id_chtab_7_environmentwall], wall_pal, 0x10);
}
}
/*if (comp_skeleton[current_level])*/ {
load_opt_sounds(44, 44); // skel alive
}
/*if (comp_mirror[current_level])*/ {
load_opt_sounds(45, 45); // mirror
}
/*if (comp_chomper[current_level])*/ {
load_opt_sounds(46, 47); // something chopped, chomper
}
/*if (comp_spike[current_level])*/ {
load_opt_sounds(48, 49); // something spiked, spikes
}
}
// seg000:0E6C
void __pascal far load_level() {
dat_type* dathandle;
dathandle = open_dat("LEVELS.DAT", 0);
load_from_opendats_to_area(current_level + 2000, &level, sizeof(level), "bin");
close_dat(dathandle);
alter_mods_allrm();
reset_level_unused_fields(true); // added
}
void reset_level_unused_fields(bool loading_clean_level) {
// Entirely unused fields in the level format: reset to zero for now
// They can be repurposed to add new stuff to the level format in the future
memset(level.roomxs, 0, COUNT(level.roomxs));
memset(level.roomys, 0, COUNT(level.roomys));
memset(level.fill_1, 0, COUNT(level.fill_1));
memset(level.fill_2, 0, COUNT(level.fill_2));
memset(level.fill_3, 0, COUNT(level.fill_3));
// For these fields, only use the bits that are actually used, and set the rest to zero.
// Good for repurposing the unused bits in the future.
int i;
for (i = 0; i < level.used_rooms; ++i) {
//level.guards_dir[i] &= 0x01; // 1 bit in use
level.guards_skill[i] &= 0x0F; // 4 bits in use
}
// In savestates, additional information may be stored (e.g. remembered guard hp) - should not reset this then!
if (loading_clean_level) {
for (i = 0; i < level.used_rooms; ++i) {
level.guards_color[i] &= 0x0F; // 4 bits in use (other 4 bits repurposed as remembered guard hp)
}
}
}
// seg000:0EA8
// returns 1 if level is restarted, 0 otherwise
int __pascal far play_kid_frame() {
loadkid_and_opp();
load_fram_det_col();
check_killed_shadow();
play_kid();
if (upside_down && Char.alive >= 0) {
upside_down = 0;
need_redraw_because_flipped = 1;
}
if (is_restart_level) {
return 1;
}
if (Char.room != 0) {
play_seq();
fall_accel();
fall_speed();
load_frame_to_obj();
load_fram_det_col();
set_char_collision();
bump_into_opponent();
check_collisions();
check_bumped();
check_gate_push();
check_action();