LUA trigger when base recive cargo

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
Norm49
Posts: 95
Joined: Fri Jun 05, 2015 3:59 pm

LUA trigger when base recive cargo

Post by Norm49 »

Hi,

i have a mission were a need to detect if a cargo and how much of it is in a base.

1st Situation did the base recive some pallet of medical supply (Custom pallet create with the cargo system)

2nd situation how many stranded unit have been move to a base, and how much it revive.
Using lua am able to print the cargo in a base but i can't find how to use in info after that.

inspiration come form the mission Hurricane Katrina. I look at the scrip in this mission but i don't understand it so i didn't help me much.

Thanks for any input
User avatar
lumiere
Posts: 267
Joined: Tue Mar 19, 2019 10:38 am

Re: LUA trigger when base recive cargo

Post by lumiere »

Here is sample cargo check event idea.
Trigger: Regular Time every 1-6 hour (check regularly)
Lua action or condition :

Code: Select all

local dbid = 9
local quantity = 50
local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo
print(cargo)

for k, v in pairs (cargo) do
  if (cargo[k].dbid == dbid) and (cargo[k].quantity >= quantity) then 
    return true  --or do something
  end
end
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
User avatar
lumiere
Posts: 267
Joined: Tue Mar 19, 2019 10:38 am

Re: LUA trigger when base recive cargo

Post by lumiere »

And event code at Katrina scenario (of Steam Workshop, right?)
(1) Collect personel cargo at major airfields, sum each cargo crew as rescued value

Code: Select all

local rescued = 0

local u = ScenEdit_GetUnit({ side='Civilians', unitname='AUS' })
for clk,clv in pairs(u.cargo) do
  for ck,cv in pairs(clv) do
    if cv[1] then
      for cik,civ in pairs(cv) do
        rescued = rescued + civ.crew
        u:deleteUnitCargo(civ.guid)
        --print(civ)
      end
     end
  end
end

local u = ScenEdit_GetUnit({ side='Civilians', unitname='ATL' })
for clk,clv in pairs(u.cargo) do
  for ck,cv in pairs(clv) do
    if cv[1] then
      for cik,civ in pairs(cv) do
        rescued = rescued + civ.crew
        u:deleteUnitCargo(civ.guid)
        --print(civ)
      end
     end
  end
end

local u = ScenEdit_GetUnit({ side='Civilians', unitname='DFW' })
for clk,clv in pairs(u.cargo) do
  for ck,cv in pairs(clv) do
    if cv[1] then
      for cik,civ in pairs(cv) do
        rescued = rescued + civ.crew
        u:deleteUnitCargo(civ.guid)
        --print(civ)
      end
     end
  end
end

local u = ScenEdit_GetUnit({ side='Civilians', unitname='IAH' })
for clk,clv in pairs(u.cargo) do
  for ck,cv in pairs(clv) do
    if cv[1] then
      for cik,civ in pairs(cv) do
        rescued = rescued + civ.crew
        u:deleteUnitCargo(civ.guid)
        --print(civ)
      end
     end
  end
end

local u = ScenEdit_GetUnit({ side='Civilians', unitname='MIA' })
for clk,clv in pairs(u.cargo) do
  for ck,cv in pairs(clv) do
    if cv[1] then
      for cik,civ in pairs(cv) do
        rescued = rescued + civ.crew
        u:deleteUnitCargo(civ.guid)
        --print(civ)
      end
     end
  end
end
(2) add rescured to side point

Code: Select all

if rescued > 0 then
  local score = ScenEdit_GetScore('Civilians')
  score = score + rescued
  ScenEdit_SetScore('Civilians',score,'Rescued ' .. math.floor(rescued) .. ' civilians')
(3) Split x10 personnel to ten x1 personnel, so that smaller transports can carry
--Search survivor facility as subtype 3001 (Building (Surface) ) units
--If facility has no cargo nor landed a/c or boats, just delete
--If facility still has cargo, check there is at least one x10 cargo (crew ~= 1). Store x10 cargo table value as split
--delete x10 cargo and generate ten x1 cargo (Facility #2046)

Code: Select all

  isEmpty = function(x)
    if next(x) == nil then
      return true
    else
      return false
    end
  end

Code: Select all

  
  local s = VP_GetSide({ side='Civilians' })
  --print(s)
  for k,v in pairs(s.units) do
    if not string.find(v.name, 'Stranded') then
      local u = ScenEdit_GetUnit({ guid=v.guid })
      if u.type == 'Facility' and u.subtype == '3001' then
        --print(u.name)
        --print(isEmpty(u.embarkedUnits))
        local split = nil
        local ones = 0
        if not u.cargo[1] and isEmpty(u.embarkedUnits.Aircraft) and isEmpty(u.embarkedUnits.Boats) then
          u:delete()
        elseif u.cargo[1] ~= nil then
          for ck,cv in pairs(u.cargo[1].cargo) do
            if cv.crew ~= 1 then
              split = cv
            else
              ones = ones + 1
            end
          end

          if ones == 0 and split ~= nil then
            for i=1, split.crew, 1 do
              u:createUnitCargo(3, 2046)
            end
            u:deleteUnitCargo(split.guid)
          end
        end
      end
    end
  end
end
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
Norm49
Posts: 95
Joined: Fri Jun 05, 2015 3:59 pm

Re: LUA trigger when base recive cargo

Post by Norm49 »

Ok if i understand correctly
local dbid = 9 -- create a local variable call dbid equal to 9 were 9 is the id of the unit in the data base i use for the scenario

local quantity = 50 -- create a local variable call quantity equal to 50 in this case to number of object with the data base id 9 I want to be move

local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo -- create a local variable call cargo equal to the fist listed cargo element contain in the unit name blue arifield1

print(cargo) -- print all the detail of the fist cargo element of blue airfield1 but in this case for the script it is actually the value of the local variable call cargo

for k, v in pairs (cargo) do -- call 2 element of the printed table of the local variable call cargo K and V. (I don't understand why K and V also next V is not use.)

if (cargo[k].dbid == dbid) and (cargo[k].quantity >= quantity) then -- check if the data base id of the cargo element is the same as the one define in the local variable dbid and check if the amount is superior or equal to the one define in the local variable quantity (Why use only k and v is not call?)

return true --or do something -- place to execute my script

end -- end the script

end -- i don't know with to end it more then one time

So can I do something like this? It this case it also check for the second cargo element in the unit i case it's not the fist one listed, or give me an error because I didn't understand something.

local medical_supply_pallet_id = 9 --(9 to be change for the proper number)
local medical_supply_pallet_to_deliver = 4
local supply_deliver = false
local refuge_camp_cargo_1 = ScenEdit_GetUnit({name = "Refuge camp Santa-maria"}).cargo[1].cargo
local refuge_camp_cargo_2 = ScenEdit_GetUnit({name = "Refuge camp Santa-maria"}).cargo[2].cargo

print(refuge_camp_cargo_1)

for k, v in pairs (refuge_camp_cargo_1) do
if (refuge_camp_cargo_1[k].dbid == medical_supply_pallet_id) and (refuge_camp_cargo_1[k].quantity >= medical_supply_pallet_to_deliver) then

supply_deliver = true
else

print(refuge_camp_cargo_2)

for k, v in pairs (refuge_camp_cargo_2) do
if (refuge_camp_cargo_2[k].dbid == medical_supply_pallet_id) and (refuge_camp_cargo_2[k].quantity >= medical_supply_pallet_to_deliver) then
supply_deliver = true
else
end
Norm49
Posts: 95
Joined: Fri Jun 05, 2015 3:59 pm

Re: LUA trigger when base recive cargo

Post by Norm49 »

ok the last one didn't work as cargo[2].cargo doesn't work. So if there is only one cargo type then I can make it work but if there is 2 cargo type, stranded personal and the pallet in this case then i doesn't work.

Any idea.
I keep trying when waiting for a answer,

but I have people working on my house foundation, guys trying to learn how to script to the noise of a jackhammer is the recipe for headache :lol:

edit; if there is more then 1 cargo with the same id it doesn't work. so if i have 10 cargo of id 9 and i do
local quantity = 1 -- it work
local quantity = 5 -- it doesn't work

in boat case i have the check (cargo[k].quantity >= quantity)
User avatar
lumiere
Posts: 267
Joined: Tue Mar 19, 2019 10:38 am

Re: LUA trigger when base recive cargo

Post by lumiere »

local dbid = 9 -- create a local variable call dbid equal to 9 were 9 is the id of the unit in the data base i use for the scenario
local quantity = 50 -- create a local variable call quantity equal to 50 in this case to number of object with the data base id 9 I want to be move
--Yes

local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo -- create a local variable call cargo equal to the fist listed cargo element contain in the unit name blue arifield1
print(cargo) -- print all the detail of the fist cargo element of blue airfield1 but in this case for the script it is actually the value of the local variable call cargo
--Actually, this calls not "first cargo element" in cargo table. See below.

for k, v in pairs (cargo) do -- call 2 element of the printed table of the local variable call cargo K and V. (I don't understand why K and V also next V is not use.)

if (cargo[k].dbid == dbid) and (cargo[k].quantity >= quantity) then -- check if the data base id of the cargo element is the same as the one define in the local variable dbid and check if the amount is superior or equal to the one define in the local variable quantity (Why use only k and v is not call?)
---Not profound reason :) you can use v.dbid or something with no problem.

return true --or do something -- place to execute my script
end -- end the script
end -- i don't know with to end it more then one time
---first end closes if statement. second one closes for loop.
Well... As you noticed, my script at #2 post actually had bit of flaws.

--I counted container itself, not actual container contents. Is there a way to count such contents? As this is far beyond my skill, so here I will stick to how to count container.
--next(cargo) ~= nil check should have included. Using this script against unit without any cargo gets error.
edit; if there is more then 1 cargo with the same id it doesn't work. so if i have 10 cargo of id 9 and i do
local quantity = 1 -- it work
local quantity = 5 -- it doesn't work
--Yup. Container cargo are stored separately, i.e. instead of x50 pallet entry, there are 50 separete single pallet entry. So, counting each quantity always end up with 1.
local cargo = ScenEdit_GetUnit({name = "Blue Airfield1"}).cargo[1].cargo -- create a local variable call cargo equal to the fist listed cargo element contain in the unit name blue arifield1
ok the last one didn't work as cargo[2].cargo doesn't work. So if there is only one cargo type then I can make it work but if there is 2 cargo type, stranded personal and the pallet in this case then i doesn't work.
-- Related to this, there needs some complicated explanation why I used cargo[1].cargo. In short, this calls cargo table itself, not first cargo item.

For example, run this script without [1].cargo to get “parent” cargo table.

Code: Select all

local cargo = ScenEdit_GetUnit({name = "Blue Airfield2"}).cargo
print(cargo)
You will get following result (line break for ease of view):

Code: Select all

{
  [1] = { guid = 'L1VD8C-0HN0UETMI2767', name = 'Blue Airfield2',
  cargo = {
    [1] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAM', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
    [2] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAO', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
    [3] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAQ', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
    …
    [20] = { mass = 0.0540000014007092, Type = 4, quantity = 1, crew = 0, status = 'Operational', guid = 'L1VD8C-0HN1DDH7SHTAS', name = 'Pallet Container', dbid = 9, area = 1.23870730400085 },
  }
}
Note that “actual” cargo table listing each cargo items is sub-table placed under [1] = {guid='L1VD8C-0HN0UETMI2767', name = 'Blue Airfield2', cargo = ...} value table. Using cargo[1].cargo can call this cargo table directly.

By the way, path to second cargo should be:
(Unit wrapper).cargo[1].cargo[2]
And not
(Unit wrapper).cargo[2]
Last edited by lumiere on Wed Feb 14, 2024 6:34 pm, edited 2 times in total.
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
User avatar
lumiere
Posts: 267
Joined: Tue Mar 19, 2019 10:38 am

Re: LUA trigger when base recive cargo

Post by lumiere »

Here is reworked script.

Function define

Code: Select all

function CargoCheck(UnitName, CargoDBID, Quota)
--UnitName = Unit to check cargo
--CargoDBID = DBID of target cargo (former dbid)
--Quota = how many target cargo needed (former quantity)

  local cargo = ScenEdit_GetUnit({name = UnitName}).cargo
  print(cargo)

  if next(cargo) ~= nil then --cargo table is not empty (unit has some cargo)
    local qty = 0 --number of target cargo which unit actually has
    for k, v in pairs (cargo[1].cargo) do
        if v.dbid == CargoDBID then --if cargo entry has target dbid, add qty to 1
          qty = qty + 1
        end
    end
    print("There are " ..qty.. " target cargo.")
    if qty >= Quota then
      print("Unit have plenty # of target cargo.")
      return true
    else
      print("Unit have not enough # of target cargo.")
      return false
    end
  else --cargo table is empty (unit has no cargo)
    print("unit has no cargo!")
      return false
  end
end
Run function

Code: Select all

return CargoCheck("Blue Airfield1", 9, 50)
Test scenario attached. At start, Airfield1 (x100 cargo) returns true, while Airfield2 (x20 cargo) and Airfield3 returns false. As cargo mission completed and cargos are transferred to Airfield2, event triggers around 1200hrs.

If you want to check another type of cargo, just run the function again with other CargoDBID value.

Code: Select all

return CargoCheck("Blue Airfield1", 18, 20)
Hope this helps.
Attachments
Cargo test.zip
(12.79 KiB) Downloaded 5 times
"How Do You Stay Calm With A 7,000 Ton Nuclear Predator Listening For Your Heartbeat?"
Post Reply

Return to “Mods and Scenarios”