mirror of
https://git.planet-casio.com/Slyvtt/Collab_RPG.git
synced 2024-12-29 13:03:43 +01:00
Début de débbugage de npc_pathfind
This commit is contained in:
parent
e7ada9d3da
commit
6de033d259
1 changed files with 25 additions and 19 deletions
44
src/npc.c
44
src/npc.c
|
@ -33,6 +33,8 @@ float length( float x, float y )
|
||||||
|
|
||||||
int npc_clear_path(NPC *npc)
|
int npc_clear_path(NPC *npc)
|
||||||
{
|
{
|
||||||
|
npc->currentPoint = 0;
|
||||||
|
npc->hasPath = 1;
|
||||||
npc->path_length = 0;
|
npc->path_length = 0;
|
||||||
free(npc->xpath);
|
free(npc->xpath);
|
||||||
free(npc->ypath);
|
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)
|
int npc_append_path(uint16_t x, uint16_t y, NPC *npc)
|
||||||
{
|
{
|
||||||
npc->path_length++;
|
npc->path_length++;
|
||||||
realloc(npc->xpath, npc->path_length);
|
npc->xpath = realloc(npc->xpath, npc->path_length);
|
||||||
realloc(npc->ypath, npc->path_length);
|
npc->ypath = realloc(npc->ypath, npc->path_length);
|
||||||
if(npc->xpath == NULL || npc->ypath == NULL) return 1;
|
if(npc->xpath == NULL || npc->ypath == NULL) return 1;
|
||||||
npc->xpath[npc->path_length-1] = x;
|
npc->xpath[npc->path_length-1] = x - npc->x;
|
||||||
npc->ypath[npc->path_length-1] = y;
|
npc->ypath[npc->path_length-1] = y - npc->y;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore)
|
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);
|
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)
|
uint16_t dest, NPC *npc)
|
||||||
{
|
{
|
||||||
if(npc_clear_path(npc)) return 1;
|
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
|
//Returns non zero error code on failure
|
||||||
//Custom a* implemetation
|
//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 npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc)
|
||||||
{
|
{
|
||||||
int i, j;
|
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;
|
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;
|
for(i=0; i<w*h; i++) gscore[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;
|
for(i=0; i<w*h; i++) fscore[i] = 255;
|
||||||
fscore[spos] = 0;
|
fscore[spos] = 0;
|
||||||
|
|
||||||
int tpos = spos;
|
|
||||||
uint8_t bscore;
|
uint8_t bscore;
|
||||||
int bx = x;
|
int bx = x;
|
||||||
int by = y;
|
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++)
|
for(i = bx-1; i < bx+1; i++)
|
||||||
{
|
{
|
||||||
for(j = by-1; j < by+1; j++)
|
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] + length(bx-i, by-j);
|
||||||
|
if(att_score < gscore[j*w+i])
|
||||||
{
|
{
|
||||||
if(map[j*w+i]) continue;
|
came_from[j*w+i] = by*w+bx;
|
||||||
att_score = gscore[by*w+bx] + lenght(bx-i, by-j);
|
gscore[j*w+i] = att_score;
|
||||||
if(att_score < gscore[j*w+i])
|
fscore[j*w+i] = att_score + length(i-dest_x, j-dest_y);
|
||||||
{
|
if(visited[j*w+i]) visited[j*w+i] = 0;
|
||||||
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;
|
as_clean(visited, gscore, fscore);
|
||||||
|
free(came_from);
|
||||||
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Refactoring to make adding complexity cleaner
|
//Refactoring to make adding complexity cleaner
|
||||||
|
|
Loading…
Reference in a new issue