diff --git a/src/core/include/scheduler.h b/src/core/include/scheduler.h index a33dca3a..02f7b1c4 100644 --- a/src/core/include/scheduler.h +++ b/src/core/include/scheduler.h @@ -37,8 +37,8 @@ public: private: list commands; list executedCommandsHashes; - unsigned int load; - double frameRate; + unsigned int load = 0; + double frameRate = 0; bool restrictSpawns = false; bool restrictToCoalition = false; diff --git a/src/core/src/scheduler.cpp b/src/core/src/scheduler.cpp index 8fba5691..66bcd17f 100644 --- a/src/core/src/scheduler.cpp +++ b/src/core/src/scheduler.cpp @@ -7,12 +7,9 @@ extern UnitsManager* unitsManager; -Scheduler::Scheduler(lua_State* L) : - load(0) +Scheduler::Scheduler(lua_State* L) { LogInfo(L, "Scheduler constructor called successfully"); - - } Scheduler::~Scheduler() @@ -20,6 +17,7 @@ Scheduler::~Scheduler() } +/* Appends a */ void Scheduler::appendCommand(Command* newCommand) { for (auto command : commands) { @@ -143,6 +141,7 @@ void Scheduler::handleRequest(string key, json::value value, string username, js log("Received request with ID: " + key); log(L"Incoming command raw value: " + value.serialize()); + /************************/ if (key.compare("setPath") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -150,7 +149,6 @@ void Scheduler::handleRequest(string key, json::value value, string username, js Unit* unit = unitsManager->getGroupLeader(ID); if (unit != nullptr) { - string unitName = unit->getUnitName(); json::value path = value[L"path"]; list newPath; for (unsigned int i = 0; i < path.as_array().size(); i++) @@ -164,19 +162,22 @@ void Scheduler::handleRequest(string key, json::value value, string username, js unit->setActivePath(newPath); unit->setState(State::REACH_DESTINATION); - log(username + " updated destination path for unit " + unitName, true); + log(username + " updated destination path for unit " + unit->getUnitName() + "(" + unit->getName() + ")", true); } } + /************************/ else if (key.compare("smoke") == 0) { string color = to_string(value[L"color"]); double lat = value[L"location"][L"lat"].as_double(); double lng = value[L"location"][L"lng"].as_double(); - log(username + " added a " + color + " smoke at (" + to_string(lat) + ", " + to_string(lng) + ")", true); + Coords loc; loc.lat = lat; loc.lng = lng; command = dynamic_cast(new Smoke(color, loc)); + log(username + " added a " + color + " smoke at (" + to_string(lat) + ", " + to_string(lng) + ")", true); } - else if (key.compare("spawnAircrafts") == 0) + /************************/ + else if (key.compare("spawnAircrafts") == 0 || key.compare("spawnHelicopters") == 0) { bool immediate = value[L"immediate"].as_bool(); string coalition = to_string(value[L"coalition"]); @@ -200,35 +201,13 @@ void Scheduler::handleRequest(string key, json::value value, string username, js log(username + " spawned a " + coalition + " " + unitType, true); } - command = dynamic_cast(new SpawnAircrafts(coalition, spawnOptions, airbaseName, country, immediate)); + if (key.compare("spawnAircrafts") == 0) + command = dynamic_cast(new SpawnAircrafts(coalition, spawnOptions, airbaseName, country, immediate)); + else + command = dynamic_cast(new SpawnHelicopters(coalition, spawnOptions, airbaseName, country, immediate)); } - else if (key.compare("spawnHelicopters") == 0) - { - bool immediate = value[L"immediate"].as_bool(); - string coalition = to_string(value[L"coalition"]); - string airbaseName = to_string(value[L"airbaseName"]); - string country = to_string(value[L"country"]); - - int spawnPoints = value[L"spawnPoints"].as_number().to_int32(); - if (!checkSpawnPoints(spawnPoints, coalition)) return; - - vector spawnOptions; - for (auto unit : value[L"units"].as_array()) { - string unitType = to_string(unit[L"unitType"]); - double lat = unit[L"location"][L"lat"].as_double(); - double lng = unit[L"location"][L"lng"].as_double(); - double alt = unit[L"altitude"].as_double(); - Coords location; location.lat = lat; location.lng = lng; location.alt = alt; - string loadout = to_string(unit[L"loadout"]); - string liveryID = to_string(unit[L"liveryID"]); - - spawnOptions.push_back({ unitType, location, loadout, liveryID }); - log(username + " spawned a " + coalition + " " + unitType, true); - } - - command = dynamic_cast(new SpawnHelicopters(coalition, spawnOptions, airbaseName, country, immediate)); - } - else if (key.compare("spawnGroundUnits") == 0) + /************************/ + else if (key.compare("spawnGroundUnits") == 0 || key.compare("spawnNavyUnits") == 0) { bool immediate = value[L"immediate"].as_bool(); string coalition = to_string(value[L"coalition"]); @@ -249,31 +228,12 @@ void Scheduler::handleRequest(string key, json::value value, string username, js log(username + " spawned a " + coalition + " " + unitType, true); } - command = dynamic_cast(new SpawnGroundUnits(coalition, spawnOptions, country, immediate)); - } - else if (key.compare("spawnNavyUnits") == 0) - { - bool immediate = value[L"immediate"].as_bool(); - string coalition = to_string(value[L"coalition"]); - string country = to_string(value[L"country"]); - - int spawnPoints = value[L"spawnPoints"].as_number().to_int32(); - if (!checkSpawnPoints(spawnPoints, coalition)) return; - - vector spawnOptions; - for (auto unit : value[L"units"].as_array()) { - string unitType = to_string(unit[L"unitType"]); - double lat = unit[L"location"][L"lat"].as_double(); - double lng = unit[L"location"][L"lng"].as_double(); - Coords location; location.lat = lat; location.lng = lng; - string liveryID = to_string(unit[L"liveryID"]); - - spawnOptions.push_back({ unitType, location, "", liveryID}); - log(username + " spawned a " + coalition + " " + unitType, true); - } - - command = dynamic_cast(new SpawnNavyUnits(coalition, spawnOptions, country, immediate)); + if (key.compare("spawnGroundUnits") == 0) + command = dynamic_cast(new SpawnGroundUnits(coalition, spawnOptions, country, immediate)); + else + command = dynamic_cast(new SpawnNavyUnits(coalition, spawnOptions, country, immediate)); } + /************************/ else if (key.compare("attackUnit") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -283,23 +243,13 @@ void Scheduler::handleRequest(string key, json::value value, string username, js Unit* unit = unitsManager->getGroupLeader(ID); Unit* target = unitsManager->getUnit(targetID); - string unitName; - string targetName; - - if (unit != nullptr) - unitName = unit->getUnitName(); - else - return; - - if (target != nullptr) - targetName = target->getUnitName(); - else - return; - - log(username + " tasked unit " + unitName + " to attack unit " + targetName, true); - unit->setTargetID(targetID); - unit->setState(State::ATTACK); + if (unit != nullptr && target != nullptr) { + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to attack unit " + target->getUnitName() + "(" + target->getName() + ")", true); + unit->setTargetID(targetID); + unit->setState(State::ATTACK); + } } + /************************/ else if (key.compare("followUnit") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -312,72 +262,80 @@ void Scheduler::handleRequest(string key, json::value value, string username, js Unit* unit = unitsManager->getGroupLeader(ID); Unit* leader = unitsManager->getUnit(leaderID); - string unitName; - string leaderName; - - if (unit != nullptr) - unitName = unit->getUnitName(); - else - return; - - if (leader != nullptr) - leaderName = leader->getUnitName(); - else - return; - - log(username + " tasked unit " + unitName + " to follow unit " + leaderName, true); - unit->setFormationOffset(Offset(offsetX, offsetY, offsetZ)); - unit->setLeaderID(leaderID); - unit->setState(State::FOLLOW); + if (unit != nullptr && leader != nullptr) { + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to follow unit " + leader->getUnitName() + "(" + leader->getName() + ")", true); + unit->setFormationOffset(Offset(offsetX, offsetY, offsetZ)); + unit->setLeaderID(leaderID); + unit->setState(State::FOLLOW); + } } + /************************/ else if (key.compare("changeSpeed") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->changeSpeed(to_string(value[L"change"])); + log(username + " changed " + unit->getUnitName() + "(" + unit->getName() + ") speed: " + to_string(value[L"change"]), true); + } } + /************************/ else if (key.compare("changeAltitude") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->changeAltitude(to_string(value[L"change"])); + log(username + " changed " + unit->getUnitName() + "(" + unit->getName() + ") altitude: " + to_string(value[L"change"]), true); + } } + /************************/ else if (key.compare("setSpeed") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->setDesiredSpeed(value[L"speed"].as_double()); + log(username + " set " + unit->getUnitName() + "(" + unit->getName() + ") speed: " + to_string(value[L"speed"].as_double()), true); + } } + /************************/ else if (key.compare("setSpeedType") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->setDesiredSpeedType(to_string(value[L"speedType"])); + log(username + " set " + unit->getUnitName() + "(" + unit->getName() + ") speed type: " + to_string(value[L"speedType"]), true); + } } + /************************/ else if (key.compare("setAltitude") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->setDesiredAltitude(value[L"altitude"].as_double()); + log(username + " set " + unit->getUnitName() + "(" + unit->getName() + ") altitude: " + to_string(value[L"altitude"].as_double()), true); + } } + /************************/ else if (key.compare("setAltitudeType") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - if (unit != nullptr) + if (unit != nullptr) { unit->setDesiredAltitudeType(to_string(value[L"altitudeType"])); + log(username + " set " + unit->getUnitName() + "(" + unit->getName() + ") altitude type: " + to_string(value[L"altitudeType"]), true); + } } + /************************/ else if (key.compare("cloneUnits") == 0) { vector cloneOptions; @@ -395,61 +353,80 @@ void Scheduler::handleRequest(string key, json::value value, string username, js command = dynamic_cast(new Clone(cloneOptions, deleteOriginal)); } + /************************/ else if (key.compare("setROE") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unsigned char ROE = value[L"ROE"].as_number().to_uint32(); - unit->setROE(ROE); - log(username + " set unit " + unit->getName() + " ROE to " + to_string(ROE), true); + if (unit != nullptr) { + unsigned char ROE = value[L"ROE"].as_number().to_uint32(); + unit->setROE(ROE); + log(username + " set unit " + unit->getUnitName() + "(" + unit->getName() + ") ROE to " + to_string(ROE), true); + } } + /************************/ else if (key.compare("setReactionToThreat") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unsigned char reactionToThreat = value[L"reactionToThreat"].as_number().to_uint32(); - unit->setReactionToThreat(reactionToThreat); - log(username + " set unit " + unit->getName() + " reaction to threat to " + to_string(reactionToThreat), true); + if (unit != nullptr) { + unsigned char reactionToThreat = value[L"reactionToThreat"].as_number().to_uint32(); + unit->setReactionToThreat(reactionToThreat); + log(username + " set unit " + unit->getUnitName() + "(" + unit->getName() + ") reaction to threat to " + to_string(reactionToThreat), true); + } } + /************************/ else if (key.compare("setEmissionsCountermeasures") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unsigned char emissionsCountermeasures = value[L"emissionsCountermeasures"].as_number().to_uint32(); - unit->setEmissionsCountermeasures(emissionsCountermeasures); - log(username + " set unit " + unit->getName() + " emissions and countermeasures to " + to_string(emissionsCountermeasures), true); + if (unit != nullptr) { + unsigned char emissionsCountermeasures = value[L"emissionsCountermeasures"].as_number().to_uint32(); + unit->setEmissionsCountermeasures(emissionsCountermeasures); + log(username + " set unit " + unit->getUnitName() + "(" + unit->getName() + ") emissions and countermeasures to " + to_string(emissionsCountermeasures), true); + } } + /************************/ else if (key.compare("landAt") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - double lat = value[L"location"][L"lat"].as_double(); - double lng = value[L"location"][L"lng"].as_double(); - Coords loc; loc.lat = lat; loc.lng = lng; - unit->landAt(loc); - log(username + " tasked unit " + unit->getName() + " to land", true); + if (unit != nullptr) { + double lat = value[L"location"][L"lat"].as_double(); + double lng = value[L"location"][L"lng"].as_double(); + Coords loc; loc.lat = lat; loc.lng = lng; + unit->landAt(loc); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to land", true); + } } + /************************/ else if (key.compare("deleteUnit") == 0) { unsigned int ID = value[L"ID"].as_integer(); bool explosion = value[L"explosion"].as_bool(); bool immediate = value[L"immediate"].as_bool(); - unitsManager->deleteUnit(ID, explosion, immediate); Unit* unit = unitsManager->getUnit(ID); - log(username + " deleted unit " + unit->getName(), true); + if (unit != nullptr) { + unitsManager->deleteUnit(ID, explosion, immediate); + log(username + " deleted unit " + unit->getUnitName() + "(" + unit->getName() + ")", true); + } } + /************************/ else if (key.compare("refuel") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::REFUEL); - log(username + " tasked unit " + unit->getName() + " to refuel", true); + if (unit != nullptr) { + unit->setState(State::REFUEL); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to refuel", true); + } } + /************************/ else if (key.compare("setAdvancedOptions") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -489,24 +466,35 @@ void Scheduler::handleRequest(string key, json::value value, string username, js }); unit->resetActiveDestination(); + + log(username + " updated unit " + unit->getUnitName() + "(" + unit->getName() + ") advancedOptions", true); } } + /************************/ else if (key.compare("setFollowRoads") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); bool followRoads = value[L"followRoads"].as_bool(); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setFollowRoads(followRoads); + if (unit != nullptr) { + unit->setFollowRoads(followRoads); + log(username + " set unit " + unit->getUnitName() + "(" + unit->getName() + ") followRoads to: " + (followRoads ? "true" : "false"), true); + } } + /************************/ else if (key.compare("setOnOff") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); bool onOff = value[L"onOff"].as_bool(); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setOnOff(onOff); + if (unit != nullptr) { + unit->setOnOff(onOff); + log(username + " set unit " + unit->getUnitName() + "(" + unit->getName() + ") onOff to: " + (onOff? "true": "false"), true); + } } + /************************/ else if (key.compare("explosion") == 0) { unsigned int intensity = value[L"intensity"].as_integer(); @@ -516,6 +504,7 @@ void Scheduler::handleRequest(string key, json::value value, string username, js Coords loc; loc.lat = lat; loc.lng = lng; command = dynamic_cast(new Explosion(intensity, loc)); } + /************************/ else if (key.compare("bombPoint") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -524,10 +513,13 @@ void Scheduler::handleRequest(string key, json::value value, string username, js double lng = value[L"location"][L"lng"].as_double(); Coords loc; loc.lat = lat; loc.lng = lng; Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::BOMB_POINT); - unit->setTargetPosition(loc); - log(username + " tasked unit " + unit->getName() + " to bomb a point", true); + if (unit != nullptr) { + unit->setState(State::BOMB_POINT); + unit->setTargetPosition(loc); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to bomb a point", true); + } } + /************************/ else if (key.compare("carpetBomb") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -536,10 +528,14 @@ void Scheduler::handleRequest(string key, json::value value, string username, js double lng = value[L"location"][L"lng"].as_double(); Coords loc; loc.lat = lat; loc.lng = lng; Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::CARPET_BOMB); - unit->setTargetPosition(loc); - log(username + " tasked unit " + unit->getName() + " to perform carpet bombing", true); + if (unit != nullptr) { + unit->setState(State::CARPET_BOMB); + unit->setTargetPosition(loc); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to perform carpet bombing", true); + } } + /************************/ + /* TODO: this command does not appear to be working in DCS and has been disabled */ else if (key.compare("bombBuilding") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -548,9 +544,12 @@ void Scheduler::handleRequest(string key, json::value value, string username, js double lng = value[L"location"][L"lng"].as_double(); Coords loc; loc.lat = lat; loc.lng = lng; Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::BOMB_BUILDING); - unit->setTargetPosition(loc); + if (unit != nullptr) { + unit->setState(State::BOMB_BUILDING); + unit->setTargetPosition(loc); + } } + /************************/ else if (key.compare("fireAtArea") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -559,10 +558,13 @@ void Scheduler::handleRequest(string key, json::value value, string username, js double lng = value[L"location"][L"lng"].as_double(); Coords loc; loc.lat = lat; loc.lng = lng; Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::FIRE_AT_AREA); - unit->setTargetPosition(loc); - log(username + " tasked unit " + unit->getName() + " to fire at area", true); + if (unit != nullptr) { + unit->setState(State::FIRE_AT_AREA); + unit->setTargetPosition(loc); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to fire at area", true); + } } + /************************/ else if (key.compare("simulateFireFight") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -572,34 +574,45 @@ void Scheduler::handleRequest(string key, json::value value, string username, js double alt = value[L"altitude"].as_double(); Coords loc; loc.lat = lat; loc.lng = lng; loc.alt = alt; Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::SIMULATE_FIRE_FIGHT); - unit->setTargetPosition(loc); - log(username + " tasked unit " + unit->getName() + " to simulate a fire fight", true); + if (unit != nullptr) { + unit->setState(State::SIMULATE_FIRE_FIGHT); + unit->setTargetPosition(loc); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to simulate a fire fight", true); + } } + /************************/ else if (key.compare("scenicAAA") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::SCENIC_AAA); - log(username + " tasked unit " + unit->getName() + " to enter scenic AAA state", true); + if (unit != nullptr) { + unit->setState(State::SCENIC_AAA); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to enter scenic AAA state", true); + } } + /************************/ else if (key.compare("missOnPurpose") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setState(State::MISS_ON_PURPOSE); - log(username + " tasked unit " + unit->getName() + " to enter Miss On Purpose state", true); + if (unit != nullptr) { + unit->setState(State::MISS_ON_PURPOSE); + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to enter Miss On Purpose state", true); + } } + /************************/ else if (key.compare("setOperateAs") == 0) { unsigned int ID = value[L"ID"].as_integer(); unitsManager->acquireControl(ID); unsigned char operateAs = value[L"operateAs"].as_number().to_uint32(); Unit* unit = unitsManager->getGroupLeader(ID); - unit->setOperateAs(operateAs); + if (unit != nullptr) + unit->setOperateAs(operateAs); } + /************************/ else if (key.compare("landAtPoint") == 0) { unsigned int ID = value[L"ID"].as_integer(); @@ -609,21 +622,26 @@ void Scheduler::handleRequest(string key, json::value value, string username, js Coords loc; loc.lat = lat; loc.lng = lng; Unit* unit = unitsManager->getGroupLeader(ID); - list newPath; - newPath.push_back(loc); - unit->setActivePath(newPath); - unit->setState(State::LAND_AT_POINT); - - log(username + " tasked unit " + unit->getName() + " to land at point", true); + if (unit != nullptr) { + list newPath; + newPath.push_back(loc); + unit->setActivePath(newPath); + unit->setState(State::LAND_AT_POINT); + + log(username + " tasked unit " + unit->getUnitName() + "(" + unit->getName() + ") to land at point", true); + } } + /************************/ else if (key.compare("setCommandModeOptions") == 0) { setCommandModeOptions(value); log(username + " updated the Command Mode Options", true); } + /************************/ else if (key.compare("reloadDatabases") == 0) { unitsManager->loadDatabases(); } + /************************/ else { log("Unknown command: " + key);