A little present for the folks trying to learn Python (I had never heard of Python before Civ4 was released so I'm still learning too). This is a little custom function I wrote for my mod that I thought displayed a few ways to do things that other may find interesting.
Im not the best programmer (not even a good one) but I thought if you guys learn like I do then the more examples you are exposed to the better you will be.
Please feel free to use this thread to post sample code of your own (please don't use this thread to post broken code, I want a common place people can trade working ideas).
What it does: passed a dmg amount and a unit object it does dmg to all units within 1 tile of the selected unit (including the unit itself). The amount of dmg that is done is mitigated by the effected units level and strength. Lastly if the unit that is hurt isn't the players own then they will declare war on the player.
Use: In my mod I have a guy that called this function every turn. He is a very nasty guy to hang around. I will also be using it as a spell that damages everyone in the area. There is nothing to keep you from a smiliar function to perform a check, add or remove promotions, etc etc.
Im not the best programmer (not even a good one) but I thought if you guys learn like I do then the more examples you are exposed to the better you will be.
Please feel free to use this thread to post sample code of your own (please don't use this thread to post broken code, I want a common place people can trade working ideas).
Code:
def FFHFireAura(self, iDmg, pUnit):
iX = pUnit.getX()
iY = pUnit.getY()
iPlayer = pUnit.getOwner()
pPlayer = gc.getPlayer(iPlayer)
eTeam = pPlayer.getTeam()
for iiX in range(iX-1, iX+2, 1):
for iiY in range(iY-1, iY+2, 1):
pPlot = CyMap().plot(iiX,iiY)
for iUnit in range(pPlot.getNumUnits()):
insDmg = iDmg
pUnit = pPlot.getUnit(iUnit)
iStr = pUnit.baseCombatStr() * 3
iLevel = pUnit.getLevel() * 3
insDmg = iDmg - (iStr + iLevel)
if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_MAGIC_RESISTANCE')):
insDmg = insDmg /2
if insDmg >= 1:
pUnit.setDamage(pUnit.getDamage() + insDmg, True)
if pUnit.getOwner() != iPlayer:
p2Player = gc.getPlayer(pUnit.getOwner())
e2Team = gc.getTeam(p2Player.getTeam())
e2Team.declareWar(eTeam, False)
What it does: passed a dmg amount and a unit object it does dmg to all units within 1 tile of the selected unit (including the unit itself). The amount of dmg that is done is mitigated by the effected units level and strength. Lastly if the unit that is hurt isn't the players own then they will declare war on the player.
Use: In my mod I have a guy that called this function every turn. He is a very nasty guy to hang around. I will also be using it as a spell that damages everyone in the area. There is nothing to keep you from a smiliar function to perform a check, add or remove promotions, etc etc.