#include "river_genlayer.h" #include "heightnoise_genlayer.h" #include "heightmap_genlayer.h" #include "utils/bmutils.h" using namespace godot; RiverChunk::RiverChunk(uint32_t res){ this->res = res; cells = new float_t[res*res]; for(uint32_t i = 0; i < res*res;i++) { cells[i] = INFINITY; } } RiverChunk::~RiverChunk(){ delete[] cells; } void RiverChunk::SetValue(Vector2i pos, float_t v){ cells[pos.x*res+pos.y] = v; } float_t RiverChunk::GetLocalCell(Vector2i pos){ return cells[pos.x*res+pos.y]; } River::River(uint32_t res,float_t maxRadius, float_t minRadius, float_t easingCurve){ this->maxRadius = maxRadius; this->minRadius = minRadius; this->easingCurve = easingCurve; this->res = res; } River::River(const River& river) : River(river.res,river.maxRadius,river.minRadius,river.easingCurve){ this->chunks = river.chunks; } void River::AddCell(Vector3i worldCell){ cells.push_back(Vector2i(worldCell.x,worldCell.z)); cellHeights.push_back(worldCell.y); } void River::AddCell(Vector2i worldCell,float_t height){ cells.push_back(worldCell); cellHeights.push_back(height); } void River::AddChunk(Vector2i chunkPos,RiverChunk* chunk){ chunks.insert(std::pair(chunkPos,chunk)); } // void River::AddNode(Vector3 nodePos,Vector3 in,Vector3 out){ // } bool River::ContainsChunk(Vector2i chunk){ return chunks.find(chunk) != chunks.end(); } uint32_t River::GetLength(){ return cells.size(); } Vector3 River::GetClosestPoint(Vector3 p){ return Vector3(INFINITY,INFINITY,INFINITY); } uint32_t River::GetChunkCount(){ return chunks.size(); } RiverChunk* River::GetChunk(Vector2i pos){ return chunks.at(pos); } Vector3i River::GetCell(uint32_t i){ Vector2i cell = cells[i]; return Vector3i(cell.x,cellHeights[0],cell.y); } float_t River::GetCellHeight(uint32_t i){ return cellHeights[i]; } Vector3i River::GetSource(){ Vector2i cell = cells[0]; return Vector3i(cell.x,cellHeights[0],cell.y); } Vector3i River::GetSourceLocal(){ Vector2i cell = cells[0]; return BMUtils::mod(Vector3i(cell.x,cellHeights[0],cell.y),res); } float River::GetRadius(uint32_t i){ float_t value = (float_t)i / (float_t)cells.size(); return minRadius + UtilityFunctions::ease(value,easingCurve) * (maxRadius-minRadius); } RiverGenlayer::RiverGenlayer(BMWorld* world,HeightnoiseGenlayer* noiseGenLayer) : GenerationLayer2D(world){ this->noiseGenlayer = noiseGenLayer; } River* RiverGenlayer::GetChunk(Vector2i pos2d){ UpdateEditorParameters(); auto chunk = rivers.find(pos2d); // check if this chunk has river data if(chunk == rivers.end()) { // if it doesnt iterate for all the chunks in a radius for(int i = -RIVER_EFFECT_RANGE; i < RIVER_EFFECT_RANGE; i++){ for(int j = -RIVER_EFFECT_RANGE; j < RIVER_EFFECT_RANGE; j++){ Vector2i posOffset = Vector2i(i,j); float_t midHeight = noiseGenlayer->GetValue((posOffset+pos2d)*chunkRes); if(midHeight > RIVER_START_HEIGHT && rivers.find(posOffset+pos2d)==rivers.end()){ //and can be generated and if is not already generated UtilityFunctions::print("Generating river"); River* river = GenerateRiver(posOffset+pos2d); UtilityFunctions::print("New river generated at chunk",posOffset+pos2d); if(river!=nullptr && river->ContainsChunk(posOffset+pos2d)){ return river; } } } } }else{ UtilityFunctions::print("Chunk has river data"); return chunk->second; } UtilityFunctions::print("No river in this chunk"); rivers.insert(std::pair(pos2d,nullptr)); return nullptr; } River* RiverGenlayer::GenerateRiver(Vector2i pos){ UtilityFunctions::print("Generating new river with origin ",pos); float_t fallOff = 0.01f; bool finish = false; // float_t max = 0.0; Vector2i startingLocalPos = Vector2i(chunkRes/2,chunkRes/2); // HeightmapChunk* chunk = this->noiseGenlayer->GetChunk(pos); // for(uint32_t i = 0; i < chunkRes; i++){ // for(uint32_t j = 0; j < chunkRes; j++){ // float_t v = chunk->GetLocalCell(Vector2i(i,j)); // if(v > max){ // bestPos.x = i; // bestPos.y = j; // max = v; // } // } // } Vector2 momentum = Vector2(0,0); Vector2i worldPos = pos * chunkRes + startingLocalPos; float_t bestWeight = -INFINITY; River* river = new River(chunkRes,world->GetRiverMaxRadius(),world->GetRiverMinRadius(),world->GetRiverRadiusCurve()); const std::vector offsets = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; while(finish == false){ finish = true; Vector2i bestPos = Vector2i(0,0); float_t cellHeight = noiseGenlayer->GetValue(worldPos); river->AddCell(worldPos,cellHeight); for(int32_t i = 0; i < offsets.size(); i++){ Vector2i newPos = worldPos + offsets[i]; Vector2i chunkPos = BMUtils::globalPos2ChunkPos(newPos,chunkRes); float_t height = noiseGenlayer->GetValue(newPos); float_t weight = -height; if(momentum.length() > 0) weight += momentum.dot(offsets[i]); if(weight > bestWeight && BMUtils::manhattanDistance(pos,chunkPos) < RIVER_EFFECT_RANGE){ if(height >= RIVER_END_HEIGHT){ bestWeight = weight; bestPos = newPos; finish = false; } } } momentum += Vector2(bestPos - worldPos) * 0.000001f; if(momentum.length() > 0.0001f) momentum = momentum.normalized() * 0.0001f; worldPos = bestPos; } // for(uint32_t k = 0; k < river->GetLength();k++){ // float_t radius = river->GetRadius(k); // int32_t intRadius = BMUtils::ceil(radius); // Vector3i cellPos3d = river->GetCell(k); // Vector3i origin = Vector3i(cellPos3d.x,0,cellPos3d.z); // float_t minHeight = INFINITY; // for(int32_t i = -intRadius; i < intRadius;i++){ // for(int32_t j = -intRadius; j < intRadius;j++){ // Vector2i globalPos = Vector2i(i,j) + Vector2i(origin.x,origin.z); // Vector2i chunkPos = BMUtils::globalPos2ChunkPos(globalPos,chunkRes); // RiverChunk* chunk; // if(!river->ContainsChunk(chunkPos)){ // chunk = new RiverChunk(chunkRes); // river->AddChunk(chunkPos,chunk); // rivers.insert(std::pair(chunkPos,river)); // }else // chunk = river->GetChunk(chunkPos); // Vector2i localPos = BMUtils::mod(globalPos,chunkRes); // float_t length = Vector2i(i,j).length(); // float_t d = length / radius; // if(d <= 1.0){ // d = (1.0f-d) * fallOff; // float_t height = noiseGenlayer->GetValue(globalPos); // d = height - d; // height = height; // minHeight = height < minHeight ? height : minHeight; // float_t oldValue = chunk->GetLocalCell(localPos); // if(oldValue > d ) // chunk->SetValue(localPos,d); // } // } // } // river->SetCellHeight(k,noiseGenlayer->Normalized2Real(minHeight)); // } return river; } void RiverGenlayer::UpdateEditorParametersCallback(){ chunkRes = world->GetChunkRes(); };