Better unitmarker.ts integration. Added scheduler load divider. Rationalized units.ts and added interfaces (to be completed)

This commit is contained in:
Pax1601
2023-02-26 20:19:31 +01:00
parent 236523ee2b
commit c5a1b00cfd
22 changed files with 1773 additions and 2281 deletions

View File

@@ -17,7 +17,7 @@ namespace SetCommandType {
REACTION_ON_THREAT = 1,
RADAR_USING = 3,
FLARE_USING = 4,
Formation = 5,
FORMATION = 5,
RTB_ON_BINGO = 6,
SILENCE = 7,
RTB_ON_OUT_OF_AMMO = 10,
@@ -56,9 +56,6 @@ namespace ReactionToThreat {
};
}
/* Base command class */
class Command
{
@@ -66,6 +63,7 @@ public:
int getPriority() { return priority; }
int getType() { return type; }
virtual wstring getString(lua_State* L) = 0;
virtual int getLoad() = 0;
protected:
int priority = CommandPriority::LOW;
@@ -88,6 +86,7 @@ public:
type = CommandType::MOVE;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 5; }
private:
const int ID;
@@ -110,6 +109,7 @@ public:
type = CommandType::SMOKE;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 5; }
private:
const wstring color;
@@ -129,6 +129,7 @@ public:
type = CommandType::SPAWN_GROUND;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 100; }
private:
const wstring coalition;
@@ -150,8 +151,8 @@ public:
priority = CommandPriority::LOW;
type = CommandType::SPAWN_AIR;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 100; }
private:
const wstring coalition;
@@ -173,6 +174,7 @@ public:
type = CommandType::CLONE;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 100; }
private:
const int ID;
@@ -190,6 +192,7 @@ public:
type = CommandType::CLONE;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 20; }
private:
const int ID;
@@ -207,6 +210,7 @@ public:
type = CommandType::FOLLOW;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 10; }
private:
const int ID;
@@ -224,6 +228,7 @@ public:
type = CommandType::RESET_TASK;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 10; }
private:
const int ID;
@@ -241,6 +246,7 @@ public:
type = CommandType::RESET_TASK;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 10; }
private:
const int ID;
@@ -260,6 +266,7 @@ public:
type = CommandType::RESET_TASK;
};
virtual wstring getString(lua_State* L);
virtual int getLoad() { return 10; }
private:
const int ID;

View File

@@ -15,5 +15,6 @@ public:
private:
list<Command*> commands;
int load;
};

View File

@@ -7,9 +7,10 @@
extern UnitsManager* unitsManager;
Scheduler::Scheduler(lua_State* L)
Scheduler::Scheduler(lua_State* L):
load(0)
{
LogInfo(L, "Units Factory constructor called successfully");
LogInfo(L, "Scheduler constructor called successfully");
}
Scheduler::~Scheduler()
@@ -24,6 +25,13 @@ void Scheduler::appendCommand(Command* command)
void Scheduler::execute(lua_State* L)
{
/* Decrease the active computation load. New commands can be sent only if the load has reached 0.
This is needed to avoid server lag. */
if (load > 0) {
load--;
return;
}
int priority = CommandPriority::HIGH;
while (priority >= CommandPriority::LOW)
{
@@ -33,9 +41,8 @@ void Scheduler::execute(lua_State* L)
{
wstring commandString = L"Olympus.protectedCall(" + command->getString(L) + L")";
if (dostring_in(L, "server", to_string(commandString)))
{
log(L"Error executing command " + commandString);
}
load = command->getLoad();
commands.remove(command);
return;
}

View File

@@ -61,15 +61,11 @@ 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)
{
AI = 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)
{
AIloop();
}
}
void Unit::updateMissionData(json::value json)
@@ -88,44 +84,56 @@ json::value Unit::json()
{
auto json = json::value::object();
json[L"alive"] = alive;
/********** Base data **********/
json[L"AI"] = AI;
json[L"name"] = json::value::string(name);
json[L"unitName"] = json::value::string(unitName);
json[L"groupName"] = json::value::string(groupName);
json[L"type"] = type;
json[L"country"] = country;
json[L"coalitionID"] = coalitionID;
json[L"latitude"] = latitude;
json[L"longitude"] = longitude;
json[L"altitude"] = altitude;
json[L"speed"] = speed;
json[L"heading"] = heading;
json[L"flags"] = flags;
json[L"alive"] = alive;
json[L"category"] = json::value::string(getCategory());
json[L"currentTask"] = json::value::string(getCurrentTask());
json[L"isLeader"] = isLeader;
json[L"isWingman"] = isWingman;
json[L"formation"] = json::value::string(formation);
json[L"fuel"] = fuel;
json[L"ammo"] = ammo;
json[L"targets"] = targets;
json[L"targetSpeed"] = getTargetSpeed();
json[L"targetAltitude"] = getTargetAltitude();
json[L"hasTask"] = hasTask;
json[L"ROE"] = json::value::string(ROE);
json[L"reactionToThreat"] = json::value::string(reactionToThreat);
/********** 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"wingmenIDs"][i++] = (*itr)->getID();
json[L"formationData"][L"wingmenIDs"][i++] = (*itr)->getID();
if (leader != nullptr)
json[L"leaderID"] = leader->getID();
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) {
auto path = json::value::object();
int count = 1;
for (auto& destination : activePath)
{
@@ -135,8 +143,13 @@ json::value Unit::json()
json[L"alt"] = destination.alt;
path[to_wstring(count++)] = json;
}
json[L"activePath"] = path;
}
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;
}
@@ -178,7 +191,6 @@ bool Unit::isTargetAlive()
return false;
}
/* This function reset the activation so that the AI lopp will call again the MoveCommand. This is useful to change speed and altitude, for example */
void Unit::resetActiveDestination()
{
activeDestination = Coords(NULL);

View File

@@ -60,21 +60,17 @@ extern "C" DllExport int coreFrame(lua_State* L)
const std::chrono::duration<double> duration = std::chrono::system_clock::now() - before;
// TODO make intervals editable
/* TODO make intervals editable */
if (duration.count() > UPDATE_TIME_INTERVAL)
{
if (unitsManager != nullptr)
{
unitsManager->updateExportData(L);
}
// TODO allow for different intervals
if (scheduler != nullptr)
{
scheduler->execute(L);
}
before = std::chrono::system_clock::now();
}
if (scheduler != nullptr)
scheduler->execute(L);
return(0);
}