From c65787ebfc5f0701bb4366ef22cddc8af2d243ee Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 25 Jul 2025 01:57:49 -0600 Subject: More tree things --- src/world.c | 88 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 27 deletions(-) (limited to 'src/world.c') diff --git a/src/world.c b/src/world.c index 52b91b1..17269ec 100644 --- a/src/world.c +++ b/src/world.c @@ -328,7 +328,46 @@ void buildWorldBVH(World* world) world->bvhDebugSelect = 0; } -World createWorld(int seed) +Seed generateWorldTrees(World* world, Seed seed) +{ + for (int index = 0; index < WORLD_TREE_COUNT; ++index) + { + Vector3 position; + position.x = FT_RANDOM16(seed) % (int)world->size.x; + position.z = FT_RANDOM16(seed) % (int)world->size.z; + position.y = getWorldHeightAtLocation( + world, + position.x, position.z) + (TREE_SCALE / 2.0); + Entity entity = createEntity(TREE, position); + + world->entities[index] = entity; + } + + return seed; +} + +Seed generateWorldItems(World* world, Seed seed) +{ + for (int index = WORLD_TREE_COUNT; index < WORLD_ENTITY_MAX; ++index) + { + FT_RANDOM16(seed); + + Vector3 position; + position.x = FT_RANDOM16(seed) % (int)world->size.x; + position.z = FT_RANDOM16(seed) % (int)world->size.z; + position.y = getWorldHeightAtLocation(world, + position.x, position.z) + 1.0; + EntityId items[] = {OLD_MINT, STICKY_NICKEL}; + size_t itemsSize = 2; + Entity entity = createEntity(items[seed % itemsSize], position); + + world->entities[index] = entity; + } + + return seed; +} + +World createWorld(Seed seed) { World world; world.size = WORLD_SIZE; @@ -348,21 +387,8 @@ World createWorld(int seed) UnloadImage(image); - // Entities. - for (int index = 0; index < WORLD_ENTITY_MAX; ++index) - { - FT_RANDOM16(seed); - - Entity entity = createEntity(seed % ENTITY_COUNT, Vector3Zero()); - Vector3 position; - position.x = FT_RANDOM16(seed) % (int)world.size.x; - position.z = FT_RANDOM16(seed) % (int)world.size.z; - position.y = getWorldHeightAtLocation(&world, - position.x, position.z) + 1.0; - setEntityPosition(&entity, position); - - world.entities[index] = entity; - } + seed = generateWorldTrees(&world, seed); + seed = generateWorldItems(&world, seed); double currentTime = GetTime(); buildWorldBVH(&world); @@ -489,11 +515,12 @@ float getWorldHeightAtLocation(const World* world, float x, float y) return 0.0; } -WorldUID castRayBVH(const World* world, BVHNode node, Ray ray, int* tests) +void castRayBVH(const World* world, BVHNode node, Ray ray, int* tests, + WorldUID* closest, float* closestDistance) { if (!GetRayCollisionBox(ray, node.box).hit) { - return -1; + return; } if (tests != NULL) @@ -511,7 +538,15 @@ WorldUID castRayBVH(const World* world, BVHNode node, Ray ray, int* tests) ray, world->entities[node.entities[index]].box).hit) { - return node.entities[index]; + float distance = Vector3Distance( + world->entities[node.entities[index]].position, + ray.position); + + if (distance < *closestDistance) + { + *closest = node.entities[index]; + *closestDistance = distance; + } } } } @@ -519,21 +554,20 @@ WorldUID castRayBVH(const World* world, BVHNode node, Ray ray, int* tests) { for (int index = 0; index < node.branchCount; ++index) { - WorldUID uid = castRayBVH(world, *node.branches[index], ray, tests); - - if (uid != -1) - { - return uid; - } + castRayBVH(world, *node.branches[index], ray, tests, closest, + closestDistance); } } - return -1; + return; } WorldUID castRayAtWorld(const World* world, Ray ray, int* tests) { - return castRayBVH(world, world->bvh, ray, tests); + WorldUID uid = -1; + float distance = Vector3LengthSqr(world->size); + castRayBVH(world, world->bvh, ray, tests, &uid, &distance); + return uid; } // Abortions are good. Get more abortions. -- cgit v1.2.3