-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.c
More file actions
98 lines (93 loc) · 3.41 KB
/
pathfinding.c
File metadata and controls
98 lines (93 loc) · 3.41 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "raylib.h"
#include "raymath.h"
//Ej klar implementation av pathfinding med A*
typedef struct Vector2 v2f;
typedef struct Path
{
v2f position;
v2f firstPosition;
float pathDistance;
bool wall;
float pathWeight;
}Path;
typedef struct ValidPositions{
Path paths[8];
}ValidPositions;
float GetWeight(Path path, v2f target){
puts("get weight");
return path.pathDistance + Vector2Distance(path.position , target);
}
ValidPositions ValidPositionFinder(Room map, Path positionToCheck, v2f target){
puts("Valid position finder");
printf("position to check x%f y%f \n",positionToCheck.position.x,positionToCheck.position.y);
int roomPOSx =(int)((positionToCheck.position.x) / tileSize);
int roomPOSy =(int)((positionToCheck.position.y) / tileSize);
printf("x%d y%d\n",roomPOSx,roomPOSy);
ValidPositions validPositions;
int index = 0;
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j++){
validPositions.paths[index].wall=true;
if(i == 0 && j == 0){
continue;
}
if(map.data[roomPOSx-j][roomPOSy-i][0]==TILE_TYPE_EMPTY){
printf("%d\n",index);
validPositions.paths[index].wall=false;
float x = (float)(roomPOSx-j)*tileSize;
float y = (float)(roomPOSy-i)*tileSize;
v2f position = { x, y};
validPositions.paths[index].position = position;
printf("position is %f ,%f\n",position.x, position.y);
if(positionToCheck.pathDistance == 0){
validPositions.paths[index].firstPosition=position;
}
validPositions.paths[index].pathDistance = positionToCheck.pathDistance+ sqrt(i*i + j*j)*tileSize;
validPositions.paths[index].pathWeight = GetWeight(validPositions.paths[index], target);
}
else{
puts("wall");
}
index++;
}
}
puts("valid position found");
return validPositions;
}
int sortBasedOnWeight(Path* path1, Path* path2){
return (int)(path2->pathWeight - path1->pathWeight);
}
v2f PathFinder(Room map, v2f playerPosition, v2f enemyPosition){
puts("path finder");
int size = 0;
Path queue [784] = { 0 };
Path enemyPath = (Path){enemyPosition, {0,0} , 0, false, 0};
enemyPath.pathWeight=GetWeight(enemyPath,playerPosition);
queue[0]=enemyPath; size ++;
while (true)
{
printf("size %d \n",size);
Path positionToCheck = queue[size];
size --;
ValidPositions validPositions = ValidPositionFinder(map,positionToCheck,playerPosition);
for(int i = 0; i <8; i++){
if(validPositions.paths[i].wall){
continue;
}
// Check if we hit player
if((int)((validPositions.paths[i].position.x) / tileSize) == (int)(playerPosition.x / tileSize)&&(int)((validPositions.paths[i].position.y) / tileSize) == (int)(playerPosition.y / tileSize)){
puts("path found");
return validPositions.paths[i].firstPosition;
}
printf("add \n");
// Otherwise: Add position to queue
queue[size++] = validPositions.paths[i];
}
qsort(queue,size,sizeof(Path),sortBasedOnWeight);
}
}