Python Snippets

bah, it's time I put my python knowledge in practice again, and not by making a goddamn parsing algorithm or an image processing program) but that has nothing to do with my question :lol:

it can be a very stupid question indeed but I'm not @ home right now so I'll still ask: the cannotTrain is used in the Civilopedia? like.. if I use your leaderspecific snippet the effects will show when viewing the unit in the pedia? "you cannot train this unit" or whatever that message is?

your snippets will serve as inspiration though...
 
New snippet for those of us who just can't figure out Python?

What about a routine that can be used in a Event Description's PythonCallback to cause one specific nation to declare war on another specific one, where CIV-A is the aggressor, and Civ-B the aggressed.

Then users like me would only need to type in the actual civs' names.
 
That isn't hard, I have several events that do this in the Navy Seal's Earth 1000AD and the Medieval Age. I'll post the code tonight, tomorrow, or Saturday.
 
Another idea/request:

Adding a building to a city after completion of a project.

For example:
Let's say you complete SDI in a city then add a SDI-building to that city which reduces damage from nukes.
Of course you need to add a SDI-building via XML with art etc. :)

I tried using an event in XML but no success :(
 
Here's another idea: assuming there is a new building class called Radar Station in a city, would it be possible to attach a script that would give air units based in that city a bonus to intercept?
Only if you could make a promotion that did what you want then we give that promotion when they enter the city, and take it away when they leave.

Adding a building to a city after completion of a project.
Yes this is very possible, I will get a snippet in the next week.
 
Only if you could make a promotion that did what you want then we give that promotion when they enter the city, and take it away when they leave.

Sure. I already have the permanent interception promotion. I can make another one or two with a different name "Ground Radar" (and "Improved Ground Radar") that would reflect the fact they come from the station itself. I probably will limit the number of radar stations that can be built, for the sake of game balance.
 
I think this is what Ambreville needs for his PythonCallback:

Pick a name that reflects the event that it belongs to and that only includes letters, numbers, and under scores (_), the name should be similar to "doMyEvent".

Then in your event (the one in Civ4EventInfos.xml, not the trigger) change the PythonCallback tag to have the name you decided on, it should look like this.
Code:
<PythonCallback>doMyEvent</PythonCallback>
Next you need to open CvRandomEventInterface.py. At the end of the file add this code.
Code:
def doMyEvent(argsList):
	iEvent = argsList[0]
	kTriggeredData = argsList[1]

	## --------- ## MAKE CHANGES HERE ## --------- ##
	##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	szEnemyPlayer	= "CIVILIZATION_CHINA"		# The type tag of the enemy civilization, it will declare war on it's team
	resetDiplomacy	= True				# Resets pTeam's diplomacy with szEnemyPlayer's Team when True, or it leaves the diplomacy the same when False
	warPlan		= WarPlanTypes.NO_WARPLAN	# Used for the AI, it tells the AI what type of war to wage
							# Possible entries are:
							# WarPlanTypes.NO_WARPLAN 			The AI decides
							# WarPlanTypes.WARPLAN_ATTACKED_RECENT,
							# WarPlanTypes.WARPLAN_ATTACKED,
							# WarPlanTypes.WARPLAN_PREPARING_LIMITED,
							# WarPlanTypes.WARPLAN_PREPARING_TOTAL,
							# WarPlanTypes.WARPLAN_LIMITED,
							# WarPlanTypes.WARPLAN_TOTAL,
							# WarPlanTypes.WARPLAN_DOGPILE,

	warn		= True				# Give a message if the civilization is not found, put False if you want no message

	## --------- ## DO NOT CHANGE ANYTHING PAST HERE ## --------- ##
	##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


	## This looks fo the civilization
	iEnemyTeam = -1
	for iPlayer in range(gc.getMAX_PLAYERS()):
		player = gc.getPlayer(iPlayer)
		iType = pPlayergetCivilizationType()
		pType = gc.getCivilizationInfo(iType)
		sType = pType.getType()
		if sType == szEnemyPlayer:
			iEnemyTeam = player.getTeam()
			break

	## If the civilization was not found give the warning
	if iEnemyTeam == -1:
		if warn:
			CyInterface().addImmediateMessage("ERROR: %s was not found, will not declare war!" % szEnemyPlayer, "")

	## Otherwise declare war
	else:
		pTeam.declareWar(iEnemyTeam, resetDiplomacy, warPlan)

	return 1
Just replace the text doMyEvent with the name you decided on and set the values on szEnemyPlayer, resetDiplomacy, warPlan, and warn. This should do what you need. Unfortunatly I was unable to test this but it should work, and I have have subjected it to more scrutiny than any other snippet I have ever written. Enjoy, and let me know if you need help. ;)
==================================
I will try to get the promotion system for Ambreville, and Arian's building placement system up tomorrow. :thumbsup:
 
Would it be possible, as a few lines of python code, to add in an XML tag to the promotionsinfos.xml that allowed certain promotions to see a new invisible type? If so, how would I go about doing this?

I've gotten to the point where I'm going to start reading the python tutorials, I can't go any further with modifications I want to make to mods with XML. This seems like a simple thing to start with (though I could be completely wrong). Edit: nevermind, looks like that would be an SDK thing. Looking at the Python files, it doesn't look like much is in python really.
 
If you ever get over RL some... here's another idea.

This one's for those who like historical mod/scenarios that call for expeditionary forces from a Civ that does not control any cities on the game map -- like for example the colonization of far-flung colonies, or WWI & WWII US troops on a map limited to just Europe (etc).

The "total kill" option needs to be used to make sure the "overseas civ" isn't instantly defeated since it doesn't have a capital on Turn #1. Python can then be used to spawn unit reinforcements on a regular or random basis.

What's still missing here is a function that gives the overseas civ a set number of beakers, gold, and spy points per turn, even if it has no cities. If it conquers some, that number would then get added to whatever is generated from the conquered cities.

The coding could be setup to be active on Turn #1, or included in the event's callback routine that brings that civ into play.

Possible? Interested?
 
Here is the code that Arian requested. It will add a building to a city when it completes a project.
Code:
def onProjectBuilt(self, argsList):
	'Project Completed'
	pCity, iProjectType = argsList
	game = gc.getGame()
	if ((not gc.getGame().isNetworkMultiPlayer()) and (pCity.getOwner() == gc.getGame().getActivePlayer())):
		popupInfo = CyPopupInfo()
		popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
		popupInfo.setData1(iProjectType)
		popupInfo.setData2(pCity.getID())
		popupInfo.setData3(2)
		popupInfo.setText(u"showWonderMovie")
		popupInfo.addPopup(pCity.getOwner())

	## ------------- ## CHANGE HERE ## ------------- ##
	sProject	= "PROJECT_SDI"			# The type tag of the project that gives the building
	sBuilding	= "BUILDING_BOMB_SHELTER"	# The type tag of the building to be given


	## ------------- ## DO NOT CHANGE ## ------------- ##
	if iProjectType == gc.getInfoTypeForString(sProject):
		pCity.setNumRealBuilding(gc.getInfoTypeForString(sBuilding), 1)
Place this code into an unchanged CvEventManager.py replacing onProjectBuilt. Or if you are using either Dr. Elmer Giggle's event manager or zModular Python the code will look like this.
Code:
def onProjectBuilt(self, argsList):
	'Project Completed'
	pCity, iProjectType = argsList


	## ------------- ## CHANGE HERE ## ------------- ##
	sProject	= "PROJECT_SDI"			# The type tag of the project that gives the building
	sBuilding	= "BUILDING_BOMB_SHELTER"	# The type tag of the building to be given


	## ------------- ## DO NOT CHANGE ## ------------- ##
	if iProjectType == gc.getInfoTypeForString(sProject):
		pCity.setNumRealBuilding(gc.getInfoTypeForString(sBuilding), 1)
Just place in your Event Manager file and away you go. If you would like multiple projects to give buildings just duplicate the code that comes after "## ------------- ## CHANGE HERE ## ------------- ##". Enjoy and just give a post if you need help. :thumbsup:

I'll be working on the promo's for Ambreville over the weekend. And about your newest Q, yes it's possible. Is it worth it? Depends on how complex it turns out to be. I would have to write code to calculate how much should go for science and treasury (don't need culture since they don't have cities). I'll see what I can do, but I can tell this much, it isn't going to be a snippet (try MOD COMP or maybe small MOD). :goodjob::cool::king:;):):D:crazyeye::scan:
 
What am I doing wrong?

This is the original code in my CvEventManager.py:

Code:
def onProjectBuilt(self, argsList):
		'Project Completed'
		pCity, iProjectType = argsList
		game = gc.getGame()
		if ((not gc.getGame().isNetworkMultiPlayer()) and (pCity.getOwner() == gc.getGame().getActivePlayer())):
			popupInfo = CyPopupInfo()
			popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
			popupInfo.setData1(iProjectType)
			popupInfo.setData2(pCity.getID())
			popupInfo.setData3(2)
			popupInfo.setText(u"showWonderMovie")
			popupInfo.addPopup(pCity.getOwner())

then I change it by pasting the code. It looks like this:

Code:
def onProjectBuilt(self, argsList):
		'Project Completed'
		pCity, iProjectType = argsList
		game = gc.getGame()
		if ((not gc.getGame().isNetworkMultiPlayer()) and (pCity.getOwner() == gc.getGame().getActivePlayer())):
			popupInfo = CyPopupInfo()
			popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
			popupInfo.setData1(iProjectType)
			popupInfo.setData2(pCity.getID())
			popupInfo.setData3(2)
			popupInfo.setText(u"showWonderMovie")
			popupInfo.addPopup(pCity.getOwner())

		## ------------- ## CHANGE HERE ## ------------- ##
		sProject	= "PROJECT_ISS"			# The type tag of the project that gives the building
		sBuilding	= "BUILDING_ISS"		# The type tag of the building to be given

		## ------------- ## DO NOT CHANGE ## ------------- ##
		if iProjectType == gc.getInfoTypeForString(sProject):
		        pCity.setNumRealBuilding(gc.getInfoTypeForString(sBuilding), 1)

In this case PROJECT_ISS is International Space Station project and BUILDING_ISS the building model of the station (found on the forum btw) but when I start a new game, the game interface is gone???
Without the code everything works fine.

I have absolutely no clue what's happening here...:confused:
Any idea what causing this??
 
Top Bottom