Disable Unit AI via LUA

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Disable Unit AI via LUA

Post by KnightHawk75 »

from http://www.warfaresims.com/?p=4990
Lua loco

The Lua API continues its expansion in CMO and offers additional hooks into the simulation engine as well as various methods for pulling the strings of the running scenario. One of the new hooks ties directly into the AI model: You can individually instruct units to, quite literally, not think for themselves (You in the back, quipping “you mean they do this now?” – SIT DOWN!). More specifically, you can set individual units to skip their AI routines for evaluating valid targets and picking out the primary one among them. This has two direct benefits:

It makes it easier to implement custom targeting AI routines in Lua, since an author not longer has to “compete” with the build-in AI for this.
As these routines are among the most CPU-expensive pieces of the simulation pipeline, disabling them can have a drastic effect on the speed & scalability of a large scenario. For example, if you disable the AI cycles of all static/inactive buildings, then only “active” units will use the CPU for this work (internally Command already does a lot of such optimizations, but since it cannot “intrinsically” know which units are static & inactive, it has to check them, which itself is not free).

Additionally, this ability can allow simulating “dormant” states for units (e.g. units begin a scenario in a “comatose” state, but later because of XYZ they become activated).

Can we get the commandlua.github.io docs updated to document whats involved in doing this or provide it here?

Dimitris
Posts: 14771
Joined: Sun Jul 31, 2005 10:29 am
Contact:

RE: Disable Unit AI via LUA

Post by Dimitris »

Thanks for the heads up! There is also a bug in the current build about this, we'll need to resolve it on the next update.
JPFisher55
Posts: 589
Joined: Sat Nov 22, 2014 7:54 pm

RE: Disable Unit AI via LUA

Post by JPFisher55 »

Can someone post instructions on how to disable AI cycles of static or inactive units?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Disable Unit AI via LUA

Post by KnightHawk75 »

ORIGINAL: Dimitris

Thanks for the heads up! There is also a bug in the current build about this, we'll need to resolve it on the next update.

Ah ok, thanks.
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Disable Unit AI via LUA

Post by Rory Noonan »

Hey guys, sorry for the delay in getting these instructions out. Now that 1115.8 is out, here is a quick guide to using the C:MO 'disable AI' Lua functionality. You will need C:MO build 1115.8 or above to do this.

First we need to retrieve the unit wrapper and assign it to a variable. This example is from the Brother Against Brother standalone scenario:
unit = ScenEdit_GetUnit({name='S 31 Sabalo', guid='8813afed-2d28-4f14-ae3b-3e0c6bb3f780'})

Once we have the unit wrapper assigned to a variable, we can turn on the AI evaluation of targets by setting them to true (on) or false (off).
unit.AI_DeterminePrimaryTarget_enabled = false
unit.AI_EvaluateTargets_enabled = false

SPOILER for Brother Against Brother
If you open Brother Against Brother in the editor, switch to the Venezuelan side, select the S 31 Sabalo and clear its current target by pressing Ctrl+E. Run the scenario, and note that after a couple of seconds of game time the Sabalo automatically selects the nearest appropriate target and goes 'engaged offensive'.

Immediately pause the sim, delete any weapons that were fired, and again clear the target list for Sabalo by pressing Ctrl+E. Now run the code above in the console, which should look like this:
local unit = ScenEdit_GetUnit({name='S 31 Sabalo', guid='8813afed-2d28-4f14-ae3b-3e0c6bb3f780'})
unit.AI_DeterminePrimaryTarget_enabled = false
unit.AI_EvaluateTargets_enabled = false

Observe that the Sabalo no longer goes 'Engaged Offensive', nor does it fire any weapons despite being assigned to a mission that would otherwise have it do so.
Image
JPFisher55
Posts: 589
Joined: Sat Nov 22, 2014 7:54 pm

RE: Disable Unit AI via LUA

Post by JPFisher55 »

Ok, now how do I disable static units? Can I do it by class, so I can disable thousands of units?
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Disable Unit AI via LUA

Post by Rory Noonan »

I've written back to you via e-mail with a detailed explanation of the steps I took, as well as actually doing the work for you and providing you with the finished result and the script I used to get there.

For other's benefit, a quick and easy way of doing this is to get the GUIDs of the units you want to work with by using the VP_GetSide units table.
local listOfGUIDsForAllUnitsOnSide = VP_GetSide({side='USAF'}).units
local typeOfUnitIAmInterestedIn = 'Aircraft'
local listOfUnitsIWantToWorkWith = {}

for k,v in ipairs (listOfGUIDsForAllUnitsOnSide) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.type == typeOfUnitIAmInterestedIn then
table.insert(listOfUnitsIWantToWorkWith,unit)
print ("'"..unit.guid.."', --"..unit.name.." ("..unit.classname..") added to list of units to work with..")
end

end

print ('List complete! '..#listOfUnitsIWantToWorkWith..' units added to list.')

local counter = 0
for k,v in ipairs (listOfUnitsIWantToWorkWith) do
local unit = ScenEdit_GetUnit({guid=v.guid})
unit.AI_DeterminePrimaryTarget_enabled = false
unit.AI_EvaluateTargets_enabled = false
print (unit.name..' AI disabled!')
counter = counter + 1
end
print ('Run complete, AI disabled for '..counter..' units.')

The above example works in the Shamal standalone scenario.

This can be manipulated to be as broad or as specific as you like, using criteria in the unit wrapper.
Image
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Disable Unit AI via LUA

Post by KnightHawk75 »

Nice. Thanks apache85 for the update and the info needed.
Post Reply

Return to “Lua Legion”