Help Extracting Fort Commander Code

MaxAstro

Spiral Knight
Joined
Dec 18, 2007
Messages
645
I'm trying to extract the Fort commander code and import it into my custom mod-mod. So far everything has worked fine; I have claimable forts, fort commanders are generated and upgrade properly. However, I can't for the life of me get them to generate influence.

I've pulled updateAllForts, updateFortCulture, updateCastleCulture, and updateCitadelCulture out of EventManager.py, but that doesn't seem to be enough; is there any other python that is needed for fort commanders to generate culture?
 
Ah, thanks, that was it. One little line of code and it completely messed me up. XD
 
New question: Is there any way easily apply the civilization's race to a fort commander when he is created? Otherwise, pretty much every civ with a racial promotion will need a UU fort commander.
 
Shouldn't the racial promotion be applied automatically whenever a non-mechanized unit lacking a race is initialized?
 
Here you go. Updated the claim fort spell so you don't have to add anything for new Commander UUs (Thanks MrUnderhill!), and added a bit of code to add the race. It checks the caster's race, and if it exists, adds to the Commander. Yes, this can make some interesting commanders where the caster is dominated and has some other race... But that's a low occurrence, and I'm okay with it. :lol: There's a second check for Undead, as that's no longer a race in FF/FFPlus.

Keep in mind, it's not actually tested. However, the initial change was from MrUnderhill, pretty sure it's working... And the race fix was already in use for an event, which is where I got it. :lol:

Code:
def spellClaimFort(pCaster):
	pPlayer = gc.getPlayer(pCaster.getOwner())
	iUnit = gc.getInfoTypeForString('UNITCLASS_FORT_COMMANDER')
	infoCiv = gc.getCivilizationInfo(pPlayer.getCivilizationType())
	iUnit = infoCiv.getCivilizationUnits(iUnit)
	iRace = pCaster.getRace()
	newUnit = pPlayer.initUnit(iUnit, pCaster.getX(), pCaster.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)	
	if iRace != -1:
		newUnit.setHasPromotion(iRace, True)
	if pCaster.isHasPromotion(gc.getInfoTypeForString('PROMOTION_UNDEAD')):
		newUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_UNDEAD'), True)
 
Could also cause some weirdness with things like Soldiers of Kilmorph for non-dwarven civs, but that should also be a relatively low occurrence; in my experience it's usually either workers or scouts casting Claim Fort, at least outside of war.

Which reminds me, having forts stolen from under you is not fun. XD The first game I actually built a fort in FF+, I didn't have the tech and I only had two captured gretchins with which to build it, so they took ~forever~.

The very turn they finished the fort, before I had a chance to tell them to claim it, one of Hannah the Irin's scouts moved onto the fort and claimed it. >.<

...At least it proved that the AI could use forts... XD
 
Could also cause some weirdness with things like Soldiers of Kilmorph for non-dwarven civs, but that should also be a relatively low occurrence; in my experience it's usually either workers or scouts casting Claim Fort, at least outside of war.

Which reminds me, having forts stolen from under you is not fun. XD The first game I actually built a fort in FF+, I didn't have the tech and I only had two captured gretchins with which to build it, so they took ~forever~.

The very turn they finished the fort, before I had a chance to tell them to claim it, one of Hannah the Irin's scouts moved onto the fort and claimed it. >.<

...At least it proved that the AI could use forts... XD

Well... As annoying as that would be, I'll leave it as a feature. :lol:
 
The very turn they finished the fort, before I had a chance to tell them to claim it, one of Hannah the Irin's scouts moved onto the fort and claimed it. >.<

Unless you were using simultaneous turns, she couldn't have taken the fort before you had a chance to claim it. Just before you were prompted to.

The workers completed the fort on your turn and had you selected them, you could have claimed the fort. I'm pointing it out since you could have loaded an autosave to avoid the problem, if you thought it was worth it.
 
Unless you were using simultaneous turns, she couldn't have taken the fort before you had a chance to claim it. Just before you were prompted to.

The workers completed the fort on your turn and had you selected them, you could have claimed the fort. I'm pointing it out since you could have loaded an autosave to avoid the problem, if you thought it was worth it.

That particular game it was indeed just before I was prompted. I have had something similar happen in simultaneous turns since then, though. :)

EDIT: Also, Valk, since you are already getting the civilization as part of that code, isn't there a way to check the race associated with the civ? It seems like there must be a python check for that.
 
That code actually seems to be causing crashes for me. I would get repeated crashes that I can only assume where happening when an AI was casting Claim Fort. The strange thing was it wasn't all the time, since I saw AI fort commanders.

In any case, changing it to simply
Code:
def spellClaimFort(pCaster):
    pPlayer = gc.getPlayer(pCaster.getOwner())
    iUnit = gc.getInfoTypeForString('UNIT_FORT_COMMANDER')
    newUnit = pPlayer.initUnit(iUnit, pCaster.getX(), pCaster.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)
seems to have made it stop crashing, although that of course requires UU fort commanders to be explicitly defined. I'm also sure that it wasn't the new code you added to apply race; I was getting the crash with MrUnderhill's code from the other thread. It seems for some reason that implementation of creating the commander based on unitclass causes AI crashing. It's possible that something else in my personal mod is aggravating the issue; if someone could do some outside testing it would be appreciated.
 
That code actually seems to be causing crashes for me. I would get repeated crashes that I can only assume where happening when an AI was casting Claim Fort. The strange thing was it wasn't all the time, since I saw AI fort commanders.

In any case, changing it to simply
Code:
def spellClaimFort(pCaster):
    pPlayer = gc.getPlayer(pCaster.getOwner())
    iUnit = gc.getInfoTypeForString('UNIT_FORT_COMMANDER')
    newUnit = pPlayer.initUnit(iUnit, pCaster.getX(), pCaster.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)
seems to have made it stop crashing, although that of course requires UU fort commanders to be explicitly defined. I'm also sure that it wasn't the new code you added to apply race; I was getting the crash with MrUnderhill's code from the other thread. It seems for some reason that implementation of creating the commander based on unitclass causes AI crashing. It's possible that something else in my personal mod is aggravating the issue; if someone could do some outside testing it would be appreciated.

Hmm... I'll look through the code for an example I can use, then.
 
Found the issue. If the civ didn't have a UU, it would crash. Here's the fixed code, new part is in bold... All it does is assign the standard unit if there's no UU.

Code:
def spellClaimFort(pCaster):
	pPlayer = gc.getPlayer(pCaster.getOwner())
	iUnit = gc.getInfoTypeForString('UNITCLASS_FORT_COMMANDER')
	infoCiv = gc.getCivilizationInfo(pPlayer.getCivilizationType())
	iUnit = infoCiv.getCivilizationUnits(iUnit)
[SIZE="4"][B]	if iUnit == -1:
		iUnit = gc.getInfoTypeForString('UNIT_FORT_COMMANDER')[/B][/SIZE]
	iRace = pCaster.getRace()
	newUnit = pPlayer.initUnit(iUnit, pCaster.getX(), pCaster.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)	
	if iRace != -1:
		newUnit.setHasPromotion(iRace, True)
	if pCaster.isHasPromotion(gc.getInfoTypeForString('PROMOTION_UNDEAD')):
		newUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_UNDEAD'), True)


Also... This MIGHT work for the race, but I'm not sure. If you could test, replace the iRace code above with this... Believe you have to leave the undead check though, the promotion is not a race.

Code:
	iRace = infoCiv.getDefaultRace()
 
Found the issue. If the civ didn't have a UU, it would crash. Here's the fixed code, new part is in bold... All it does is assign the standard unit if there's no UU.

Also... This MIGHT work for the race, but I'm not sure. If you could test, replace the iRace code above with this... Believe you have to leave the undead check though, the promotion is not a race.

Awesome, thanks for the fix. Also, that bit with the race should work even for the Scions; as long as it grabs whatever promotion is set in the <defaultrace> field for the apropriate civ it should work with pretty much anything.

I'll test it as soon as I can.
 
Tested and confirmed working. Started a game as the Scions, gave them Bambur, had him cast Claim Fort, the fort commander ended up Undead and not Dwarven.

Also tested with the Ljosalfar, the one race I've given a UU to so far, and the UU was created properly and had Elven.

Thanks a lot for the help, I was really pulling my hair out over this one. :)
 
Back
Top Bottom