Unloading cargo from facilities - Two companion scripts
Posted: Thu May 05, 2022 10:37 am
				
				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
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.
  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
And the second script (companion script to the first one):
Group units by class
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  
  
Please let me know if you find issues.
			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
2) Group units by classTo 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
--------------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.
  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
 
  Please let me know if you find issues.
 
 