diff options
author | nathan <nathansmith@disroot.org> | 2025-07-06 17:11:56 +0000 |
---|---|---|
committer | nathan <nathansmith@disroot.org> | 2025-07-06 17:11:56 +0000 |
commit | 255c938a77f8a73229b1867aadd10f3bf9c44f2e (patch) | |
tree | 7972e9a81d5e1a221bb160c7d82397c80b07fac7 | |
parent | b00544e8f0e77c149d44dca3ab81d97a07fcea3f (diff) | |
download | FindThings-255c938a77f8a73229b1867aadd10f3bf9c44f2e.tar.gz FindThings-255c938a77f8a73229b1867aadd10f3bf9c44f2e.tar.bz2 FindThings-255c938a77f8a73229b1867aadd10f3bf9c44f2e.zip |
Playing around with world generating a bit
-rw-r--r-- | src/entity.h | 2 | ||||
-rw-r--r-- | src/game.c | 15 | ||||
-rw-r--r-- | src/game.h | 3 | ||||
-rw-r--r-- | src/utils.h | 4 | ||||
-rw-r--r-- | src/world.c | 29 | ||||
-rw-r--r-- | src/world.h | 21 |
6 files changed, 51 insertions, 23 deletions
diff --git a/src/entity.h b/src/entity.h index 0c431ea..2a75f93 100644 --- a/src/entity.h +++ b/src/entity.h @@ -7,6 +7,8 @@ typedef int8_t EntityId; +#define ENTITY_COUNT 2 + enum { ENTITY_NONE = -1, OLD_MINT, @@ -18,19 +18,11 @@ void initGame(Game* game) // Player. game->player = createPlayer(); - game->player.position = (Vector3){50.0, 30.0, 50.0}; + game->player.position = (Vector3){0.0, 30.0, 0.0}; // World. game->world = createWorld(&game->assets); - game->entities[0] = createEntity(OLD_MINT, (Vector3){100.0, 0.0, 100.0}); - game->entities[1] = createEntity(STICKY_NICKEL, - (Vector3){100.0, 0.0, 105.0}); - game->entities[0].position.y = getWorldHeightAtLocation(game->world, - 100, 100) + 1.0; - game->entities[1].position.y = getWorldHeightAtLocation(game->world, - 100, 105) + 1.0; - DisableCursor(); } @@ -49,11 +41,6 @@ void updateGameScene(Game* game) updateWorld(&game->world, game); - for (int index = 0; index < 2; ++index) - { - updateEntity(&game->entities[index], game); - } - EndMode3D(); } @@ -19,9 +19,6 @@ struct Game { Assets assets; Player player; World world; - - // For testing only. - Entity entities[2]; }; void initGame(Game* game); diff --git a/src/utils.h b/src/utils.h index db6f7a8..c165e2c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -38,6 +38,10 @@ typedef enum FTError { #define TOGGLE_BIT(b, n) (b ^ (0x1 << n)) #define HAS_FLAG(v, f) ((v & f) == f) +// https://en.wikipedia.org/wiki/Linear_congruential_generator +#define FT_RANDOM16(seed) (seed = (75 * seed + 74) % 65537) +#define FT_RANDOM32(seed) (seed = (1664525 * seed + 1013904223) % 4294967296) + // Helpful for debugging #define PRINT_VECTOR2(v) printf("%f %f\n", v.x, v.y) #define PRINT_VECTOR3(v) printf("%f %f %f\n", v.x, v.y, v.z) diff --git a/src/world.c b/src/world.c index db6e55d..3513042 100644 --- a/src/world.c +++ b/src/world.c @@ -4,15 +4,32 @@ World createWorld(const Assets* assets) { World world; - world.size = (Vector3){1000.0, 30.0, 1000.0}; + world.size = (Vector3){1000.0, 20.0, 1000.0}; world.image = &assets->images[HEIGHT_MAP_IMAGE]; - + + // Heightmap. Mesh mesh = GenMeshHeightmap(*world.image, world.size); world.heightmap = LoadModelFromMesh(mesh); world.heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = assets->textures[HEIGHT_MAP_TEXTURE]; - world.heightmapColors = LoadImageColors(assets->images[HEIGHT_MAP_IMAGE]); + uint32_t seed = 57; + + // Entities. + for (int index = 0; index < WORLD_ENTITY_MAX; ++index) + { + FT_RANDOM16(seed); + + Entity entity = createEntity(seed % ENTITY_COUNT, Vector3Zero()); + FT_RANDOM16(seed); + // It be funky and give negative numbers. + entity.position.x = seed % (int)world.size.x; + FT_RANDOM16(seed); + entity.position.z = seed % (int)world.size.z; + entity.position.y = getWorldHeightAtLocation(world, entity.position.x, + entity.position.z) + 1.0; + world.entities[index] = entity; + } return world; } @@ -20,12 +37,16 @@ World createWorld(const Assets* assets) void updateWorld(World* world, Game* game) { DrawModel(world->heightmap, Vector3Zero(), 1.0, WHITE); + + for (int index = 0; index < WORLD_ENTITY_MAX; ++index) + { + updateEntity(&world->entities[index], game); + } } void freeWorld(World world) { UnloadModel(world.heightmap); - UnloadImageColors(world.heightmapColors); } float getWorldPixelHeight(World world, int x, int y) diff --git a/src/world.h b/src/world.h index 6a214b6..81512d0 100644 --- a/src/world.h +++ b/src/world.h @@ -1,16 +1,33 @@ #include "utils.h" #include "assets.h" +#include "entity.h" // This file is likely completely change. #ifndef WORLD_H #define WORLD_H +// Max entities per node. +#define BVH_MAX 4 + +#define WORLD_ENTITY_MAX 1000 + +// UID for anything in the world.x +typedef int16_t WorldUID; + +typedef struct BVHNode { + BoundingBox box; + WorldUID entities[BVH_MAX]; + struct BVHNode* branch1; + struct BVHNode* branch2; +} BVHNode; + typedef struct { Vector3 size; - Model heightmap; const Image* image; - Color* heightmapColors; + Model heightmap; + Entity entities[WORLD_ENTITY_MAX]; + BVHNode bvh; } World; World createWorld(const Assets* assets); |