From 77da016ba6e2edd23d665c9e5e738f520bb6c96d Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Thu, 22 Aug 2024 22:40:25 -0400 Subject: [PATCH] Even better mouse ai? --- haloo3d | 2 +- maze.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/haloo3d b/haloo3d index 96dbee2..ece5d21 160000 --- a/haloo3d +++ b/haloo3d @@ -1 +1 @@ -Subproject commit 96dbee2942ae8b3d09302a24b075024c8134bb55 +Subproject commit ece5d2155e95fb9efbc72425fb3c851d9037755c diff --git a/maze.c b/maze.c index fa9a528..7f38976 100644 --- a/maze.c +++ b/maze.c @@ -18,10 +18,10 @@ // INteresting flags for performance comparisons #define FASTFILL -#define WIDTH 640 -#define HEIGHT 400 +#define WIDTH 1280 +#define HEIGHT 800 #define ASPECT ((float)WIDTH / HEIGHT) -#define SCREENSCALE 2 +#define SCREENSCALE 1 #define SWIDTH (WIDTH * SCREENSCALE) #define SHEIGHT (HEIGHT * SCREENSCALE) #define NEARCLIP 0.01 @@ -38,6 +38,9 @@ #define PAINTINGODDS 20 // Some arbitrarily large number, up to you #define MAZEHRAND 100 +// The pool of numbers to choose from when checking if a mouse +// will turn left or right randomly. Higher = less chance to turn +#define MOUSETURNRAND 4 // The horizontal choice will be this out of MAZEHRAND. // So, if MAZEHRAND is 100, setting this to 80 will mean // an 8 / 10 chance to go horizontal @@ -117,16 +120,18 @@ int maze_visited(uint8_t *maze, int x, int y, int size) { } int maze_connected(uint8_t *maze, int x, int y, int size, uint8_t move) { - if (move == DIREAST) { + switch (move) { + case DIREAST: return (maze[x + y * size] & MAZEEAST) == 0; - } else if (move == DIRWEST) { + case DIRWEST: return (x > 0) && ((maze[x - 1 + y * size] & MAZEEAST) == 0); - } else if (move == DIRNORTH) { + case DIRNORTH: return (maze[x + y * size] & MAZENORTH) == 0; - } else if (move == DIRSOUTH) { + case DIRSOUTH: return (y > 0) && ((maze[x + (y - 1) * size] & MAZENORTH) == 0); + default: + return 0; } - return 0; } void maze_to_pos(struct vec2i *maze, mfloat_t *dest, mfloat_t cellsize) { @@ -605,7 +610,30 @@ void sys_mouseai(ecs_mouseai *mouseai, ecs_placement *p, ecs_autonav *anav) { if (anav->timer == 0) { eprintf("MOUSEAI DIR: %d POS: (%f, %f)\n", mouseai->dir, p->pos.x, p->pos.z); - // Player can only move forward if there's nothing in front of them + // Look left or right randomly. You can affect how "straight" the mouse + // moves by increasing the MOUSETURNRAND + uint8_t ndir; + switch (rand() % MOUSETURNRAND) { + case 0: + ndir = TURNRIGHT(mouseai->dir); + break; + case 1: + ndir = TURNLEFT(mouseai->dir); + break; + default: + ndir = mouseai->dir; + } + // If the direction is valid, switch to it. This means that if the mouse + // is faced with just one new direction while walking down a hallway, + // there's only a CHANCE it will move that way. This ALSO allows the mouse + // to handle hitting a wall, as they will eventually choose a valid + // direction + if (maze_connected(mouseai->ws->maze, mouseai->mpos.x, mouseai->mpos.y, + mouseai->ws->size, ndir)) { + mouseai->dir = ndir; + } + // Now, regardless of what happened above, make sure the current direction + // we're moving is valid before we step that direction if (maze_connected(mouseai->ws->maze, mouseai->mpos.x, mouseai->mpos.y, mouseai->ws->size, mouseai->dir)) { struct vec2i movement = dirtovec(mouseai->dir); @@ -615,7 +643,7 @@ void sys_mouseai(ecs_mouseai *mouseai, ecs_placement *p, ecs_autonav *anav) { anav->dest.x = p->pos.x + mouseai->ws->cellsize * movement.x; anav->dest.z = p->pos.z + mouseai->ws->cellsize * movement.y; } else { - // Mouse only picks new direction if it can't move forward anymore + // Choose a direction at random if we can't move mouseai->dir = (1 << (rand() % 4)); } } @@ -806,6 +834,8 @@ void create_paintingobj(haloo3d_obj *obj) { int main() { // int argc, char **argv) { + srand(clock()); + haloo3d_easystore storage; haloo3d_easystore_init(&storage);