Added back attack and movement functions, missionData is now part of units

This commit is contained in:
Pax1601
2023-01-17 22:17:20 +01:00
parent 280799b27a
commit b77f271183
18 changed files with 388 additions and 225 deletions

View File

@@ -13,7 +13,8 @@ public:
Unit(json::value json, int ID);
~Unit();
void update(json::value json);
void updateExportData(json::value json);
void updateMissionData(json::value json);
json::value json();
void setPath(list<Coords> path);
@@ -65,7 +66,6 @@ protected:
double heading = NULL;
double speed = NULL;
json::value flags = json::value::null();
Coords oldPosition = Coords(0); // Used to approximate speed
int targetID = NULL;
bool holding = false;
bool looping = false;
@@ -77,9 +77,13 @@ protected:
vector<Unit*> wingmen;
double targetSpeed = 0;
double targetAltitude = 0;
double fuel = 0;
json::value ammo;
json::value targets;
list<Coords> activePath;
Coords activeDestination = Coords(0);
Coords oldPosition = Coords(0); // Used to approximate speed
virtual void AIloop();

View File

@@ -11,8 +11,8 @@ public:
~UnitsFactory();
Unit* getUnit(int ID);
void getMissionDB(lua_State* L);
void update(lua_State* L);
void updateExportData(lua_State* L);
void updateMissionData(json::value missionData);
void updateAnswer(json::value& answer);
private:

View File

@@ -25,7 +25,7 @@ Unit::~Unit()
}
void Unit::update(json::value json)
void Unit::updateExportData(json::value json)
{
/* Lock for thread safety */
lock_guard<mutex> guard(mutexLock);
@@ -77,6 +77,16 @@ void Unit::update(json::value json)
}
}
void Unit::updateMissionData(json::value json)
{
if (json.has_number_field(L"fuel"))
fuel = json[L"fuel"].as_number().to_int32();
if (json.has_object_field(L"ammo"))
ammo = json[L"ammo"];
if (json.has_object_field(L"targets"))
targets = json[L"targets"];
}
void Unit::setPath(list<Coords> path)
{
activePath = path;
@@ -187,6 +197,9 @@ json::value Unit::json()
json[L"leader"] = leader;
json[L"wingman"] = wingman;
json[L"formation"] = json::value::string(formation);
json[L"fuel"] = fuel;
json[L"ammo"] = ammo;
json[L"targets"] = targets;
int i = 0;
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)

View File

@@ -11,7 +11,6 @@ auto before = std::chrono::system_clock::now();
UnitsFactory* unitsFactory = nullptr;
Server* server = nullptr;
Scheduler* scheduler = nullptr;
json::value missionData;
/* Called when DCS simulation stops. All singleton instances are deleted. */
extern "C" DllExport int coreDeinit(lua_State* L)
@@ -48,7 +47,7 @@ extern "C" DllExport int coreFrame(lua_State* L)
{
if (unitsFactory != nullptr)
{
unitsFactory->update(L);
unitsFactory->updateExportData(L);
}
// TODO allow for different intervals
@@ -65,7 +64,10 @@ extern "C" DllExport int coreMissionData(lua_State * L)
{
lua_getglobal(L, "Olympus");
lua_getfield(L, -1, "missionData");
missionData = luaTableToJSON(L, -1);
json::value missionData = luaTableToJSON(L, -1);
if (missionData.has_object_field(L"unitsData"))
unitsFactory->updateMissionData(missionData[L"unitsData"]);
return(0);
}

View File

@@ -9,7 +9,6 @@
extern UnitsFactory* unitsFactory;
extern Scheduler* scheduler;
extern json::value missionData;
void handle_eptr(std::exception_ptr eptr)
{
@@ -58,7 +57,6 @@ void Server::handle_get(http_request request)
std::exception_ptr eptr;
try {
unitsFactory->updateAnswer(answer);
answer[L"missionData"] = missionData;
response.set_body(answer);
}
catch (...) {

View File

@@ -26,11 +26,11 @@ Unit* UnitsFactory::getUnit(int ID)
}
}
void UnitsFactory::update(lua_State* L)
void UnitsFactory::updateExportData(lua_State* L)
{
map<int, json::value> unitJSONs = getAllUnits(L);
/* Update all units, create them if needed */
/* Update all units, create them if needed TODO: move code to get constructor in dedicated function */
for (auto const& p : unitJSONs)
{
int ID = p.first;
@@ -74,7 +74,7 @@ void UnitsFactory::update(lua_State* L)
/* Update the unit if present*/
if (units.count(ID) != 0)
{
units[ID]->update(p.second);
units[ID]->updateExportData(p.second);
}
}
@@ -88,6 +88,21 @@ void UnitsFactory::update(lua_State* L)
}
}
void UnitsFactory::updateMissionData(json::value missionData)
{
/* Update all units */
for (auto const& p : units)
{
int ID = p.first;
log(to_string(ID));
if (missionData.has_field(to_wstring(ID)))
{
log("mix");
p.second->updateMissionData(missionData[to_wstring(ID)]);
}
}
}
void UnitsFactory::updateAnswer(json::value& answer)
{
// TODO THREAT SAFEY!
@@ -100,3 +115,4 @@ void UnitsFactory::updateAnswer(json::value& answer)
answer[L"units"] = unitsJson;
}

View File

@@ -4,7 +4,7 @@
void DllExport stackUpdate(lua_State* L, int& stackDepth, int initialStack = 0);
void DllExport stackPop(lua_State* L, int popDepth = 1);
void DllExport stackClean(lua_State* L, int stackDepth);
json::value DllExport luaTableToJSON(lua_State* L, int index);
json::value DllExport luaTableToJSON(lua_State* L, int index, bool logKeys = false);
#define STACK_UPDATE stackUpdate(L, stackDepth, initialStack);
#define STACK_INIT int stackDepth = 0; int initialStack = 0; stackUpdate(L, initialStack);

View File

@@ -18,7 +18,7 @@ void stackClean(lua_State* L, int stackDepth)
lua_pop(L, stackDepth);
}
json::value luaTableToJSON(lua_State* L, int index)
json::value luaTableToJSON(lua_State* L, int index, bool logKeys)
{
auto json = json::value::object();
@@ -32,9 +32,13 @@ json::value luaTableToJSON(lua_State* L, int index)
{
lua_pushvalue(L, -2);
const char* key = lua_tostring(L, -1);
if (logKeys)
{
log(key);
}
if (lua_istable(L, -2))
{
json[to_wstring(key)] = luaTableToJSON(L, -2);
json[to_wstring(key)] = luaTableToJSON(L, -2, logKeys);
}
else if (lua_isnumber(L, -2))
{
@@ -44,7 +48,7 @@ json::value luaTableToJSON(lua_State* L, int index)
{
json[to_wstring(key)] = json::value::boolean(lua_toboolean(L, -2));
}
else if (lua_isstring(L, -2)) // Keep last, only checks if it can be stringified (not if it actually IS a string)
else if (lua_isstring(L, -2)) // Keep last, lua_isstring only checks if it can be stringified (not if it actually IS a string)
{
json[to_wstring(key)] = json::value::string(to_wstring(lua_tostring(L, -2)));
}