Changed unit:getCategory to Object.getCategory(unit)

And added some isExist guards
This commit is contained in:
Pax1601
2023-11-17 21:34:50 +01:00
parent ca81d1c4ce
commit 11d6f25606

View File

@@ -180,7 +180,7 @@ function Olympus.buildTask(groupName, options)
if options ['altitudeType'] then if options ['altitudeType'] then
if options ['altitudeType'] == "AGL" then if options ['altitudeType'] == "AGL" then
local groundHeight = 0 local groundHeight = 0
if group then if group ~= nil then
local groupPos = mist.getLeadPos(group) local groupPos = mist.getLeadPos(group)
groundHeight = land.getHeight({x = groupPos.x, y = groupPos.z}) groundHeight = land.getHeight({x = groupPos.x, y = groupPos.z})
end end
@@ -287,7 +287,7 @@ end
function Olympus.move(groupName, lat, lng, altitude, altitudeType, speed, speedType, category, taskOptions) function Olympus.move(groupName, lat, lng, altitude, altitudeType, speed, speedType, category, taskOptions)
Olympus.debug("Olympus.move " .. groupName .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. altitudeType .. " ".. speed .. "m/s " .. category .. " " .. Olympus.serializeTable(taskOptions), 2) Olympus.debug("Olympus.move " .. groupName .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. altitudeType .. " ".. speed .. "m/s " .. category .. " " .. Olympus.serializeTable(taskOptions), 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
if category == "Aircraft" then if category == "Aircraft" then
local startPoint = mist.getLeadPos(group) local startPoint = mist.getLeadPos(group)
local endPoint = coord.LLtoLO(lat, lng, 0) local endPoint = coord.LLtoLO(lat, lng, 0)
@@ -765,9 +765,9 @@ end
-- Find a database entry by ID -- Find a database entry by ID
function Olympus.findInDatabase(ID) function Olympus.findInDatabase(ID)
for idx, unit in pairs(Olympus.cloneDatabase) do for idx, unitRecord in pairs(Olympus.cloneDatabase) do
if unit ~= nil and unit["ID"] == ID then if unitRecord ~= nil and unitRecord["ID"] == ID then
return unit return unitRecord
end end
end end
return nil return nil
@@ -789,11 +789,11 @@ function Olympus.clone(cloneTable, deleteOriginal)
-- All the units in the table will be cloned in a single group -- All the units in the table will be cloned in a single group
for idx, cloneData in pairs(cloneTable) do for idx, cloneData in pairs(cloneTable) do
local ID = cloneData.ID local ID = cloneData.ID
local unit = Olympus.findInDatabase(ID) local unitRecord = Olympus.findInDatabase(ID)
if unit ~= nil then if unitRecord ~= nil then
-- Update the data of the cloned unit -- Update the data of the cloned unit
local unitTable = mist.utils.deepCopy(unit) local unitTable = mist.utils.deepCopy(unitRecord)
local point = coord.LLtoLO(cloneData['lat'], cloneData['lng'], 0) local point = coord.LLtoLO(cloneData['lat'], cloneData['lng'], 0)
if unitTable then if unitTable then
@@ -803,8 +803,8 @@ function Olympus.clone(cloneTable, deleteOriginal)
end end
if countryID == nil and category == nil then if countryID == nil and category == nil then
countryID = unit["country"] countryID = unitRecord["country"]
if unit["category"] == Unit.Category.AIRPLANE then if unitRecord["category"] == Unit.Category.AIRPLANE then
category = 'plane' category = 'plane'
route = { route = {
["points"] = ["points"] =
@@ -823,7 +823,7 @@ function Olympus.clone(cloneTable, deleteOriginal)
}, },
}, },
} }
elseif unit["category"] == Unit.Category.HELICOPTER then elseif unitRecord["category"] == Unit.Category.HELICOPTER then
category = 'helicopter' category = 'helicopter'
route = { route = {
["points"] = ["points"] =
@@ -842,9 +842,9 @@ function Olympus.clone(cloneTable, deleteOriginal)
}, },
}, },
} }
elseif unit["category"] == Unit.Category.GROUND_UNIT then elseif unitRecord["category"] == Unit.Category.GROUND_UNIT then
category = 'vehicle' category = 'vehicle'
elseif unit["category"] == Unit.Category.SHIP then elseif unitRecord["category"] == Unit.Category.SHIP then
category = 'ship' category = 'ship'
end end
end end
@@ -884,7 +884,7 @@ end
function Olympus.delete(ID, explosion, explosionType) function Olympus.delete(ID, explosion, explosionType)
Olympus.debug("Olympus.delete " .. ID .. " " .. tostring(explosion), 2) Olympus.debug("Olympus.delete " .. ID .. " " .. tostring(explosion), 2)
local unit = Olympus.getUnitByID(ID) local unit = Olympus.getUnitByID(ID)
if unit then if unit ~= nil and unit:isExist() then
if unit:getPlayerName() or explosion then if unit:getPlayerName() or explosion then
if explosionType == nil then if explosionType == nil then
explosionType = "normal" explosionType = "normal"
@@ -903,7 +903,7 @@ end
function Olympus.setTask(groupName, taskOptions) function Olympus.setTask(groupName, taskOptions)
Olympus.debug("Olympus.setTask " .. groupName .. " " .. Olympus.serializeTable(taskOptions), 2) Olympus.debug("Olympus.setTask " .. groupName .. " " .. Olympus.serializeTable(taskOptions), 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
local task = Olympus.buildTask(groupName, taskOptions); local task = Olympus.buildTask(groupName, taskOptions);
Olympus.debug("Olympus.setTask " .. Olympus.serializeTable(task), 20) Olympus.debug("Olympus.setTask " .. Olympus.serializeTable(task), 20)
if task then if task then
@@ -917,7 +917,7 @@ end
function Olympus.resetTask(groupName) function Olympus.resetTask(groupName)
Olympus.debug("Olympus.resetTask " .. groupName, 2) Olympus.debug("Olympus.resetTask " .. groupName, 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
group:getController():resetTask() group:getController():resetTask()
Olympus.debug("Olympus.resetTask completed successfully", 2) Olympus.debug("Olympus.resetTask completed successfully", 2)
end end
@@ -927,7 +927,7 @@ end
function Olympus.setCommand(groupName, command) function Olympus.setCommand(groupName, command)
Olympus.debug("Olympus.setCommand " .. groupName .. " " .. Olympus.serializeTable(command), 2) Olympus.debug("Olympus.setCommand " .. groupName .. " " .. Olympus.serializeTable(command), 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
group:getController():setCommand(command) group:getController():setCommand(command)
Olympus.debug("Olympus.setCommand completed successfully", 2) Olympus.debug("Olympus.setCommand completed successfully", 2)
end end
@@ -937,7 +937,7 @@ end
function Olympus.setOption(groupName, optionID, optionValue) function Olympus.setOption(groupName, optionID, optionValue)
Olympus.debug("Olympus.setOption " .. groupName .. " " .. optionID .. " " .. tostring(optionValue), 2) Olympus.debug("Olympus.setOption " .. groupName .. " " .. optionID .. " " .. tostring(optionValue), 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
group:getController():setOption(optionID, optionValue) group:getController():setOption(optionID, optionValue)
Olympus.debug("Olympus.setOption completed successfully", 2) Olympus.debug("Olympus.setOption completed successfully", 2)
end end
@@ -947,7 +947,7 @@ end
function Olympus.setOnOff(groupName, onOff) function Olympus.setOnOff(groupName, onOff)
Olympus.debug("Olympus.setOnOff " .. groupName .. " " .. tostring(onOff), 2) Olympus.debug("Olympus.setOnOff " .. groupName .. " " .. tostring(onOff), 2)
local group = Group.getByName(groupName) local group = Group.getByName(groupName)
if group then if group ~= nil then
group:getController():setOnOff(onOff) group:getController():setOnOff(onOff)
Olympus.debug("Olympus.setOnOff completed successfully", 2) Olympus.debug("Olympus.setOnOff completed successfully", 2)
end end
@@ -965,19 +965,19 @@ function Olympus.setUnitsData(arg, time)
index = index + 1 index = index + 1
-- Only the indexes between startIndex and endIndex are handled. This is a simple way to spread the update load over many cycles -- Only the indexes between startIndex and endIndex are handled. This is a simple way to spread the update load over many cycles
if index > startIndex then if index > startIndex then
if unit ~= nil then if unit ~= nil and unit:isExist() then
local table = {} local table = {}
-- Get the object category in Olympus name -- Get the object category in Olympus name
local objectCategory = unit:getCategory() local objectCategory = Object.getCategory(unit)
if objectCategory == Object.Category.UNIT then if objectCategory == Object.Category.UNIT then
if unit:getDesc().category == Unit.Category.AIRPLANE then if unit:getCategory() == Unit.Category.AIRPLANE then
table["category"] = "Aircraft" table["category"] = "Aircraft"
elseif unit:getDesc().category == Unit.Category.HELICOPTER then elseif unit:getCategory() == Unit.Category.HELICOPTER then
table["category"] = "Helicopter" table["category"] = "Helicopter"
elseif unit:getDesc().category == Unit.Category.GROUND_UNIT then elseif unit:getCategory() == Unit.Category.GROUND_UNIT then
table["category"] = "GroundUnit" table["category"] = "GroundUnit"
elseif unit:getDesc().category == Unit.Category.SHIP then elseif unit:getCategory() == Unit.Category.SHIP then
table["category"] = "NavyUnit" table["category"] = "NavyUnit"
end end
else else
@@ -1039,7 +1039,7 @@ function Olympus.setUnitsData(arg, time)
local name = unit:getName() local name = unit:getName()
if Olympus.cloneDatabase[name] ~= nil then if Olympus.cloneDatabase[name] ~= nil then
Olympus.cloneDatabase[name]["ID"] = ID Olympus.cloneDatabase[name]["ID"] = ID
Olympus.cloneDatabase[name]["category"] = unit:getDesc().category Olympus.cloneDatabase[name]["category"] = unit:getCategory()
Olympus.cloneDatabase[name]["heading"] = table["heading"] Olympus.cloneDatabase[name]["heading"] = table["heading"]
Olympus.cloneDatabase[name]["alt"] = alt Olympus.cloneDatabase[name]["alt"] = alt
Olympus.cloneDatabase[name]["country"] = unit:getCountry() Olympus.cloneDatabase[name]["country"] = unit:getCountry()
@@ -1091,17 +1091,17 @@ function Olympus.setWeaponsData(arg, time)
-- Only the indexes between startIndex and endIndex are handled. This is a simple way to spread the update load over many cycles -- Only the indexes between startIndex and endIndex are handled. This is a simple way to spread the update load over many cycles
if index > startIndex then if index > startIndex then
if weapon ~= nil then if weapon ~= nil and weapon:isExist() then
local table = {} local table = {}
-- Get the object category in Olympus name -- Get the object category in Olympus name
local objectCategory = weapon:getCategory() local objectCategory = Object.getCategory(weapon)
if objectCategory == Object.Category.WEAPON then if objectCategory == Object.Category.WEAPON then
if weapon:getDesc().category == Weapon.Category.MISSILE then if weapon:getCategory() == Weapon.Category.MISSILE then
table["category"] = "Missile" table["category"] = "Missile"
elseif weapon:getDesc().category == Weapon.Category.ROCKET then elseif weapon:getCategory() == Weapon.Category.ROCKET then
table["category"] = "Missile" table["category"] = "Missile"
elseif weapon:getDesc().category == Weapon.Category.BOMB then elseif weapon:getCategory() == Weapon.Category.BOMB then
table["category"] = "Bomb" table["category"] = "Bomb"
end end
else else
@@ -1216,7 +1216,7 @@ function Olympus.initializeUnits()
if mist and mist.DBs and mist.DBs.MEunitsById then if mist and mist.DBs and mist.DBs.MEunitsById then
for id, unitsTable in pairs(mist.DBs.MEunitsById) do for id, unitsTable in pairs(mist.DBs.MEunitsById) do
local unit = Unit.getByName(unitsTable["unitName"]) local unit = Unit.getByName(unitsTable["unitName"])
if unit then if unit ~= nil and unit:isExist() then
Olympus.units[unit["id_"]] = unit Olympus.units[unit["id_"]] = unit
end end
end end