6 Commits

Author SHA1 Message Date
David Pierron
adc9aa110b Merge pull request #129 from ciribob/bugfix/BF-2025.01.04
## Bugfix/BF-2025.01.04
- removed unneeded tracing in ctld.i18n_translate() 
- bug: corrected listNearbyCrates() method not returning anything 
- bug: corrected ctld.dropSlingCrate() method crashing before unloading a crate
- bug: corrected the way we handle dynamic slots; it used to cause random CTDs
2025-01-07 13:47:00 +01:00
David Pierron
9d101faf20 updated the test and demo missions 2025-01-07 13:42:05 +01:00
David Pierron
af7e5c5eb4 bug: corrected the way we handle dynamic slots; it used to cause random CTDs 2025-01-07 13:41:56 +01:00
David Pierron
e6e89e2290 bug: corrected ctld.dropSlingCrate() method crashing before unloading a crate 2025-01-04 22:01:50 +01:00
David Pierron
c84b937e99 bug: corrected listNearbyCrates() method not returning anything 2025-01-04 22:01:19 +01:00
David Pierron
a5eb3b2609 removed unneeded tracing in ctld.i18n_translate() 2025-01-04 22:00:53 +01:00
4 changed files with 32 additions and 89 deletions

121
CTLD.lua
View File

@@ -35,7 +35,7 @@ ctld = {} -- DONT REMOVE!
ctld.Id = "CTLD - " ctld.Id = "CTLD - "
--- Version. --- Version.
ctld.Version = "1.2.1" ctld.Version = "1.2.2"
-- To add debugging messages to dcs.log, change the following log levels to `true`; `Debug` is less detailed than `Trace` -- To add debugging messages to dcs.log, change the following log levels to `true`; `Debug` is less detailed than `Trace`
ctld.Debug = false ctld.Debug = false
@@ -814,77 +814,8 @@ ctld.i18n["es"]["Reset TGT Selection"] = "Restablecer selección TGT"
---@param ... any (list) The parameters to replace in the text, in order (all paremeters will be converted to string) ---@param ... any (list) The parameters to replace in the text, in order (all paremeters will be converted to string)
---@return string the translated and formatted text ---@return string the translated and formatted text
function ctld.i18n_translate(text, ...) function ctld.i18n_translate(text, ...)
local function p(o, level)
local MAX_LEVEL = 20
if level == nil then level = 0 end
if level > MAX_LEVEL then
return ""
end
local text = ""
if (type(o) == "table") then
text = "\n"
for key,value in pairs(o) do
for i=0, level do
text = text .. " "
end
text = text .. ".".. key.."="..p(value, level+1) .. "\n"
end
elseif (type(o) == "function") then
text = "[function]"
elseif (type(o) == "boolean") then
if o == true then
text = "[true]"
else
text = "[false]"
end
else
if o == nil then
text = "[nil]"
else
text = tostring(o)
end
end
return text
end
local function formatText(text, ...)
if not text then
return ""
end
if type(text) ~= 'string' then
text = p(text)
else
local args = ...
if args and args.n and args.n > 0 then
local pArgs = {}
for i=1,args.n do
pArgs[i] = p(args[i])
end
text = text:format(unpack(pArgs))
end
end
local fName = nil
local cLine = nil
if debug and debug.getinfo then
local dInfo = debug.getinfo(3)
fName = dInfo.name
cLine = dInfo.currentline
end
if fName and cLine then
return fName .. '|' .. cLine .. ': ' .. text
elseif cLine then
return cLine .. ': ' .. text
else
return ' ' .. text
end
end
local function logTrace(message, ...)
message = formatText(message, arg)
env.info(" T - " .. ctld.Id .. message)
end
logTrace("ctld.i18n_translate(lang=%s, text=%s", p(ctld.i18n_lang), p(text))
local _text = ctld.i18n[ctld.i18n_lang][text] local _text = ctld.i18n[ctld.i18n_lang][text]
logTrace("_text=%s", p(_text))
-- default to english -- default to english
if _text == nil then if _text == nil then
@@ -899,14 +830,13 @@ function ctld.i18n_translate(text, ...)
if arg and arg.n and arg.n > 0 then if arg and arg.n and arg.n > 0 then
local _args = {} local _args = {}
for i=1,arg.n do for i=1,arg.n do
_args[i] = p(arg[i]) _args[i] = tostring(arg[i]) or ""
end end
for i = 1, #_args do for i = 1, #_args do
_text = string.gsub(_text, "%%" .. i, _args[i]) _text = string.gsub(_text, "%%" .. i, _args[i])
end end
end end
logTrace("returning %s", p(_text))
return _text return _text
end end
@@ -4038,7 +3968,7 @@ function ctld.listNearbyCrates(_args)
local _txt = ctld.i18n_translate("No Nearby Crates") local _txt = ctld.i18n_translate("No Nearby Crates")
if _message ~= "" or _fobMsg ~= "" then if _message ~= "" or _fobMsg ~= "" then
local _txt = "" _txt = ""
if _message ~= "" then if _message ~= "" then
_txt = ctld.i18n_translate("Nearby Crates:\n%1", _message) _txt = ctld.i18n_translate("Nearby Crates:\n%1", _message)
@@ -4500,8 +4430,14 @@ function ctld.dropSlingCrate(_args)
local _heightDiff = ctld.heightDiff(_heli) local _heightDiff = ctld.heightDiff(_heli)
if ctld.inAir(_heli) == false or _heightDiff <= 7.5 then if ctld.inAir(_heli) == false or _heightDiff <= 7.5 then
ctld.displayMessageToGroup(_heli, ctld.i18n_translate("%1 crate has been safely unhooked and is at your %2 o'clock", _currentCrate.desc, ctld.getClockDirection(_heli, _currentCrate.crateUnit)), 10)
_point = ctld.getPointAt12Oclock(_heli, 30) _point = ctld.getPointAt12Oclock(_heli, 30)
local _position = "12"
if ctld.unitDynamicCargoCapable(_heli) then
_point = ctld.getPointAt6Oclock(_heli, 15)
_position = "6"
end
ctld.displayMessageToGroup(_heli, ctld.i18n_translate("%1 crate has been safely unhooked and is at your %2 o'clock", _currentCrate.desc, _position), 10)
elseif _heightDiff > 7.5 and _heightDiff <= 40.0 then elseif _heightDiff > 7.5 and _heightDiff <= 40.0 then
ctld.displayMessageToGroup(_heli, ctld.i18n_translate("%1 crate has been safely dropped below you", _currentCrate.desc), 10) ctld.displayMessageToGroup(_heli, ctld.i18n_translate("%1 crate has been safely dropped below you", _currentCrate.desc), 10)
else -- _heightDiff > 40.0, destroy crate else -- _heightDiff > 40.0, destroy crate
@@ -8207,40 +8143,47 @@ end
--- Handle world events. --- Handle world events.
ctld.eventHandler = {} ctld.eventHandler = {}
function ctld.eventHandler:onEvent(event) function ctld.eventHandler:onEvent(event)
ctld.logTrace("ctld.eventHandler:onEvent(), event = %s", ctld.p(event)) ctld.logTrace("ctld.eventHandler:onEvent()")
if event == nil then if event == nil then
ctld.logError("Event handler was called with a nil event!") ctld.logError("Event handler was called with a nil event!")
return return
end end
local eventName = "unknown"
-- check that we know the event -- check that we know the event
if event.id ~= 20 then -- S_EVENT_PLAYER_ENTER_UNIT if event.id == world.event.S_EVENT_PLAYER_ENTER_UNIT then
eventName = "S_EVENT_PLAYER_ENTER_UNIT"
elseif event.id == world.event.S_EVENT_BIRTH then
eventName = "S_EVENT_BIRTH"
else
ctld.logTrace("Ignoring event %s", ctld.p(event))
return return
end end
ctld.logDebug("caught event %s: %s", ctld.p(eventName), ctld.p(event))
-- find the originator unit -- find the originator unit
local unitName = nil local unitName = nil
if event.initiator ~= nil and event.initiator.getName then if event.initiator ~= nil and event.initiator.getName then
unitName = event.initiator:getName() unitName = event.initiator:getName()
ctld.logDebug("caught event S_EVENT_PLAYER_ENTER_UNIT for unit [%s]", ctld.p(unitName)) ctld.logDebug("unitName = [%s]", ctld.p(unitName))
end end
if not unitName then if not unitName then
ctld.logWarning("no unitname found in event %s", ctld.p(event)) ctld.logInfo("no unitname found in event %s", ctld.p(event))
return return
end end
local nextSteps = coroutine.create(function() local function processHumanPlayer()
ctld.logTrace("in the 'nextSteps' coroutine") ctld.logTrace("in the 'processHumanPlayer' function")
if mist.DBs.humansByName[unitName] then -- it's a human unit if mist.DBs.humansByName[unitName] then -- it's a human unit
ctld.logDebug("caught event S_EVENT_PLAYER_ENTER_UNIT for human unit [%s]", ctld.p(unitName)) ctld.logDebug("caught event %s for human unit [%s]", ctld.p(eventName), ctld.p(unitName))
local _unit = Unit.getByName(unitName) local _unit = Unit.getByName(unitName)
if _unit ~= nil then if _unit ~= nil then
-- assign transport pilot -- assign transport pilot
ctld.logTrace("_unit = %s", ctld.p(_unit)) ctld.logTrace("_unit = %s", ctld.p(_unit))
local playerTypeName = _unit:getTypeName() local playerTypeName = _unit:getTypeName()
ctld.logTrace("playerTypeName = %s", ctld.p(playerTypeName)) ctld.logTrace("playerTypeName = %s", ctld.p(playerTypeName))
-- Allow units to CTLD by aircraft type and not by pilot name -- Allow units to CTLD by aircraft type and not by pilot name
if ctld.addPlayerAircraftByType then if ctld.addPlayerAircraftByType then
for _,aircraftType in pairs(ctld.aircraftTypeTable) do for _,aircraftType in pairs(ctld.aircraftTypeTable) do
@@ -8265,18 +8208,18 @@ function ctld.eventHandler:onEvent(event)
end end
end end
end end
end) end
if not mist.DBs.humansByName[unitName] then if not mist.DBs.humansByName[unitName] then
-- give a few milliseconds for MiST to handle the BIRTH event too -- give a few milliseconds for MiST to handle the BIRTH event too
ctld.logTrace("give MiST some time to handle the BIRTH event too") ctld.logTrace("give MiST some time to handle the BIRTH event too")
timer.scheduleFunction(function() timer.scheduleFunction(function()
ctld.logTrace("resuming the 'nextSteps' coroutine in a timer") ctld.logTrace("calling the 'processHumanPlayer' function in a timer")
coroutine.resume(nextSteps) processHumanPlayer()
end, nil, timer.getTime() + 0.5) end, nil, timer.getTime()+0.5)
else else
ctld.logTrace("resuming the 'nextSteps' coroutine immediately") ctld.logTrace("calling the 'processHumanPlayer' function immediately")
coroutine.resume(nextSteps) processHumanPlayer()
end end
end end

BIN
demo-mission.miz Normal file

Binary file not shown.

BIN
test-dev-static.miz Normal file

Binary file not shown.