Merge pull request #663 from Pax1601/task-optimization

Task optimization
This commit is contained in:
Pax1601
2023-12-05 10:07:09 +01:00
committed by GitHub
18 changed files with 117 additions and 99 deletions

View File

@@ -914,7 +914,7 @@ function Olympus.setTask(groupName, taskOptions)
end
end
-- Reset the dask of a group
-- Reset the task of a group
function Olympus.resetTask(groupName)
Olympus.debug("Olympus.resetTask " .. groupName, 2)
local group = Group.getByName(groupName)

View File

@@ -1,6 +1,8 @@
#pragma once
#include "airunit.h"
#define AIRCRAFT_DEST_DIST_THR 2000 // Meters
class Aircraft : public AirUnit
{
public:
@@ -11,6 +13,8 @@ public:
virtual void changeSpeed(string change);
virtual void changeAltitude(string change);
virtual double getDestinationReachedThreshold() { return AIRCRAFT_DEST_DIST_THR; }
protected:
static json::value database;
};

View File

@@ -17,6 +17,7 @@ public:
virtual void changeSpeed(string change) = 0;
virtual void changeAltitude(string change) = 0;
virtual double getDestinationReachedThreshold() { return AIR_DEST_DIST_THR; }
protected:
virtual void AIloop();

View File

@@ -124,10 +124,10 @@ public:
taskOptions(taskOptions),
category(category)
{
priority = CommandPriority::HIGH;
priority = CommandPriority::MEDIUM;
};
virtual string getString();
virtual unsigned int getLoad() { return 2; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -196,7 +196,7 @@ public:
priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW;
};
virtual string getString();
virtual unsigned int getLoad() { return immediate ? 1 : 30; }
virtual unsigned int getLoad() { return immediate ? 1 : 60; }
private:
const string coalition;
@@ -220,7 +220,7 @@ public:
priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW;
};
virtual string getString();
virtual unsigned int getLoad() { return immediate ? 1 : 30; }
virtual unsigned int getLoad() { return immediate ? 1 : 45; }
private:
const string coalition;
@@ -245,7 +245,7 @@ public:
priority = immediate ? CommandPriority::IMMEDIATE : CommandPriority::LOW;
};
virtual string getString();
virtual unsigned int getLoad() { return immediate ? 1 : 30; }
virtual unsigned int getLoad() { return immediate ? 1 : 45; }
private:
const string coalition;
@@ -289,7 +289,7 @@ public:
immediate = immediate;
};
virtual string getString();
virtual unsigned int getLoad() { return immediate? 1: 5; }
virtual unsigned int getLoad() { return immediate? 1: 30; }
private:
const unsigned int ID;
@@ -310,7 +310,7 @@ public:
priority = CommandPriority::MEDIUM;
};
virtual string getString();
virtual unsigned int getLoad() { return 1; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -328,7 +328,7 @@ public:
priority = CommandPriority::HIGH;
};
virtual string getString();
virtual unsigned int getLoad() { return 1; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -346,7 +346,7 @@ public:
priority = CommandPriority::HIGH;
};
virtual string getString();
virtual unsigned int getLoad() { return 1; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -379,7 +379,7 @@ public:
priority = CommandPriority::HIGH;
};
virtual string getString();
virtual unsigned int getLoad() { return 1; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -401,7 +401,7 @@ public:
priority = CommandPriority::HIGH;
};
virtual string getString();
virtual unsigned int getLoad() { return 1; }
virtual unsigned int getLoad() { return 5; }
private:
const string groupName;
@@ -421,7 +421,7 @@ public:
priority = CommandPriority::MEDIUM;
};
virtual string getString();
virtual unsigned int getLoad() { return 4; }
virtual unsigned int getLoad() { return 5; }
private:
const Coords location;

View File

@@ -1,6 +1,8 @@
#pragma once
#include "airunit.h"
#define HELICOPTER_DEST_DIST_THR 500 // Meters
class Helicopter : public AirUnit
{
public:
@@ -11,6 +13,8 @@ public:
virtual void changeSpeed(string change);
virtual void changeAltitude(string change);
virtual double getDestinationReachedThreshold() { return HELICOPTER_DEST_DIST_THR; }
protected:
static json::value database;
};

View File

@@ -24,7 +24,7 @@ public:
void setEras(vector<string> newEras) { eras = newEras; }
void setCommandModeOptions(json::value newOptions);
int getFrameRate() { return frameRate; };
int getFrameRate() { return static_cast<int>(round(frameRate)); };
int getLoad();
bool getRestrictSpawns() { return restrictSpawns; }
bool getRestrictToCoalition() { return restrictToCoalition; }

View File

@@ -220,7 +220,7 @@ protected:
virtual void AIloop() = 0;
void appendString(stringstream& ss, const unsigned char& datumIndex, const string& datumValue) {
const unsigned short size = datumValue.size();
const unsigned short size = static_cast<unsigned short>(datumValue.size());
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));
ss << datumValue;
@@ -245,7 +245,7 @@ protected:
template <typename T>
void appendVector(stringstream& ss, const unsigned char& datumIndex, vector<T>& datumValue) {
const unsigned short size = datumValue.size();
const unsigned short size = static_cast<unsigned short>(datumValue.size());
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));
@@ -255,7 +255,7 @@ protected:
template <typename T>
void appendList(stringstream& ss, const unsigned char& datumIndex, list<T>& datumValue) {
const unsigned short size = datumValue.size();
const unsigned short size = static_cast<unsigned short>(datumValue.size());;
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));

View File

@@ -59,7 +59,7 @@ protected:
/********** Private methods **********/
void appendString(stringstream& ss, const unsigned char& datumIndex, const string& datumValue) {
const unsigned short size = datumValue.size();
const unsigned short size = static_cast<unsigned short>(datumValue.size());
ss.write((const char*)&datumIndex, sizeof(unsigned char));
ss.write((const char*)&size, sizeof(unsigned short));
ss << datumValue;

View File

@@ -34,7 +34,6 @@ Aircraft::Aircraft(json::value json, unsigned int ID) : AirUnit(json, ID)
setCategory("Aircraft");
setDesiredSpeed(knotsToMs(300));
setDesiredAltitude(ftToM(20000));
};
void Aircraft::changeSpeed(string change)
@@ -48,11 +47,6 @@ void Aircraft::changeSpeed(string change)
if (getDesiredSpeed() < knotsToMs(50))
setDesiredSpeed(knotsToMs(50));
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
}
void Aircraft::changeAltitude(string change)
@@ -69,14 +63,9 @@ void Aircraft::changeAltitude(string change)
if (getDesiredAltitude() > 5000)
setDesiredAltitude(getDesiredAltitude() + ftToM(2500));
else if (getDesiredAltitude() >= 0)
setDesiredAltitude(getDesiredAltitude() + ftToM(500));
setDesiredAltitude(getDesiredAltitude() + ftToM(500));
}
if (getDesiredAltitude() < 0)
setDesiredAltitude(0);
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
}

View File

@@ -146,12 +146,15 @@ void AirUnit::setState(unsigned char newState)
break;
}
resetTask();
setHasTask(false);
resetTaskFailedCounter();
log(unitName + " setting state from " + to_string(state) + " to " + to_string(newState));
state = newState;
triggerUpdate(DataIndex::state);
AIloop();
}
void AirUnit::AIloop()
@@ -218,7 +221,7 @@ void AirUnit::AIloop()
goToDestination(enrouteTask);
}
else {
if (isDestinationReached(AIR_DEST_DIST_THR)) {
if (isDestinationReached(getDestinationReachedThreshold())) {
if (updateActivePath(looping) && setActiveDestination())
goToDestination(enrouteTask);
else

View File

@@ -104,7 +104,6 @@ void GroundUnit::setState(unsigned char newState)
setEnableTaskCheckFailed(true);
clearActivePath();
resetActiveDestination();
resetTask();
break;
}
case State::FIRE_AT_AREA: {
@@ -135,12 +134,15 @@ void GroundUnit::setState(unsigned char newState)
break;
}
resetTask();
setHasTask(false);
resetTaskFailedCounter();
log(unitName + " setting state from " + to_string(state) + " to " + to_string(newState));
state = newState;
triggerUpdate(DataIndex::state);
AIloop();
}
void GroundUnit::AIloop()
@@ -217,7 +219,7 @@ void GroundUnit::AIloop()
case State::SIMULATE_FIRE_FIGHT: {
setTask("Simulating fire fight");
if (internalCounter == 0 && targetPosition != Coords(NULL)) {
if (internalCounter == 0 && targetPosition != Coords(NULL) && scheduler->getLoad() == 0) {
/* Get the distance and bearing to the target */
Coords scatteredTargetPosition = targetPosition;
double distance;
@@ -257,7 +259,7 @@ void GroundUnit::AIloop()
}
/* Wait an amout of time depending on the shots intensity */
internalCounter = ((ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>(((ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL);
}
if (targetPosition == Coords(NULL))
@@ -265,7 +267,7 @@ void GroundUnit::AIloop()
/* Fallback if something went wrong */
if (internalCounter == 0)
internalCounter = 20 / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>(20 / FRAMERATE_TIME_INTERVAL);
internalCounter--;
break;
@@ -300,7 +302,7 @@ void GroundUnit::AIloop()
}
if (internalCounter == 0)
internalCounter = 20 / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>(20 / FRAMERATE_TIME_INTERVAL);
internalCounter--;
break;
@@ -390,7 +392,7 @@ void GroundUnit::AIloop()
scheduler->appendCommand(command);
setHasTask(true);
internalCounter = (aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>((aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL);
}
/* Else, do miss on purpose */
else {
@@ -412,13 +414,13 @@ void GroundUnit::AIloop()
scheduler->appendCommand(command);
setHasTask(true);
setTargetPosition(Coords(aimLat, aimLng, target->getPosition().alt));
internalCounter = (aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>((aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL);
}
else if (distance < aimMethodRange) {
/* If the unit is closer than the aim method range, use the aim method range */
aimAtPoint(Coords(aimLat, aimLng, aimAlt));
setTargetPosition(Coords(aimLat, aimLng, target->getPosition().alt));
internalCounter = (aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>((aimTime + (ShotsIntensity::HIGH - shotsIntensity) * shotsBaseInterval + 2) / FRAMERATE_TIME_INTERVAL);
}
else {
/* Else just wake the unit up with an impossible command */
@@ -431,7 +433,7 @@ void GroundUnit::AIloop()
setTargetPosition(Coords(NULL));
/* Don't wait too long before checking again */
internalCounter = 5 / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>(5 / FRAMERATE_TIME_INTERVAL);
}
}
missOnPurposeTarget = target;
@@ -450,7 +452,7 @@ void GroundUnit::AIloop()
if (databaseEntry.has_number_field(L"alertnessTimeConstant"))
alertnessTimeConstant = databaseEntry[L"alertnessTimeConstant"].as_number().to_double();
}
internalCounter = (5 + RANDOM_ZERO_TO_ONE * alertnessTimeConstant * 0 /* TODO: remove to enable alertness again */) / FRAMERATE_TIME_INTERVAL;
internalCounter = static_cast<unsigned int>((5 + RANDOM_ZERO_TO_ONE * alertnessTimeConstant * 0 /* TODO: remove to enable alertness again */) / FRAMERATE_TIME_INTERVAL);
missOnPurposeTarget = nullptr;
setTargetPosition(Coords(NULL));
}

View File

@@ -41,33 +41,31 @@ void Helicopter::changeSpeed(string change)
if (change.compare("stop") == 0)
setState(State::IDLE);
else if (change.compare("slow") == 0)
desiredSpeed -= knotsToMs(10);
setDesiredSpeed(getDesiredSpeed() - knotsToMs(10));
else if (change.compare("fast") == 0)
desiredSpeed += knotsToMs(10);
if (desiredSpeed < 0)
desiredSpeed = 0;
setDesiredSpeed(getDesiredSpeed() + knotsToMs(10));
goToDestination(); /* Send the command to reach the destination */
if (getDesiredSpeed() < knotsToMs(0))
setDesiredSpeed(knotsToMs(0));
}
void Helicopter::changeAltitude(string change)
{
if (change.compare("descend") == 0)
{
if (desiredAltitude > 100)
desiredAltitude -= ftToM(100);
else if (desiredAltitude > 0)
desiredAltitude -= ftToM(10);
if (getDesiredAltitude() > 100)
setDesiredAltitude(getDesiredAltitude() - ftToM(100));
else if (getDesiredAltitude() > 0)
setDesiredAltitude(getDesiredAltitude() - ftToM(10));
}
else if (change.compare("climb") == 0)
{
if (desiredAltitude > 100)
desiredAltitude += ftToM(100);
else if (desiredAltitude >= 0)
desiredAltitude += ftToM(10);
if (getDesiredAltitude() > 100)
setDesiredAltitude(getDesiredAltitude() + ftToM(100));
else if (getDesiredAltitude() >= 0)
setDesiredAltitude(getDesiredAltitude() + ftToM(10));
}
if (desiredAltitude < 0)
desiredAltitude = 0;
goToDestination(); /* Send the command to reach the destination */
}
if (getDesiredAltitude() < 0)
setDesiredAltitude(0);
}

View File

@@ -93,34 +93,33 @@ void NavyUnit::setState(unsigned char newState)
setEnableTaskCheckFailed(true);
clearActivePath();
resetActiveDestination();
resetTask();
break;
}
case State::FIRE_AT_AREA: {
setEnableTaskCheckFailed(true);
clearActivePath();
resetActiveDestination();
resetTask();
break;
}
case State::SIMULATE_FIRE_FIGHT: {
setEnableTaskCheckFailed(true);
clearActivePath();
resetActiveDestination();
resetTask();
break;
}
default:
break;
}
if (newState != state)
resetTask();
setHasTask(false);
resetTaskFailedCounter();
log(unitName + " setting state from " + to_string(state) + " to " + to_string(newState));
state = newState;
triggerUpdate(DataIndex::state);
AIloop();
}
void NavyUnit::AIloop()
@@ -188,15 +187,9 @@ void NavyUnit::AIloop()
case State::SIMULATE_FIRE_FIGHT: {
setTask("Simulating fire fight");
if (!getHasTask()) {
std::ostringstream taskSS;
taskSS.precision(10);
// TODO
taskSS << "{id = 'FireAtPoint', lat = " << targetPosition.lat << ", lng = " << targetPosition.lng << ", radius = 1}";
Command* command = dynamic_cast<Command*>(new SetTask(groupName, taskSS.str(), [this]() { this->setHasTaskAssigned(true); }));
scheduler->appendCommand(command);
setHasTask(true);
}
setState(State::IDLE);
}
default:
break;

View File

@@ -56,7 +56,13 @@ void Scheduler::execute(lua_State* L)
log("Error executing command " + commandString);
else
log("Command '" + commandString + "' executed correctly, current load " + to_string(getLoad()));
load = command->getLoad();
/* Adjust the load depending on the fps */
double fpsMultiplier = 20;
if (getFrameRate() + 3 > 0)
fpsMultiplier = static_cast<unsigned int>(max(1, 60 / (getFrameRate() + 3))); /* Multiplier between 1 and 20 */
load = static_cast<unsigned int>(command->getLoad() * fpsMultiplier);
commands.remove(command);
executedCommandsHashes.push_back(command->getHash());
command->executeCallback(); /* Execute the command callback (this is a lambda function that can be used to execute a function when the command is run) */
@@ -82,7 +88,7 @@ void Scheduler::setCommandModeOptions(json::value value) {
setRedSpawnPoints(value[L"spawnPoints"][L"red"].as_number().to_int32());
}
if (value.has_array_field(L"eras")) {
int length = value[L"eras"].as_array().size();
int length = static_cast<int>(value[L"eras"].as_array().size());
vector<string> newEras;
for (int idx = 0; idx < length; idx++)
newEras.push_back(to_string(value[L"eras"].as_array().at(idx)));
@@ -132,6 +138,8 @@ bool Scheduler::checkSpawnPoints(int spawnPoints, string coalition)
return false;
}
}
return false;
}
void Scheduler::handleRequest(string key, json::value value, string username, json::value& answer)
@@ -254,7 +262,7 @@ void Scheduler::handleRequest(string key, json::value value, string username, js
{
unsigned int ID = value[L"ID"].as_integer();
unitsManager->acquireControl(ID);
unsigned int leaderID = value[L"targetID"].as_double();
unsigned int leaderID = value[L"targetID"].as_integer();
double offsetX = value[L"offsetX"].as_double();
double offsetY = value[L"offsetY"].as_double();
double offsetZ = value[L"offsetZ"].as_double();

View File

@@ -89,7 +89,7 @@ void Server::handle_get(http_request request)
try {
time = stoull((*(query.find(L"time"))).second);
}
catch (const std::exception& e) {
catch (...) {
time = 0;
}
}

View File

@@ -430,7 +430,11 @@ void Unit::resetTask()
void Unit::setFormationOffset(Offset newFormationOffset)
{
formationOffset = newFormationOffset;
resetTask();
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::formationOffset);
}
@@ -519,7 +523,11 @@ void Unit::setIsActiveTanker(bool newIsActiveTanker)
{
if (isActiveTanker != newIsActiveTanker) {
isActiveTanker = newIsActiveTanker;
resetTask();
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::isActiveTanker);
}
@@ -529,7 +537,11 @@ void Unit::setIsActiveAWACS(bool newIsActiveAWACS)
{
if (isActiveAWACS != newIsActiveAWACS) {
isActiveAWACS = newIsActiveAWACS;
resetTask();
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::isActiveAWACS);
}
@@ -644,10 +656,11 @@ void Unit::setDesiredSpeed(double newDesiredSpeed)
{
if (desiredSpeed != newDesiredSpeed) {
desiredSpeed = newDesiredSpeed;
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::desiredSpeed);
}
@@ -657,10 +670,11 @@ void Unit::setDesiredAltitude(double newDesiredAltitude)
{
if (desiredAltitude != newDesiredAltitude) {
desiredAltitude = newDesiredAltitude;
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::desiredAltitude);
}
@@ -670,10 +684,11 @@ void Unit::setDesiredSpeedType(string newDesiredSpeedType)
{
if (desiredSpeedType != (newDesiredSpeedType.compare("GS") == 0)) {
desiredSpeedType = newDesiredSpeedType.compare("GS") == 0;
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::desiredSpeedType);
}
@@ -683,10 +698,11 @@ void Unit::setDesiredAltitudeType(string newDesiredAltitudeType)
{
if (desiredAltitudeType != (newDesiredAltitudeType.compare("AGL") == 0)) {
desiredAltitudeType = newDesiredAltitudeType.compare("AGL") == 0;
if (state == State::IDLE)
resetTask();
else
goToDestination(); /* Send the command to reach the destination */
/* Apply the change */
setHasTask(false);
resetTaskFailedCounter();
AIloop();
triggerUpdate(DataIndex::desiredAltitudeType);
}

View File

@@ -82,7 +82,7 @@ void getAllUnits(lua_State* L, map<unsigned int, json::value>& unitJSONs)
lua_pushnil(L);
while (lua_next(L, 2) != 0)
{
unsigned int ID = lua_tonumber(L, -2);
unsigned int ID = static_cast<unsigned int>(lua_tonumber(L, -2));
if (unitJSONs.find(ID) == unitJSONs.end())
unitJSONs[ID] = json::value::object();
luaTableToJSON(L, -1, unitJSONs[ID]);

View File

@@ -18,7 +18,7 @@ namespace base64 {
uint32_t bit_stream = 0;
const std::string base64_chars = get_base64_chars();
std::string encoded;
encoded.reserve(ceil(4.0 / 3.0 * size));
encoded.reserve(static_cast<unsigned long>(ceil(4.0 / 3.0 * size)));
int offset = 0;
for (unsigned int idx = 0; idx < size; idx++) {
unsigned char c = data[idx];
@@ -63,7 +63,7 @@ namespace base64 {
auto num_val = base64_chars.find(c);
if (num_val != std::string::npos) {
offset = 18 - counter % 4 * 6;
bit_stream += num_val << offset;
bit_stream += static_cast<uint32_t>(num_val << offset);
if (offset == 12) {
decoded += static_cast<char>(bit_stream >> 16 & 0xff);
}