Python Questions: Text manipulation + Adding unit Movement Limitation

ruff_hi

Live 4ever! Or die trying
Joined
Oct 24, 2005
Messages
9,135
Location
an Aussie in Boston
I have a couple of python questions.

Stuff I can do / already have done
I've randomly (I have code to do the random part) added a direction to the name of my units (eg for south). The direction will depend on the type of unit and will be randomly assigned. EG. A worker will get [F] (free movement) 80% of the time and one compass direction the other 20% of the time (5% each direction). So, 5% of my workers with have [N], 5% , etc at the end of their name.

Stuff I need help with
  • How do I parse a unit name (ie test if the name ends with ']' so that I can tell if I have already given a unit a name? (in VBA I would just use right(name,1) = "]")
  • I also need to be able to extract the actual direction (in VBA, the mid function)
  • How do I determine the type of unit it is (ie worker, archer, melee, etc)?
  • How do I restrict a unit with '[N]' at the end of its name from moving North?
  • I also need to turn that restriction off if the unit is on a boat (ie a boat carrying a '[N]' unit can move north).

Why do I want to do this? I'm putting together this mod so that I can play a really stupid Succession Game where your units have restricted movement.
 
How do I parse a unit name (ie test if the name ends with ']' so that I can tell if I have already given a unit a name? (in VBA I would just use right(name,1) = "]")

ex: txt = "exampl" , txt[-1:] return "l"

I also need to be able to extract the actual direction (in VBA, the mid function)

ex: txt = "exampl" , txt[-2:-1] return "p"
ex: txt = "ab,ar,ac,redf" , txt.split(",") return ["ab","ar","ac","redf"]

How do I determine the type of unit it is (ie worker, archer, melee, etc)

How do you get the name of the unit ? do you want to get the type string ie: "UNIT_SETTLER" ?

How do I restrict a unit with '[N]' at the end of its name from moving North?

I don't think it's possible just in python , the sdk function is not exposed in python . But i'm not sure of that

Tcho !
 
To get a unit name ...

PHP:
def onUnitMove(self, argsList):
	'unit move'
	pPlot,pUnit = argsList
	player = PyPlayer(pUnit.getOwner())
	unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())

	# get the description
	#zsName = unitInfo.getDescription()

	# get the unit name (no desc means that you don't get the '(settler)' part
	if pUnit.getNameNoDesc() == "":
		zsName = pUnit.getName()
	else:
		zsName = pUnit.getNameNoDesc()
 
I don't think it's possible just in python , the sdk function is not exposed in python . But i'm not sure of that
What about a work around ... can I get a units X,Y (pre movement) and if they made an invalid move, then move them back to the original tile? If so, how do I move a unit via python?
 
There is no vanilla way in python to find out a units previous x/y or to stop them from being able to move into a type of tile. Unfortunatly to restrict a units movement options you have to use the sdk.
 
Here is what I am planing to attempt ...

  • store with each unit the unit's movement restriction (I can do this)
  • store with each unit the unit's current X,Y location (I can do this)
  • in the 'onunitmove' action, compare the unit's current location with the stored location and the movement restriction. If the movement restriction has been violated, then move the unit back to the original location (I think I can do this)

I think this should work. Any comments?
 
Here is what I am planing to attempt ...

  • store with each unit the unit's movement restriction (I can do this)
  • store with each unit the unit's current X,Y location (I can do this)
  • in the 'onunitmove' action, compare the unit's current location with the stored location and the movement restriction. If the movement restriction has been violated, then move the unit back to the original location (I think I can do this)

I think this should work. Any comments?

Unfortunately, once the unit moves, they will have already done combat / movement changes / line of sight changes / etc. So, moving it back might not have the desired effect you want.
 
Unfortunately, once the unit moves, they will have already done combat / movement changes / line of sight changes / etc. So, moving it back might not have the desired effect you want.
Hmm - good point re combat. I was thinking of just moving it back and the owner effectively losing the movement for that unit for that turn. I will think about the combat issues.
 
Actually the tutorial that came with civ prevents your units from moving too far west. It accomplishes this by using a PYTHON function in CvGameUtils.py. I think it's called canMoveTo, not sure. Now if you change the return from False to True it will prevent all units from ever moving (:D) or you could use logic code to see if the unit can move to the plot passed in the args.
 
Actually the tutorial that came with civ prevents your units from moving too far west. It accomplishes this by using a PYTHON function in CvGameUtils.py. I think it's called canMoveTo, not sure. Now if you change the return from False to True it will prevent all units from ever moving (:D) or you could use logic code to see if the unit can move to the plot passed in the args.
Ahh - so it does. It also turns that off after a while so that Rome can meet India. I will have a look at India to test that. Thanks for the info.
 
Actually the tutorial that came with civ prevents your units from moving too far west. It accomplishes this by using a PYTHON function in CvGameUtils.py. I think it's called canMoveTo, not sure. Now if you change the return from False to True it will prevent all units from ever moving (:D) or you could use logic code to see if the unit can move to the plot passed in the args.

Is this the function you meant ...

PHP:
	def cannotSelectionListMove(self,argsList):
		pPlot = argsList[0]
		bAlt = argsList[1]
		bShift = argsList[2]
		bCtrl = argsList[3]
		return False

notes to self:
this is used on a selection, not a unit and I am guessing that the plot is the destination plot, not the current plot. Thus I will need to get the units selected (CyInterface().getHeadSelectedUnit() or CyInterface().getSelectedUnit() - what is the difference) and check if this unit can move to the selected tile. Need to do this check within a modified 'cannotSelectionListMove'. If it can, return false. If it cannot, return true. I guess that this will put a red circle on the tile. Can check by creating a customassets version of this file and changing 'return false' to return true' - this should freeze all units.

Edit: I tested this and it stops the movement of units using the right click option but you can still use the goto option.

Any ideas anyone?
 
do you want to get the type string ie: "UNIT_SETTLER" ?
How do I go this? I am hoping that the units are broken down into settler, worker, archery, mounted, melee, gunpowder, etc - that is the info that I am looking for.
 
if you have an unit (pUnit) you can get the type string like that :

Code:
CvUtil.convertToStr(gc.getUnitInfo(pUnit.getUnitType()).getType())
For an example you get UNIT_SETTLER if pUnit is a settler .

TCho !
 
Back
Top Bottom