From 3fac8a5663a6e8761a5e0e8e64baa36b6d480206 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Wed, 26 Jul 2023 10:58:39 +0200 Subject: [PATCH] Fixed missing isAlive value --- client/src/server/server.ts | 2 +- scripts/OlympusCommand.lua | 21 +++++++++++++++++---- src/core/include/unit.h | 1 + src/core/src/unit.cpp | 32 ++++++++++++++++++++------------ 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/client/src/server/server.ts b/client/src/server/server.ts index 6df9e2e4..0dbd2734 100644 --- a/client/src/server/server.ts +++ b/client/src/server/server.ts @@ -388,7 +388,7 @@ export function startUpdate() { getUnits((buffer: ArrayBuffer) => { var time = getUnitsManager()?.update(buffer); return time; - }, false); + }, true); getConnectionStatusPanel()?.update(getConnected()); } }, 5000); diff --git a/scripts/OlympusCommand.lua b/scripts/OlympusCommand.lua index 542414b0..f07bae12 100644 --- a/scripts/OlympusCommand.lua +++ b/scripts/OlympusCommand.lua @@ -1,6 +1,6 @@ local version = "v0.4.0-alpha" -local debug = true +local debug = false Olympus.unitCounter = 1 Olympus.payloadRegistry = {} @@ -14,6 +14,9 @@ Olympus.log = mist.Logger:new("Olympus", 'info') Olympus.missionData = {} Olympus.unitsData = {} +Olympus.unitNames = {} + +Olympus.missionStartTime = DCS.getRealTime() function Olympus.debug(message, displayFor) if debug == true then @@ -781,7 +784,6 @@ function Olympus.setUnitsData(arg, time) table["ammo"] = unit:getAmmo() --TODO remove a lot of stuff we don't really need table["fuel"] = unit:getFuel() table["life"] = unit:getLife() / unit:getLife0() - table["isAlive"] = (unit:getLife() > 1) and unit:isExist() table["contacts"] = contacts table["position"] = {} table["position"]["lat"] = lat @@ -790,8 +792,10 @@ function Olympus.setUnitsData(arg, time) table["speed"] = mist.vec.mag(unit:getVelocity()) table["heading"] = heading table["country"] = unit:getCountry() - + table["isAlive"] = (unit:getLife() > 1) and unit:isExist() + units[unit:getObjectID()] = table + Olympus.unitNames[unit:getObjectID()] = unit:getName() -- Used to find what units are dead, since they will not be in mist.DBs.groupsByName end end end @@ -808,6 +812,15 @@ function Olympus.setUnitsData(arg, time) end end + -- All the units that can't be retrieved by getByName are dead + for ID, name in pairs(Olympus.unitNames) do + local unit = Unit.getByName(name) + if unit == nil then + units[ID] = {isAlive = false} + Olympus.unitNames[ID] = nil + end + end + -- Assemble unitsData table Olympus.unitsData["units"] = units @@ -847,7 +860,7 @@ function Olympus.setMissionData(arg, time) local mission = {} mission.theatre = env.mission.theatre mission.dateAndTime = { - ["elapsedTime"] = DCS.getRealTime() - env.mission.start_time, + ["elapsedTime"] = DCS.getRealTime() - Olympus.missionStartTime, ["time"] = mist.time.getDHMS(timer.getAbsTime()), ["startTime"] = env.mission.start_time, ["date"] = env.mission.date diff --git a/src/core/include/unit.h b/src/core/include/unit.h index d8c7dbf1..4617ed57 100644 --- a/src/core/include/unit.h +++ b/src/core/include/unit.h @@ -92,6 +92,7 @@ public: void runAILoop(); void update(json::value json, double dt); + void refreshLeaderData(unsigned long long time); unsigned int getID() { return ID; } void getData(stringstream& ss, unsigned long long time); diff --git a/src/core/src/unit.cpp b/src/core/src/unit.cpp index 842c28b9..7859fae9 100644 --- a/src/core/src/unit.cpp +++ b/src/core/src/unit.cpp @@ -136,12 +136,27 @@ void Unit::runAILoop() { Unit* leader = nullptr; setIsLeader(unitsManager->isUnitGroupLeader(this, leader)); + /* If the unit is alive, controlled, is the leader of the group and it is not a human, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */ + if (getAlive() && getControlled() && !getHuman() && getIsLeader()) { + if (checkTaskFailed() && state != State::IDLE && state != State::LAND) + setState(State::IDLE); + AIloop(); + } + + refreshLeaderData(lastLoopTime); + + milliseconds ms = duration_cast(system_clock::now().time_since_epoch()); + lastLoopTime = ms.count(); +} + +void Unit::refreshLeaderData(unsigned long long time) { /* When units are in a group, most data comes from the group leader. If new data is available, align it from the leader */ if (!getIsLeader()) { + Unit* leader = unitsManager->getGroupLeader(this); if (leader != nullptr) { for (unsigned char datumIndex = DataIndex::startOfData + 1; datumIndex < DataIndex::lastIndex; datumIndex++) { - if (leader->checkFreshness(datumIndex, lastLoopTime)) { + if (leader->checkFreshness(datumIndex, time)) { switch (datumIndex) { case DataIndex::controlled: updateValue(controlled, leader->controlled, datumIndex); break; case DataIndex::state: updateValue(state, leader->state, datumIndex); break; @@ -171,19 +186,8 @@ void Unit::runAILoop() { } } } - - /* If the unit is alive, controlled, is the leader of the group and it is not a human, run the AI Loop that performs the requested commands and instructions (moving, attacking, etc) */ - if (getAlive() && getControlled() && !getHuman() && getIsLeader()) { - if (checkTaskFailed() && state != State::IDLE && state != State::LAND) - setState(State::IDLE); - AIloop(); - } - - milliseconds ms = duration_cast(system_clock::now().time_since_epoch()); - lastLoopTime = ms.count(); } - bool Unit::checkFreshness(unsigned char datumIndex, unsigned long long time) { auto it = updateTimeMap.find(datumIndex); if (it == updateTimeMap.end()) @@ -201,6 +205,10 @@ bool Unit::hasFreshData(unsigned long long time) { void Unit::getData(stringstream& ss, unsigned long long time) { + /* When an update is requested, make sure data is refreshed */ + if (time == 0) + refreshLeaderData(0); + const unsigned char endOfData = DataIndex::endOfData; ss.write((const char*)&ID, sizeof(ID)); for (unsigned char datumIndex = DataIndex::startOfData + 1; datumIndex < DataIndex::lastIndex; datumIndex++)