From 73d037a79a5c265dfc91fcded2a551071393aa37 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Tue, 21 Mar 2023 22:01:36 +0100 Subject: [PATCH] Added functions for activePath manipulation --- src/core/include/unit.h | 4 ++++ src/core/src/airunit.cpp | 18 +++++++++--------- src/core/src/groundunit.cpp | 2 +- src/core/src/helicopter.cpp | 2 +- src/core/src/unit.cpp | 32 ++++++++++++++++++++++++++++++-- 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/core/include/unit.h b/src/core/include/unit.h index b94f7b1a..e5e27ef0 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -98,6 +98,10 @@ public: virtual void setTargetAltitude(double newTargetAltitude) { targetAltitude = newTargetAltitude; addMeasure(L"targetAltitude", json::value(newTargetAltitude));} //TODO fix, double definition void setActiveDestination(Coords newActiveDestination) { activeDestination = newActiveDestination; addMeasure(L"activeDestination", json::value("")); } // TODO fix void setActivePath(list newActivePath); + void clearActivePath(); + void pushActivePathFront(Coords newActivePathFront); + void pushActivePathBack(Coords newActivePathBack); + void popActivePathFront(); void setTargetID(int newTargetID) { targetID = newTargetID; addMeasure(L"targetID", json::value(newTargetID));} wstring getCurrentTask() { return currentTask; } virtual double getTargetSpeed() { return targetSpeed; }; diff --git a/src/core/src/airunit.cpp b/src/core/src/airunit.cpp index cd32c094..b3c455be 100644 --- a/src/core/src/airunit.cpp +++ b/src/core/src/airunit.cpp @@ -61,8 +61,8 @@ void AirUnit::setState(int newState) if (isTargetAlive()) { Unit* target = unitsManager->getUnit(targetID); Coords targetPosition = Coords(target->getLatitude(), target->getLongitude(), 0); - activePath.clear(); - activePath.push_front(targetPosition); + clearActivePath(); + pushActivePathFront(targetPosition); resetActiveDestination(); } break; @@ -128,17 +128,17 @@ bool AirUnit::setActiveDestination() void AirUnit::createHoldingPattern() { /* Air units must ALWAYS have a destination or they will RTB and become uncontrollable */ - activePath.clear(); + clearActivePath(); Coords point1; Coords point2; Coords point3; Geodesic::WGS84().Direct(latitude, longitude, 45, 10000, point1.lat, point1.lng); Geodesic::WGS84().Direct(point1.lat, point1.lng, 135, 10000, point2.lat, point2.lng); Geodesic::WGS84().Direct(point2.lat, point2.lng, 225, 10000, point3.lat, point3.lng); - activePath.push_back(point1); - activePath.push_back(point2); - activePath.push_back(point3); - activePath.push_back(Coords(latitude, longitude)); + pushActivePathBack(point1); + pushActivePathBack(point2); + pushActivePathBack(point3); + pushActivePathBack(Coords(latitude, longitude)); log(unitName + L" holding pattern created"); } @@ -148,7 +148,7 @@ bool AirUnit::updateActivePath(bool looping) { /* Push the next destination in the queue to the front */ if (looping) - activePath.push_back(activePath.front()); + pushActivePathBack(activePath.front()); activePath.pop_front(); log(unitName + L" active path front popped"); return true; @@ -310,7 +310,7 @@ void AirUnit::AIloop() } case State::WINGMAN: { /* In the WINGMAN state, the unit relinquishes control to the leader */ - activePath.clear(); + clearActivePath(); activeDestination = Coords(NULL); if (leader == nullptr || !leader->getAlive()) { diff --git a/src/core/src/groundunit.cpp b/src/core/src/groundunit.cpp index f9525015..b01bc2b5 100644 --- a/src/core/src/groundunit.cpp +++ b/src/core/src/groundunit.cpp @@ -51,7 +51,7 @@ void GroundUnit::AIloop() if (newDist < GROUND_DEST_DIST_THR) { /* Destination reached */ - activePath.pop_front(); + popActivePathFront(); log(unitName + L" destination reached"); } } diff --git a/src/core/src/helicopter.cpp b/src/core/src/helicopter.cpp index facfd3f5..7d2d1e20 100644 --- a/src/core/src/helicopter.cpp +++ b/src/core/src/helicopter.cpp @@ -26,7 +26,7 @@ void Helicopter::changeSpeed(wstring change) if (change.compare(L"stop") == 0) { /* Air units can't hold a position, so we can only set them to hold. At the moment, this will erase any other command. TODO: helicopters should be able to hover in place */ - activePath.clear(); + clearActivePath(); } else if (change.compare(L"slow") == 0) targetSpeed -= 10 / 1.94384; diff --git a/src/core/src/unit.cpp b/src/core/src/unit.cpp index 09bb63bd..0f5fcdfd 100644 --- a/src/core/src/unit.cpp +++ b/src/core/src/unit.cpp @@ -174,6 +174,34 @@ void Unit::setActivePath(list newPath) addMeasure(L"activePath", path); } +void Unit::clearActivePath() +{ + list newPath; + setActivePath(newPath); +} + +void Unit::pushActivePathFront(Coords newActivePathFront) +{ + list path = activePath; + path.push_front(newActivePathFront); + setActivePath(path); +} + +void Unit::pushActivePathBack(Coords newActivePathBack) +{ + list path = activePath; + path.push_back(newActivePathBack); + setActivePath(path); +} + +void Unit::popActivePathFront() +{ + list path = activePath; + path.pop_front(); + setActivePath(path); +} + + void Unit::setCoalitionID(int newCoalitionID) { if (newCoalitionID == 0) @@ -316,7 +344,7 @@ void Unit::setReactionToThreat(wstring newReactionToThreat) { } void Unit::landAt(Coords loc) { - activePath.clear(); - activePath.push_back(loc); + clearActivePath(); + pushActivePathBack(loc); setState(State::LAND); } \ No newline at end of file