diff --git a/src/core/include/unit.h b/src/core/include/unit.h index d60f6c36..73d8ca0b 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -148,7 +148,7 @@ public: virtual DataTypes::Radio getRadio() { return radio; } virtual DataTypes::GeneralSettings getGeneralSettings() { return generalSettings; } virtual vector getAmmo() { return ammo; } - virtual vector getTargets() { return contacts; } + virtual vector getContacts() { return contacts; } virtual list getActivePath() { return activePath; } virtual bool getIsLeader() { return isLeader; } virtual unsigned char getOperateAs() { return operateAs; } diff --git a/src/core/src/groundunit.cpp b/src/core/src/groundunit.cpp index 56ba31d9..8462a5bd 100644 --- a/src/core/src/groundunit.cpp +++ b/src/core/src/groundunit.cpp @@ -68,6 +68,10 @@ void GroundUnit::setState(unsigned char newState) case State::REACH_DESTINATION: { break; } + case State::ATTACK: { + setTargetID(NULL); + break; + } case State::FIRE_AT_AREA: { setTargetPosition(Coords(NULL)); break; @@ -102,6 +106,13 @@ void GroundUnit::setState(unsigned char newState) resetActiveDestination(); break; } + case State::ATTACK: { + setEnableTaskCheckFailed(true); + clearActivePath(); + resetActiveDestination(); + resetTask(); + break; + } case State::FIRE_AT_AREA: { setEnableTaskCheckFailed(true); clearActivePath(); @@ -174,6 +185,27 @@ void GroundUnit::AIloop() } break; } + case State::ATTACK: { + Unit* target = unitsManager->getUnit(getTargetID()); + if (target != nullptr) { + setTask("Attacking " + target->getUnitName()); + + if (!getHasTask()) { + /* Send the command */ + std::ostringstream taskSS; + taskSS.precision(10); + taskSS << "{id = 'AttackUnit', unitID = " << target->getID() << " }"; + Command* command = dynamic_cast(new SetTask(groupName, taskSS.str(), [this]() { this->setHasTaskAssigned(true); })); + scheduler->appendCommand(command); + setHasTask(true); + } + } + else { + setState(State::IDLE); + } + + break; + } case State::FIRE_AT_AREA: { setTask("Firing at area"); diff --git a/src/core/src/navyunit.cpp b/src/core/src/navyunit.cpp index b965d423..a8cc7d87 100644 --- a/src/core/src/navyunit.cpp +++ b/src/core/src/navyunit.cpp @@ -65,6 +65,10 @@ void NavyUnit::setState(unsigned char newState) case State::REACH_DESTINATION: { break; } + case State::ATTACK: { + setTargetID(NULL); + break; + } case State::FIRE_AT_AREA: { setTargetPosition(Coords(NULL)); break; @@ -91,6 +95,13 @@ void NavyUnit::setState(unsigned char newState) resetActiveDestination(); break; } + case State::ATTACK: { + setEnableTaskCheckFailed(true); + clearActivePath(); + resetActiveDestination(); + resetTask(); + break; + } case State::FIRE_AT_AREA: { setEnableTaskCheckFailed(true); clearActivePath(); @@ -148,6 +159,25 @@ void NavyUnit::AIloop() } break; } + case State::ATTACK: { + Unit* target = unitsManager->getUnit(getTargetID()); + if (target != nullptr) { + setTask("Attacking " + target->getUnitName()); + + if (!getHasTask()) { + /* Send the command */ + std::ostringstream taskSS; + taskSS.precision(10); + taskSS << "{id = 'AttackUnit', unitID = " << target->getID() << " }"; + Command* command = dynamic_cast(new SetTask(groupName, taskSS.str(), [this]() { this->setHasTaskAssigned(true); })); + scheduler->appendCommand(command); + setHasTask(true); + } + } + else { + setState(State::IDLE); + } + } case State::FIRE_AT_AREA: { setTask("Firing at area");