mirror of
https://git.planet-casio.com/Slyvtt/Collab_RPG.git
synced 2024-12-28 20:43:42 +01:00
npc : Suivi sans chemin fonctionel (sans collisions)
This commit is contained in:
parent
7094d4b4e3
commit
1767643da1
3 changed files with 65 additions and 16 deletions
|
@ -269,8 +269,8 @@ void game_get_inputs(Game *game) {
|
|||
if(mynpc) {
|
||||
mynpc->curx = (player->x << PRECISION) / PXSIZE;
|
||||
mynpc->cury = (player->y << PRECISION) / PXSIZE;
|
||||
mynpc->x = player->x;
|
||||
mynpc->y = player->x;
|
||||
mynpc->x = 0;
|
||||
mynpc->y = 0;
|
||||
mynpc->hasPath = 0;
|
||||
mynpc->owns_path = false;
|
||||
mynpc->face = 0;
|
||||
|
|
71
src/npc.c
71
src/npc.c
|
@ -74,7 +74,7 @@ int npc_clear_path(NPC *npc) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int npc_append_path(uint16_t x, uint16_t y, NPC *npc) {
|
||||
int npc_append_path(int16_t x, int16_t y, NPC *npc) {
|
||||
npc->xpath = realloc(npc->xpath, npc->path_length * 2 + 2);
|
||||
npc->ypath = realloc(npc->ypath, npc->path_length * 2 + 2);
|
||||
if(npc->xpath == NULL || npc->ypath == NULL)
|
||||
|
@ -91,6 +91,50 @@ void as_clean(uint8_t *visited, uint8_t *gscore, uint8_t *fscore) {
|
|||
free(fscore);
|
||||
}
|
||||
|
||||
int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc){
|
||||
uint8_t *map = full_map->walkable;
|
||||
uint32_t w = full_map->w;
|
||||
uint32_t h = full_map->h;
|
||||
uint32_t sx = npc_from_curxy(npc->curx);
|
||||
uint32_t sy = npc_from_curxy(npc->cury);
|
||||
dest_x /= PXSIZE;
|
||||
dest_y /= PXSIZE;
|
||||
|
||||
if(dest_x < 0 || dest_y < 0 || dest_x >= w*T_WIDTH || dest_y >= h*T_HEIGHT)
|
||||
return 2;
|
||||
|
||||
/*if(map[dest_y*w + dest_x])
|
||||
return 2;
|
||||
if(map[sy*w + sx])
|
||||
return 2;*/
|
||||
|
||||
if(!npc->owns_path || npc->path_length > 64)
|
||||
npc_clear_path(npc);
|
||||
|
||||
/*int bx;
|
||||
int by;
|
||||
int bscore = 0x7FFFFFFF;
|
||||
|
||||
for(int i = sx-1; i < sx+2; i++){
|
||||
for(int j = sy-1; j < sy+2; j++){
|
||||
int tscore = (dest_x-sx)*(dest_x-sx) + (dest_y-sy)*(dest_y-sy);
|
||||
if(tscore < bscore){
|
||||
bscore = tscore;
|
||||
bx = i;
|
||||
by = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(bscore == 0x7FFFFFFF) return 3;*/
|
||||
|
||||
if(npc_append_path(dest_x,dest_y, npc))
|
||||
return 1;
|
||||
|
||||
npc->hasPath = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int as_reconstruct_path(int16_t *came_from, int w, int h, int16_t start,
|
||||
int16_t dest, bool is_alloc, NPC *npc) {
|
||||
if(npc_clear_path(npc))
|
||||
|
@ -132,8 +176,8 @@ int as_reconstruct_path(int16_t *came_from, int w, int h, int16_t start,
|
|||
npc->ypath[npc->path_length - i - 1] = ty;
|
||||
}*/
|
||||
|
||||
if(is_alloc)
|
||||
free(came_from);
|
||||
/*if(is_alloc)
|
||||
free(came_from);*/
|
||||
|
||||
npc->hasPath = true;
|
||||
|
||||
|
@ -141,8 +185,8 @@ int as_reconstruct_path(int16_t *came_from, int w, int h, int16_t start,
|
|||
|
||||
as_recons_fail:
|
||||
|
||||
if(is_alloc)
|
||||
free(came_from);
|
||||
/*if(is_alloc)
|
||||
free(came_from);*/
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -150,8 +194,8 @@ as_recons_fail:
|
|||
uint32_t xyram = 0xe500e000 + 32;
|
||||
|
||||
/* Custom a* implemetation
|
||||
* Unoptimized, may become an issue */
|
||||
int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) {
|
||||
* Borked, use npc_pathfind instead*/
|
||||
int __npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) {
|
||||
int32_t i, j;
|
||||
|
||||
int32_t w = full_map->w/T_WIDTH/PXSIZE;
|
||||
|
@ -180,7 +224,7 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) {
|
|||
|
||||
bool is_alloc;
|
||||
|
||||
if(1 || isSH3() || w * h * 5 > 1024 * 15) {
|
||||
if(isSH3() || w * h * 5 > 1024 * 15) {
|
||||
is_alloc = true;
|
||||
|
||||
visited = malloc(w * h);
|
||||
|
@ -242,9 +286,9 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) {
|
|||
bscore = fscore[i];
|
||||
}
|
||||
if(bx == dest_x && by == dest_y) {
|
||||
as_clean(visited, gscore, fscore);
|
||||
if(is_alloc) as_clean(visited, gscore, fscore);
|
||||
return 0; /*as_reconstruct_path(came_from, w, h, spos,
|
||||
dest_y * w + dest_x, npc)*/
|
||||
dest_y * w + dest_x, is_alloc npc)*/
|
||||
}
|
||||
|
||||
visited[by * w + bx] = 1;
|
||||
|
@ -274,9 +318,10 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) {
|
|||
}
|
||||
}
|
||||
|
||||
as_clean(visited, gscore, fscore);
|
||||
|
||||
free(came_from);
|
||||
if(is_alloc){
|
||||
as_clean(visited, gscore, fscore);
|
||||
free(came_from);
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
/*Maximum iterations before the pathfinding considers the target unreacheable*/
|
||||
#define PATHFIND_MAX_ITER 64
|
||||
|
||||
#define npc_from_curxy(a) (((a)>>PRECISION)/PXSIZE)
|
||||
|
||||
#define npc_to_curxy(a) (((a)*PXSIZE)<<PRECISION)
|
||||
|
||||
enum {
|
||||
|
||||
NPC_NONE = 0,
|
||||
|
@ -41,7 +45,7 @@ int npc_clear_path(NPC *npc);
|
|||
/* Adds point x,y to the path of npc
|
||||
* Won't work on static NPCs, use npc_clear_path before or make them on the
|
||||
* heap */
|
||||
int npc_append_path(uint16_t x, uint16_t y, NPC *npc);
|
||||
int npc_append_path(int16_t x, int16_t y, NPC *npc);
|
||||
|
||||
/* Clears the NPCs path and creates a new one going to dest,
|
||||
* avoiding non-walkable tiles
|
||||
|
|
Loading…
Reference in a new issue