-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacman.cpp
1291 lines (1116 loc) · 32.6 KB
/
Pacman.cpp
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
/* Copyright (c) 2009, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/
/*
* Edited by Oliver Martin [OJM] for LaFortuna 06/05/2018
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice from Peter Barrett and this permission notice
* by Oliver Martin appear in all copies.
*/
#ifndef byte
typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned long ulong;
#endif
#ifndef uchar
typedef unsigned char uchar;
#endif
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
extern "C"
{
#include "iob.h"
/* Include JPMLs music library for sounds - [OJM] */
#include "jpml/jpml.h"
#include "fatfs/ff.h"
}
#ifndef max
#define max(_a,_b) (((_a) > (_b)) ? (_a) : (_b))
#define min(_a,_b) (((_a) < (_b)) ? (_a) : (_b))
#endif
#include "Graphics.h"
#include "LCD.h"
#include "PacmanTiles.h"
extern const byte _initSprites[] PROGMEM;
extern const byte _palette2[] PROGMEM;
extern const byte _paletteIcon2[] PROGMEM;
extern const byte _opposite[] PROGMEM;
extern const byte _scatterChase[] PROGMEM;
extern const byte _scatterTargets[] PROGMEM;
extern const char _pinkyTargetOffset[] PROGMEM;
extern const byte _pacLeftAnim[] PROGMEM;
extern const byte _pacRightAnim[] PROGMEM;
extern const byte _pacVAnim[] PROGMEM;
enum GameState {
ReadyState,
PlayState,
DeadGhostState, // Player got a ghost, show score sprite and only move eyes
DeadPlayerState,
EndLevelState
};
enum SpriteState
{
PenState,
RunState,
FrightenedState,
DeadNumberState,
DeadEyesState,
AteDotState, // pacman
DeadPacmanState
};
enum {
MStopped = 0,
MRight = 1,
MDown = 2,
MLeft = 3,
MUp = 4
};
#define C16(_rr,_gg,_bb) ((ushort)(((_bb & 0xF8) << 8) | ((_gg & 0xFC) << 3) | ((_rr & 0xF8) >> 3)))
// 8 bit palette - in RAM because of graphics driver
short _paletteW[] =
{
C16(0,0,0),
C16(255,0,0), // 1 red
C16(222,151,81), // 2 brown
C16(255,184,255), // 3 pink
C16(0,0,0),
C16(0,255,255), // 5 cyan
C16(71,84,255), // 6 mid blue
C16(255,184,81), // 7 lt brown
C16(0,0,0),
C16(255,255,0), // 9 yellow
C16(0,0,0),
C16(33,33,255), // 11 blue
C16(0,255,0), // 12 green
C16(71,84,174), // 13 aqua
C16(255,184,174), // 14 lt pink
C16(222,222,255), // 15 whiteish
};
#define BINKY 0
#define PINKY 1
#define INKY 2
#define CLYDE 3
#define PACMAN 4
const byte _initSprites[] =
{
BINKY, 14, 17-3, 31, MLeft,
PINKY, 14-2, 17, 79, MLeft,
INKY, 14, 17, 137, MLeft,
CLYDE, 14+2, 17, 203, MRight,
PACMAN, 14, 17+9, 0, MLeft,
};
// Ghost colors
const byte _palette2[] =
{
0,11,1,15, // BINKY red
0,11,3,15, // PINKY pink
0,11,5,15, // INKY cyan
0,11,7,15, // CLYDE brown
0,11,9,9, // PACMAN yellow
0,11,15,15,// FRIGHTENED
0,11,0,15, // DEADEYES
};
const byte _paletteIcon2[] =
{
0,9,9,9, // PACMAN
0,2,15,1, // cherry
0,12,15,1, // strawberry
0,12,2,7, // peach
0,5,15,9, // bell
0,2,15,1, // apple
0,12,15,5, // grape
0,1,9,11, // galaxian
0,5,15,15, // key
};
#define PACMANICON 1
#define FRIGHTENEDPALETTE 5
#define DEADEYESPALETTE 6
#define FPS 60;
#define CHASE 0
#define SCATTER 1
#define DOT 7
#define PILL 14
#define PENGATE 0x1B
const byte _opposite[] = { MStopped,MLeft,MUp,MRight,MDown };
#define OppositeDirection(_x) pgm_read_byte(_opposite + _x)
const byte _scatterChase[] = { 7,20,7,20,5,20,5,0 };
const byte _scatterTargets[] = { 2,0,25,0,0,35,27,35 }; // inky/clyde scatter targets are backwards
const char _pinkyTargetOffset[] = { 4,0,0,4,-4,0,-4,4 }; // Includes pinky target bug
#define FRIGHTENEDGHOSTSPRITE 0
#define GHOSTSPRITE 2
#define NUMBERSPRITE 10
#define PACMANSPRITE 14
const byte _pacLeftAnim[] = { 5,6,5,4 };
const byte _pacRightAnim[] = { 2,0,2,4 };
const byte _pacVAnim[] = { 4,3,1,3 };
/* ======================== */
byte pacManLives = 3;
class Sprite
{
public:
short _x,_y;
short lastx,lasty;
byte cx,cy; // cell x and y
byte tx,ty; // target x and y
SpriteState state;
byte pentimer; // could be the same
byte who;
byte speed;
byte dir;
byte phase;
// Sprite bits
byte palette2; // 4->16 color map index
byte bits; // index of sprite bits
char sy;
/*=======PACMAN SPECIFIC =======*/
byte userIntendedDir;
short lastCellAlignedX, lastCellAlignedY;
/*==============================*/
void Init(const byte* s)
{
who = pgm_read_byte(s++);
cx = pgm_read_byte(s++);
cy = pgm_read_byte(s++);
pentimer = pgm_read_byte(s++);
dir = pgm_read_byte(s);
// PAC-MAN ONLY
userIntendedDir = dir;
_x = lastx = (short)cx*8-4;
_y = lasty = (short)cy*8;
state = PenState;
speed = 0;
}
void Target(byte x, byte y)
{
tx = x;
ty = y;
}
short Distance(byte x, byte y)
{
short dx = cx - x;
short dy = cy - y;
return dx*dx + dy*dy; // Distance to target
}
// once per sprite, not 9 times
void SetupDraw(GameState gameState, byte deadGhostIndex)
{
sy = 1;
palette2 = who;
byte p = phase >> 3;
if (who != PACMAN)
{
bits = GHOSTSPRITE + ((dir-1) << 1) + (p&1); // Ghosts
switch (state)
{
case FrightenedState:
bits = FRIGHTENEDGHOSTSPRITE + (p&1); // frightened
palette2 = FRIGHTENEDPALETTE;
break;
case DeadNumberState:
palette2 = FRIGHTENEDPALETTE;
bits = NUMBERSPRITE+ deadGhostIndex;
break;
case DeadEyesState:
palette2 = DEADEYESPALETTE;
break;
default:
;
}
return;
}
// PACMAN animation
byte f = (phase>>1) & 3;
if (dir == MLeft)
f = pgm_read_byte(_pacLeftAnim + f);
else if (dir == MRight)
f = pgm_read_byte(_pacRightAnim + f);
else
f = pgm_read_byte(_pacVAnim + f);
if (dir == MUp)
sy = -1;
bits = f + PACMANSPRITE;
}
// Draw this sprite into the tile at x,y
void Draw8(short x, short y, byte* tile)
{
short px = x - (_x-4);
if (px <= -8 || px >= 16) return;
short py = y - (_y-4);
if (py <= -8 || py >= 16) return;
// Clip y
short lines = py+8;
if (lines > 16)
lines = 16;
if (py < 0)
{
tile -= py*8;
py = 0;
}
lines -= py;
// Clip in X
byte right = 16 - px;
if (right > 8)
right = 8;
byte left = 0;
if (px < 0)
{
left = -px;
px = 0;
}
// Get bitmap
char dy = sy;
if (dy < 0)
py = 15-py; // VFlip
byte* data = (byte*)(pacman16x16+bits*64);
data += py << 2;
dy <<= 2;
data += px >> 2;
px &= 3;
const byte* palette = _palette2 + (palette2<<2);
while (lines)
{
const byte *src = data;
byte d = pgm_read_byte(src++);
d >>= px << 1;
byte sx = 4 - px;
byte x = left;
do
{
byte p = d & 3;
if (p)
{
p = pgm_read_byte(palette+p);
if (p)
tile[x] = p;
}
d >>= 2; // Next pixel
if (!--sx)
{
d = pgm_read_byte(src++);
sx = 4;
}
} while (++x < right);
tile += 8;
data += dy;
lines--;
}
}
};
class Playfield
{
Sprite _sprites[5];
byte _dotMap[(32/4)*(36-6)];
GameState _state;
long _score; // 7 digits of score
char _scoreStr[8];
byte _icons[14]; // Along bottom of screen
ushort _stateTimer;
ushort _frightenedTimer;
byte _frightenedCount;
byte _scIndex; //
ushort _scTimer; // next change of sc status
bool _inited;
byte* _dirty;
public:
Playfield() : _inited(false)
{
// Swizzle palette TODO just fix in place
byte * p = (byte*)_paletteW;
for (int i = 0; i < 16; i++)
{
ushort w = _paletteW[i]; // Swizzle
*p++ = w >> 8;
*p++ = w;
}
}
// Draw 2 bit BG into 8 bit icon tiles at bottom
void DrawBG2(byte cx, byte cy, byte* tile)
{
byte index = _icons[cx >> 1]; // 13 icons across bottom
if (index == 0)
{
memset(tile,0,64);
return;
}
index--;
byte b = (1-(cx&1)) + ((cy&1)<<1); // Index of tile
index <<= 2; // 4 tiles per icon
const byte* bg = pacman8x8x2 + ((b + index) << 4);
const byte* palette = _paletteIcon2 + index;
byte x = 16;
while (x--)
{
byte bits = (char)pgm_read_byte(bg++);
byte i = 4;
while (i--)
{
tile[i] = pgm_read_byte(palette + (bits & 3));
bits >>= 2;
}
tile += 4;
}
}
// Draw 1 bit BG into 8 bit tile
void DrawBG(byte cx, byte cy, byte* tile)
{
if (cy >= 34)
{
DrawBG2(cx,cy,tile);
return;
}
byte c = 11; // Blue
byte b = GetTile(cx,cy);
const byte* bg;
// This is a little messy
memset(tile,0,64);
if (cy == 20 && cx >= 11 && cx < 17)
{
if (_state != ReadyState)
b = 0; // hide 'READY!'
}
else if (cy == 1)
{
if (cx < 7)
b = _scoreStr[cx];
else if (cx >= 10 && cx < 17)
b = _scoreStr[cx-10];
} else {
if (b == DOT || b == PILL)
{
if (!GetDot(cx,cy))
return;
c = 14;
}
if (b == PENGATE)
c = 14;
}
bg = playTiles + (b << 3);
if (b >= '0')
c = 15; // text is white
for (byte y = 0; y < 8; y++)
{
char bits = (char)pgm_read_byte(bg++);
byte x = 0;
while (bits)
{
if (bits < 0)
tile[x] = c;
bits <<= 1;
x++;
}
tile += 8;
}
dump_tile(tile);
}
void dump_tile(byte* tile)
{
int i=0, j;
for(uint8_t x=0; x<8; x++) {
for(uint8_t x=0; x<8; x++)
printf("%02x",tile[i++]);
printf("\n");
}
scanf("%d", &j);
}
// Draw BG then all sprites in this cell
void Draw(short x, short y, bool sprites)
{
byte tile[8*8];
// Fill with BG
// DrawBG(x,y,tile);
byte* tilep = tile;
if (y >= 34)
{
DrawBG2(x,y,tile);
}
else
{
byte c = 11; // Blue
byte b = GetTile(x,y);
const byte* bg;
// This is a little messy
memset(tile,0,64);
if (y == 20 && x >= 11 && x < 17)
{
if (_state != ReadyState)
b = 0; // hide 'READY!'
}
else if (y == 1)
{
if (x < 7)
b = _scoreStr[x];
else if (x >= 10 && x < 17)
b = _scoreStr[x-10];
} else {
if (b == DOT || b == PILL)
{
if (!GetDot(x,y))
goto jumpout;
c = 14;
}
if (b == PENGATE)
c = 14;
}
bg = playTiles + (b << 3);
if (b >= '0')
c = 15; // text is white
for (byte cy = 0; cy < 8; cy++)
{
char bits = (char)pgm_read_byte(bg++);
byte cx = 0;
while (bits)
{
if (bits < 0)
tilep[cx] = c;
bits <<= 1;
cx++;
}
tilep += 8;
}
}
jumpout:
// Overlay sprites
x <<= 3;
y <<= 3;
if (sprites)
{
for (byte i = 0; i < 5; i++)
_sprites[i].Draw8(x,y,tile);
}
// Show sprite block
#if 0
for (byte i = 0; i < 5; i++)
{
Sprite* s = _sprites + i;
if (s->cx == (x>>3) && s->cy == (y>>3))
{
memset(tile,0,8);
for (byte j = 1; j < 7; j++)
tile[j*8] = tile[j*8+7] = 0;
memset(tile+56,0,8);
}
}
#endif
x += (240-224)/2;
y += (320-288)/2;
// Should be a direct Graphics call
LCD::SetWrap(x,y,8,8);
LCD::SetGRAM(x,y);
LCD::PixelsIndexed(64,tile,(byte*)_paletteW);
}
// Mark tile as dirty (should not need range checking here)
static void Mark(short x, short y, byte* m)
{
x -= 4;
y -= 4;
short top = y >> 3;
short bottom = ((y + 16 + 7) >> 3);
top = max(0,top);
bottom = min(36,bottom);
byte* row = m + (top << 2); // 32 bits per row
while (top < bottom)
{
short left = x >> 3;
short right = (x + 16 + 7) >> 3;
left = max(0,left);
right = min(28,right);
while (left < right)
{
row[left >> 3] |= 0x80 >> (left & 7);
left++;
}
row += 4;
top++;
}
}
void DrawAllBG()
{
for (byte y = 0; y < 36; y++)
for (byte x = 0; x < 28; x++)
Draw(x,y,false);
}
// Draw sprites overlayed on cells
// I love sprites
void DrawAll()
{
byte* m = _dirty;
// Mark sprite old/new positions as dirty
for (byte i = 0; i < 5; i++)
{
Sprite* s = _sprites + i;
Mark(s->lastx,s->lasty,m);
Mark(s->_x,s->_y,m);
}
// Animation
for (byte i = 0; i < 5; i++)
_sprites[i].SetupDraw(_state,_frightenedCount-1);
// Redraw only dirty tiles
byte* row = m;
for (byte y = 0; y < 36; y++) // skip n lines TODO
{
for (byte x = 0; x < 32; x += 8) // 28 actually
{
char b = (char)*row++;
byte xx = x;
while (b)
{
if (b < 0)
Draw(xx,y,true);
b <<= 1;
xx++;
}
}
}
}
byte GetTile(int cx, int ty)
{
return pgm_read_byte(playMap + ty*28 + cx);
}
short Chase(Sprite* s, short cx, short cy)
{
while (cx < 0) // Tunneling
cx += 28;
while (cx >= 28)
cx -= 28;
byte t = GetTile(cx,cy);
//-- make sure not colliding with walls:
if (!(t == 0 || t == DOT || t == PILL || t == PENGATE))
return 0x7FFF;
//-- special case: pengate
if (t == PENGATE)
{
if (s->who == PACMAN)
return 0x7FFF; // Pacman can't cross this to enter pen
if (!(InPen(s->cx,s->cy) || s->state == DeadEyesState))
return 0x7FFF; // Can cross if dead or in pen trying to get out
}
short dx = s->tx-cx;
short dy = s->ty-cy;
return dx*dx + dy*dy; // Distance to target
}
void UpdateTimers()
{
// Update scatter/chase selector, low bit of index indicates scatter
if (_scIndex < 8)
{
if (_scTimer-- == 0)
{
byte duration = pgm_read_byte(_scatterChase + _scIndex++);
_scTimer = duration*FPS;
}
}
// Release frightened ghosts
if (_frightenedTimer && !--_frightenedTimer)
{
for (byte i = 0; i < 4; i++)
{
Sprite* s = _sprites + i;
if (s->state == FrightenedState)
{
s->state = RunState;
s->dir = OppositeDirection(s->dir);
}
}
}
}
void Scatter(Sprite* s)
{
const byte* st = _scatterTargets + (s->who << 1);
s->Target(pgm_read_byte(st),pgm_read_byte(st+1));
}
void UpdateTargets()
{
if (_state == ReadyState)
return;
Sprite* pacman = _sprites + PACMAN;
// Ghost AI
bool scatter = _scIndex & 1;
for (byte i = 0; i < 4; i++)
{
Sprite* s = _sprites+i;
// Deal with returning ghost to pen
if (s->state == DeadEyesState)
{
if (s->cx == 14 && s->cy == 17) // returned to pen
{
s->state = PenState; // Revived in pen
s->pentimer = 80;
}
else
s->Target(14,17); // target pen
continue; //
}
// Release ghost from pen when timer expires
if (s->pentimer)
{
if (--s->pentimer) // stay in pen for awhile
continue;
s->state = RunState;
}
if (InPen(s->cx,s->cy))
{
s->Target(14,14-2); // Get out of pen first
} else {
if (scatter || s->state == FrightenedState)
Scatter(s);
else
{
// Chase mode targeting
byte tx = pacman->cx;
byte ty = pacman->cy;
switch (s->who)
{
case PINKY:
{
const char* pto = _pinkyTargetOffset + ((pacman->dir-1)<<1);
tx += pgm_read_byte(pto);
ty += pgm_read_byte(pto+1);
}
break;
case INKY:
{
const char* pto = _pinkyTargetOffset + ((pacman->dir-1)<<1);
Sprite* binky = _sprites + BINKY;
tx += pgm_read_byte(pto)>>1;
ty += pgm_read_byte(pto+1)>>1;
tx += tx - binky->cx;
ty += ty - binky->cy;
}
break;
case CLYDE:
{
if (s->Distance(pacman->cx,pacman->cy) < 64)
{
const byte* st = _scatterTargets + CLYDE*2;
tx = pgm_read_byte(st);
ty = pgm_read_byte(st+1);
}
}
break;
}
s->Target(tx,ty);
}
}
}
}
// Default to current direction
byte ChooseDir(int dir, Sprite* s)
{
short choice[4];
choice[0] = Chase(s,s->cx,s->cy-1); // Up
choice[1] = Chase(s,s->cx-1,s->cy); // Left
choice[2] = Chase(s,s->cx,s->cy+1); // Down
choice[3] = Chase(s,s->cx+1,s->cy); // Right
// Don't choose opposite of current direction?
short dist = choice[4-dir]; // favor current direction
byte opposite = OppositeDirection(dir);
for (byte i = 0; i < 4; i++)
{
byte d = 4-i;
if (d != opposite && choice[i] < dist)
{
dist = choice[i];
dir = d;
}
}
return dir;
}
bool InPen(byte cx, byte cy)
{
if (cx <= 10 || cx >= 18) return false;
if (cy <= 14 || cy >= 18) return false;
return true;
}
byte GetSpeed(Sprite* s)
{
// This first IF causes PacMan to pause as well as the ghosts when a ghost is killed - [OJM]
if (_state == DeadGhostState) {
return 0;
}
if (s->who == PACMAN)
return _frightenedTimer ? 90 : 80;
if (s->state == FrightenedState)
return 40;
if (s->state == DeadEyesState)
return 100;
if (s->cy == 17 && (s->cx <= 5 || s->cx > 20))
return 40; // tunnel
return 75;
}
void MoveAll()
{
UpdateTimers();
UpdateTargets();
// Update game state
if (_stateTimer)
{
if (--_stateTimer == 0)
{
switch (_state)
{
case ReadyState:
_state = PlayState;
_dirty[20*4 + 1] |= 0x1F; // Clear 'READY!'
_dirty[20*4 + 2] |= 0x80;
break;
case DeadGhostState:
_state = PlayState;
for (byte i = 0; i < 4; i++)
{
Sprite* s = _sprites + i;
if (s->state == DeadNumberState)
s->state = DeadEyesState;
}
break;
default:
;
}
} else {
if (_state == ReadyState)
return;
}
}
GhostAI();
PacmanControl();
}
/**
* Ghosts select where to go
*/
void GhostAI() {
for (byte i = 0; i < 4; i++) {
Sprite* s = _sprites + i;
// In DeadGhostState, only eyes move
if (_state == DeadGhostState && s->state != DeadEyesState)
continue;
// Calculate speed
s->speed += GetSpeed(s);
if (s->speed < 100)
continue;
s->speed -= 100;
s->lastx = s->_x;
s->lasty = s->_y;
s->phase++;
int x = s->_x;
int y = s->_y;
if ((x & 0x7) == 0 && (y & 0x7) == 0) // cell aligned,
s->dir = ChooseDir(s->dir,s); // time to choose another direction
//-- resolve the direction into new x coordinates
switch (s->dir) {
case MLeft: x -= 1; break;
case MRight: x += 1; break;
case MUp: y -= 1; break;
case MDown: y += 1; break;
}
//-- wrap x because of tunnels
while (x < 0)
x += 224;
while (x >= 224)
x -= 224;
//-- update ghosts's internal variables
s->_x = x;
s->_y = y;
s->cx = (x + 4) >> 3;
s->cy = (y + 4) >> 3;
}
}
/**
* Move the pacman and resolve collisions
*/
void PacmanControl() {
Sprite* pacman = _sprites + PACMAN;
// Calculate speed
pacman->speed += GetSpeed(pacman);
if (pacman->speed < 100)
return;
pacman->speed -= 100;
pacman->lastx = pacman->_x;
pacman->lasty = pacman->_y;
pacman->phase++;
int x = pacman->_x;
int y = pacman->_y;
int rayX = x;
int rayY = y;
/* =========== CODE SNIPPET 1: user control ========== */
/**
* Implement pac man control using the IO Board.
* Have a look at how ghosts choose their direction in the
* GhostAI() method. When can direction be changed and how
* is it done?
*/
//-- pacman is controlled by the IO board
// Updated for LaFortuna - [OJM]
if(get_switch_press(_BV(SWE))) pacman->userIntendedDir = MRight;
if(get_switch_press(_BV(SWW))) pacman->userIntendedDir = MLeft;
if(get_switch_press(_BV(SWN))) pacman->userIntendedDir = MUp;
if(get_switch_press(_BV(SWS))) pacman->userIntendedDir = MDown;