-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
266 lines (214 loc) · 10.7 KB
/
utils.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
#include <curses.h>
#include "utils.h"
#include "pacman.h"
bool execute_move_t(state_t* state, move_t move) {
int ch; //Key pushed
static int chtmp; //Buffered key
int tmp;
bool changed_direction = false;
ch = getch(); //Figure out which key is pressed
//If they are not pressing something, use previous input
if(ch == ERR) ch = chtmp;
chtmp = ch;
//Determine which button is pushed
switch (move) {
case up: //Move pacman up
if(state->Loc[4][0] <= 0) tmp = LEVEL_HEIGHT - 1;
else tmp = state->Loc[4][0] - 1;
if((state->Level[tmp][state->Loc[4][1]] != 1)
&& (state->Level[tmp][state->Loc[4][1]] != 4))
{ state->Dir[4][0] = -1; state->Dir[4][1] = 0; changed_direction = true; }
break;
case down: //Move pacman down
if(state->Loc[4][0] >= 28) tmp = 0;
else tmp = state->Loc[4][0] + 1;
if((state->Level[tmp][state->Loc[4][1]] != 1)
&& (state->Level[tmp][state->Loc[4][1]] != 4))
{ state->Dir[4][0] = 1; state->Dir[4][1] = 0; changed_direction = true; }
break;
case left: //Move pacman left
if(state->Loc[4][1] <= 0) tmp = LEVEL_WIDTH - 1;
else tmp = state->Loc[4][1] - 1;
if((state->Level[state->Loc[4][0]][tmp] != 1)
&& (state->Level[state->Loc[4][0]][tmp] != 4))
{ state->Dir[4][0] = 0; state->Dir[4][1] = -1; changed_direction = true;}
break;
case right: //Move pacman right
if(state->Loc[4][1] >= 27) tmp = 0;
else tmp = state->Loc[4][1] + 1;
if((state->Level[state->Loc[4][0]][tmp] != 1)
&& (state->Level[state->Loc[4][0]][tmp] != 4))
{ state->Dir[4][0] = 0; state->Dir[4][1] = 1; changed_direction = true;}
break;
}
MovePacmanSim(state); CheckCollisionSim(state);
MoveGhostsSim(state); CheckCollisionSim(state);
return changed_direction;
}
/****************************************************************
* Function: CheckCollision(state_t* state) *
* Parameters: none *
* Returns: none *
* Description: Check and handle if Pacman collided with a ghost *
****************************************************************/
void CheckCollisionSim(state_t* state) {
//Temporary variable
int a = 0;
//Check each ghost, one at a time for collision
for(a = 0; a < 4; a++) {
//Ghost X and Y location is equal to Pacman X and Y location (collision)
if((state->Loc[a][0] == state->Loc[4][0]) && (state->Loc[a][1] == state->Loc[4][1])) {
//Pacman is invincible, ghost dies
if(state->Invincible == 1) {
state->Points = state->Points + state->GhostsInARow * 20; //Increase points by an increasing value
state->GhostsInARow *= 2;
//Reset the ghost's position to the starting location
state->Loc[a][0] = state->StartingPoints[a][0]; state->Loc[a][1] = state->StartingPoints[a][1];
}
//Pacman is vulnerable, Pacman dies
else {
//Subtract one life
state->Lives--;
//Reset Pacman's and ghosts' positions
for(a = 0; a < 5; a++) {
state->Loc[a][0] = state->StartingPoints[a][0];
state->Loc[a][1] = state->StartingPoints[a][1];
}
//Reset directions
state->Dir[0][0] = 1; state->Dir[0][1] = 0;
state->Dir[1][0] = -1; state->Dir[1][1] = 0;
state->Dir[2][0] = 0; state->Dir[2][1] = -1;
state->Dir[3][0] = 0; state->Dir[3][1] = 1;
state->Dir[4][0] = 0; state->Dir[4][1] = -1;
}
}
}
}
/****************************************************************
* Function: MoveGhosts() *
* Parameters: none *
* Returns: none *
* Description: Move all ghosts and check for wall collisions *
****************************************************************/
void MoveGhostsSim(state_t* state) {
//Set up some temporary variables
int a = 0; int b = 0; int c = 0;
int checksides[] = { 0, 0, 0, 0, 0, 0 };
static int SlowerGhosts = 0;
int tmp;
/* Move ghosts slower when they are vulnerable. This will allow
the ghosts to move only x times for every y times Pacman moves */
if(state->Invincible == 1) {
SlowerGhosts++;
if(SlowerGhosts > HOW_SLOW)
SlowerGhosts = 0;
}
//If ghosts are not vulnerable OR the ghosts can move now
if((state->Invincible == 0) || SlowerGhosts < HOW_SLOW)
//Loop through each ghost, one at a time
for(a = 0; a < 4; a++) {
//Switch sides? (Transport to other side of screen)
if((state->Loc[a][0] == 0) && (state->Dir[a][0] == -1)) state->Loc[a][0] = 28;
else if((state->Loc[a][0] == 28) && (state->Dir[a][0] == 1)) state->Loc[a][0] = 0;
else if((state->Loc[a][1] == 0) && (state->Dir[a][1] == -1)) state->Loc[a][1] = 27;
else if((state->Loc[a][1] == 27) && (state->Dir[a][1] == 1)) state->Loc[a][1] = 0;
else {
//Determine which directions we can go
for(b = 0; b < 4; b++) checksides[b] = 0;
if(state->Loc[a][0] == 28) tmp = 0;
else tmp = state->Loc[a][0] + 1;
if(state->Level[tmp][state->Loc[a][1]] != 1) checksides[0] = 1;
if(state->Loc[a][0] == 0) tmp = 28;
else tmp = state->Loc[a][0] - 1;
if(state->Level[tmp][state->Loc[a][1]] != 1) checksides[1] = 1;
if(state->Loc[a][1] == 27) tmp = 0;
else tmp = state->Loc[a][1] + 1;
if(state->Level[state->Loc[a][0]][tmp] != 1) checksides[2] = 1;
if(state->Loc[a][1] == 0) tmp = 27;
else tmp = state->Loc[a][1] - 1;
if(state->Level[state->Loc[a][0]][tmp] != 1) checksides[3] = 1;
//Don't do 180 unless we have to
c = 0; for(b = 0; b < 4; b++) if(checksides[b] == 1) c++;
if(c > 1) {
if(state->Dir[a][0] == 1) checksides[1] = 0;
else if(state->Dir[a][0] == -1) checksides[0] = 0;
else if(state->Dir[a][1] == 1) checksides[3] = 0;
else if(state->Dir[a][1] == -1) checksides[2] = 0;
}
c = 0;
do {
//Decide direction, based somewhat-randomly
b = (int)(rand() / (1625000000 / 4));
/* Tend to mostly chase Pacman if he is vulnerable
or run away when he is invincible */
if(checksides[b] == 1) {
if(b == 0) { state->Dir[a][0] = 1; state->Dir[a][1] = 0; }
else if(b == 1) { state->Dir[a][0] = -1; state->Dir[a][1] = 0; }
else if(b == 2) { state->Dir[a][0] = 0; state->Dir[a][1] = 1; }
else if(b == 3) { state->Dir[a][0] = 0; state->Dir[a][1] = -1; }
}
else {
if(state->Invincible == 0) {
//Chase Pacman
if((state->Loc[4][0] > state->Loc[a][0]) && (checksides[0] == 1)) { state->Dir[a][0] = 1; state->Dir[a][1] = 0; c = 1; }
else if((state->Loc[4][0] < state->Loc[a][0]) && (checksides[1] == 1)) { state->Dir[a][0] = -1; state->Dir[a][1] = 0; c = 1; }
else if((state->Loc[4][1] > state->Loc[a][1]) && (checksides[2] == 1)) { state->Dir[a][0] = 0; state->Dir[a][1] = 1; c = 1; }
else if((state->Loc[4][1] < state->Loc[a][1]) && (checksides[3] == 1)) { state->Dir[a][0] = 0; state->Dir[a][1] = -1; c = 1; }
}
else {
//Run away from Pacman
if((state->Loc[4][0] > state->Loc[a][0]) && (checksides[1] == 1)) { state->Dir[a][0] = -1; state->Dir[a][1] = 0; c = 1; }
else if((state->Loc[4][0] < state->Loc[a][0]) && (checksides[0] == 1)) { state->Dir[a][0] = 1; state->Dir[a][1] = 0; c = 1; }
else if((state->Loc[4][1] > state->Loc[a][1]) && (checksides[3] == 1)) { state->Dir[a][0] = 0; state->Dir[a][1] = -1; c = 1; }
else if((state->Loc[4][1] < state->Loc[a][1]) && (checksides[2] == 1)) { state->Dir[a][0] = 0; state->Dir[a][1] = 1; c = 1; }
}
}
} while ((checksides[b] == 0) && (c == 0));
//Move Ghost
state->Loc[a][0] += state->Dir[a][0];
state->Loc[a][1] += state->Dir[a][1];
}
}
}
/****************************************************************
* Function: MovePacman() *
* Parameters: none *
* Returns: none *
* Description: Move Pacman and check for wall collisions *
****************************************************************/
void MovePacmanSim(state_t* state) {
static int itime = 0;
//Switch sides? (Transport to other side of screen)
if((state->Loc[4][0] == 0) && (state->Dir[4][0] == -1)) state->Loc[4][0] = 28;
else if((state->Loc[4][0] == 28) && (state->Dir[4][0] == 1)) state->Loc[4][0] = 0;
else if((state->Loc[4][1] == 0) && (state->Dir[4][1] == -1)) state->Loc[4][1] = 27;
else if((state->Loc[4][1] == 27) && (state->Dir[4][1] == 1)) state->Loc[4][1] = 0;
//Or
else {
//Move Pacman
state->Loc[4][0] += state->Dir[4][0];
state->Loc[4][1] += state->Dir[4][1];
//If he hit a wall, move back
if((state->Level[state->Loc[4][0]][state->Loc[4][1]] == 1) || (state->Level[state->Loc[4][0]][state->Loc[4][1]] == 4)) {
state->Loc[4][0] -= state->Dir[4][0]; state->Loc[4][1] -= state->Dir[4][1];
}
}
//What is Pacman eating?
switch (state->Level[state->Loc[4][0]][state->Loc[4][1]]) {
case 2: //Pellet
state->Level[state->Loc[4][0]][state->Loc[4][1]] = 0;
state->Points++;
state->Food--;
break;
case 3: //PowerUp
state->Level[state->Loc[4][0]][state->Loc[4][1]] = 0;
state->Invincible = 1;
if(state->GhostsInARow == 0) state->GhostsInARow = 1;
itime = time(0);
break;
}
//Is he invincible?
if(state->Invincible == 1) state->tleft = (11 - state->LevelNumber - time(0) + itime);
//Is invincibility up yet?
if(state->tleft < 0) { state->Invincible = 0; state->GhostsInARow = 0; state->tleft = 0; }
}