-
Notifications
You must be signed in to change notification settings - Fork 0
/
65536in1.c
3401 lines (2961 loc) · 86.5 KB
/
65536in1.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 <stdint.h>
#include <stdlib.h>
#include <uzebox.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "data/tileset.inc"
#include "data/rtmaps.inc"
#include "data/marumbi.inc"
#include "data/trollen.inc"
#include "data/theone.inc"
#include "data/patches.inc"
#include "data/zipzipzip.inc"
#define WHITE_NUMBER 16
#define BLUE_NUMBER (-RAM_TILES_COUNT)
#define GREEN_NUMBER (10-RAM_TILES_COUNT)
#define CROSSED_OUT_NUMBER (20-RAM_TILES_COUNT)
#define CPU_DELAY 30
#define STENCH_ROOM 1
#define BREEZE_ROOM 2
#define BOTH_ROOM 3
#define WUMPUS_ROOM 4
#define HOLE_ROOM 5
#define CORRIDOR 6
#define CAVE_WIDTH 12
#define CAVE_HEIGHT 10
#define CAVE_OFFSET_X (VRAM_TILES_H/2-CAVE_WIDTH)
#define CAVE_OFFSET_Y (VRAM_TILES_V/2-CAVE_HEIGHT)
#define MOVE_DELAY 20
#define SHOT_DELAY 6
#define FASTER_DELAY 6
#define NO_TROLL 0
#define V_TROLL 1
#define H_TROLL 2
#define S_TROLL 3
#define TOP_WALL 1
#define RIGHT_WALL 2
#define BOTTOM_WALL 4
#define LEFT_WALL 8
#define MAX_TRAPS 3
#define VICTORY 1
#define DEFEAT 2
#define OUTSIDE 1
#define PEG 2
#define HOLE 3
#define GOOD_CURSOR 0x80
#define BAD_CURSOR 0x40
#define BLINK_DELAY 4
#define MARUMBI_BLUE 235
#define DEFAULT_GRAY 82
#define RED_6 6
#define RED_7 7
#define BROWN 20
#define BLUE_3 (3 << 6)
#define GREEN_3 (3 << 3)
#define TOTAL_CHESTS 26
#define LIMIT_COLOR 126
#define RAM 0
#define FLASH 1
#define COLOR_CHANGE_DELAY 2
#define SKY_COLOR 226
#define TOTAL_CAT_TILES 8
#define CAT_ANIMATION_DELAY 20
#define CAT_MOVE_DELAY 4
const char strGreed[] PROGMEM = "GREED";
const char strPiles[] PROGMEM = "PILES";
const char strGrab[] PROGMEM = "GRAB";
const char str9[] PROGMEM = "9 SQUARES";
const char strGet[] PROGMEM = "GET 3";
const char strGrid[] PROGMEM = "THE GRID";
const char strTrollen [] PROGMEM = "TROLLEN";
const char strEscape[] PROGMEM = "ESCAPE";
const char strAdventure[] PROGMEM = "ADVENTURE";
const char strSlide[] PROGMEM = "SLIDE";
const char strPuzzle[] PROGMEM = "PUZZLE";
const char strAncient[] PROGMEM = "ANCIENT GAME";
const char strOne[] PROGMEM = "THE ONE";
const char strRemove[] PROGMEM = "REMOVE";
const char strTake[] PROGMEM = "TAKE";
const char strMonster[] PROGMEM = "MONSTER";
const char strDungeon[] PROGMEM = "DUNGEON";
const char strHunt[] PROGMEM = "HUNT";
const char strSurvival[] PROGMEM = "SURVIVAL";
const char strAvoider[] PROGMEM = "AVOIDER";
const char strInstinct[] PROGMEM = "INSTINCT";
const char strRain[] PROGMEM = "RAIN";
const char strFalling[] PROGMEM = "FALLING BUTTONS";
const char strReflex[] PROGMEM = "REFLEX";
const char strArray[] PROGMEM = "ARRAY";
const char strSort[] PROGMEM = "SORT";
const char strFast[] PROGMEM = "BE FAST";
const char strRich[] PROGMEM = "RICH";
const char strChests[] PROGMEM = "CHESTS";
const char strDream[] PROGMEM = "DREAM";
const char * const names[] PROGMEM = {
strGreed, strPiles, strGrab,
strGrid, strGet, str9,
strTrollen, strEscape, strAdventure,
strSlide, strPuzzle, strAncient,
strOne, strRemove, strTake,
strMonster, strDungeon, strHunt,
strSurvival, strAvoider, strInstinct,
strRain, strFalling, strReflex,
strArray, strSort, strFast,
strRich, strChests, strDream
};
const char strVsCpu[] PROGMEM = "VS CPU";
const char strVsHuman[] PROGMEM = "VS HUMAN";
const char strExit[] PROGMEM = "EXIT";
const char strCFlavio[] PROGMEM = "\\ 2016 FL^VIO ZAVAN";
const char strLicense[] PROGMEM = "CODE LICENSED UNDER";
const char strMIT[] PROGMEM = "THE MIT LICENSE";
const char strTurn[] PROGMEM = "PLAYER 'S TURN";
const char strWins[] PROGMEM = "PLAYER WINS";
const char strStart[] PROGMEM = "START";
const char strStarting[] PROGMEM = "PICK YOUR CHEST";
const char strOpenChest[] PROGMEM = "OPEN A CHEST";
const char strSee[] PROGMEM = "LET'S SEE WHAT";
const char strInside[] PROGMEM = "WAS INSIDE THE CHEST";
const char strPhone[] PROGMEM = "THE PHONE IS RINGING";
const char strBanker[] PROGMEM = "IT'S THE BANKER";
const char strOffer[] PROGMEM = "HE'S OFFERING YOU";
const char strFor[] PROGMEM = "FOR YOUR CHEST";
const char strAccept[] PROGMEM = "ACCEPT";
const char strRefuse[] PROGMEM = "REFUSE";
const char strReceived[] PROGMEM = "YOU RECEIVED";
const char strWantKeep[] PROGMEM = "DO YOU WANT TO KEEP";
const char strChestNo[] PROGMEM = "CHEST OR DO YOU";
const char strTradeChest[] PROGMEM = "TRADE IT FOR CHEST ?";
const char strKeep[] PROGMEM = "KEEP";
const char strTrade[] PROGMEM = "TRADE";
const char strInsideDef[] PROGMEM = "WAS INSIDE CHEST";
const char strCongrat[] PROGMEM = "CONGRATULATIONS";
const char strPlayer1[] PROGMEM = "PLAYER 1";
const char strPlayer2[] PROGMEM = "PLAYER 2";
const char strGenerating[] PROGMEM = "GENERATING LEVEL";
const char strWait[] PROGMEM = "PLEASE WAIT";
const char strAte[] PROGMEM = "THE MONSTER ATE YOU";
const char strFell[] PROGMEM = "YOU FELL IN A HOLE";
const char strKill[] PROGMEM = "YOU KILLED THE MONSTER";
const char strLives[] PROGMEM = "LIVES:";
const char strScore[] PROGMEM = "SCORE:";
const char strGameOver[] PROGMEM = "GAME OVER";
const char strDraw[] PROGMEM = "DRAW";
const char strDied[] PROGMEM = "YOU DIED";
const char strRainButtons[] PROGMEM = "<()>ABXYLR";
const char strMonster0[] PROGMEM = "THE MONSTER";
const char strMonster1[] PROGMEM = "ITS STENCH CAN BE";
const char strMonster2[] PROGMEM = "SMELLED TWO ROOMS AWAY";
const char strMonster3[] PROGMEM = "A HOLE";
const char strMonster4[] PROGMEM = "ITS BREEZE CAN BE";
const char strMonster5[] PROGMEM = "FELT A ROOM AWAY";
const char strNoMoves[] PROGMEM = "NO MOVES LEFT";
const char strToggle[] PROGMEM = "PRESS L TO TOGGLE MUSIC";
const char strVersion[] PROGMEM = "V1.01";
const char * const batteries[] PROGMEM = {
battery0Map,
battery1Map,
battery2Map,
battery3Map,
battery4Map
};
const char * const monsterRooms[] PROGMEM = {
clearRoomMap,
redRoomMap,
greenRoomMap,
bothRoomMap,
wumpusMap,
holeMap,
corridor1Map,
corridor2Map,
};
const char * const marumbiMaps[] PROGMEM = {
NULL,
marumbi01Map,
marumbi02Map,
marumbi03Map,
marumbi04Map,
marumbi05Map,
marumbi06Map,
marumbi07Map,
marumbi08Map,
marumbi09Map,
marumbi10Map,
marumbi11Map,
marumbi12Map,
marumbi13Map,
marumbi14Map,
marumbi15Map,
};
const uint32_t prizes[] PROGMEM = {
50, 100, 500, 1000, 2500, 5000, 7500, 10000, 20000, 30000, 40000, 50000,
75000, 100000, 500000, 1000000, 2500000, 5000000, 7500000, 10000000,
20000000, 30000000, 40000000, 50000000, 75000000, 100000000
};
const uint8_t monsterDx[] PROGMEM = {0, 1, 0, 255};
const uint8_t monsterDy[] PROGMEM = {255, 0, 1, 0};
const uint8_t monsterCor[2][4] PROGMEM = {
{1, 0, 3, 2},
{3, 2, 1, 0}
};
const uint8_t catAnimationPixels[][2] PROGMEM = {
{1, 9},
{15, 14},
{62, 54},
{0, 0},
{6, 14},
{8, 9},
{57, 49},
};
struct int8_t_pair {
int8_t x, y;
};
struct troll {
struct int8_t_pair pos;
uint8_t stun;
uint8_t type;
};
struct trollenLevel {
struct int8_t_pair hero;
struct troll troll[2];
struct int8_t_pair trap[MAX_TRAPS];
uint8_t map[6][6];
};
/* A simple sprite for blitting onto a ram tile */
struct simpleSprite {
uint16_t p;
uint8_t rt;
uint8_t bgTile;
const char *fgTile;
uint8_t fgSource;
};
extern Track tracks[CHANNELS];
extern bool playSong;
extern uint8_t vram[];
extern char ram_tiles[];
/* Controller Handling */
uint16_t held[2] = {0, 0},
pressed[2] = {0, 0},
released[2] = {0, 0},
prev[2] = {0, 0};
void controllerStart();
void controllerEnd();
void playSound(uint8_t i);
void cutSong();
void copyTileToRam(const char *tt, uint8_t src, uint8_t dst);
void rtMirror(char src, char dst);
void rtRotate90(char src, char dst);
void rtReplaceColor(uint8_t t, char src, char dst);
void rtDrawRectangle(uint8_t t, uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint8_t color);
void printColoredByte(uint8_t x, uint8_t y, uint8_t byte, uint8_t base);
void printColoredByte2(uint8_t x, uint8_t y, uint8_t byte, uint8_t base);
void printColoredShort(uint8_t x, uint8_t y, uint16_t byte, uint8_t base);
void loadColoredNumbers();
void ssLoadFromMap(const char *map, struct simpleSprite *ss,
uint8_t x, uint8_t y, uint8_t baseRt, const char *tt);
void ssSwitchMap(const char *map, struct simpleSprite *ss, const char *tt);
void ssUnblit(struct simpleSprite *ss, uint8_t len);
void ssBlit(struct simpleSprite *ss, uint8_t len);
void ssMove(struct simpleSprite *ss, uint8_t len, int8_t x, int8_t y);
void printMoney(uint8_t x, uint8_t y, uint32_t value, uint8_t base);
void greedLoadBatteryTiles();
void greed(uint8_t human);
uint8_t rainMoveDown(uint8_t player);
void rainShoot(uint8_t player, uint8_t key);
void richDrawStatus(bool *available, uint8_t *order, uint8_t startingChest);
void rich();
uint8_t testSlide(uint8_t *m);
void switchSlide(uint8_t p, uint8_t o, uint8_t *m);
void slide();
void theOneLoadTiles();
void theOneDrawMap(const uint8_t map[9][9]);
bool theOneIsHoleGood(struct int8_t_pair peg, struct int8_t_pair hole,
const uint8_t map[9][9]);
uint8_t theOneLoadBoard(uint8_t id, uint8_t map[9][9]);
bool theOneGetPossibleMove(uint8_t map[9][9], struct int8_t_pair peg,
struct int8_t_pair *move);
void theOne(uint8_t boardId);
bool checkConnectivity(uint8_t map[][(CAVE_WIDTH+1)/2]);
void flagAround(uint8_t y, uint8_t x, uint8_t f, uint8_t dist,
uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2]);
void monsterDrawMap(const uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
bool whole);
void monsterFixSS(struct simpleSprite *ss, uint8_t len);
void loadFrame(uint8_t bgColor, uint8_t offset);
void drawFrame(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t o);
void monsterGenerateLevel(uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t *px, uint8_t *py);
void monsterPlay(uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y);
uint8_t mapGetVisible(const uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y);
void mapSetVisible(uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y);
void mapSetInvisible(uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y);
uint8_t mapGetType(const uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y);
void mapSetType(uint8_t map[CAVE_HEIGHT][(CAVE_WIDTH+1)/2],
uint8_t x, uint8_t y, uint8_t t);
void monster();
bool testArray(const int8_t *m);
void gridDrawCursor(int8_t x, int8_t y, uint8_t tile);
int8_t gridCheck(const int8_t l[3][3]);
struct int8_t_pair gridGetMove(int8_t p, int8_t l[3][3]);
void grid(uint8_t human);
void animateCat();
bool survivalMoveDown(uint8_t p, int8_t height[2][10]);
void survival(uint8_t players);
void rain(uint8_t human);
void array(uint8_t human);
void trollenLoadBaseTiles();
void trollenLoadLevel(uint8_t n, struct trollenLevel *level);
void trollenDrawLevel(const struct trollenLevel *level);
void trollenDrawTitle();
uint8_t trollen(uint8_t level);
uint16_t topmenu();
void drawBaseTitle(uint16_t game);
int8_t twoPlayersMenu();
int8_t onePlayerMenu(uint8_t level, uint8_t levels);
int main();
void controllerStart() {
held[0] = ReadJoypad(0);
pressed[0] = held[0] & (held[0] ^ prev[0]);
released[0] = prev[0] & (held[0] ^ prev[0]);
held[1] = ReadJoypad(1);
pressed[1] = held[1] & (held[1] ^ prev[1]);
released[1] = prev[1] & (held[1] ^ prev[1]);
}
void controllerEnd() {
prev[0] = held[0];
prev[1] = held[1];
}
void playSound(uint8_t i) {
TriggerFx(i, 0xff, true);
}
void cutSong() {
for (uint8_t i = 0; i < CHANNELS; i++) {
tracks[i].envelopeStep = -128;
}
playSong = false;
}
void copyTileToRam(const char *tt, uint8_t src, uint8_t dst) {
const char *t = tt + 64*src;
char *rt = ram_tiles + 64*dst;
for (uint8_t i = 0; i < 64; i++)
rt[i] = pgm_read_byte(t+i);
}
void rtMirror(char src, char dst) {
const char *srt = ram_tiles + 64*src;
char *drt = ram_tiles + 64*dst;
uint8_t i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
drt[i*8+j] = srt[i*8+(7-j)];
}
void rtRotate90(char src, char dst) {
const char *srt = ram_tiles + 64*src;
char *drt = ram_tiles + 64*dst;
uint8_t i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
drt[i*8+j] = srt[(7-j)*8+i];
}
void rtReplaceColor(uint8_t t, char src, char dst) {
char *rt = ram_tiles + 64*t;
for (uint8_t i = 0; i < 64; i++)
if (rt[i] == src)
rt[i] = dst;
}
void rtDrawRectangle(uint8_t t, uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint8_t color) {
uint8_t i;
uint8_t j;
char *rt = ram_tiles+64*t+8*y;
for (i = 0; i < h; i++, rt+=8)
for (j = 0; j < w; j++)
rt[x+j] = color;
}
void printColoredByte(uint8_t x, uint8_t y, uint8_t byte, uint8_t base) {
SetTile(x--, y, base + (byte % 10));
byte /= 10;
SetTile(x--, y, (byte? base + (byte % 10) : 0));
byte /= 10;
SetTile(x--, y, (byte? base + (byte % 10) : 0));
}
void printColoredByte2(uint8_t x, uint8_t y, uint8_t byte, uint8_t base) {
/* Print exactly two digits with zero padding */
SetTile(x--, y, base + (byte % 10));
byte /= 10;
SetTile(x--, y, base + (byte % 10));
}
void printColoredShort(uint8_t x, uint8_t y, uint16_t s, uint8_t base) {
do {
SetTile(x--, y, base + (s % 10));
s /= 10;
} while (s);
}
void loadColoredNumbers() {
uint8_t i = 0;
for (i = 0; i < 10; i++) {
copyTileToRam(tileset, WHITE_NUMBER+i, i);
rtReplaceColor(i, 255, 168);
copyTileToRam(tileset, WHITE_NUMBER+i, 10+i);
rtReplaceColor(10+i, 255, 32);
}
}
void ssLoadFromMap(const char *map, struct simpleSprite *ss,
uint8_t x, uint8_t y, uint8_t baseRt, const char *tt) {
uint8_t w = pgm_read_byte(map++);
uint8_t h = pgm_read_byte(map++);
uint8_t i, j, p;
uint8_t *row = vram + y*VRAM_TILES_H;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
ss->p = (y+i)*VRAM_TILES_H + x+j;
ss->rt = baseRt++;
ss->bgTile = row[x+j];
p = pgm_read_byte(map++);
if (p > 256-RAM_TILES_COUNT) {
ss->fgSource = RAM;
p += RAM_TILES_COUNT;
}
else
ss->fgSource = FLASH;
ss->fgTile = (ss->fgSource == FLASH? tt : ram_tiles) + 64*p;
ss++;
}
row += VRAM_TILES_H;
}
}
void ssSwitchMap(const char *map, struct simpleSprite *ss, const char *tt) {
uint8_t w = pgm_read_byte(map++);
uint8_t h = pgm_read_byte(map++);
uint8_t i, p;
ssUnblit(ss, w*h);
for (i = 0; i < w*h; i++) {
p = pgm_read_byte(map++);
if (p > 256-RAM_TILES_COUNT) {
ss[i].fgSource = RAM;
p += RAM_TILES_COUNT;
}
else
ss[i].fgSource = FLASH;
ss[i].fgTile = (ss[i].fgSource == FLASH? tt : ram_tiles) + 64*p;
}
ssBlit(ss, w*h);
}
void ssUnblit(struct simpleSprite *ss, uint8_t len) {
while (len--) {
vram[ss->p] = ss->bgTile;
ss++;
}
}
void ssBlit(struct simpleSprite *ss, uint8_t len) {
uint8_t i, b;
char *rt;
const char *bg;
bool ram;
while (len--) {
rt = ram_tiles + 64*ss->rt;
ram = ss->bgTile < RAM_TILES_COUNT;
/* Current tileset info is not available, assume it's tileset */
bg = (ram? ram_tiles : tileset - 64*RAM_TILES_COUNT) + 64*ss->bgTile;
for (i = 0; i < 64; i++) {
b = ss->fgSource == FLASH? pgm_read_byte(ss->fgTile+i) : ss->fgTile[i];
rt[i] = b == TRANSLUCENT_COLOR? (ram? bg[i] : pgm_read_byte(bg+i)) : b;
}
vram[ss->p] = ss->rt;
ss++;
}
}
void ssMove(struct simpleSprite *ss, uint8_t len, int8_t x, int8_t y) {
while (len--) {
ss->p += VRAM_TILES_H*y + x;
ss->bgTile = vram[ss->p];
ss++;
}
}
void printMoney(uint8_t x, uint8_t y, uint32_t value, uint8_t base) {
SetTile(x--, y, base + (value % 10));
value /= 10;
SetTile(x--, y, base + (value % 10));
value /= 10;
SetTile(x--, y, '.' - ' ');
SetTile(x--, y, base + (value % 10));
value /= 10;
while (value) {
SetTile(x--, y, base + (value % 10));
value /= 10;
}
SetTile(x, y, '$' - ' ');
}
void greedLoadBatteryTiles() {
uint8_t i, j;
uint8_t newColors[] = {72, 67, 19, 236};
uint8_t p = 20;
for (i = 0; i < 4; i++)
for (j = 1; j < 6; j++) {
if (j == 3)
continue;
copyTileToRam(tileset, pgm_read_byte(battery0Map+2+j), p);
rtReplaceColor(p++, 24, newColors[i]);
}
}
void greed(uint8_t human) {
uint8_t i, n, t, s, v, w, xor, h, nv, d;
uint8_t array[5];
ClearVram();
/* Randomly generate a new game and draw on the screen */
for (i = 0; i < 5; i++) {
array[i] = (random() % 8) + 1;
PrintByte(5 + 5 * i, 24, array[i], 0);
for (n = 0; n < array[i]; n++) {
DrawMap2(3 + 5 * i, 21 - (n << 1),
(const char *) pgm_read_word(batteries + i));
}
}
t = (human? random() % 2 : 0);
s = 0;
w = 0;
v = 0;
nv = 0;
d = 0;
Print(8, 4, strTurn);
SetTile(15, 4, WHITE_NUMBER + 1 + t);
printColoredByte(5, 24, array[0], BLUE_NUMBER);
while (1) {
controllerStart();
/* Select returns to the menu */
if (pressed[0] & BTN_SELECT) {
return;
}
if (!t || human) {
if (!s) {
/* Choosing a pile */
if (pressed[t] & BTN_RIGHT) {
greed_move_right:
playSound(MOVE_SELECTION_PATCH);
printColoredByte(5 + 5 * w, 24, array[w], WHITE_NUMBER);
w = (w + 1) % 5;
printColoredByte(5 + 5 * w, 24, array[w], BLUE_NUMBER);
}
else if (pressed[t] & BTN_LEFT) {
playSound(MOVE_SELECTION_PATCH);
printColoredByte(5 + 5 * w, 24, array[w], WHITE_NUMBER);
w = (10 + w - 1) % 5;
printColoredByte(5 + 5 * w, 24, array[w], BLUE_NUMBER);
}
else if (pressed[t] & BTN_A && array[w]) {
greed_select:
playSound(MOVE_SELECTION_PATCH);
s = (t && !human? 2 : 1);
v = array[w];
printColoredByte(5 + 5 * w, 24, array[w], GREEN_NUMBER);
}
}
/* Reducing */
else {
if (pressed[t] & BTN_DOWN && v) {
greed_reduce:
playSound(TAKE_BATTERY_PATCH);
Fill(3 + 5 * w, 21 - (--v << 1), 3, 2, 0);
printColoredByte(5 + 5 * w, 24, v, GREEN_NUMBER);
}
else if (pressed[t] & BTN_UP && v < array[w]) {
playSound(PLACE_BATTERY_PATCH);
DrawMap2(3 + 5 * w, 21 - (v << 1),
(const char *) pgm_read_word(batteries + w));
printColoredByte(5 + 5 * w, 24, ++v, GREEN_NUMBER);
}
else if (pressed[t] & BTN_B) {
playSound(PLACE_BATTERY_PATCH);
for (i = 0; i < array[w]; i++) {
DrawMap2(3 + 5 * w, 21 - (i << 1),
(const char *) pgm_read_word(batteries + w));
}
s = 0;
printColoredByte(5 + 5 * w, 24, array[w], BLUE_NUMBER);
}
else if (pressed[t] & BTN_A && v < array[w]) {
greed_apply:
playSound(MOVE_SELECTION_PATCH);
array[w] = v;
printColoredByte(5 + 5 * w, 24, array[w], WHITE_NUMBER);
w = 0;
printColoredByte(5, 24, array[0], BLUE_NUMBER);
s = 0;
t ^= 1;
SetTile(15, 4, WHITE_NUMBER + 1 + t);
/* Check for victory */
for (i = 0; i < 5; i++) {
if (array[i]) {
break;
}
}
if (i == 5) {
break;
}
}
}
}
else {
/* AI */
if (!s) {
s = 1;
d = CPU_DELAY;
/* Calculate the play */
xor = 0;
for (i = 0; i < 5; i++) {
xor ^= array[i];
}
/* No winning strategy */
if (!xor) {
for (i = 0; i < 5; i++) {
if (array[i]) {
n = i;
break;
}
}
nv = array[n] - 1;
goto greed_finish;
}
/* The if is going to be met because xor != 0 */
for (h = 3; ; h--) {
if (xor & (1 << h)) {
break;
}
}
for (i = 0; i < 5; i++) {
if (array[i] & (1 << h)) {
n = i;
break;
}
}
nv = xor ^ array[n];
}
else if (s == 1) {
/* Select the right pile */
if (d) {
d--;
goto greed_finish;
}
d = CPU_DELAY;
if (w != n) {
goto greed_move_right;
}
else {
goto greed_select;
}
}
else {
/* Reduce */
if (d) {
d--;
goto greed_finish;
}
d = CPU_DELAY;
if (v != nv) {
goto greed_reduce;
}
else {
goto greed_apply;
}
}
}
greed_finish:
WaitVsync(1);
controllerEnd();
}
/* Announce the winner */
Fill(5, 4, 20, 1, 0);
Print(8, 4, strWins);
SetTile(15, 4, WHITE_NUMBER + 1 + !t);
playSound(!t && !human? LOSS_PATCH : VICTORY_PATCH);
/* Wait one of the players to press start */
while (1) {
controllerStart();
if (pressed[0] & BTN_START)
break;
WaitVsync(1);
controllerEnd();
}
}
void printChests(bool *available) {
uint8_t y, x, i;
x = 3;
y = 4;
for (i = 0; i < TOTAL_CHESTS; i++) {
if (!available[i])
continue;
DrawMap2(x, y, closedChestMap);
printColoredByte2(x + 2, y + 1, i, WHITE_NUMBER);
x += 5;
if (x > 23) {
x = 3;
y += 4;
}
}
}
void richDrawStatus(bool *available, uint8_t *order, uint8_t startingChest) {
uint8_t i;
uint8_t revOrder[TOTAL_CHESTS];
for (i = 0; i < TOTAL_CHESTS; i++) {
revOrder[order[i]] = i;
}
for (i = 0; i < TOTAL_CHESTS; i++) {
printMoney(i < TOTAL_CHESTS/2? 10 : 26, 1+2*(i%(TOTAL_CHESTS/2)),
pgm_read_dword(prizes+i),
revOrder[i] == startingChest || available[revOrder[i]]?
WHITE_NUMBER : CROSSED_OUT_NUMBER);
}
}
void rich() {
uint8_t i, n, s, left;
uint32_t r;
bool available[TOTAL_CHESTS];
uint8_t order[TOTAL_CHESTS];
uint8_t screenChest[TOTAL_CHESTS];
memset(available, 1, sizeof(available));
for (i = 0; i < 26; i++)
order[i] = i;
/* Generate crossed-out numbers */
for (i = 0; i < 10; i++) {
copyTileToRam(tileset, WHITE_NUMBER+i, 20+i);
rtDrawRectangle(20+i, 0, 3, 8, 2, RED_6);
}
/* Randomize */
for (i = 0; i < 26; i++) {
n = (random() % (26 - i)) + i;
s = order[i];
order[i] = order[n];
order[n] = s;
}
/* Select the starting chest */
s = 0;
ClearVram();
Print(7, 6, strStarting);
DrawMap2(13, 12, closedChestMap);
for (bool done = false; !done; ) {
controllerStart();
if (pressed[0] & BTN_LEFT) {
s = (TOTAL_CHESTS - 1+ s) % TOTAL_CHESTS;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_RIGHT) {
s = (s + 1) % TOTAL_CHESTS;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_SELECT)
return;
else if (pressed[0] & BTN_A)
done = true;
printColoredByte2(15, 13, s, GREEN_NUMBER);
WaitVsync(1);
controllerEnd();
}
available[s] = 0;
playSound(PLACE_BATTERY_PATCH);
left = 25;
r = 0;
/* Play */
while (left) {
/* Pick a chest */
ClearVram();
Print(9, 2, strOpenChest);
printChests(available);
n = 0;
for (i = 0; i < TOTAL_CHESTS; i++) {
if (!available[i])
continue;
screenChest[n++] = i;
}
i = 0;
for (bool done = false; !done; ) {
controllerStart();
printColoredByte2(5+5*(i%5), 5+4*(i/5), screenChest[i], WHITE_NUMBER);
if (pressed[0] & BTN_RIGHT && i < left-1) {
i++;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_LEFT && i > 0) {
i--;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_UP && i >= 5) {
i -= 5;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_DOWN && i < left-5) {
i += 5;
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_A)
done = true;
else if (pressed[0] & BTN_SELECT)
return;
printColoredByte2(5+5*(i%5), 5+4*(i/5), screenChest[i], BLUE_NUMBER);
WaitVsync(1);
controllerEnd();
}
playSound(PLACE_BATTERY_PATCH);
/* Open the chest */
i = screenChest[i];
left--;
available[i] = 0;
ClearVram();
Print(8, 5, strSee);
Print(5, 6, strInside);
DrawMap2(13, 12, closedChestMap);
WaitVsync(60);
DrawMap2(13, 12, openChestMap);
printMoney(19, 10, pgm_read_dword(prizes + order[i]), WHITE_NUMBER);
playSound(order[i] > 13? LOSS_PATCH : VICTORY_PATCH);
for (bool done = false; !done; ) {
controllerStart();
if (pressed[0] & BTN_A)
done = true;
else if (pressed[0] & BTN_SELECT)
return;
WaitVsync(1);
controllerEnd();
}
/* Show status */
ClearVram();
richDrawStatus(available, order, s);
WaitVsync(30);
for (bool done = false; !done; ) {
controllerStart();
if (pressed[0] & BTN_A) {
done = true;
}
else if (pressed[0] & BTN_SELECT) {
controllerEnd();
return;
}
WaitVsync(1);
controllerEnd();
}
/* Offer */
if (left != 20 && left != 15 && left != 10 && left != 6 && left != 4
&& left != 2 && left != 1) {
continue;
}
/* Switch chests */
if (left == 1) {
ClearVram();
for (i = 0; !available[i]; i++);
Print(6, 6, strWantKeep);
Print(6, 7, strChestNo);
printColoredByte2(13, 7, s, WHITE_NUMBER);
Print(4, 8, strTradeChest);
printColoredByte2(24, 8, i, WHITE_NUMBER);
Print(12, 15, strKeep);
Print(12, 16, strTrade);
SetTile(10, 15, ARROW_TILE);
n = 0;
for (bool done = false; !done; ) {
controllerStart();
if (pressed[0] & BTN_A)
done = true;
else if (pressed[0] & BTN_DOWN || pressed[0] & BTN_UP) {
SetTile(10, 15 + n, 0);
n ^= 1;
SetTile(10, 15 + n, ARROW_TILE);
playSound(MOVE_SELECTION_PATCH);
}
else if (pressed[0] & BTN_SELECT)
return;
WaitVsync(1);
controllerEnd();
}
playSound(PLACE_BATTERY_PATCH);
if (n) {
s ^= i;
i ^= s;
s ^= i;
}
r = pgm_read_dword(prizes + order[s]);
left--;
/* Animate the opening */
ClearVram();
Print(8, 5, strSee);
Print(5, 6, strInsideDef);
printColoredByte2(23, 6, i, WHITE_NUMBER);
DrawMap2(13, 12, closedChestMap);