From dc57028a3c36fd29c880b1d094c35a01066de343 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Wed, 3 May 2023 10:07:38 +0200 Subject: [PATCH] Ground units can now be copy-pasted --- scripts/OlympusCommand.lua | 19 +++++++------ src/core/include/commands.h | 20 +------------ src/core/include/unit.h | 2 +- src/core/src/airunit.cpp | 2 +- src/core/src/commands.cpp | 57 +++++++++++++++++++++++++------------ src/core/src/groundunit.cpp | 2 +- src/core/src/unit.cpp | 2 +- 7 files changed, 55 insertions(+), 49 deletions(-) diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index 2dedadda..05366e67 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -1,6 +1,6 @@ local version = "v0.2.0-alpha" -local debug = false +local debug = true Olympus.unitCounter = 1 Olympus.payloadRegistry = {} @@ -361,17 +361,20 @@ function Olympus.spawnAircraft(coalition, unitType, lat, lng, spawnOptions) end -- Clones a unit by ID. Will clone the unit with the same original payload as the source unit. TODO: only works on Olympus unit not ME units. -function Olympus.clone(ID, lat, lng) - Olympus.debug("Olympus.clone " .. ID, 2) +function Olympus.clone(ID, lat, lng, category) + Olympus.debug("Olympus.clone " .. ID .. ", " .. category, 2) local unit = Olympus.getUnitByID(ID) if unit then local coalition = Olympus.getCoalitionByCoalitionID(unit:getCoalition()) - -- TODO: only works on Aircraft - local spawnOptions = { - payload = Olympus.payloadRegistry[unit:getName()] - } - Olympus.spawnAircraft(coalition, unit:getTypeName(), lat, lng, spawnOptions) + if category == "Aircraft" then + local spawnOptions = { + payload = Olympus.payloadRegistry[unit:getName()] + } + Olympus.spawnAircraft(coalition, unit:getTypeName(), lat, lng, spawnOptions) + elseif category == "GroundUnit" then + Olympus.spawnGroundUnit(coalition, unit:getTypeName(), lat, lng) + end end Olympus.debug("Olympus.clone completed successfully", 2) end diff --git a/src/core/include/commands.h b/src/core/include/commands.h index c8ffbb21..17383cf1 100644 --- a/src/core/include/commands.h +++ b/src/core/include/commands.h @@ -7,10 +7,6 @@ namespace CommandPriority { enum CommandPriorities { LOW, MEDIUM, HIGH }; }; -namespace CommandType { - enum CommandTypes { NO_TYPE, MOVE, SMOKE, SPAWN_AIR, SPAWN_GROUND, CLONE, FOLLOW, RESET_TASK, SET_OPTION, SET_COMMAND, SET_TASK }; -}; - namespace SetCommandType { enum SetCommandTypes { ROE = 0, @@ -61,29 +57,25 @@ class Command { public: int getPriority() { return priority; } - int getType() { return type; } virtual wstring getString(lua_State* L) = 0; virtual int getLoad() = 0; protected: int priority = CommandPriority::LOW; - int type = CommandType::NO_TYPE; }; /* Simple low priority move command (from user click) */ class Move : public Command { public: - Move(int ID, Coords destination, double speed, double altitude, wstring unitCategory, wstring taskOptions): + Move(int ID, Coords destination, double speed, double altitude, wstring taskOptions): ID(ID), destination(destination), speed(speed), altitude(altitude), - unitCategory(unitCategory), taskOptions(taskOptions) { priority = CommandPriority::HIGH; - type = CommandType::MOVE; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 5; } @@ -91,7 +83,6 @@ public: private: const int ID; const Coords destination; - const wstring unitCategory; const double speed; const double altitude; const wstring taskOptions; @@ -106,7 +97,6 @@ public: location(location) { priority = CommandPriority::LOW; - type = CommandType::SMOKE; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 5; } @@ -126,7 +116,6 @@ public: location(location) { priority = CommandPriority::LOW; - type = CommandType::SPAWN_GROUND; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 100; } @@ -149,7 +138,6 @@ public: airbaseName(airbaseName) { priority = CommandPriority::LOW; - type = CommandType::SPAWN_AIR; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 100; } @@ -171,7 +159,6 @@ public: location(location) { priority = CommandPriority::LOW; - type = CommandType::CLONE; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 100; } @@ -189,7 +176,6 @@ public: ID(ID) { priority = CommandPriority::HIGH; - type = CommandType::CLONE; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 20; } @@ -207,7 +193,6 @@ public: task(task) { priority = CommandPriority::MEDIUM; - type = CommandType::FOLLOW; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 10; } @@ -225,7 +210,6 @@ public: ID(ID) { priority = CommandPriority::HIGH; - type = CommandType::RESET_TASK; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 10; } @@ -243,7 +227,6 @@ public: command(command) { priority = CommandPriority::HIGH; - type = CommandType::RESET_TASK; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 10; } @@ -263,7 +246,6 @@ public: optionValue(optionValue) { priority = CommandPriority::HIGH; - type = CommandType::RESET_TASK; }; virtual wstring getString(lua_State* L); virtual int getLoad() { return 10; } diff --git a/src/core/include/unit.h b/src/core/include/unit.h index b9fde024..0f80a3ba 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -34,6 +34,7 @@ public: void updateExportData(json::value json); void updateMissionData(json::value json); json::value getData(long long time); + virtual wstring getCategory() { return L"No category"; }; /********** Base data **********/ void setAI(bool newAI) { AI = newAI; addMeasure(L"AI", json::value(newAI)); } @@ -201,7 +202,6 @@ protected: Coords oldPosition = Coords(0); // Used to approximate speed /********** Functions **********/ - virtual wstring getCategory() { return L"No category"; }; wstring getTargetName(); wstring getLeaderName(); bool isTargetAlive(); diff --git a/src/core/src/airunit.cpp b/src/core/src/airunit.cpp index 1faf5118..1c313d19 100644 --- a/src/core/src/airunit.cpp +++ b/src/core/src/airunit.cpp @@ -156,7 +156,7 @@ void AirUnit::goToDestination(wstring enrouteTask) { if (activeDestination != NULL) { - Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), getCategory(), enrouteTask)); + Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), enrouteTask)); scheduler->appendCommand(command); hasTask = true; } diff --git a/src/core/src/commands.cpp b/src/core/src/commands.cpp index ef5a7535..68f633da 100644 --- a/src/core/src/commands.cpp +++ b/src/core/src/commands.cpp @@ -1,21 +1,33 @@ #include "commands.h" #include "logger.h" #include "dcstools.h" +#include "unit.h" +#include "unitsmanager.h" + +extern UnitsManager* unitsManager; /* Move command */ wstring Move::getString(lua_State* L) { - std::wostringstream commandSS; - commandSS.precision(10); - commandSS << "Olympus.move, " - << ID << ", " - << destination.lat << ", " - << destination.lng << ", " - << altitude << ", " - << speed << ", " - << "\"" << unitCategory << "\"" << ", " - << taskOptions; - return commandSS.str(); + Unit* unit = unitsManager->getUnit(ID); + if (unit != nullptr) + { + std::wostringstream commandSS; + commandSS.precision(10); + commandSS << "Olympus.move, " + << ID << ", " + << destination.lat << ", " + << destination.lng << ", " + << altitude << ", " + << speed << ", " + << "\"" << unit->getCategory() << "\"" << ", " + << taskOptions; + return commandSS.str(); + } + else + { + return L""; + } } /* Smoke command */ @@ -67,13 +79,22 @@ wstring SpawnAircraft::getString(lua_State* L) /* Clone unit command */ wstring Clone::getString(lua_State* L) { - std::wostringstream commandSS; - commandSS.precision(10); - commandSS << "Olympus.clone, " - << ID << ", " - << location.lat << ", " - << location.lng; - return commandSS.str(); + Unit* unit = unitsManager->getUnit(ID); + if (unit != nullptr) + { + std::wostringstream commandSS; + commandSS.precision(10); + commandSS << "Olympus.clone, " + << ID << ", " + << location.lat << ", " + << location.lng << ", " + << "\"" << unit->getCategory() << "\""; + return commandSS.str(); + } + else + { + return L""; + } } /* Delete unit command */ diff --git a/src/core/src/groundunit.cpp b/src/core/src/groundunit.cpp index b01bc2b5..545aefc5 100644 --- a/src/core/src/groundunit.cpp +++ b/src/core/src/groundunit.cpp @@ -29,7 +29,7 @@ void GroundUnit::AIloop() if (activeDestination != activePath.front()) { activeDestination = activePath.front(); - Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), getCategory(), L"nil")); + Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), L"nil")); scheduler->appendCommand(command); } } diff --git a/src/core/src/unit.cpp b/src/core/src/unit.cpp index f442e044..228a26a9 100644 --- a/src/core/src/unit.cpp +++ b/src/core/src/unit.cpp @@ -4,7 +4,7 @@ #include "commands.h" #include "scheduler.h" #include "defines.h" -#include "unitsManager.h" +#include "unitsmanager.h" #include using namespace std::chrono;