diff --git a/Assets/Scripts/unitData.lua b/Assets/Scripts/unitData.lua index 9297767..769fd71 100644 --- a/Assets/Scripts/unitData.lua +++ b/Assets/Scripts/unitData.lua @@ -2,7 +2,7 @@ numUnits = 4 UnitClass = {VESSEL = 0, ENGINEER = 1, SAMPLE_STRUCTURE = 2}; -UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3}; +UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, HOVER = 2, LAND = 3, AIR = 4}; unitClass = { UnitClass.VESSEL, UnitClass.VESSEL, @@ -10,8 +10,8 @@ unitClass = { }; unitType = { - UnitType.SEA_LEVEL, UnitType.UNDERWATER, - UnitType.LAND, UnitType.LAND + UnitType.HOVER, UnitType.SEA_LEVEL, + UnitType.LAND, UnitType.HOVER } isVehicle = { diff --git a/pathfinder.cpp b/pathfinder.cpp index 23adaab..7db4c59 100644 --- a/pathfinder.cpp +++ b/pathfinder.cpp @@ -1,7 +1,7 @@ #include -#include #include "pathfinder.h" +#include "unit.h" namespace battleship{ using namespace std; @@ -19,7 +19,7 @@ namespace battleship{ Pathfinder::Pathfinder(){ } - vector Pathfinder::findPath(vector &cells, int source, int dest){ + vector Pathfinder::findPath(vector &cells, int source, int dest, int unitType){ int size = cells.size(); u32 distances[size]; vector paths[size]; @@ -52,8 +52,13 @@ namespace battleship{ for(int i = 0; i < size; i++){ bool isChecked = find(checkedNodes.begin(), checkedNodes.end(), i) != checkedNodes.end(); + bool canMoveToStrichCell = true; + bool ship = ((UnitType)unitType == UnitType::UNDERWATER || (UnitType)unitType == UnitType::SEA_LEVEL); - if(!isChecked && (distances[vertStrich] + cells[vertStrich].getEdgeWeight(i) < distances[i])){ + if((ship && cells[vertStrich].type != Map::Cell::WATER) || ((UnitType)unitType == UnitType::LAND && cells[vertStrich].type != Map::Cell::LAND)) + canMoveToStrichCell = false; + + if(!isChecked && (canMoveToStrichCell && distances[vertStrich] + cells[vertStrich].getEdgeWeight(i) < distances[i])){ distances[i] = distances[vertStrich] + cells[vertStrich].getEdgeWeight(i); paths[i] = paths[vertStrich]; paths[i].push_back(i); diff --git a/pathfinder.h b/pathfinder.h index d9fed62..de76f0b 100644 --- a/pathfinder.h +++ b/pathfinder.h @@ -11,7 +11,7 @@ namespace battleship{ class Pathfinder{ public: static Pathfinder* getSingleton(); - std::vector findPath(std::vector&, int, int); + std::vector findPath(std::vector&, int, int, int = -1); inline vb01::u32 getImpassibleNodeVal(){return impassibleNodeVal;} inline void setImpassibleNodeVal(vb01::u32 val){this->impassibleNodeVal = val;} private: diff --git a/unit.h b/unit.h index 1781af6..05ed3ee 100755 --- a/unit.h +++ b/unit.h @@ -42,7 +42,7 @@ namespace battleship{ enum class MoveDir {LEFT, UP, FORW}; enum class Corner {FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT}; enum class UnitClass {VESSEL, ENGINEER, SAMPLE_BUILDING}; - enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR}; + enum class UnitType {UNDERWATER, SEA_LEVEL, HOVER, LAND, AIR, NONE = -1}; class Unit { public: diff --git a/vehicle.cpp b/vehicle.cpp index 3bf2d1e..4bcaa33 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -153,8 +153,15 @@ namespace battleship{ int source = map->getCellId(pos); int dest = map->getCellId(order.targets[0].pos); + if(type == UnitType::UNDERWATER || type == UnitType::SEA_LEVEL || type == UnitType::LAND){ + Map::Cell::Type cellType = (type == UnitType::LAND ? Map::Cell::LAND : Map::Cell::WATER); + bool canReach = (cells[source].type == cellType && cells[dest].type == cellType); + + if(!canReach) return; + } + Pathfinder *pathfinder = Pathfinder::getSingleton(); - vector path = pathfinder->findPath(cells, source, dest); + vector path = pathfinder->findPath(cells, source, dest, (int)type); Node *rootNode = Root::getSingleton()->getRootNode(); for(int p : path){