Merge pull request #122 from Pax1601/95-waypoints-not-cleared-upon-reaching

Added functions for activePath manipulation
This commit is contained in:
Pax1601 2023-03-22 08:40:57 +01:00 committed by GitHub
commit b980431aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 13 deletions

View File

@ -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<Coords> 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; };

View File

@ -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())
{

View File

@ -51,7 +51,7 @@ void GroundUnit::AIloop()
if (newDist < GROUND_DEST_DIST_THR)
{
/* Destination reached */
activePath.pop_front();
popActivePathFront();
log(unitName + L" destination reached");
}
}

View File

@ -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;

View File

@ -174,6 +174,34 @@ void Unit::setActivePath(list<Coords> newPath)
addMeasure(L"activePath", path);
}
void Unit::clearActivePath()
{
list<Coords> newPath;
setActivePath(newPath);
}
void Unit::pushActivePathFront(Coords newActivePathFront)
{
list<Coords> path = activePath;
path.push_front(newActivePathFront);
setActivePath(path);
}
void Unit::pushActivePathBack(Coords newActivePathBack)
{
list<Coords> path = activePath;
path.push_back(newActivePathBack);
setActivePath(path);
}
void Unit::popActivePathFront()
{
list<Coords> 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);
}