few more python questions

gautam

Chieftain
Joined
Dec 13, 2005
Messages
24
Hi,

I am trying to give a new unit on every second turn of the player turn. I have the following code

Code:
def checkPlayerTurnEvents(self, iGameTurn, iPlayer):
		'Check for events on every turn and give a unit on on the 10 turns'
		print "Called checkPlayerTurnEvents"

		if(iGameTurn % 2 == 0):
			popup = PyPopup.PyPopup()
			popup.setHeaderString("NEW UNIT")
			popup.setBodyString("New unit Mechanized Infantry created")
			popup.launch()
			player = gc.getPlayer(iPlayer)
			city = player.getCapitalCity()
			print city.getName()
			
			self.makeUnit(37, iPlayer, city.plot())		
	
	def makeUnit( self, iUnit, iPlayer, plot):
		print plot
		print iPlayer
		print iUnit
		player = gc.getPlayer(iPlayer)
		unit = player.initUnit(iUnit, plot.getX(), plot.getY(), UnitAITypes.UNITAI_CITY_DEFENSE)
		return unit

But however the new unit doesn't go into fortify mode. I would like to know if this is possible. I am assuming the UNITAI_CITY_DEFENSE is fortify. Other thing is as per the python API - unit type Mechanized Infantry is 37 but it seems to make a marine unit. Any ideas on why this could be happening.

Thanks
 
Don't use numbers. They can change. Use gc.getInfoTypeForString('some unit') instead.

As for how to make the unit go into fortify mode I haven't found a way. But it might work with pUnit.doCommand(CommandTypes.COMMAND_WAKE). It's a long shot since it doesn't specificly say fortify but given how fortify and wake seems to be tied together it could work.
 
snarko said:
Don't use numbers. They can change. Use gc.getInfoTypeForString('some unit') instead.

As for how to make the unit go into fortify mode I haven't found a way. But it might work with pUnit.doCommand(CommandTypes.COMMAND_WAKE). It's a long shot since it doesn't specificly say fortify but given how fortify and wake seems to be tied together it could work.

From 12 Monkeys - Tool Collection, note the pGroup.pushMission(MissionTypes.MISSION_FORTIFY, ....) line
Code:
	def doReInit(self):
		iPlayer = PyPlayer(CyGame().getActivePlayer())
		unitList = iPlayer.getUnitList()
		# Loop for all players cities
		for ii in range(len(unitList)):
			pLoopUnit = unitList[ii]
			# get the group the unit belongs to. 
			pGroup = pLoopUnit.getGroup()
			# check if unit is doing air patrol
			if (pGroup.getActivityType() == ActivityTypes.ACTIVITY_INTERCEPT):
				# remove mission (this one shold be obsolete, but who knows ;)
				pGroup.popMission()
				pPlot = pGroup.plot()
				# add new mission -> fortify
				pGroup.pushMission(MissionTypes.MISSION_FORTIFY, 0, 0, 0, false, false, MissionAITypes.NO_MISSIONAI, pPlot, pLoopUnit)
				# add new mission -> air patrol
				pGroup.pushMission(MissionTypes.MISSION_AIRPATROL, 0, 0, 0, false, false, MissionAITypes.NO_MISSIONAI, pPlot, pLoopUnit)
 
To TheLopez :

I tried that but it still isn't working. I have the following code based on what you mentioned :-

Code:
bResult = unit.canFortify()
if(bResult == True):		
	group = unit.getGroup()
             plot = group.plot()
	print plot
	group.pushMission(MissionTypes.MISSION_FORTIFY, 0, 0, 0, false, false, MissionAITypes.NO_MISSIONAI, plot, 0)

Now I even tried the doCommand(CommandType.WAKE) but I don't think this would be the case. It definitely didn't work. This seems like a command to wake up a unit put to sleep which would make more sense. And fortify would probably be a mission rather than a command I suppose. I suppose this could have a close relation with sentry as in wake up if an enemy is near but I doubt its anywhere related to Fortify.

On coming to the getInfoTypeForString I tried gc.getIntoTypeForString("MECHANIZED_INFANTRY") and it crashed the game. Any ideas on where I could get information on what strings are used for which units.
 
gautam said:
On coming to the getInfoTypeForString I tried gc.getIntoTypeForString("MECHANIZED_INFANTRY") and it crashed the game. Any ideas on where I could get information on what strings are used for which units.

I think you need to make it UNIT_MECHANIZED_INFANTRY

you use the string inside the type tags.
 
gautam said:
To TheLopez :

Now I even tried the doCommand(CommandType.WAKE) but I don't think this would be the case. It definitely didn't work. This seems like a command to wake up a unit put to sleep which would make more sense. And fortify would probably be a mission rather than a command I suppose. I suppose this could have a close relation with sentry as in wake up if an enemy is near but I doubt its anywhere related to Fortify.

There is no such command as fortify in the CommandType list here is the complete list from the Python API:
-1 = NO_COMMAND
0 = COMMAND_PROMOTION
1 = COMMAND_UPGRADE
2 = COMMAND_AUTOMATE
3 = COMMAND_WAKE
4 = COMMAND_CANCEL
5 = COMMAND_CANCEL_ALL
6 = COMMAND_STOP_AUTOMATION
7 = COMMAND_DELETE
8 = COMMAND_GIFT
9 = COMMAND_LOAD
10 = COMMAND_UNLOAD
11 = COMMAND_UNLOAD_ALL
12 = COMMAND_HOTKEY
13 = NUM_COMMAND_TYPES

The pGroup.pushMission(MissionTypes.MISSION_FORTIFY, 0, 0, 0, false, false, MissionAITypes.NO_MISSIONAI, pPlot, pLoopUnit) has been working in many mods now so I do not see any reason why it shouldn't be working for you.

gautam said:
On coming to the getInfoTypeForString I tried gc.getIntoTypeForString("MECHANIZED_INFANTRY") and it crashed the game. Any ideas on where I could get information on what strings are used for which units.

rcuddy is right you are missing the "UNIT_" in gc.getIntoTypeForString("MECHANIZED_INFANTRY").
 
gautam said:
Code:
	group.pushMission(MissionTypes.MISSION_FORTIFY, 0, 0, 0, false, false, MissionAITypes.NO_MISSIONAI, plot, 0)

Change the last 0 to unit.


gautam said:
Now I even tried the doCommand(CommandType.WAKE) but I don't think this would be the case. It definitely didn't work. This seems like a command to wake up a unit put to sleep which would make more sense. And fortify would probably be a mission rather than a command I suppose. I suppose this could have a close relation with sentry as in wake up if an enemy is near but I doubt its anywhere related to Fortify.

I doubt it does work but it's not CommandType.WAKE. It's exactly what I wrote, CommandType.COMMAND_WAKE.
 
Hi,

Thanks, I got it working. Actually it was failing on

bResult = unit.canFortify()

which should have been
bResult = unit.canFortify(unti.plot()).
 
Back
Top Bottom