Facility Shut Down

All discussions & material related to Command's Lua interface

Moderators: michaelm75au, angster, RoryAndersonCDT, MOD_Command

Post Reply
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

Facility Shut Down

Post by kevinkins »

Is there a function or two that will control an artillery unit as if they are having system problems? “Turn off/on” Looked through the functions and search the forum a bit this morning and did not find anything yet.

Thanks. Kevin
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Facility Shut Down

Post by KnightHawk75 »

This specifics of the facility may matter, but have you tried imparting damage or destroyed status on what you want disabled or impacted (be that a sensor, mount, or other component), or alternatively (harder with aircraft in the air) remove all quantity of weapons you don't want firing till you otherwise set it back.

So for a simple example lets say you have a Single Unit Airfield and a M777 or Ceasar and you wanted to disable any aircraft launching due to temporary weather or whatever, and you wanted to disable the m777 or Ceasar from firing temporarily without wanting to track ammo and not caring about existing operational status. We'll further assume one doesn't care if there is destruction to any aircraft\boats that are technically presently hosted on runway or rap at the moment of execution, though this warning would apply more to hangers and parking spaces etc(not in the example) but you have been warned.

Code: Select all

---// function to set ALL or first matching components matching a name-contains string:find() pattern to a specified operational status.
---@param u CMO__UnitWrapper - The unit wrapper to operate upon.
---@param thecompname string - the string.find() pattern to search the component name for containing. 
---@param thestatus string - the operational status string code 'none','destroyed','light' or 'heavy'
---@param firstonly? boolean - optional (required if log param will be specified as well) defaults to false, controls of only first-match search is done. 
---@param log boolean - defaults to false, true to enable successful operation logging.
---@return boolean - true if no errors, false upon at least 1 error even if somoe successes.
local function SetComponentStatusByNameContains(u,thecompname,thestatus,firstonly,log)
    if u==nil or thecompname == nil or thestatus==nil then print('invalid params. unit wrapper,component name, and string status required.'); return false; end
    if (firstonly == nil) or type(firstonly) ~='boolean' then firstonly=false; end 
    if log==nil then log=false; end
    local retval1,retval2,eflag = false,nil,true;
    for _,v in ipairs(u.components) do
      retval1,retval2=false,nil;
      if v.comp_name:find(thecompname) ~= nil then --user can use string.find special indicators if needbe.
        retval1,retval2 = pcall(ScenEdit_SetUnitDamage,{guid=u.guid, components={{v.comp_guid,thestatus}}});
        if retval1 == false then --log error
            print('failed to set operation status to ' ..tostring(thestatus).. ' on  component: ' .. tostring(v.comp_name)); eflag=false;
        elseif log then
            print(tostring(v.comp_name) .. ' set to ' .. thestatus);
        end
        if firstonly then break; end
      end
    end
    return eflag
end

---// function to set ALL or first matching mounts matching a name-contains string:find() pattern to a specified operational status.
---@param u CMO__UnitWrapper - The unit wrapper to operate upon.
---@param themountname string - the string.find() pattern to search the component name for containing. 
---@param thestatus string - the operational status string code 'none','destroyed','light' or 'heavy'
---@param firstonly? boolean - optional (required if log param will be specified as well) defaults to false, controls of only first-match search is done. 
---@param log? boolean - optional defaults to false, true to enable successful operation logging.
---@return boolean - true if no errors, false upon at least 1 error even if somoe successes.
local function SetMountStatusByNameContains(u,themountname,thestatus,firstonly,log)
    if u==nil or themountname == nil or thestatus==nil then print('invalid params. unit wrapper, mount name, and status string required.'); return false; end
    if (firstonly == nil) or type(firstonly) ~='boolean' then firstonly=false; end 
    if log==nil then log =false; end
    local retval1,retval2,egflag= false,nil,true;
    for _,v in ipairs(u.mounts) do
      retval1,retval2=false,nil;
      if v.mount_name:find(themountname) ~= nil then --user can use string.find special indicators if needbe.
        retval1,retval2 = pcall(ScenEdit_SetUnitDamage,{guid=u.guid, components={{v.mount_guid,thestatus}}});
        if retval1 == false then --log error
            print('failed to set operational status to '.. tostring(thestatus) .. ' on mount: ' .. tostring(v.mount_name)); eflag=false;
        elseif log then
            print(tostring(v.mount_name) .. ' set to ' .. thestatus);
        end
        if firstonly then break; end
      end
    end
    return eflag
end

--usage example:
local sua = ScenEdit_GetUnit({guid='4FH7PU-0HMIGRLV5HQ8V',name="Blue_SUA_1"}); --replace with whatever unit
local art = ScenEdit_GetUnit({guid='4FH7PU-0HMIGRLV5HQES',name='#2401 155mm/52 Caesar'}); --replace with whatever unit

SetComponentStatusByNameContains(sua,'Runway','destroyed',nil,true); -- as of db490 should cover all runway\runway-taxiway\and raps.
SetMountStatusByNameContains(art,'155mm/','destroyed'); -- example covering most 155mm entries, like caesar\m777\m109 etc.
-- check DamageCtrl screen for said units here you will see destroyed status without rest of unit destroyed...
SetComponentStatusByNameContains(sua,'Runway','none',false,true) ;
SetMountStatusByNameContains(art,'155mm/','none');
-- check DamageCtrl screen for said units here you will see operational status restored...
print('done');
-- This is just a very simple example. It does not take into account a full solution,
-- that say tracks the state of said component\mount\sensor before hand, keeps that state data, and then returns
-- the state to what it was at the time of disabling nor does it cover the saving of that global state.
-- It just a simple how do you change the values example. Also it does not try to completely maximize speed of execution\compare.
Hope that hopes, and if you are wonder why the two functions when technically in the above case you could just use components version to do the mounts, the primary is there will be less to unnecessarily check with 'mounts' version then all components, hence better performance.
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

Re: Facility Shut Down

Post by kevinkins »

Thank you. Will check out the code later today. "imparting damage or destroyed status" to the artillery units and then resurrecting them did come to mind. Or just teleporting them in and out of range of the battlefield. But teleporting is a last resort. I wonder if the game will automatically fix components damaged via lua or does that take additional code?

Kevin
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
rneum
Posts: 17
Joined: Fri Nov 12, 2021 4:56 pm

Re: Facility Shut Down

Post by rneum »

Have you tried setting the weapon_control_status_land? http://commandlua.github.io/
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

Re: Facility Shut Down

Post by kevinkins »

Thanks for the suggestion. Looks like weapon_control_status_land is associated with the ScenEdit_SetDoctrine() function since there are just 4 mentions in the documentation. Gave it a try and no error was created. But the weapon still can fire its ammo (Manually). ScenEdit_SetDoctrine({side="Blue", unitname="Arty Plt (HIMARS)", weapon_control_status_land = "Hold" }) Replace Hold with a 2 and the same thing.

Kevin
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Facility Shut Down

Post by KnightHawk75 »

kevinkins wrote: Sat Jun 18, 2022 10:04 am Thank you. Will check out the code later today. "imparting damage or destroyed status" to the artillery units and then resurrecting them did come to mind. Or just teleporting them in and out of range of the battlefield. But teleporting is a last resort. I wonder if the game will automatically fix components damaged via lua or does that take additional code?

Kevin
It will not if it's 'destroyed' like in the sample. it will if say you used 'heavy' for heavily damaged (but heavily damaged may still fire so I didn't use that example).

Yeah you could use wcs hold and weapon doctrine change temp to 'do not use' for like everything for most cases too ... but you might find it might fire or turn on radars etc in self defense even if doctrine says no, or can be manually over-ridden (by actual human player), unless otherwise forced and have every setting just right. For example sometimes for a sensor to stay TF off the way you actually want it too only way to do it is destroy it temporarily or cut a comm link etc... but again will not be case for most artillery.

But yeah if you looking to remove firing option from a human player, playing outside the editor, I'd go with disable-destroying the mounts, and then re-enabling them, that is if the fact some of those mounts could get erroneously re-enabled (ie were actually damaged before hand) isn't much a problem for you scene. If it is and you need to do such in a stateful way hit me up for another far more extended example. ;)
Post Reply

Return to “Lua Legion”