Disable Unit AI via LUA (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion



Message


KnightHawk75 -> Disable Unit AI via LUA (11/21/2019 3:42:11 AM)

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

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 -> RE: Disable Unit AI via LUA (11/21/2019 7:02:23 AM)

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 -> RE: Disable Unit AI via LUA (11/21/2019 2:13:43 PM)

Can someone post instructions on how to disable AI cycles of static or inactive units?




KnightHawk75 -> RE: Disable Unit AI via LUA (11/21/2019 11:34:09 PM)


quote:

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.




apache85 -> RE: Disable Unit AI via LUA (11/22/2019 9:16:14 PM)

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:

quote:


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).

quote:


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:

quote:


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.




JPFisher55 -> RE: Disable Unit AI via LUA (11/22/2019 9:22:52 PM)

Ok, now how do I disable static units? Can I do it by class, so I can disable thousands of units?




apache85 -> RE: Disable Unit AI via LUA (11/22/2019 10:24:35 PM)

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.

quote:


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.




KnightHawk75 -> RE: Disable Unit AI via LUA (11/22/2019 10:34:05 PM)

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




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
1.953125E-02