Change UnitAI with Python

Avatar Of Woe

Chieftain
Joined
Nov 20, 2011
Messages
5
Oh wise and great masters of Civ4 modding, help this humble modder.

I made a spell that summons a worker unit, but the summoned unit is given UNITAI_ATTACK, so he loses all the nice automation that workers should have (RouteTo and such).

So I figured I could use onUnitCreated to give the right AI to the little guy. Sounds easy, right? I thought so, too, once.

So my question: what is the code that would make this work?

Here's what I've been trying so far (unsuccessfully):

Code:
from CvPythonExtensions import *
from BasicFunctions import *
import PyHelpers

gc = CyGlobalContext()

def onUnitCreated(self, argsList):
   pUnit = argsList[1]
   if pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_WORKER"):
      pUnit.setUnitAIType(UnitAITypes.UNITAI_WORKER)

Any advice?
 
Hi,

The default DefaultUnitAI for a worker is UNITAI_WORKER, so I'm surprised you're getting something else.

I shouldn't think you need to mess around with onUnitCreated. You can have a specific python callback, <PyResult> within any spell.
If creating a unit in python, the it's something like,
Code:
	pPlot = caster.plot()
	newUnit = pPlayer.initUnit(gc.getInfoTypeForString('UNIT_WORKER'), pPlot.getX(), pPlot.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)

I've always used NO_UNITAI for the UnitAITypes but in your case you could try UNITAI_WORKER.
 
In the castCreateUnit() function in the DLL, it's hardcoded to give UNITAI_ATTACK to all summoned units. This can be changed to NO_UNITAI, which will give them their default AI (which I would recommend), though you then have to make sure that all summoned units have an appropriate default AI, and also catch special cases such as Animals, which behave differently under Barb control than they would under an AI civ's control.

Edit: Alternate option is as Lplate suggested, and that is creating the unit yourself in python.
 
Thank you, kind internet peoples. Lplate's suggestion worked pretty well with NO_UNITAI to create a unit with DefaultAI .

Next question is how to add a unit to a casters list of summons?

I can add it as a minion to the commander list with "caster.addMinion(newUnit)", but I'd rather have it as a summons.

If it's not possible, is there another way I can kill these summoned workers when the caster dies and restrict the number of summoned workers to 1?
 
Me again. Still having trouble getting new units (made in python) to behave as Summoned units.

Using the dll from FallFurther (only one I could find), I found these lines in CVUnit.cpp under castCreateUnit:

Code:
    pUnit->setSummoner(getID());
    pUnit->setMasterUnit(getIDInfo());
    addSlaveUnit(pUnit->getID());

So the questions:
Can these commands be accessed from python?
And what's the python syntax that would make them work?
 
I don't have the code in front of me right now, but off the top of my head: It is accessible, and you want to check the Dominate python. Dominated units are made summons/slaves of the caster, IIRC. May just be minions, but the two are near identical; Should be able to replace minion with slave.
 
Thanks, but Dominate uses addMinion(). It look like I'm the only person to try setSummoner(), though getSummoner() is used in CallForm.

I've tried:
caster.addSlaveUnit(pUnit)
caster.setMasterUnit(pUnit)
caster.setSummoner(pUnit)

and I've tried swapping pUnit and caster. Nothing seems to work, so I'm out of ideas. Internet? Any clues?
 
Ah, that explains it.

So I can 1) go through the normal XML summoning method, then try to change the unit_ai in python. Does the XML fire before the PyResult command?

Or 2) use addMinion() and try to disable the "Leave Army" command. Is that possible?
 
Top Bottom