-
Notifications
You must be signed in to change notification settings - Fork 3
/
boulder_dash.c
1845 lines (1584 loc) · 59.8 KB
/
boulder_dash.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
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#define ARRAY_LENGTH(array) (sizeof(array)/sizeof(*array))
#include "sound.h"
#include "sound.c"
#include "data_sprites.h"
#include "data_caves.h"
// Developer options
#define DEV_IMMEDIATE_STARTUP 0
#define DEV_NEAR_OUTBOX 0
#define DEV_SINGLE_DIAMOND_NEEDED 0
#define DEV_CHEAP_BONUS_LIFE 0
#define DEV_CAMERA_DEBUGGING 0
#define DEV_SLOW_TICK_DURATION 0
#define DEV_QUICK_OUT_OF_TIME 0
#define DEV_SINGLE_LIFE 0
// Gameplay constants
#define START_CAVE CAVE_A
#define TICKS_PER_TURN 5
#define ROCKFORD_TURNS_TILL_BIRTH 12
#define CELL_COVER_TURNS 40
#define TILE_COVER_TICKS (32*TICKS_PER_TURN)
#define BONUS_LIFE_COST (DEV_CHEAP_BONUS_LIFE ? 5 : 500)
#define SPACE_FLASHING_TURNS 10
#define MAX_LIVES 9
#define COVER_PAUSE 2
#define TICKS_PER_CAVE_SECOND (7*TICKS_PER_TURN)
#define OUT_OF_TIME_ON_TURNS 25
#define OUT_OF_TIME_OFF_TURNS 42
#define TURNS_TILL_GAME_RESTART 10
#define TURNS_TILL_EXITING_CAVE 12
#define TOO_MANY_AMOEBA 200
#define AMOEBA_FACTOR_SLOW 127
#define AMOEBA_FACTOR_FAST 15
// Keys
#define KEY_FIRE VK_SPACE
#define KEY_RIGHT VK_RIGHT
#define KEY_LEFT VK_LEFT
#define KEY_DOWN VK_DOWN
#define KEY_UP VK_UP
#define KEY_FAIL 'Q'
#define KEY_QUIT VK_ESCAPE
// Cave map consists of cells, each cell contains 4 (2x2) tiles
#define TILE_SIZE 8
#define CELL_SIZE (TILE_SIZE*2)
#define BORDER_SIZE CELL_SIZE
#define STATUS_BAR_HEIGHT CELL_SIZE
// Viewport is the whole screen area except the border
#define VIEWPORT_WIDTH 256
#define VIEWPORT_HEIGHT 192
#define VIEWPORT_LEFT BORDER_SIZE
#define VIEWPORT_TOP BORDER_SIZE
#define VIEWPORT_RIGHT (VIEWPORT_LEFT + VIEWPORT_WIDTH - 1)
#define VIEWPORT_BOTTOM (VIEWPORT_TOP + VIEWPORT_HEIGHT - 1)
// Playfield is the whole viewport except the status bar
#define PLAYFIELD_WIDTH VIEWPORT_WIDTH
#define PLAYFIELD_HEIGHT (VIEWPORT_HEIGHT - STATUS_BAR_HEIGHT)
#define PLAYFIELD_LEFT VIEWPORT_LEFT
#define PLAYFIELD_TOP (VIEWPORT_TOP + STATUS_BAR_HEIGHT)
#define PLAYFIELD_RIGHT (PLAYFIELD_LEFT + PLAYFIELD_WIDTH - 1)
#define PLAYFIELD_BOTTOM (PLAYFIELD_TOP + PLAYFIELD_HEIGHT - 1)
#define PLAYFIELD_HEIGHT_IN_TILES (PLAYFIELD_HEIGHT/TILE_SIZE)
#define PLAYFIELD_WIDTH_IN_TILES (PLAYFIELD_WIDTH/TILE_SIZE)
#define CAMERA_START_LEFT (PLAYFIELD_LEFT + 6*TILE_SIZE)
#define CAMERA_STOP_LEFT (PLAYFIELD_LEFT + 14*TILE_SIZE)
#define CAMERA_START_TOP (PLAYFIELD_TOP + 4*TILE_SIZE)
#define CAMERA_STOP_TOP (PLAYFIELD_TOP + 9*TILE_SIZE)
#define CAMERA_START_RIGHT (PLAYFIELD_RIGHT - 6*TILE_SIZE + 1)
#define CAMERA_STOP_RIGHT (PLAYFIELD_RIGHT - 13*TILE_SIZE + 1)
#define CAMERA_START_BOTTOM (PLAYFIELD_BOTTOM - 4*TILE_SIZE + 1)
#define CAMERA_STOP_BOTTOM (PLAYFIELD_BOTTOM - 9*TILE_SIZE + 1)
#define CAMERA_X_MIN 0
#define CAMERA_Y_MIN 0
#define CAMERA_X_MAX (CAVE_WIDTH*CELL_SIZE - PLAYFIELD_WIDTH)
#define CAMERA_Y_MAX (CAVE_HEIGHT*CELL_SIZE - PLAYFIELD_HEIGHT)
#define CAMERA_STEP TILE_SIZE
// Backbuffer has 4 bits per pixel
#define BACKBUFFER_WIDTH (VIEWPORT_WIDTH + BORDER_SIZE*2)
#define BACKBUFFER_HEIGHT (VIEWPORT_HEIGHT + BORDER_SIZE*2)
#define BACKBUFFER_BYTES (BACKBUFFER_WIDTH*BACKBUFFER_HEIGHT/2)
typedef enum {
OBJ_SPACE = 0x00,
OBJ_DIRT = 0x01,
OBJ_BRICK_WALL = 0x02,
OBJ_MAGIC_WALL = 0x03,
OBJ_PRE_OUTBOX = 0x04,
OBJ_FLASHING_OUTBOX = 0x05,
OBJ_STEEL_WALL = 0x07,
OBJ_FIREFLY_LEFT = 0x08,
OBJ_FIREFLY_UP = 0x09,
OBJ_FIREFLY_RIGHT = 0x0A,
OBJ_FIREFLY_DOWN = 0x0B,
OBJ_FIREFLY_LEFT_SCANNED = 0x0C,
OBJ_FIREFLY_UP_SCANNED = 0x0D,
OBJ_FIREFLY_RIGHT_SCANNED = 0x0E,
OBJ_FIREFLY_DOWN_SCANNED = 0x0F,
OBJ_BOULDER_STATIONARY = 0x10,
OBJ_BOULDER_STATIONARY_SCANNED = 0x11,
OBJ_BOULDER_FALLING = 0x12,
OBJ_BOULDER_FALLING_SCANNED = 0x13,
OBJ_DIAMOND_STATIONARY = 0x14,
OBJ_DIAMOND_STATIONARY_SCANNED = 0x15,
OBJ_DIAMOND_FALLING = 0x16,
OBJ_DIAMOND_FALLING_SCANNED = 0x17,
OBJ_EXPLODE_TO_SPACE_0 = 0x1B,
OBJ_EXPLODE_TO_SPACE_1 = 0x1C,
OBJ_EXPLODE_TO_SPACE_2 = 0x1D,
OBJ_EXPLODE_TO_SPACE_3 = 0x1E,
OBJ_EXPLODE_TO_SPACE_4 = 0x1F,
OBJ_EXPLODE_TO_DIAMOND_0 = 0x20,
OBJ_EXPLODE_TO_DIAMOND_1 = 0x21,
OBJ_EXPLODE_TO_DIAMOND_2 = 0x22,
OBJ_EXPLODE_TO_DIAMOND_3 = 0x23,
OBJ_EXPLODE_TO_DIAMOND_4 = 0x24,
OBJ_PRE_ROCKFORD_1 = 0x25,
OBJ_PRE_ROCKFORD_2 = 0x26,
OBJ_PRE_ROCKFORD_3 = 0x27,
OBJ_PRE_ROCKFORD_4 = 0x28,
OBJ_BUTTERFLY_DOWN = 0x30,
OBJ_BUTTERFLY_LEFT = 0x31,
OBJ_BUTTERFLY_UP = 0x32,
OBJ_BUTTERFLY_RIGHT = 0x33,
OBJ_BUTTERFLY_DOWN_SCANNED = 0x34,
OBJ_BUTTERFLY_LEFT_SCANNED = 0x35,
OBJ_BUTTERFLY_UP_SCANNED = 0x36,
OBJ_BUTTERFLY_RIGHT_SCANNED = 0x37,
OBJ_ROCKFORD = 0x38,
OBJ_ROCKFORD_SCANNED = 0x39,
OBJ_AMOEBA = 0x3A,
OBJ_AMOEBA_SCANNED = 0x3B,
} Object;
typedef enum {
OBJST_SINGLE,
OBJST_LINE,
OBJST_FILLED_RECT,
OBJST_RECT,
} ObjectStructure;
#define CAVE_HEIGHT 22
#define CAVE_WIDTH 40
#define NUM_DIFFICULTY_LEVELS 5
#define NUM_RANDOM_OBJECTS 4
typedef struct {
uint8_t caveNumber;
uint8_t magicWallMillingTime; // also amoebaSlowGrowthTime
uint8_t initialDiamondValue;
uint8_t extraDiamondValue;
uint8_t randomiserSeed[NUM_DIFFICULTY_LEVELS];
uint8_t diamondsNeeded[NUM_DIFFICULTY_LEVELS];
uint8_t caveTime[NUM_DIFFICULTY_LEVELS];
uint8_t backgroundColor1;
uint8_t backgroundColor2;
uint8_t foregroundColor;
uint8_t unused[2];
uint8_t randomObject[NUM_RANDOM_OBJECTS];
uint8_t objectProbability[NUM_RANDOM_OBJECTS];
} CaveInfo;
typedef enum {
CAVE_A, CAVE_B, CAVE_C, CAVE_D, INTERMISSION_1,
CAVE_E, CAVE_F, CAVE_G, CAVE_H, INTERMISSION_2,
CAVE_I, CAVE_J, CAVE_K, CAVE_L, INTERMISSION_3,
CAVE_M, CAVE_N, CAVE_O, CAVE_P, INTERMISSION_4,
CAVE_COUNT,
} CaveName;
typedef enum {BLACK, GRAY, WHITE, RED, YELLOW, GREEN, BLUE, PURPLE, CYAN, COLOR_COUNT} Color;
typedef struct {
Color boulderFg;
Color brickWallFg;
Color brickWallBg;
Color dirtFg;
Color flyFg;
Color flyBg;
} CaveColors;
typedef enum {UP, DOWN, LEFT, RIGHT, DIRECTION_COUNT} Direction;
typedef enum {TURN_LEFT, STRAIGHT_AHEAD, TURN_RIGHT} Turning;
typedef enum {MAGIC_WALL_OFF, MAGIC_WALL_ON, MAGIC_WALL_EXPIRED} MagicWallStatus;
//
// Global variables
//
uint8_t *backbuffer;
uint8_t map[CAVE_HEIGHT][CAVE_WIDTH];
CaveInfo *caveInfo;
int turnsSinceRockfordSeenAlive;
bool isOutOfTime;
int livesLeft;
int difficultyLevel;
int score;
int scoreTillBonusLife;
int spaceFlashingTurnsLeft;
int currentCaveNumber;
MagicWallStatus magicWallStatus;
///////////////
void debugPrint(char *format, ...) {
va_list argptr;
va_start(argptr, format);
char str[1024];
vsprintf_s(str, sizeof(str), format, argptr);
va_end(argptr);
OutputDebugString(str);
}
//
// Graphics
//
void setPixel(int x, int y, Color color) {
assert(color < COLOR_COUNT);
assert((color & 0xF0) == 0);
int pixelOffset = y*BACKBUFFER_WIDTH + x;
int byteOffset = pixelOffset / 2;
assert(byteOffset >= 0 && byteOffset < BACKBUFFER_BYTES);
Color oldColor = backbuffer[byteOffset];
Color newColor;
if (pixelOffset % 2 == 0) {
newColor = (color << 4) | (oldColor & 0x0F);
} else {
newColor = (oldColor & 0xF0) | color;
}
backbuffer[byteOffset] = newColor;
}
void drawRect(int left, int top, int right, int bottom, Color color) {
for (int x = left; x <= right; ++x) {
setPixel(x, top, color);
setPixel(x, bottom, color);
}
for (int y = top; y <= bottom; ++y) {
setPixel(left, y, color);
setPixel(right, y, color);
}
}
void drawFilledRect(int left, int top, int right, int bottom, Color color) {
for (int y = top; y <= bottom; ++y) {
for (int x = left; x <= right; ++x) {
setPixel(x, y, color);
}
}
}
void drawTile(uint8_t *tile, int dstX, int dstY, Color fgColor, Color bgColor, int vOffset) {
for (uint8_t bmpY = 0; bmpY < TILE_SIZE; ++bmpY) {
int y = dstY + bmpY;
uint8_t byte = tile[(bmpY + vOffset) % TILE_SIZE];
for (uint8_t bmpX = 0; bmpX < TILE_SIZE; ++bmpX) {
int x = dstX + bmpX;
uint8_t mask = 1 << ((TILE_SIZE-1) - bmpX);
uint8_t bit = byte & mask;
Color color = bit ? fgColor : bgColor;
setPixel(x, y, color);
}
}
}
void drawSprite(uint8_t *sprite, int frame, int dstX, int dstY, Color fgColor, Color bgColor, int vOffset) {
int frames = sprite[0];
int size = sprite[1];
int bytesPerFrame = size*size*TILE_SIZE;
int bytesPerRow = size*TILE_SIZE;
for (int row = 0; row < size; ++row) {
for (int col = 0; col < size; ++col) {
int x = dstX + col*TILE_SIZE;
int y = dstY + row*TILE_SIZE;
if (x >= VIEWPORT_LEFT && (x+TILE_SIZE-1) <= VIEWPORT_RIGHT &&
y >= VIEWPORT_TOP && (y+TILE_SIZE-1) <= VIEWPORT_BOTTOM) {
uint8_t *data = sprite + 2 + (frame%frames)*bytesPerFrame + row*bytesPerRow + col*TILE_SIZE;
drawTile(data, x, y, fgColor, bgColor, vOffset);
}
}
}
}
//
// Cave decoding
//
void nextRandom(int *randSeed1, int *randSeed2) {
int tempRand1 = (*randSeed1 & 0x0001) * 0x0080;
int tempRand2 = (*randSeed2 >> 1) & 0x007F;
int result = (*randSeed2) + (*randSeed2 & 0x0001) * 0x0080;
int carry = (result > 0x00FF);
result = result & 0x00FF;
result = result + carry + 0x13;
carry = (result > 0x00FF);
*randSeed2 = result & 0x00FF;
result = *randSeed1 + carry + tempRand1;
carry = (result > 0x00FF);
result = result & 0x00FF;
result = result + carry + tempRand2;
*randSeed1 = result & 0x00FF;
}
void placeObjectLine(Object object, int row, int col, int length, int direction) {
int ldx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int ldy[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
for (int i = 0; i < length; i++) {
map[row + i*ldy[direction]][col + i*ldx[direction]] = object;
}
}
void placeObjectFilledRect(Object object, int row, int col, int width, int height, Object fillObject) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (y == 0 || y == height-1 || x == 0 || x == width-1) {
map[row+y][col+x] = object;
} else {
map[row+y][col+x] = fillObject;
}
}
}
}
void placeObjectRect(Object object, int row, int col, int width, int height) {
for (int i = 0; i < width; i++) {
map[row][col+i] = object;
map[row+height-1][col+i] = object;
}
for (int i = 0; i < height; i++) {
map[row+i][col] = object;
map[row+i][col+width-1] = object;
}
}
void decodeCave(int caveIndex) {
uint8_t *caves[CAVE_COUNT] = {
caveA, caveB, caveC, caveD, intermission1,
caveE, caveF, caveG, caveH, intermission2,
caveI, caveJ, caveK, caveL, intermission3,
caveM, caveN, caveO, caveP, intermission4,
};
assert(caveIndex >= 0 && caveIndex < CAVE_COUNT);
caveInfo = (CaveInfo *)caves[caveIndex];
// Clear out the map
for (int row = 0; row < CAVE_HEIGHT; row++) {
for (int col = 0; col < CAVE_WIDTH; col++) {
map[row][col] = OBJ_STEEL_WALL;
}
}
// Decode random map objects
{
int randSeed1 = 0;
int randSeed2 = caveInfo->randomiserSeed[0];
for (int row = 1; row < CAVE_HEIGHT; row++) {
for (int col = 0; col < CAVE_WIDTH; col++) {
Object object = OBJ_DIRT;
nextRandom(&randSeed1, &randSeed2);
for (int i = 0; i < NUM_RANDOM_OBJECTS; i++) {
if (randSeed1 < caveInfo->objectProbability[i]) {
object = caveInfo->randomObject[i];
}
}
map[row][col] = object;
}
}
}
// Steel bounds
placeObjectRect(OBJ_STEEL_WALL, 0, 0, CAVE_WIDTH, CAVE_HEIGHT);
// Decode explicit map data
{
uint8_t *explicitData = caves[caveIndex] + sizeof(CaveInfo);
int uselessTopBorderHeight = 2;
for (int i = 0; explicitData[i] != 0xFF; i++) {
Object object = (explicitData[i] & 0x3F);
switch (3 & (explicitData[i] >> 6)) {
case OBJST_SINGLE: {
int col = explicitData[++i];
int row = explicitData[++i] - uselessTopBorderHeight;
map[row][col] = object;
break;
}
case OBJST_LINE: {
int col = explicitData[++i];
int row = explicitData[++i] - uselessTopBorderHeight;
int length = explicitData[++i];
int direction = explicitData[++i];
placeObjectLine(object, row, col, length, direction);
break;
}
case OBJST_FILLED_RECT: {
int col = explicitData[++i];
int row = explicitData[++i] - uselessTopBorderHeight;
int width = explicitData[++i];
int height = explicitData[++i];
Object fill = explicitData[++i];
placeObjectFilledRect(object, row, col, width, height, fill);
break;
}
case OBJST_RECT: {
int col = explicitData[++i];
int row = explicitData[++i] - uselessTopBorderHeight;
int width = explicitData[++i];
int height = explicitData[++i];
placeObjectRect(object, row, col, width, height);
break;
}
}
}
}
}
//
// Gameplay
//
bool isObjectRound(Object object) {
return object == OBJ_BOULDER_STATIONARY || object == OBJ_DIAMOND_STATIONARY || object == OBJ_BRICK_WALL;
}
bool isObjectExplosive(Object object) {
return object == OBJ_ROCKFORD ||
object == OBJ_FIREFLY_LEFT ||
object == OBJ_FIREFLY_UP ||
object == OBJ_FIREFLY_RIGHT ||
object == OBJ_FIREFLY_DOWN ||
object == OBJ_BUTTERFLY_DOWN ||
object == OBJ_BUTTERFLY_LEFT ||
object == OBJ_BUTTERFLY_UP ||
object == OBJ_BUTTERFLY_RIGHT;
}
bool explodesToDiamonds(Object object) {
assert(isObjectExplosive(object));
return object == OBJ_BUTTERFLY_DOWN || object == OBJ_BUTTERFLY_LEFT || object == OBJ_BUTTERFLY_UP || object == OBJ_BUTTERFLY_RIGHT;
}
void explodeCell(int row, int col, bool toDiamonds, int stage) {
if (map[row][col] != OBJ_STEEL_WALL) {
if (toDiamonds) {
map[row][col] = stage == 0 ? OBJ_EXPLODE_TO_DIAMOND_0 : OBJ_EXPLODE_TO_DIAMOND_1;
} else {
map[row][col] = stage == 0 ? OBJ_EXPLODE_TO_SPACE_0 : OBJ_EXPLODE_TO_SPACE_1;
}
}
}
void explode(int atRow, int atCol, int scanRow, int scanCol) {
bool toDiamonds = explodesToDiamonds(map[atRow][atCol]);
for (int row = atRow-1; row <= atRow+1; ++row) {
for (int col = atCol-1; col <= atCol+1; ++col) {
int stage = ((row < scanRow) || (row == scanRow && col <= scanCol)) ? 1 : 0;
explodeCell(row, col, toDiamonds, stage);
}
}
}
void updateBoulderAndDiamond(SoundSystem *sys, int row, int col, bool isFalling, bool isBoulder) {
Object fallingScannedObj = isBoulder ? OBJ_BOULDER_FALLING_SCANNED : OBJ_DIAMOND_FALLING_SCANNED;
Object stationaryScannedObj = isBoulder ? OBJ_BOULDER_STATIONARY_SCANNED : OBJ_DIAMOND_STATIONARY_SCANNED;
Object fallingScannedObjInvert = isBoulder ? OBJ_DIAMOND_FALLING_SCANNED : OBJ_BOULDER_FALLING_SCANNED;
if (map[row+1][col] == OBJ_SPACE) {
map[row+1][col] = fallingScannedObj;
map[row][col] = OBJ_SPACE;
if (!isFalling) {
playSound(sys, isBoulder ? SND_BOULDER : SND_DIAMOND);
}
} else if (isFalling && map[row+1][col] == OBJ_MAGIC_WALL) {
if (magicWallStatus == MAGIC_WALL_OFF) {
magicWallStatus = MAGIC_WALL_ON;
}
if (magicWallStatus == MAGIC_WALL_ON && map[row+2][col] == OBJ_SPACE) {
map[row+2][col] = fallingScannedObjInvert;
}
map[row][col] = OBJ_SPACE;
} else if (isObjectRound(map[row+1][col])) {
// Try to roll off
if (map[row][col-1] == OBJ_SPACE && map[row+1][col-1] == OBJ_SPACE) {
// Roll left
map[row][col-1] = fallingScannedObj;
map[row][col] = OBJ_SPACE;
} else if (map[row][col+1] == OBJ_SPACE && map[row+1][col+1] == OBJ_SPACE) {
// Roll right
map[row][col+1] = fallingScannedObj;
map[row][col] = OBJ_SPACE;
} else {
map[row][col] = stationaryScannedObj;
if (isFalling) {
playSound(sys, isBoulder ? SND_BOULDER : SND_DIAMOND);
}
}
} else if (isFalling && isObjectExplosive(map[row+1][col])) {
explode(row+1, col, row, col);
} else {
map[row][col] = stationaryScannedObj;
if (isFalling) {
playSound(sys, isBoulder ? SND_BOULDER : SND_DIAMOND);
}
}
}
bool isFailed() {
return turnsSinceRockfordSeenAlive >= 16 || isOutOfTime;
}
bool checkFlyExplode(Object object) {
return object == OBJ_ROCKFORD || object == OBJ_ROCKFORD_SCANNED || object == OBJ_AMOEBA;
}
void getNewFlyPosition(int curRow, int curCol, Direction curDirection, Turning turning, int *newRow, int *newCol, Direction *newDirection) {
*newRow = curRow;
*newCol = curCol;
switch(curDirection) {
case UP:
switch (turning) {
case TURN_LEFT: (*newCol)--; *newDirection = LEFT; break;
case STRAIGHT_AHEAD: (*newRow)--; *newDirection = UP; break;
case TURN_RIGHT: (*newCol)++; *newDirection = RIGHT; break;
}
break;
case DOWN:
switch (turning) {
case TURN_LEFT: (*newCol)++; *newDirection = RIGHT; break;
case STRAIGHT_AHEAD: (*newRow)++; *newDirection = DOWN; break;
case TURN_RIGHT: (*newCol)--; *newDirection = LEFT; break;
}
break;
case LEFT:
switch (turning) {
case TURN_LEFT: (*newRow)++; *newDirection = DOWN; break;
case STRAIGHT_AHEAD: (*newCol)--; *newDirection = LEFT; break;
case TURN_RIGHT: (*newRow)--; *newDirection = UP; break;
}
break;
case RIGHT:
switch (turning) {
case TURN_LEFT: (*newRow)--; *newDirection = UP; break;
case STRAIGHT_AHEAD: (*newCol)++; *newDirection = RIGHT; break;
case TURN_RIGHT: (*newRow)++; *newDirection = DOWN; break;
}
break;
}
}
Object getFlyScanned(Direction direction, bool isFirefly) {
switch (direction) {
case UP : return isFirefly ? OBJ_FIREFLY_UP_SCANNED : OBJ_BUTTERFLY_UP_SCANNED;
case DOWN : return isFirefly ? OBJ_FIREFLY_DOWN_SCANNED : OBJ_BUTTERFLY_DOWN_SCANNED;
case LEFT : return isFirefly ? OBJ_FIREFLY_LEFT_SCANNED : OBJ_BUTTERFLY_LEFT_SCANNED;
case RIGHT : return isFirefly ? OBJ_FIREFLY_RIGHT_SCANNED : OBJ_BUTTERFLY_RIGHT_SCANNED;
}
}
Direction getFlyDirection(Object fly, bool isFirefly) {
if (isFirefly) {
switch (fly) {
case OBJ_FIREFLY_UP: return UP;
case OBJ_FIREFLY_DOWN: return DOWN;
case OBJ_FIREFLY_LEFT: return LEFT;
case OBJ_FIREFLY_RIGHT: return RIGHT;
}
} else {
switch (fly) {
case OBJ_BUTTERFLY_UP: return UP;
case OBJ_BUTTERFLY_DOWN: return DOWN;
case OBJ_BUTTERFLY_LEFT: return LEFT;
case OBJ_BUTTERFLY_RIGHT: return RIGHT;
}
}
}
void updateFly(int row, int col, bool isFirefly) {
if (checkFlyExplode(map[row-1][col]) || checkFlyExplode(map[row+1][col]) ||
checkFlyExplode(map[row][col-1]) || checkFlyExplode(map[row][col+1])) {
explode(row, col, row, col);
} else {
int direction = getFlyDirection(map[row][col], isFirefly);
int newRow, newCol;
Direction newDirection;
getNewFlyPosition(row, col, direction, (isFirefly ? TURN_LEFT : TURN_RIGHT), &newRow, &newCol, &newDirection);
if (map[newRow][newCol] == OBJ_SPACE) {
map[newRow][newCol] = getFlyScanned(newDirection, isFirefly);
map[row][col] = OBJ_SPACE;
} else {
getNewFlyPosition(row, col, direction, STRAIGHT_AHEAD, &newRow, &newCol, &newDirection);
if (map[newRow][newCol] == OBJ_SPACE) {
map[newRow][newCol] = getFlyScanned(newDirection, isFirefly);
map[row][col] = OBJ_SPACE;
} else {
getNewFlyPosition(row, col, direction, (isFirefly ? TURN_RIGHT : TURN_LEFT), &newRow, &newCol, &newDirection);
map[row][col] = getFlyScanned(newDirection, isFirefly);
}
}
}
}
void addScore(int amount) {
score += amount;
// Check for bonus life
scoreTillBonusLife -= amount;
if (scoreTillBonusLife <= 0) {
scoreTillBonusLife += BONUS_LIFE_COST;
spaceFlashingTurnsLeft = SPACE_FLASHING_TURNS;
++livesLeft;
if (livesLeft > MAX_LIVES) {
livesLeft = MAX_LIVES;
}
}
}
bool isIntermission() {
return ((currentCaveNumber + 1) % 5) == 0;
}
void incrementCaveNumber() {
++currentCaveNumber;
if (currentCaveNumber >= CAVE_COUNT) {
currentCaveNumber = 0;
if (difficultyLevel < NUM_DIFFICULTY_LEVELS-1) {
++difficultyLevel;
}
}
}
char getCurrentCaveLetter() {
switch (currentCaveNumber) {
case CAVE_A: return 'A';
case CAVE_B: return 'B';
case CAVE_C: return 'C';
case CAVE_D: return 'D';
case CAVE_E: return 'E';
case CAVE_F: return 'F';
case CAVE_G: return 'G';
case CAVE_H: return 'H';
case CAVE_I: return 'I';
case CAVE_J: return 'J';
case CAVE_K: return 'K';
case CAVE_L: return 'L';
case CAVE_M: return 'M';
case CAVE_N: return 'N';
case CAVE_O: return 'O';
case CAVE_P: return 'P';
}
return ' ';
}
bool canAmoebaGrowHere(int row, int col) {
return map[row][col] == OBJ_SPACE || map[row][col] == OBJ_DIRT;
}
void getRandomCellNear(int row, int col, int *newRow, int *newCol) {
*newRow = row;
*newCol = col;
switch (rand() % DIRECTION_COUNT) {
case UP: (*newRow)--; break;
case DOWN: (*newRow)++; break;
case LEFT: (*newCol)--; break;
case RIGHT: (*newCol)++; break;
}
}
////////////////
bool isKeyDown(uint8_t virtKey) {
return GetFocus() && (GetKeyState(virtKey) & 0x8000);
}
LRESULT CALLBACK wndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(wnd, msg, wparam, lparam);
}
return 0;
}
int CALLBACK WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdShow) {
UNREFERENCED_PARAMETER(prevInst);
UNREFERENCED_PARAMETER(cmdLine);
WNDCLASS wndClass = {0};
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = wndProc;
wndClass.hInstance = inst;
wndClass.hCursor = LoadCursor(0, IDC_ARROW);
wndClass.lpszClassName = "Boulder Dash";
RegisterClass(&wndClass);
int windowScale = 3;
int windowWidth = BACKBUFFER_WIDTH * windowScale;
int windowHeight = BACKBUFFER_HEIGHT * windowScale;
RECT crect = {0};
crect.right = windowWidth;
crect.bottom = windowHeight;
DWORD wndStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
AdjustWindowRect(&crect, wndStyle, 0);
HWND wnd = CreateWindowEx(0, wndClass.lpszClassName, "Boulder Dash", wndStyle, 300, 100,
crect.right - crect.left, crect.bottom - crect.top,
0, 0, inst, 0);
ShowWindow(wnd, cmdShow);
UpdateWindow(wnd);
//
// Initialize graphics
//
HDC deviceContext = GetDC(wnd);
backbuffer = malloc(BACKBUFFER_BYTES);
BITMAPINFO *bitmapInfo = malloc(sizeof(BITMAPINFOHEADER) + (COLOR_COUNT * sizeof(RGBQUAD)));
bitmapInfo->bmiHeader.biSize = sizeof(bitmapInfo->bmiHeader);
bitmapInfo->bmiHeader.biWidth = BACKBUFFER_WIDTH;
bitmapInfo->bmiHeader.biHeight = -BACKBUFFER_HEIGHT;
bitmapInfo->bmiHeader.biPlanes = 1;
bitmapInfo->bmiHeader.biBitCount = 4;
bitmapInfo->bmiHeader.biCompression = BI_RGB;
bitmapInfo->bmiHeader.biClrUsed = COLOR_COUNT;
RGBQUAD black = {0x00, 0x00, 0x00, 0x00};
RGBQUAD red = {0x00, 0x00, 0xCC, 0x00};
RGBQUAD green = {0x00, 0xCC, 0x00, 0x00};
RGBQUAD yellow = {0x00, 0xCC, 0xCC, 0x00};
RGBQUAD blue = {0xCC, 0x00, 0x00, 0x00};
RGBQUAD purple = {0xCC, 0x00, 0xCC, 0x00};
RGBQUAD cyan = {0xCC, 0xCC, 0x00, 0x00};
RGBQUAD gray = {0xCC, 0xCC, 0xCC, 0x00};
RGBQUAD white = {0xFF, 0xFF, 0xFF, 0x00};
bitmapInfo->bmiColors[BLACK] = black;
bitmapInfo->bmiColors[RED] = red;
bitmapInfo->bmiColors[GREEN] = green;
bitmapInfo->bmiColors[YELLOW] = yellow;
bitmapInfo->bmiColors[BLUE] = blue;
bitmapInfo->bmiColors[PURPLE] = purple;
bitmapInfo->bmiColors[CYAN] = cyan;
bitmapInfo->bmiColors[GRAY] = gray;
bitmapInfo->bmiColors[WHITE] = white;
//
// Clock
//
float dt = 0.0f;
float targetFps = 60.0f;
float maxDt = 1.0f / targetFps;
LARGE_INTEGER perfcFreq = {0};
LARGE_INTEGER perfc = {0};
LARGE_INTEGER perfcPrev = {0};
QueryPerformanceFrequency(&perfcFreq);
QueryPerformanceCounter(&perfc);
//
// Initialize cave colors
//
CaveColors caveColors[CAVE_COUNT] = {0};
caveColors[CAVE_A].boulderFg = YELLOW;
caveColors[CAVE_A].brickWallFg = GRAY;
caveColors[CAVE_A].brickWallBg = RED;
caveColors[CAVE_A].dirtFg = RED;
caveColors[CAVE_B].boulderFg = BLUE;
caveColors[CAVE_B].brickWallFg = BLUE;
caveColors[CAVE_B].brickWallBg = YELLOW;
caveColors[CAVE_B].dirtFg = PURPLE;
caveColors[CAVE_B].flyFg = BLUE;
caveColors[CAVE_B].flyBg = YELLOW;
caveColors[CAVE_C].boulderFg = PURPLE;
caveColors[CAVE_C].brickWallFg = GRAY;
caveColors[CAVE_C].brickWallBg = PURPLE;
caveColors[CAVE_C].dirtFg = GREEN;
caveColors[CAVE_D].boulderFg = YELLOW;
caveColors[CAVE_D].dirtFg = BLUE;
caveColors[CAVE_D].flyFg = CYAN;
caveColors[CAVE_D].flyBg = BLACK;
caveColors[INTERMISSION_1].boulderFg = PURPLE;
caveColors[INTERMISSION_1].dirtFg = BLUE;
caveColors[INTERMISSION_1].flyFg = BLUE;
caveColors[INTERMISSION_1].flyBg = BLACK;
caveColors[CAVE_E].boulderFg = PURPLE;
caveColors[CAVE_E].dirtFg = RED;
caveColors[CAVE_E].flyFg = RED;
caveColors[CAVE_E].flyBg = YELLOW;
caveColors[CAVE_F].boulderFg = PURPLE;
caveColors[CAVE_F].brickWallFg = PURPLE;
caveColors[CAVE_F].brickWallBg = GRAY;
caveColors[CAVE_F].dirtFg = BLUE;
caveColors[CAVE_F].flyFg = PURPLE;
caveColors[CAVE_F].flyBg = GRAY;
caveColors[CAVE_G].boulderFg = PURPLE;
caveColors[CAVE_G].brickWallFg = RED;
caveColors[CAVE_G].brickWallBg = GRAY;
caveColors[CAVE_G].dirtFg = RED;
caveColors[CAVE_G].flyFg = PURPLE;
caveColors[CAVE_G].flyBg = YELLOW;
caveColors[CAVE_H].boulderFg = CYAN;
caveColors[CAVE_H].brickWallFg = CYAN;
caveColors[CAVE_H].brickWallBg = GRAY;
caveColors[CAVE_H].dirtFg = RED;
caveColors[CAVE_H].flyFg = BLUE;
caveColors[CAVE_H].flyBg = CYAN;
caveColors[INTERMISSION_2].boulderFg = YELLOW;
caveColors[INTERMISSION_2].dirtFg = BLUE;
caveColors[INTERMISSION_2].flyFg = RED;
caveColors[INTERMISSION_2].flyBg = GRAY;
caveColors[CAVE_I].boulderFg = BLUE;
caveColors[CAVE_I].brickWallFg = BLUE;
caveColors[CAVE_I].brickWallBg = CYAN;
caveColors[CAVE_I].dirtFg = PURPLE;
caveColors[CAVE_J].boulderFg = RED;
caveColors[CAVE_J].brickWallFg = BLUE;
caveColors[CAVE_J].brickWallBg = GRAY;
caveColors[CAVE_J].dirtFg = BLUE;
caveColors[CAVE_J].flyFg = RED;
caveColors[CAVE_J].flyBg = GRAY;
caveColors[CAVE_K].boulderFg = YELLOW;
caveColors[CAVE_K].brickWallFg = GRAY;
caveColors[CAVE_K].brickWallBg = YELLOW;
caveColors[CAVE_K].dirtFg = GREEN;
caveColors[CAVE_K].flyFg = GREEN;
caveColors[CAVE_K].flyBg = GRAY;
caveColors[CAVE_L].boulderFg = YELLOW;
caveColors[CAVE_L].brickWallFg = YELLOW;
caveColors[CAVE_L].brickWallBg = GRAY;
caveColors[CAVE_L].dirtFg = GREEN;
caveColors[CAVE_L].flyFg = GREEN;
caveColors[CAVE_L].flyBg = GRAY;
caveColors[INTERMISSION_3].boulderFg = GREEN;
caveColors[INTERMISSION_3].flyFg = GREEN;
caveColors[INTERMISSION_3].flyBg = RED;
caveColors[CAVE_M].boulderFg = RED;
caveColors[CAVE_M].brickWallFg = BLACK;
caveColors[CAVE_M].brickWallBg = GRAY;
caveColors[CAVE_M].dirtFg = BLUE;
caveColors[CAVE_M].flyFg = YELLOW;
caveColors[CAVE_M].flyBg = BLACK;
caveColors[CAVE_N].boulderFg = YELLOW;
caveColors[CAVE_N].dirtFg = PURPLE;
caveColors[CAVE_N].flyFg = YELLOW;
caveColors[CAVE_N].flyBg = BLACK;
caveColors[CAVE_O].boulderFg = BLUE;
caveColors[CAVE_O].brickWallFg = BLACK;
caveColors[CAVE_O].brickWallBg = GRAY;
caveColors[CAVE_O].dirtFg = PURPLE;
caveColors[CAVE_O].flyFg = BLUE;
caveColors[CAVE_O].flyBg = GRAY;
caveColors[CAVE_P].boulderFg = CYAN;
caveColors[CAVE_P].brickWallFg = PURPLE;
caveColors[CAVE_P].brickWallBg = GRAY;
caveColors[CAVE_P].dirtFg = RED;
caveColors[CAVE_P].flyFg = PURPLE;
caveColors[CAVE_P].flyBg = GRAY;
caveColors[INTERMISSION_4].boulderFg = GREEN;
caveColors[INTERMISSION_4].brickWallFg = GRAY;
caveColors[INTERMISSION_4].brickWallBg = GREEN;
caveColors[INTERMISSION_4].dirtFg = YELLOW;
//
// Initialize game
//
bool cellCover[CAVE_HEIGHT][CAVE_WIDTH];
bool tileCover[PLAYFIELD_HEIGHT_IN_TILES][PLAYFIELD_WIDTH_IN_TILES];
char statusBarText[PLAYFIELD_WIDTH_IN_TILES];
CaveColors curColors;
int turn = 0;
int tick = 0;
float tickTimer = 0;
float tickDuration = DEV_SLOW_TICK_DURATION ? 0.15f : 0.03375f;
bool isGameStart = true;
int turnsTillGameRestart = 0;
int turnsTillExitingCave = 0;
bool isAddingTimeToScore = false;
Color normalBorderColor = BLACK;
Color flashBorderColor = GRAY;
Color borderColor = normalBorderColor;
int cameraX = 0;
int cameraY = 0;
int cameraVelX = 0;
int cameraVelY = 0;
//
// These variables are initialized when game starts
//
bool isCaveStart;
int pauseTurnsLeft;
//
// These variables are initialized when cave starts
//
bool isExitingCave;
int caveTimeLeft;
int ticksTillNextCaveSecond;
bool isOutOfTimeTextShown;
int outOfTimeTurn = 0;
int diamondsCollected;
int currentDiamondValue;
int rockfordTurnsTillBirth;
int cellCoverTurnsLeft;
int tileCoverTicksLeft;
int rockfordCol;
int rockfordRow;
bool rockfordIsBlinking;
bool rockfordIsTapping;
bool rockfordIsMoving;
bool rockfordIsFacingRight;
int amoebaSlowGrowthTimeLeft;
int magicWallMillingTimeLeft;
int numberOfAmoebaFoundThisTurn;
int totalAmoebaFoundLastTurn;
bool amoebaSuffocatedLastTurn;
bool atLeastOneAmoebaFoundThisTurnWhichCanGrow;
//
// Initialize sound
//
SoundSystem soundSystem = {0};
initializeSoundSystem(&soundSystem, maxDt, tickDuration);
//
// Game loop
//