Début de débbugage de npc_pathfind

This commit is contained in:
attilavs2 2024-07-21 12:26:41 +02:00
parent e7ada9d3da
commit 6de033d259

View file

@ -33,6 +33,8 @@ float length( float x, float y )
int npc_clear_path(NPC *npc)
{
npc->currentPoint = 0;
npc->hasPath = 1;
npc->path_length = 0;
free(npc->xpath);
free(npc->ypath);
@ -46,11 +48,12 @@ int npc_clear_path(NPC *npc)
int npc_append_path(uint16_t x, uint16_t y, NPC *npc)
{
npc->path_length++;
realloc(npc->xpath, npc->path_length);
realloc(npc->ypath, npc->path_length);
npc->xpath = realloc(npc->xpath, npc->path_length);
npc->ypath = realloc(npc->ypath, npc->path_length);
if(npc->xpath == NULL || npc->ypath == NULL) return 1;
npc->xpath[npc->path_length-1] = x;
npc->ypath[npc->path_length-1] = y;
npc->xpath[npc->path_length-1] = x - npc->x;
npc->ypath[npc->path_length-1] = y - npc->y;
return 0;
}
void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore)
@ -60,7 +63,7 @@ void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore)
free(fscore);
}
int as_reconstruct_path(uint16_t *came_from, int w, int h, uint16_t spos,
int as_reconstruct_path(int16_t *came_from, int w, int h, uint16_t spos,
uint16_t dest, NPC *npc)
{
if(npc_clear_path(npc)) return 1;
@ -89,7 +92,7 @@ int as_reconstruct_path(uint16_t *came_from, int w, int h, uint16_t spos,
//Returns non zero error code on failure
//Custom a* implemetation
//(Very much unfinished for now)
//(doesn't work yet)
int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc)
{
int i, j;
@ -112,14 +115,13 @@ int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc)
for(i=0; i<w*h; i++) came_from[i] = -1;
uint8_t *gscore = malloc(w*h);
for(i=0; i<w*h; i++) fscore[i] = 255;
for(i=0; i<w*h; i++) gscore[i] = 255;
gscore[spos] = 0;
uint8_t *fscore = malloc(w*h);
for(i=0; i<w*h; i++) fscore[i] = 255;
fscore[spos] = 0;
int tpos = spos;
uint8_t bscore;
int bx = x;
int by = y;
@ -148,21 +150,25 @@ int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc)
for(i = bx-1; i < bx+1; i++)
{
if(i > w || i < 0) continue;
for(j = by-1; j < by+1; j++)
{
if(j > h || j < 0) continue;
if(map[j*w+i]) continue;
att_score = gscore[by*w+bx] + lenght(bx-i, by-j);
att_score = gscore[by*w+bx] + length(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);
fscore[j*w+i] = att_score + length(i-dest_x, j-dest_y);
if(visited[j*w+i]) visited[j*w+i] = 0;
}
}
}
}
return 1;
as_clean(visited, gscore, fscore);
free(came_from);
return 3;
}
//Refactoring to make adding complexity cleaner