From dca3438db082afb107415a0062424598e9822d2a Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Mon, 5 Jun 2023 21:22:22 +0200 Subject: [PATCH] Added backend for altitude type, onOff, follow roads and explosion --- client/src/controls/mapcontextmenu.ts | 4 +- client/src/missionhandler/missionhandler.ts | 2 +- client/src/panels/unitcontrolpanel.ts | 2 + client/src/units/unit.ts | 2 +- client/src/units/unitsmanager.ts | 7 +++ olympus.json | 2 +- scripts/OlympusCommand.lua | 47 +++++++++++++++------ src/core/include/aircraft.h | 8 ---- src/core/include/airunit.h | 6 ++- src/core/include/commands.h | 34 ++++++++++++--- src/core/include/groundunit.h | 5 +-- src/core/include/helicopter.h | 8 ---- src/core/include/navyunit.h | 3 -- src/core/include/unit.h | 16 ++++++- src/core/include/unitsmanager.h | 2 +- src/core/src/aircraft.cpp | 17 ++------ src/core/src/airunit.cpp | 24 +++++++++-- src/core/src/commands.cpp | 19 ++++++++- src/core/src/groundunit.cpp | 22 +++++++++- src/core/src/helicopter.cpp | 16 ++----- src/core/src/navyunit.cpp | 3 +- src/core/src/scheduler.cpp | 34 ++++++++++++++- src/core/src/unit.cpp | 2 +- src/core/src/unitsmanager.cpp | 4 +- 24 files changed, 198 insertions(+), 91 deletions(-) diff --git a/client/src/controls/mapcontextmenu.ts b/client/src/controls/mapcontextmenu.ts index 38cbba2c..2a5fc2db 100644 --- a/client/src/controls/mapcontextmenu.ts +++ b/client/src/controls/mapcontextmenu.ts @@ -26,7 +26,7 @@ export class MapContextMenu extends ContextMenu { #aircrafSpawnAltitudeSlider: Slider; #groundUnitRoleDropdown: Dropdown; #groundUnitTypeDropdown: Dropdown; - #spawnOptions: SpawnOptions = { role: "", type: "", latlng: new LatLng(0, 0), loadout: null, coalition: "blue", airbaseName: null, altitude: 20000 }; + #spawnOptions: SpawnOptions = { role: "", type: "", latlng: new LatLng(0, 0), loadout: null, coalition: "blue", airbaseName: null, altitude: 20000 * 0.3048 }; constructor(id: string) { super(id); @@ -37,7 +37,7 @@ export class MapContextMenu extends ContextMenu { this.#aircraftRoleDropdown = new Dropdown("aircraft-role-options", (role: string) => this.#setAircraftRole(role)); this.#aircraftTypeDropdown = new Dropdown("aircraft-type-options", (type: string) => this.#setAircraftType(type)); this.#aircraftLoadoutDropdown = new Dropdown("loadout-options", (loadout: string) => this.#setAircraftLoadout(loadout)); - this.#aircrafSpawnAltitudeSlider = new Slider("aircraft-spawn-altitude-slider", 0, 50000, "ft", (value: number) => {this.#spawnOptions.altitude = value;}); + this.#aircrafSpawnAltitudeSlider = new Slider("aircraft-spawn-altitude-slider", 0, 50000, "ft", (value: number) => {this.#spawnOptions.altitude = value * 0.3048;}); this.#groundUnitRoleDropdown = new Dropdown("ground-unit-role-options", (role: string) => this.#setGroundUnitRole(role)); this.#groundUnitTypeDropdown = new Dropdown("ground-unit-type-options", (type: string) => this.#setGroundUnitType(type)); diff --git a/client/src/missionhandler/missionhandler.ts b/client/src/missionhandler/missionhandler.ts index 5e05358a..867e08bf 100644 --- a/client/src/missionhandler/missionhandler.ts +++ b/client/src/missionhandler/missionhandler.ts @@ -87,7 +87,7 @@ export class MissionHandler } } - if ("mission" in data) + if ("mission" in data && data.mission != null) { if (data.mission != null && data.mission.theatre != this.#theatre) { diff --git a/client/src/panels/unitcontrolpanel.ts b/client/src/panels/unitcontrolpanel.ts index 27e68b79..075bdea5 100644 --- a/client/src/panels/unitcontrolpanel.ts +++ b/client/src/panels/unitcontrolpanel.ts @@ -88,6 +88,8 @@ export class UnitControlPanel extends Panel { this.#altitudeTypeSwitch.resetExpectedValue(); this.#onOffSwitch.resetExpectedValue(); this.#followRoadsSwitch.resetExpectedValue(); + this.#altitudeSlider.resetExpectedValue(); + this.#speedSlider.resetExpectedValue(); } addButtons() { diff --git a/client/src/units/unit.ts b/client/src/units/unit.ts index 2e8bf910..35353fe4 100644 --- a/client/src/units/unit.ts +++ b/client/src/units/unit.ts @@ -211,7 +211,7 @@ export class Unit extends CustomMarker { if (key1 in data && key2 in data[key1]) { var value1 = this.#data[key1 as keyof(UnitData)]; var value2 = value1[key2 as keyof typeof value1]; - if (typeof data[key1][key2] === typeof value2) + if (typeof data[key1][key2] === typeof value2 || typeof value2 === "undefined") //@ts-ignore this.#data[key1 as keyof(UnitData)][key2 as keyof typeof struct] = data[key1][key2]; } diff --git a/client/src/units/unitsmanager.ts b/client/src/units/unitsmanager.ts index 540ea7ef..afa0cc9a 100644 --- a/client/src/units/unitsmanager.ts +++ b/client/src/units/unitsmanager.ts @@ -22,6 +22,7 @@ export class UnitsManager { document.addEventListener('unitDeselection', (e: CustomEvent) => this.#onUnitDeselection(e.detail)); document.addEventListener('deleteSelectedUnits', () => this.selectedUnitsDelete()); document.addEventListener('explodeSelectedUnits', () => this.selectedUnitsDelete(true)); + document.addEventListener('keyup', (event) => this.#onKeyUp(event)); } getSelectableAircraft() { @@ -457,6 +458,12 @@ export class UnitsManager { } /***********************************************/ + #onKeyUp(event: KeyboardEvent) { + if (!keyEventWasInInput(event) && event.key === "Delete" ) { + this.selectedUnitsDelete(); + } + } + #onUnitSelection(unit: Unit) { if (this.getSelectedUnits().length > 0) { getMap().setState(MOVE_UNIT); diff --git a/olympus.json b/olympus.json index 45d703ff..3618c236 100644 --- a/olympus.json +++ b/olympus.json @@ -1,6 +1,6 @@ { "server": { - "address": "136.243.170.132", + "address": "localhost", "port": 30000 }, "authentication": { diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index bd61f8d2..5935b5c4 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -145,23 +145,27 @@ function Olympus.buildTask(options) end -- Move a unit. Since many tasks in DCS are Enroute tasks, this function is an important way to control the unit AI -function Olympus.move(ID, lat, lng, altitude, speed, category, taskOptions) - Olympus.debug("Olympus.move " .. ID .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. speed .. "m/s " .. category, 2) +function Olympus.move(ID, lat, lng, altitude, altitudeType, speed, speedType, category, taskOptions) + Olympus.debug("Olympus.move " .. ID .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. altitudeType .. " ".. speed .. "m/s " .. category .. " " .. Olympus.serializeTable(taskOptions), 2) local unit = Olympus.getUnitByID(ID) if unit then if category == "Aircraft" then local startPoint = mist.getLeadPos(unit:getGroup()) local endPoint = coord.LLtoLO(lat, lng, 0) + if altitudeType == "AGL" then + altitude = land.getHeight({x = endPoint.x, y = endPoint.z}) + altitude + end + local path = {} if taskOptions and taskOptions['id'] == 'Land' then path = { - [1] = mist.fixedWing.buildWP(startPoint, flyOverPoint, speed, altitude, 'BARO'), + [1] = mist.fixedWing.buildWP(startPoint, turningPoint, speed, altitude, 'BARO'), [2] = mist.fixedWing.buildWP(endPoint, landing, speed, 0, 'AGL') } else path = { - [1] = mist.fixedWing.buildWP(startPoint, flyOverPoint, speed, altitude, 'BARO'), + [1] = mist.fixedWing.buildWP(startPoint, turningPoint, speed, altitude, 'BARO'), [2] = mist.fixedWing.buildWP(endPoint, turningPoint, speed, altitude, 'BARO') } end @@ -195,11 +199,17 @@ function Olympus.move(ID, lat, lng, altitude, speed, category, taskOptions) { group = unit:getGroup(), point = coord.LLtoLO(lat, lng, 0), - form = "Off Road", heading = 0, - speed = speed, - disableRoads = true + speed = speed } + + if taskOptions and taskOptions['id'] == 'FollowRoads' and taskOptions['value'] == true then + vars["disableRoads"] = false + else + vars["form"] = "Off Road" + vars["disableRoads"] = true + end + mist.groupToRandomPoint(vars) Olympus.debug("Olympus.move executed succesfully on a ground unit", 2) else @@ -279,12 +289,12 @@ end -- payloadName: a string, one of the names defined in unitPayloads.lua. Must be compatible with the unitType -- airbaseName: a string, if present the aircraft will spawn on the ground of the selected airbase -- payload: a table, if present the unit will receive this specific payload. Overrides payloadName -function Olympus.spawnAircraft(coalition, unitType, lat, lng, spawnOptions) +function Olympus.spawnAircraft(coalition, unitType, lat, lng, alt, spawnOptions) local payloadName = spawnOptions["payloadName"] local airbaseName = spawnOptions["airbaseName"] local payload = spawnOptions["payload"] - Olympus.debug("Olympus.spawnAircraft " .. coalition .. " " .. unitType .. " (" .. lat .. ", " .. lng ..")", 2) + Olympus.debug("Olympus.spawnAircraft " .. coalition .. " " .. unitType .. " (" .. lat .. ", " .. lng ..", " .. alt .. ")", 2) local spawnLocation = mist.utils.makeVec3GL(coord.LLtoLO(lat, lng, 0)) if payload == nil then @@ -304,7 +314,7 @@ function Olympus.spawnAircraft(coalition, unitType, lat, lng, spawnOptions) ["type"] = unitType, ["x"] = spawnLocation.x, ["y"] = spawnLocation.z, - ["alt"] = 20000 * 0.3048, + ["alt"] = alt, ["alt_type"] = "BARO", ["skill"] = "Excellent", ["payload"] = @@ -390,7 +400,7 @@ function Olympus.clone(ID, lat, lng, category) local spawnOptions = { payload = Olympus.payloadRegistry[unit:getName()] } - Olympus.spawnAircraft(coalition, unit:getTypeName(), lat, lng, spawnOptions) + Olympus.spawnAircraft(coalition, unit:getTypeName(), lat, lng, unit:getPoint().y, spawnOptions) elseif category == "GroundUnit" then Olympus.spawnGroundUnit(coalition, unit:getTypeName(), lat, lng) end @@ -398,11 +408,11 @@ function Olympus.clone(ID, lat, lng, category) Olympus.debug("Olympus.clone completed successfully", 2) end -function Olympus.delete(ID, lat, lng) - Olympus.debug("Olympus.delete " .. ID, 2) +function Olympus.delete(ID, explosion) + Olympus.debug("Olympus.delete " .. ID .. " " .. tostring(explosion), 2) local unit = Olympus.getUnitByID(ID) if unit then - if unit:getPlayerName() then + if unit:getPlayerName() or explosion then trigger.action.explosion(unit:getPoint() , 250 ) --consider replacing with forcibly deslotting the player, however this will work for now Olympus.debug("Olympus.delete completed successfully", 2) else @@ -452,6 +462,15 @@ function Olympus.setOption(ID, optionID, optionValue) end end +function Olympus.setOnOff(ID, onOff) + Olympus.debug("Olympus.setOnOff " .. ID .. " " .. tostring(onOff), 2) + local unit = Olympus.getUnitByID(ID) + if unit then + unit:getGroup():getController():setOnOff(onOff) + Olympus.debug("Olympus.setOnOff completed successfully", 2) + end +end + function Olympus.serializeTable(val, name, skipnewlines, depth) skipnewlines = skipnewlines or false depth = depth or 0 diff --git a/src/core/include/aircraft.h b/src/core/include/aircraft.h index 6d15e4f6..7fd3f615 100644 --- a/src/core/include/aircraft.h +++ b/src/core/include/aircraft.h @@ -10,12 +10,4 @@ public: virtual void changeSpeed(wstring change); virtual void changeAltitude(wstring change); - virtual double getTargetSpeed() { return targetSpeed; }; - virtual double getTargetAltitude() { return targetAltitude; }; - virtual void setTargetSpeed(double newTargetSpeed); - virtual void setTargetAltitude(double newTargetAltitude); - -protected: - double targetSpeed = 300 / 1.94384; - double targetAltitude = 20000 * 0.3048; }; \ No newline at end of file diff --git a/src/core/include/airunit.h b/src/core/include/airunit.h index a46e9909..e9393dc4 100644 --- a/src/core/include/airunit.h +++ b/src/core/include/airunit.h @@ -15,8 +15,10 @@ public: virtual wstring getCategory() = 0; virtual void changeSpeed(wstring change) {}; virtual void changeAltitude(wstring change) {}; - virtual void setTargetSpeed(double newTargetSpeed) {}; - virtual void setTargetAltitude(double newTargetAltitude) {}; + virtual void setTargetSpeed(double newTargetSpeed); + virtual void setTargetAltitude(double newTargetAltitude); + virtual void setTargetSpeedType(wstring newTargetSpeedType); + virtual void setTargetAltitudeType(wstring newTargetAltitudeType); protected: virtual void AIloop(); diff --git a/src/core/include/commands.h b/src/core/include/commands.h index 7752eea8..920256bb 100644 --- a/src/core/include/commands.h +++ b/src/core/include/commands.h @@ -81,8 +81,6 @@ namespace ECMUse { }; } - - /* Base command class */ class Command { @@ -99,11 +97,13 @@ protected: class Move : public Command { public: - Move(int ID, Coords destination, double speed, double altitude, wstring taskOptions): + Move(int ID, Coords destination, double speed, wstring speedType, double altitude, wstring altitudeType, wstring taskOptions): ID(ID), destination(destination), speed(speed), + speedType(speedType), altitude(altitude), + altitudeType(altitudeType), taskOptions(taskOptions) { priority = CommandPriority::HIGH; @@ -115,7 +115,9 @@ private: const int ID; const Coords destination; const double speed; + const wstring speedType; const double altitude; + const wstring altitudeType; const wstring taskOptions; }; @@ -203,8 +205,9 @@ private: class Delete : public Command { public: - Delete(int ID) : - ID(ID) + Delete(int ID, bool explosion) : + ID(ID), + explosion(explosion) { priority = CommandPriority::HIGH; }; @@ -213,6 +216,7 @@ public: private: const int ID; + const bool explosion; }; /* Follow command */ @@ -299,4 +303,22 @@ private: const int optionValue; const bool optionBool; const bool isBoolean; -}; \ No newline at end of file +}; + +/* Set on ooff */ +class SetOnOff : public Command +{ +public: + SetOnOff(int ID, bool onOff) : + ID(ID), + onOff(onOff) + { + priority = CommandPriority::HIGH; + }; + virtual wstring getString(lua_State* L); + virtual int getLoad() { return 10; } + +private: + const int ID; + const bool onOff; +}; diff --git a/src/core/include/groundunit.h b/src/core/include/groundunit.h index 6edb1a3b..6fb9b967 100644 --- a/src/core/include/groundunit.h +++ b/src/core/include/groundunit.h @@ -12,8 +12,7 @@ public: virtual wstring getCategory() { return L"GroundUnit"; }; virtual void changeSpeed(wstring change); virtual void changeAltitude(wstring change) {}; - virtual double getTargetSpeed() { return targetSpeed; }; + virtual void setOnOff(bool newOnOff); + virtual void setFollowRoads(bool newFollowRoads); -protected: - double targetSpeed = 10; }; \ No newline at end of file diff --git a/src/core/include/helicopter.h b/src/core/include/helicopter.h index 50a0e9c2..097990b3 100644 --- a/src/core/include/helicopter.h +++ b/src/core/include/helicopter.h @@ -10,12 +10,4 @@ public: virtual void changeSpeed(wstring change); virtual void changeAltitude(wstring change); - virtual double getTargetSpeed() { return targetSpeed; }; - virtual double getTargetAltitude() { return targetAltitude; }; - virtual void setTargetSpeed(double newTargetSpeed); - virtual void setTargetAltitude(double newTargetAltitude); - -protected: - double targetSpeed = 100 / 1.94384; - double targetAltitude = 5000 * 0.3048; }; \ No newline at end of file diff --git a/src/core/include/navyunit.h b/src/core/include/navyunit.h index d70d86d2..b61761c7 100644 --- a/src/core/include/navyunit.h +++ b/src/core/include/navyunit.h @@ -10,8 +10,5 @@ public: virtual wstring getCategory() { return L"NavyUnit"; }; virtual void changeSpeed(wstring change); virtual void changeAltitude(wstring change) {}; - virtual double getTargetSpeed() { return targetSpeed; }; -protected: - double targetSpeed = 10; }; \ No newline at end of file diff --git a/src/core/include/unit.h b/src/core/include/unit.h index 2d6d6bcf..c8a02ce1 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -112,9 +112,11 @@ public: Offset getFormationoffset() { return formationOffset; } /********** Task data **********/ - void setCurrentTask(wstring newCurrentTask) { currentTask = newCurrentTask;addMeasure(L"currentTask", json::value(newCurrentTask)); } + void setCurrentTask(wstring newCurrentTask) { currentTask = newCurrentTask; addMeasure(L"currentTask", json::value(newCurrentTask)); } virtual void setTargetSpeed(double newTargetSpeed) { targetSpeed = newTargetSpeed; addMeasure(L"targetSpeed", json::value(newTargetSpeed));} - virtual void setTargetAltitude(double newTargetAltitude) { targetAltitude = newTargetAltitude; addMeasure(L"targetAltitude", json::value(newTargetAltitude));} //TODO fix, double definition + virtual void setTargetAltitude(double newTargetAltitude) { targetAltitude = newTargetAltitude; addMeasure(L"targetAltitude", json::value(newTargetAltitude));} + virtual void setTargetSpeedType(wstring newTargetSpeedType) { targetSpeedType = newTargetSpeedType; addMeasure(L"targetSpeedType", json::value(newTargetSpeedType)); } + virtual void setTargetAltitudeType(wstring newTargetAltitudeType) { targetAltitudeType = newTargetAltitudeType; addMeasure(L"targetAltitudeType", json::value(newTargetAltitudeType)); } void setActiveDestination(Coords newActiveDestination) { activeDestination = newActiveDestination; addMeasure(L"activeDestination", json::value("")); } // TODO fix void setActivePath(list newActivePath); void clearActivePath(); @@ -124,15 +126,21 @@ public: void setTargetID(int newTargetID) { targetID = newTargetID; addMeasure(L"targetID", json::value(newTargetID));} void setIsTanker(bool newIsTanker); void setIsAWACS(bool newIsAWACS); + virtual void setOnOff(bool newOnOff) { onOff = newOnOff; addMeasure(L"onOff", json::value(newOnOff));}; + virtual void setFollowRoads(bool newFollowRoads) { followRoads = newFollowRoads; addMeasure(L"followRoads", json::value(newFollowRoads)); }; wstring getCurrentTask() { return currentTask; } virtual double getTargetSpeed() { return targetSpeed; }; virtual double getTargetAltitude() { return targetAltitude; }; + virtual wstring getTargetSpeedType() { return targetSpeedType; }; + virtual wstring getTargetAltitudeType() { return targetAltitudeType; }; Coords getActiveDestination() { return activeDestination; } list getActivePath() { return activePath; } int getTargetID() { return targetID; } bool getIsTanker() { return isTanker; } bool getIsAWACS() { return isAWACS; } + bool getOnOff() { return onOff; }; + bool getFollowRoads() { return followRoads; }; /********** Options data **********/ void setROE(wstring newROE); @@ -196,11 +204,15 @@ protected: wstring currentTask = L""; double targetSpeed = 0; double targetAltitude = 0; + wstring targetSpeedType = L"GS"; + wstring targetAltitudeType = L"AGL"; list activePath; Coords activeDestination = Coords(0); int targetID = NULL; bool isTanker = false; bool isAWACS = false; + bool onOff = true; + bool followRoads = false; /********** Options data **********/ wstring ROE = L"Designated"; diff --git a/src/core/include/unitsmanager.h b/src/core/include/unitsmanager.h index a851a388..5dfc1dfb 100644 --- a/src/core/include/unitsmanager.h +++ b/src/core/include/unitsmanager.h @@ -14,7 +14,7 @@ public: void updateExportData(lua_State* L); void updateMissionData(json::value missionData); void getData(json::value& answer, long long time); - void deleteUnit(int ID); + void deleteUnit(int ID, bool explosion); private: map units; diff --git a/src/core/src/aircraft.cpp b/src/core/src/aircraft.cpp index 79f2694d..0d839c39 100644 --- a/src/core/src/aircraft.cpp +++ b/src/core/src/aircraft.cpp @@ -17,6 +17,9 @@ Aircraft::Aircraft(json::value json, int ID) : AirUnit(json, ID) { log("New Aircraft created with ID: " + to_string(ID)); addMeasure(L"category", json::value(getCategory())); + + double targetSpeed = 300 / 1.94384; + double targetAltitude = 20000 * 0.3048; setTargetSpeed(targetSpeed); setTargetAltitude(targetAltitude); }; @@ -58,18 +61,4 @@ void Aircraft::changeAltitude(wstring change) setTargetAltitude(0); goToDestination(); /* Send the command to reach the destination */ -} - -void Aircraft::setTargetSpeed(double newTargetSpeed) { - targetSpeed = newTargetSpeed; - addMeasure(L"targetSpeed", json::value(targetSpeed)); - if (activeDestination != NULL) - goToDestination(); -} - -void Aircraft::setTargetAltitude(double newTargetAltitude) { - targetAltitude = newTargetAltitude; - addMeasure(L"targetAltitude", json::value(targetAltitude)); - if (activeDestination != NULL) - goToDestination(); } \ No newline at end of file diff --git a/src/core/src/airunit.cpp b/src/core/src/airunit.cpp index 78d87cdf..ffcf2395 100644 --- a/src/core/src/airunit.cpp +++ b/src/core/src/airunit.cpp @@ -155,12 +155,10 @@ void AirUnit::goToDestination(wstring enrouteTask) { if (activeDestination != NULL) { - Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), enrouteTask)); + Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetSpeedType(), getTargetAltitude(), getTargetAltitudeType(), enrouteTask)); scheduler->appendCommand(command); setHasTask(true); } - else - log(unitName + L" error, no active destination!"); } void AirUnit::AIloop() @@ -318,3 +316,23 @@ void AirUnit::AIloop() addMeasure(L"currentTask", json::value(currentTask)); } + +void AirUnit::setTargetSpeed(double newTargetSpeed) { + Unit::setTargetSpeed(newTargetSpeed); + goToDestination(); +} + +void AirUnit::setTargetAltitude(double newTargetAltitude) { + Unit::setTargetAltitude(newTargetAltitude); + goToDestination(); +} + +void AirUnit::setTargetSpeedType(wstring newTargetSpeedType) { + Unit::setTargetSpeedType(newTargetSpeedType); + goToDestination(); +} + +void AirUnit::setTargetAltitudeType(wstring newTargetAltitudeType) { + Unit::setTargetAltitudeType(newTargetAltitudeType); + goToDestination(); +} \ No newline at end of file diff --git a/src/core/src/commands.cpp b/src/core/src/commands.cpp index 662e62ab..a360f7c2 100644 --- a/src/core/src/commands.cpp +++ b/src/core/src/commands.cpp @@ -19,7 +19,9 @@ wstring Move::getString(lua_State* L) << destination.lat << ", " << destination.lng << ", " << altitude << ", " + << "\"" << altitudeType << "\"" << ", " << speed << ", " + << "\"" << speedType << "\"" << ", " << "\"" << unit->getCategory() << "\"" << ", " << taskOptions; return commandSS.str(); @@ -72,6 +74,7 @@ wstring SpawnAircraft::getString(lua_State* L) << "\"" << unitType << "\"" << ", " << location.lat << ", " << location.lng << ", " + << location.alt << ", " << optionsSS.str(); return commandSS.str(); } @@ -103,7 +106,8 @@ wstring Delete::getString(lua_State* L) std::wostringstream commandSS; commandSS.precision(10); commandSS << "Olympus.delete, " - << ID; + << ID << ", " + << (explosion ? "true" : "false"); return commandSS.str(); } @@ -160,4 +164,17 @@ wstring SetOption::getString(lua_State* L) << (optionBool? "true": "false"); } return commandSS.str(); +} + +/* Set onOff command */ +wstring SetOnOff::getString(lua_State* L) +{ + std::wostringstream commandSS; + commandSS.precision(10); + + commandSS << "Olympus.setOnOff, " + << ID << ", " + << (onOff ? "true" : "false"); + + return commandSS.str(); } \ No newline at end of file diff --git a/src/core/src/groundunit.cpp b/src/core/src/groundunit.cpp index 545aefc5..08815b35 100644 --- a/src/core/src/groundunit.cpp +++ b/src/core/src/groundunit.cpp @@ -17,8 +17,9 @@ GroundUnit::GroundUnit(json::value json, int ID) : Unit(json, ID) { log("New Ground Unit created with ID: " + to_string(ID)); addMeasure(L"category", json::value(getCategory())); + + double targetSpeed = 10; setTargetSpeed(targetSpeed); - setTargetAltitude(targetAltitude); }; void GroundUnit::AIloop() @@ -29,7 +30,11 @@ void GroundUnit::AIloop() if (activeDestination != activePath.front()) { activeDestination = activePath.front(); - Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetAltitude(), L"nil")); + + std::wostringstream taskSS; + taskSS << "{ id = 'FollowRoads', value = " << (getFollowRoads()? "true" : "false") << " }"; + + Command* command = dynamic_cast(new Move(ID, activeDestination, getTargetSpeed(), getTargetSpeedType(), getTargetAltitude(), getTargetAltitudeType(), taskSS.str())); scheduler->appendCommand(command); } } @@ -71,4 +76,17 @@ void GroundUnit::changeSpeed(wstring change) { } +} + +void GroundUnit::setOnOff(bool newOnOff) +{ + Unit::setOnOff(newOnOff); + Command* command = dynamic_cast(new SetOnOff(ID, onOff)); + scheduler->appendCommand(command); +} + +void GroundUnit::setFollowRoads(bool newFollowRoads) +{ + Unit::setFollowRoads(newFollowRoads); + resetActiveDestination(); /* Reset active destination to apply option*/ } \ No newline at end of file diff --git a/src/core/src/helicopter.cpp b/src/core/src/helicopter.cpp index 7d2d1e20..d9f8d444 100644 --- a/src/core/src/helicopter.cpp +++ b/src/core/src/helicopter.cpp @@ -17,6 +17,9 @@ Helicopter::Helicopter(json::value json, int ID) : AirUnit(json, ID) { log("New Helicopter created with ID: " + to_string(ID)); addMeasure(L"category", json::value(getCategory())); + + double targetSpeed = 100 / 1.94384; + double targetAltitude = 5000 * 0.3048; setTargetSpeed(targetSpeed); setTargetAltitude(targetAltitude); }; @@ -59,16 +62,3 @@ void Helicopter::changeAltitude(wstring change) goToDestination(); /* Send the command to reach the destination */ } - - -void Helicopter::setTargetSpeed(double newTargetSpeed) { - targetSpeed = newTargetSpeed; - addMeasure(L"targetSpeed", json::value(targetSpeed)); - goToDestination(); -} - -void Helicopter::setTargetAltitude(double newTargetAltitude) { - targetAltitude = newTargetAltitude; - addMeasure(L"targetAltitude", json::value(targetAltitude)); - goToDestination(); -} \ No newline at end of file diff --git a/src/core/src/navyunit.cpp b/src/core/src/navyunit.cpp index 25f835a6..9898b24d 100644 --- a/src/core/src/navyunit.cpp +++ b/src/core/src/navyunit.cpp @@ -17,8 +17,9 @@ NavyUnit::NavyUnit(json::value json, int ID) : Unit(json, ID) { log("New Navy Unit created with ID: " + to_string(ID)); addMeasure(L"category", json::value(getCategory())); + + double targetSpeed = 10; setTargetSpeed(targetSpeed); - setTargetAltitude(targetAltitude); }; void NavyUnit::AIloop() diff --git a/src/core/src/scheduler.cpp b/src/core/src/scheduler.cpp index ad46db11..a6b133c4 100644 --- a/src/core/src/scheduler.cpp +++ b/src/core/src/scheduler.cpp @@ -111,7 +111,8 @@ void Scheduler::handleRequest(wstring key, json::value value) 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(); - Coords loc; loc.lat = lat; loc.lng = lng; + double altitude = value[L"altitude"].as_double(); + Coords loc; loc.lat = lat; loc.lng = lng; loc.alt = altitude; wstring payloadName = value[L"payloadName"].as_string(); 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")"); @@ -192,6 +193,13 @@ void Scheduler::handleRequest(wstring key, json::value value) if (unit != nullptr) unit->setTargetSpeed(value[L"speed"].as_double()); } + else if (key.compare(L"setSpeedType") == 0) + { + int ID = value[L"ID"].as_integer(); + Unit* unit = unitsManager->getUnit(ID); + if (unit != nullptr) + unit->setTargetSpeedType(value[L"speedType"].as_string()); + } else if (key.compare(L"setAltitude") == 0) { int ID = value[L"ID"].as_integer(); @@ -199,6 +207,13 @@ void Scheduler::handleRequest(wstring key, json::value value) if (unit != nullptr) unit->setTargetAltitude(value[L"altitude"].as_double()); } + else if (key.compare(L"setAltitudeType") == 0) + { + int ID = value[L"ID"].as_integer(); + Unit* unit = unitsManager->getUnit(ID); + if (unit != nullptr) + unit->setTargetAltitudeType(value[L"altitudeType"].as_string()); + } else if (key.compare(L"cloneUnit") == 0) { int ID = value[L"ID"].as_integer(); @@ -241,7 +256,8 @@ void Scheduler::handleRequest(wstring key, json::value value) else if (key.compare(L"deleteUnit") == 0) { int ID = value[L"ID"].as_integer(); - unitsManager->deleteUnit(ID); + bool explosion = value[L"explosion"].as_bool(); + unitsManager->deleteUnit(ID, explosion); } else if (key.compare(L"refuel") == 0) { @@ -286,6 +302,20 @@ void Scheduler::handleRequest(wstring key, json::value value) unit->resetActiveDestination(); } } + else if (key.compare(L"setFollowRoads") == 0) + { + int ID = value[L"ID"].as_integer(); + bool followRoads = value[L"followRoads"].as_bool(); + Unit* unit = unitsManager->getUnit(ID); + unit->setFollowRoads(followRoads); + } + else if (key.compare(L"setOnOff") == 0) + { + int ID = value[L"ID"].as_integer(); + bool onOff = value[L"onOff"].as_bool(); + Unit* unit = unitsManager->getUnit(ID); + unit->setOnOff(onOff); + } else { log(L"Unknown command: " + key); diff --git a/src/core/src/unit.cpp b/src/core/src/unit.cpp index b32dc785..0c29509f 100644 --- a/src/core/src/unit.cpp +++ b/src/core/src/unit.cpp @@ -179,7 +179,7 @@ json::value Unit::getData(long long time) /********** Task data **********/ json[L"taskData"] = json::value::object(); - for (auto key : { L"currentState", L"currentTask", L"targetSpeed", L"targetAltitude", L"activePath", L"isTanker", L"isAWACS" }) + for (auto key : { L"currentState", L"currentTask", L"targetSpeed", L"targetAltitude", L"targetSpeedType", L"targetAltitudeType", L"activePath", L"isTanker", L"isAWACS", L"onOff", L"followRoads"}) { if (measures.find(key) != measures.end() && measures[key]->getTime() > time) json[L"taskData"][key] = measures[key]->getValue(); diff --git a/src/core/src/unitsmanager.cpp b/src/core/src/unitsmanager.cpp index 00b1b8cd..d6bacc2e 100644 --- a/src/core/src/unitsmanager.cpp +++ b/src/core/src/unitsmanager.cpp @@ -107,11 +107,11 @@ void UnitsManager::getData(json::value& answer, long long time) answer[L"units"] = unitsJson; } -void UnitsManager::deleteUnit(int ID) +void UnitsManager::deleteUnit(int ID, bool explosion) { if (getUnit(ID) != nullptr) { - Command* command = dynamic_cast(new Delete(ID)); + Command* command = dynamic_cast(new Delete(ID, explosion)); scheduler->appendCommand(command); } }