From 394366480b8b37aa9da9bb09663c7bed0fcf6bee Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 5 Oct 2025 15:32:57 +0100 Subject: [PATCH] feat: Random npc walkabouts --- code/first.c | 3 ++- code/game/impl/npc.c | 7 ++++--- code/game/world.h | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/code/first.c b/code/first.c index 59fd591..4a2bd06 100644 --- a/code/first.c +++ b/code/first.c @@ -63,6 +63,7 @@ int main(int argc, char **argv) { game->draw.camera = camera; World *world = M_ArenaPush(arena, World); game->world = world; + world->random = Random_Seed(29237489723847); world->npcCount = 2; NPC *npc1 = &world->npcs[0]; npc1->collision.pos.x = 15; @@ -79,7 +80,7 @@ int main(int argc, char **argv) { npc1->collision.size.x = 10; npc1->collision.size.y = 10; - NPC *npc2 = &world->npcs[0]; + NPC *npc2 = &world->npcs[1]; npc2->collision.pos.x = 15; npc2->collision.pos.y = 15; npc2->collision.size.x = 10; diff --git a/code/game/impl/npc.c b/code/game/impl/npc.c index 4c9d1a2..bc7358e 100644 --- a/code/game/impl/npc.c +++ b/code/game/impl/npc.c @@ -1,6 +1,7 @@ #include "game/npc.h" #include "game/world.h" #include "core/types.h" +#include "core/math.h" #include @@ -10,8 +11,8 @@ void updateNPC(F32 delta, NPC *npc, World *world) { npc->waitTime+=delta; if(npc->waitTime > npc->maxWaitTime) { npc->mode = NPC_ACTION_WALKING; - U32 next = npc->targetNavNode == 100 ? 20 : 100; - npc->targetNavNode = next; // TODO RANDOM + // TODO change targets to poi's rather than just random nodes + npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount); printf("Starting to nav path\n"); npc->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode); printf("done\n"); @@ -26,7 +27,7 @@ void updateNPC(F32 delta, NPC *npc, World *world) { if(npc->path.nodeCount == npc->pathIndex+1){ printf("Finished! so I'm waiting\n"); npc->mode = NPC_ACTION_WAITING; - npc->maxWaitTime = 20; // TODO RANDOM + npc->maxWaitTime = Random_F32(&world->random, 10, 40); npc->waitTime = 0; npc->pathIndex = 0; return; diff --git a/code/game/world.h b/code/game/world.h index 433070b..d237538 100644 --- a/code/game/world.h +++ b/code/game/world.h @@ -4,6 +4,7 @@ #include "player.h" #include "npc.h" #include "bandit.h" +#include "../core/math.h" // Areas are which typedef U32 World_Area; @@ -16,6 +17,7 @@ typedef struct World World; struct World { //// Static stuff NavMesh *navMesh; + Random random; //// Player Player player;