From 72cbdd1ad8bbdac436d887c3fc77607c55710a4b Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 13 Aug 2022 17:11:27 +0300 Subject: [PATCH] refactored order addition --- activeGameState.cpp | 11 +++++------ activeGameState.h | 3 +-- cruiser.cpp | 4 ++-- demoJet.cpp | 2 +- destroyer.cpp | 4 ++-- inGameAppState.cpp | 10 +++------- inGameAppState.h | 2 +- jet.cpp | 12 ++++++------ map.cpp | 24 ++++++++++++++++-------- map.h | 13 +++++++++---- missile.cpp | 6 +++--- missile.h | 4 ++-- missileJet.cpp | 17 ++++++++--------- missileJet.h | 2 +- submarine.cpp | 2 +- unit.cpp | 30 +++++++++++++++++++++++------- unit.h | 13 +++++++------ vessel.cpp | 2 +- 18 files changed, 92 insertions(+), 69 deletions(-) diff --git a/activeGameState.cpp b/activeGameState.cpp index 57a4d44..d0b1186 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -33,13 +33,12 @@ namespace battleship{ const int size = 50; - ActiveGameState::ActiveGameState(GuiAppState *guiState, Map *map, vector players, int playerId) : AbstractAppState( + ActiveGameState::ActiveGameState(GuiAppState *guiState, vector players, int playerId) : AbstractAppState( AppStateType::ACTIVE_STATE, configData::calcSumBinds(AppStateType::ACTIVE_STATE, true), configData::calcSumBinds(AppStateType::ACTIVE_STATE, false), GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ this->guiState = guiState; - this->map = map; this->players = players; GameManager *gm = GameManager::getSingleton(); @@ -226,7 +225,7 @@ namespace battleship{ for (int i = 0; i < selectedUnits.size(); ++i) { Unit *u = selectedUnits[i]; - lineRenderer->addLine(u->getPos(), *targets[i].pos, color); + lineRenderer->addLine(u->getPos(), targets[i].pos, color); vector lines = lineRenderer->getLines(); o.line = lines[lines.size() - 1]; @@ -234,7 +233,7 @@ namespace battleship{ if (type != Order::TYPE::LAUNCH || (u->getId() == 4 || u->getId() == 5)) { if(type == Order::TYPE::PATROL){ Vector3 p = u->getPos(); - targets[0] = Order::Target(nullptr, new Vector3(p)); + targets[0] = Order::Target(nullptr, p); } if (addOrder) @@ -254,7 +253,7 @@ namespace battleship{ vector results; Vector3 rayDir = (endPos - camPos).norm(); - Ray::retrieveCollisions(camPos, rayDir, map->getNodeParent(), results); + Ray::retrieveCollisions(camPos, rayDir, Map::getSingleton()->getNodeParent(), results); std::map unitData; Ray::sortResults(results); @@ -263,7 +262,7 @@ namespace battleship{ vector ancestors = results[0].mesh->getNode()->getAncestors(); Node *node = ancestors[ancestors.size() - 2]; std::map::iterator it = unitData.find(node); - targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), new Vector3(results[0].pos))); + targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), results[0].pos)); } } diff --git a/activeGameState.h b/activeGameState.h index ebd6866..39b4a65 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -13,7 +13,7 @@ namespace vb01{ namespace battleship{ class ActiveGameState : public gameBase::AbstractAppState { public: - ActiveGameState(GuiAppState*, Map*, std::vector players, int); + ActiveGameState(GuiAppState*, std::vector players, int); ~ActiveGameState(); void onAttached(); void onDettached(); @@ -35,7 +35,6 @@ namespace battleship{ void orientCamera(vb01::Vector3, double); GuiAppState *guiState; - Map *map; std::vector players; Player *mainPlayer; vb01::Quad *dragbox = nullptr; diff --git a/cruiser.cpp b/cruiser.cpp index 4706d28..03fa3e4 100755 --- a/cruiser.cpp +++ b/cruiser.cpp @@ -15,10 +15,10 @@ namespace battleship{ void Cruiser::launch(Order order) { if (guidedMissiles > 0) { - float angle = order.targets[0].pos->getAngleBetween(dirVec); + float angle = order.targets[0].pos.getAngleBetween(dirVec); Quaternion rotQuat = Quaternion(dirVec.x < 0 ? angle : -angle, Vector3(0, 1, 0)); Vector3 basePos = leftVec * projectileData::pos[getId()][1][guidedMissiles - 1].x + upVec * projectileData::pos[getId()][1][guidedMissiles - 1].y - dirVec * projectileData::pos[getId()][1][guidedMissiles - 1].z; - addProjectile(new GuidedMissile(this, pos + basePos, *order.targets[0].pos, rotQuat * Vector3(0, 1, 0), rotQuat * Vector3(1, 0, 0), rotQuat * Vector3(0, 0, -1), getId(), 1, 0)); + addProjectile(new GuidedMissile(this, pos + basePos, order.targets[0].pos, rotQuat * Vector3(0, 1, 0), rotQuat * Vector3(1, 0, 0), rotQuat * Vector3(0, 0, -1), getId(), 1, 0)); removeOrder(0); guidedMissiles--; } diff --git a/demoJet.cpp b/demoJet.cpp index 7021165..9a1e261 100755 --- a/demoJet.cpp +++ b/demoJet.cpp @@ -14,7 +14,7 @@ namespace battleship{ void DemoJet::attack(Order order){ if(!onBoard){ Unit::attack(order); - Vector3 target = *order.targets[0].pos; + Vector3 target = order.targets[0].pos; float angle = dirVec.getAngleBetween(target - pos); Vector3 axis = dirVec.cross(target - pos); diff --git a/destroyer.cpp b/destroyer.cpp index 70579b1..766dc14 100755 --- a/destroyer.cpp +++ b/destroyer.cpp @@ -30,13 +30,13 @@ namespace battleship{ } void Destroyer::attack(Order order){ - Vector3 t = *order.targets[0].pos; + Vector3 t = order.targets[0].pos; bool sub=false; InGameAppState *inGameState=((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()) - if(u->getType() == UNIT_TYPE::SUBMARINE&&order.targets[0].pos == u->getPosPtr()) + if(u->getType() == UNIT_TYPE::SUBMARINE && order.targets[0].pos == u->getPos()) sub = true; if(sub) diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 6e698ad..121b5eb 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -183,8 +183,7 @@ namespace battleship{ this->playerId = 0; this->difficultyLevels = difficultyLevels; this->factions = factions; - - map = new Map(mapName); + this->mapName = mapName; } InGameAppState::~InGameAppState() { @@ -193,7 +192,7 @@ namespace battleship{ void InGameAppState::onAttached() { AbstractAppState::onAttached(); - map->load(); + Map::getSingleton()->load(mapName); for (int i = 0; i < factions.size(); i++) { int faction; @@ -219,7 +218,7 @@ namespace battleship{ mainPlayer = players[playerId]; StateManager *stateManager = GameManager::getSingleton()->getStateManager(); guiState = ((GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE)); - activeState = new ActiveGameState(guiState, map, players, playerId); + activeState = new ActiveGameState(guiState, players, playerId); stateManager->attachAppState(activeState); UnitDataManager *unitDataManager = UnitDataManager::getSingleton(); @@ -238,9 +237,6 @@ namespace battleship{ } void InGameAppState::update() { - if (map) - map->update(); - for (Player *p : players) if (p) { p->update(); diff --git a/inGameAppState.h b/inGameAppState.h index c5089c8..a0422bb 100755 --- a/inGameAppState.h +++ b/inGameAppState.h @@ -135,8 +135,8 @@ namespace battleship{ std::vector difficultyLevels, factions; std::vector projectiles; std::vector fx; + std::string mapName; Player *mainPlayer; - Map *map; int playerId; ActiveGameState* activeState; GuiAppState* guiState; diff --git a/jet.cpp b/jet.cpp index a38cf67..f469529 100755 --- a/jet.cpp +++ b/jet.cpp @@ -24,14 +24,14 @@ namespace battleship{ if (orders.size() == 0) { Order o; o.type = Order::TYPE::MOVE; - o.targets.push_back(Order::Target(nullptr, new Vector3(lp))); + o.targets.push_back(Order::Target(nullptr, lp)); setOrder(o); } if (!landing - &&aircraftCarrier - &&pos.getDistanceFrom(lp) <= unitData::destinationOffset[id] - &&*orders[0].targets[0].pos == lp){ + && aircraftCarrier + && pos.getDistanceFrom(lp) <= unitData::destinationOffset[id] + && orders[0].targets[0].pos == lp){ float angle = dirVec.getAngleBetween(aircraftCarrier->getDirVec()), maxAngle = PI - anglePrecision[id] / 180 * PI; if(!toTurnPoint && anglePrecision[id] / 180 * PI < angle && angle < maxAngle){ @@ -47,7 +47,7 @@ namespace battleship{ Vector3 tanPoint = pos + (jetSideVec + carrierSideVec) * radius; float ang1 = (tanPoint - lp).getAngleBetween(carrierSideVec); float ang2 = dirVec.getAngleBetween(-carrierSideVec); - float base = (tanPoint-lp).getLength() * cos(ang1); + float base = (tanPoint - lp).getLength() * cos(ang1); float hyp = base / cos(ang2); landingPos = (pos + dirVec * hyp); toTurnPoint = true; @@ -84,7 +84,7 @@ namespace battleship{ Vector3 offsetDir = Quaternion(PI / 2, upVec) * destDir; Vector3 destDirPos = Quaternion(angle, upVec) * dirVec; Vector3 offsetDirPos = Quaternion(angle + PI / 2, upVec) * dirVec; - Vector3 hypVec = *orders[0].targets[0].pos - destDirPos; + Vector3 hypVec = orders[0].targets[0].pos - destDirPos; float triAngle = hypVec.getAngleBetween(offsetDir); float distance = cos(triAngle) * hypVec.getLength(); Vector3 destPoint = offsetDirPos + offsetDirPos.norm() * distance; diff --git a/map.cpp b/map.cpp index 8200a61..4609337 100755 --- a/map.cpp +++ b/map.cpp @@ -17,15 +17,14 @@ using namespace gameBase; namespace battleship{ using namespace configData; - Map::Map(string mapName) { - this->mapName = mapName; - } + Map *map = nullptr; - Map::~Map() { - } + Map* Map::getSingleton(){ + if(!map) + map = new Map; - void Map::update() { - } + return map; + } void Map::loadSkybox(LuaManager *luaManager){ int numPaths = 6; @@ -102,7 +101,8 @@ namespace battleship{ } } - void Map::load() { + void Map::load(string mapName) { + this->mapName = mapName; LuaManager *luaManager = LuaManager::getSingleton(); luaManager->buildScript(vector{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"}); @@ -174,4 +174,12 @@ namespace battleship{ } void Map::unload() {} + + Vector3 Map::getCellPos(){ + return Vector3::VEC_ZERO; + } + + int Map::getCellId(){ + return 0; + } } diff --git a/map.h b/map.h index d362a1c..9512e95 100755 --- a/map.h +++ b/map.h @@ -20,19 +20,24 @@ namespace battleship{ class Map { public: - Map(std::string); - ~Map(); - void update(); - void load(); + static Map* getSingleton(); + ~Map(){} + void update(){} + void load(std::string); void unload(); + vb01::Vector3 getCellPos(); + int getCellId(); inline vb01::Node* getNodeParent(){return nodeParent;} + inline vb01::Vector2 getSize(){return size;} private: vb01::Node *nodeParent = nullptr; std::string mapName; float cellSize = 1; GraphNode*** cells = nullptr; int **weights = nullptr; + vb01::Vector2 size; + Map(){} void loadSkybox(gameBase::LuaManager*); void loadTerrain(gameBase::LuaManager*); void loadWaterbodies(gameBase::LuaManager*); diff --git a/missile.cpp b/missile.cpp index 472ebd3..bff4b28 100755 --- a/missile.cpp +++ b/missile.cpp @@ -6,7 +6,7 @@ using namespace vb01; namespace battleship{ - Missile::Missile(Unit *unit, Node *node, Vector3 *target, Vector3 pos, Vector3 dir, Vector3 left, Vector3 up, int id, int weaponTypeId, int weaponId) : + Missile::Missile(Unit *unit, Node *node, Vector3 target, Vector3 pos, Vector3 dir, Vector3 left, Vector3 up, int id, int weaponTypeId, int weaponId) : Projectile(unit, node, pos, dir, left, up, id, weaponTypeId, weaponId) { this->target = target; this->type = (MissileType)weaponId; @@ -17,8 +17,8 @@ namespace battleship{ Projectile::update(); pos = pos + dirVec * speed; node->setPosition(pos); - float angle = dirVec.getAngleBetween(*target - pos); - Vector3 axis = dirVec.cross(*target - pos).norm(); + float angle = dirVec.getAngleBetween(target - pos); + Vector3 axis = dirVec.cross(target - pos).norm(); if(rotationSpeed / 180 * PI < angle){ Quaternion rotQuat = Quaternion(rotationSpeed / 180 * PI, axis); diff --git a/missile.h b/missile.h index 7703eca..6d04da4 100755 --- a/missile.h +++ b/missile.h @@ -9,13 +9,13 @@ namespace battleship{ class Missile : public Projectile { public: - Missile(Unit*, vb01::Node*, vb01::Vector3*, vb01::Vector3, vb01::Vector3, vb01::Vector3, vb01::Vector3, int, int, int); + Missile(Unit*, vb01::Node*, vb01::Vector3, vb01::Vector3, vb01::Vector3, vb01::Vector3, vb01::Vector3, int, int, int); ~Missile(){} void update(); private: s64 initTime; int selfDistructTime = 10000; - vb01::Vector3 *target; + vb01::Vector3 target; float rotationSpeed = 5; MissileType type; }; diff --git a/missileJet.cpp b/missileJet.cpp index 44ae2db..78b06d2 100755 --- a/missileJet.cpp +++ b/missileJet.cpp @@ -15,24 +15,23 @@ namespace battleship{ void MissileJet::attack(Order order) { if(!onBoard && canFire() && missilesInstalled){ - Vector3 target = *order.targets[0].pos; + Vector3 target = order.targets[0].pos; float distance=pos.getDistanceFrom(target); if(distance <= range){ - Vector3 *targetPtr = nullptr; + Vector3 targetPtr = Vector3::VEC_ZERO; InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()){ bool jet = (u->getType() == UNIT_TYPE::MISSILE_JET || u->getType() == UNIT_TYPE::DEMO_JET); - if((jet && type == AAM) && (!jet && type == AWM) && u->getPosPtr() == order.targets[0].pos) - targetPtr = u->getPosPtr(); + + if((jet && type == AAM) && (!jet && type == AWM) && u->getPos() == order.targets[0].pos) + targetPtr = u->getPos(); } - if(!targetPtr){ - targetPtr = new Vector3(); - *targetPtr = target; - } + if(targetPtr != Vector3::VEC_ZERO) + targetPtr = target; fireMissile(targetPtr); } @@ -45,7 +44,7 @@ namespace battleship{ removeOrder(0); } - void MissileJet::fireMissile(Vector3 *t) { + void MissileJet::fireMissile(Vector3 t) { Vector3 p = pos + leftVec * projectileData::pos[id][0][missiles - 1].x + upVec * projectileData::pos[id][0][missiles - 1].y - dirVec * projectileData::pos[id][0][missiles - 1].z; addProjectile(new Missile(this, missileNodes[missiles-1], t, p, dirVec, leftVec, upVec, id, 0, 0)); missileNodes[missiles-1]->setParent(Root::getSingleton()->getRootNode()); diff --git a/missileJet.h b/missileJet.h index 855daf9..02188a4 100755 --- a/missileJet.h +++ b/missileJet.h @@ -15,7 +15,7 @@ namespace battleship { void installMissiles(bool); private: void attack(Order); - void fireMissile(vb01::Vector3*); + void fireMissile(vb01::Vector3); inline bool canFire() {return missiles > 0 && vb01::getTime() - lastFireTime > rateOfFire;} MissileType type; vb01::Node *missileNodes[2]{nullptr,nullptr}; diff --git a/submarine.cpp b/submarine.cpp index 4726508..251fa23 100755 --- a/submarine.cpp +++ b/submarine.cpp @@ -12,7 +12,7 @@ namespace battleship{ Submarine::Submarine(Player *player, Vector3 pos, int id) : Unit(player, pos, id) {} void Submarine::attack(Order order) { - float angle = dirVec.getAngleBetween(*(order.targets[0].pos) - pos); + float angle = dirVec.getAngleBetween(order.targets[0].pos - pos); if(angle / PI * 180 > 50) Unit::move(order, range); diff --git a/unit.cpp b/unit.cpp index 3e7d3b5..20a7f1b 100755 --- a/unit.cpp +++ b/unit.cpp @@ -11,6 +11,7 @@ #include "unit.h" #include "util.h" #include "inGameAppState.h" +#include "pathfinder.h" #include "unitData.h" using namespace glm; @@ -152,7 +153,7 @@ 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; + Vector3 dest = order.targets[targetId].pos; dest.y = pos.y; Vector3 center; @@ -270,14 +271,29 @@ namespace battleship{ return projectiles; } + + void Unit::addOrder(Order order){ + u32 **weights = new u32*; + float eps = getCircleRadius(); + Map *map = Map::getSingleton(); + Vector2 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()); + pathPoints.clear(); + + for(int p : path) + pathPoints.push_back(map->getCellPos()); + + orders.push_back(order); + } void Unit::removeOrder(int id) { - LineRenderer::getSingleton()->removeLine(orders[id].line.id); - - for(Order::Target t : orders[id].targets) - if(t.unit) - delete t.pos; - + LineRenderer::getSingleton()->removeLine(orders[id].line.id); orders.erase(orders.begin() + id); } diff --git a/unit.h b/unit.h index 77cac1c..f95e7b4 100755 --- a/unit.h +++ b/unit.h @@ -28,11 +28,11 @@ namespace battleship{ enum class TYPE {ATTACK, MOVE, PATROL, LAUNCH}; struct Target{ Unit *unit = nullptr; - vb01::Vector3 *pos = nullptr; + vb01::Vector3 pos; Target(){} - Target(Unit *unit, vb01::Vector3 *pos){ + Target(Unit *unit, vb01::Vector3 pos){ this->unit = unit; this->pos = pos; } @@ -62,7 +62,7 @@ namespace battleship{ float getCircleRadius(); vb01::Vector3 getCorner(int); std::vector getProjectiles(); - inline void addOrder(Order o){orders.push_back(o);} + void addOrder(Order); inline bool isSelected(){return selected;} inline bool isSelectable(){return selectable;} inline bool isDebuggable(){return debugging;} @@ -93,9 +93,10 @@ namespace battleship{ const int orderVecDispLength = 2000; sf::SoundBuffer *selectionSfxBuffer; sf::Sound *selectionSfx = nullptr; - vb01::Quad *hpBackground = nullptr, *hpForeground = nullptr; - vb01::Node *hpBackgroundNode = nullptr, *hpForegroundNode = nullptr; - int lenHpBar = 200; + vb01::Quad *hpBackground = nullptr, *hpForeground = nullptr; + vb01::Node *hpBackgroundNode = nullptr, *hpForegroundNode = nullptr; + int lenHpBar = 200; + std::vector pathPoints; protected: Player *player; MoveDir moveDir = MoveDir::FORWARD; diff --git a/vessel.cpp b/vessel.cpp index 27adbbc..f674de5 100755 --- a/vessel.cpp +++ b/vessel.cpp @@ -121,7 +121,7 @@ namespace battleship{ } void Vessel::attack(Order order) { - Vector3 target = *order.targets[0].pos; + Vector3 target = order.targets[0].pos; for (Turret *t : turrets) { Vector3 turrPos = t->getNode()->localToGlobalPosition(Vector3::VEC_ZERO);