From a7293245dcceda3dbff36944c2eced4584878896 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 26 Aug 2023 18:12:03 +0300 Subject: [PATCH] underwater cell generation --- mapEditorAppState.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 73440ce..49d11ad 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -21,7 +21,8 @@ #include -#include +#include +#include #include #include @@ -262,6 +263,7 @@ namespace battleship{ int numHorCells = int(mapSize.x / cellSize.x); int numVertCells = int(mapSize.y / cellSize.z); vector cells; + vector> waterBodyBedPoints; Node *terrainNode = map->getNodeParent(); for(int i = 0; i < numVertCells; i++) @@ -278,6 +280,7 @@ namespace battleship{ } Map::Cell::Type type = Map::Cell::Type::LAND; + Vector3 pos = res[0].pos; for(int k = 1; k < terrainNode->getNumChildren(); k++){ Vector3 waterPos = terrainNode->getChild(k)->getPosition(); @@ -285,6 +288,8 @@ namespace battleship{ if(fabs(res[0].pos.x - waterPos.x) < .5 * cellSize.x && fabs(res[0].pos.z - waterPos.z) < .5 * cellSize.z){ type = Map::Cell::Type::WATER; + pos.y = waterPos.y; + waterBodyBedPoints.push_back(pair(numVertCells * i + j, res[0].pos.y)); break; } } @@ -304,9 +309,42 @@ namespace battleship{ if(i < numVertCells - 1) edges.push_back(Map::Edge(weight, numVertCells * i + j, numVertCells * (i + 1) + j)); - cells.push_back(Map::Cell(res[0].pos, type, edges)); + cells.push_back(Map::Cell(pos, type, edges)); } + std::map> waterCellMap; + int currUnderWaterCellId = cells.size(); + int weight = 2; + + for(pair p : waterBodyBedPoints){ + int numUnderWaterCells = (int)((cells[p.first].pos.y - p.second) / cellSize.y); + vector underWaterCellIds; + + if(numUnderWaterCells) + cells[p.first].edges.push_back(Map::Edge(weight, p.first, currUnderWaterCellId)); + + for(int i = 0; i < numUnderWaterCells; i++, currUnderWaterCellId++) + underWaterCellIds.push_back(currUnderWaterCellId); + + waterCellMap[p.first] = underWaterCellIds; + } + + + for(std::map>::iterator it = waterCellMap.begin(); it != waterCellMap.end(); ++it){ + for(int i = 0; i < waterCellMap[it->first].size(); i++){ + vector edges = vector{Map::Edge(weight, waterCellMap[it->first][i], it->first)}; + + for(Map::Edge edge : cells[it->first].edges){ + if(cells[edge.destCellId].type == Map::Cell::Type::WATER && waterCellMap[edge.destCellId].size() >= i){ + edges.push_back(Map::Edge(weight, waterCellMap[it->first][i], waterCellMap[edge.destCellId][i])); + } + } + + Vector3 cellPos = cells[it->first].pos - Vector3::VEC_J * cellSize.y * (i + 1); + cells.push_back(Map::Cell(cellPos, Map::Cell::Type::WATER, edges)); + } + } + return cells; }