Pathfinding NPC complet (a tester)

This commit is contained in:
attilavs2 2024-07-21 11:50:12 +02:00
parent 96f413eb14
commit e7ada9d3da

108
src/npc.c
View file

@ -53,9 +53,38 @@ int npc_append_path(uint16_t x, uint16_t y, NPC *npc)
npc->ypath[npc->path_length-1] = y; npc->ypath[npc->path_length-1] = y;
} }
uint8_t *as_reconstruct_path(uint8_t *came_from, int came_size, uint8_t *current, int curr_size) void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore)
{ {
free(visited);
free(gscore);
free(fscore);
}
int as_reconstruct_path(uint16_t *came_from, int w, int h, uint16_t spos,
uint16_t dest, NPC *npc)
{
if(npc_clear_path(npc)) return 1;
uint16_t next = came_from[dest];
int returnv = 0;
while(true)
{
if(npc_append_path(next%w, next/h, npc))
{
returnv = 1;
break;
}
if(next == dest){
returnv = 0;
break;
}
next = came_from[next];
}
free(came_from);
return returnv;
} }
//Returns non zero error code on failure //Returns non zero error code on failure
@ -63,60 +92,75 @@ uint8_t *as_reconstruct_path(uint8_t *came_from, int came_size, uint8_t *current
//(Very much unfinished for now) //(Very much unfinished for now)
int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc) int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc)
{ {
int i, j;
uint32_t w = full_map->w; uint32_t w = full_map->w;
uint32_t h = full_map->h; uint32_t h = full_map->h;
uint32_t x = npc->x; uint32_t x = floor(npc->curx);
uint32_t y = npc->y; uint32_t y = floor(npc->cury);
uint32_t spos = y*w+x; uint32_t spos = y*w+x;
uint8_t *map = full_map->walkable; uint8_t *map = full_map->walkable;
if(map[spos]) return 2; if(map[spos]) return 2;
if(map[dest_y*w+dest_x]) return 2;
npc_clear_path(npc); npc_clear_path(npc);
uint8_t *came_from = malloc(w*h); uint8_t *visited = malloc(w*h);
for(i=0; i<w*h; i++) visited[i] = 1;
int16_t *came_from = malloc(w*h*2);
for(i=0; i<w*h; i++) came_from[i] = -1;
uint8_t *gscore = malloc(w*h); uint8_t *gscore = malloc(w*h);
for(i=0; i<w*h; i++) fscore[i] = 255;
gscore[spos] = 0; gscore[spos] = 0;
uint8_t *fscore = malloc(w*h); uint8_t *fscore = malloc(w*h);
for(i=0; i<w*h; i++) fscore[i] = 255;
fscore[spos] = 0; fscore[spos] = 0;
int tx = x;
int ty = y;
int tpos = spos; int tpos = spos;
int bx; uint8_t bscore;
int by; int bx = x;
int i, j; int by = y;
for(int iter=0; iter < 1024; iter++) for(int iter=0; iter < 1024; iter++)
{ {
//Check surronding tiles for cost bscore = 255;
bx = x-1; //Cheapest known tile
by = y-1; for(i = 0; i < w*h; i++)
for(i = tx-1; i < tx+1; i++)
{ {
//Out of bounds (admissible) if(visited[i]) continue;
if(i < 0) continue; if(fscore[i] > bscore) continue;
//Out of bounds (unforgiveable) bx = i%w;
if(i > w) break; by = i/w;
for(j = ty-1; j < ty+1; j++) bscore = fscore[i];
}
if(bx == dest_x && by == dest_y)
{ {
//Out of bounds (admissible) as_clean(visited, gscore, fscore);
if(j < 0) continue; return as_reconstruct_path(came_from, w, h, spos, dest_y*w+dest_x, npc);
//Out of bounds (joever for it)
if(j > h) break;
//Has collision
if(map[j*w+i]) continue;
if(i == dest_x && j == dest_y){
return npc_append_path(i, j, npc);
}
//Calculate score (direct distance)
int deltX = tx-dest_x;
int deltY = ty-dest_y;
int tscore = sqrtf(deltX*deltX+deltY*deltY);
}
} }
visited[by*w+bx] = 1;
int att_score;
for(i = bx-1; i < bx+1; i++)
{
for(j = by-1; j < by+1; j++)
{
if(map[j*w+i]) continue;
att_score = gscore[by*w+bx] + lenght(bx-i, by-j);
if(att_score < gscore[j*w+i])
{
came_from[j*w+i] = by*w+bx;
gscore[j*w+i] = att_score;
fscore[j*w+i] = att_score + lenght(i-dest_x, j-dest_y);
if(visited[j*w+i]) visited[j*w+i] = 0;
}
}
}
} }
return 1; return 1;
} }