From c0d0c23ef99e36a6de85a558491a42ae98bc107b Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 23 Jul 2024 17:54:22 +0200 Subject: [PATCH] =?UTF-8?q?npc=5Fpathfind=20compl=C3=A9t=C3=A9*=20*Test?= =?UTF-8?q?=C3=A9=20avec=20un=20nombre=20limit=C3=A9=20de=20cas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/npc.c | 78 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/npc.c b/src/npc.c index 46dbe26..8f84f66 100644 --- a/src/npc.c +++ b/src/npc.c @@ -48,8 +48,8 @@ int npc_clear_path(NPC *npc) int npc_append_path(uint16_t x, uint16_t y, NPC *npc) { npc->path_length++; - npc->xpath = realloc(npc->xpath, npc->path_length); - npc->ypath = realloc(npc->ypath, npc->path_length); + npc->xpath = realloc(npc->xpath, npc->path_length*2); + npc->ypath = realloc(npc->ypath, npc->path_length*2); if(npc->xpath == NULL || npc->ypath == NULL) return 1; npc->xpath[npc->path_length-1] = x - npc->x; npc->ypath[npc->path_length-1] = y - npc->y; @@ -63,36 +63,54 @@ void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore) free(fscore); } -int as_reconstruct_path(int16_t *came_from, int w, int h, uint16_t spos, - uint16_t dest, NPC *npc) +int as_reconstruct_path(int16_t *came_from, int w, int h, int16_t spos, + int16_t dest, NPC *npc) { - if(npc_clear_path(npc)) return 1; + if(npc_clear_path(npc) goto as_recons_fail; - uint16_t next = came_from[dest]; + int16_t next = came_from[dest]; - int returnv = 0; + int i; - while(true) + for(i = 0; i < 64; i++) { - if(npc_append_path(next%w, next/h, npc)) - { - returnv = 1; - break; - } - if(next == dest){ - returnv = 0; - break; - } + if(next == -1) goto as_recons_fail; + if(npc_append_path(next%w, next/h, npc)) goto as_recons_fail; + next = came_from[next]; + if(next == spos){ + if(npc_append_path(spos%w, spos/h, npc)) + goto as_recons_fail; + break; + } } + + uint16_t tx, ty; + + //Flip the path because it started from the end + + for(i = 1; i < npc->path_length/2; i++) + { + tx = npc->xpath[i]; + ty = npc->ypath[i]; + npc->xpath[i] = npc->xpath[npc->path_length-i]; + npc->ypath[i] = npc->ypath[npc->path_length-i]; + npc->ypath[npc->path_length-i] = tx; + npc->ypath[npc->path_length-i] = ty; + } + + return 0; + + as_recons_fail: + free(came_from); - return returnv; + return 1; } //Returns non zero error code on failure //Custom a* implemetation -//(doesn't work yet) +//Unoptimized, may become an issue int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc) { int i, j; @@ -110,6 +128,7 @@ int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc) uint8_t *visited = malloc(w*h); for(i=0; i bscore) continue; bx = i%w; by = i/w; @@ -148,25 +168,29 @@ int npc_pathfind(int dest_x, int dest_y, Map *full_map, NPC *npc) int att_score; - for(i = bx-1; i < bx+1; i++) + for(i = bx-1; i < bx+2; i++) { - if(i > w || i < 0) continue; - for(j = by-1; j < by+1; j++) + if(i < 0) continue; + if(i > w) break; + for(j = by-1; j < by+2; j++) { - if(j > h || j < 0) continue; + if(j > h ) break; + if(j < 0) continue; if(map[j*w+i]) continue; - att_score = gscore[by*w+bx] + length(bx-i, by-j); + att_score = gscore[by*w+bx] + ceil(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 + length(i-dest_x, j-dest_y); + fscore[j*w+i] = att_score + ceil(length(dest_x-i, dest_y-j)); if(visited[j*w+i]) visited[j*w+i] = 0; } } } } + as_clean(visited, gscore, fscore); + free(came_from); return 3; }