-
Notifications
You must be signed in to change notification settings - Fork 0
/
newpac.cpp
357 lines (285 loc) · 9.46 KB
/
newpac.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
#include <GL/glut.h>
#include <math.h>
#include <cstring>
#include <cstdio>
#include "newpac.h"
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <iostream>
#include <chrono>
#include <random>
#include <algorithm>
using namespace std;
#define COLUMNS 19
#define ROWS 22
pthread_t ghostThreads[4], gameEngineThread, uiThread, pacmanThread;
pthread_mutex_t mutex;
sem_t gameEngineSemaphore, turnSemaphore;
pthread_mutex_t coutmutex;
pthread_t pellet_respawn; //scenario 2
sem_t ghostSemaphore;//new
sem_t speedBoostSemaphore;//new
const int FAST_DURATION_SECONDS = 5;//new
const int RESET_FAST_INTERVAL = 10;//new
const int NORMAL_SLEEP_DURATION = 555000;//new
const int BOOSTED_SLEEP_DURATION = 200000;//new
std::chrono::high_resolution_clock::time_point fastStartTime[4];//new
int completedSpeedBoosts = 0;//new
pthread_mutex_t boostCountMutex;//new
pthread_mutex_t resourceMutex;//to prevent deadlock cond in ghosts //new3
sem_t keySemaphore;//new3
sem_t permitSemaphore;//new3
const int VISIBLE_DELAY_DURATION = 2000000; //new3 to have a visible delay in ghosts leaving house
void acquireResourcesAndLeave(int ghostIndex)
{//new3
//mutex lock laga diya so no other thread can preempt it hence no deadlock occurs
pthread_mutex_lock(&resourceMutex);
sem_wait(&keySemaphore);
sem_wait(&permitSemaphore);
pthread_mutex_lock(&coutmutex);
cout << "ghost " << ghostIndex << " acquired key and permit" << endl;
pthread_mutex_unlock(&coutmutex);
//ab preempt kar bhi do to no masla
pthread_mutex_unlock(&resourceMutex);
//so that thora delay aaye
usleep(VISIBLE_DELAY_DURATION);
sem_post(&permitSemaphore);
sem_post(&keySemaphore);
pthread_mutex_lock(&coutmutex);
cout << "2 seconds later ghost " << ghostIndex << " left the ghost house" << endl;
pthread_mutex_unlock(&coutmutex);
}
int calculateManhattanDistance(int x1, int y1, int x2, int y2)//new
{
return abs(x1 - x2) + abs(y1 - y2);
}
void selectClosestFastGhosts() {//new
g1.isFast = g2.isFast = g3.isFast = g4.isFast = false;
struct GhostDistance {
Ghost* ghost;
int distance;
int index;
};
GhostDistance ghostDistances[] = {//new
{&g1, calculateManhattanDistance(g1.x, g1.y, pacmanX, pacmanY), 0},
{&g2, calculateManhattanDistance(g2.x, g2.y, pacmanX, pacmanY), 1},
{&g3, calculateManhattanDistance(g3.x, g3.y, pacmanX, pacmanY), 2},
{&g4, calculateManhattanDistance(g4.x, g4.y, pacmanX, pacmanY), 3}
};
std::sort(ghostDistances, ghostDistances + 4, [](const GhostDistance& a, const GhostDistance& b) {
return a.distance < b.distance;
});
ghostDistances[0].ghost->isFast = true;
ghostDistances[1].ghost->isFast = true;
pthread_mutex_lock(&coutmutex);
cout << "---------closest ghost index selected----" << ghostDistances[0].index << endl;
cout << "---------closest ghost index selected----" << ghostDistances[1].index << endl;
pthread_mutex_unlock(&coutmutex);
}
void boostGhostSpeed(Ghost& ghost, int index) {//new
pthread_mutex_lock(&coutmutex);
cout << "---------------------------ghost getting speed BOOST--------------" << index << endl;
pthread_mutex_unlock(&coutmutex);
ghost.speedBoosted = true;
fastStartTime[index] = std::chrono::high_resolution_clock::now();
}
void resetGhostSpeed(Ghost& ghost, int index) {//new
pthread_mutex_lock(&coutmutex);
cout << "---------------------------ghost ENDING speed BOOSt---------------" << index << endl;
pthread_mutex_unlock(&coutmutex);
ghost.speedBoosted = false;
pthread_mutex_lock(&boostCountMutex);
completedSpeedBoosts++;
if (completedSpeedBoosts >= 2) {
completedSpeedBoosts = 0;
selectClosestFastGhosts();
pthread_mutex_lock(&coutmutex);
cout << "---------------------------Resetting FAST flags for all ghosts----------------" << endl;
pthread_mutex_unlock(&coutmutex);
}
pthread_mutex_unlock(&boostCountMutex);
}
bool isFastDurationElapsed(int index) {//new
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::seconds>(currentTime - fastStartTime[index]);
return elapsedTime.count() >= FAST_DURATION_SECONDS;
}
void* updateGhost(void* arg) {//new3
Ghost* ghost = (Ghost*)arg;
int ghostIndex = -1;
if (ghost == &g1) ghostIndex = 0;
else if (ghost == &g2) ghostIndex = 1;
else if (ghost == &g3) ghostIndex = 2;
else if (ghost == &g4) ghostIndex = 3;
while (1) {
sem_wait(&ghostSemaphore);
//agar ghost house k door par hey only then do the key permit stuff
if (ghost->x == 9 && (ghost->y == 10 || ghost->y == 12)) {
acquireResourcesAndLeave(ghostIndex);
}
if (ghost->isFast && !ghost->speedBoosted) {
pthread_mutex_lock(&coutmutex);
cout << "---------------------------ghost WAITING for speed BOOST----------------" << ghostIndex << endl;
pthread_mutex_unlock(&coutmutex);
sem_wait(&speedBoostSemaphore);
pthread_mutex_lock(&coutmutex);
cout << "---------------------------ghost IN Critical section----------------" << ghostIndex << endl;
pthread_mutex_unlock(&coutmutex);
boostGhostSpeed(*ghost, ghostIndex);
} else if (ghost->speedBoosted && isFastDurationElapsed(ghostIndex)) {
resetGhostSpeed(*ghost, ghostIndex);
sem_post(&speedBoostSemaphore);
}
switch (ghostIndex) {
case 0: moveGhost1(*ghost); break;
case 1: moveGhost2(*ghost); break;
case 2: moveGhost3(*ghost); break;
case 3: moveGhost4(*ghost); break;
}
glutPostRedisplay();
if (ghost->speedBoosted) {
usleep(BOOSTED_SLEEP_DURATION);
} else {
usleep(NORMAL_SLEEP_DURATION);
}
sem_post(&ghostSemaphore);
}
return NULL;
}
void* updatePacman(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex); //lock
pthread_mutex_lock(&coutmutex);
cout << "pacman " << endl;
pthread_mutex_unlock(&coutmutex);
sem_wait(&turnSemaphore); //wait for turn
movement();
pthread_mutex_unlock(&mutex); //unlock
sem_post(&turnSemaphore); //ab next turn possible hai
usleep(150000);
}
return NULL;
}
void startGhostThreads()
{
srand(time(NULL));//new
selectClosestFastGhosts();//new
pthread_create(&ghostThreads[0], NULL, updateGhost, (void*)&g1);
pthread_create(&ghostThreads[1], NULL, updateGhost, (void*)&g2);
pthread_create(&ghostThreads[2], NULL, updateGhost, (void*)&g3);
pthread_create(&ghostThreads[3], NULL, updateGhost, (void*)&g4);
usleep(100);
}
void stopGhostThreads()
{
for (int i = 0; i < 4; ++i)
{
pthread_cancel(ghostThreads[i]);
}
}
void startPacmanThread()
{
pthread_create(&pacmanThread, NULL, updatePacman, NULL);
usleep(100);
}
void stopPacmanThread()
{
pthread_cancel(pacmanThread);
}
void keyboard_callback(unsigned char key, int x, int y)
{
currentmove = key;
}
void* uifunc(void* arg)
{
while (1)
{
ui_function();
glutPostRedisplay();
usleep(80000);
}
return NULL;
}
void startuiThread()
{
pthread_create(&uiThread, NULL, uifunc, NULL);
}
void stopuiThread()
{
pthread_cancel(uiThread);
}
void* produce_pellet(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (pelletmoves == 18)
{
pacdots[pellet_consumed_x][pellet_consumed_y] = 2;
}
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void startPelletRespawn()
{
pthread_create(&pellet_respawn, NULL, produce_pellet, NULL);
}
void stopPelletRespawn()
{
pthread_cancel(pellet_respawn);
}
void* gameEngine(void* arg)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(1400, 800);
glutCreateWindow("Pacman");
glutDisplayFunc(ui_function);
glutReshapeFunc(reshape_callback); //callback func in header file
glutKeyboardFunc(keyboard_callback);
init();
startuiThread();
startGhostThreads();
startPacmanThread();
startPelletRespawn();
glutMainLoop();
stopPelletRespawn();
stopGhostThreads();
stopPacmanThread();
stopuiThread();
sem_post(&gameEngineSemaphore); //semaphore ends when game engine thread finishes
return NULL;
}
void startGameEngineThread()
{
sem_init(&gameEngineSemaphore, 0, 0);
sem_init(&ghostSemaphore, 0, 4);//new
sem_init(&speedBoostSemaphore, 0, 2);//new
pthread_mutex_init(&boostCountMutex, NULL);//new
sem_init(&turnSemaphore, 0, 5); //initialise turn semaphore with 5 to allow 5 turns initially
sem_init(&keySemaphore, 0, 2); //aik time par 2 ghosts can leave the house my marzi //new3
sem_init(&permitSemaphore, 0, 2); //new3
pthread_mutex_init(&resourceMutex, NULL); //new3
pthread_create(&gameEngineThread, NULL, gameEngine, NULL);
}
void stopGameEngineThread()
{
pthread_cancel(gameEngineThread);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
pthread_mutex_init(&mutex, NULL);
startGameEngineThread();
sem_wait(&gameEngineSemaphore); //this waits for the game engine thread to finish
pthread_mutex_destroy(&mutex);
sem_destroy(&gameEngineSemaphore);
sem_destroy(&ghostSemaphore);//new
sem_destroy(&speedBoostSemaphore);//new
pthread_mutex_destroy(&boostCountMutex);//new
sem_destroy(&turnSemaphore);
return 0;
}