From 807cdfeb5b415d73398faccccb110d8ed3b40daf Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Sat, 1 Jul 2023 12:31:30 +0200 Subject: [PATCH] Added code to update ammo and contacts --- src/core/include/unit.h | 92 ++++++++++++++++++------------------ src/core/src/unit.cpp | 102 ++++++++++++++++++++++++++-------------- 2 files changed, 112 insertions(+), 82 deletions(-) diff --git a/src/core/include/unit.h b/src/core/include/unit.h index fa973d5b..df335df9 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -157,48 +157,48 @@ public: virtual void setTACAN(DataTypes::TACAN newValue, bool force = false); virtual void setRadio(DataTypes::Radio newValue, bool force = false); virtual void setGeneralSettings(DataTypes::GeneralSettings newValue, bool force = false); - virtual void setAmmo(vector newValue) { updateValue(ammo, newValue, DataIndex::ammo); } - virtual void setContacts(vector newValue) { updateValue(contacts, newValue, DataIndex::contacts); } + virtual void setAmmo(vector newValue); + virtual void setContacts(vector newValue); virtual void setActivePath(list newValue); /********** Getters **********/ - virtual string getCategory() { return category; }; - virtual bool getAlive() { return alive; } - virtual bool getHuman() { return human; } - virtual bool getControlled() { return controlled; } - virtual unsigned int getCoalition() { return coalition; } - virtual unsigned char getCountry() { return country; } - virtual string getName() { return name; } - virtual string getUnitName() { return unitName; } - virtual string getGroupName() { return groupName; } - virtual unsigned char getState() { return state; } - virtual string getTask() { return task; } - virtual bool getHasTask() { return hasTask; } - virtual Coords getPosition() { return position; } - virtual double getSpeed() { return speed; } - virtual double getHeading() { return heading; } - virtual bool getIsTanker() { return isTanker; } - virtual bool getIsAWACS() { return isAWACS; } - virtual bool getOnOff() { return onOff; }; - virtual bool getFollowRoads() { return followRoads; }; - virtual double getFuel() { return fuel; } - virtual double getDesiredSpeed() { return desiredSpeed; }; - virtual bool getDesiredSpeedType() { return desiredSpeedType; }; - virtual double getDesiredAltitude() { return desiredAltitude; }; - virtual bool getDesiredAltitudeType() { return desiredAltitudeType; }; - virtual unsigned int getLeaderID() { return leaderID; } - virtual Offset getFormationoffset() { return formationOffset; } - virtual unsigned int getTargetID() { return targetID; } - virtual Coords getTargetPosition() { return targetPosition; } - virtual unsigned char getROE() { return ROE; } - virtual unsigned char getReactionToThreat() { return reactionToThreat; } - virtual unsigned char getEmissionsCountermeasures() { return emissionsCountermeasures; }; - virtual DataTypes::TACAN getTACAN() { return TACAN; } - virtual DataTypes::Radio getRadio() { return radio; } - virtual DataTypes::GeneralSettings getGeneralSettings() { return generalSettings; } - virtual vector getAmmo() { return ammo; } - virtual vector getTargets() { return contacts; } - virtual list getActivePath() { return activePath; } + virtual const string& getCategory() { return category; }; + virtual const bool& getAlive() { return alive; } + virtual const bool& getHuman() { return human; } + virtual const bool& getControlled() { return controlled; } + virtual const unsigned char& getCoalition() { return coalition; } + virtual const unsigned char& getCountry() { return country; } + virtual const string& getName() { return name; } + virtual const string& getUnitName() { return unitName; } + virtual const string& getGroupName() { return groupName; } + virtual const unsigned char& getState() { return state; } + virtual const string& getTask() { return task; } + virtual const bool& getHasTask() { return hasTask; } + virtual const Coords& getPosition() { return position; } + virtual const double& getSpeed() { return speed; } + virtual const double& getHeading() { return heading; } + virtual const bool& getIsTanker() { return isTanker; } + virtual const bool& getIsAWACS() { return isAWACS; } + virtual const bool& getOnOff() { return onOff; }; + virtual const bool& getFollowRoads() { return followRoads; }; + virtual const unsigned short& getFuel() { return fuel; } + virtual const double& getDesiredSpeed() { return desiredSpeed; }; + virtual const bool& getDesiredSpeedType() { return desiredSpeedType; }; + virtual const double& getDesiredAltitude() { return desiredAltitude; }; + virtual const bool& getDesiredAltitudeType() { return desiredAltitudeType; }; + virtual const unsigned int& getLeaderID() { return leaderID; } + virtual const Offset& getFormationoffset() { return formationOffset; } + virtual const unsigned int& getTargetID() { return targetID; } + virtual const Coords& getTargetPosition() { return targetPosition; } + virtual const unsigned char& getROE() { return ROE; } + virtual const unsigned char& getReactionToThreat() { return reactionToThreat; } + virtual const unsigned char& getEmissionsCountermeasures() { return emissionsCountermeasures; }; + virtual const DataTypes::TACAN& getTACAN() { return TACAN; } + virtual const DataTypes::Radio& getRadio() { return radio; } + virtual const DataTypes::GeneralSettings& getGeneralSettings() { return generalSettings; } + virtual const vector& getAmmo() { return ammo; } + virtual const vector& getTargets() { return contacts; } + virtual const list& getActivePath() { return activePath; } protected: unsigned int ID; @@ -251,6 +251,13 @@ protected: /********** Private methods **********/ virtual void AIloop() = 0; + void appendString(stringstream& ss, const unsigned char& datumIndex, const string& datumValue) { + const unsigned short size = datumValue.size(); + ss.write((const char*)&datumIndex, sizeof(unsigned char)); + ss.write((const char*)&size, sizeof(unsigned short)); + ss << datumValue; + } + /********** Template methods **********/ template void updateValue(T& value, T& newValue, unsigned char datumIndex) @@ -268,13 +275,6 @@ protected: ss.write((const char*)&datumValue, sizeof(T)); } - void appendString(stringstream& ss, const unsigned char& datumIndex, string& datumValue) { - const unsigned short size = datumValue.size(); - ss.write((const char*)&datumIndex, sizeof(unsigned char)); - ss.write((const char*)&size, sizeof(unsigned short)); - ss << datumValue; - } - template void appendVector(stringstream& ss, const unsigned char& datumIndex, vector& datumValue) { const unsigned short size = datumValue.size(); diff --git a/src/core/src/unit.cpp b/src/core/src/unit.cpp index 5d3788de..01cfbc71 100644 --- a/src/core/src/unit.cpp +++ b/src/core/src/unit.cpp @@ -174,64 +174,94 @@ void Unit::updateMissionData(json::value json) void Unit::getData(stringstream& ss, unsigned long long time, bool refresh) { - /* Prepare the data packet and copy it to memory */ - /* If the unit is in a group, get the update data from the group leader and only replace the position: speed and heading */ - //if (unitsManager->isUnitInGroup(this) && !unitsManager->isUnitGroupLeader(this)) { - // DataTypes::DataPacket* p = (DataTypes::DataPacket*)data; - // p->position = position; - // p->speed = speed; - // p->heading = heading; - //} + Unit* sourceUnit = this; + if (unitsManager->isUnitInGroup(this) && !unitsManager->isUnitGroupLeader(this)) + sourceUnit = unitsManager->getGroupLeader(this); const unsigned char endOfData = DataIndex::endOfData; - ss.write((const char*)&ID, sizeof(ID)); for (auto d : updateTimeMap) { if (d.second > time) { switch (d.first) { case DataIndex::category: appendString(ss, d.first, category); break; case DataIndex::alive: appendNumeric(ss, d.first, alive); break; - case DataIndex::human: appendNumeric(ss, d.first, human); break; - case DataIndex::controlled: appendNumeric(ss, d.first, controlled); break; - case DataIndex::coalition: appendNumeric(ss, d.first, coalition); break; - case DataIndex::country: appendNumeric(ss, d.first, country); break; + case DataIndex::human: appendNumeric(ss, d.first, sourceUnit->human); break; + case DataIndex::controlled: appendNumeric(ss, d.first, sourceUnit->controlled); break; + case DataIndex::coalition: appendNumeric(ss, d.first, sourceUnit->coalition); break; + case DataIndex::country: appendNumeric(ss, d.first, sourceUnit->country); break; case DataIndex::name: appendString(ss, d.first, name); break; case DataIndex::unitName: appendString(ss, d.first, unitName); break; - case DataIndex::groupName: appendString(ss, d.first, groupName); break; - case DataIndex::state: appendNumeric(ss, d.first, state); break; - case DataIndex::task: appendString(ss, d.first, task); break; - case DataIndex::hasTask: appendNumeric(ss, d.first, hasTask); break; + case DataIndex::groupName: appendString(ss, d.first, sourceUnit->groupName); break; + case DataIndex::state: appendNumeric(ss, d.first, sourceUnit->state); break; + case DataIndex::task: appendString(ss, d.first, sourceUnit->task); break; + case DataIndex::hasTask: appendNumeric(ss, d.first, sourceUnit->hasTask); break; case DataIndex::position: appendNumeric(ss, d.first, position); break; case DataIndex::speed: appendNumeric(ss, d.first, speed); break; case DataIndex::heading: appendNumeric(ss, d.first, heading); break; - case DataIndex::isTanker: appendNumeric(ss, d.first, isTanker); break; - case DataIndex::isAWACS: appendNumeric(ss, d.first, isAWACS); break; - case DataIndex::onOff: appendNumeric(ss, d.first, onOff); break; - case DataIndex::followRoads: appendNumeric(ss, d.first, followRoads); break; + case DataIndex::isTanker: appendNumeric(ss, d.first, sourceUnit->isTanker); break; + case DataIndex::isAWACS: appendNumeric(ss, d.first, sourceUnit->isAWACS); break; + case DataIndex::onOff: appendNumeric(ss, d.first, sourceUnit->onOff); break; + case DataIndex::followRoads: appendNumeric(ss, d.first, sourceUnit->followRoads); break; case DataIndex::fuel: appendNumeric(ss, d.first, fuel); break; - case DataIndex::desiredSpeed: appendNumeric(ss, d.first, desiredSpeed); break; - case DataIndex::desiredSpeedType: appendNumeric(ss, d.first, desiredSpeedType); break; - case DataIndex::desiredAltitude: appendNumeric(ss, d.first, desiredAltitude); break; - case DataIndex::desiredAltitudeType: appendNumeric(ss, d.first, desiredAltitudeType); break; - case DataIndex::leaderID: appendNumeric(ss, d.first, leaderID); break; - case DataIndex::formationOffset: appendNumeric(ss, d.first, formationOffset); break; - case DataIndex::targetID: appendNumeric(ss, d.first, targetID); break; - case DataIndex::targetPosition: appendNumeric(ss, d.first, targetPosition); break; - case DataIndex::ROE: appendNumeric(ss, d.first, ROE); break; - case DataIndex::reactionToThreat: appendNumeric(ss, d.first, reactionToThreat); break; - case DataIndex::emissionsCountermeasures: appendNumeric(ss, d.first, emissionsCountermeasures); break; - case DataIndex::TACAN: appendNumeric(ss, d.first, TACAN); break; - case DataIndex::radio: appendNumeric(ss, d.first, radio); break; - case DataIndex::generalSettings: appendNumeric(ss, d.first, generalSettings); break; + case DataIndex::desiredSpeed: appendNumeric(ss, d.first, sourceUnit->desiredSpeed); break; + case DataIndex::desiredSpeedType: appendNumeric(ss, d.first, sourceUnit->desiredSpeedType); break; + case DataIndex::desiredAltitude: appendNumeric(ss, d.first, sourceUnit->desiredAltitude); break; + case DataIndex::desiredAltitudeType: appendNumeric(ss, d.first, sourceUnit->desiredAltitudeType); break; + case DataIndex::leaderID: appendNumeric(ss, d.first, sourceUnit->leaderID); break; + case DataIndex::formationOffset: appendNumeric(ss, d.first, sourceUnit->formationOffset); break; + case DataIndex::targetID: appendNumeric(ss, d.first, sourceUnit->targetID); break; + case DataIndex::targetPosition: appendNumeric(ss, d.first, sourceUnit->targetPosition); break; + case DataIndex::ROE: appendNumeric(ss, d.first, sourceUnit->ROE); break; + case DataIndex::reactionToThreat: appendNumeric(ss, d.first, sourceUnit->reactionToThreat); break; + case DataIndex::emissionsCountermeasures: appendNumeric(ss, d.first, sourceUnit->emissionsCountermeasures); break; + case DataIndex::TACAN: appendNumeric(ss, d.first, sourceUnit->TACAN); break; + case DataIndex::radio: appendNumeric(ss, d.first, sourceUnit->radio); break; + case DataIndex::generalSettings: appendNumeric(ss, d.first, sourceUnit->generalSettings); break; case DataIndex::ammo: appendVector(ss, d.first, ammo); break; case DataIndex::contacts: appendVector(ss, d.first, contacts); break; - case DataIndex::activePath: appendList(ss, d.first, activePath); break; + case DataIndex::activePath: appendList(ss, d.first, sourceUnit->activePath); break; } } } ss.write((const char*)&endOfData, sizeof(endOfData)); } +void Unit::setAmmo(vector newValue) +{ + if (ammo.size() == newValue.size()) { + bool equal = true; + for (int i = 0; i < ammo.size(); i++) { + if (ammo.at(i) != newValue.at(i)) + { + equal = false; + break; + } + } + if (equal) + return; + } + ammo = newValue; + triggerUpdate(DataIndex::ammo); +} + +void Unit::setContacts(vector newValue) +{ + if (contacts.size() == newValue.size()) { + bool equal = true; + for (int i = 0; i < ammo.size(); i++) { + if (contacts.at(i) != newValue.at(i)) + { + equal = false; + break; + } + } + if (equal) + return; + } + contacts = newValue; + triggerUpdate(DataIndex::contacts); +} + void Unit::setActivePath(list newPath) { activePath = newPath;