diff --git a/src/npc.c b/src/npc.c index 2910198..46dbe26 100644 --- a/src/npc.c +++ b/src/npc.c @@ -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 || 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; - 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; - } + came_from[j*w+i] = by*w+bx; + gscore[j*w+i] = att_score; + 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