-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacman.c
More file actions
1092 lines (988 loc) · 28.6 KB
/
Copy pathPacman.c
File metadata and controls
1092 lines (988 loc) · 28.6 KB
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
#define GLFW_INCLUDE_NONE
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <SOIL/SOIL.h>
#include "Pacman.h"
#include <SDL2/SDL_mixer.h> // Add SDL_mixer header
// Size of each block of the table
#define block 70
// Size of the table
#define N 20
//Size of each block of the table in the window
#define TAM 0.1f
//Functions that convert the line and column of the table in a coordinate
#define MAT2X(j) ((j)*0.1f-1)
#define MAT2Y(i) (0.9-(i)*0.1f)
// Structs used to control the game
struct TPoint {
int x, y;
};
const struct TPoint directions[4] = {{1,0},{0,1},{-1,0},{0,-1}};
struct TPacman {
int status;
int xi, yi, x, y;
int direction, step, partial;
int score;
int invencible;
int alive;
int animation;
};
struct TPhantom {
int status;
int xi, yi, x, y;
int direction, step, partial;
int decided_turn, begin_turn;
int actual_index;
int *path;
};
struct TVertex {
int x, y;
int neighbour[4];
};
struct TScenario {
int map[N][N];
int number_p;
int NV;
struct TVertex *graph;
};
// Textures and sounds
GLuint pacmanTex2d[12];
GLuint phantomTex2d[12];
GLuint mapTex2d[14];
GLuint startscreen, screenGameOver, background; // Add background texture
Mix_Chunk *movingSound = NULL; // Change to Mix_Chunk for playing on a specific channel
Mix_Chunk *phantomMovingSound = NULL; // Change to Mix_Chunk for playing on a specific channel
Mix_Chunk *pointSound = NULL; // Add point sound
Mix_Chunk *panicSound = NULL; // Add panic sound
static void drawSprite(float column, float line, GLuint tex);
static GLuint loadArqTexture(char *str);
void drawTypeScreen(float x, float y, float size, GLuint tex);
// Functions that load all the textures in the game
void loadTextures()
{
int i;
char str[50];
for (i = 0; i < 12; i++)
{
sprintf(str, ".//Textures//phantom%d.png", i);
phantomTex2d[i] = loadArqTexture(str);
}
for (i = 0; i < 12; i++)
{
sprintf(str, ".//Textures//pacman%d.png", i);
pacmanTex2d[i] = loadArqTexture(str);
}
for(i = 0; i < 14; i++)
{
sprintf(str, ".//Textures//mapa%d.png", i);
mapTex2d[i] = loadArqTexture(str);
}
startscreen = loadArqTexture(".//Textures//start.png");
screenGameOver = loadArqTexture(".//Textures//gameover.png");
background = loadArqTexture(".//Textures//screen.jpg"); // Load background texture
// Load moving sound
movingSound = Mix_LoadWAV(".//Sounds//moving.mp3");
if (movingSound == NULL) {
printf("Mix_LoadWAV failed: %s\n", Mix_GetError());
exit(1);
}
// Load phantom moving sound
phantomMovingSound = Mix_LoadWAV(".//Sounds//phantom_moving.mp3");
if (phantomMovingSound == NULL) {
printf("Mix_LoadWAV failed: %s\n", Mix_GetError());
exit(1);
}
// Load point sound
pointSound = Mix_LoadWAV(".//Sounds//point.mp3");
if (pointSound == NULL) {
printf("Mix_LoadWAV failed: %s\n", Mix_GetError());
exit(1);
}
// Load panic sound
panicSound = Mix_LoadWAV(".//Sounds//panic.mp3");
if (panicSound == NULL) {
printf("Mix_LoadWAV failed: %s\n", Mix_GetError());
exit(1);
}
}
static GLuint loadArqTexture(char *str)
{
// using soil (Simple OpenGL Image Loader)
GLuint tex = SOIL_load_OGL_texture
(
str,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y |
SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if(0 == tex)
{
printf("Error in SOIL: %s\n", SOIL_last_result());
}
return tex;
}
// Function that receives a line and a column from the table and a code of texture
// draws a square on screen with that texture
void drawSprite(float column, float line, GLuint tex)
{
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(column, line);
glTexCoord2f(1.0f, 0.0f); glVertex2f(column + TAM, line);
glTexCoord2f(1.0f, 1.0f); glVertex2f(column + TAM, line + TAM);
glTexCoord2f(0.0f, 1.0f); glVertex2f(column, line + TAM);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
void drawTypeScreen(float x, float y, float size, GLuint tex)
{
// Draw background first
glColor3f(1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, background);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glEnd();
// Draw the actual screen on top
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(x - size, y + size);
glTexCoord2f(1.0f, 1.0f); glVertex2f(x + size, y + size);
glTexCoord2f(1.0f, 0.0f); glVertex2f(x + size, y - size);
glTexCoord2f(0.0f, 0.0f); glVertex2f(x - size, y - size);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
void drawScreen(int type)
{
if (type == 0)
{
drawTypeScreen(0, 0, 1.0, startscreen);
}
else
{
drawTypeScreen(0, 0, 1.0, screenGameOver);
}
}
// Scenario
static int scenario_IsTurn(int x, int y, struct TScenario* scen);
static int scenario_CheckDirection(int mat[N][N], int y, int x, int direction);
static void scenario_buildGraph(struct TScenario* scen);
struct TScenario* scenario_load(char *archive)
{
int i, j;
FILE *arq = fopen(archive, "r");
if (arq == NULL) {
printf("Error loading scenario\n");
exit(1);
}
struct TScenario* scen = malloc(sizeof(struct TScenario));
if (scen == NULL) {
fprintf(stderr, "Memory allocation failed for Scenario\n");
exit(1);
}
scen->number_p = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (fscanf(arq, "%d", &scen->map[i][j]) != 1) {
fprintf(stderr, "Error reading map data\n");
free(scen); // Free allocated memory before exiting
exit(1);
}
if (scen->map[i][j] == 1 || scen->map[i][j] == 2)
{
scen->number_p++;
}
}
}
fclose(arq);
scenario_buildGraph(scen);
return scen;
}
// Free data from the scenario
void scenario_destroy(struct TScenario* scen)
{
free(scen->graph);
free(scen);
}
// Goes through the matrix of the game drawing sprites
void scenario_draw(struct TScenario* scen)
{
int i,j;
for (i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
drawSprite(MAT2X(j),MAT2Y(i),mapTex2d[scen->map[i][j]]);
}
}
}
// Verifies if a position (x, y) in the scenario is a turn and not a wall
static int scenario_IsTurn(int x, int y, struct TScenario* scen) {
int i, cont = 0;
int v[4];
// Iterates over the four possible directions (up, down, left, right)
for(i = 0; i < 4; i++) {
// Checks if the adjacent position in the current direction is a valid path (not a wall)
if (scen->map[y + directions[i].y][x + directions[i].x] <= 2) {
cont++; // Increment the valid path counter
v[i] = 1; // Mark the direction as valid
} else {
v[i] = 0; // Mark the direction as invalid (wall)
}
}
if (cont > 1)
{
if (cont == 2)
{
if ((v[0] == v[2] && v[0]) || (v[1] == v[3] && v[1]))
{
return 0;
}
else
{
return 1;
}
} else return 1;
}else return 0;
}
// Function that checks if it is possible to go in a given direction
static int scenario_CheckDirection(int mat[N][N], int y, int x, int direction)
{
int xt = x;
int yt = y;
while(mat[yt + directions[direction].y][xt + directions[direction].x] == 0)
{
yt = yt + directions[direction].y;
xt = xt + directions[direction].x;
}
if(mat[yt + directions[direction].y][xt + directions[direction].x] < 0)
{
return -1;
}
else
{
return mat[yt + directions[direction].y][xt + directions[direction].x] - 1;
}
}
// Given a scenario, builds the graph that will help the ghosts to
// come back to the beginning point
static void scenario_buildGraph(struct TScenario* scen)
{
int mat[N][N];
int i, j, k, idx, cont = 0;
// Initialize the matrix with -1
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
mat[i][j] = -1;
}
}
// Identify the vertices in the scenario
for (i = 1; i < N - 1; i++)
{
for (j = 1; j < N - 1; j++)
{
if (scen->map[i][j] <= 2)
{
if (scenario_IsTurn(j, i, scen))
{
cont++;
mat[i][j] = cont;
}
}
}
}
scen->NV = cont;
scen->graph = malloc(cont * sizeof(struct TVertex));
if (scen->graph == NULL)
{
fprintf(stderr, "Memory allocation failed for graph\n");
exit(1);
}
// Build the graph
for (i = 1; i < N - 1; i++)
{
for (j = 1; j < N - 1; j++)
{
if (mat[i][j] > 0)
{
idx = mat[i][j] - 1;
scen->graph[idx].x = j;
scen->graph[idx].y = i;
for (k = 0; k < 4; k++)
{
scen->graph[idx].neighbour[k] = scenario_CheckDirection(mat, i, j, k);
}
}
}
}
}
// Pacman functions
static int pacman_is_invencible(Pacman *pac);
static void pacman_dies(Pacman *pac);
static void pacman_scores_ghosts(Pacman *pac);
static void pacman_deathanimation(float column, float line, Pacman *pac);
// Add a static variable to keep track of the time since the last point was eaten
static int timeSinceLastPoint = 0;
// Functions that begin pacman's data
Pacman* pacman_create(int x, int y)
{
Pacman* pac = malloc(sizeof(Pacman));
if (pac == NULL) {
fprintf(stderr, "Memory allocation failed for Pacman\n");
exit(1);
}
if (pac != NULL)
{
pac->invencible = 0;
pac->score = 0;
pac->step = 4;
pac->alive = 1;
pac->status = 0;
pac->direction = 0;
pac->partial = 0;
pac->xi = x;
pac->yi = y;
pac->x = x;
pac->y = y;
}
return pac;
}
// Functions that free all data related to Pacman
void pacman_destroy(Pacman *pac)
{
free(pac);
}
// Function that verifies if pacman is alive or not
int pacman_alive(Pacman *pac)
{
if (pac->alive) {
return 1;
}
else
{
if (pac->animation > 60) { return 0; } else { return 1; }
}
}
// Function that verifies if pacman can go to a certain direction chosen
void pacman_ChangeDirections(Pacman *pac, int direction, struct TScenario *scen)
{
if (scen->map[pac->y + directions[direction].y][pac->x + directions[direction].x] <= 2) {
int di = abs(direction - pac->direction);
if (di != 2 && di != 0)
{
pac->partial = 0;
}
pac->direction = direction;
}
}
// Make the pacman move
void pacman_moving(Pacman *pac, struct TScenario *scen)
{
if (pac->alive == 0)
{
return;
}
// Changes his position inside a square in the matrix or changes squares
if (scen->map[pac->y + directions[pac->direction].y][pac->x + directions[pac->direction].x] <= 2)
{
if (pac->direction < 2)
{
pac->partial += pac->step;
if (pac->partial >= block)
{
pac->x += directions[pac->direction].x;
pac->y += directions[pac->direction].y;
pac->partial = 0;
}
}
else
{
pac->partial -= pac->step;
if (pac->partial <= -block)
{
pac->x += directions[pac->direction].x;
pac->y += directions[pac->direction].y;
pac->partial = 0;
}
}
}
// Eats a point in the map
if (scen->map[pac->y][pac->x] == 1 || scen->map[pac->y][pac->x] == 2)
{
if (Mix_Playing(0) == 0) { // Use channel 0 for Pacman moving sound
if (Mix_PlayChannel(0, movingSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
if (scen->map[pac->y][pac->x] == 1) {
pac->score += 10;
} else if (scen->map[pac->y][pac->x] == 2) {
pac->score += 50;
pac->invencible = 1000;
// Play the point sound
if (Mix_PlayChannel(-1, pointSound, 0) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
// Stop phantom moving sound and play panic sound
Mix_HaltChannel(1);
if (Mix_PlayChannel(1, panicSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
scen->map[pac->y][pac->x] = 0; // Update the map to indicate the point has been eaten
scen->number_p--;
timeSinceLastPoint = 0; // Reset the timer since Pacman ate a point
} else {
timeSinceLastPoint++;
if (timeSinceLastPoint > 100) { // Adjust the threshold as needed
if (Mix_Playing(0) != 0) {
Mix_HaltChannel(0); // Stop the music if Pacman is not eating points
}
}
}
}
// Functions that draw pacman
void pacman_draw(Pacman *pac)
{
float line, column;
float step = (pac->partial / (float)block);
// Checks position
if (pac->direction == 0 || pac->direction == 2)
{
line = pac->y;
column = pac->x + step;
}
else
{
line = pac->y + step;
column = pac->x;
}
if (pac->alive)
{
// Choose the sprite based on the direction
int idx = 2 * pac->direction;
// Choose if draws with open or closed mouth
if (pac->status < 15)
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[idx]);
}
else
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[idx + 1]);
}
// Alternates between open and closed mouth
pac->status = (pac->status + 1) % 30;
if (pac->invencible > 0)
{
pac->invencible--;
}
else
{
// Shows death animation
pacman_deathanimation(column, line, pac);
}
}
}
static int pacman_is_invencible(Pacman *pac)
{
return pac->invencible > 0;
}
static void pacman_dies(Pacman *pac)
{
if (pac->alive)
{
pac->alive = 0;
pac->animation = 0;
}
}
static void pacman_scores_ghosts(Pacman *pac)
{
pac->score += 100;
}
static void pacman_deathanimation(float column, float line, Pacman *pac)
{
pac->animation++;
// Checks which ones of the sprites should be drawn to give the effect of
// pacman slowly fading away
if (pac->animation < 15)
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[8]);
}
else
{
if (pac->animation < 30)
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[9]);
}
else
{
if (pac->animation < 45)
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[10]);
}
else
{
drawSprite(MAT2X(column), MAT2Y(line), pacmanTex2d[11]);
}
}
}
}
// NEXT IS PHANTOM
static void phantom_move(Phantom *ph, int direction, struct TScenario *scen);
static int phantom_DirectionGraph(Phantom *ph, struct TScenario *scen);
static int phantom_DistanceGraph(struct TScenario *scen, int noA, int noB);
static int phantom_DrawsDirection(Phantom *ph, struct TScenario *scen);
static int phantom_SeePacman(Phantom *ph, Pacman *pac, struct TScenario *scen, int direction);
static void phantom_SearchBestPath(Phantom *ph, struct TScenario *scen);
static int phantom_MovingPhantomAlive(Phantom *ph, Pacman *pac, struct TScenario *scen);
static int phantom_MovingPhantomDead(Phantom *ph, struct TScenario *scen);
// Function that initializes the data from the phantom
Phantom* phantom_create(int x, int y)
{
Phantom* ph = malloc(sizeof(Phantom));
if (ph == NULL) {
fprintf(stderr, "Memory allocation failed for Phantom\n");
exit(1);
}
if (ph != NULL)
{
ph->step = 3;
ph->decided_turn = 0;
ph->begin_turn = 0;
ph->actual_index = 0;
ph->status = 0;
ph->direction = 0;
ph->partial = 0;
ph->xi = x;
ph->yi = y;
ph->x = x;
ph->y = y;
ph->path = NULL;
}
return ph;
}
// Function that liberates the data from the phantom
void phantom_destroy(Phantom *ph)
{
if (ph->path != NULL)
{
free(ph->path);
}
free(ph);
}
// Function that draws a phantom
void phantom_draw(Phantom *ph)
{
float line, column;
float step = (ph->partial / (float)block);
// Check position
if (ph->direction == 0 || ph->direction == 2)
{
line = ph->y;
column = ph->x + step;
}
else
{
line = ph->y + step;
column = ph->x;
}
// Chooses the sprite based on the direction and status (alive, dead, vulnerable)
int idx = 3 * ph->direction + ph->status;
drawSprite(MAT2X(column), MAT2Y(line), phantomTex2d[idx]);
}
// Updates the position of a phantom
void phantom_moving(Phantom *ph, struct TScenario *scen, Pacman *pac)
{
int d = 0; // Initialize variable d
if (ph->status == 1)
{
// If a phantom is dead, he comes back through the path of the graph
d = phantom_MovingPhantomDead(ph, scen);
}
else
{
// Run or pursue Pacman? ? or 0
if (pacman_is_invencible(pac))
{
ph->status = 2;
// Stop phantom moving sound and play panic sound
if (Mix_Playing(1) != 0 && Mix_GetChunk(1) == phantomMovingSound) {
Mix_HaltChannel(1);
if (Mix_PlayChannel(1, panicSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
}
else
{
ph->status = 0;
// Resume phantom moving sound if panic sound was playing
if (Mix_Playing(1) != 0 && Mix_GetChunk(1) == panicSound) {
Mix_HaltChannel(1);
if (Mix_PlayChannel(1, phantomMovingSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
}
// Calls the function that calculates the new direction to where the phantom
// should be moving based on the map and the position of pacman
d = phantom_MovingPhantomAlive(ph, pac, scen);
// What to do if touches pacman?
if (pac->x == ph->x && pac->y == ph->y)
{
if (pacman_is_invencible(pac))
{
ph->status = 1; // he is dead
pacman_scores_ghosts(pac);
ph->begin_turn = 0;
}
else
{
if (pacman_alive(pac))
{
pacman_dies(pac);
}
}
}
}
// Moving and drawing the phantom on screen
phantom_move(ph, d, scen);
// Play the phantom moving sound if not invincible
if (!pacman_is_invencible(pac) && Mix_Playing(1) == 0) { // Use channel 1 for phantom moving sound
if (Mix_PlayChannel(1, phantomMovingSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
}
// Updates the position of a phantom
static void phantom_move(Phantom *ph, int direction, struct TScenario *scen)
{
int xt = ph->x;
int yt = ph->y;
// Increments the position inside of a square of the matrix or changes the square
if (scen->map[ph->y + directions[direction].y][ph->x + directions[direction].x] <= 2)
{
if (direction == ph->direction)
{
if (ph->direction < 2)
{
ph->partial += ph->step;
if (ph->partial >= block)
{
ph->x += directions[direction].x;
ph->y += directions[direction].y;
ph->partial = 0;
}
}
else
{
ph->partial -= ph->step;
if (ph->partial <= -block)
{
ph->x += directions[direction].x;
ph->y += directions[direction].y;
ph->partial = 0;
}
}
}
else
{
// Change direction...
if (abs(direction - ph->direction) != 2)
{
ph->partial = 0;
}
ph->direction = direction;
}
}
if (xt != ph->x || yt != ph->y)
{
ph->decided_turn = 0;
}
// Play the phantom moving sound
if (Mix_Playing(1) == 0) { // Use channel 1 for phantom moving sound
if (Mix_PlayChannel(1, phantomMovingSound, -1) == -1) {
printf("Mix_PlayChannel failed: %s\n", Mix_GetError());
}
}
}
// Function that helps to choose the path when the phantom dies
static int phantom_DirectionGraph(Phantom *ph, struct TScenario *scen)
{
if (scen->graph[ph->actual_index].x == scen->graph[ph->path[ph->actual_index]].x)
{
if (scen->graph[ph->actual_index].y > scen->graph[ph->path[ph->actual_index]].y)
{
return 3;
}
else
{
return 1;
}
}
else
{
if (scen->graph[ph->actual_index].x > scen->graph[ph->path[ph->actual_index]].x)
{
return 2;
}
else
{
return 0;
}
}
}
// Function that helps in choosing the path when the phantom dies
static int phantom_DistanceGraph(struct TScenario *scen, int noA, int noB)
{
return abs(scen->graph[noA].x - scen->graph[noB].x) + abs(scen->graph[noA].y - scen->graph[noB].y);
}
// When the phantom finds a turn, he draws a new direction
// He tends to move forward, sometimes change directions and almost never go back the same path
static int phantom_DrawsDirection(Phantom *ph, struct TScenario *scen)
{
int i, j, k = 0, max; // Initialize k to avoid uninitialized warning
int chances[4], dir[4];
for (i = 0; i < 4; i++)
{
chances[i] = rand() % 10 + 1;
}
chances[ph->direction] = 7;
chances[(ph->direction + 2) % 4] = 3;
// Orders the chances of each direction
for (j = 0; j < 4; j++)
{
max = 0;
for (i = 0; i < 4; i++)
{
if (chances[i] > max)
{
max = chances[i];
k = i;
}
}
dir[j] = k;
chances[k] = 0;
}
// Chooses the first valid direction
i = 0;
while (scen->map[ph->y + directions[dir[i]].y][ph->x + directions[dir[i]].x] > 2)
{
i++;
}
return dir[i];
}
// Function that groups all the cases of choice of direction of the phantom
static int phantom_MovingPhantomAlive(Phantom *ph, Pacman *pac, struct TScenario *scen)
{
int d = 0;
int i;
if (scenario_IsTurn(ph->x, ph->y, scen))
{
if (!ph->decided_turn)
{
// On turn if Pacman, then pursue
d = -1;
for (i = 0; i < 4; i++)
{
if (phantom_SeePacman(ph, pac, scen, i))
{
d = i;
}
}
// Turn: Did not see pacman, draw a direction
if (d == -1)
{
d = phantom_DrawsDirection(ph, scen);
}
else
{
// Turn: saw pacman, but he is invincible
// The RUN THE F*** OUT MAN!! kkkk, draws new direction
if (pacman_is_invencible(pac))
{
i = d;
while (i == d)
{
d = phantom_DrawsDirection(ph, scen);
}
}
}
ph->decided_turn = 1;
}
else
{
d = ph->direction;
}
}
else
{
// There is no turn: keep the same path
ph->decided_turn = 0;
d = ph->direction;
if (pacman_is_invencible(pac))
{
// If saw pacman ahead and he is invincible, go opposite way
if (phantom_SeePacman(ph, pac, scen, d))
{
d = (d + 2) % 4;
}
}
// Invert direction
if (scen->map[ph->y + directions[d].y][ph->x + directions[d].x] > 2)
{
d = (d + 2) % 4;
}
}
return d;
}
// Function that treats the cases where the phantom saw pacman
static int phantom_SeePacman(Phantom *ph, Pacman *pac, struct TScenario *scen, int direction)
{
int continum = 0;
if (direction == 0 || direction == 2)
{
if (pac->y == ph->y)
{
continum = 1;
}
}
else
{
if (pac->x == ph->x)
{
continum = 1;
}
}
if (continum)
{
int xt = ph->x;
int yt = ph->y;
while (scen->map[yt + directions[direction].y][xt + directions[direction].x] <= 2)
{
yt = yt + directions[direction].y;
xt = xt + directions[direction].x;
if (xt == pac->x && yt == pac->y)
{
return 1;
}
}
}
return 0;
}
// Function that helps in searching the best path when the phantom dies
static void phantom_SearchBestPath(Phantom *ph, struct TScenario *scen)
{
int i, k, index_on = -1; // Initialize index_on to avoid uninitialized warning
int continum, d;
int *dist;
dist = malloc(scen->NV * sizeof(int));
if (ph->path == NULL)
{