2023-10-13 06:00:53 +00:00
|
|
|
#include "world.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2023-10-14 14:48:09 +00:00
|
|
|
void tc_add_chunk_to_loader(tc_chunkloader_s *chunkloader, tc_chunk_s *chunk)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-14 14:48:09 +00:00
|
|
|
if(chunkloader->num_chunks >= chunkloader->chunks_capacity)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-14 14:48:09 +00:00
|
|
|
chunkloader->chunks_capacity *= 2;
|
|
|
|
chunkloader->chunks = realloc(chunkloader->chunks, sizeof(tc_chunk_s) * chunkloader->chunks_capacity);
|
2023-10-13 06:00:53 +00:00
|
|
|
}
|
2023-10-14 14:48:09 +00:00
|
|
|
chunkloader->chunks[chunkloader->num_chunks] = chunk;
|
|
|
|
++chunkloader->num_chunks;
|
2023-10-13 06:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tc_coord_is_within_loaders_range(tc_chunkloader_s *loader, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
if(coord.x < (loader->center.x - loader->extent.x / 2)) return false;
|
|
|
|
if(coord.y < (loader->center.y - loader->extent.y / 2)) return false;
|
|
|
|
if(coord.z < (loader->center.z - loader->extent.z / 2)) return false;
|
|
|
|
|
|
|
|
if(coord.x > (loader->center.x + loader->extent.x / 2)) return false;
|
|
|
|
if(coord.y > (loader->center.y + loader->extent.y / 2)) return false;
|
|
|
|
if(coord.z > (loader->center.z + loader->extent.z / 2)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_chunkloader_s * tc_get_corresponding_loader(tc_world_s *world, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
for(uint32_t loader_index = 0; loader_index < world->num_loaders; ++loader_index)
|
|
|
|
if(tc_coord_is_within_loaders_range(&world->loaders[loader_index], coord))
|
|
|
|
return &world->loaders[loader_index];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_chunk_s * tc_get_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index)
|
|
|
|
if(tc_vec3i_equ(loader->chunks[chunk_index]->position, coord))
|
|
|
|
return loader->chunks[chunk_index];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_chunk_s * tc_get_loaded_chunk(tc_world_s *world, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
tc_chunkloader_s *loader = tc_get_corresponding_loader(world, coord);
|
|
|
|
if(loader == NULL) return NULL;
|
|
|
|
|
|
|
|
return tc_get_chunk_of_loader(loader, coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
tc_chunk_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
tc_chunk_s *chunk = tc_get_chunk_of_loader(loader, coord);
|
|
|
|
|
|
|
|
if(chunk == NULL)
|
|
|
|
{
|
|
|
|
chunk = tc_allocate_chunk(loader->world);
|
|
|
|
chunk->position = coord;
|
|
|
|
|
|
|
|
loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, chunk);
|
|
|
|
tc_upload_chunk(chunk);
|
|
|
|
|
|
|
|
tc_add_chunk_to_loader(loader, chunk);
|
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_reload_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
tc_chunk_s *chunk = tc_get_chunk_of_loader(loader, coord);
|
|
|
|
if(chunk == NULL) return;
|
|
|
|
|
|
|
|
tc_meshize_chunk(chunk);
|
|
|
|
tc_upload_chunk(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_reload_chunk(tc_world_s *world, tc_vec3i_s coord)
|
|
|
|
{
|
|
|
|
tc_chunkloader_s *loader = tc_get_corresponding_loader(world, coord);
|
|
|
|
if(loader == NULL) return;
|
|
|
|
|
|
|
|
tc_reload_chunk_of_loader(loader, coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
|
|
|
|
{
|
|
|
|
tc_free_chunk(loader->world, loader->chunks[index]);
|
|
|
|
|
2023-10-14 08:47:21 +00:00
|
|
|
if(loader->num_chunks != 0)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-14 08:47:21 +00:00
|
|
|
// This is is NOT last element
|
|
|
|
if((index+1) < loader->num_chunks)
|
|
|
|
{
|
|
|
|
loader->chunks[index] = loader->chunks[loader->num_chunks-1]; // move last chunk into gap
|
|
|
|
}
|
|
|
|
|
|
|
|
--loader->num_chunks;
|
2023-10-13 06:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
|
|
|
|
{
|
|
|
|
for(uint32_t chunk_index = 0; chunk_index < loader->num_chunks; ++chunk_index)
|
|
|
|
{
|
|
|
|
tc_chunk_s *chunk = loader->chunks[chunk_index];
|
|
|
|
if(!tc_coord_is_within_loaders_range(loader, chunk->position))
|
|
|
|
{
|
|
|
|
tc_remove_loaders_chunk(loader, chunk_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_load_all_chunks_of_chunkloader(tc_chunkloader_s *loader)
|
|
|
|
{
|
2023-10-14 08:47:21 +00:00
|
|
|
for(int32_t x = 0; x < loader->extent.x; ++x)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-14 08:47:21 +00:00
|
|
|
for(int32_t y = 0; y < loader->extent.y; ++y)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-14 08:47:21 +00:00
|
|
|
for(int32_t z = 0; z < loader->extent.z; ++z)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
|
|
|
tc_vec3i_s coords;
|
2023-10-14 08:47:21 +00:00
|
|
|
coords.x = (loader->center.x - (x - ((int32_t) loader->extent.x)))-1;
|
|
|
|
coords.y = (loader->center.y - (y - ((int32_t) loader->extent.y)))-1;
|
|
|
|
coords.z = (loader->center.z - (z - ((int32_t) loader->extent.z)))-1;
|
2023-10-13 06:00:53 +00:00
|
|
|
|
|
|
|
tc_load_chunk_of_loader(loader, coords);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_reload_chunkloader_zone(tc_chunkloader_s *loader)
|
|
|
|
{
|
|
|
|
tc_remove_unnecessary_chunks_of_loader(loader);
|
|
|
|
tc_load_all_chunks_of_chunkloader(loader);
|
|
|
|
}
|
|
|
|
|
2023-10-13 10:23:26 +00:00
|
|
|
uint64_t world_update_tick = 0;
|
|
|
|
|
2023-10-13 06:00:53 +00:00
|
|
|
void tc_update_world(tc_world_s *world)
|
2023-10-13 10:23:26 +00:00
|
|
|
{
|
2023-10-14 08:47:21 +00:00
|
|
|
if((world_update_tick % 512) == 0)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-13 10:23:26 +00:00
|
|
|
uint32_t chunkloader_index = 0;
|
|
|
|
while(chunkloader_index < world->num_loaders)
|
2023-10-13 06:00:53 +00:00
|
|
|
{
|
2023-10-13 10:23:26 +00:00
|
|
|
if(world->loaders[chunkloader_index].needs_reload)
|
|
|
|
{
|
|
|
|
tc_reload_chunkloader_zone(&world->loaders[chunkloader_index]);
|
|
|
|
}
|
|
|
|
++chunkloader_index;
|
2023-10-13 06:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 10:23:26 +00:00
|
|
|
++world_update_tick;
|
2023-10-13 06:00:53 +00:00
|
|
|
}
|
|
|
|
|