Started units spawn functions and major refactoring

This commit is contained in:
Pax1601
2022-12-07 19:46:54 +01:00
parent a10762ac0c
commit 3a46a6df21
41 changed files with 1104 additions and 1121 deletions

View File

@@ -19,27 +19,34 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Commands.h" />
<ClInclude Include="include\LUAFunctions.h" />
<ClInclude Include="include\RESTServer.h" />
<ClInclude Include="include\Scheduler.h" />
<ClInclude Include="include\Unit.h" />
<ClInclude Include="include\UnitsHandler.h" />
<ClInclude Include="include\commands.h" />
<ClInclude Include="include\scriptLoader.h" />
<ClInclude Include="include\server.h" />
<ClInclude Include="include\scheduler.h" />
<ClInclude Include="include\unit.h" />
<ClInclude Include="include\unitsFactory.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Commands.cpp" />
<ClCompile Include="src\LUAFunctions.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\RESTServer.cpp" />
<ClCompile Include="src\Scheduler.cpp" />
<ClCompile Include="src\Unit.cpp" />
<ClCompile Include="src\UnitsHandler.cpp" />
<ClCompile Include="src\commands.cpp" />
<ClCompile Include="src\scriptLoader.cpp" />
<ClCompile Include="src\core.cpp" />
<ClCompile Include="src\server.cpp" />
<ClCompile Include="src\scheduler.cpp" />
<ClCompile Include="src\unit.cpp" />
<ClCompile Include="src\unitsFactory.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\dcstools\dcstools.vcxproj">
<Project>{2b255368-39a0-431a-a6de-cc739ac70dc1}</Project>
</ProjectReference>
<ProjectReference Include="..\logger\logger.vcxproj">
<Project>{873ecabe-fcfe-4217-ac15-91959c3cf1c6}</Project>
</ProjectReference>
<ProjectReference Include="..\luatools\luatools.vcxproj">
<Project>{de139ec1-4f88-47d5-be73-f41915fe14a3}</Project>
</ProjectReference>
<ProjectReference Include="..\utils\utils.vcxproj">
<Project>{b85009ce-4a5c-4a5a-b85d-001b3a2651b2}</Project>
<LinkLibraryDependencies>true</LinkLibraryDependencies>
<UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals">
@@ -162,9 +169,10 @@
<PreprocessorDefinitions>NDEBUG;CORE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include; ..\..\third-party\lua\include; ..\utils\include; ..\shared\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>include;..\..\third-party\lua\include;..\utils\include;..\shared\include;..\dcstools\include;..\logger\include;..\luatools\include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@@ -9,45 +9,45 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\Commands.h">
<ClInclude Include="include\commands.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\RESTServer.h">
<ClInclude Include="include\server.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Scheduler.h">
<ClInclude Include="include\scheduler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Unit.h">
<ClInclude Include="include\unit.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\UnitsHandler.h">
<ClInclude Include="include\unitsFactory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\LUAFunctions.h">
<ClInclude Include="include\scriptLoader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Commands.cpp">
<ClCompile Include="src\commands.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\main.cpp">
<ClCompile Include="src\core.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\RESTServer.cpp">
<ClCompile Include="src\server.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Scheduler.cpp">
<ClCompile Include="src\scheduler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Unit.cpp">
<ClCompile Include="src\unit.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\UnitsHandler.cpp">
<ClCompile Include="src\unitsFactory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\LUAFunctions.cpp">
<ClCompile Include="src\scriptLoader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

View File

@@ -1,13 +1,14 @@
#pragma once
#include "LUAUtils.h"
#include "Utils.h"
#include "framework.h"
#include "luatools.h"
#include "utils.h"
namespace CommandPriority {
enum CommandPriorities { LOW, MEDIUM, HIGH };
};
namespace CommandType {
enum CommandTypes { NO_TYPE, MOVE };
enum CommandTypes { NO_TYPE, MOVE, SMOKE, LASE, EXPLODE, SPAWN_AIR, SPAWN_GROUND };
};
/* Base command class */
@@ -27,11 +28,78 @@ protected:
class MoveCommand : public Command
{
public:
MoveCommand(int ID, wstring unitName, Coords destination) : ID(ID), unitName(unitName), destination(destination) { priority = CommandPriority::LOW; type = CommandType::MOVE; };
MoveCommand(int ID, wstring unitName, Coords destination, int unitCategory) :
ID(ID),
unitName(unitName),
destination(destination),
unitCategory(unitCategory)
{
priority = CommandPriority::LOW;
type = CommandType::MOVE;
};
virtual void execute(lua_State* L);
private:
const int ID;
const wstring unitName;
const Coords destination;
const int unitCategory;
};
/* Smoke command */
class SmokeCommand : public Command
{
public:
SmokeCommand(wstring color, Coords location) :
color(color),
location(location)
{
priority = CommandPriority::LOW;
type = CommandType::SMOKE;
};
virtual void execute(lua_State* L);
private:
const wstring color;
const Coords location;
};
/* Spawn ground unit command */
class SpawnGroundCommand : public Command
{
public:
SpawnGroundCommand(wstring coalition, wstring unitType, Coords location) :
coalition(coalition),
unitType(unitType),
location(location)
{
priority = CommandPriority::LOW;
type = CommandType::SPAWN_GROUND;
};
virtual void execute(lua_State* L);
private:
const wstring coalition;
const wstring unitType;
const Coords location;
};
/* Spawn air unit command */
class SpawnAirCommand : public Command
{
public:
SpawnAirCommand(wstring coalition, wstring unitType, Coords location) :
coalition(coalition),
unitType(unitType),
location(location)
{
priority = CommandPriority::LOW;
type = CommandType::SPAWN_AIR;
};
virtual void execute(lua_State* L);
private:
const wstring coalition;
const wstring unitType;
const Coords location;
};

View File

@@ -1,7 +0,0 @@
#pragma once
#include "framework.h"
namespace LUAFunctions
{
void registerLuaFunctions(lua_State* L);
}

View File

@@ -1,29 +0,0 @@
#pragma once
#include "framework.h"
#include "LUAUtils.h"
using namespace web::http;
using namespace web::http::experimental::listener;
class UnitsFactory;
class Scheduler;
class RESTServer
{
public:
RESTServer(lua_State* L);
~RESTServer();
private:
std::thread* serverThread;
void handle_options(http_request request);
void handle_get(http_request request);
void handle_request(http_request request, function<void(json::value const&, json::value&)> action);
void handle_put(http_request request);
void task();
atomic<bool> runListener;
};

View File

@@ -1,7 +1,7 @@
#pragma once
#include "LUAUtils.h"
#include "framework.h"
#include "Commands.h"
#include "luatools.h"
#include "commands.h"
class Scheduler
{

View File

@@ -1,8 +1,12 @@
#pragma once
#include "Utils.h"
#include "DCSUtils.h"
#include "LUAUtils.h"
#include "framework.h"
#include "utils.h"
#include "dcstools.h"
#include "luatools.h"
namespace UnitCategory {
enum UnitCategories { NO_CATEGORY, AIR, GROUND, NAVY }; // Do not edit, this codes are tied to values in DCS
};
class Unit
{
@@ -13,28 +17,31 @@ public:
void update(json::value json);
void setPath(list<Coords> path);
void setAlive(bool newAlive) { alive = newAlive; }
int getID() { return ID; }
wstring getName() { return name; }
wstring getUnitName() { return unitName; }
wstring getGroupName() { return groupName; }
int getType() { return type; }
wstring getCountry() { return country; }
json::value getType() { return type; } // This functions returns the complete type of the object (Level1, Level2, Level3, Level4)
int getCountry() { return country; }
int getCoalitionID() { return coalitionID; }
double getLatitude() { return latitude; }
double getLongitude() { return longitude; }
double getAltitude() { return altitude; }
double getHeading() { return heading; }
int getCategory();
json::value json();
protected:
int ID;
bool alive = true;
wstring name = L"undefined";
wstring unitName = L"undefined";
wstring groupName = L"undefined";
int type = 0;
wstring country = L"undefined";
json::value type = json::value::null();
int country = 0;
int coalitionID = 0;
double latitude = 0;
double longitude = 0;

View File

@@ -1,23 +0,0 @@
#pragma once
#include "framework.h"
#include "DCSUtils.h"
class Unit;
class UnitsFactory
{
public:
UnitsFactory(lua_State* L);
~UnitsFactory();
Unit* getUnit(int ID);
void getMissionDB(lua_State* L);
void update(lua_State* L);
void updateAnswer(json::value& answer);
private:
map<int, Unit*> units;
json::value missionDB;
};

View File

@@ -1,12 +1,12 @@
#include "Commands.h"
#include "framework.h"
#include "Logger.h"
#include "commands.h"
#include "logger.h"
/* Move command */
void MoveCommand::execute(lua_State* L)
{
std::ostringstream command;
command << "Olympus.move(\"" << Utils::to_string(unitName) << "\"," << destination.lat << "," << destination.lng << "," << 10 << ")";
command.precision(10);
command << "Olympus.move(\"" << to_string(unitName) << "\", " << destination.lat << ", " << destination.lng << ", " << 10 << ", " << unitCategory << ")";
lua_getglobal(L, "net");
lua_getfield(L, -1, "dostring_in");
@@ -14,10 +14,73 @@ void MoveCommand::execute(lua_State* L)
lua_pushstring(L, command.str().c_str());
if (lua_pcall(L, 2, 0, 0) != 0)
{
LOGGER->Log("Error executing MoveCommand");
log("Error executing MoveCommand");
}
else
{
LOGGER->Log("MoveCommand executed successfully");
log("MoveCommand executed successfully");
}
}
/* Smoke command */
void SmokeCommand::execute(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");
}
}
/* Spawn ground command */
void SpawnGroundCommand::execute(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");
}
}
/* Spawn air command */
void SpawnAirCommand::execute(lua_State* L)
{
std::ostringstream command;
command.precision(10);
command << "Olympus.spawnAir(\"" << 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("SpawnAirCommand executed successfully");
}
}

View File

@@ -1,84 +0,0 @@
#include "LUAFunctions.h"
#include "Logger.h"
/* 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 LUAFunctions::registerLuaFunctions(lua_State* L)
{
string modLocation;
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "OLYMPUS") == 0 && buf != nullptr)
{
modLocation = buf;
free(buf);
}
else
{
LOGGER->Log("OLYMPUS environment variable is missing");
return;
}
{
ifstream f(modLocation + "\\Scripts\\mist_4_4_90.lua");
string str;
LOGGER->Log("Reading MIST from " + modLocation + "\\Scripts\\mist_4_4_90.lua");
if (f) {
ostringstream ss;
ss << f.rdbuf();
str = ss.str();
LOGGER->Log("MIST read succesfully");
}
else
{
LOGGER->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)
{
LOGGER->Log("Error registering MIST");
}
else
{
LOGGER->Log("MIST registered successfully");
}
}
{
ifstream f(modLocation + "\\Scripts\\OlympusCommand.lua");
string str;
LOGGER->Log("Reading OlympusCommand.lua from " + modLocation + "\\Scripts\\OlympusCommand.lua");
if (f) {
ostringstream ss;
ss << f.rdbuf();
str = ss.str();
LOGGER->Log("OlympusCommand.lua read succesfully");
}
else
{
LOGGER->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)
{
LOGGER->Log("Error registering OlympusCommand.lua");
}
else
{
LOGGER->Log("OlympusCommand.lua registered successfully");
}
}
}

View File

@@ -1,130 +0,0 @@
#include "RESTServer.h"
#include "Logger.h"
#include "defines.h"
#include "UnitsHandler.h"
#include "Scheduler.h"
#include "LUAUtils.h"
extern UnitsFactory* unitsHandler;
extern Scheduler* scheduler;
extern json::value missionData;
RESTServer::RESTServer(lua_State* L):
runListener(true)
{
DCSUtils::LogInfo(L, "Starting RESTServer");
serverThread = new thread(&RESTServer::task, this);
}
RESTServer::~RESTServer()
{
runListener = false;
}
void RESTServer::handle_options(http_request request)
{
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
request.reply(response);
}
void RESTServer::handle_get(http_request request)
{
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
auto answer = json::value::object();
try
{
unitsHandler->updateAnswer(answer);
answer[L"missionData"] = missionData;
}
catch (http_exception const& e)
{
LOGGER->Log(e.what());
}
response.set_body(answer);
request.reply(response);
}
void RESTServer::handle_request(http_request request, function<void(json::value const&, json::value&)> action)
{
auto answer = json::value::object();
request.extract_json().then([&answer, &action](pplx::task<json::value> task)
{
try
{
auto const& jvalue = task.get();
if (!jvalue.is_null())
{
action(jvalue, answer);
}
}
catch (http_exception const& e)
{
LOGGER->Log(e.what());
}
}).wait();
http_response response(status_codes::OK);
response.headers().add(U("Allow"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
response.headers().add(U("Access-Control-Allow-Methods"), U("GET, POST, PUT, OPTIONS"));
response.headers().add(U("Access-Control-Allow-Headers"), U("Content-Type"));
response.set_body(answer);
request.reply(response);
}
void RESTServer::handle_put(http_request request)
{
handle_request(
request,
[](json::value const& jvalue, json::value& answer)
{
for (auto const& e : jvalue.as_object())
{
auto key = e.first;
auto value = e.second;
scheduler->handleRequest(key, value);
}
});
}
void RESTServer::task()
{
http_listener listener(REST_ADDRESS);
std::function<void(http_request)> handle_options = std::bind(&RESTServer::handle_options, this, std::placeholders::_1);
std::function<void(http_request)> handle_get = std::bind(&RESTServer::handle_get, this, std::placeholders::_1);
std::function<void(http_request)> handle_put = std::bind(&RESTServer::handle_put, this, std::placeholders::_1);
listener.support(methods::OPTIONS, handle_options);
listener.support(methods::GET, handle_get);
listener.support(methods::PUT, handle_put);
try
{
listener.open()
.then([&listener]() {LOGGER->Log("RESTServer starting to listen"); })
.wait();
while (runListener);
listener.close();
LOGGER->Log("RESTServer stopped listening");
}
catch (exception const& e)
{
LOGGER->Log(e.what());
}
}

View File

@@ -1,15 +1,15 @@
#include "Scheduler.h"
#include "Logger.h"
#include "DCSUtils.h"
#include "UnitsHandler.h"
#include "Utils.h"
#include "Unit.h"
#include "scheduler.h"
#include "logger.h"
#include "dcstools.h"
#include "unitsFactory.h"
#include "utils.h"
#include "unit.h"
extern UnitsFactory* unitsHandler;
extern UnitsFactory* unitsFactory;
Scheduler::Scheduler(lua_State* L)
{
DCSUtils::LogInfo(L, "Units Factory constructor called successfully");
LogInfo(L, "Units Factory constructor called successfully");
}
Scheduler::~Scheduler()
@@ -32,7 +32,7 @@ void Scheduler::execute(lua_State* L)
{
if (command->getPriority() == priority)
{
LOGGER->Log("Executing command");
log("Executing command");
switch (command->getType())
{
case CommandType::MOVE:
@@ -42,7 +42,30 @@ void Scheduler::execute(lua_State* 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;
}
goto exit;
@@ -62,12 +85,11 @@ void Scheduler::handleRequest(wstring key, json::value value)
lock.lock();
Command* command = nullptr;
LOGGER->Log(L"Received request with ID: " + key);
log(L"Received request with ID: " + key);
if (key.compare(L"setPath") == 0)
{
int ID = value[L"ID"].as_integer();
wstring unitName = value[L"unitName"].as_string();
LOGGER->Log(unitName);
json::value path = value[L"path"];
list<Coords> newPath;
for (auto const& e : path.as_object())
@@ -75,21 +97,54 @@ void Scheduler::handleRequest(wstring key, json::value value)
wstring WP = e.first;
double lat = path[WP][L"lat"].as_double();
double lng = path[WP][L"lng"].as_double();
LOGGER->Log(unitName + L" set path destination " + WP + L" (" + to_wstring(lat) + L", " + to_wstring(lng) + L")");
log(unitName + L" set path destination " + WP + L" (" + to_wstring(lat) + L", " + to_wstring(lng) + L")");
Coords dest; dest.lat = lat; dest.lng = lng;
newPath.push_back(dest);
Unit* unit = unitsHandler->getUnit(ID);
Unit* unit = unitsFactory->getUnit(ID);
if (unit != nullptr)
{
unit->setPath(newPath);
LOGGER->Log(unitName + L" new path set successfully");
log(unitName + L" new path set successfully");
}
else
{
LOGGER->Log(unitName + L" not found, request will be discarded");
log(unitName + L" not found, request will be discarded");
}
}
}
else if (key.compare(L"smoke") == 0)
{
wstring color = value[L"color"].as_string();
double lat = value[L"location"][L"lat"].as_double();
double lng = value[L"location"][L"lng"].as_double();
log(L"Adding " + color + L" smoke at (" + to_wstring(lat) + L", " + to_wstring(lng) + L")");
Coords loc; loc.lat = lat; loc.lng = lng;
command = dynamic_cast<Command*>(new SmokeCommand(color, loc));
}
else if (key.compare(L"spawnGround") == 0)
{
wstring coalition = value[L"coalition"].as_string();
wstring type = value[L"type"].as_string();
double lat = value[L"location"][L"lat"].as_double();
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));
}
else if (key.compare(L"spawnAir") == 0)
{
wstring coalition = value[L"coalition"].as_string();
wstring type = value[L"type"].as_string();
double lat = value[L"location"][L"lat"].as_double();
double lng = value[L"location"][L"lng"].as_double();
log(L"Spawning " + coalition + L" air 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 SpawnAirCommand(coalition, type, loc));
}
else
{
log(L"Unknown command: " + key);
}
if (command != nullptr)
{

View File

@@ -1,15 +1,15 @@
#include "Unit.h"
#include "Utils.h"
#include "Logger.h"
#include "Commands.h"
#include "Scheduler.h"
#include "unit.h"
#include "utils.h"
#include "logger.h"
#include "commands.h"
#include "scheduler.h"
extern Scheduler* scheduler;
Unit::Unit(json::value json, int ID) :
ID(ID)
{
LOGGER->Log("Creating unit with ID: " + to_string(ID));
log("Creating unit with ID: " + to_string(ID));
update(json);
}
@@ -18,20 +18,36 @@ Unit::~Unit()
}
int Unit::getCategory()
{
if (type.has_number_field(L"level1"))
{
return type[L"level1"].as_number().is_int32();
}
else
{
return UnitCategory::NO_CATEGORY;
}
}
void Unit::update(json::value json)
{
name = json[L"Name"].as_string();
unitName = json[L"UnitName"].as_string();
groupName = json[L"GroupName"].as_string();
//type = json[L"Type"].as_number().to_int32();
//country = json[L"Country"].as_string();
//coalitionID = json[L"CoalitionID"].as_number().to_int32();
type = json[L"Type"];
country = json[L"Country"].as_number().to_int32();
coalitionID = json[L"CoalitionID"].as_number().to_int32();
latitude = json[L"LatLongAlt"][L"Lat"].as_number().to_double();
longitude = json[L"LatLongAlt"][L"Long"].as_number().to_double();
altitude = json[L"LatLongAlt"][L"Alt"].as_number().to_double();
heading = json[L"Heading"].as_number().to_double();
AIloop();
/* If the unit is alive, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */
if (alive)
{
AIloop();
}
}
void Unit::setPath(list<Coords> path)
@@ -46,7 +62,7 @@ void Unit::AIloop()
if (activeDestination != activePath.front())
{
activeDestination = activePath.front();
Command* command = dynamic_cast<Command*>(new MoveCommand(ID, unitName, activeDestination));
Command* command = dynamic_cast<Command*>(new MoveCommand(ID, unitName, activeDestination, getCategory()));
scheduler->appendCommand(command);
}
}
@@ -56,12 +72,13 @@ json::value Unit::json()
{
auto json = json::value::object();
json[L"alive"] = alive;
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"] = json::value::string(country);
json[L"coalitionID"] = type;
json[L"type"] = type;
json[L"country"] = country;
json[L"coalitionID"] = coalitionID;
json[L"latitude"] = latitude;
json[L"longitude"] = longitude;
json[L"altitude"] = altitude;

View File

@@ -1,59 +0,0 @@
#include "UnitsHandler.h"
#include "Logger.h"
#include "Unit.h"
#include "framework.h"
#include "Utils.h"
UnitsFactory::UnitsFactory(lua_State* L)
{
DCSUtils::LogInfo(L, "Units Factory constructor called successfully");
}
UnitsFactory::~UnitsFactory()
{
}
Unit* UnitsFactory::getUnit(int ID)
{
if (units.find(ID) == units.end()) {
return nullptr;
}
else {
return units[ID];
}
}
void UnitsFactory::update(lua_State* L)
{
//lua_getglobal(L, "net");
//lua_getfield(L, -1, "dostring_in");
//lua_pushstring(L, "server");
//lua_pushstring(L, "dostring_in(\"export\", \"Olympus.OlympusDLL.test()\")");
//lua_pcall(L, 2, 0, 0);
map<int, json::value> unitJSONs = DCSUtils::getAllUnits(L);
for (auto const& p : unitJSONs)
{
int ID = p.first;
if (units.count(ID) == 0)
{
units[ID] = new Unit(p.second, ID);
}
units[ID]->update(p.second);
}
}
void UnitsFactory::updateAnswer(json::value& answer)
{
// TODO THREAT SAFEY!
auto unitsJson = json::value::object();
for (auto const& p : units)
{
unitsJson[to_wstring(p.first)] = p.second->json();
}
answer[L"units"] = unitsJson;
}

View File

@@ -1,90 +0,0 @@
#include "framework.h"
#include "DCSUtils.h"
#include "Logger.h"
#include "defines.h"
#include "UnitsHandler.h"
#include "RESTServer.h"
#include "Scheduler.h"
#include "LUAFunctions.h"
auto before = std::chrono::system_clock::now();
UnitsFactory* unitsHandler = nullptr;
RESTServer* restserver = nullptr;
Scheduler* scheduler = nullptr;
json::value missionData;
/* Standard DllMain entry point */
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#define DllExport __declspec( dllexport )
/* Called when DCS simulation stops. All singleton instances are deleted. */
extern "C" DllExport int coreDeinit(lua_State* L)
{
LOGGER->Log("Olympus coreDeinit called successfully");
delete unitsHandler;
delete restserver;
delete scheduler;
LOGGER->Log("All singletons objects destroyed successfully");
return(0);
}
/* Called when DCS simulation starts. All singletons are instantiated, and the custom Lua functions are registered in the Lua state. */
extern "C" DllExport int coreInit(lua_State* L)
{
unitsHandler = new UnitsFactory(L);
restserver = new RESTServer(L);
scheduler = new Scheduler(L);
LUAFunctions::registerLuaFunctions(L);
return(0);
}
extern "C" DllExport int coreFrame(lua_State* L)
{
const std::chrono::duration<double> duration = std::chrono::system_clock::now() - before;
// TODO make intervals editable
if (duration.count() > UPDATE_TIME_INTERVAL)
{
if (unitsHandler != nullptr)
{
unitsHandler->update(L);
}
// TODO allow for different intervals
if (scheduler != nullptr)
{
scheduler->execute(L);
}
before = std::chrono::system_clock::now();
}
return(0);
}
extern "C" DllExport int coreMissionData(lua_State * L)
{
lua_getglobal(L, "Olympus");
lua_getfield(L, -1, "missionData");
missionData = LUAUtils::tableToJSON(L, -1);
return(0);
}