From 18428aec58757a7b652a6c9f703db34a3b5f4bc7 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 9 Sep 2023 14:42:59 +0300 Subject: [PATCH] implemented underwater unit submerging while pathfinding --- activeGameState.cpp | 31 ++++++++++++++++++++----------- map.cpp | 24 +++++++++++++++++++++--- map.h | 2 +- mapEditorAppState.cpp | 19 +++++++++++++++++-- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/activeGameState.cpp b/activeGameState.cpp index 3dd9a7a..f141e9c 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -276,21 +276,30 @@ namespace battleship{ Map *map = Map::getSingleton(); - /* - for(Unit *u : mainPlayer->getSelectedUnits()) - if(u->getType() == UnitType::UNDERWATER){ - for(int i = 1; i < map->getNumTerrainObjects(); i++){ - if(map->isPointWithinTerrainObject(u->getPos(), i) && map->isPointWithinTerrainObject(results[0].pos, i)){ - vector res; - Vector3 pos = u->getPos(); - Ray::retrieveCollisions(Vector3(pos.x, map->getTerrainObject(0).size.y, pos.z), Vector3(0, -1, 0), map->getTerrainObject(0).node->getChild(0), res); - Ray::sortResults(res); - results[0].pos.y = res[0].pos.y + depth * (map->getTerrainObject(i).pos.y - res[0].pos.y); + for(Unit *u : mainPlayer->getSelectedUnits()){ + Vector3 pos = u->getPos(); + Node *nodeParent = map->getNodeParent(); + + if(u->getType() == UnitType::UNDERWATER && nodeParent->getNumChildren() > 0){ + vector res; + Ray::retrieveCollisions(Vector3(pos.x, 100, pos.z), Vector3(0, -1, 0), nodeParent->getChild(0), res); + Ray::sortResults(res); + + Vector3 waterBodyPos = Vector3::VEC_ZERO; + Vector3 cellSize = map->getCellSize(); + + for(int i = 1; i < nodeParent->getNumChildren(); i++){ + Vector3 wpos = nodeParent->getChild(i)->getPosition(); + + if(fabs(wpos.x - pos.x) < .5 * cellSize.x && fabs(wpos.z - pos.z) < .5 * cellSize.z){ + waterBodyPos = wpos; break; } } + + results[0].pos.y = res[0].pos.y + depth * (waterBodyPos.y - res[0].pos.y); } - */ + } targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : unit), results[0].pos)); } diff --git a/map.cpp b/map.cpp index d058827..7b30da2 100755 --- a/map.cpp +++ b/map.cpp @@ -193,6 +193,12 @@ namespace battleship{ edges.push_back(Edge(edgeTable["weight"], edgeTable["srcCellId"], edgeTable["destCellId"])); } + int numUnderWaterCells = cellTable["numUnderWaterCells"]; + vector underWaterCellIds; + + for(int j = 0; j < numUnderWaterCells; j++) + underWaterCellIds.push_back((int)cellTable["underWaterCellId"][j + 1]); + Vector3 cellPos = Vector3(posTable["x"], posTable["y"], posTable["z"]); Cell::Type cellType = (Cell::Type)cellTable["type"]; @@ -204,7 +210,7 @@ namespace battleship{ node->attachMesh(quad); cellNode->attachChild(node); - cells.push_back(Cell(cellPos, cellType, edges)); + cells.push_back(Cell(cellPos, cellType, edges, underWaterCellIds)); } } @@ -270,7 +276,6 @@ namespace battleship{ } //TODO replace search with binary search - //TODO implement search for underwater cells int Map::getCellId(Vector3 pos){ int numHorCells = int(mapSize.x / CELL_SIZE.x), horId = -1; @@ -288,6 +293,19 @@ namespace battleship{ break; } - return vertId * numHorCells + horId; + int surfaceCellId = vertId * numHorCells + horId; + + if(cells[surfaceCellId].type == Cell::Type::WATER && !cells[surfaceCellId].underWaterCellIds.empty()){ + int cellId = surfaceCellId; + + for(int i = 0; i <= cells[surfaceCellId].underWaterCellIds.size(); i++){ + cellId = (i == 0 ? surfaceCellId : cells[surfaceCellId].underWaterCellIds[i - 1]); + + if(fabs(cells[cellId].pos.y - pos.y) < .5 * CELL_SIZE.y) + return cellId; + } + } + else + return surfaceCellId; } } diff --git a/map.h b/map.h index 4064997..f83bc2e 100755 --- a/map.h +++ b/map.h @@ -36,7 +36,7 @@ namespace battleship{ std::vector underWaterCellIds; Cell(){} - Cell(vb01::Vector3 p, Type t, std::vector e = std::vector{}): pos(p), type(t), edges(e){} + Cell(vb01::Vector3 p, Type t, std::vector e = std::vector{}, std::vector uc = std::vector{}): pos(p), type(t), edges(e), underWaterCellIds(uc){} int getEdgeWeight(int); }; diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 8ad0d1c..5de5dde 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -103,11 +103,11 @@ namespace battleship{ mat->addBoolUniform("texturingEnabled", true); mat->addTexUniform("textures[0]", waterTextures[0], false); - Vector3 size = Vector3(10, 10, 1); + Vector3 size = Vector3(30, 30, 1); Quad *quad = new Quad(size, true); quad->setMaterial(mat); - Vector3 pos = 0.1 * Vector3::VEC_J; + Vector3 pos = 12 * Vector3::VEC_J; Node *node = new Node(pos); node->attachMesh(quad); @@ -349,6 +349,9 @@ namespace battleship{ int aboveCellId = (j == 0 ? waterCells[i].edges[0].srcCellId : j - 1); vector edges = vector{Map::Edge(weight, waterCells[i].underWaterCellIds[j], aboveCellId)}; + if(waterCells[i].underWaterCellIds.size() > j + 1) + edges.push_back(Map::Edge(weight, waterCells[i].underWaterCellIds[j], waterCells[i].underWaterCellIds[j + 1])); + for(int k = 0; k < waterCells[i].edges.size(); k++){ Map::Cell adjacentUnderwaterCell = cells[waterCells[i].edges[k].destCellId]; @@ -423,6 +426,18 @@ namespace battleship{ for(Map::Edge edge : cell.edges) mapScript += "{srcCellId = " + to_string(edge.srcCellId) + ", destCellId = " + to_string(edge.destCellId) + ", weight = " + to_string(edge.weight) + "}, "; + int numSubCells = cell.underWaterCellIds.size(); + mapScript += "}, numUnderWaterCells = " + to_string(numSubCells) + ","; + + if(numSubCells > 0){ + mapScript += "underWaterCellId = {"; + + for(int subCellId : cell.underWaterCellIds) + mapScript += to_string(subCellId) + ", "; + + mapScript += "}"; + } + mapScript += "}\n},\n"; }