#include "heightmap_genlayer.h" #include "river_genlayer.h" #include "heightnoise_genlayer.h" #include "chunk.h" #include "utils/bmutils.h" using namespace godot; HeightmapGenlayer::HeightmapGenlayer(BMWorld * world,HeightnoiseGenlayer* heightnoiseGenlayer,RiverGenlayer* riverGenlayer) : GenerationLayer2D(world){ this->riverGenlayer= riverGenlayer; this->noiseGenlayer = heightnoiseGenlayer; } HeightmapChunk* HeightmapGenlayer::GetChunk(Vector2i pos){ auto found = chunks.find(pos); HeightmapChunk * chunk; if(found == chunks.end()){ chunk = Generate(pos); chunks.insert(std::pair(pos,chunk)); }else chunk = found->second; return chunk; } float_t HeightmapGenlayer::GetGlobalCell(Vector2i pos){ UpdateEditorParameters(); Vector2i chunkPos = BMUtils::globalPos2ChunkPos(pos,chunkRes); Vector2i localPos = BMUtils::mod(pos,chunkRes); HeightmapChunk* chunk = GetChunk(chunkPos); return chunk->GetLocalCell(localPos); } HeightmapChunk* HeightmapGenlayer::Generate(Vector2i chunkPos){ // UtilityFunctions::print("Generating chunk ",chunkPos," in heightmap GenLayer"); uint32_t res = world->GetChunkRes(); UpdateEditorParameters(); River* river = riverGenlayer->GetChunk(chunkPos); RiverChunk* riverChunk = nullptr; if(river!=nullptr){ if(river->ContainsChunk(chunkPos)){ UtilityFunctions::print("Has river with length ",river->GetLength()," and ", river->GetChunkCount()," chunks, in heightmap GenLayer"); riverChunk = river->GetChunk(chunkPos); } } HeightmapChunk * chunk = new HeightmapChunk(res,river); for(int32_t i = 0; i < res+1; i++){ for(int32_t j = 0; j < res+1; j++){ Vector2i localPos(i,j); // Vector2i globalPos = BMUtils::localPos2GlobalPos(localPos,chunkPos,res); float_t v = noiseGenlayer->GetValue( BMUtils::localPos2GlobalPos(Vector2i(i,j),chunkPos,res)); if(riverChunk!=nullptr && i < res && j < res){ float_t riverValue = riverChunk->GetLocalCell(localPos); if(riverValue < INFINITY) v = riverValue; } chunk->SetValue(localPos,noiseGenlayer->Normalized2Real(v)); } } return chunk; } HeightmapChunk::HeightmapChunk(uint32_t res, River* riverSource){ cells = new float_t[(res+1)*(res+1)]; this->res = res; this->riverSource = riverSource; } HeightmapChunk::~HeightmapChunk(){ delete[] cells; } void HeightmapChunk::SetValue(Vector2i pos, float_t v){ cells[pos.x*(res+1)+pos.y] = v; minHeight = minHeight > v ? v : minHeight; maxHeight = maxHeight < v ? v : maxHeight; //! cuidado con esto, habría que recalcular los máximos si sobreescribimos un antiguo máximo (y mínimos) } float_t HeightmapChunk::GetLocalCell(Vector2i pos){ return cells[pos.x*(res+1)+pos.y]; } float_t HeightmapChunk::GetMaxHeight(){ return maxHeight; } float_t HeightmapChunk::GetMinHeight(){ return minHeight; }