-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlode.c
More file actions
1697 lines (1404 loc) · 45.7 KB
/
Copy pathxlode.c
File metadata and controls
1697 lines (1404 loc) · 45.7 KB
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
/* ---------------------------------------------------------------- */
/* Includes */
/* ---------------------------------------------------------------- */
/* ***** Standard C ***** */
#include <stdlib.h>
#include <stdio.h>
#ifndef SIZE_320_200
#define SIZE_640_480
#endif
#include "sizes.h"
#include "GFX_SDL.c"
#include "IO_SDL.c"
#include "LIB_SDL.c"
#include "SFX_SDL_mixer.c"
#include "MUS_SDL_mixer.c"
/* ---------------------------------------------------------------- */
/* Definitions */
/* ---------------------------------------------------------------- */
#undef TRUE
#undef FALSE
#ifndef NULL
#define NULL 0
#endif
#define XLode_debug(x) fprintf(stderr, x)
#define XLode_debug1(x,x1) fprintf(stderr, x,x1)
#define XLode_debug2(x,x1,x2) fprintf(stderr, x,x1,x2)
#define XLode_debug3(x,x1,x2,x3) fprintf(stderr, x,x1,x2,x3)
#define XLode_debug4(x,x1,x2,x3,x4) fprintf(stderr, x,x1,x2,x3,x4)
// #define randomNumber(x) ((int)(x * rand()/(RAND_MAX+1.0)))
#define randomNumber(x) (rand() % x)
/* ***** Game parameters ***** */
/**
* Limitations
*/
#define Ci_arenaW 40
#define Ci_arenaH 20
#define Ci_stageCount 4
#define Ci_figureCount 11
#define Ci_monkCount 10
/**
* Sound channels
*/
#define Ci_lodeChannel 0
#define Ci_brickChannel 1
#define Ci_treasureChannel 2
#define Ci_messageChannel 3
/**
* Motion properties of a game element
*/
#define FROM_SIDES 1
#define FROM_UP 2
#define FROM_DOWN 4
#define TO_SIDES 8
#define TO_UP 16
#define TO_DOWN 32
#define FALL_FROM 64
#define FALL_TO 128
/**
* Amount of time that the player can't move while breaking a brick
*/
#define Ci_breakingTime 5
/**
* Number of songs used in the game
*/
#define Ci_songCount 5
/* --------------------------------------------------------------- */
/* Data structures */
/* --------------------------------------------------------------- */
/* ***** Enumerations ***** */
typedef enum {
LODE = 0,
MONK,
BRICK,
ROCK,
GHOSTBRICK,
EMPTY,
STAIRS,
TREASURE,
ROPE,
BROKENBRICK,
HIDDENSTAIRS
} ElementType;
typedef enum {
GET_TREASURE = 0,
BREAK_BRICK,
REACH_SKY,
READY,
GO,
DEATH,
YESSS
} SampleType;
#define Ci_sampleCount 7
typedef enum {
NONE,
UP,
DOWN,
LEFT,
RIGHT
} Direction;
typedef enum {
FALSE = 0,
TRUE
} Boolean;
/* ***** Records ***** */
typedef struct {
ElementType e_type;
int i_moveSet;
int i_status;
} Element;
typedef struct {
int i_x;
int i_y;
int i_offsetX;
int i_offsetY;
Direction e_direction;
Direction e_nextDirection;
Direction e_lastDirection;
Boolean b_falling;
GFX_Image** papr_runningLeft;
GFX_Image** papr_runningRight;
GFX_Image* pr_defaultFigure;
GFX_Image* pr_defaultFalling;
int i_respawnX;
int i_respawnY;
Boolean b_isLode;
Boolean b_locked; /* Monk is locked in a broken brick */
} Player;
typedef struct {
int i_x;
int i_y;
int i_timer;
int i_step;
} BrickEvent;
/* --------------------------------------------------------------- */
/* Game data */
/* --------------------------------------------------------------- */
/**
* Generic game data
*/
Element aar_arena[Ci_arenaW][Ci_arenaH];
char aac_mapCache[Ci_stageCount+1][Ci_arenaW][Ci_arenaH];
Boolean b_retry;
Boolean b_gameOver;
int i_stage;
int i_score;
int i_lives;
Boolean ab_cachedStage[Ci_stageCount+1];
Player r_lode;
int i_monkCount;
Player ar_monks[Ci_monkCount];
int i_treasures;
int i_gameTimer;
Boolean b_playing;
Boolean b_victory;
int i_breaking;
Direction e_scheduledBreak;
Direction e_lastBreak;
/**
* Broken bricks
*/
int i_brokenCount;
int i_brokenHead, i_brokenTail;
BrickEvent ar_broken[Ci_arenaW * Ci_arenaH];
/**
* Frames for the "breaking bricks" animation
*/
// Concepts:
// The animation contains eight frames, called "steps", but only 5
// different images are used; hence the difference between the
// steps of the animation and the frames that are actually used.
#define Ci_brokenSteps 8
int ai_brokenTimes[Ci_brokenSteps] = { 2, 4, 6, 8, 120, 130, 140, 150 };
int ai_brokenFrames[Ci_brokenSteps] = { 1, 2, 3, 0, 3, 2, 1, 4 };
/**
* Monk logic
*/
Direction ae_directions[4] = {UP, DOWN, LEFT, RIGHT};
/** ***** Graphics ***** */
/**
* Numbers
*/
GFX_Image* apr_numbers[10];
/**
* Lode animations
*/
GFX_Image* apr_lodeRunningRight[8];
GFX_Image* apr_lodeRunningLeft[8];
GFX_Image* pr_lodeFalling;
/**
* Monk animations
*/
GFX_Image* apr_monkRunningRight[8];
GFX_Image* apr_monkRunningLeft[8];
GFX_Image* pr_monkFalling;
/**
* Broken bricks
*/
#define Ci_brokenFrames 5
GFX_Image* apr_brokenAnimation[Ci_brokenFrames];
GFX_Image* pr_lodeBreakingLeft;
GFX_Image* pr_lodeBreakingRight;
/**
* Generic game graphics
*/
GFX_Image* apr_figures[Ci_figureCount];
GFX_Image* pr_backBuffer;
GFX_Image* pr_arenaBuffer;
/**
* Graphics for the opening
*/
GFX_Image* pr_credits;
GFX_Image* pr_author;
GFX_Image* pr_title;
GFX_Image* pr_options;
GFX_Image* pr_lodeIcon;
GFX_Image* pr_background;
GFX_Image* pr_gameOver;
GFX_Image* pr_gameOverFlip;
/* ***** Sounds ***** */
int i_lodeSoundX;
int i_lodeSoundY;
SFX_Sample* pr_lodeVoice;
Boolean b_wasFalling;
MUS_Song* apr_songs[Ci_songCount];
SFX_Sample* apr_samples[Ci_sampleCount];
Boolean b_halfTime = FALSE;
/**
* Lode on the rope
*/
#define Ci_ropeSampleCount 8
SFX_Sample* apr_ropeSamples[Ci_ropeSampleCount];
/**
* Lode falling
*/
#define Ci_fallSampleCount 5
SFX_Sample* apr_fallSamples[Ci_fallSampleCount];
/**
* End of a fall ("ouch")
*/
#define Ci_ouchSampleCount 3
SFX_Sample* apr_ouchSamples[Ci_ouchSampleCount];
/**
* Lode on the stairs
*/
#define Ci_stairsSampleCount 4
SFX_Sample* apr_stairsSamples[Ci_stairsSampleCount];
/**
* Lode running
*/
#define Ci_runningSampleCount 3
SFX_Sample* apr_runningSamples[Ci_runningSampleCount];
/* ---------------------------------------------------------------- */
/* Game specific routines */
/* ---------------------------------------------------------------- */
/* ***** Animation ***** */
/*
void Animation_play(char* sz_fileName_, JGMOD* pr_module) {
GFX_Image* pr_centered;
clear(screen);
Allegro_setVideo(8);
pr_centered = create_sub_bitmap(screen, Ci_resX/2-(320/2),Ci_resY/2-(200/2), 320, 200);
set_mod_volume(250);
play_mod(pr_module, FALSE);
play_fli(sz_fileName_, pr_centered, 0, NULL);
destroy_bitmap(pr_centered);
Allegro_setVideo(16);
}
*/
/*
void Animation_play(char* sz_fileName_, JGMOD* pr_module) {
GFX_Image* pr_centered;
set_color_conversion(COLORCONV_EXPAND_256);
clear(screen);
pr_centered = create_sub_bitmap(screen, Ci_resX/2-(320/2),Ci_resY/2-(200/2), 320, 200);
set_mod_volume(250);
play_mod(pr_module, FALSE);
open_fli(sz_fileName_);
while (next_fli_frame(0) == FLI_OK) {
set_palette(fli_palette);
rest(30);
GFX_Blit(fli_bitmap, pr_centered, 0, 0, 0, 0, 320, 200);
}
close_fli();
destroy_bitmap(pr_centered);
}
*/
/* ***** Figures ***** */
/**
* Draws the figure that's at the indicated position
* directly at the back buffer.
*/
void Figure_draw(int i_x_, int i_y_) {
Element* pr_this = &aar_arena[i_x_][i_y_];
GFX_Blit(pr_background, pr_backBuffer,
i_x_ * Ci_spriteW, i_y_ * Ci_spriteH,
i_x_ * Ci_spriteW, i_y_ * Ci_spriteH,
Ci_spriteW, Ci_spriteH);
if (apr_figures[pr_this->e_type] != NULL) {
GFX_Draw(pr_backBuffer, apr_figures[pr_this->e_type],
i_x_ * Ci_spriteW, i_y_ * Ci_spriteH);
}
}
/**
* Draws the figure that's at the indicated position,
* obtaining it from the arena's cache.
*/
void Figure_show(int i_x_, int i_y_) {
GFX_Blit(pr_arenaBuffer, pr_backBuffer, i_x_ * Ci_spriteW, i_y_ * Ci_spriteH, i_x_ * Ci_spriteW, i_y_ * Ci_spriteH, Ci_spriteW, Ci_spriteH);
}
/**
* Updates the arena's cache putting the given sprite at
* the specified position.
*/
void Figure_update(int i_x_, int i_y_, GFX_Image* pr_sprite_) {
Element* pr_this = &aar_arena[i_x_][i_y_];
GFX_Blit(pr_background, pr_arenaBuffer, i_x_ * Ci_spriteW, i_y_ * Ci_spriteH, i_x_ * Ci_spriteW, i_y_ * Ci_spriteH, Ci_spriteW, Ci_spriteH);
if (pr_sprite_ != NULL) {
GFX_Draw(pr_arenaBuffer, pr_sprite_, i_x_ * Ci_spriteW, i_y_ * Ci_spriteH);
}
}
void Number_displayAt(GFX_Image* pr_target, int i_x_, int i_y_, int i_number_, int i_digits_) {
char sz_written[20];
int i_length;
int i_pad;
int i;
i_pad = 0;
sprintf(sz_written, "%d", i_number_);
i_length = strlen(sz_written);
if (i_length < i_digits_) {
for(; i_pad < i_digits_ - i_length; i_pad++) {
GFX_Draw(pr_target, apr_numbers[0], i_x_+(i_pad*Ci_numberWidth), i_y_);
}
}
for(i = 0; i < i_length; i++) {
GFX_Draw(pr_target, apr_numbers[sz_written[i]-48], i_x_+(i_pad*Ci_numberWidth), i_y_);
i_pad++;
}
}
/* ***** Sounds ***** */
void Sound_play(SampleType e_id_, int i_xPosition_, int i_channel_) {
SFX_Play(apr_samples[e_id_], 255, 255 * ((double)i_xPosition_ / (double)Ci_arenaW), i_channel_, FALSE);
}
/**
* Play a sound base on Lode's current position
*/
void Sound_lodeSound() {
int i_height;
int i_yCount;
int i_fall;
int i_panning;
SFX_Sample* pr_newLodeVoice;
pr_newLodeVoice = NULL;
i_panning = 255 * ((double)r_lode.i_x / (double)Ci_arenaW);
if (i_lodeSoundX == r_lode.i_x && i_lodeSoundY == r_lode.i_y)
return;
i_lodeSoundX = r_lode.i_x;
i_lodeSoundY = r_lode.i_y;
if (aar_arena[r_lode.i_x][r_lode.i_y].e_type == ROPE) {
if (b_halfTime = ~b_halfTime)
pr_newLodeVoice = apr_ropeSamples[randomNumber(Ci_ropeSampleCount)];
else
pr_newLodeVoice = NULL;
} else if (aar_arena[r_lode.i_x][r_lode.i_y].e_type == STAIRS) {
if (b_halfTime = ~b_halfTime)
pr_newLodeVoice = apr_stairsSamples[randomNumber(Ci_stairsSampleCount)];
else
pr_newLodeVoice = NULL;
} else if (r_lode.b_falling && b_wasFalling) {
// Check if done falling
i_yCount = r_lode.i_y + 1;
// TODO: this entire function sucks at 04:16am
if (!(i_yCount <= Ci_arenaH && aar_arena[r_lode.i_x][i_yCount].i_moveSet & FALL_TO
&& i_yCount <= Ci_arenaH && aar_arena[r_lode.i_x][i_yCount].i_moveSet & FALL_FROM)) {
pr_newLodeVoice = apr_ouchSamples[randomNumber(Ci_ouchSampleCount)];
b_wasFalling = FALSE;
}
} else if (r_lode.b_falling && !b_wasFalling) {
// Scream
i_height = 0;
i_yCount = r_lode.i_y;
// TODO: fix this; it's 3:42am and I don't want to think about it
while (i_yCount <= Ci_arenaH && aar_arena[r_lode.i_x][i_yCount].i_moveSet & FALL_TO
&& i_yCount <= Ci_arenaH && aar_arena[r_lode.i_x][i_yCount].i_moveSet & FALL_FROM) {
i_height++;
i_yCount++;
}
if (i_height <= 2) i_fall = 0;
else if (i_height <= 5) i_fall = 1;
else if (i_height <= 8) i_fall = 2;
else if (i_height <= 11) i_fall = 3;
else i_fall = 4;
pr_newLodeVoice = apr_fallSamples[i_fall];
b_wasFalling = TRUE;
} else {
if (b_halfTime = ~b_halfTime)
pr_newLodeVoice = apr_runningSamples[randomNumber(Ci_runningSampleCount)];
else
pr_newLodeVoice = NULL;
}
if (pr_newLodeVoice != NULL) {
SFX_Stop(Ci_lodeChannel);
SFX_Play(pr_newLodeVoice, 255, i_panning, Ci_lodeChannel, 0);
pr_lodeVoice = pr_newLodeVoice;
}
}
/**
* Check if there's a monk at a given point
*/
Player* Monk_anyAt(int i_x, int i_y) {
int i;
for (i = 0; i < i_monkCount; i++) {
if (ar_monks[i].i_x == i_x && ar_monks[i].i_y == i_y) {
return &ar_monks[i];
}
}
return NULL;
}
/* ***** Bricks ***** */
/**
* Break a brick: make the space empty and schedule the bricks reappearance.
*/
void Bricks_break(int i_x_, int i_y_) {
BrickEvent* pr_brick = &(ar_broken[i_brokenTail]);
Sound_play(BREAK_BRICK, i_x_, Ci_brickChannel);
i_brokenCount++;
pr_brick->i_x = i_x_;
pr_brick->i_y = i_y_;
pr_brick->i_timer = i_gameTimer;
pr_brick->i_step = 0;
i_brokenTail++;
aar_arena[i_x_][i_y_].e_type = BROKENBRICK;
aar_arena[i_x_][i_y_].i_moveSet = FROM_SIDES | FROM_UP | FROM_DOWN
| TO_SIDES | TO_DOWN
| FALL_FROM | FALL_TO;
}
void Bricks_update() {
int i;
int i_x, i_y;
Player* pr_monk;
if (i_brokenCount == 0)
return;
for (i = i_brokenHead; i < i_brokenTail; i++) {
if (ar_broken[i].i_timer + ai_brokenTimes[ar_broken[i].i_step] == i_gameTimer) {
Figure_update(ar_broken[i].i_x,
ar_broken[i].i_y,
apr_brokenAnimation[ai_brokenFrames[ar_broken[i].i_step]]);
Figure_show(ar_broken[i].i_x, ar_broken[i].i_y);
ar_broken[i].i_step++;
}
}
if (ar_broken[i_brokenHead].i_step == Ci_brokenSteps) {
i_x = ar_broken[i_brokenHead].i_x;
i_y = ar_broken[i_brokenHead].i_y;
aar_arena[i_x][i_y].e_type = BRICK;
aar_arena[i_x][i_y].i_moveSet = 0;
i_brokenHead++;
i_brokenCount--;
if (i_brokenCount == 0) {
i_brokenHead = 0;
i_brokenTail = 0;
}
if (r_lode.i_x == i_x && r_lode.i_y == i_y) {
b_playing = FALSE;
}
while (pr_monk = Monk_anyAt(i_x, i_y)) {
pr_monk->i_x = pr_monk->i_respawnX;
pr_monk->i_y = pr_monk->i_respawnY;
pr_monk->b_locked = FALSE;
}
if (r_lode.i_x == i_x && r_lode.i_y == i_y) {
b_playing = FALSE;
}
}
}
/* ***** Player ***** */
/**
* Verifies that the attempted move is valid in the arena.
*/
int Player_isValidMove(Player r_this, Direction e_direction) {
int i_x, i_lx;
int i_y, i_ly;
if (e_direction == NONE)
return TRUE;
i_x = r_this.i_x;
i_lx = i_x;
i_y = r_this.i_y;
i_ly = i_y;
if (e_direction == UP) i_y--;
if (e_direction == DOWN) i_y++;
if (e_direction == LEFT) i_x--;
if (e_direction == RIGHT) i_x++;
if (!r_this.b_isLode && Monk_anyAt(i_x, i_y))
return FALSE;
if (i_x < 0 || i_x > Ci_arenaW - 1 || i_y < 0 || i_y > Ci_arenaH - 1)
return FALSE;
if (e_direction == LEFT || e_direction == RIGHT)
return (aar_arena[i_lx][i_ly].i_moveSet & TO_SIDES
&& aar_arena[i_x][i_y].i_moveSet & FROM_SIDES);
if (e_direction == UP)
return (aar_arena[i_lx][i_ly].i_moveSet & TO_UP
&& aar_arena[i_x][i_y].i_moveSet & FROM_DOWN);
if (e_direction == DOWN)
return (aar_arena[i_lx][i_ly].i_moveSet & TO_DOWN
&& aar_arena[i_x][i_y].i_moveSet & FROM_UP);
return FALSE;
}
/**
* Erase the area surrounding the player, restoring
* the arena. Notice that other characters (monks)
* will be erased too.
*/
void Player_clear(Player r_this) {
Figure_show(r_this.i_x, r_this.i_y);
if (r_this.i_offsetX > 0)
Figure_show(r_this.i_x+1, r_this.i_y);
else if (r_this.i_offsetX < 0)
Figure_show(r_this.i_x-1, r_this.i_y);
else if (r_this.i_offsetY > 0)
Figure_show(r_this.i_x, r_this.i_y+1);
else if (r_this.i_offsetY < 0)
Figure_show(r_this.i_x, r_this.i_y-1);
}
/**
* Draw the player.
*/
void Player_draw(Player r_this) {
int i_index;
int i_rope;
GFX_Image* pr_sprite;
GFX_Image** apr_runningLeft;
GFX_Image** apr_runningRight;
apr_runningLeft = r_this.papr_runningLeft;
apr_runningRight = r_this.papr_runningRight;
// Remember that only one of those will be > 0
i_index =
ABS( r_this.i_offsetX / Ci_horizSprites + r_this.i_offsetY / Ci_vertSprites );
if (aar_arena[r_this.i_x][r_this.i_y].e_type == ROPE)
i_rope = 4;
else
i_rope = 0;
// TODO: stairs
if (r_this.e_lastDirection == LEFT)
pr_sprite = apr_runningLeft[i_index + i_rope];
else if (r_this.e_lastDirection == RIGHT)
pr_sprite = apr_runningRight[i_index + i_rope];
else if (r_this.e_lastDirection == UP)
pr_sprite = r_this.pr_defaultFalling;
else if (r_this.e_lastDirection == DOWN) {
if (aar_arena[r_this.i_x][r_this.i_y].i_moveSet & FALL_FROM)
pr_sprite = r_this.pr_defaultFalling;
else
pr_sprite = r_this.pr_defaultFalling;
}
if (r_this.b_isLode && i_breaking > 0) {
if (e_lastBreak == LEFT)
pr_sprite = pr_lodeBreakingLeft;
else
pr_sprite = pr_lodeBreakingRight;
}
GFX_Draw(pr_backBuffer,
pr_sprite,
Ci_spriteW * r_this.i_x + r_this.i_offsetX,
Ci_spriteH * r_this.i_y + r_this.i_offsetY);
}
/**
* Updates the player's position, based on current
* direction coordinates.
*/
// Concepts:
// Stable - player is exactly in a position in the arena, with no offset.
// Unstable - player is moving between two positions, as indicated by the
// offset.
void Player_move(Player* pr_this) {
Element r_here, r_under;
int i_x, i_y;
i_x = pr_this->i_x;
i_y = pr_this->i_y;
r_here = aar_arena[i_x][i_y];
r_under = aar_arena[i_x][i_y+1];
// If in a stable place, check if direction is valid and update it.
if (pr_this->i_offsetX == 0 && pr_this->i_offsetY == 0) {
if ((!pr_this->b_isLode) && r_here.e_type == BROKENBRICK) {
// Lock monk in broken brick
// TODO timer for monk escape
pr_this->e_direction = NONE;
pr_this->e_nextDirection = NONE;
} else if (pr_this->i_y < Ci_arenaH
&& r_here.i_moveSet & FALL_FROM
&& r_under.i_moveSet & FALL_TO
&& !Monk_anyAt(i_x, i_y+1)
) {
// Gravity has the priority :)
pr_this->e_direction = DOWN;
pr_this->e_nextDirection = DOWN;
pr_this->b_falling = TRUE;
} else {
if (pr_this->b_falling) {
pr_this->e_direction = NONE;
pr_this->e_nextDirection = NONE;
} else if (Player_isValidMove(*pr_this, pr_this->e_nextDirection)) {
pr_this->e_direction = pr_this->e_nextDirection;
} else {
pr_this->e_direction = NONE;
pr_this->e_nextDirection = NONE;
}
pr_this->b_falling = FALSE;
}
} else {
// If opposite direction, allow changing direction even if unstable
if (pr_this->e_direction == UP && pr_this->e_nextDirection == DOWN)
pr_this->e_direction = DOWN;
if (!pr_this->b_falling && pr_this->e_direction == DOWN && pr_this->e_nextDirection == UP)
pr_this->e_direction = UP;
if (pr_this->e_direction == LEFT && pr_this->e_nextDirection == RIGHT)
pr_this->e_direction = RIGHT;
if (pr_this->e_direction == RIGHT && pr_this->e_nextDirection == LEFT)
pr_this->e_direction = LEFT;
}
// Move
if (pr_this->e_direction == RIGHT)
pr_this->i_offsetX += Ci_deltaX;
if (pr_this->e_direction == LEFT)
pr_this->i_offsetX -= Ci_deltaX;
if (pr_this->e_direction == DOWN)
pr_this->i_offsetY += Ci_deltaY;
if (pr_this->e_direction == UP)
pr_this->i_offsetY -= Ci_deltaY;
// If full offset is reached, stabilize position
if (pr_this->i_offsetX == Ci_spriteW) {
pr_this->i_x++;
pr_this->i_offsetX = 0;
}
if (pr_this->i_offsetX == -Ci_spriteW) {
pr_this->i_x--;
pr_this->i_offsetX = 0;
}
if (pr_this->i_offsetY == Ci_spriteH) {
pr_this->i_y++;
pr_this->i_offsetY = 0;
}
if (pr_this->i_offsetY == -Ci_spriteH) {
pr_this->i_y--;
pr_this->i_offsetY = 0;
}
if (pr_this->e_direction != NONE)
pr_this->e_lastDirection = pr_this->e_direction;
}
/**
* Returns TRUE if the player is perfectly centered in a position.
*/
int Player_isStable(Player r_this) {
return (r_this.i_offsetX == 0 && r_this.i_offsetY == 0);
}
/**
* Blink all players on the screen.
*/
void Player_blinkAll(int i_times_, int i_onTime_, int i_offTime_) {
int i;
int j;
for (j = 0; j < i_times_; j++) {
Player_clear(r_lode);
for (i = 0; i < i_monkCount; i++)
Player_clear(ar_monks[i]);
GFX_DisplayBuffer(pr_backBuffer);
IO_Wait(i_offTime_);
Player_draw(r_lode);
for (i = 0; i < i_monkCount; i++)
Player_draw(ar_monks[i]);
GFX_DisplayBuffer(pr_backBuffer);
IO_Wait(i_onTime_);
}
}
/* ***** Lode specific functions ***** */
/**
* Attempt to break a brick in a specified direction, which may be
* LEFT or RIGHT.
*/
void Lode_break(Direction e_direction) {
int i_x;
int i_y;
i_x = r_lode.i_x;
i_y = r_lode.i_y;
if (i_y == Ci_arenaH)
return;
if (e_direction == LEFT) {
if (i_x == 0)
return;
if (aar_arena[i_x-1][i_y].i_moveSet & TO_SIDES
&& aar_arena[i_x-1][i_y+1].e_type == BRICK
&& !Monk_anyAt(i_x-1, i_y)
) {
Bricks_break(i_x-1, i_y+1);
}
} else if (e_direction == RIGHT) {
if (i_x == Ci_arenaW - 1)
return;
if (aar_arena[i_x+1][i_y].i_moveSet & TO_SIDES
&& aar_arena[i_x+1][i_y+1].e_type == BRICK
&& !Monk_anyAt(i_x+1, i_y)
) {
Bricks_break(i_x+1, i_y+1);
}
}
}
/* ***** Monk specific functions ***** */
Direction Monk_decideDirection(Player r_monk) {
int i_xDistance;
int i_yDistance;
Direction e_choice;
Boolean ab_isValid[4];
int i;
int i_limit;
i_xDistance = r_monk.i_x - r_lode.i_x;
i_yDistance = r_monk.i_y - r_lode.i_y;
for(i=0; i<4; i++)
ab_isValid[i] = Player_isValidMove(r_monk, ae_directions[i]);
// Case 1: Lode is in sight
if (ab_isValid[0] && i_xDistance == 0 && i_yDistance > 0)
return UP;
if (ab_isValid[1] && i_xDistance == 0 && i_yDistance < 0)
return DOWN;
if (ab_isValid[2] && i_yDistance == 0 && i_xDistance > 0)
return LEFT;
if (ab_isValid[3] && i_yDistance == 0 && i_xDistance < 0)
return RIGHT;
// Case 2: Know when Lode is close
if (ab_isValid[0] && ABS(i_xDistance) < 5 && i_yDistance > 0)
if (randomNumber(10))
return UP;
if (ab_isValid[1] && ABS(i_xDistance) < 5 && i_yDistance < 0)
if (randomNumber(2))
return DOWN;
if (ab_isValid[2] && ABS(i_yDistance) < 5 && i_xDistance > 0)
return LEFT;
if (ab_isValid[3] && ABS(i_yDistance) < 5 && i_xDistance < 0)
return RIGHT;
i_limit = 0;
do {
if (randomNumber(3))
e_choice = r_monk.e_lastDirection;
else
e_choice = ae_directions[randomNumber(4)];
i_limit++;
} while (i_limit < 16 && !Player_isValidMove(r_monk, e_choice));
if (i_limit == 16) e_choice = NONE;
return e_choice;
}
/* ***** Treasure ***** */
void Treasure_check() {
int i, j;
if (aar_arena[r_lode.i_x][r_lode.i_y].e_type == TREASURE) {
i_treasures--;
if (i_treasures == 0) {
Sound_play(REACH_SKY, r_lode.i_x, Ci_messageChannel);
for (j = 0; j < Ci_arenaH; j++)
for (i = 0; i < Ci_arenaW; i++)
if (aar_arena[i][j].e_type == HIDDENSTAIRS) {
aar_arena[i][j].e_type == STAIRS;
aar_arena[i][j].i_moveSet = FROM_SIDES | FROM_UP | FROM_DOWN
| TO_SIDES | TO_UP | TO_DOWN;
Figure_update(i, j, apr_figures[STAIRS]);
Figure_show(i, j);
}
} else {
Sound_play(GET_TREASURE, r_lode.i_x, Ci_treasureChannel);
}
i_score+=50;
aar_arena[r_lode.i_x][r_lode.i_y].e_type = EMPTY;
Figure_update(r_lode.i_x, r_lode.i_y,
apr_figures[EMPTY]);
Figure_show(r_lode.i_x, r_lode.i_y);
}
}
/* ***** Status bar ***** */
void Status_displayScore() {
GFX_Blit(pr_arenaBuffer, pr_backBuffer, Ci_scoreX, Ci_statusY, Ci_scoreX, Ci_statusY, 8*Ci_numberWidth, Ci_numberHeight);
Number_displayAt(pr_backBuffer, Ci_scoreX, Ci_statusY, i_score, 7);
}
/* ***** Gameplay ***** */
/**
* Prepare for the start of a new round of gameplay:
* clear everything and start at the first stage.
*/
void Play_init() {
b_retry = FALSE;
b_gameOver = FALSE;
i_stage = 1;
i_score = 0;
i_lives = 3;
}
/**
* Load stage file; construct the arena.
*/
void Play_loadStage() {
char sz_fileName[20];
int i, j;
FILE* fd;
i_brokenCount = 0;
i_brokenHead = 0;
i_brokenTail = 0;
i_treasures = 0;
sprintf(sz_fileName, "stage%d.map", i_stage);
GFX_Clear(GFX_Screen());
GFX_UpdateScreen();
GFX_Clear(pr_backBuffer);