[python] get/set unit direction?

davidlallen

Deity
Joined
Apr 28, 2008
Messages
4,743
Location
California
I am using pUnit.convert() to convert one unit into a different unit type. The code is:

pNew = pOwner.initUnit(iType, pOld.getX(), pOld.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_EAST)
pNew.convert(pOld)
pOld.kill(true, iOwner)

It is working. But, notice "EAST". If the unit was facing some other direction, it suddenly spins to face east. This is not fatal, but undesirable.

Is there a way to get the DirectionTypes of a unit, and then set it? There is nothing I can immediately find in the python API which seems relevant. There is CvUnitEntity.getUnitFacingDirection, but it returns a float, and I don't know the relationship between CvUnit and CvUnitEntity. For example, there is no CvUnit.getEntity or similar.

Can anybody suggest how to convert without changing direction?
 
From CyUnitInterface1.h:
Code:
		.def("getFacingDirection", &CyUnit::getFacingDirection, "int ()")
		.def("rotateFacingDirectionClockwise", &CyUnit::rotateFacingDirectionClockwise, "void ()")
		.def("rotateFacingDirectionCounterClockwise", &CyUnit::rotateFacingDirectionCounterClockwise, "void ()")
The Interface is what actually exposes the functions to Python but it's usually easier to read the C++ header since the order's a little weird on the interface.
From CyUnit.h:
Code:
	int getFacingDirection();
	void rotateFacingDirectionClockwise();
	void rotateFacingDirectionCounterClockwise();


CyUnit::getFacingDirection() returns a DirectionType int:
Code:
DirectionTypes:

 -1 = NO_DIRECTION
  0 = DIRECTION_NORTH
  1 = DIRECTION_NORTHEAST
  2 = DIRECTION_EAST
  3 = DIRECTION_SOUTHEAST
  4 = DIRECTION_SOUTH
  5 = DIRECTION_SOUTHWEST
  6 = DIRECTION_WEST
  7 = DIRECTION_NORTHWEST
  8 = NUM_DIRECTION_TYPES
CyUnit::rotateFacingDirectionClockwise() and CyUnit::rotateFacingDirectionCounterClockwise() rotate the unit 45 degrees.

These must be BTS-added functions since they're not listed in Apolyton's online API, but I tested them in BTS 3.17 using the console and they worked as expected.

Note: the rotation may not be fast enough to go completely unnoticed, but setFacingDirection() isn't exposed to Python so that's likely the best you can do.
 
I just realized you may not need to use the rotations at all.

Try something like
Code:
pNew = pOwner.initUnit(iType, pOld.getX(), pOld.getY(), UnitAITypes.NO_UNITAI, pOld.getFacingDirection())
 
I just realized you may not need to use the rotations at all.

Doh! That seems obvious in retrospect. I will try it tonight.

I am also using the apolyton python api doc, which is obviously missing some things. Are you aware of any updated one which shows bts additions? Or, how to create a new one? I gather the apolyton one was generated semiautomatically by some script.
 
You can download the script that made the Apolyton one from http://civilization4.net/files/modding/PythonAPI/genHTML.zip but it still seems geared for warlords/vanilla and didn't work very well for me when I tried it... Zebra9 has released a BTS API (download and access locally) which is similarly laid out. It's missing some classes but it does list the 3 direction functions.
 
Try something like
initUnit(..., UnitAITypes.NO_UNITAI, pOld.getFacingDirection())

Sadly that exact line does not work:
Code:
ArgumentError: Python argument types in
    CyPlayer.initUnit(CyPlayer, int, int, int, CvPythonExtensions.UnitAITypes, int)
did not match C++ signature:
    initUnit(class CyPlayer {lvalue}, int, int, int, enum UnitAITypes, enum DirectionTypes)

In C/C++, that is a "casting" problem which could be resolved like:

(DirectionTypes) pOld.getFacingDirection()

I searched a little to find one possible way to "cast":

DirectionTypes[pOld.getFacingDirection()]

But that fails with:

TypeError: unsubscriptable object

Any idea how to cast int to this DirectionTypes enum?
 
Wow, what an amazing sidetrack that was! :)

Here's what I could do with the script without spending days on it. I've added the missing BTS items and generated the docs. Enjoy.
 
Top Bottom