Converted data transfer to binary key-value method

This commit is contained in:
Pax1601
2023-06-26 18:53:04 +02:00
parent 1989219579
commit 4d9dd364b6
22 changed files with 858 additions and 885 deletions

View File

@@ -6,8 +6,6 @@ class Aircraft : public AirUnit
public:
Aircraft(json::value json, unsigned int ID);
virtual string getCategory() { return "Aircraft"; };
virtual void changeSpeed(string change);
virtual void changeAltitude(string change);
};

View File

@@ -14,7 +14,6 @@ public:
virtual void setState(unsigned char newState);
virtual string getCategory() = 0;
virtual void changeSpeed(string change) = 0;
virtual void changeAltitude(string change) = 0;

View File

@@ -7,7 +7,6 @@ class GroundUnit : public Unit
{
public:
GroundUnit(json::value json, unsigned int ID);
virtual string getCategory() { return "GroundUnit"; };
virtual void setState(unsigned char newState);

View File

@@ -6,8 +6,6 @@ class Helicopter : public AirUnit
public:
Helicopter(json::value json, unsigned int ID);
virtual string getCategory() { return "Helicopter"; };
virtual void changeSpeed(string change);
virtual void changeAltitude(string change);
};

View File

@@ -7,7 +7,6 @@ public:
NavyUnit(json::value json, unsigned int ID);
virtual void AIloop();
virtual string getCategory() { return "NavyUnit"; };
virtual void changeSpeed(string change);
};

View File

@@ -12,6 +12,50 @@ using namespace std::chrono;
#define TASK_CHECK_INIT_VALUE 10
namespace DataIndex {
enum DataIndexes {
startOfData = 0,
category,
alive,
human,
controlled,
coalition,
country,
name,
unitName,
groupName,
state,
task,
hasTask,
position,
speed,
heading,
isTanker,
isAWACS,
onOff,
followRoads,
fuel,
desiredSpeed,
desiredSpeedType,
desiredAltitude,
desiredAltitudeType,
leaderID,
formationOffset,
targetID,
targetPosition,
ROE,
reactionToThreat,
emissionsCountermeasures,
TACAN,
radio,
generalSettings,
ammo,
contacts,
activePath,
endOfData = 255
};
}
namespace State
{
enum States
@@ -33,7 +77,7 @@ namespace State
};
#pragma pack(push, 1)
namespace Options {
namespace DataTypes {
struct TACAN
{
bool isOn = false;
@@ -57,9 +101,7 @@ namespace Options {
bool prohibitAfterburner = false;
bool prohibitAirWpn = false;
};
}
namespace DataTypes {
struct Ammo {
unsigned short quantity = 0;
char name[32];
@@ -72,31 +114,6 @@ namespace DataTypes {
unsigned int ID = 0;
unsigned char detectionMethod = 0;
};
struct DataPacket {
unsigned int ID;
unsigned int bitmask;
Coords position;
double speed;
double heading;
unsigned short fuel;
double desiredSpeed;
double desiredAltitude;
unsigned int leaderID;
unsigned int targetID;
Coords targetPosition;
unsigned char coalition;
unsigned char state;
unsigned char ROE;
unsigned char reactionToThreat;
unsigned char emissionsCountermeasures;
Options::TACAN TACAN;
Options::Radio Radio;
unsigned short pathLength;
unsigned short ammoLength;
unsigned short contactsLength;
unsigned char taskLength;
};
}
#pragma pack(pop)
@@ -106,209 +123,213 @@ public:
Unit(json::value json, unsigned int ID);
~Unit();
/********** Public methods **********/
/********** Methods **********/
void initialize(json::value json);
void setDefaults(bool force = false);
unsigned int getID() { return ID; }
void runAILoop();
void updateExportData(json::value json, double dt = 0);
void updateMissionData(json::value json);
unsigned int getDataPacket(char* &data);
void getData(stringstream &ss, unsigned long long time, bool refresh);
virtual string getCategory() { return "No category"; };
/********** Base data **********/
void setControlled(bool newValue) { updateValue(controlled, newValue); }
void setName(string newValue) { updateValue(name, newValue); }
void setUnitName(string newValue) { updateValue(unitName, newValue); }
void setGroupName(string newValue) { updateValue(groupName, newValue); }
void setAlive(bool newValue) { updateValue(alive, newValue); }
void setCountry(unsigned int newValue) { updateValue(country, newValue); }
void setHuman(bool newValue) { updateValue(human, newValue); }
bool getControlled() { return controlled; }
string getName() { return name; }
string getUnitName() { return unitName; }
string getGroupName() { return groupName; }
bool getAlive() { return alive; }
unsigned int getCountry() { return country; }
bool getHuman() { return human; }
/********** Flight data **********/
void setPosition(Coords newValue) { updateValue(position, newValue); }
void setHeading(double newValue) { updateValue(heading, newValue); }
void setSpeed(double newValue) { updateValue(speed, newValue); }
Coords getPosition() { return position; }
double getHeading() { return heading; }
double getSpeed() { return speed; }
/********** Mission data **********/
void setFuel(unsigned short newValue) { updateValue(fuel, newValue); }
void setAmmo(vector<DataTypes::Ammo> newAmmo) { ammo = newAmmo; }
void setContacts(vector<DataTypes::Contact> newContacts) { contacts = newContacts; }
void setHasTask(bool newValue) { updateValue(hasTask, newValue); }
void setCoalition(unsigned char newValue) { updateValue(coalition, newValue);}
double getFuel() { return fuel; }
vector<DataTypes::Ammo> getAmmo() { return ammo; }
vector<DataTypes::Contact> getTargets() { return contacts; }
bool getHasTask() { return hasTask; }
unsigned int getCoalition() { return coalition; }
/********** Formation data **********/
void setLeaderID(unsigned int newValue) { updateValue(leaderID, newValue); }
void setFormationOffset(Offset formationOffset);
unsigned int getLeaderID() { return leaderID; }
Offset getFormationoffset() { return formationOffset; }
/********** Task data **********/
void setTask(string newValue) { updateValue(task, newValue); }
void setDesiredSpeed(double newValue);
void setDesiredAltitude(double newValue);
void setDesiredSpeedType(string newValue);
void setDesiredAltitudeType(string newValue);
void setActiveDestination(Coords newValue) { updateValue(activeDestination, newValue); }
void setActivePath(list<Coords> newValue);
void setTargetID(unsigned int newValue) { updateValue(targetID, newValue); }
void setTargetPosition(Coords newValue) { updateValue(targetPosition, newValue); }
void setIsTanker(bool newValue);
void setIsAWACS(bool newValue);
virtual void setOnOff(bool newValue) { updateValue(onOff, newValue); };
virtual void setFollowRoads(bool newValue) { updateValue(followRoads, newValue); };
string getTask() { return task; }
virtual double getDesiredSpeed() { return desiredSpeed; };
virtual double getDesiredAltitude() { return desiredAltitude; };
virtual bool getDesiredSpeedType() { return desiredSpeedType; };
virtual bool getDesiredAltitudeType() { return desiredAltitudeType; };
unsigned int getDataPacket(char*& data);
unsigned int getID() { return ID; }
void getData(stringstream& ss, unsigned long long time, bool refresh);
Coords getActiveDestination() { return activeDestination; }
list<Coords> getActivePath() { return activePath; }
unsigned int getTargetID() { return targetID; }
Coords getTargetPosition() { return targetPosition; }
bool getIsTanker() { return isTanker; }
bool getIsAWACS() { return isAWACS; }
bool getOnOff() { return onOff; };
bool getFollowRoads() { return followRoads; };
/********** Options data **********/
void setROE(unsigned char newValue, bool force = false);
void setReactionToThreat(unsigned char newValue, bool force = false);
void setEmissionsCountermeasures(unsigned char newValue, bool force = false);
void setTACAN(Options::TACAN newValue, bool force = false);
void setRadio(Options::Radio newValue, bool force = false);
void setGeneralSettings(Options::GeneralSettings newValue, bool force = false);
void setEPLRS(bool newValue, bool force = false);
unsigned char getROE() { return ROE; }
unsigned char getReactionToThreat() { return reactionToThreat; }
unsigned char getEmissionsCountermeasures() { return emissionsCountermeasures; };
Options::TACAN getTACAN() { return TACAN; }
Options::Radio getRadio() { return radio; }
Options::GeneralSettings getGeneralSettings() { return generalSettings; }
bool getEPLRS() { return EPLRS; }
/********** Control functions **********/
void landAt(Coords loc);
virtual void changeSpeed(string change) {};
virtual void changeAltitude(string change) {};
bool setActiveDestination();
void resetActiveDestination();
virtual void setState(unsigned char newState) { state = newState; };
void resetTask();
void landAt(Coords loc);
bool updateActivePath(bool looping);
void clearActivePath();
void pushActivePathFront(Coords newActivePathFront);
void pushActivePathBack(Coords newActivePathBack);
void popActivePathFront();
void triggerUpdate() { lastUpdateTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); }
unsigned long long getLastUpdateTime() { return lastUpdateTime; };
void goToDestination(string enrouteTask = "nil");
bool isDestinationReached(double threshold);
protected:
unsigned int ID;
map<string, Measure*> measures;
unsigned int taskCheckCounter = 0;
/********** Base data **********/
bool controlled = false;
string name = "undefined";
string unitName = "undefined";
string groupName = "undefined";
bool alive = true;
bool human = false;
unsigned int country = NULL;
/********** Flight data **********/
Coords position = Coords(NULL);
double speed = NULL;
double heading = NULL;
/********** Mission data **********/
unsigned short fuel = 0;
double initialFuel = 0; // Used internally to detect refueling completed
vector<DataTypes::Ammo> ammo;
vector<DataTypes::Contact> contacts;
bool hasTask = false;
unsigned char coalition;
/********** Formation data **********/
unsigned int leaderID = NULL;
Offset formationOffset = Offset(NULL);
/********** Task data **********/
string task = "";
double desiredSpeed = 0;
double desiredAltitude = 0;
bool desiredSpeedType = 1;
bool desiredAltitudeType = 1;
list<Coords> activePath;
Coords activeDestination = Coords(NULL);
unsigned int targetID = NULL;
Coords targetPosition = Coords(NULL);
bool isTanker = false;
bool isAWACS = false;
bool onOff = true;
bool followRoads = false;
/********** Options data **********/
unsigned char ROE = ROE::OPEN_FIRE_WEAPON_FREE;
unsigned char reactionToThreat = ReactionToThreat::EVADE_FIRE;
unsigned char emissionsCountermeasures = EmissionCountermeasure::DEFEND;
Options::TACAN TACAN;
Options::Radio radio;
Options::GeneralSettings generalSettings;
bool EPLRS = false;
/********** Data packet **********/
DataTypes::DataPacket dataPacket;
/********** State machine **********/
unsigned char state = State::NONE;
/********** Other **********/
unsigned long long lastUpdateTime = 0;
Coords oldPosition = Coords(0); // Used to approximate speed
/********** Functions **********/
string getTargetName();
string getLeaderName();
bool isTargetAlive();
bool isLeaderAlive();
virtual void AIloop() = 0;
bool isDestinationReached(double threshold);
bool setActiveDestination();
bool updateActivePath(bool looping);
void goToDestination(string enrouteTask = "nil");
void resetTask();
bool checkTaskFailed();
void resetTaskFailedCounter();
void triggerUpdate(unsigned char datumIndex);
/********** Setters **********/
virtual void setCategory(string newValue) { updateValue(category, newValue, DataIndex::category); }
virtual void setAlive(bool newValue) { updateValue(alive, newValue, DataIndex::alive); }
virtual void setHuman(bool newValue) { updateValue(human, newValue, DataIndex::human); }
virtual void setControlled(bool newValue) { updateValue(controlled, newValue, DataIndex::controlled); }
virtual void setCoalition(unsigned char newValue) { updateValue(coalition, newValue, DataIndex::coalition); }
virtual void setCountry(unsigned int newValue) { updateValue(country, newValue, DataIndex::country); }
virtual void setName(string newValue) { updateValue(name, newValue, DataIndex::name); }
virtual void setUnitName(string newValue) { updateValue(unitName, newValue, DataIndex::unitName); }
virtual void setGroupName(string newValue) { updateValue(groupName, newValue, DataIndex::groupName); }
virtual void setState(unsigned char newValue) { updateValue(state, newValue, DataIndex::state); };
virtual void setTask(string newValue) { updateValue(task, newValue, DataIndex::task); }
virtual void setHasTask(bool newValue) { updateValue(hasTask, newValue, DataIndex::hasTask); }
virtual void setPosition(Coords newValue) { updateValue(position, newValue, DataIndex::position); }
virtual void setSpeed(double newValue) { updateValue(speed, newValue, DataIndex::speed); }
virtual void setHeading(double newValue) { updateValue(heading, newValue, DataIndex::heading); }
virtual void setIsTanker(bool newValue);
virtual void setIsAWACS(bool newValue);
virtual void setOnOff(bool newValue) { updateValue(onOff, newValue, DataIndex::onOff); };
virtual void setFollowRoads(bool newValue) { updateValue(followRoads, newValue, DataIndex::followRoads); };
virtual void setFuel(unsigned short newValue) { updateValue(fuel, newValue, DataIndex::fuel); }
virtual void setDesiredSpeed(double newValue);
virtual void setDesiredSpeedType(string newValue);
virtual void setDesiredAltitude(double newValue);
virtual void setDesiredAltitudeType(string newValue);
virtual void setLeaderID(unsigned int newValue) { updateValue(leaderID, newValue, DataIndex::leaderID); }
virtual void setFormationOffset(Offset formationOffset);
virtual void setTargetID(unsigned int newValue) { updateValue(targetID, newValue, DataIndex::targetID); }
virtual void setTargetPosition(Coords newValue) { updateValue(targetPosition, newValue, DataIndex::targetPosition); }
virtual void setROE(unsigned char newValue, bool force = false);
virtual void setReactionToThreat(unsigned char newValue, bool force = false);
virtual void setEmissionsCountermeasures(unsigned char newValue, bool force = false);
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<DataTypes::Ammo> newAmmo) { ammo = newAmmo; }
virtual void setContacts(vector<DataTypes::Contact> newContacts) { contacts = newContacts; }
virtual void setActivePath(list<Coords> 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 int 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<DataTypes::Ammo> getAmmo() { return ammo; }
virtual vector<DataTypes::Contact> getTargets() { return contacts; }
virtual list<Coords> getActivePath() { return activePath; }
protected:
unsigned int ID;
string category;
bool alive = true;
bool human = false;
bool controlled = false;
unsigned char coalition;
unsigned int country = NULL;
string name = "undefined";
string unitName = "undefined";
string groupName = "undefined";
unsigned char state = State::NONE;
string task = "";
bool hasTask = false;
Coords position = Coords(NULL);
double speed = NULL;
double heading = NULL;
bool isTanker = false;
bool isAWACS = false;
bool onOff = true;
bool followRoads = false;
unsigned short fuel = 0;
double desiredSpeed = 0;
bool desiredSpeedType = 1;
double desiredAltitude = 0;
bool desiredAltitudeType = 1;
unsigned int leaderID = NULL;
Offset formationOffset = Offset(NULL);
unsigned int targetID = NULL;
Coords targetPosition = Coords(NULL);
unsigned char ROE = ROE::OPEN_FIRE_WEAPON_FREE;
unsigned char reactionToThreat = ReactionToThreat::EVADE_FIRE;
unsigned char emissionsCountermeasures = EmissionCountermeasure::DEFEND;
DataTypes::TACAN TACAN;
DataTypes::Radio radio;
DataTypes::GeneralSettings generalSettings;
vector<DataTypes::Ammo> ammo;
vector<DataTypes::Contact> contacts;
list<Coords> activePath;
/********** Other **********/
unsigned int taskCheckCounter = 0;
Coords activeDestination = Coords(NULL);
double initialFuel = 0;
Coords oldPosition = Coords(0);
map<unsigned char, unsigned long long> updateTimeMap;
/********** Private methods **********/
virtual void AIloop() = 0;
/********** Template methods **********/
template <typename T>
void updateValue(T& value, T& newValue)
void updateValue(T& value, T& newValue, unsigned char datumIndex)
{
if (newValue != value)
{
triggerUpdate();
triggerUpdate(datumIndex);
*(&value) = newValue;
}
}
template <typename T>
void appendNumeric(stringstream& ss, const unsigned char& datumIndex, T& datumValue) {
ss.write((const char*)&datumIndex, sizeof(unsigned char));
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 <typename T>
void appendVector(stringstream& ss, const unsigned char& datumIndex, vector<T>& datumValue) {
const unsigned short size = datumValue.size();
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));
ss.write((const char*)&datumValue, size * sizeof(T));
}
template <typename T>
void appendList(stringstream& ss, const unsigned char& datumIndex, list<T>& datumValue) {
const unsigned short size = datumValue.size();
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));
for (auto el: datumValue)
ss.write((const char*)&el, sizeof(T));
}
};

View File

@@ -5,9 +5,6 @@ class Weapon : public Unit
{
public:
Weapon(json::value json, unsigned int ID);
virtual string getCategory() = 0;
protected:
/* Weapons are not controllable and have no AIloop */
virtual void AIloop() {};
@@ -17,14 +14,10 @@ class Missile : public Weapon
{
public:
Missile(json::value json, unsigned int ID);
virtual string getCategory() { return "Missile"; };
};
class Bomb : public Weapon
{
public:
Bomb(json::value json, unsigned int ID);
virtual string getCategory() { return "Bomb"; };
};