Declaring War script

Nitram15

Pro Libertate!
Joined
Sep 15, 2009
Messages
1,855
Location
Hungary
Hi! I am new to Python scripting, and already create successful python scripts (well can hardly call a script; it was three popups which was scripted to iGameTurn == 1, 3, and 5 and called at onBeginGameTurn) so I wanted to script that if the popup comes, the turkish will declare war on the russian.

Here is the base code of the popup:

Code:
	def onBeginGameTurn(self, argsList):
		'Called at the beginning of the end of each turn'
		iGameTurn = argsList[0]
		CvTopCivs.CvTopCivs().turnChecker(iGameTurn)

		if iGameTurn == 3:
			for iPlayer in range(gc.getMAX_PLAYERS()):
				player = gc.getPlayer(iPlayer)
				if (player.isAlive() and player.isHuman()):
					popupInfo = CyPopupInfo()
					popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_TEXT)
					popupInfo.setText("THE RUSSO-TURKISH WAR\n\nIn 1806, Sultan Selim III, encouraged by the Russian defeat at Austerlitz and adviced by the French Empire, deposed the pro-Russian Constantine Ypsilanti as Hospodar of the Principality of Wallachia and Alexander Mourousis as Hospodar of Moldavia, both Ottoman vassal states. Simultaneously, the French Empire occupied Dalmatia and threatened to penetrate the Danubian principalities at any time. In order to safeguard the Russian border against a possible French attack, a 40,000-strong Russian contingent advanced into Moldavia and Wallachia. The Sultan reacted by blocking the Dardanelles to Russian ships and declared war on Russia.")
					popupInfo.addPopup(iPlayer)

So I took a look at the python API, but I don't know how to use what I found:mischief:

I found isDeclareWar () but I don't how can I use it. I tried getCivilizationTypes and use the ID in my XML. Tried the following noob code:
getCivilizationTypes (7) = gc.DeclareWar CivilizationTypes (3)

So the new code looks like this:
Code:
	def onBeginGameTurn(self, argsList):
		'Called at the beginning of the end of each turn'
		iGameTurn = argsList[0]
		CvTopCivs.CvTopCivs().turnChecker(iGameTurn)

		if iGameTurn == 3:
			for iPlayer in range(gc.getMAX_PLAYERS()):
				player = gc.getPlayer(iPlayer)
				[COLOR="Red"]isDeclareWar = gc.getCivilizationTypes (7 , 3)[/COLOR]
				if (player.isAlive() and player.isHuman()):
					popupInfo = CyPopupInfo()
					popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_TEXT)
					popupInfo.setText("THE RUSSO-TURKISH WAR\n\nIn 1806, Sultan Selim III, encouraged by the Russian defeat at Austerlitz and adviced by the French Empire, deposed the pro-Russian Constantine Ypsilanti as Hospodar of the Principality of Wallachia and Alexander Mourousis as Hospodar of Moldavia, both Ottoman vassal states. Simultaneously, the French Empire occupied Dalmatia and threatened to penetrate the Danubian principalities at any time. In order to safeguard the Russian border against a possible French attack, a 40,000-strong Russian contingent advanced into Moldavia and Wallachia. The Sultan reacted by blocking the Dardanelles to Russian ships and declared war on Russia.")
					popupInfo.addPopup(iPlayer)

So I started the game and after the scripted time, Python Exception gave the following error message:
'CyGlobalContent' object has no attribute 'getCivilizationTypes'
I assume I did something terrible in that code.:(
Please be kind. I never did such thing before. I still need to learn about Python.:sad:
 
It doesn't work because of what it says: there is no such function. There is a gc.getCivilizationInfo(iCivType), but nothing like what you tried to use.

War declaration is for a team vs. a team.

The thing basically has two broad steps:

1) Get the team IDs of the two players that will be involved, via something like pPlayer.getTeam(). If they are the same, this is a no-go. Try setting pTeam1 to be the team that will declare war and pTeam2 to be the one that will be declared on.

2) The one that will be declaring war does a "pTeam1.declareWar( pTeam2, true, WarPlanType)". WarPlanType should probably be either WarPlanTypes.WARPLAN_LIMITED or WarPlanTypes.WARPLAN_TOTAL, depending on how much effort they should put into it.

Note that this should be instantaneous. It does not give the AI any time to make preparations. The immediate results can, therefore, be rather unimpressive.

But first you should do a step 0: make sure both players are still alive.
 
Example of what you need:
PHP:
gc.getTeam(17).declareWar(iTeam, false, WarPlanTypes.WARPLAN_TOTAL)

That leads to a war declaration of team #17 against the team iTeam.
To find out what teams you need is your duty ;).


And you've looked at the wrong function ;).
Take again a look at isDeclareWar, scroll a bit higher: It belongs to the events ;). And if you take a look at the events, you'll notice, that this is the tag which will lead in an event to a war declaration.

And another basic beginner hint: Look at the beginning.
If you see a "void" at the beginning, this function will do something.
If you see something else, e.g. bool, string, etc. at the beginning, this function will only give you back some infos, but will not do anything in the game.


Edit: Damn, i should refresh the page before answering.
 
Thanks for both of you, I'll try out what you said. :)
EDIT: I also saw that VOID declareWar (TeamType eTeam, BOOL bNewDiplo, WarPlanType eWarPlan) on the API but I found it too difficult, so tried the boolean, which I know now: it doesn't do anything. Good to know, anyway. :)
 
Thanks for the help guys! I tried the script you gave me The_J, and it worked well! (Of course, I defined the iTeam, but wrongly(!) and not the turkish declared war on the russian, but wurttemberg declared war on prussia! :D in the code below, it has been fixed)

So the code is now good:
Code:
		if iGameTurn == 3 [COLOR="Red"]and iTeam == 4[/COLOR]:
			for iPlayer in range(gc.getMAX_PLAYERS()):
				player = gc.getPlayer(iPlayer)
				[COLOR="Red"]gc.getTeam(5).declareWar(iTeam, false, WarPlanTypes.WARPLAN_TOTAL)[/COLOR]
				if (player.isAlive() and player.isHuman()):
					popupInfo = CyPopupInfo()
					popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_TEXT)
					popupInfo.setText("THE RUSSO-TURKISH WAR\n\nIn 1806, Sultan Selim III, encouraged by the Russian defeat at Austerlitz and adviced by the French Empire, deposed the pro-Russian Constantine Ypsilanti as Hospodar of the Principality of Wallachia and Alexander Mourousis as Hospodar of Moldavia, both Ottoman vassal states. Simultaneously, the French Empire occupied Dalmatia and threatened to penetrate the Danubian principalities at any time. In order to safeguard the Russian border against a possible French attack, a 40,000-strong Russian contingent advanced into Moldavia and Wallachia. The Sultan reacted by blocking the Dardanelles to Russian ships and declared war on Russia.")
					popupInfo.addPopup(iPlayer)
 
You are declaring war inside a loop so it is doing it 18 times since gc.getMAX_PLAYERS() returns 18 (or more, if you are using a mod with more than 18 players allowed). You should move the war declaration line to be before the loop (which is just used to notify the humans in the game that it has happened).
 
Hmm, okay. If you say so, then I'll put it out of the loop. ;) (Btw, I modified the DLL, so the max players are 30 now)
EDIT: Is it good now?
Code:
[COLOR="Red"]		if iGameTurn == 3 and iTeam == 4:
			gc.getTeam(5).declareWar(iTeam, false, WarPlanTypes.WARPLAN_TOTAL)[/COLOR]
			for iPlayer in range(gc.getMAX_PLAYERS()):
				player = gc.getPlayer(iPlayer)
				if (player.isAlive() and player.isHuman()):
					popupInfo = CyPopupInfo()
					popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_TEXT)
					popupInfo.setText("THE RUSSO-TURKISH WAR\n\nIn 1806, Sultan Selim III, encouraged by the Russian defeat at Austerlitz and adviced by the French Empire, deposed the pro-Russian Constantine Ypsilanti as Hospodar of the Principality of Wallachia and Alexander Mourousis as Hospodar of Moldavia, both Ottoman vassal states. Simultaneously, the French Empire occupied Dalmatia and threatened to penetrate the Danubian principalities at any time. In order to safeguard the Russian border against a possible French attack, a 40,000-strong Russian contingent advanced into Moldavia and Wallachia. The Sultan reacted by blocking the Dardanelles to Russian ships and declared war on Russia.")
					popupInfo.addPopup(iPlayer)
 
Unless this is for a specific scenario, you probably don't want to hardcode the team numbers into the function. Team and player IDs change from game to game.
 
Back
Top Bottom