From 6cfe0ab6870cb6fa4191ed24b708080665ce53b3 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:38:29 +0200 Subject: [PATCH] Got NPCs working again! --- assets/converters.py | 5 +++-- src/config.h | 1 + src/game.c | 4 ++-- src/game.h | 4 ++-- src/npc.c | 18 +++++++++--------- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/assets/converters.py b/assets/converters.py index bad536b..5592787 100644 --- a/assets/converters.py +++ b/assets/converters.py @@ -9,6 +9,7 @@ from tiled import * VERBOSE = 1 SIGN_TYPES = ["SGN", "INFO"] FACES = ["MALE", "FEMALE", "MILKMAN", "POLICE"] +PRECISION = 8 def convert(input, output, params, target): if params["custom-type"] == "tmx": @@ -211,8 +212,8 @@ def convert_map(input, output, params, target): map_struct += fxconv.u32(len(npcs)) npc_struct = fxconv.Structure() for i in npcs.values(): - npc_struct += fxconv.u32(i["position"][0]+i["path"][0]) - npc_struct += fxconv.u32(i["position"][1]+i["path"][1]) + npc_struct += fxconv.u32((i["position"][0]+i["path"][0])< diff --git a/src/game.c b/src/game.c index e600ac6..a96ad4e 100644 --- a/src/game.c +++ b/src/game.c @@ -30,10 +30,10 @@ void interaction_available(Game *game) { /* simple distance check along X and Y axis */ /* Be careful to use world coordinates, not local (i.e.map) ones */ if((abs((int)game->player.wx - - (int)game->map_level->npcs[i].curx * PXSIZE) < + (int)(game->map_level->npcs[i].curx>>PRECISION) * PXSIZE) < MAX_INTERACTION_DISTANCE * PXSIZE) && (abs((int)game->player.wy - - (int)game->map_level->npcs[i].cury * PXSIZE) < + (int)(game->map_level->npcs[i].cury>>PRECISION) * PXSIZE) < MAX_INTERACTION_DISTANCE * PXSIZE)) { /* the player can do something */ game->player.canDoSomething = true; diff --git a/src/game.h b/src/game.h index 4eea9f3..0b5dbb1 100644 --- a/src/game.h +++ b/src/game.h @@ -72,8 +72,8 @@ typedef struct { typedef struct { /* current coordinates of the NPC */ - uint32_t curx; /* Initialize them at runtime */ - uint32_t cury; /* Initialize them at runtime */ + uint32_t curx; + uint32_t cury; /* initial coordinates of the NPC (needed to get absolute coordinates of * path) */ diff --git a/src/npc.c b/src/npc.c index 31e2ff8..7342244 100644 --- a/src/npc.c +++ b/src/npc.c @@ -108,8 +108,8 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) { int32_t w = full_map->w; int32_t h = full_map->h; - int32_t x = floor(npc->curx) / T_WIDTH; - int32_t y = floor(npc->cury) / T_HEIGHT; + int32_t x = (npc->curx>>PRECISION) / T_WIDTH; + int32_t y = (npc->cury>>PRECISION) / T_HEIGHT; dest_x /= T_WIDTH; dest_y /= T_HEIGHT; int32_t spos = y * w + x; @@ -202,7 +202,7 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) { } // Refactoring to make adding complexity cleaner -void update_npcs([[maybe_unused]] Game *game) { +void update_npcs(Game *game) { for(uint32_t u = 0; u < game->map_level->nbNPC; u++) { update_npc(&game->map_level->npcs[u]); } @@ -213,8 +213,8 @@ void update_npc(NPC *npc) { if(!npc->hasPath || npc->paused == true) return; - float vecX = (float)(npc->xpath[npc->currentPoint] + npc->x) - npc->curx; - float vecY = (float)(npc->ypath[npc->currentPoint] + npc->y) - npc->cury; + float vecX = (float)(npc->xpath[npc->currentPoint] + npc->x) - (npc->curx>>PRECISION); + float vecY = (float)(npc->ypath[npc->currentPoint] + npc->y) - (npc->cury>>PRECISION); float vecN = length(vecX, vecY); if(vecN > 0.5f) { @@ -225,8 +225,8 @@ void update_npc(NPC *npc) { npc->currentPoint = npc->currentPoint % npc->path_length; } - npc->curx += vecX; - npc->cury += vecY; + npc->curx += vecX*(float)(1<cury += vecY*(float)(1<curx * PXSIZE)) - (int16_t)pl->wx; - int16_t delY = ((int16_t)(Data->cury * PXSIZE)) - (int16_t)pl->wy; + int16_t delX = ((int16_t)((Data->curx>>PRECISION) * PXSIZE)) - (int16_t)pl->wx; + int16_t delY = ((int16_t)((Data->cury>>PRECISION) * PXSIZE)) - (int16_t)pl->wy; bopti_image_t *face = npc_sprites[Data->face]; dimage(pl->px - P_WIDTH / 2 + delX, pl->py - P_HEIGHT / 2 + delY, face); }