npc : npc_remove correct et npc_remove_pos

This commit is contained in:
attilavs2 2024-08-02 09:29:41 +02:00
parent 77d3cc9c9b
commit 66c579db60
3 changed files with 33 additions and 6 deletions

View file

@ -2,8 +2,8 @@
#include "config.h" #include "config.h"
#include "map.h" #include "map.h"
#include "npc.h"
#include "mapdata.h" #include "mapdata.h"
#include "npc.h"
#include <gint/cpu.h> #include <gint/cpu.h>
#include <gint/display.h> #include <gint/display.h>
@ -201,11 +201,19 @@ void game_get_inputs(Game *game) {
mynpc->x = player->x; mynpc->x = player->x;
mynpc->y = player->x; mynpc->y = player->x;
mynpc->hasPath = 0; mynpc->hasPath = 0;
mynpc->face = 0; mynpc->face = 1;
mynpc->paused = 0; mynpc->paused = 0;
mynpc->has_dialog = 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 */ /* Display Debug Information on screen */
#if DEBUGMODE #if DEBUGMODE

View file

@ -29,15 +29,30 @@ NPC *npc_create() {
void npc_remove(NPC *npc) { void npc_remove(NPC *npc) {
uint32_t pos = (uint32_t)npc - (uint32_t)npc_stack; uint32_t pos = (uint32_t)npc - (uint32_t)npc_stack;
if(pos == 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--; npc_count--;
return; 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); } float length(float x, float y) { return sqrtf(x * x + y * y); }
int npc_clear_path(NPC *npc) { int npc_clear_path(NPC *npc) {

View file

@ -28,6 +28,10 @@ NPC *npc_create();
* Consider this as a free */ * Consider this as a free */
void npc_remove(NPC *npc); 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 /* Frees then malloc()s a new path to npc
* Useful if you want to safely edit a path */ * Useful if you want to safely edit a path */
int npc_clear_path(NPC *npc); int npc_clear_path(NPC *npc);