#include "world.h" #include "entity.h" #include #include void tc_add_chunk_to_loader(tc_chunkloader_s *chunkloader, tc_entity_s *entity) { if(chunkloader->num_chunks >= chunkloader->chunks_capacity) { chunkloader->chunks_capacity *= 2; chunkloader->chunks = realloc(chunkloader->chunks, sizeof(tc_entity_s *) * chunkloader->chunks_capacity); } chunkloader->chunks[chunkloader->num_chunks] = entity; ++chunkloader->num_chunks; } 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_entity_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) { tc_chunk_s *chunk = loader->chunks[chunk_index]->specific; if(tc_vec3i_equ(chunk->position, coord)) return loader->chunks[chunk_index]; } return NULL; } tc_entity_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_entity_s * tc_load_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) { tc_entity_s *entity = tc_get_chunk_of_loader(loader, coord); if(entity == NULL) { tc_entity_s *entity = tc_create_entity("chunk", loader->world); tc_chunk_s *chunk = entity->specific; chunk = entity->specific; chunk->position = coord; loader->world->worldgen->fn_generate_chunk(loader->world->worldgen, chunk); tc_run_hooklist(&loader->world->after_chunk_generate, chunk); tc_upload_chunk(chunk); tc_add_chunk_to_loader(loader, entity); } return entity; } void tc_reload_chunk_of_loader(tc_chunkloader_s *loader, tc_vec3i_s coord) { tc_entity_s *entity = tc_get_chunk_of_loader(loader, coord); tc_chunk_s *chunk = entity->specific; if(chunk == NULL) return; tc_meshize_chunk(chunk, NULL); 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_chunk_from_loader(tc_chunkloader_s *loader, uint32_t index) { if(loader->num_chunks != 0) { // 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; } } 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_entity_s *entity = loader->chunks[chunk_index]; tc_chunk_s *chunk = entity->specific; if(!tc_coord_is_within_loaders_range(loader, chunk->position)) { tc_remove_chunk_from_loader(loader, chunk_index); } } } void tc_load_all_chunks_of_chunkloader(tc_chunkloader_s *loader) { for(int32_t x = 0; x < loader->extent.x; ++x) { for(int32_t y = 0; y < loader->extent.y; ++y) { for(int32_t z = 0; z < loader->extent.z; ++z) { tc_vec3i_s coords; 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; 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); } uint64_t world_update_tick = 0; void tc_update_world(tc_world_s *world) { if((world_update_tick % 512) == 0) { uint32_t chunkloader_index = 0; while(chunkloader_index < world->num_loaders) { if(world->loaders[chunkloader_index].needs_reload) { tc_reload_chunkloader_zone(&world->loaders[chunkloader_index]); } ++chunkloader_index; } } ++world_update_tick; }