From be1644994b6519d49afa67fa97f6f2c7a88f2b01 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 20 Aug 2022 10:16:38 +0300 Subject: [PATCH] map refactoring --- map.cpp | 67 +++++++------------------------------------------- map.h | 27 ++++++++++---------- pathfinder.cpp | 2 +- pathfinder.h | 8 +++--- unit.cpp | 26 +++++++++++--------- 5 files changed, 42 insertions(+), 88 deletions(-) diff --git a/map.cpp b/map.cpp index 4609337..f6b5b6b 100755 --- a/map.cpp +++ b/map.cpp @@ -66,6 +66,7 @@ namespace battleship{ model->setMaterial(mat); root->getRootNode()->attachChild(model); nodeParent->attachChild(model); + terrainModel = model; } void Map::loadWaterbodies(LuaManager *luaManager){ @@ -73,6 +74,7 @@ namespace battleship{ int numWaterBodies = 1; string table = "waterBodies"; Root *root = Root::getSingleton(); + waterBodies = new WaterBody[numWaterBodies]; for(int i = 0; i < numWaterBodies; i++){ float sizeX = luaManager->getFloatFromTable(table, vector{Index("sizeX", true)}); @@ -89,6 +91,7 @@ namespace battleship{ mat->addTexUniform("textures[0]", t, true); quad->setMaterial(mat); + bool rect = luaManager->getBoolFromTable(table, vector{Index("rect", true)}); Node *waterNode = new Node(); waterNode->attachMesh(quad); root->getRootNode()->attachChild(waterNode); @@ -98,6 +101,10 @@ namespace battleship{ waterNode->setPosition(Vector3(posX, posY, posZ)); waterNode->setOrientation(Quaternion(-1.57, Vector3::VEC_I)); nodeParent->attachChild(waterNode); + + waterBodies[i].pos = Vector3(posX, posY, posZ); + waterBodies[i].size = Vector2(sizeX, sizeY); + waterBodies[i].rect = rect; } } @@ -119,67 +126,11 @@ namespace battleship{ cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); } - void Map::generateCells(){ - //TODO replace magic values by retrieving terrain dimensions - Vector3 terrainSize = Vector3(20, 6, 20); - Vector3 initPos = terrainSize * (-0.5); - int numCellsByDim[]{terrainSize.x / cellSize, terrainSize.y / cellSize, terrainSize.z / cellSize}; - cells = new GraphNode**[numCellsByDim[0]]; - - for(int i = 0; i < numCellsByDim[0]; i++){ - cells[i] = new GraphNode*[numCellsByDim[1]]; - - for(int j = 0; j < numCellsByDim[1]; j++) - cells[i][j] = new GraphNode[numCellsByDim[2]]; - } - - for(int i = 0; i < numCellsByDim[0]; i++){ - for(int j = 0; j < numCellsByDim[1]; j++){ - for(int k = 0; k < numCellsByDim[2]; k++){ - GraphNode::Type type = GraphNode::SEA; - - GraphNode node; - node.type = type; - node.pos = initPos + Vector3(cellSize * i, cellSize * j, cellSize * k); - cells[i][j][k] = node; - } - } - } - - int numCells = numCellsByDim[0] * numCellsByDim[1] * numCellsByDim[2]; - weights = new int*[numCells]; - - for(int i = 0; i < numCells; i++){ - weights[i] = new int[numCells]; - int x1 = i / numCellsByDim[2] / numCellsByDim[1]; - int y1 = i / numCellsByDim[2] % numCellsByDim[1]; - int z1 = i % numCellsByDim[2]; - - for(int j = 0; j < numCells; j++){ - int x2 = j / numCellsByDim[2] / numCellsByDim[1]; - int y2 = j / numCellsByDim[2] % numCellsByDim[1]; - int z2 = j % numCellsByDim[2]; - int weight; - - if(abs(x1 - x2) == 1 || abs(y1 - y2) == 1 || abs(z1 - z2) == 1) - weight = 1; - else if(x1 == x2 && y1 == y2 && z1 == z2) - weight = 0; - else - weight = -1; - - weights[i][j] = weight; - } - } - } - void Map::unload() {} - Vector3 Map::getCellPos(){ - return Vector3::VEC_ZERO; + Vector3 Map::getCellPos(int id){ } - int Map::getCellId(){ - return 0; + int Map::getCellId(Vector3 pos){ } } diff --git a/map.h b/map.h index 9512e95..4f51913 100755 --- a/map.h +++ b/map.h @@ -11,12 +11,11 @@ namespace gameBase{ } namespace battleship{ - struct GraphNode{ - enum Type{LAND, SEA, AIR}; - - Type type; - vb01::Vector3 pos; - }; + struct WaterBody{ + vb01::Vector3 pos; + vb01::Vector2 size; + bool rect; + }; class Map { public: @@ -25,23 +24,23 @@ namespace battleship{ void update(){} void load(std::string); void unload(); - vb01::Vector3 getCellPos(); - int getCellId(); + vb01::Vector3 getCellPos(int); + int getCellId(vb01::Vector3); + inline WaterBody getWaterBody(int i){return waterBodies[i];} + inline vb01::Model* getTerrainModel(){return terrainModel;} inline vb01::Node* getNodeParent(){return nodeParent;} - inline vb01::Vector2 getSize(){return size;} + inline vb01::Vector3 getSize(){return size;} private: vb01::Node *nodeParent = nullptr; + vb01::Model *terrainModel = nullptr; std::string mapName; - float cellSize = 1; - GraphNode*** cells = nullptr; - int **weights = nullptr; - vb01::Vector2 size; + WaterBody *waterBodies = nullptr; + vb01::Vector3 size = vb01::Vector3(20, 20, 6); Map(){} void loadSkybox(gameBase::LuaManager*); void loadTerrain(gameBase::LuaManager*); void loadWaterbodies(gameBase::LuaManager*); - void generateCells(); }; } diff --git a/pathfinder.cpp b/pathfinder.cpp index 8d9abbc..fbb02c4 100644 --- a/pathfinder.cpp +++ b/pathfinder.cpp @@ -62,6 +62,6 @@ namespace battleship{ return paths[dest]; } - void Pathfinder::generateWeights(u32 **weights, int &size, Vector3* verts[], Vector2 mapSize){ + void Pathfinder::generateWeights(u32 **weights, int &size, Vector3* verts[], Vector3 mapSize){ } } diff --git a/pathfinder.h b/pathfinder.h index 8022974..ace6686 100644 --- a/pathfinder.h +++ b/pathfinder.h @@ -8,11 +8,11 @@ namespace battleship{ class Pathfinder{ public: - static Pathfinder* getSingleton(); - std::vector findPath(vb01::u32**, int, int, int); - void generateWeights(vb01::u32**, int&, vb01::Vector3*[], vb01::Vector2); + static Pathfinder* getSingleton(); + std::vector findPath(vb01::u32**, int, int, int); + void generateWeights(vb01::u32**, int&, vb01::Vector3*[], vb01::Vector3); private: - Pathfinder(); + Pathfinder(); }; } diff --git a/unit.cpp b/unit.cpp index 20a7f1b..ced08b9 100755 --- a/unit.cpp +++ b/unit.cpp @@ -152,8 +152,8 @@ namespace battleship{ void Unit::move(Order order, float destOffset) { float movementAmmount, radius = getCircleRadius(), angle = 0.; - int targetId = (order.type == Order::TYPE::PATROL ? getNextPatrolPointId(order.targets.size()) : 0); - Vector3 dest = order.targets[targetId].pos; + int targetId = (order.type == Order::TYPE::PATROL ? getNextPatrolPointId(order.targets.size()) : 0); + Vector3 dest = pathPoints[0]; dest.y = pos.y; Vector3 center; @@ -194,13 +194,17 @@ namespace battleship{ advance(movementAmmount); - if (pos.getDistanceFrom(dest) <= destOffset && orders[0].type != Order::TYPE::ATTACK) { - moveDir = MoveDir::FORWARD; + if (pos.getDistanceFrom(dest) <= destOffset){ + pathPoints.erase(pathPoints.begin()); - if(orders[0].type == Order::TYPE::MOVE) - removeOrder(0); - else if(orders[0].type == Order::TYPE::PATROL) - patrolPointId = targetId; + if(pathPoints.empty() && orders[0].type != Order::TYPE::ATTACK) { + moveDir = MoveDir::FORWARD; + + if(orders[0].type == Order::TYPE::MOVE) + removeOrder(0); + else if(orders[0].type == Order::TYPE::PATROL) + patrolPointId = targetId; + } } } @@ -276,18 +280,18 @@ namespace battleship{ u32 **weights = new u32*; float eps = getCircleRadius(); Map *map = Map::getSingleton(); - Vector2 mapSize = map->getSize(); + Vector3 mapSize = map->getSize(); int numCells = int(mapSize.x / eps) * int(mapSize.y / eps); Pathfinder *pathfinder = Pathfinder::getSingleton(); Vector3 **verts = nullptr; pathfinder->generateWeights(weights, numCells, verts, mapSize); - vector path = pathfinder->findPath(weights, numCells, map->getCellId(), map->getCellId()); + vector path = pathfinder->findPath(weights, numCells, map->getCellId(pos), map->getCellId(order.targets[0].pos)); pathPoints.clear(); for(int p : path) - pathPoints.push_back(map->getCellPos()); + pathPoints.push_back(map->getCellPos(p)); orders.push_back(order); }