More core work, completed time tagged measures

Split back unit info panel
This commit is contained in:
Pax1601
2023-03-12 17:27:59 +01:00
parent 6e203c09f9
commit 955fd7541a
28 changed files with 311 additions and 12818 deletions

View File

@@ -33,13 +33,13 @@ public:
int getID() { return ID; }
void updateExportData(json::value json);
void updateMissionData(json::value json);
json::value getData(int time);
json::value getData(long long time);
/********** Base data **********/
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 setUnitName(wstring newUnitName) { unitName = newUnitName; addMeasure(L"unitName", json::value(newUnitName));}
void setGroupName(wstring newGroupName) { groupName = 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));}
@@ -68,20 +68,21 @@ public:
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 setCoalitionID(int newCoalitionID);
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; }
bool getHasTask() { return hasTask; }
int getCoalitionID() { return coalitionID; }
wstring getCoalition() { return coalition; }
int getCoalitionID();
json::value getFlags() { return flags; }
/********** Formation data **********/
void setIsLeader(bool newIsLeader);
void setIsWingman(bool newIsWingman);
void setLeader(Unit *newLeader) { leader = newLeader; addMeasure(L"leader", json::value("")); } // TODO fix
void setWingmen(vector<Unit*> newWingmen) { wingmen = newWingmen; addMeasure(L"wingmen", json::value("")); } // TODO fix
void setLeader(Unit* newLeader);
void setWingmen(vector<Unit*> newWingmen);
void setFormation(wstring newFormation) { formation = newFormation; addMeasure(L"formation", json::value(formation));}
void setFormationOffset(Offset formationOffset);
bool getIsLeader() { return isLeader; }
@@ -145,7 +146,7 @@ protected:
json::value ammo = json::value::null();
json::value targets = json::value::null();
bool hasTask = false;
int coalitionID = NULL; // TODO: save coalition directly
wstring coalition = L"";
json::value flags = json::value::null();
/********** Formation data **********/

View File

@@ -4,16 +4,16 @@
class Measure
{
public:
Measure(json::value value, int time): value(value), time(time) {};
Measure(json::value value, long long time): value(value), time(time) {};
void setValue(json::value newValue) { value = newValue; }
void setTime(int newTime) { time = newTime; }
void setTime(long long newTime) { time = newTime; }
json::value getValue() { return value; }
int getTime() { return time; }
long long getTime() { return time; }
private:
json::value value;
int time;
long long time;
};

View File

@@ -13,7 +13,7 @@ public:
Unit* getUnit(int ID);
void updateExportData(lua_State* L);
void updateMissionData(json::value missionData);
void getData(json::value& answer, int time);
void getData(json::value& answer, long long time);
void deleteUnit(int ID);
private:

View File

@@ -48,7 +48,7 @@ void Unit::updateExportData(json::value json)
{
double dist = 0;
Geodesic::WGS84().Inverse(latitude, longitude, oldPosition.lat, oldPosition.lng, dist);
speed = speed * 0.95 + (dist / UPDATE_TIME_INTERVAL) * 0.05;
setSpeed(getSpeed() * 0.95 + (dist / UPDATE_TIME_INTERVAL) * 0.05);
}
oldPosition = Coords(latitude, longitude, altitude);
@@ -77,11 +77,10 @@ void Unit::updateExportData(json::value json)
/* 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)
setAI(true);
setAI(getUnitName().find(L"Olympus") != wstring::npos);
/* 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)
if (getAI() && getAlive() && getFlags()[L"Human"].as_bool() == false)
AIloop();
}
@@ -96,83 +95,8 @@ void Unit::updateMissionData(json::value json)
if (json.has_boolean_field(L"hasTask"))
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::getData(int time)
json::value Unit::getData(long long time)
{
auto json = json::value::object();
@@ -227,17 +151,66 @@ json::value Unit::getData(int time)
return json;
}
void Unit::setActivePath(list<Coords> path)
void Unit::setActivePath(list<Coords> newPath)
{
if (state != State::WINGMAN && state != State::FOLLOW)
{
activePath = path;
activePath = newPath;
resetActiveDestination();
}
addMeasure(L"activePath", json::value("")); // TODO fix
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;
}
}
addMeasure(L"activePath", path);
}
void Unit::setCoalitionID(int newCoalitionID)
{
if (newCoalitionID == 0)
coalition = L"neutral";
else if (newCoalitionID == 1)
coalition = L"red";
else
coalition = L"blue";
addMeasure(L"coalition", json::value(coalition));
}
int Unit::getCoalitionID()
{
if (coalition == L"neutral")
return 0;
else if (coalition == L"red")
return 1;
else
return 2;
}
void Unit::setLeader(Unit* newLeader)
{
leader = newLeader;
if (leader != nullptr)
addMeasure(L"leaderID", json::value(leader->getID()));
}
void Unit::setWingmen(vector<Unit*> newWingmen) {
wingmen = newWingmen;
auto wingmenIDs = json::value::object();
int i = 0;
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)
wingmenIDs[i++] = (*itr)->getID();
addMeasure(L"wingmen", wingmenIDs);
}
wstring Unit::getTargetName()
{
if (isTargetAlive())

View File

@@ -16,6 +16,7 @@ extern UnitsManager* unitsManager;
Aircraft::Aircraft(json::value json, int ID) : AirUnit(json, ID)
{
log("New Aircraft created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};
void Aircraft::changeSpeed(wstring change)
@@ -58,11 +59,13 @@ void Aircraft::changeAltitude(wstring change)
}
void Aircraft::setTargetSpeed(double newTargetSpeed) {
setTargetSpeed(newTargetSpeed);
targetSpeed = newTargetSpeed;
addMeasure(L"targetSpeed", json::value(targetSpeed));
goToDestination();
}
void Aircraft::setTargetAltitude(double newTargetAltitude) {
setTargetAltitude(newTargetAltitude);
targetAltitude = newTargetAltitude;
addMeasure(L"targetAltitude", json::value(targetAltitude));
goToDestination();
}

View File

@@ -203,6 +203,7 @@ void AirUnit::taskWingmen()
void AirUnit::AIloop()
{
log(L"AILoop");
/* State machine */
switch (state) {
case State::IDLE: {

View File

@@ -94,7 +94,7 @@ extern "C" DllExport int coreMissionData(lua_State * L)
unitsManager->updateMissionData(missionData[L"unitsData"]);
if (missionData.has_object_field(L"airbases"))
airbasesData = missionData[L"airbases"];
if (missionData.has_object_field(L"bullseye"))
if (missionData.has_object_field(L"bullseyes"))
bullseyesData = missionData[L"bullseyes"];
return(0);

View File

@@ -16,6 +16,7 @@ extern UnitsManager* unitsManager;
GroundUnit::GroundUnit(json::value json, int ID) : Unit(json, ID)
{
log("New Ground Unit created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};
void GroundUnit::AIloop()

View File

@@ -16,6 +16,7 @@ extern UnitsManager* unitsManager;
Helicopter::Helicopter(json::value json, int ID) : AirUnit(json, ID)
{
log("New Helicopter created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};
void Helicopter::changeSpeed(wstring change)
@@ -60,10 +61,12 @@ void Helicopter::changeAltitude(wstring change)
void Helicopter::setTargetSpeed(double newTargetSpeed) {
targetSpeed = newTargetSpeed;
addMeasure(L"targetSpeed", json::value(targetSpeed));
goToDestination();
}
void Helicopter::setTargetAltitude(double newTargetAltitude) {
targetAltitude = newTargetAltitude;
addMeasure(L"targetAltitude", json::value(targetAltitude));
goToDestination();
}

View File

@@ -16,6 +16,7 @@ extern UnitsManager* unitsManager;
NavyUnit::NavyUnit(json::value json, int ID) : Unit(json, ID)
{
log("New Navy Unit created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};
void NavyUnit::AIloop()

View File

@@ -7,6 +7,9 @@
#include <exception>
#include <stdexcept>
#include <chrono>
using namespace std::chrono;
extern UnitsManager* unitsManager;
extern Scheduler* scheduler;
extern json::value airbasesData;
@@ -78,9 +81,18 @@ void Server::handle_get(http_request request)
{
if (path[0] == UNITS_URI)
{
wstring fragment = uri::decode(request.relative_uri().fragment());
log(fragment);
//unitsManager->getData(answer);
map<utility::string_t, utility::string_t> query = request.relative_uri().split_query(request.relative_uri().query());
long long time = 0;
if (query.find(L"time") != query.end())
{
try {
time = stoll((*(query.find(L"time"))).second);
}
catch (const std::exception& e) {
time = 0;
}
}
unitsManager->getData(answer, time);
}
else if (path[0] == LOGS_URI)
{
@@ -93,6 +105,8 @@ void Server::handle_get(http_request request)
else if (path[0] == BULLSEYE_URI)
answer[L"bullseyes"] = bullseyesData;
milliseconds ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
answer[L"time"] = json::value::string(to_wstring(ms.count()));
answer[L"sessionHash"] = json::value::string(to_wstring(sessionHash));
}

View File

@@ -92,7 +92,7 @@ void UnitsManager::updateMissionData(json::value missionData)
}
}
void UnitsManager::getData(json::value& answer, int time)
void UnitsManager::getData(json::value& answer, long long time)
{
auto unitsJson = json::value::object();
for (auto const& p : units)

View File

@@ -22,10 +22,12 @@ Weapon::Weapon(json::value json, int ID) : Unit(json, ID)
Missile::Missile(json::value json, int ID) : Weapon(json, ID)
{
log("New Missile created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};
/* Bomb */
Bomb::Bomb(json::value json, int ID) : Weapon(json, ID)
{
log("New Bomb created with ID: " + to_string(ID));
addMeasure(L"category", json::value(getCategory()));
};