Unloading cargo from facilities - Two companion scripts

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

Hello,

Let me propose you these two scripts about unloading cargo from facility. I'm using them both in the latest versions of Gaddafi's Legacy and The End Of FranceAfrique.

You can just add these scripts as two different special actions. Just copy-paste the code in a special action Lua script

The two scripts are:
1) Unload Cargo from base
To unload cargo select one unit containing cargo at a base (hangar, tarmac space, ...) and execute the special action.
All cargo of one type will be unloaded next to the selected unit (hangar, tarmac space). You can then use the cargo like any ground units. Once the cargo is unloaded, select all the stack and give a move order [F3] or come pickup the units with a helicopter. You can then use the "group units per class" special action (see below) to group unloaded cargo items by class (arty, armored, mech inf, inf) so that they are easier to handle
2) Group units by class
Use this special action to group units by class (armored, arty, infantry, ...). Particularly useful when you need to group the cargo units you just unloaded from a CARGO hangar with the above script "Unload Cargo from base". Select all units in a stack after unloading cargo and run the script. The stack will be split into several groups, one per unit class.
--------------
Disclaimer: I'm new at Lua with CMO and I'm definitely not a pro-coder so the scripts are certainly not the best possible ones, so excuse my non-optimized way of coding. However, they seem to be functional. And yes, I'm not using functions (yet) :-( I however think these scripts can be useful to scenario creators who are not yet using Lua to create scripts.

Feedback welcomed ! :-)

Thank you to Knighthawk75 for explaining me the algorithm to count items in a table. However, the under-optimized code remains 100% mine with all the possible errors it contains.
--------------

1) Unload Cargo from base

Code: Select all

local selectedUnit = ScenEdit_SelectedUnits()

local s = 0

if selectedUnit.units ~= nil then
  for _ in pairs(selectedUnit.units) do 
  s = s+1 
  end
else

ScenEdit_MsgBox ("ERROR: you selected an ennemy contact. Select one single unit containing cargo (hangar/Tarmac space, etc) and try again. \n\nPress 'OK' to CLOSE this window" , 1)
return 
end 

if s > 1 then 
ScenEdit_MsgBox ("ERROR: more than 1 unit is selected. \n\nPress 'OK' to CLOSE this window" , 1)
return
end

--should work only on facilities (hangars, tarmac speces, etc)
local unit = ScenEdit_GetUnit({guid=selectedUnit.units[1].guid})
if unit.type ~= "Facility"  then 
ScenEdit_MsgBox ("ERROR: The unit you selected is not a facility. \n\nPress 'Ok' to CLOSE this window" , 1)
return
end

local comp = ScenEdit_GetUnit({guid=selectedUnit.units[1].guid}):filterOnComponent('Cargo')
---------------------
  if (comp == nil) or type(comp) ~= "table" then print("Missing or nil table parameter.")
  return {}
  end
  local cargoTable = {}
  local cargoTableName = {}

  for l =1, #comp do
    if cargoTable[comp[l].comp_dbid] ~= nil then 
      cargoTable[comp[l].comp_dbid]= cargoTable[comp[l].comp_dbid] + 1 
      cargoTableName[comp[l].comp_name] =cargoTableName[comp[l].comp_name] + 1

    else
      cargoTable[comp[l].comp_dbid] = 1 
      cargoTableName[comp[l].comp_name] = 1
    end
  end
---------------------
for k,v in pairs (cargoTable) do 

local name
for j = 1, #comp do
if comp[j].comp_dbid == k then name =  comp[j].comp_name end 
end

local x = ScenEdit_MsgBox ("Do you want to unload ".. v .." units of  ".. name .." ? \n\n WARNING: This can't be undone ! " , 4)

          if x == "Yes" then
          ScenEdit_UnloadCargo(selectedUnit.units[1].guid,{{v,k}})
          
local y = ScenEdit_MsgBox ("You SUCCESSFULLY unloaded ".. v .." units of : "..name.." - \n\n Press 'OK' to CLOSE this window and proceed with the next item or press 'CANCEL' to abort the script" , 1)
if y == "Cancel" then return end
          else 
          
local z = ScenEdit_MsgBox ("Cargo "..name.." WON'T BE UNLOADED. Run the speacial action again later. \n\nPress 'OK' to CLOSE this window and proceed with the next item to unload or press 'CANCEL' to ABORT the script" , 1)
if z == "Cancel" then return end
          end
          end
          
ScenEdit_MsgBox ("No more cargo to unload \n\nPress 'Ok' to CLOSE this window" , 1)


And the second script (companion script to the first one):
Group units by class

Code: Select all

math.randomseed( os.time() ) --used for generating a random group number
-- variables declaration
local selectedUnit = ScenEdit_SelectedUnits()
local s = 0
local myUnit = { }
local myClassTable = { }
local indic = 0
local groupNum = string.sub((tostring(os.time()) .. tostring(math.random(10,99)) .. " from cargo"),8) -- creates number to name the groups 
--counts number of units in selection for tests
if selectedUnit.units ~= nil then
  for _ in pairs(selectedUnit.units) do 
  s = s+1 
  end
else
ScenEdit_MsgBox ("ERROR: you selected enemy contact(s). Select the units you unloaded from your CARGO facility (hangar/Tarmac space, etc) that you want to group and try again. \n\nPress OK to CLOSE this window" , 1)
return 
end 

for k = 1, s do
local myUnit = ScenEdit_GetUnit({guid=selectedUnit.units[k].guid})
if myUnit.type ~= "Facility"  then 

ScenEdit_MsgBox ("ERROR: Some of the units you selected are not facilities \n\nPress OK to CLOSE this window" , 1)
return
end
end

if s < 2  then 
ScenEdit_MsgBox ("ERROR: more than 1 unit must be selected. \n\nPress OK to CLOSE this window" , 1)
return
end

--should work only on facilities 
--grouping

for a = 1, s do

local myUnit = ScenEdit_GetUnit({guid=selectedUnit.units[a].guid})

if myUnit.classname ~= nil then
table.insert(myClassTable,myUnit.classname) --building classnames table
end 
end

for j =1 , s do

local myUnit = ScenEdit_GetUnit({guid=selectedUnit.units[j].guid})

    if myClassTable[j] == myClassTable[j-1] then
if myUnit.subtype == "5001" or myUnit.subtype == "5002" then 
myUnit.group = tostring(myClassTable[j]) .." Group #".. groupNum
indic = indic + 1
end
    else
if myUnit.subtype == "5001" or myUnit.subtype == "5002" then 
myUnit.group = tostring(myClassTable[j]) .. " Group #" .. groupNum 
indic = indic + 1
end
end
end

if indic >0 then 
ScenEdit_MsgBox ("SUCCESS: Selected units have been grouped by class. \n\nSwitch to GROUP view to use the groups you just created \n\nPress OK to CLOSE this window" , 1)
else
ScenEdit_MsgBox ("FAILURE: No units have been grouped. This script works only with Mobile Vehicles and Mobile Personnel. Select units you want to group and try again\n\nPress OK to CLOSE this window" , 1)
end

One advice for scenario creators: when creating a scenario with cargo, if you plan to use the two above scrips, clearly identify the facilities containing CARGO in their names for the user to easily find them (well, it might also give a hint to the enemy so think twice :-)
CARGO identified.jpg
CARGO identified.jpg (204.42 KiB) Viewed 619 times



Please let me know if you find issues.
Last edited by nukkxx5058 on Thu May 05, 2022 7:06 pm, edited 7 times in total.
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

yes, I have the "[ i ]" sequence in my code which is interpretated as HTML. I need to fix it as it won't display correctly in the forum post !!!
PLEASE WAIT
Last edited by nukkxx5058 on Thu May 05, 2022 11:11 am, edited 1 time in total.
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

Ok, I fixed it, replaced with [l] and [a] . hope it's OK now. Let me know if you encounter problems. Morality : chose more appropriate variable names :-)

Note that some variables for the "FOR" loops are not declared. hence they are considered global. Check if appropriate.
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Unloading cargo from facilities - Two companion scripts

Post by KnightHawk75 »

nukkxx5058 wrote: Thu May 05, 2022 10:43 am yes, I have the "[ i ]" sequence in my code which is interpretated as HTML. I need to fix it as it won't display correctly in the forum post !!!
PLEASE WAIT
There is a specific forum tag for code it's the < / > icon, if you use it your code stays untouched, and it makes copy and pasting with retained formatting much easier.

Code: Select all

--example
for i=1,10 do 
  print('hello' ..tostring(something[i]));
end
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

Ah good to know. I suspected there might be something ...

test:

Code: Select all

--example
for i=1,10 do 
  print('hello' ..tostring(something[i]));
end
*edit: ok it works, I edited my original post to use it. Thanks !!
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

I wrote a short note with screenshots on how to use the two scripts.
Have a look and decide if it's for you.

Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
User avatar
ClaudeJ
Posts: 754
Joined: Wed Mar 08, 2006 5:38 pm
Location: Bastogne

Re: Unloading cargo from facilities - Two companion scripts

Post by ClaudeJ »

Holy crap! I've been longing to get the shipment out of their delivery room, for some fresh air and the occasional fight for ages! I was thinking about how best to make a suggestion that made sense, and you just came up with a solution.

THANK YOU NUKKXX :!:

Thanks to your nifty script, one can replicate the 1973 air bridge to Israel, eg. 8-)

Now, the icing on the cake would be to do the opposite : to load a unit as cargo into a facility.
Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz, NVIDIA GeForce GTX 1650 4 Go, Windows 10 64bits, 32 GB RAM, Regional settings = French, Belgium
(Previously known as JanMasters0n)
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Unloading cargo from facilities - Two companion scripts

Post by nukkxx5058 »

ClaudeJ wrote: Fri May 06, 2022 8:12 am Holy crap! I've been longing to get the shipment out of their delivery room, for some fresh air and the occasional fight for ages! I was thinking about how best to make a suggestion that made sense, and you just came up with a solution.

THANK YOU NUKKXX :!:

Thanks to your nifty script, one can replicate the 1973 air bridge to Israel, eg. 8-)

Now, the icing on the cake would be to do the opposite : to load a unit as cargo into a facility.
Thanks ! I agree that being able to load cargo into facilities would be cool too ... :-)
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
Post Reply

Return to “Lua Legion”