diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj index e3063bf5..b6c5614d 100644 --- a/src/core/core.vcxproj +++ b/src/core/core.vcxproj @@ -36,6 +36,7 @@ + @@ -51,6 +52,7 @@ + diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters index a50b8e11..00e7a412 100644 --- a/src/core/core.vcxproj.filters +++ b/src/core/core.vcxproj.filters @@ -45,6 +45,9 @@ Header Files + + Header Files + @@ -86,5 +89,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/src/core/include/Unit.h b/src/core/include/Unit.h index 371cce1e..e400cc84 100644 --- a/src/core/include/Unit.h +++ b/src/core/include/Unit.h @@ -3,6 +3,7 @@ #include "utils.h" #include "dcstools.h" #include "luatools.h" +#include "measure.h" namespace State { @@ -28,88 +29,86 @@ public: Unit(json::value json, int ID); ~Unit(); + /********** Public methods **********/ int getID() { return ID; } - void updateExportData(json::value json); void updateMissionData(json::value json); - json::value json(bool fullRefresh); + json::value getData(int time); /********** Base data **********/ - void setAI(bool newAI) { AI = newAI; } + void setAI(bool newAI) { AI = newAI; addMeasure(L"AI", json::value(newAI)); } + void setName(wstring newName) { name = newName; addMeasure(L"name", json::value(newName));} + void setUnitName(wstring newUnitName) { name = newUnitName; addMeasure(L"unitName", json::value(newUnitName));} + void setGroupName(wstring newGroupName) { name = newGroupName; addMeasure(L"groupName", json::value(newGroupName));} + void setAlive(bool newAlive) { alive = newAlive; addMeasure(L"alive", json::value(newAlive));} + void setType(json::value newType) { type = newType; addMeasure(L"type", newType);} + void setCountry(int newCountry) { country = newCountry; addMeasure(L"country", json::value(newCountry));} bool getAI() { return AI; } - void setName(wstring newName){name = newName}; wstring getName() { return name; } - void setUnitName(wstring newName){name = newName}; wstring getUnitName() { return unitName; } - void setGroupName(wstring newName){name = newName}; wstring getGroupName() { return groupName; } - void setAlive(bool newAlive) { alive = newAlive; } bool getAlive() { return alive; } - void setType(json::value newType) { type = newType; } json::value getType() { return type; } - void setCountry(int newCountry) { country = newCountry; } int getCountry() { return country; } /********** Flight data **********/ - void setLatitude(double newLatitude) {latitude = newLatitude;} + void setLatitude(double newLatitude) {latitude = newLatitude; addMeasure(L"latitude", json::value(newLatitude));} + void setLongitude(double newLongitude) {longitude = newLongitude; addMeasure(L"longitude", json::value(newLongitude));} + void setAltitude(double newAltitude) {altitude = newAltitude; addMeasure(L"altitude", json::value(newAltitude));} + void setHeading(double newHeading) {heading = newHeading; addMeasure(L"heading", json::value(newHeading));} + void setSpeed(double newSpeed) {speed = newSpeed; addMeasure(L"speed", json::value(newSpeed));} double getLatitude() { return latitude; } - void setLongitude(double newLatitude) {longitude = newLongitude;} double getLongitude() { return longitude; } - void setAltitude(double newAltitude) {altitude = newAltitude;} double getAltitude() { return altitude; } - void setHeading(double newHeading) {heading = newHeading;} double getHeading() { return heading; } - void setSpeed(double newSpeed) {speed = newSpeed;} - double getSpeed() {return speed; } + double getSpeed() { return speed; } /********** Mission data **********/ - void setFuel(double newFuel) { fuel = newFuel;} - double getFuel() {return fuel;} - void setAmmo(json::value newAmmo) { ammo = newAmmo; } - json::value getAmmo { return ammo; } - void setTargets(json::value newTargets) {targets = newTargets;} + void setFuel(double newFuel) { fuel = newFuel; addMeasure(L"fuel", json::value(newFuel));} + void setAmmo(json::value newAmmo) { ammo = newAmmo; addMeasure(L"ammo", json::value(newAmmo));} + void setTargets(json::value newTargets) {targets = newTargets; addMeasure(L"targets", json::value(newTargets));} + void setHasTask(bool newHasTask) { hasTask = newHasTask; addMeasure(L"hasTask", json::value(newHasTask));} + void setCoalitionID(int newCoalitionID) { coalitionID = newCoalitionID; addMeasure(L"coalition", json::value(newCoalitionID)); } // TODO fix + void setFlags(json::value newFlags) { flags = newFlags; addMeasure(L"flags", json::value(newFlags));} + double getFuel() { return fuel; } + json::value getAmmo() { return ammo; } json::value getTargets() { return targets; } - void setHasTask(bool newHasTask) { hasTask = newHasTask;} bool getHasTask() { return hasTask; } - void setCoalitionID(int newCoalitionID) { coalitionID = newCoalitionID; } int getCoalitionID() { return coalitionID; } - void setFlags(json::value newFlags) { flags = newFlags; } json::value getFlags() { return flags; } /********** Formation data **********/ void setIsLeader(bool newIsLeader); - bool getIsLeader() { return isLeader; } void setIsWingman(bool newIsWingman); - bool getIsWingman() { return isWingman; } - void setLeader(Unit *newLeader) { leader = newLeader; } - Unit* getLeader() {return leader;} - void setWingmen(vector newWingmen) { wingmen = newWingmen; } - vector getWingmen() {return wingmen;} - void setFormation(wstring newFormation) { formation = newFormation; } - wstring getFormation() { return formation; } + void setLeader(Unit *newLeader) { leader = newLeader; addMeasure(L"leader", json::value("")); } // TODO fix + void setWingmen(vector newWingmen) { wingmen = newWingmen; addMeasure(L"wingmen", json::value("")); } // TODO fix + void setFormation(wstring newFormation) { formation = newFormation; addMeasure(L"formation", json::value(formation));} void setFormationOffset(Offset formationOffset); - Offset getFormationoffset() {return formationOffset;} - + bool getIsLeader() { return isLeader; } + bool getIsWingman() { return isWingman; } + Unit* getLeader() { return leader; } + vector getWingmen() { return wingmen; } + wstring getFormation() { return formation; } + Offset getFormationoffset() { return formationOffset; } + /********** Task data **********/ - void setCurrentTask(wstring newCurrentTask) { currentTask = newCurrentTask; } - wstring getCurrentTask() { return currentTask; } - virtual void setTargetSpeed(double newSpeed) { targetSpeed = newSpeed; } - virtual double getTargetSpeed() { return targetSpeed; }; - virtual void setTargetAltitude(double newAltitude) { targetAltitude = newAltitude; } - virtual double getTargetAltitude() { return targetAltitude; }; - void setActiveDestination(Coords newActiveDestination) { activeDestination = newActiveDestination; } - Coords getActiveDestination() { return activeDestination; } + void setCurrentTask(wstring newCurrentTask) { currentTask = newCurrentTask;addMeasure(L"currentTask", json::value(newCurrentTask)); } + virtual void setTargetSpeed(double newTargetSpeed) { targetSpeed = newTargetSpeed; addMeasure(L"targetSpeed", json::value(newTargetSpeed));} + 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); - list getPath() {return activePath} - void setActiveDestination(Coords newActiveDestination) { activeDestination = newActiveDestination; } + void setTargetID(int newTargetID) { targetID = newTargetID; addMeasure(L"targetID", json::value(newTargetID));} + wstring getCurrentTask() { return currentTask; } + virtual double getTargetSpeed() { return targetSpeed; }; + virtual double getTargetAltitude() { return targetAltitude; }; Coords getActiveDestination() { return activeDestination; } - void setTarget(int targetID); - wstring getTarget(); + list getActivePath() { return activePath; } + int getTargetID() { return targetID; } /********** Options data **********/ void setROE(wstring newROE); - wstring getROE() {return ROE;} void setReactionToThreat(wstring newReactionToThreat); + wstring getROE() { return ROE; } wstring getReactionToThreat() {return reactionToThreat;} /********** Control functions **********/ @@ -120,15 +119,11 @@ public: virtual void setState(int newState) { state = newState; }; void resetTask(); - /********** Other functions **********/ - void setHasNewData(bool newHasNewData) { hasNewData = newHasNewData; } - virtual wstring getCategory() { return L"No category"; }; - bool isTargetAlive(); - bool getHasNewData() { return hasNewData; } - protected: int ID; + map measures; + /********** Base data **********/ bool AI = false; wstring name = L"undefined"; @@ -178,9 +173,11 @@ protected: /********** Other **********/ Coords oldPosition = Coords(0); // Used to approximate speed - bool hasNewData = false; - int newDataCounter = 0; /********** Functions **********/ + virtual wstring getCategory() { return L"No category"; }; + wstring getTargetName(); + bool isTargetAlive(); virtual void AIloop() = 0; + void addMeasure(wstring key, json::value value); }; diff --git a/src/core/include/measure.h b/src/core/include/measure.h new file mode 100644 index 00000000..3c43c2af --- /dev/null +++ b/src/core/include/measure.h @@ -0,0 +1,19 @@ +#pragma once +#include "framework.h" + +class Measure +{ +public: + Measure(json::value value, int time): value(value), time(time) {}; + + void setValue(json::value newValue) { value = newValue; } + void setTime(int newTime) { time = newTime; } + json::value getValue() { return value; } + int getTime() { return time; } + +private: + json::value value; + int time; + +}; + diff --git a/src/core/include/unitsmanager.h b/src/core/include/unitsmanager.h index b73c791f..98e7d828 100644 --- a/src/core/include/unitsmanager.h +++ b/src/core/include/unitsmanager.h @@ -11,9 +11,10 @@ public: ~UnitsManager(); Unit* getUnit(int ID); + void expireMeasures(); void updateExportData(lua_State* L); void updateMissionData(json::value missionData); - void updateAnswer(json::value& answer, bool fullRefresh); + void getData(json::value& answer, int time); void deleteUnit(int ID); private: diff --git a/src/core/src/Scheduler.cpp b/src/core/src/Scheduler.cpp index 8f5caf82..8e80f9bf 100644 --- a/src/core/src/Scheduler.cpp +++ b/src/core/src/Scheduler.cpp @@ -139,7 +139,7 @@ void Scheduler::handleRequest(wstring key, json::value value) return; log(L"Unit " + unitName + L" attacking unit " + targetName); - unit->setTarget(targetID); + unit->setTargetID(targetID); unit->setState(State::ATTACK); } else if (key.compare(L"stopAttack") == 0) diff --git a/src/core/src/Unit.cpp b/src/core/src/Unit.cpp index fd43c544..31a8d940 100644 --- a/src/core/src/Unit.cpp +++ b/src/core/src/Unit.cpp @@ -6,6 +6,9 @@ #include "defines.h" #include "unitsManager.h" +#include +using namespace std::chrono; + #include using namespace GeographicLib; @@ -16,7 +19,6 @@ Unit::Unit(json::value json, int ID) : ID(ID) { log("Creating unit with ID: " + to_string(ID)); - newDataCounter = 1.0 / UPDATE_TIME_INTERVAL > 0? 1.0 / UPDATE_TIME_INTERVAL: 1; // Mark the unit has hasNewData for 1 second } Unit::~Unit() @@ -24,12 +26,23 @@ Unit::~Unit() } +void Unit::addMeasure(wstring key, json::value value) +{ + milliseconds ms = duration_cast(system_clock::now().time_since_epoch()); + if (measures.find(key) == measures.end()) + measures[key] = new Measure(value, ms.count()); + else + { + if (measures[key]->getValue() != value) + { + measures[key]->setValue(value); + measures[key]->setTime(ms.count()); + } + } +} + void Unit::updateExportData(json::value json) { - if (newDataCounter > 0) - newDataCounter--; - setHasNewData(newDataCounter); - /* Compute speed (loGetWorldObjects does not provide speed, we compute it for better performance instead of relying on many lua calls) */ if (oldPosition != NULL) { @@ -40,32 +53,32 @@ void Unit::updateExportData(json::value json) oldPosition = Coords(latitude, longitude, altitude); if (json.has_string_field(L"Name")) - name = json[L"Name"].as_string(); + setName(json[L"Name"].as_string()); if (json.has_string_field(L"UnitName")) - unitName = json[L"UnitName"].as_string(); + setUnitName(json[L"UnitName"].as_string()); if (json.has_string_field(L"GroupName")) - groupName = json[L"GroupName"].as_string(); + setGroupName(json[L"GroupName"].as_string()); if (json.has_object_field(L"Type")) - type = json[L"Type"]; + setType(json[L"Type"]); if (json.has_number_field(L"Country")) - country = json[L"Country"].as_number().to_int32(); + setCountry(json[L"Country"].as_number().to_int32()); if (json.has_number_field(L"CoalitionID")) - coalitionID = json[L"CoalitionID"].as_number().to_int32(); + setCoalitionID(json[L"CoalitionID"].as_number().to_int32()); if (json.has_object_field(L"LatLongAlt")) { - latitude = json[L"LatLongAlt"][L"Lat"].as_number().to_double(); - longitude = json[L"LatLongAlt"][L"Long"].as_number().to_double(); - altitude = json[L"LatLongAlt"][L"Alt"].as_number().to_double(); + setLatitude(json[L"LatLongAlt"][L"Lat"].as_number().to_double()); + setLongitude(json[L"LatLongAlt"][L"Long"].as_number().to_double()); + setAltitude(json[L"LatLongAlt"][L"Alt"].as_number().to_double()); } if (json.has_number_field(L"Heading")) - heading = json[L"Heading"].as_number().to_double(); + setHeading(json[L"Heading"].as_number().to_double()); if (json.has_object_field(L"Flags")) - flags = json[L"Flags"]; + setFlags(json[L"Flags"]); /* All units which contain the name "Olympus" are automatically under AI control */ /* TODO: I don't really like using this method */ if (unitName.find(L"Olympus") != wstring::npos) - AI = true; + setAI(true); /* If the unit is alive and it is not a human, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */ if (AI && alive && flags[L"Human"].as_bool() == false) @@ -74,90 +87,141 @@ void Unit::updateExportData(json::value json) void Unit::updateMissionData(json::value json) { - newDataCounter = 1.0 / UPDATE_TIME_INTERVAL > 0 ? 1.0 / UPDATE_TIME_INTERVAL : 1; // Mark the unit has hasNewData for 1 second if (json.has_number_field(L"fuel")) - fuel = int(json[L"fuel"].as_number().to_double() * 100); + setFuel(int(json[L"fuel"].as_number().to_double() * 100)); if (json.has_object_field(L"ammo")) - ammo = json[L"ammo"]; + setAmmo(json[L"ammo"]); if (json.has_object_field(L"targets")) - targets = json[L"targets"]; + setTargets(json[L"targets"]); if (json.has_boolean_field(L"hasTask")) - hasTask = json[L"hasTask"].as_bool(); + setHasTask(json[L"hasTask"].as_bool()); } +// +//json::value Unit::getRefreshData() +//{ +// auto json = json::value::object(); +// +// /********** Base data **********/ +// json[L"baseData"] = json::value::object(); +// json[L"baseData"][L"AI"] = AI; +// json[L"baseData"][L"name"] = json::value::string(name); +// json[L"baseData"][L"unitName"] = json::value::string(unitName); +// json[L"baseData"][L"groupName"] = json::value::string(groupName); +// json[L"baseData"][L"alive"] = alive; +// json[L"baseData"][L"category"] = json::value::string(getCategory()); +// +// /********** Flight data **********/ +// json[L"flightData"] = json::value::object(); +// json[L"flightData"][L"latitude"] = latitude; +// json[L"flightData"][L"longitude"] = longitude; +// json[L"flightData"][L"altitude"] = altitude; +// json[L"flightData"][L"speed"] = speed; +// json[L"flightData"][L"heading"] = heading; +// +// /********** Mission data **********/ +// json[L"missionData"] = json::value::object(); +// json[L"missionData"][L"fuel"] = fuel; +// json[L"missionData"][L"ammo"] = ammo; +// json[L"missionData"][L"targets"] = targets; +// json[L"missionData"][L"hasTask"] = hasTask; +// if (coalitionID == 0) +// json[L"missionData"][L"coalition"] = json::value::string(L"neutral"); +// else if (coalitionID == 1) +// json[L"missionData"][L"coalition"] = json::value::string(L"red"); +// else +// json[L"missionData"][L"coalition"] = json::value::string(L"blue"); +// json[L"missionData"][L"flags"] = flags; +// +// /********** Formation data **********/ +// json[L"formationData"] = json::value::object(); +// json[L"formationData"][L"isLeader"] = isLeader; +// json[L"formationData"][L"isWingman"] = isWingman; +// json[L"formationData"][L"formation"] = json::value::string(formation); +// int i = 0; +// for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++) +// json[L"formationData"][L"wingmenIDs"][i++] = (*itr)->getID(); +// +// if (leader != nullptr) +// json[L"formationData"][L"leaderID"] = leader->getID(); +// +// /********** Task data **********/ +// json[L"taskData"] = json::value::object(); +// json[L"taskData"][L"currentTask"] = json::value::string(getCurrentTask()); +// json[L"taskData"][L"targetSpeed"] = getTargetSpeed(); +// json[L"taskData"][L"targetAltitude"] = getTargetAltitude(); +// /* Send the active path as a json object */ +// auto path = json::value::object(); +// if (activePath.size() > 0) { +// int count = 1; +// for (auto& destination : activePath) +// { +// auto json = json::value::object(); +// json[L"lat"] = destination.lat; +// json[L"lng"] = destination.lng; +// json[L"alt"] = destination.alt; +// path[to_wstring(count++)] = json; +// } +// } +// json[L"taskData"][L"activePath"] = path; +// +// /********** Options data **********/ +// json[L"optionsData"] = json::value::object(); +// json[L"optionsData"][L"ROE"] = json::value::string(ROE); +// json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat); +// return json; +//} -json::value Unit::json(bool fullRefresh) + +json::value Unit::getData(int time) { auto json = json::value::object(); /********** Base data **********/ json[L"baseData"] = json::value::object(); - json[L"baseData"][L"AI"] = AI; - json[L"baseData"][L"name"] = json::value::string(name); - json[L"baseData"][L"unitName"] = json::value::string(unitName); - json[L"baseData"][L"groupName"] = json::value::string(groupName); - json[L"baseData"][L"alive"] = alive; - json[L"baseData"][L"category"] = json::value::string(getCategory()); + for (auto key : { L"AI", L"name", L"unitName", L"groupName", L"alive", L"category"}) + { + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"baseData"][key] = measures[key]->getValue(); + } /********** Flight data **********/ json[L"flightData"] = json::value::object(); - json[L"flightData"][L"latitude"] = latitude; - json[L"flightData"][L"longitude"] = longitude; - json[L"flightData"][L"altitude"] = altitude; - json[L"flightData"][L"speed"] = speed; - json[L"flightData"][L"heading"] = heading; - - if (fullRefresh || getHasNewData()) + for (auto key : { L"latitude", L"longitude", L"altitude", L"speed", L"heading"}) { - /********** Mission data **********/ - json[L"missionData"] = json::value::object(); - json[L"missionData"][L"fuel"] = fuel; - json[L"missionData"][L"ammo"] = ammo; - json[L"missionData"][L"targets"] = targets; - json[L"missionData"][L"hasTask"] = hasTask; - if (coalitionID == 0) - json[L"missionData"][L"coalition"] = json::value::string(L"neutral"); - else if (coalitionID == 1) - json[L"missionData"][L"coalition"] = json::value::string(L"red"); - else - json[L"missionData"][L"coalition"] = json::value::string(L"blue"); - json[L"missionData"][L"flags"] = flags; + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"flightData"][key] = measures[key]->getValue(); + } - /********** Formation data **********/ - json[L"formationData"] = json::value::object(); - json[L"formationData"][L"isLeader"] = isLeader; - json[L"formationData"][L"isWingman"] = isWingman; - json[L"formationData"][L"formation"] = json::value::string(formation); - int i = 0; - for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++) - json[L"formationData"][L"wingmenIDs"][i++] = (*itr)->getID(); + /********** Mission data **********/ + json[L"missionData"] = json::value::object(); + for (auto key : { L"fuel", L"ammo", L"targets", L"hasTask", L"coalition", L"flags"}) + { + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"missionData"][key] = measures[key]->getValue(); + } - if (leader != nullptr) - json[L"formationData"][L"leaderID"] = leader->getID(); + /********** Formation data **********/ + json[L"formationData"] = json::value::object(); + for (auto key : { L"isLeader", L"isWingman", L"formation", L"wingmenIDs", L"leaderID" }) + { + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"missionData"][key] = measures[key]->getValue(); + } - /********** Task data **********/ - json[L"taskData"] = json::value::object(); - json[L"taskData"][L"currentTask"] = json::value::string(getCurrentTask()); - json[L"taskData"][L"targetSpeed"] = getTargetSpeed(); - json[L"taskData"][L"targetAltitude"] = getTargetAltitude(); - /* Send the active path as a json object */ - auto path = json::value::object(); - if (activePath.size() > 0) { - int count = 1; - for (auto& destination : activePath) - { - auto json = json::value::object(); - json[L"lat"] = destination.lat; - json[L"lng"] = destination.lng; - json[L"alt"] = destination.alt; - path[to_wstring(count++)] = json; - } - } - json[L"taskData"][L"activePath"] = path; + /********** Task data **********/ + json[L"taskData"] = json::value::object(); + for (auto key : { L"currentTask", L"targetSpeed", L"targetAltitude", L"activePath" }) + { + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"taskData"][key] = measures[key]->getValue(); + } - /********** Options data **********/ - json[L"optionsData"] = json::value::object(); - json[L"optionsData"][L"ROE"] = json::value::string(ROE); - json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat); + /********** Options data **********/ + json[L"optionsData"] = json::value::object(); + for (auto key : { L"ROE", L"reactionToThreat" }) + { + if (measures.find(key) != measures.end() && measures[key]->getTime() > time) + json[L"optionsData"][key] = measures[key]->getValue(); } return json; @@ -170,14 +234,11 @@ void Unit::setActivePath(list path) activePath = path; resetActiveDestination(); } + + addMeasure(L"activePath", json::value("")); // TODO fix } -void Unit::setTarget(int newTargetID) -{ - targetID = newTargetID; -} - -wstring Unit::getTarget() +wstring Unit::getTargetName() { if (isTargetAlive()) { @@ -221,6 +282,7 @@ void Unit::setIsLeader(bool newIsLeader) { wingman->setLeader(nullptr); } } + addMeasure(L"isLeader", json::value(newIsLeader)); } void Unit::setIsWingman(bool newIsWingman) @@ -230,6 +292,8 @@ void Unit::setIsWingman(bool newIsWingman) setState(State::WINGMAN); else setState(State::IDLE); + + addMeasure(L"isWingman", json::value(isWingman)); } void Unit::setFormationOffset(Offset newFormationOffset) @@ -255,6 +319,7 @@ void Unit::setROE(wstring newROE) { return; Command* command = dynamic_cast(new SetOption(ID, SetCommandType::ROE, ROEEnum)); scheduler->appendCommand(command); + addMeasure(L"ROE", json::value(newROE)); } void Unit::setReactionToThreat(wstring newReactionToThreat) { @@ -274,6 +339,7 @@ void Unit::setReactionToThreat(wstring newReactionToThreat) { return; Command* command = dynamic_cast(new SetOption(ID, SetCommandType::REACTION_ON_THREAT, reactionToThreatEnum)); scheduler->appendCommand(command); + addMeasure(L"reactionToThreat", json::value(newReactionToThreat)); } void Unit::landAt(Coords loc) { diff --git a/src/core/src/aircraft.cpp b/src/core/src/aircraft.cpp index ae4f9dac..35854aad 100644 --- a/src/core/src/aircraft.cpp +++ b/src/core/src/aircraft.cpp @@ -25,12 +25,12 @@ void Aircraft::changeSpeed(wstring change) setState(State::IDLE); } else if (change.compare(L"slow") == 0) - targetSpeed -= 25 / 1.94384; + setTargetSpeed(getTargetSpeed() - 25 / 1.94384); else if (change.compare(L"fast") == 0) - targetSpeed += 25 / 1.94384; + setTargetSpeed(getTargetSpeed() + 25 / 1.94384); - if (targetSpeed < 50 / 1.94384) - targetSpeed = 50 / 1.94384; + if (getTargetSpeed() < 50 / 1.94384) + setTargetSpeed(50 / 1.94384); goToDestination(); /* Send the command to reach the destination */ } @@ -39,30 +39,30 @@ void Aircraft::changeAltitude(wstring change) { if (change.compare(L"descend") == 0) { - if (targetAltitude > 5000) - targetAltitude -= 2500 / 3.28084; - else if (targetAltitude > 0) - targetAltitude -= 500 / 3.28084; + if (getTargetAltitude() > 5000) + setTargetAltitude(getTargetAltitude() - 2500 / 3.28084); + else if (getTargetAltitude() > 0) + setTargetAltitude(getTargetAltitude() - 500 / 3.28084); } else if (change.compare(L"climb") == 0) { - if (targetAltitude > 5000) - targetAltitude += 2500 / 3.28084; - else if (targetAltitude >= 0) - targetAltitude += 500 / 3.28084; + if (getTargetAltitude() > 5000) + setTargetAltitude(getTargetAltitude() + 2500 / 3.28084); + else if (getTargetAltitude() >= 0) + setTargetAltitude(getTargetAltitude() + 500 / 3.28084); } - if (targetAltitude < 0) - targetAltitude = 0; + if (getTargetAltitude() < 0) + setTargetAltitude(0); goToDestination(); /* Send the command to reach the destination */ } void Aircraft::setTargetSpeed(double newTargetSpeed) { - targetSpeed = newTargetSpeed; + setTargetSpeed(newTargetSpeed); goToDestination(); } void Aircraft::setTargetAltitude(double newTargetAltitude) { - targetAltitude = newTargetAltitude; + setTargetAltitude(newTargetAltitude); goToDestination(); } \ No newline at end of file diff --git a/src/core/src/airunit.cpp b/src/core/src/airunit.cpp index 376b7a6d..44c549fb 100644 --- a/src/core/src/airunit.cpp +++ b/src/core/src/airunit.cpp @@ -30,7 +30,7 @@ void AirUnit::setState(int newState) break; } case State::ATTACK: { - setTarget(NULL); + setTargetID(NULL); break; } case State::FOLLOW: { @@ -280,7 +280,7 @@ void AirUnit::AIloop() << "targetID = " << targetID << "," << "}"; wstring enrouteTask = enrouteTaskSS.str(); - currentTask = L"Attacking " + getTarget(); + currentTask = L"Attacking " + getTargetName(); if (activeDestination == NULL || !hasTask) { diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index 1b95c108..e02ecf43 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -66,7 +66,10 @@ extern "C" DllExport int coreFrame(lua_State* L) if (duration.count() > UPDATE_TIME_INTERVAL) { if (unitsManager != nullptr) + { + unitsManager->expireMeasures(); unitsManager->updateExportData(L); + } before = std::chrono::system_clock::now(); } diff --git a/src/core/src/measure.cpp b/src/core/src/measure.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/core/src/server.cpp b/src/core/src/server.cpp index 68a64e0f..9aded60b 100644 --- a/src/core/src/server.cpp +++ b/src/core/src/server.cpp @@ -76,13 +76,10 @@ void Server::handle_get(http_request request) if (path.size() > 0) { - if (path[0] == UNITS_URI && path.size() > 1) + if (path[0] == UNITS_URI) { - if (path[1] == UPDATE_URI) - unitsManager->updateAnswer(answer, false); - else if (path[1] == REFRESH_URI) - unitsManager->updateAnswer(answer, true); - + wstring fragment = uri::decode(request.relative_uri().fragment()) + unitsManager->getData(answer); } else if (path[0] == LOGS_URI) { diff --git a/src/core/src/unitsmanager.cpp b/src/core/src/unitsmanager.cpp index 101eb523..ce7d933f 100644 --- a/src/core/src/unitsmanager.cpp +++ b/src/core/src/unitsmanager.cpp @@ -32,6 +32,16 @@ Unit* UnitsManager::getUnit(int ID) } } + +void UnitsManager::expireMeasures() +{ + /* Decrement the time to live of all measures and remove stale measures */ + for (auto const& p : units) + { + p.second->expireMeasures(); + } +} + void UnitsManager::updateExportData(lua_State* L) { map unitJSONs = getAllUnits(L); @@ -92,12 +102,12 @@ void UnitsManager::updateMissionData(json::value missionData) } } -void UnitsManager::updateAnswer(json::value& answer, bool fullRefresh) +void UnitsManager::getData(json::value& answer, int time) { auto unitsJson = json::value::object(); for (auto const& p : units) { - unitsJson[to_wstring(p.first)] = p.second->json(fullRefresh); + unitsJson[to_wstring(p.first)] = p.second->getData(time); } answer[L"units"] = unitsJson; }