-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.c
360 lines (351 loc) · 7.71 KB
/
pacman.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
/*----------------------------------------------------------------------
*
* PACMAN for BBN BitGraphs
*
* File: pacman.c
* Contents: Pacman control routines
* Author: Bob Brown (rlb)
* Description: routines for initialization, movement, and
* drawing.
*
* Copyright (c) 1987 Robert L. Brown. All Rights Reserved. This code
* is public domain.
*
*----------------------------------------------------------------------
*/
#include "style.h"
#include "pacman.h"
#include "bg.h"
#include "compat.h"
/*
* Initialize the pacman
*
* pacnew - called a beginning of game.
* pacinit - called at each board restart.
*/
pacnew()
{
}
pacinit()
{
Pacman.dir = PACFACE;
Pacman.moving = FALSE;
Pacman.moves = 0;
Pacman.mouth = 0;
Pacman.font = FACECHAR;
Pacman.row = MZtoSC(PACROW);
Pacman.col = MZtoSC(PACCOL)-1;
Pacman.power = 0;
Pacman.alive = TRUE;
Pacman.adjusttime = 0;
paccomp();
eladd(Pacman.time+STARTDELAY,pacmove, NULL, 0);
}
/*
* pacmove - move the pacman based on the description in
* global Pacman - called by event-list manager.
*
* mouthfont is indexed by the direction the pacman is facing and
* gives the font character to use for each mouth-step
*/
char *mouthfont[] = { "abcdcb", "aefgfe", "ahijih", "aklmlk"};
int
pacmove()
{
paccomp();
if ( Pacman.moving ) {
/*
* Check if next move is feasible
*/
if ( Pacman.pendcnt > 0 ) {
Pacman.pendcnt--;
if ( trymove(Pacman.pending) )
Pacman.pendcnt = 0;
else
trymove(Pacman.dir);
} else
trymove(Pacman.dir);
}
/*
* Now have a (potentially new) position
*/
if ( ++Pacman.mouth >= 6 )
Pacman.mouth = 0;
Pacman.font = mouthfont[Pacman.dir-1][Pacman.mouth];
paccomp();
/*
* The routines that are called below rely on the maze coordinates
* having already been computed, for efficiency's sake.
*/
Pacman.mrow = SCtoMZ(Pacman.row+2);
Pacman.mcol = SCtoMZ(Pacman.col+2);
/*
* Check for walls going off and gobbling a dot
*/
Pacman.moves++;
if ( !Wallsoff && Pacman.moves>(WALLSDISAPPEAR-50*max(Wave,15)) ) {
compwalls();
Wallsoff = TRUE;
}
if ( hasdot() ) {
Pacman.adjusttime = DOTDELAY;
Dotsrem--;
sounddot();
addscore(VALGOLD);
} else
Pacman.adjusttime = 0;
/*
* Check for swallowing a power pill, fruit, and in safe zone
*/
if ( haspill() )
pacpower(NULL,ON);
if ( hasfruit() )
hitfruit();
Pacman.safe = issafe();
/*
* If powerful, detect collisions with monsters
*/
if ( Pacman.power > 0 ) {
register monster *mptr, *mptr2;
for ( mptr=Monster ; mptr < &Monster[MAXMONSTER] ; mptr++ ) {
if ( mptr->state==EDIBLE && collide(mptr) ) {
/*
* Captured a monster...
*/
int crow, ccol;
addscore(Monvalue);
settextop(INVERT);
crow = SCtoROW(mptr->row)+1;
ccol = SCtoCOL(min(mptr->col,Pacman.col))-6;
mvprintf(crow, ccol, "%4d",Monvalue);
if ( Silent )
pause(750);
else
soundkill();
mvprintf(crow, ccol, "%4d",Monvalue);
#if V1_25
settextop(REPLACE|COMPL);
#else
settextop(REPLACE);
#endif
Monvalue *= 2;
mptr->state = DEAD;
monfont1(mptr,EYESCHAR);
mptr->movestate = HOMING;
for ( mptr2=Monster ; mptr2 < &Monster[MAXMONSTER]; mptr2++ )
if ( mptr2->state == EDIBLE )
mptr2->canreverse = TRUE;
}
}
}
eladd(Pacman.time+Pacman.adjusttime,pacmove, NULL, 0);
}
paccomp()
{
blt40(Pacman.font,Pacman.row,Pacman.col,INVERT);
}
/*
* pacpower - handle a transition in the Pacman's power
* called from scheduler second & third time per pill.
*
* Parameters are set for 9 secs - 0.5 secs per wave, min 2 sec
*/
int
pacpower(junk,type)
char *junk;
int type;
{
register int powertime;
register monster *mptr;
powertime = max(POWERTIME-Wave*POWERDELTA,MINPOWERTIME);
switch ( type ) {
case ON:
/* soundpill(): */
addscore(VALPILL);
Monvalue = VALMONSTER;
for ( mptr=Monster ; mptr<&Monster[MAXMONSTER] ; mptr++ ) {
mptr->canreverse = TRUE;
if ( mptr->state == DANGEROUS || mptr->state == EDIBLE ) {
monfont1(mptr,GHOSTCHAR);
mptr->state = EDIBLE;
}
}
Pacman.power++;
eladd(3*powertime/4,pacpower,NULL,CLOSE);
break;
case CLOSE:
if ( Pacman.power == 1 ) {
for ( mptr=Monster ; mptr<&Monster[MAXMONSTER];mptr++ )
if ( mptr->state == EDIBLE )
monfont1(mptr,CLOSECHAR);
}
eladd(powertime/4,pacpower,NULL,OFF);
break;
case OFF:
if ( --Pacman.power <= 0 ) {
for ( mptr=Monster ; mptr<&Monster[MAXMONSTER];mptr++ ) {
if ( mptr->state == EDIBLE ) {
mptr->state = DANGEROUS;
monfont1(mptr,MONSTERCHAR[0]);
}
}
}
break;
}
}
/*
* trymove - try to make a move
*/
trymove(newmove)
int newmove;
{
move try;
/*
* If changing direction, row and col mod (FONTSIZE/BYTESIZE) must be 0
*/
if ( ((newmove^Pacman.dir)&1) && !aligns(Pacman.row,Pacman.col)) {
return 0;
}
/*
* Movement ok, compute next position
*/
onestep(Pacman.row, Pacman.col, newmove, &try);
if ( hitwall(try.row,try.col,newmove)) {
return 0;
}
Pacman.row = try.row;
Pacman.col = try.col;
Pacman.dir = newmove;
return 1;
}
/*
* check if a screen coordinate hits a wall
*/
hitwall(row,col,dir)
{
bool wall;
if ( inslow(row,col) && (dir&1) )
return(0);
wall = (Board[SCtoMZ(row+2)][SCtoMZ(col+2)] & TYPEMASK) <= WALLMAX;
return (wall || ((Board[SCtoMZ(row)][SCtoMZ(col)] & TYPEMASK) <= WALLMAX));
}
hitdoor(row,col)
{
bool door;
door = (Board[SCtoMZ(row+2)][SCtoMZ(col+2)] & TYPEMASK) == DOOR;
return (door || ((Board[SCtoMZ(row)][SCtoMZ(col)] & TYPEMASK) == DOOR));
}
hitblk(row,col)
{
int type;
bool blk;
type = (Board[SCtoMZ(row+2)][SCtoMZ(col+2)] & TYPEMASK);
blk = type == BLKDOT || type == BLKNODOT;
type = (Board[SCtoMZ(row)][SCtoMZ(col)] & TYPEMASK);
return (blk || (type == BLKDOT || type == BLKNODOT));
}
inslow(row,col)
{
bool slow;
slow = (Board[SCtoMZ(row+2)][SCtoMZ(col+2)] & TYPEMASK) == SLOW;
return (slow || ((Board[SCtoMZ(row)][SCtoMZ(col)] & TYPEMASK) == SLOW));
}
aligns(row,col)
{
int rowmod, colmod;
rowmod = row%(FONTSIZE/BYTESIZE);
colmod = col%(FONTSIZE/BYTESIZE);
return(rowmod==0 && colmod==0);
}
/*
* Check of the pacman collided with a monster
*/
collide(mptr)
register monster *mptr;
{
return (iabs(mptr->row-Pacman.row)+iabs(mptr->col-Pacman.col)<3 );
}
/*
* Check if the pacman has eaten a dot
*/
hasdot()
{
if (Board[Pacman.mrow][Pacman.mcol] & GOLD ) {
Board[Pacman.mrow][Pacman.mcol] &= ~GOLD;
blt24(GOLDCHAR,MZtoSC(Pacman.mrow),MZtoSC(Pacman.mcol),INVERT);
return TRUE;
}
return FALSE;
}
/*
* Check if the pacman has eaten a pill
*/
haspill()
{
if (Board[Pacman.mrow][Pacman.mcol] & PILL ) {
Board[Pacman.mrow][Pacman.mcol] &= ~PILL;
blt24(PILLCHAR,MZtoSC(Pacman.mrow),MZtoSC(Pacman.mcol),INVERT);
return TRUE;
}
return FALSE;
}
/*
* Check if the Pacman is in the safe corridor
*/
issafe()
{
return (Board[Pacman.mrow][Pacman.mcol]&TYPEMASK) == SAFE;
}
rempacface()
{
if ( Pacmen < MAXFACES )
blt40(FACECHAR,MZtoSC(MAZEROWS+1),MZtoSC(Pacmen*2),INVERT);
}
/*
* Handle the death of the pacman
*/
char *pacdeath = "egstu"; /* font sequence */
int pacdtop[] = {150,170,190,210,0};
int pacdbot[] = {200,220,240,280,0};
pacdie()
{
int i;
Pacman.alive = FALSE;
addscore(FLUSHSCORE);
soundoff();
#if V1_25 | V1_76 | V2_0
sleep(500);
#else
snooze(500L);
#endif
moncomp();
fruitcomp();
elinit();
for ( i=0 ; pacdeath[i]!='\0' ; i++ ) {
paccomp();
Pacman.font = pacdeath[i];
paccomp();
rsetdead();
if ( Silent )
#if V1_25 | V1_76 | V2_0
sleep(500);
#else
snooze(500L);
#endif
else {
if ( pacdtop[i] ) {
tonegen(1,pacdtop[i],15-i*2);
PsgSweep(0,pacdtop[i],pacdbot[i],10);
} else {
soundoff();
#if V1_25 | V1_76 | V2_0
sleep(500);
#else
snooze(500L);
#endif
}
}
}
soundoff();
paccomp();
}