Congrats!
Looking over your code it seems you can trim some unnecessary lines. You aren't using playerX/Y, unitX/Y, unit, or iAttacker and can remove all five of those lines. They were probably leftover from your old code. Plus you are only using the PyHelper classes in those lines to delete, allowing you to cut out three more lines. Finally, you're not using BugUtil anymore--probably had some debugging messages in there, eh?
You can simplify your code to this:
Code:
# Baddykill
# Kill the Bad Guy, win the game!
from CvPythonExtensions import *
gc = CyGlobalContext()
def onCombatResult(argsList):
'Combat Result'
pWinner,pLoser = argsList
iBaddy = gc.getInfoTypeForString("UNITCLASS_DEMON_KING")
if (pLoser.getUnitClassType() == iBaddy):
CyGame().setWinner(pWinner.getTeam(), gc.getInfoTypeForString("VICTORY_CONQUEST"))
As this function is called only when combat ends, it won't get called thousands of times a turn. However, it's a good example on how to speed up your code easily using one of BUG's key features: module initialization functions.
What we want to do is avoid the call to getInfoTypeForString() every time we need the value because that value never changes after the game loads. For that we can ask BUG to call a function once when the game starts and store the result of the function in a variable to use inside the function.
1. Create the variable and function.
Code:
gc = CyGlobalContext()
[COLOR="Green"][B]# This will be set once by init(). ALL_CAPS typically denotes a constant value in a module
BADDY_UNIT_CLASS = None
def init():
global BADDY_UNIT_CLASS
BADDY_UNIT_CLASS = gc.getInfoTypeForString("UNITCLASS_DEMON_KING")[/B][/COLOR]
def onCombatResult(argsList):
2. Use the variable in your onCombatResult().
Note that you only need to use "global <variable>" in a function if you are going to assign it a value with =. You don't need "global" below to to use BADDY_UNIT_CLASS in the "if (...)" test.
Code:
def onCombatResult(argsList):
'Combat Result'
pWinner,pLoser = argsList
[B][COLOR="Red"][s]iBaddy = gc.getInfoTypeForString("UNITCLASS_DEMON_KING")[/s][/COLOR][/B]
if (pLoser.getUnitClassType() == [B][COLOR="Green"]BADDY_UNIT_CLASS[/COLOR][/B]):
CyGame().setWinner(pWinner.getTeam(), gc.getInfoTypeForString("VICTORY_CONQUEST"))
3. Tell BUG to call init().
Add this line to your module's configuration XML file:
Code:
<mod ...>
...
[B][COLOR="Green"]<init module="Baddykill"/>[/COLOR][/B]
...
</mod>
This assumes the above Python file is called "Baddykill.py". Since <init>s are normally called
after all modules have been loaded, it doesn't matter so much where you place it in your <mod> element as long as it is inside it.
Why didn't I do the same thing with VICTORY_CONQUEST? Mostly because I'm lazy.
This value will only be needed when the bad guy is killed--once per game--so it hardly seemed worth it. It would make a good exercise for the reader, though.