mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Started migration to TypeScript
This commit is contained in:
@@ -8,7 +8,7 @@ namespace CommandPriority {
|
||||
};
|
||||
|
||||
namespace CommandType {
|
||||
enum CommandTypes { NO_TYPE, MOVE, SMOKE, LASE, EXPLODE, SPAWN_AIR, SPAWN_GROUND };
|
||||
enum CommandTypes { NO_TYPE, MOVE, SMOKE, LASE, EXPLODE, SPAWN_AIR, SPAWN_GROUND, CLONE, LAND, REFUEL, FOLLOW };
|
||||
};
|
||||
|
||||
/* Base command class */
|
||||
@@ -17,7 +17,7 @@ class Command
|
||||
public:
|
||||
int getPriority() { return priority; }
|
||||
int getType() { return type; }
|
||||
virtual void execute(lua_State* L) = 0;
|
||||
virtual wstring getString(lua_State* L) = 0;
|
||||
|
||||
protected:
|
||||
int priority = CommandPriority::LOW;
|
||||
@@ -28,28 +28,26 @@ protected:
|
||||
class MoveCommand : public Command
|
||||
{
|
||||
public:
|
||||
MoveCommand(int ID, wstring unitName, Coords destination, double speed, double altitude, wstring unitCategory, wstring targetName):
|
||||
ID(ID),
|
||||
unitName(unitName),
|
||||
MoveCommand(int ID, Coords destination, double speed, double altitude, wstring unitCategory, wstring taskOptions):
|
||||
ID(ID),
|
||||
destination(destination),
|
||||
speed(speed),
|
||||
altitude(altitude),
|
||||
unitCategory(unitCategory),
|
||||
targetName(targetName)
|
||||
taskOptions(taskOptions)
|
||||
{
|
||||
priority = CommandPriority::LOW;
|
||||
priority = CommandPriority::HIGH;
|
||||
type = CommandType::MOVE;
|
||||
};
|
||||
virtual void execute(lua_State* L);
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const int ID;
|
||||
const wstring unitName;
|
||||
const Coords destination;
|
||||
const wstring unitCategory;
|
||||
const double speed;
|
||||
const double altitude;
|
||||
const wstring targetName;
|
||||
const wstring taskOptions;
|
||||
};
|
||||
|
||||
/* Smoke command */
|
||||
@@ -63,7 +61,7 @@ public:
|
||||
priority = CommandPriority::LOW;
|
||||
type = CommandType::SMOKE;
|
||||
};
|
||||
virtual void execute(lua_State* L);
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const wstring color;
|
||||
@@ -71,10 +69,10 @@ private:
|
||||
};
|
||||
|
||||
/* Spawn ground unit command */
|
||||
class SpawnGroundCommand : public Command
|
||||
class SpawnGroundUnitCommand : public Command
|
||||
{
|
||||
public:
|
||||
SpawnGroundCommand(wstring coalition, wstring unitType, Coords location) :
|
||||
SpawnGroundUnitCommand(wstring coalition, wstring unitType, Coords location) :
|
||||
coalition(coalition),
|
||||
unitType(unitType),
|
||||
location(location)
|
||||
@@ -82,7 +80,7 @@ public:
|
||||
priority = CommandPriority::LOW;
|
||||
type = CommandType::SPAWN_GROUND;
|
||||
};
|
||||
virtual void execute(lua_State* L);
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const wstring coalition;
|
||||
@@ -91,23 +89,60 @@ private:
|
||||
};
|
||||
|
||||
/* Spawn air unit command */
|
||||
class SpawnAirCommand : public Command
|
||||
class SpawnAircraftCommand : public Command
|
||||
{
|
||||
public:
|
||||
SpawnAirCommand(wstring coalition, wstring unitType, Coords location, wstring payloadName) :
|
||||
SpawnAircraftCommand(wstring coalition, wstring unitType, Coords location, wstring payloadName, wstring airbaseName) :
|
||||
coalition(coalition),
|
||||
unitType(unitType),
|
||||
location(location),
|
||||
payloadName(payloadName)
|
||||
payloadName(payloadName),
|
||||
airbaseName(airbaseName)
|
||||
{
|
||||
priority = CommandPriority::LOW;
|
||||
type = CommandType::SPAWN_AIR;
|
||||
};
|
||||
virtual void execute(lua_State* L);
|
||||
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const wstring coalition;
|
||||
const wstring unitType;
|
||||
const Coords location;
|
||||
const wstring payloadName;
|
||||
const wstring airbaseName;
|
||||
};
|
||||
|
||||
/* Clone unit command */
|
||||
class CloneCommand : public Command
|
||||
{
|
||||
public:
|
||||
CloneCommand(int ID) :
|
||||
ID(ID)
|
||||
{
|
||||
priority = CommandPriority::LOW;
|
||||
type = CommandType::CLONE;
|
||||
};
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const int ID;
|
||||
};
|
||||
|
||||
/* Follow command */
|
||||
class FollowCommand : public Command
|
||||
{
|
||||
public:
|
||||
FollowCommand(int leaderID, int ID) :
|
||||
leaderID(leaderID),
|
||||
ID(ID)
|
||||
{
|
||||
priority = CommandPriority::LOW;
|
||||
type = CommandType::FOLLOW;
|
||||
};
|
||||
virtual wstring getString(lua_State* L);
|
||||
|
||||
private:
|
||||
const int leaderID;
|
||||
const int ID;
|
||||
};
|
||||
|
||||
@@ -14,26 +14,27 @@ public:
|
||||
~Unit();
|
||||
|
||||
void update(json::value json);
|
||||
json::value json();
|
||||
|
||||
void setPath(list<Coords> path);
|
||||
void setActiveDestination(Coords newActiveDestination) { activeDestination = newActiveDestination; }
|
||||
void setAlive(bool newAlive) { alive = newAlive; }
|
||||
void setTarget(int targetID);
|
||||
wstring getTarget();
|
||||
wstring getCurrentTask();
|
||||
|
||||
void resetActiveDestination();
|
||||
void setLeader(bool newLeader) { leader = newLeader; }
|
||||
void setWingman(bool newWingman) { wingman = newWingman; }
|
||||
void setWingmen(vector<Unit*> newWingmen) { wingmen = newWingmen; }
|
||||
void setFormation(wstring newFormation) { formation = newFormation; }
|
||||
|
||||
virtual void changeSpeed(wstring change) {};
|
||||
virtual void changeAltitude(wstring change) {};
|
||||
|
||||
virtual double getTargetSpeed() { return targetSpeed; };
|
||||
virtual double getTargetAltitude() { return targetAltitude; };
|
||||
void resetActiveDestination();
|
||||
|
||||
int getID() { return ID; }
|
||||
wstring getName() { return name; }
|
||||
wstring getUnitName() { return unitName; }
|
||||
wstring getGroupName() { return groupName; }
|
||||
json::value getType() { return type; } // This functions returns the complete type of the object (Level1, Level2, Level3, Level4)
|
||||
json::value getType() { return type; } // This function returns the complete type of the object (Level1, Level2, Level3, Level4)
|
||||
int getCountry() { return country; }
|
||||
int getCoalitionID() { return coalitionID; }
|
||||
double getLatitude() { return latitude; }
|
||||
@@ -42,34 +43,40 @@ public:
|
||||
double getHeading() { return heading; }
|
||||
json::value getFlags() { return flags; }
|
||||
Coords getActiveDestination() { return activeDestination; }
|
||||
|
||||
virtual wstring getCategory() { return L"No category"; };
|
||||
|
||||
json::value json();
|
||||
wstring getTarget();
|
||||
wstring getCurrentTask() { return currentTask; }
|
||||
virtual double getTargetSpeed() { return targetSpeed; };
|
||||
virtual double getTargetAltitude() { return targetAltitude; };
|
||||
|
||||
protected:
|
||||
int ID;
|
||||
bool AI = false;
|
||||
bool alive = true;
|
||||
wstring name = L"undefined";
|
||||
wstring unitName = L"undefined";
|
||||
wstring groupName = L"undefined";
|
||||
json::value type = json::value::null();
|
||||
int country = NULL;
|
||||
int coalitionID = NULL;
|
||||
double latitude = NULL;
|
||||
double longitude = NULL;
|
||||
double altitude = NULL;
|
||||
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;
|
||||
|
||||
double targetSpeed = 0;
|
||||
double targetAltitude = 0;
|
||||
bool AI = false;
|
||||
bool alive = true;
|
||||
wstring name = L"undefined";
|
||||
wstring unitName = L"undefined";
|
||||
wstring groupName = L"undefined";
|
||||
json::value type = json::value::null();
|
||||
int country = NULL;
|
||||
int coalitionID = NULL;
|
||||
double latitude = NULL;
|
||||
double longitude = NULL;
|
||||
double altitude = NULL;
|
||||
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;
|
||||
wstring taskOptions = L"{}";
|
||||
wstring currentTask = L"";
|
||||
bool leader = false;
|
||||
bool wingman = false;
|
||||
wstring formation = L"";
|
||||
vector<Unit*> wingmen;
|
||||
double targetSpeed = 0;
|
||||
double targetAltitude = 0;
|
||||
|
||||
list<Coords> activePath;
|
||||
Coords activeDestination = Coords(0);
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
#pragma once
|
||||
#include "framework.h"
|
||||
|
||||
#define PROTECTED_CALL "Olympus = {}\n \
|
||||
function Olympus.protectedCall(...)\n\n \
|
||||
local status, retval = pcall(...)\n \
|
||||
if not status then\n \
|
||||
trigger.action.outText(\"ERROR: \" ..retval, 20)\n \
|
||||
end\n \
|
||||
end\n \
|
||||
trigger.action.outText(\"Olympus.protectedCall registered successfully\", 10)\n"
|
||||
|
||||
void registerLuaFunctions(lua_State* L);
|
||||
|
||||
@@ -1,86 +1,87 @@
|
||||
#include "commands.h"
|
||||
#include "logger.h"
|
||||
#include "dcstools.h"
|
||||
|
||||
/* Move command */
|
||||
void MoveCommand::execute(lua_State* L)
|
||||
wstring MoveCommand::getString(lua_State* L)
|
||||
{
|
||||
std::ostringstream command;
|
||||
command.precision(10);
|
||||
command << "Olympus.move(\"" << to_string(unitName) << "\", " << destination.lat << ", " << destination.lng << ", " << altitude << ", " << speed << ", \"" << to_string(unitCategory) << "\", \"" << to_string(targetName) << "\")";
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, command.str().c_str());
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error executing MoveCommand");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("MoveCommand executed successfully");
|
||||
}
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.move, "
|
||||
<< ID << ", "
|
||||
<< destination.lat << ", "
|
||||
<< destination.lng << ", "
|
||||
<< altitude << ", "
|
||||
<< speed << ", "
|
||||
<< "\"" << unitCategory << "\"" << ", "
|
||||
<< taskOptions;
|
||||
return commandSS.str();
|
||||
}
|
||||
|
||||
/* Smoke command */
|
||||
void SmokeCommand::execute(lua_State* L)
|
||||
wstring SmokeCommand::getString(lua_State* L)
|
||||
{
|
||||
std::ostringstream command;
|
||||
command.precision(10);
|
||||
command << "Olympus.smoke(\"" << to_string(color) << "\", " << location.lat << ", " << location.lng << ")";
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, command.str().c_str());
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error executing SmokeCommand");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("SmokeCommand executed successfully");
|
||||
}
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.smoke, "
|
||||
<< "\"" << color << "\"" << ", "
|
||||
<< location.lat << ", "
|
||||
<< location.lng;
|
||||
return commandSS.str();
|
||||
}
|
||||
|
||||
/* Spawn ground command */
|
||||
void SpawnGroundCommand::execute(lua_State* L)
|
||||
wstring SpawnGroundUnitCommand::getString(lua_State* L)
|
||||
{
|
||||
std::ostringstream command;
|
||||
command.precision(10);
|
||||
command << "Olympus.spawnGround(\"" << to_string(coalition) << "\", \"" << to_string(unitType) << "\", " << location.lat << ", " << location.lng << ")";
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, command.str().c_str());
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error executing SpawnGroundCommand");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("SpawnGroundCommand executed successfully");
|
||||
}
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.spawnGroundUnit, "
|
||||
<< "\"" << coalition << "\"" << ", "
|
||||
<< "\"" << unitType << "\"" << ", "
|
||||
<< location.lat << ", "
|
||||
<< location.lng;
|
||||
return commandSS.str();
|
||||
}
|
||||
|
||||
/* Spawn air command */
|
||||
void SpawnAirCommand::execute(lua_State* L)
|
||||
wstring SpawnAircraftCommand::getString(lua_State* L)
|
||||
{
|
||||
std::ostringstream command;
|
||||
command.precision(10);
|
||||
command << "Olympus.spawnAir(\"" << to_string(coalition) << "\", \"" << to_string(unitType) << "\", " << location.lat << ", " << location.lng << "," << "\"" << to_string(payloadName) << "\")";
|
||||
std::wostringstream optionsSS;
|
||||
optionsSS.precision(10);
|
||||
optionsSS << "{"
|
||||
<< "payloadName = \"" << payloadName << "\", "
|
||||
<< "airbaseName = \"" << airbaseName << "\","
|
||||
<< "}";
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, command.str().c_str());
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error executing SpawnAirCommand");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("SpawnAirCommand executed successfully");
|
||||
}
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.spawnAircraft, "
|
||||
<< "\"" << coalition << "\"" << ", "
|
||||
<< "\"" << unitType << "\"" << ", "
|
||||
<< location.lat << ", "
|
||||
<< location.lng << ","
|
||||
<< optionsSS.str();
|
||||
return commandSS.str();
|
||||
}
|
||||
|
||||
/* Clone unit command */
|
||||
wstring CloneCommand::getString(lua_State* L)
|
||||
{
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.clone, "
|
||||
<< ID;
|
||||
return commandSS.str();
|
||||
}
|
||||
|
||||
/* Follow unit command */
|
||||
wstring FollowCommand::getString(lua_State* L)
|
||||
{
|
||||
std::wostringstream commandSS;
|
||||
commandSS.precision(10);
|
||||
commandSS << "Olympus.follow, "
|
||||
<< leaderID << ","
|
||||
<< ID;
|
||||
|
||||
return commandSS.str();
|
||||
}
|
||||
@@ -33,42 +33,15 @@ void Scheduler::execute(lua_State* L)
|
||||
{
|
||||
if (command->getPriority() == priority)
|
||||
{
|
||||
log("Executing command");
|
||||
switch (command->getType())
|
||||
wstring commandString = L"Olympus.protectedCall(" + command->getString(L) + L")";
|
||||
if (dostring_in(L, "server", to_string(commandString)))
|
||||
{
|
||||
case CommandType::MOVE:
|
||||
{
|
||||
MoveCommand* moveCommand = dynamic_cast<MoveCommand*>(command);
|
||||
moveCommand->execute(L);
|
||||
commands.remove(command);
|
||||
break;
|
||||
}
|
||||
case CommandType::SMOKE:
|
||||
{
|
||||
SmokeCommand* smokeCommand = dynamic_cast<SmokeCommand*>(command);
|
||||
smokeCommand->execute(L);
|
||||
commands.remove(command);
|
||||
break;
|
||||
}
|
||||
case CommandType::SPAWN_GROUND:
|
||||
{
|
||||
SpawnGroundCommand* spawnCommand = dynamic_cast<SpawnGroundCommand*>(command);
|
||||
spawnCommand->execute(L);
|
||||
commands.remove(command);
|
||||
break;
|
||||
}
|
||||
case CommandType::SPAWN_AIR:
|
||||
{
|
||||
SpawnAirCommand* spawnCommand = dynamic_cast<SpawnAirCommand*>(command);
|
||||
spawnCommand->execute(L);
|
||||
commands.remove(command);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
log("Unknown command of type " + to_string(command->getType()));
|
||||
commands.remove(command);
|
||||
break;
|
||||
log(L"Error executing command " + commandString);
|
||||
}
|
||||
{
|
||||
log(L"Command " + commandString + L" executed succesfully");
|
||||
}
|
||||
commands.remove(command);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -130,7 +103,7 @@ void Scheduler::handleRequest(wstring key, json::value value)
|
||||
double lng = value[L"location"][L"lng"].as_double();
|
||||
log(L"Spawning " + coalition + L" ground unit of type " + type + L" at (" + to_wstring(lat) + L", " + to_wstring(lng) + L")");
|
||||
Coords loc; loc.lat = lat; loc.lng = lng;
|
||||
command = dynamic_cast<Command*>(new SpawnGroundCommand(coalition, type, loc));
|
||||
command = dynamic_cast<Command*>(new SpawnGroundUnitCommand(coalition, type, loc));
|
||||
}
|
||||
else if (key.compare(L"spawnAir") == 0)
|
||||
{
|
||||
@@ -140,15 +113,16 @@ void Scheduler::handleRequest(wstring key, json::value value)
|
||||
double lng = value[L"location"][L"lng"].as_double();
|
||||
Coords loc; loc.lat = lat; loc.lng = lng;
|
||||
wstring payloadName = value[L"payloadName"].as_string();
|
||||
log(L"Spawning " + coalition + L" air unit of type " + type + L" with payload " + payloadName + L" at (" + to_wstring(lat) + L", " + to_wstring(lng) + L")");
|
||||
command = dynamic_cast<Command*>(new SpawnAirCommand(coalition, type, loc, payloadName));
|
||||
wstring airbaseName = value[L"airbaseName"].as_string();
|
||||
log(L"Spawning " + coalition + L" air unit of type " + type + L" with payload " + payloadName + L" at (" + to_wstring(lat) + L", " + to_wstring(lng) + L" " + airbaseName + L")");
|
||||
command = dynamic_cast<Command*>(new SpawnAircraftCommand(coalition, type, loc, payloadName, airbaseName));
|
||||
}
|
||||
else if (key.compare(L"attackUnit") == 0)
|
||||
{
|
||||
int unitID = value[L"unitID"].as_integer();
|
||||
int ID = value[L"ID"].as_integer();
|
||||
int targetID = value[L"targetID"].as_integer();
|
||||
|
||||
Unit* unit = unitsFactory->getUnit(unitID);
|
||||
Unit* unit = unitsFactory->getUnit(ID);
|
||||
Unit* target = unitsFactory->getUnit(targetID);
|
||||
|
||||
wstring unitName;
|
||||
@@ -193,6 +167,43 @@ void Scheduler::handleRequest(wstring key, json::value value)
|
||||
unit->changeAltitude(value[L"change"].as_string());
|
||||
}
|
||||
}
|
||||
else if (key.compare(L"cloneUnit") == 0)
|
||||
{
|
||||
int ID = value[L"ID"].as_integer();
|
||||
command = dynamic_cast<Command*>(new CloneCommand(ID));
|
||||
log(L"Cloning unit " + to_wstring(ID));
|
||||
}
|
||||
else if (key.compare(L"setLeader") == 0)
|
||||
{
|
||||
int ID = value[L"ID"].as_integer();
|
||||
Unit* unit = unitsFactory->getUnit(ID);
|
||||
json::value wingmenIDs = value[L"wingmenIDs"];
|
||||
vector<Unit*> wingmen;
|
||||
if (unit != nullptr)
|
||||
{
|
||||
for (auto itr = wingmenIDs.as_array().begin(); itr != wingmenIDs.as_array().end(); itr++)
|
||||
{
|
||||
Unit* wingman = unitsFactory->getUnit(itr->as_integer());
|
||||
if (wingman != nullptr)
|
||||
{
|
||||
wingman->setWingman(true);
|
||||
wingmen.push_back(wingman);
|
||||
log(L"Setting " + wingman->getName() + L" as wingman leader");
|
||||
}
|
||||
}
|
||||
unit->setWingmen(wingmen);
|
||||
unit->setLeader(true);
|
||||
unit->resetActiveDestination();
|
||||
log(L"Setting " + unit->getName() + L" as formation leader");
|
||||
}
|
||||
}
|
||||
else if (key.compare(L"setFormation") == 0)
|
||||
{
|
||||
int ID = value[L"ID"].as_integer();
|
||||
Unit* unit = unitsFactory->getUnit(ID);
|
||||
wstring formation = value[L"formation"].as_string();
|
||||
unit->setFormation(formation);
|
||||
}
|
||||
else
|
||||
{
|
||||
log(L"Unknown command: " + key);
|
||||
|
||||
@@ -116,59 +116,46 @@ wstring Unit::getTarget()
|
||||
}
|
||||
}
|
||||
|
||||
wstring Unit::getCurrentTask()
|
||||
void Unit::AIloop()
|
||||
{
|
||||
if (activePath.size() == 0)
|
||||
// For wingman units, the leader decides the active destination
|
||||
if (!wingman)
|
||||
{
|
||||
return L"Idle";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (getTarget().empty())
|
||||
/* Set the active destination to be always equal to the first point of the active path. This is in common with all AI units */
|
||||
if (activePath.size() > 0)
|
||||
{
|
||||
if (looping)
|
||||
if (activeDestination != activePath.front())
|
||||
{
|
||||
return L"Looping";
|
||||
}
|
||||
else if (holding)
|
||||
{
|
||||
return L"Holding";
|
||||
}
|
||||
else
|
||||
{
|
||||
return L"Reaching destination";
|
||||
activeDestination = activePath.front();
|
||||
Command* command = dynamic_cast<Command*>(new MoveCommand(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), getCategory(), taskOptions));
|
||||
scheduler->appendCommand(command);
|
||||
|
||||
if (leader)
|
||||
{
|
||||
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)
|
||||
{
|
||||
// Manually set the path and the active destination of the wingmen
|
||||
(*itr)->setPath(activePath);
|
||||
(*itr)->setActiveDestination(activeDestination);
|
||||
Command* command = dynamic_cast<Command*>(new FollowCommand(ID, (*itr)->getID()));
|
||||
scheduler->appendCommand(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return L"Attacking " + getTarget();
|
||||
if (activeDestination != NULL)
|
||||
{
|
||||
log(unitName + L" no more points in active path");
|
||||
activeDestination = Coords(0); // Set the active path to NULL
|
||||
currentTask = L"Idle";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::AIloop()
|
||||
{
|
||||
/* Set the active destination to be always equal to the first point of the active path. This is in common with all AI units */
|
||||
if (activePath.size() > 0)
|
||||
{
|
||||
if (activeDestination != activePath.front())
|
||||
{
|
||||
activeDestination = activePath.front();
|
||||
Command* command = dynamic_cast<Command*>(new MoveCommand(ID, unitName, activeDestination, getTargetSpeed(), getTargetAltitude(), getCategory(), getTarget()));
|
||||
scheduler->appendCommand(command);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (activeDestination != NULL)
|
||||
{
|
||||
log(unitName + L" no more points in active path");
|
||||
activeDestination = Coords(0); // Set the active path to NULL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* This function calls again the MoveCommand to reach the active destination. This is useful to change speed and altitude, for example */
|
||||
/* 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()
|
||||
{
|
||||
log(unitName + L" resetting active destination");
|
||||
@@ -197,6 +184,15 @@ json::value Unit::json()
|
||||
json[L"flags"] = flags;
|
||||
json[L"category"] = json::value::string(getCategory());
|
||||
json[L"currentTask"] = json::value::string(getCurrentTask());
|
||||
json[L"leader"] = leader;
|
||||
json[L"wingman"] = wingman;
|
||||
json[L"formation"] = json::value::string(formation);
|
||||
|
||||
int i = 0;
|
||||
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)
|
||||
{
|
||||
json[L"wingmenIDs"][i++] = (*itr)->getID();
|
||||
}
|
||||
|
||||
/* Send the active path as a json object */
|
||||
if (activePath.size() > 0) {
|
||||
@@ -224,6 +220,21 @@ AirUnit::AirUnit(json::value json, int ID) : Unit(json, ID)
|
||||
|
||||
void AirUnit::AIloop()
|
||||
{
|
||||
if (targetID != 0)
|
||||
{
|
||||
std::wostringstream taskOptionsSS;
|
||||
taskOptionsSS << "{"
|
||||
<< "id = 'EngageUnit'" << ","
|
||||
<< "targetID = " << targetID << ","
|
||||
<< "}";
|
||||
taskOptions = taskOptionsSS.str();
|
||||
currentTask = L"Attacking " + getTarget();
|
||||
}
|
||||
else
|
||||
{
|
||||
currentTask = L"Reaching destination";
|
||||
}
|
||||
|
||||
/* Call the common AI loop */
|
||||
Unit::AIloop();
|
||||
|
||||
@@ -257,6 +268,7 @@ void AirUnit::AIloop()
|
||||
activePath.push_back(point3);
|
||||
activePath.push_back(Coords(latitude, longitude));
|
||||
holding = true;
|
||||
currentTask = L"Holding";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,39 @@
|
||||
#include "scriptLoader.h"
|
||||
#include "logger.h"
|
||||
#include "luatools.h"
|
||||
#include "dcstools.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
bool executeLuaScript(lua_State* L, string path)
|
||||
{
|
||||
replace(path.begin(), path.end(), '\\', '/');
|
||||
// Encase the loading call in a procted call to catch any syntax errors
|
||||
string str = "Olympus.protectedCall(dofile, \"" + path + "\")";
|
||||
if (dostring_in(L, "server", str.c_str()) != 0)
|
||||
{
|
||||
log("Error registering " + path);
|
||||
}
|
||||
else
|
||||
{
|
||||
log(path + " registered successfully");
|
||||
}
|
||||
}
|
||||
|
||||
/* Executes the "OlympusCommand.lua" file to load in the "Server" Lua space all the Lua functions necessary to perform DCS commands (like moving units) */
|
||||
void registerLuaFunctions(lua_State* L)
|
||||
{
|
||||
string modLocation;
|
||||
|
||||
if (dostring_in(L, "server", PROTECTED_CALL))
|
||||
{
|
||||
log("Error registering protectedCall");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("protectedCall registered successfully");
|
||||
}
|
||||
|
||||
char* buf = nullptr;
|
||||
size_t sz = 0;
|
||||
if (_dupenv_s(&buf, &sz, "DCSOLYMPUS_PATH") == 0 && buf != nullptr)
|
||||
@@ -20,97 +47,7 @@ void registerLuaFunctions(lua_State* L)
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
ifstream f(modLocation + "\\Scripts\\mist_4_4_90.lua");
|
||||
string str;
|
||||
log("Reading MIST from " + modLocation + "\\Scripts\\mist_4_4_90.lua");
|
||||
if (f) {
|
||||
ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
str = ss.str();
|
||||
log("MIST read succesfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Error reading MIST");
|
||||
return;
|
||||
}
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, str.c_str());
|
||||
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error registering MIST");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("MIST registered successfully");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ifstream f(modLocation + "\\Scripts\\OlympusCommand.lua");
|
||||
string str;
|
||||
log("Reading OlympusCommand.lua from " + modLocation + "\\Scripts\\OlympusCommand.lua");
|
||||
if (f) {
|
||||
ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
str = ss.str();
|
||||
log("OlympusCommand.lua read succesfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Error reading OlympusCommand.lua");
|
||||
return;
|
||||
}
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, str.c_str());
|
||||
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error registering OlympusCommand.lua");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("OlympusCommand.lua registered successfully");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ifstream f(modLocation + "\\Scripts\\unitPayloads.lua");
|
||||
string str;
|
||||
log("Reading unitPayloads.lua from " + modLocation + "\\Scripts\\unitPayloads.lua");
|
||||
if (f) {
|
||||
ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
str = ss.str();
|
||||
log("unitPayloads.lua read succesfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("Error reading unitPayloads.lua");
|
||||
return;
|
||||
}
|
||||
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, "server");
|
||||
lua_pushstring(L, str.c_str());
|
||||
|
||||
if (lua_pcall(L, 2, 0, 0) != 0)
|
||||
{
|
||||
log("Error registering unitPayloads.lua");
|
||||
}
|
||||
else
|
||||
{
|
||||
log("unitPayloads.lua registered successfully");
|
||||
}
|
||||
}
|
||||
|
||||
executeLuaScript(L, modLocation + "\\Scripts\\mist_4_4_90.lua");
|
||||
executeLuaScript(L, modLocation + "\\Scripts\\OlympusCommand.lua");
|
||||
executeLuaScript(L, modLocation + "\\Scripts\\unitPayloads.lua");
|
||||
}
|
||||
@@ -6,6 +6,6 @@ void DllExport LogInfo(lua_State* L, string message);
|
||||
void DllExport LogWarning(lua_State* L, string message);
|
||||
void DllExport LogError(lua_State* L, string message);
|
||||
void DllExport Log(lua_State* L, string message, int level);
|
||||
|
||||
int DllExport dostring_in(lua_State* L, string target, string command);
|
||||
map<int, json::value> DllExport getAllUnits(lua_State* L);
|
||||
|
||||
|
||||
@@ -92,4 +92,14 @@ map<int, json::value> getAllUnits(lua_State* L)
|
||||
exit:
|
||||
STACK_CLEAN;
|
||||
return units;
|
||||
}
|
||||
|
||||
|
||||
int dostring_in(lua_State* L, string target, string command)
|
||||
{
|
||||
lua_getglobal(L, "net");
|
||||
lua_getfield(L, -1, "dostring_in");
|
||||
lua_pushstring(L, target.c_str());
|
||||
lua_pushstring(L, command.c_str());
|
||||
return lua_pcall(L, 2, 0, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user