diff --git a/Assets/Scripts/unitData.lua b/Assets/Scripts/unitData.lua index 110980a..f11a89c 100644 --- a/Assets/Scripts/unitData.lua +++ b/Assets/Scripts/unitData.lua @@ -1,15 +1,26 @@ numUnits = 11 -UnitType = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6}; +UnitClass = {VESSEL = 0, DESTROYER = 1, CRUISER = 2, AIRCRAFT_CARRIER = 3, SUBMARINE = 4, MISSILE_JET = 5, DEMO_JET = 6}; + +UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3}; + +unitClass = { + UnitClass.VESSEL, UnitClass.VESSEL, + UnitClass.DESTROYER, UnitClass.DESTROYER, + UnitClass.CRUISER, UnitClass.CRUISER, + UnitClass.AIRCRAFT_CARRIER, UnitClass.AIRCRAFT_CARRIER, + UnitClass.SUBMARINE, + UnitClass.MISSILE_JET, UnitClass.DEMO_JET +}; unitType = { - UnitType.VESSEL, UnitType.VESSEL, - UnitType.DESTROYER, UnitType.DESTROYER, - UnitType.CRUISER, UnitType.CRUISER, - UnitType.AIRCRAFT_CARRIER, UnitType.AIRCRAFT_CARRIER, - UnitType.SUBMARINE, - UnitType.MISSILE_JET, UnitType.DEMO_JET -}; + UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, + UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, + UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, + UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, + UnitType.UNDERWATER, + UnitType.AIR, UnitType.AIR +} health = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500} speed = {.025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15} @@ -17,7 +28,7 @@ destinationOffset = {.75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5} anglePrecision = {.1, .1, .1, .1, .1, .1, .1, .1, .1, 3, 3} cost = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500} maxTurnAngle = {1, 1, .5, .5, 1, 1, .5, .5, 1, 2, 2} -range = {15, 15, 14, 14, 13, 13, 12, 12,15,25} +range = {15, 15, 14, 14, 13, 13, 12, 12, 15, 25} unitCornerPoints = { { diff --git a/activeGameState.cpp b/activeGameState.cpp index e2e4a5f..d936047 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -300,7 +300,7 @@ namespace battleship{ case Bind::TOGGLE_SUB: if (isPressed) { for (Unit *u : selectedUnits) { - if (u->getPlayer() == mainPlayer && u->getType() == UnitType::SUBMARINE) { + if (u->getPlayer() == mainPlayer && u->getUnitClass() == UnitClass::SUBMARINE) { Submarine *s = (Submarine*) u; if (s->isSubmerged()) diff --git a/destroyer.cpp b/destroyer.cpp index 8d8201c..01fa521 100755 --- a/destroyer.cpp +++ b/destroyer.cpp @@ -36,7 +36,7 @@ namespace battleship{ for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()) - if(u->getType() == UnitType::SUBMARINE && order.targets[0].pos == u->getPos()) + if(u->getUnitClass() == UnitClass::SUBMARINE && order.targets[0].pos == u->getPos()) sub = true; if(sub) diff --git a/inGameAppState.cpp b/inGameAppState.cpp index e653e9d..c6c8a02 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -241,75 +241,74 @@ namespace battleship{ if (p) { p->update(); - for (int i=0;igetNumberOfUnits();i++){ - Unit *u=p->getUnit(i); + for (int i = 0; i < p->getNumberOfUnits(); i++){ + Unit *u = p->getUnit(i); if(u->isWorking()) u->update(); else{ - int selectedId=-1; - std::vector &units=p->getUnits(),&selectedUnits=activeState->getSelectedUnits(); - units.erase(units.begin()+i); + int selectedId = -1; + std::vector &units = p->getUnits(), &selectedUnits = activeState->getSelectedUnits(); + units.erase(units.begin() + i); - if(u->getType() == UnitType::MISSILE_JET || u->getType() == UnitType::DEMO_JET){ - AircraftCarrier *carrier=nullptr; + if(u->getUnitClass() == UnitClass::MISSILE_JET || u->getUnitClass() == UnitClass::DEMO_JET){ + AircraftCarrier *carrier = nullptr; - for(int i2=0;i2getUnits().size()&&!carrier;i2++){ - AircraftCarrier *a=((Jet*)u)->getAircraftCarrier(); + for(int i2 = 0; i2 < p->getUnits().size() && !carrier; i2++){ + AircraftCarrier *a = ((Jet*)u)->getAircraftCarrier(); if(a) - carrier=a; + carrier = a; } if(carrier){ - Jet** jets=carrier->getJets(); - jets[((Jet*)u)->getJetId()]=nullptr; + Jet** jets = carrier->getJets(); + jets[((Jet*)u)->getJetId()] = nullptr; } } - else if(u->getType() == UnitType::AIRCRAFT_CARRIER){ - AircraftCarrier *carrier=(AircraftCarrier*)u; + else if(u->getUnitClass() == UnitClass::AIRCRAFT_CARRIER){ + AircraftCarrier *carrier = (AircraftCarrier*)u; - for(int i2=0;i2getMaxNumJets();i2++){ - Jet *j=carrier->getJet(i2); + for(int i2 = 0; i2 < carrier->getMaxNumJets(); i2++){ + Jet *j = carrier->getJet(i2); - if(j&&j->isOnBoard()) + if(j && j->isOnBoard()) j->blowUp(); } } - for(int i2=0;i2<10;i2++){ - int id=-1; - std::vector &group=activeState->getUnitGroup(i2); + for(int i2 = 0; i2 < 10; i2++){ + int id = -1; + std::vector &group = activeState->getUnitGroup(i2); - for(int i3=0;i3isExploded()){ + if(!p->isExploded()) p->update(); - } else{ delete p; - projectiles.erase(projectiles.begin()+i); + projectiles.erase(projectiles.begin() + i); } } } diff --git a/map.cpp b/map.cpp index 2d9094b..6063e09 100755 --- a/map.cpp +++ b/map.cpp @@ -7,6 +7,7 @@ #include #include "map.h" +#include "pathfinder.h" #include "gameManager.h" #include "defConfigs.h" @@ -114,6 +115,8 @@ namespace battleship{ LuaManager *luaManager = LuaManager::getSingleton(); luaManager->buildScript(vector{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"}); + Pathfinder::getSingleton()->setImpassibleNodeVal(u16(0 - 1)); + nodeParent = new Node(); Root *root = Root::getSingleton(); root->getRootNode()->attachChild(nodeParent); @@ -131,7 +134,6 @@ namespace battleship{ Vector3 Map::getCellPos(int id, Vector3 cellSize){ int numCellsX = size.x / cellSize.x; - int numCellsY = size.y / cellSize.y; int numCellsZ = size.z / cellSize.z; Vector3 initPos = Vector3(size.x, 0, size.z) * -0.5; @@ -144,12 +146,11 @@ namespace battleship{ int Map::getCellId(Vector3 pos, Vector3 cellSize){ int numCellsX = size.x / cellSize.x; - int numCellsY = size.y / cellSize.y; int numCellsZ = size.z / cellSize.z; Vector3 initPos = Vector3(size.x, 0, size.z) * -0.5; int x = fabs(pos.x - initPos.x) / cellSize.x; - int y = fabs(pos.y - initPos.y) / cellSize.y; + int y = (cellSize.y > 0 ? (fabs(pos.y - initPos.y) / cellSize.y) : 0); int z = fabs(pos.z - initPos.z) / cellSize.z; return (numCellsX * numCellsZ * y + (numCellsX * z + x)); diff --git a/map.h b/map.h index 79bbda3..fafedef 100755 --- a/map.h +++ b/map.h @@ -43,7 +43,7 @@ namespace battleship{ std::string mapName; std::vector waterBodies; //TODO replace hardcoded values - vb01::Vector3 size = vb01::Vector3(20, 6, 20); + vb01::Vector3 size = vb01::Vector3(100, 6, 100); Map(){} void loadSkybox(gameBase::LuaManager*); diff --git a/missileJet.cpp b/missileJet.cpp index 4618510..0a7cbe4 100755 --- a/missileJet.cpp +++ b/missileJet.cpp @@ -24,7 +24,7 @@ namespace battleship{ for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()){ - bool jet = (u->getType() == UnitType::MISSILE_JET || u->getType() == UnitType::DEMO_JET); + bool jet = (u->getUnitClass() == UnitClass::MISSILE_JET || u->getUnitClass() == UnitClass::DEMO_JET); if((jet && type == AAM) && (!jet && type == AWM) && u->getPos() == order.targets[0].pos) targetPtr = u->getPos(); diff --git a/pathfinder.cpp b/pathfinder.cpp index 176843f..8be2df6 100644 --- a/pathfinder.cpp +++ b/pathfinder.cpp @@ -1,4 +1,5 @@ #include +#include #include "pathfinder.h" #include "map.h" @@ -24,8 +25,15 @@ namespace battleship{ vector paths[size]; paths[source].push_back(source); - for(int i = 0; i < size; i++) - distances[i] = (i == source ? 0 : u16(0 - 1)); + for(int i = 0; i < size; i++){ + distances[i] = (i == source ? 0 : impassibleNodeVal); + + for(int j = 0; j < size; j++) + if(weights[i][j] > impassibleNodeVal){ + cout << "Node val higher than allowed max value\n"; + exit(-1); + } + } vector checkedNodes; diff --git a/pathfinder.h b/pathfinder.h index 84de681..008e79e 100644 --- a/pathfinder.h +++ b/pathfinder.h @@ -10,8 +10,12 @@ namespace battleship{ public: static Pathfinder* getSingleton(); std::vector findPath(vb01::u32**, int, int, int); + inline vb01::u32 getImpassibleNodeVal(){return impassibleNodeVal;} + inline void setImpassibleNodeVal(vb01::u32 val){this->impassibleNodeVal = val;} private: Pathfinder(); + + vb01::u32 impassibleNodeVal; }; } diff --git a/pathfinderTest.cpp b/pathfinderTest.cpp index 24a7d4e..1ede913 100644 --- a/pathfinderTest.cpp +++ b/pathfinderTest.cpp @@ -11,7 +11,8 @@ namespace battleship{ void PathfinderTest::testFindPath(){ int size = 7; - const u32 INF = u16(0 - 1); + const u16 INF = u16(0 - 1); + //pathfinder->setImpassibleNodeVal(INF); u32 w[size][size] = { {0, 2, 4, INF, INF, INF, INF}, {INF, 0, 1, 9, 13, INF, INF}, diff --git a/unit.cpp b/unit.cpp index 8b405c0..1b91958 100755 --- a/unit.cpp +++ b/unit.cpp @@ -28,6 +28,7 @@ namespace battleship{ range = udm->getRange()[id]; lineOfSight = udm->getLineOfSight()[id]; speed = udm->getSpeed()[id]; + unitClass = udm->getUnitClass()[id]; type = udm->getUnitType()[id]; width = udm->getUnitCornerPoints()[id][0].x - udm->getUnitCornerPoints()[id][1].x; height = udm->getUnitCornerPoints()[id][4].y - udm->getUnitCornerPoints()[id][0].y; @@ -277,24 +278,61 @@ namespace battleship{ } void Unit::generateWeights(u32 **weights, int size){ + Map *map = Map::getSingleton(); + int waterbodyId = -1; + + if(type == UnitType::UNDERWATER || type == UnitType::SEA_LEVEL) + for(int i = 0; i < map->getNumWaterBodies(); i++){ + WaterBody waterBody = map->getWaterBody(i); + Vector3 p = waterBody.pos; + Vector2 s = waterBody.size; + + if((waterBody.rect && fabs(pos.x - p.x) < s.x && fabs(pos.z - p.z) < s.y) || + (!waterBody.rect && Vector2(pos.x, pos.z).getDistanceFrom(Vector2(p.x, p.z)) < s.x)){ + waterbodyId = i; + break; + } + } + + u32 impassibleNodeVal = Pathfinder::getSingleton()->getImpassibleNodeVal(); + + for(int i = 0; i < size; i++){ + for(int j = 0; j < size; j++){ + int weight = (abs(i - j) == 1 ? 1 : impassibleNodeVal); + weights[i][j] = weight; + } + } } void Unit::addOrder(Order order){ float eps = getCircleRadius(); Map *map = Map::getSingleton(); + Vector3 mapSize = map->getSize(); - Vector3 cellSize = Vector3(int(mapSize.x / eps), 0, int(mapSize.z / eps)); - int numCells = cellSize.x * (cellSize.y > 0 ? cellSize.y : 1) * cellSize.z; + Vector3 cellSize = Vector3(eps, (type == UnitType::UNDERWATER ? int(mapSize.y / height) : 0), eps); + + int numCells = + int(mapSize.x / cellSize.x) * + (cellSize.y == 0 ? 1 : int(mapSize.y / cellSize.y)) * + int(mapSize.z / cellSize.z); u32 **weights = new u32*[numCells]; for(int i = 0; i < numCells; i++) weights[i] = new u32[numCells]; + generateWeights(weights, numCells); + Pathfinder *pathfinder = Pathfinder::getSingleton(); vector path = pathfinder->findPath(weights, numCells, map->getCellId(pos, cellSize), map->getCellId(order.targets[0].pos, cellSize)); + + for(int i = 0; i < numCells; i++) + delete[] weights[i]; + + delete[] weights; + pathPoints.clear(); for(int p : path) diff --git a/unit.h b/unit.h index f4aaf45..5fa91b9 100755 --- a/unit.h +++ b/unit.h @@ -77,6 +77,7 @@ namespace battleship{ inline vb01::Model* getNode() {return model;} inline Player* getPlayer(){return player;} inline UnitType getType() {return type;} + inline UnitClass getUnitClass() {return unitClass;} inline void toggleDebugging(bool d){this->debugging=d;} inline void takeDamage(int damage) {health -= damage;} inline int getId() {return id;} @@ -101,6 +102,7 @@ namespace battleship{ protected: Player *player; MoveDir moveDir = MoveDir::FORWARD; + UnitClass unitClass; UnitType type; vb01::Vector2 screenPos; std::vector orders; diff --git a/unitDataManager.cpp b/unitDataManager.cpp index aa55c78..c590df8 100644 --- a/unitDataManager.cpp +++ b/unitDataManager.cpp @@ -31,6 +31,7 @@ namespace battleship{ speed = new float[numUnits]; destinationOffset = new float[numUnits]; unitType = new UnitType[numUnits]; + unitClass = new UnitClass[numUnits]; anglePrecision = new float[numUnits]; cost = new int[numUnits]; maxTurnAngle = new float[numUnits]; @@ -46,7 +47,7 @@ namespace battleship{ for(int i = 0; i < numUnits; i++){ health[i] = luaManager->getIntFromTable("health", vector{Index(to_string(i + 1), false)}); speed[i] = luaManager->getFloatFromTable("speed", vector{Index(to_string(i + 1), false)}); - unitType[i] = (UnitType)luaManager->getIntFromTable("unitType", vector{Index(to_string(i + 1), false)}); + unitClass[i] = (UnitClass)luaManager->getIntFromTable("unitClass", vector{Index(to_string(i + 1), false)}); destinationOffset[i] = luaManager->getFloatFromTable("destinationOffset", vector{Index(to_string(i + 1), false)}); anglePrecision[i] = luaManager->getFloatFromTable("anglePrecision", vector{Index(to_string(i + 1), false)}); cost[i] = luaManager->getIntFromTable("cost", vector{Index(to_string(i + 1), false)}); diff --git a/unitDataManager.h b/unitDataManager.h index 992714d..7d76cff 100644 --- a/unitDataManager.h +++ b/unitDataManager.h @@ -6,7 +6,9 @@ #include namespace battleship{ - enum class UnitType {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET}; + enum class UnitClass {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET}; + + enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR}; class UnitDataManager{ public: @@ -14,6 +16,7 @@ namespace battleship{ inline int getNumUnits(){return numUnits;} inline int* getHealth(){return health;} inline UnitType* getUnitType(){return unitType;} + inline UnitClass* getUnitClass(){return unitClass;} inline float* getMaxTurnAngle(){return maxTurnAngle;} inline float* getRange(){return range;} inline float* getLineOfSight(){return lineOfSight;} @@ -29,6 +32,7 @@ namespace battleship{ int numUnits; UnitType *unitType; + UnitClass *unitClass; int *health; float *speed; float *destinationOffset;