From 66c579db607bd13f2c3556bfeb4d1c1f82a2806c Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Fri, 2 Aug 2024 09:29:41 +0200 Subject: [PATCH] npc : npc_remove correct et npc_remove_pos --- src/game.c | 12 ++++++++++-- src/npc.c | 23 +++++++++++++++++++---- src/npc.h | 4 ++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/game.c b/src/game.c index 36e86b3..9741be7 100644 --- a/src/game.c +++ b/src/game.c @@ -2,8 +2,8 @@ #include "config.h" #include "map.h" -#include "npc.h" #include "mapdata.h" +#include "npc.h" #include #include @@ -201,11 +201,19 @@ void game_get_inputs(Game *game) { mynpc->x = player->x; mynpc->y = player->x; mynpc->hasPath = 0; - mynpc->face = 0; + mynpc->face = 1; mynpc->paused = 0; mynpc->has_dialog = 0; + mynpc->xpath = NULL; + mynpc->ypath = NULL; } } + if(keydown(KEY_F2)) { + npc_remove_pos(0); + /*while(keydown(KEY_F2)) { + clearevents(); + }*/ + } /* Display Debug Information on screen */ #if DEBUGMODE diff --git a/src/npc.c b/src/npc.c index b9cf88e..44bfb32 100644 --- a/src/npc.c +++ b/src/npc.c @@ -29,15 +29,30 @@ NPC *npc_create() { void npc_remove(NPC *npc) { uint32_t pos = (uint32_t)npc - (uint32_t)npc_stack; - if(pos == npc_count) { - npc_count--; + if(pos >= NPC_STACK_SIZE) + return; + + if(npc->xpath) + free(npc->xpath); + if(npc->ypath) + free(npc->ypath); + + if(pos == NPC_STACK_SIZE) { + if(npc_count) + npc_count--; return; } - memmove(npc, npc + sizeof(NPC), sizeof(NPC) * (npc_count - pos)); + uint32_t move_size = sizeof(NPC) * (npc_count - pos); + if(move_size + pos > NPC_STACK_SIZE) + move_size = NPC_STACK_SIZE - pos; - return; + memmove(npc, (void *)(npc + sizeof(NPC)), move_size); + if(npc_count) + npc_count--; } +void npc_remove_pos(uint32_t pos) { npc_remove(&npc_stack[pos]); } + float length(float x, float y) { return sqrtf(x * x + y * y); } int npc_clear_path(NPC *npc) { diff --git a/src/npc.h b/src/npc.h index 8c7f83e..74e5a57 100644 --- a/src/npc.h +++ b/src/npc.h @@ -28,6 +28,10 @@ NPC *npc_create(); * Consider this as a free */ void npc_remove(NPC *npc); +/* Pops the NPC at pos from the NPC stack + * Consider this as a free*/ +void npc_remove_pos(uint32_t pos); + /* Frees then malloc()s a new path to npc * Useful if you want to safely edit a path */ int npc_clear_path(NPC *npc);