Something went wrong. Try again.
Simple scenery generator
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153#include "terrain_genlayer.h"
#include <godot_cpp/classes/time.hpp>
#include "chunk.h"#include "utils/bmutils.h"#include "heightmap_genlayer.h"#include "river_genlayer.h"
using namespace godot;
TerrainGenlayer::TerrainGenlayer(BMWorld * world,HeightmapGenlayer* heightmapGenlayer,RiverGenlayer* riverGenlayer) : GenerationLayer3D(world){ this->heightmapGenlayer = heightmapGenlayer; this->riverGenLayer = riverGenlayer;}
TerrainChunk* TerrainGenlayer::GetChunk(Vector3i pos){ auto found = chunks.find(pos); TerrainChunk* chunk; // TerrainChunk * chunk; if(found == chunks.end()){ chunk = Generate(pos); chunks.insert(std::pair(pos,chunk)); }else{ chunk = found->second; } return chunk;}
TerrainChunk * TerrainGenlayer::Generate(Vector3i chunkPos){ UtilityFunctions::print("Generating chunk ",chunkPos," in terrain GenLayer"); UpdateEditorParameters();
HeightmapChunk * heightmapChunk = heightmapGenlayer->GetChunk(Vector2i(chunkPos.x,chunkPos.z));
int32_t min = heightmapChunk->GetMinHeight() - chunkPos.y*(int32_t)chunkRes-1; int32_t max = heightmapChunk->GetMaxHeight() - chunkPos.y*(int32_t)chunkRes+1; min = min < 0 ? 0 : min; max = max < 0 ? 0 : max; min = min > chunkRes ? chunkRes : min; max = max > chunkRes ? chunkRes : max;
// River* river = riverGenLayer->GetChunk(Vector2i(chunkPos.x,chunkPos.z)); // if(river!=nullptr){ // UtilityFunctions::print("Has river with length ",river->GetLength()," and ", river->GetChunkCount()," chunks, in terrain GenLayer"); // min = 0;
// }else{ // min = heightmapGenLayer->Normalized2Real(heightmapChunk->GetMinHeight()) - chunkPos.y*(int32_t)chunkRes-1; // }
TerrainChunk * chunk ;
// if(river!=nullptr&& BMUtils::globalPos2ChunkPos(river->GetSource(),chunkRes)==chunkPos){ // chunk = new TerrainChunk(chunkRes,min,max,river); // }else{ River* river = heightmapChunk->GetRiverSource(); if(river!=nullptr){ if(BMUtils::globalPos2ChunkPos(river->GetSource(),chunkRes)==chunkPos){ chunk = new TerrainChunk(chunkRes,min,max,river); }else chunk = new TerrainChunk(chunkRes,min,max); }else chunk = new TerrainChunk(chunkRes,min,max); // }
for(int32_t i = 0; i < chunkRes; i++){ for(int32_t j = min; j < max; j++){ for(int32_t k = 0; k < chunkRes; k++){ Vector3i outPos(i,j,k); Vector2i inPos(outPos.x,outPos.z);
float_t v = heightmapChunk->GetLocalCell(inPos); // if(river!=nullptr){ // if(river->ContainsChunk(Vector2i(chunkPos.x,chunkPos.z))){ // float_t riverValue = river->GetChunk(Vector2i(chunkPos.x,chunkPos.z))->GetLocalCell(inPos); // if(riverValue < INFINITY) v = riverValue; // } // }
v = - j + v - chunkPos.y*(int32_t)chunkRes;
chunk->SetValue(outPos,v);
} } }
return chunk;}
void TerrainGenlayer::Clean(){ chunks.clear();}
void TerrainGenlayer::UpdateEditorParametersCallback(){ chunkRes = world->GetChunkRes();};
TerrainChunk::TerrainChunk(uint32_t res, int min, int max, River* riverSource){ this->riverSource = riverSource; this->res = res;
max = max < 0 ? 0 : max; max = max >= res ? res : max; min = min < 0 ? 0 : min; min = min > max ? max : min;
minHeight = min; maxHeight = max; diff = maxHeight - minHeight;
if(diff>0) cells = new float[res*res*diff];}
TerrainChunk::~TerrainChunk(){ delete[] cells;}
void TerrainChunk::SetValue(Vector3i pos, float v){ if(pos.y < minHeight) return; if(pos.y >= maxHeight) return; uint32_t memPos = pos.x*res*diff+(pos.y-minHeight)*res+pos.z;
cells[memPos] = v;
}
float TerrainChunk::GetLocalCell(Vector3i pos){ if(pos.y >= maxHeight) return 0; if(pos.y < minHeight) return 1; return cells[pos.x*res*diff+(pos.y-minHeight)*res+pos.z];}
float TerrainChunk::GetValue(Vector3 pos){ return 0.0f; //! arreglar datos redundantes antes de usar esto}
int TerrainChunk::GetMaxHeight(){ return maxHeight;}
int TerrainChunk::GetMinHeight(){ return minHeight;}