[BTS] Gunship-Crash

Alrik2002

Warlord
Joined
May 24, 2012
Messages
214
Location
Berlin, Germany
Hi,

I would like to allow gunships to cross water and peaks. That works fine but I want to add, that they have to end their turn on "regular" terrain. I know this is python, I believe I have to add it in the eventmanager. Since I´ve only merged some things in python until now, I tried to copy an paste some things of other mods. When I tried to start a new game it didn´t show any interface, so that I couldn´t test it. Please be so kind an help me to develop a code which works. Here my first try:

Code:
	def onEndPlayerTurn(self, argsList):
		'Called at the end of a players turn'
		iGameTurn, iPlayer = argsList
## Hubschrauber Crash Start ##		
			pPlayer = gc.getPlayer(iPlayer)
			pUnit = pPlayer.getUnit(i)                    
				if pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_GUNSHIP"):
					iX = pUnit.getX()
					iY = pUnitX.getY()
					pPlot = CyMap().plot(iX(),iY())
					if pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_OCEAN") or pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_COAST") or pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_PEAK"):
						strUnitButton = gc.getUnitInfo(iUnitType).getButton ()
						pUnit.kill(False,-1)
						CyInterface().addMessage(pPlayer.getID(),False,15,CyTranslator().getText("TXT_ABSTURZ",()),'',0,strUnitButton,ColorTypes(gc.getInfoTypeForString("COLOR_RED")), iX, iY, True,True
## Hubschrauber Crash End ##

thx
 
Could you copy the whole function from which you copied that snip btw? The original I mean.
 
Please activate Python exceptions in your main BtS .ini file.

How? Go to Documents\My Games\Bts, open the CivilizationIV.ini with a text editor, and change
PHP:
; Set to 1 for no python exception popups
HidePythonExceptions = 1

to 0.
This will tell you in which line you have an error.


And check again the mod where you stole the code from. Because you must have forgotten the first line.


Oh, and I'd not recommend to use that code. The AI will not understand it, and it's not really processing friendly.
 
Hi,

I tried to put it together of the following two. They are about sinking ships:

Code:
def onUnitMove(self, argsList):
        'unit move'
        pPlot,pUnit,pOldPlot = argsList
        player = PyPlayer(pUnit.getOwner())
        unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())
###sinking ships start
                InitialSinkingChanceOcean = 80
                InitialSinkingChanceCoast = 50                
                SinkingChanceWithCompass = 20
                SinkingChanceWithAstronomy = 5        
        if (pPlot.isWater()) and (not (pPlot.isCity())):
                        if pUnit.getDomainType ()==gc.getInfoTypeForString("DOMAIN_SEA"):
                            pPlayer = gc.getPlayer(pUnit.getOwner())
                            pTeam = gc.getTeam(pPlayer.getTeam())                            
                            if pPlot.getTerrainType ()==gc.getInfoTypeForString("TERRAIN_OCEAN"):
                                SinkChance = InitialSinkingChanceOcean
                            if pPlot.getTerrainType ()==gc.getInfoTypeForString("TERRAIN_COAST"):
                                SinkChance = InitialSinkingChanceCoast                                
                            if pTeam.isHasTech(gc.getInfoTypeForString("TECH_COMPASS")):
                                SinkChance = SinkingChanceWithCompass
                            if pTeam.isHasTech(gc.getInfoTypeForString("TECH_ASTRONOMY")):
                                SinkChance = SinkingChanceWithAstronomy
                            SinkingDescission = CyGame().getSorenRandNum(100, "WillItSink")
                            if SinkingDescission < SinkChance:
                                iUnitType = pUnit.getUnitType()
                                strUnitButton = gc.getUnitInfo(iUnitType).getButton ()
                                iX = pUnit.getX()
                                iY = pUnit.getY()
                                pUnit.kill(False,-1)
                                CyInterface().addMessage(pPlayer.getID(),False,15,CyTranslator().getText("TXT_SHIP_SUNK",()),'',0,strUnitButton,ColorTypes(gc.getInfoTypeForString("COLOR_RED")), iX, iY, True,True)                                                
###sinking ships end
                                               
        if (not self.__LOG_MOVEMENT):
            return
        if player and unitInfo:
            CvUtil.pyPrint('Player %d Civilization %s unit %s is moving to %d, %d' 
                %(player.getID(), player.getCivilizationName(), unitInfo.getDescription(), 
                pUnit.getX(), pUnit.getY()))

Code:
iNumUnits = pPlayer.getNumUnits
for i in range iNumUnits:
    PUnit = pPlayer.getUnit(i)
    if pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_GALLEY") or pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_TRIREME"):
        iX = pUnit.getX()
        iY = pUnitX.getY()
        pPlot = CyMap().plot(iX(),iY())
        if pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_OCEAN"):
            iChance = 50
        elif pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_COAST"):
            iChance = 1
        elif pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_TIEFSEE"):
            iChance = 80
        self.iUntergang = self.getRandomNumber( 100 )
        if iChance > self.iUntergang:
            pUnit.kill(False,iPlayer)

In the forum where I have them from it´s not written if they work or not. The thread there was not finished.
 
There is your problem. As I thought, you have a loop without a loop. Are you familiar with any programing language so that I should go on and explain?
 
Oh, and I'd not recommend to use that code. The AI will not understand it, and it's not really processing friendly.

Ok. So the AI would fly with Gunships over the ocean and let them crash? To change this you have to go in the dll right?:(

What do you mean with processing friendly? Is it slow because of checking and the end of the turn? is it possible to make it leaner?

Thank you for your help.
 
There is your problem. As I thought, you have a loop without a loop. Are you familiar with any programing language so that I should go on and explain?

Unfortunatly not, but I´ve a friend which is familiar with some programing language I could ask if you would explain it to me.
 
Hi,

I tried to put it together of the following two. They are about sinking ships:
[...]
In the forum where I have them from it´s not written if they work or not. The thread there was not finished.

Oh, since I see that one part of that code is by me, I'm pretty sure that I must have said that the whole thing is problematic due to the AI ;).

There is your problem. As I thought, you have a loop without a loop. Are you familiar with any programing language so that I should go on and explain?

Exactly my thought. Not sure if there's anywhere else maybe a problem :hmm:.

Ok. So the AI would fly with Gunships over the ocean and let them crash? To change this you have to go in the dll right?:(

What do you mean with processing friendly? Is it slow because of checking and the end of the turn? is it possible to make it leaner?

Thank you for your help.

Yes to the first 3 questions, no idea at the last :dunno:.
 
Unfortunatly not, but I´ve a friend which is familiar with some programing language I could ask if you would explain it to me.
A programing loop is a set of instructions that makes the program repeat a line over and over again. With the only thing that changes being one or more instructions labeled by an itterator. The itterator is basically just a number that counts the number of loops so far. In your case you left out the part where you tell the code to loop. And so it just crashes.

Your code should look like this:
Code:
[COLOR="Red"]iNumUnits = pPlayer.getNumUnits
for i in range iNumUnits:[/COLOR]
	PUnit = pPlayer.getUnit(i)
				if pUnit.getUnitClassType() == gc.getInfoTypeForString("UNITCLASS_GUNSHIP"):
					iX = pUnit.getX()
					iY = pUnitX.getY()
					pPlot = CyMap().plot(iX(),iY())
					if pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_OCEAN") or pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_COAST") or pPlot.getTerrainType() == gc.getInfoTypeForString("TERRAIN_PEAK"):
						strUnitButton = gc.getUnitInfo(iUnitType).getButton ()
						pUnit.kill(False,-1)
						CyInterface().addMessage(pPlayer.getID(),False,15,CyTranslator().getText("TXT_ABSTURZ",()),'',0,strUnitButton,ColorTypes(gc.getInfoTypeForString("COLOR_RED")), iX, iY, True,True
Notice the lines in red. They tell the code that it should start a loop.

One key thing to remember about python thou is that it's completely invisible to the AI. As in, if you code something in python the AI just does not know it exists. So say you code a victory condition in python. The AI will newer try to win that way because for it the condition is not there. In your case, that means the AI would just flat out not be aware that it's gunships are crashing due to the conditions you set. It would be like a lost child crying: "What's happening to my gunships?!" And than promptly build a new one and park it on the exact same spot where it lost the one last turn.
 
Thank you for your help. So I will discuss this with the friends which want to play the MOD. The AI would be weaken very hard I believe.

When you say the AI couldn´t see any python things to decide what to do, what is with wonders? I´ve added several wonders. The functions are in python, but there are XML-things too. Would the AI ignore them too?
 
Thank you for your help. So I will discuss this with the friends which want to play the MOD. The AI would be weaken very hard I believe.

When you say the AI couldn´t see any python things to decide what to do, what is with wonders? I´ve added several wonders. The functions are in python, but there are XML-things too. Would the AI ignore them too?
The AI sees everything that is in the XML and DLL but nothing from python. So if your wonder does something like say grant a free building in every city. The AI would know that it exists but not that it adds a free building.
 
I found that toggling "bCanMoveAllTerrain" allows you to cross oceans, what is the difference between that and this python script.
 
Will the AI know that the gunship will die over ocean or a peak? If not I think that would be an unfair advantage for the human player.
 
Will the AI know that the gunship will die over ocean or a peak? If not I think that would be an unfair advantage for the human player.

Python = definitely no, AI has no clues
 
Top Bottom