Fixed missing isAlive value

This commit is contained in:
Pax1601 2023-07-26 10:58:39 +02:00
parent 3c11e1c2a1
commit 3fac8a5663
4 changed files with 39 additions and 17 deletions

View File

@ -388,7 +388,7 @@ export function startUpdate() {
getUnits((buffer: ArrayBuffer) => {
var time = getUnitsManager()?.update(buffer);
return time;
}, false);
}, true);
getConnectionStatusPanel()?.update(getConnected());
}
}, 5000);

View File

@ -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

View File

@ -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);

View File

@ -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<milliseconds>(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<milliseconds>(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++)