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
|
#include <stdint.h>
#include <stdio.h>
#include <dos.h>
#include "keyb.h"
#include "control.h"
#include "vga.h"
#include "sound.h"
#include "text.h"
#include "map.h"
#include "data.h"
#include "timer.h"
#include "entities.h"
#include "tmonster.h"
#include "player.h"
#include "game.h"
#define GAME_LIVES_START 3
#define GAME_LIVES_MAX 9
#define GAME_TIME_MAX 60
#define GAME_MAX_PICKAXE 3
#define GAME_EXTRA_LIFE ((uint16_t)10000)
#define HUD_CLEAN 0
#define HUD_LIVES 1
#define HUD_SCORE 2
#define HUD_STAGE 4
#define HUD_TIME 8
#define HUD_PICKAXE 16
#define HUD_ALL 255
#define GAMEOVER_DELAY 96
#define STAGECLEAR_DELAY 64
static uint32_t hiscore = 15000;
static uint8_t hud;
static volatile uint8_t clock_updated;
/* game variables */
static uint8_t lives;
static uint32_t score;
static uint16_t extra_life;
static uint8_t stage;
static uint8_t time;
static uint8_t pickaxe;
static uint8_t stageclear;
static uint8_t gameover;
static uint8_t pause;
static Entity *tmonster;
static uint8_t continuegame;
/* 0-index */
#define START_STAGE 0
#define STAGES_LEN 30
static const uint8_t *stages[STAGES_LEN] = {
binary_stage01_start, binary_stage02_start, binary_stage03_start, binary_stage04_start,
binary_stage05_start, binary_stage06_start, binary_stage07_start, binary_stage08_start,
binary_stage09_start, binary_stage10_start, binary_stage11_start, binary_stage12_start,
binary_stage13_start, binary_stage14_start, binary_stage15_start, binary_stage16_start,
binary_stage17_start, binary_stage18_start, binary_stage19_start, binary_stage20_start,
binary_stage21_start, binary_stage22_start, binary_stage23_start, binary_stage24_start,
binary_stage25_start, binary_stage26_start, binary_stage27_start, binary_stage28_start,
binary_stage29_start, binary_stage30_start,
};
static void hud_render()
{
char b[32];
if (hud & HUD_ALL)
{
Rect src = { 128, 32, 144, 112 };
Rect dst = { 4, 0, 16, 16 };
/* lives */
blitrc(binary_sprites_start, &src, &dst);
/* pickaxe */
src.x = 112;
dst.x = 90;
blitrc(binary_sprites_start, &src, &dst);
put_text(132, 4, "TIME", 1);
put_text(249, 4, "STAGE", 1);
}
if (hud & HUD_LIVES)
{
sprintf(b, "%d", lives);
put_text(18, 4, b, 1);
}
if (hud & HUD_SCORE)
{
sprintf(b, "%06li", score);
put_text(34, 4, b, 5);
}
if (hud & HUD_PICKAXE)
{
sprintf(b, "%d", pickaxe);
put_text(106, 4, b, 1);
}
if (hud & HUD_TIME)
{
sprintf(b, "%02d", time);
put_text(172, 4, b, time > 10 || stageclear ? 1 : 15);
if (!gameover && !stageclear && time <= 10)
sound_play_efx(EFX_TIME);
}
if (hud & HUD_STAGE)
{
sprintf(b, "%02d", stage < STAGES_LEN ? stage + 1 : stage);
put_text(297, 4, b, 1);
}
hud = HUD_CLEAN;
}
#define WALK_DELAY 8
#define WALK_CYCLE_FRAMES 4
static const Rect intro_frames[WALK_CYCLE_FRAMES] =
{
/* walk cycle */
{ 0, 0, 144, 112 },
{ 16, 0, 144, 112 },
{ 0, 0, 144, 112 },
{ 32, 0, 144, 112 },
};
static void run_intro()
{
/* for the HUD */
pickaxe = 0;
time = GAME_TIME_MAX;
hud = HUD_ALL;
blit_target(TARGET_BUFFER);
blit_erase(0);
map_init(binary_intro_start);
map_render();
hud_render();
put_text(88, 65, "GET ALL THAT GOLD!", 1);
wait_vsync();
blit_copy_all();
Rect dst = { 80, 88 + MAP_OFFS_Y, 16, 16 };
uint8_t frame = 0, delay = 0;
wait_frames(32);
sound_music_pattern(PAT_INTRO);
/* so the music starts */
wait_vsync();
blit_target(TARGET_SCREEN);
while (dst.x < 224)
{
wait_vsync();
blit_copy16(&dst);
dst.x++;
if (map_update_gold(dst.x + 8, dst.y + 15 - MAP_OFFS_Y))
sound_play_efx(EFX_GOLD);
blitrc(binary_sprites_start, &intro_frames[frame], &dst);
if (delay++ == WALK_DELAY)
{
delay = 0;
frame++;
if (frame == WALK_CYCLE_FRAMES)
frame = 0;
}
}
wait_vsync();
blit_copy16(&dst);
wait_frames(32);
sound_music_pattern(PAT_SILENCE);
wait_frames(64);
}
static uint8_t run_gameover()
{
wait_vsync();
blit_erase(0);
/* restore the HUD after erasing the screen */
hud = HUD_ALL;
hud_render();
put_text(124, 90, "GAME OVER", 1);
sound_music_pattern(PAT_GAMEOVER);
/* wait some time, and check if the player wants to continue */
for (uint16_t i = 0; i < 524; i++)
{
if (stage < STAGES_LEN)
{
if (i == 200 || i == 400)
put_text(68, 105, "PRESS SPACE TO CONTINUE", i == 200 ? 5 : 0);
if (i > 200 && i < 400 && keys[KEY_SPACE])
{
sound_music_pattern(PAT_SILENCE);
return 1;
}
}
wait_vsync();
}
sound_music_pattern(PAT_SILENCE);
return 0;
}
static const Rect pop_in_frames[] =
{
{ 128, 16, 144, 112 },
{ 112, 16, 144, 112 }
};
static const Rect pop_in_letters[] =
{
{ 32, 32, 160, 48 },
{ 48, 32, 160, 48 },
{ 64, 32, 160, 48 },
{ 64, 32, 160, 48 },
{ 80, 32, 160, 48 },
{ 96, 32, 160, 48 },
{ 112, 32, 160, 48 },
{ 48, 32, 160, 48 },
};
static void pop_in_letter(uint16_t x, uint16_t y, uint8_t letter)
{
Rect dst = { x, y, 16, 16 };
const Rect *src;
sound_play_efx(EFX_WARP);
for (uint8_t i = 0; i < 2; i++)
{
src = &pop_in_frames[i];
wait_vsync();
blitrc(binary_sprites_start, src, &dst);
wait_frames(7);
}
src = &pop_in_letters[letter];
wait_vsync();
blitrc(binary_tiles_start, src, &dst);
}
static void run_endgame()
{
blit_target(TARGET_BUFFER);
blit_erase(0);
/* for the HUD */
pickaxe = 0;
time = GAME_TIME_MAX;
hud = HUD_ALL;
hud_render();
wait_vsync();
blit_copy_all();
blit_target(TARGET_SCREEN);
for (uint8_t i = 0; i < 4; i++)
pop_in_letter(88 + i * 16, 60, i);
for (uint8_t i = 0; i < 4; i++)
pop_in_letter(168 + i * 16, 60, 4 + i);
wait_vsync();
put_text(48, 100, "YOU HAVE COMPLETED THE GAME!", 1);
put_text(108, 115, "THANK YOU FOR", 6);
put_text(72, 125, "PLAYING GOLD MINE RUN!", 6);
sound_music_pattern(PAT_STAGE);
wait_frames(512);
run_gameover();
}
static void run_stageclear()
{
blit_target(TARGET_BUFFER);
blit_erase(0);
hud = HUD_ALL;
hud_render();
put_text(116, 90, "STAGE", 1);
put_text(164, 90, "CLEAR", 1);
wait_vsync();
blit_copy_all();
blit_target(TARGET_SCREEN);
sound_music_pattern(PAT_STAGE);
/* wait some time */
wait_frames(164);
while (time)
{
add_score(20);
time--;
hud |= HUD_TIME;
wait_frames(4);
wait_vsync();
hud_render();
sound_play_efx(EFX_GOLD);
}
/* wait some time */
wait_frames(64);
}
static uint8_t run_confirm_quit()
{
timer_stop();
blit_target(TARGET_BUFFER);
blit_erase(0);
hud = HUD_ALL;
hud_render();
put_text(108, 90, "ABANDON GAME?", 1);
wait_vsync();
blit_copy_all();
blit_target(TARGET_SCREEN);
uint8_t sel = 0;
uint8_t ctl = CTL_NONE;
uint8_t cooldown = 0;
update_menu:
wait_vsync();
if (sel)
{
put_text(144, 105, " YES", 4);
put_text(144, 115, ">NO", 5);
}
else
{
put_text(144, 105, ">YES", 5);
put_text(144, 115, " NO", 4);
}
while (1)
{
if (cooldown)
cooldown--;
else
{
ctl = control_read();
if ((ctl & CTL_UP) || (ctl & CTL_DOWN))
{
cooldown = 10;
sel ^= 1;
goto update_menu;
}
if (keys[KEY_ENTER] || (ctl & CTL_FIRE1))
{
while (keys[KEY_ENTER] || (ctl & CTL_FIRE1))
{
ctl = control_read();
wait_vsync();
}
break;
}
}
wait_vsync();
}
/* restore the game screen only when not abandoned */
if (sel)
{
blit_target(TARGET_BUFFER);
map_render();
wait_vsync();
blit_copy_all();
blit_target(TARGET_SCREEN);
wait_vsync();
entities_draw();
}
timer_resume();
return sel == 0;
}
void run_game()
{
continuegame = 0;
continue_game:
lives = GAME_LIVES_START;
score = 0;
extra_life = 0;
if (!continuegame)
{
stage = START_STAGE;
run_intro();
}
next_stage:
pause = 0;
gameover = 0;
stageclear = 0;
pickaxe = 0;
time = GAME_TIME_MAX;
hud = HUD_ALL;
tmonster = NULL;
blit_target(TARGET_BUFFER);
blit_erase(0);
hud_render();
put_text(136, 90, "READY?", 1);
wait_vsync();
blit_copy_all();
sound_music_pattern(PAT_READY);
/* wait some time */
wait_frames(164);
map_init(stages[stage]);
map_render();
entities_sort();
wait_vsync();
blit_copy_all();
blit_target(TARGET_SCREEN);
wait_vsync();
entities_draw();
timer_start(GAME_TIME_MAX, &clock_updated);
sound_music_pattern(PAT_PLAY);
while (1)
{
if (clock_updated)
{
time = timer_value();
hud |= HUD_TIME;
}
if (hud)
hud_render();
if (keys[KEY_ESC])
{
if (run_confirm_quit())
break;
}
if (keys[KEY_P])
{
/* pause / resume */
pause ^= 1;
/* wait for the key to be released */
while (keys[KEY_P])
wait_vsync();
if (pause)
{
timer_stop();
sound_mute();
}
else
{
timer_resume();
sound_unmute();
}
}
if (pause)
continue;
if (map_is_complete())
{
if (!stageclear)
{
timer_stop();
tmonster = NULL;
entities_warp_out_all();
sound_play_efx(EFX_WARP);
player_stageclear();
stageclear = STAGECLEAR_DELAY;
}
else
{
if (stageclear == STAGECLEAR_DELAY)
sound_music_pattern(PAT_SILENCE);
stageclear--;
if (stageclear == 1)
{
sound_music_pattern(PAT_SILENCE);
run_stageclear();
stage++;
if (stage == STAGES_LEN)
{
run_endgame();
break;
}
goto next_stage;
}
}
}
else
{
player_update();
/* time monster */
if (!time && !tmonster)
{
tmonster = entities_new();
if (tmonster)
tmonster_init(tmonster);
}
if (time && tmonster)
{
tmonster->used = USED_FREE;
tmonster = NULL;
}
}
entities_update();
entities_sort();
wait_vsync();
/* prevent interrupts so updating the audio doesn't result in sprite
* flickering on the less powered machines */
disable();
entities_draw();
enable();
if (gameover)
{
if (gameover == GAMEOVER_DELAY)
sound_music_pattern(PAT_SILENCE);
gameover--;
if (gameover == 1)
{
if (run_gameover())
{
continuegame = 1;
goto continue_game;
}
break;
}
}
}
sound_music_pattern(PAT_SILENCE);
}
void add_score(uint8_t v)
{
score += v;
extra_life += v;
if (extra_life >= GAME_EXTRA_LIFE)
{
extra_life -= GAME_EXTRA_LIFE;
if (lives < GAME_LIVES_MAX)
{
sound_play_efx(EFX_ONEUP);
lives++;
hud |= HUD_LIVES;
}
}
hud |= HUD_SCORE;
}
uint32_t get_hiscore()
{
return hiscore;
}
uint8_t dec_lives()
{
lives--;
if (lives == 0)
{
gameover = GAMEOVER_DELAY;
timer_stop();
}
hud |= HUD_LIVES;
return lives;
}
void reset_time()
{
time = GAME_TIME_MAX;
hud |= HUD_TIME;
timer_start(GAME_TIME_MAX, &clock_updated);
}
void add_pickaxe()
{
if (pickaxe < GAME_MAX_PICKAXE)
{
pickaxe++;
hud |= HUD_PICKAXE;
}
}
uint8_t use_pickaxe()
{
if (pickaxe)
{
pickaxe--;
hud |= HUD_PICKAXE;
return 1;
}
return 0;
}
uint8_t is_stageclear()
{
return stageclear;
}
|