Boarding OPS simulated

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Boarding OPS simulated

Post by Parel803 »

Good afternoon,

Based on Apache Seventh District Vice I tried some to simulate a boarding.
Got it working but not the message that should be generated so I short-cut that with a new Msgbox
Also added the original LuaInit file.

My question is how I can get in the function 'BoardAndCaptureSuspectVessel' a way that is has a xx% change of being a smuggler and switch side and for the most ships are clean and stay on original side?

Code: Select all

function GetFriendlyShipAndSuspectVesselData()
    local selectedUnits, result = ScenEdit_SelectedUnits(), nil
    if selectedUnits.contacts == nil or selectedUnits.units == nil then
        ScenEdit_MsgBox('Select both a suspect vessel to board and a nearby friendly ship to perform this special action.',0)
        return nil
    elseif #selectedUnits.contacts == 1 and #selectedUnits.units == 1 then
        local ship = ScenEdit_GetUnit({guid=selectedUnits.units[1].guid})
        local vessel = ScenEdit_GetContact({side='United States',guid=selectedUnits.contacts[1].guid})
        local vessel = ScenEdit_GetUnit({guid=vessel.actualunitid})
        result = {ship=ship,vessel=vessel}
        return result
    else
        ScenEdit_MsgBox('Too many units are selected.\n\nEnsure you have selected both a suspect vessel to board and a nearby friendly ship to perform this special action',0)
        return nil
    end
end

function FriendlyShipIsEligibleToBoard()
    local selectedUnits, result = GetFriendlyShipAndSuspectVesselData(), false
    if selectedUnits.ship.type == 'Ship' then
        return true
    else
        ScenEdit_MsgBox('Unit must be a friendly ship to attempt this special action.',0)
        return result
    end
end

function FriendlyShipHasSpeedAdvantageOverSuspectVessel()
    local selectedUnits = GetFriendlyShipAndSuspectVesselData()
    if selectedUnits.ship.speed > selectedUnits.vessel.speed then
        return true
    else
        ScenEdit_MsgBox('Friendly ship must be moving faster than the suspect vessel to board.\n\nIncrease speed or try with a faster ship.',0)
        return false
    end
end

Code: Select all

function CalculateRangeForFriendlyShipAndSuspectVessel()
    local metresPerNMi, feetPerM  = 1852, 3.28084
    local selectedUnits = GetFriendlyShipAndSuspectVesselData()
    local rangeMetres = Round(Tool_Range(selectedUnits.ship.guid, selectedUnits.vessel.guid) * metresPerNMi)
    local rangeFeet = Round(rangeMetres * feetPerM)
    return {feet=rangeFeet,metres=rangeMetres}
end

function FriendlyShipIsInBoardingRangeOfSuspectVessel()
    local range = CalculateRangeForFriendlyShipAndSuspectVessel()
    local maxBoardingRange, feetPerM = 610, 3.28084
    local maxBoardingRangeFeet =  Round(maxBoardingRange * feetPerM)
    if range.metres < maxBoardingRange then
        return true
    else
        ScenEdit_MsgBox('Ship must be within '..maxBoardingRange..'m / '..maxBoardingRangeFeet..'ft.\n\nCurrent range is '..range.metres..'m / '..range.feet..'ft.\n\nMove closer to the vessel.',0)
        return false
    end
end

function BoardAndCaptureSuspectVessel()                                                                         -- can I make it to have a 10% change to switch side and 90% stay on same (nothing found)
    local selectedUnits = GetFriendlyShipAndSuspectVesselData()                                                 -- all start at civilain side with x% change of being suspect and switch side?
    ScenEdit_SetUnitSide({side=selectedUnits.vessel.side, name=selectedUnits.vessel.name, newside='Boarded'})   -- how to influence in advance which one if not the side, ?name??
    ScenEdit_MsgBox('We captured the boarded ship and hand it over ......',0)                                   -- Or pattern of life parameters?
end

function AttemptBoarding()
    local selectedUnits = GetFriendlyShipAndSuspectVesselData()
    if selectedUnits ~= nil then
        if FriendlyShipIsEligibleToBoard() and 
            FriendlyShipHasSpeedAdvantageOverSuspectVessel() and 
                FriendlyShipIsInBoardingRangeOfSuspectVessel() then
                    BoardAndCaptureSuspectVessel()
        end
    end
end

AttemptBoarding()
best regards GJ
Attachments
LuaInit.zip
7thViceDistrict LuaIni
(12.83 KiB) Downloaded 7 times
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Boarding OPS simulated

Post by KnightHawk75 »

Code: Select all


--snagged long time ago from https://github.com/rjstone/cmano-lua-helpers/blob/master/cmano_helpers.lua
--it doesn't help all that much but it's marginally better than just os.time in tight loops.
---@param quality? number @The quality number generally between 1 and 20, defaults to 10 if omitted.
function  improvedRandomseed(quality)
    if quality == nil then quality=10 end
    -- os.time() removes dependency on how long the software has been running currently
    -- os.clock() provides miliseconds, unlike os.time()
    -- we "chain" this with previous math.random() output to retain any accumulated entropy
    -- and "stir" a little by repeating more than once, though not much because the execution time
    -- probably doesn't vary much between calls and spending too much time will be a waste unless we want
    -- to spend several seconds doing this.
    math.randomseed(os.time() + math.random(90071992547)) -- preserve some previous entropy if there was any
    for i = 1, quality do
        -- Retain some previous PRNG state while adding a little jitter entropy, but not much.
        -- Jitter entropy comes from thread preemption, interrupt handing, and stuff like that in the OS that is
        -- somewhat random. This means you might not get much if any on a powerful and calm system.
        -- If we had a higher precision clock with ns instead of just ms then that would be more helpful.
        math.randomseed(((os.clock() * 1000) % 1000) + math.random(900719925470000))
    end
end

function BoardAndCaptureSuspectVessel()
    local selectedUnits = GetFriendlyShipAndSuspectVesselData()
    improvedRandomseed(); --init/reinit the randomizer.
    if (selectedUnits ~=nil) and math.random(1,100) <= 10 then  -- call random number generator
      --10% of time
      ScenEdit_SetUnitSide({side=selectedUnits.vessel.side, name=selectedUnits.vessel.guid, newside='Boarded'});
      ScenEdit_MsgBox('We captured the boarded ship and hand it over ......',0);
    else
      --90% of time
      ScenEdit_MsgBox('We boarded ship and found nothing of interest ......',0);  
    end
end
Not tested in game, just spit balling.

GetFriendlyShipsAndSuspectVesselData only returns one vessel, one that is selected, so I don't follow the question on comment line 2 of the function, nor 3-4 as I'm not sure what you want to influence in advance, do you mean what side to switch it too?
Last edited by KnightHawk75 on Fri Dec 30, 2022 12:19 am, edited 1 time in total.
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Boarding OPS simulated

Post by Parel803 »

KnightHawk,
Thanks for your answer, gonna try the % of change it is a smuggler.
Sorry for the questions they were more for myself what I might wanna do in fututre scenrio builds, not important at all, just brainfarts if I would teach some stuff to our sailors with own build scenario's.
I do realize my question are often messy, sorry about that.
best regards GJ
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Boarding OPS simulated

Post by Parel803 »

Realizing that I sound very stupid, I had it working yesterday, it now gives me an error:
ERROR: [string "Console"]:46: attempt to call a nil value (global 'Round')
Internal ERROR: [string "Console"]:46: attempt to call a nil value (global 'Round')

Line being:
46: local rangeMetres = Round(Tool_Range(selectedUnits.ship.guid, selectedUnits.vessel.guid) * metresPerNMi)

Probably forgot what I did yesterday. If someone sees in a sec that would be much appriciated.
Feels stupid but cannot find it.

best regards GJ
User avatar
blu3s
Posts: 1031
Joined: Fri Jul 08, 2022 9:45 am

Re: Boarding OPS simulated

Post by blu3s »

It seems that you must declare the Round function. Every time you start the game all variables are cleared so you should implement a "LuaInit" script with all aux functions you need for you scen.
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Boarding OPS simulated

Post by Parel803 »

blu3s,

Thank you for your time and answer. I look for that.

That was it, thanks.
I had the side "Boarded" still with neutral posture, gonna use friendly. Just noticed the contact stays unfriendly (original side posture), change to neutral after deleting contact. Contact is within VID range.
Was just wondering if this is intended?
Disregards, I remembered this from an earlier question, I'll have to look back.

Also the change/random works, thanks.

best regards GJ
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Boarding OPS simulated

Post by KnightHawk75 »

Parel803 wrote: Tue Dec 27, 2022 9:09 am KnightHawk,
Thanks for your answer, gonna try the % of change it is a smuggler.
Sorry for the questions they were more for myself what I might wanna do in fututre scenrio builds, not important at all, just brainfarts if I would teach some stuff to our sailors with own build scenario's.
I do realize my question are often messy, sorry about that.
best regards GJ
No worries at all, my answers are sometimes a bit messy too, not to mention typo filled. :)
Seems though like you got that % chance part working for what you're doing, let me know if that's not the case.
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Boarding OPS simulated

Post by Parel803 »

KH,
Seems though like you got that % chance part working for what you're doing
I did thx again.

regards GJ
Post Reply

Return to “Lua Legion”