Giving units variable bonus defense strength

DRK248

Chieftain
Joined
Jul 28, 2014
Messages
19
I would like to make a promotion that gives units bonus defense strength based on the amount of culture per turn that is being generated. I've had no luck finding a function that changes the base defense strength like the SetBaseCombatStrength function or one that does the opposite of GetDefenseModifier. I was wondering if there were any workarounds to this. I was thinking about having the promotion kick in whenever an enemy unit attacks an unit with the promotion but I'd rather not have to mess around with the combat mechanics.
 
  • I would think you would want multiple promotions, each with a different "DefenseMod" value.
  • Then at the beginning of the player's turn (using lua hook PlayerDoTurn) you 1st look at the player's Culture-per-Turn, and based on that erase the old promotion from all units and add the new one correct for that Culture-per-Turn "level".
  • So your promotions XML would look something like this (does not show any other XML-code such as the <Language_en_US> that would be desired to match up to the TXT_KEY references shown):
    Code:
    <GameData>
    	<UnitPromotions>
    		<Row>
    			<Type>PROMOTION_CULTURAL_DEFENSE_[COLOR="Green"][B]1[/B][/COLOR]</Type>
    			<Description>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_1</Description>
    			<Help>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_1_HELP</Help>
    			<Sound>AS2D_IF_LEVELUP</Sound>
    			<CannotBeChosen>true</CannotBeChosen>
    			[COLOR="Blue"]<DefenseMod>5</DefenseMod>[/COLOR]
    			<PortraitIndex>49</PortraitIndex>
    			<IconAtlas>PROMOTION_ATLAS</IconAtlas>
    			<PediaType>PEDIA_SHARED</PediaType>
    			<PediaEntry>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_1</PediaEntry>
    		</Row>
    		<Row>
    			<Type>PROMOTION_CULTURAL_DEFENSE_[COLOR="Red"][B]2[/B][/COLOR]</Type>
    			<Description>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_2</Description>
    			<Help>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_2_HELP</Help>
    			<Sound>AS2D_IF_LEVELUP</Sound>
    			<CannotBeChosen>true</CannotBeChosen>
    			[COLOR="blue"]<DefenseMod>10</DefenseMod>[/COLOR]
    			<PortraitIndex>49</PortraitIndex>
    			<IconAtlas>PROMOTION_ATLAS</IconAtlas>
    			<PediaType>PEDIA_SHARED</PediaType>
    			<PediaEntry>TXT_KEY_PROMOTION_CULTURAL_DEFENSE_2</PediaEntry>
    		</Row>
    	</UnitPromotions>
    </GameData>
  • Your lua code (which should be set-up in ModBuddy as an InGameUIAddin whoward69's what ModBuddy setting for what file types tutorial), would look something like this:
    Code:
    local iRequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
    local iDomainAir = GameInfoTypes.DOMAIN_AIR
    local tPromotionsToGive = {[[COLOR="Green"][B]1[/B][/COLOR]]=GameInfoTypes.PROMOTION_CULTURAL_DEFENSE_[COLOR="green"][B]1[/B][/COLOR],[[COLOR="Red"][B]2[/B][/COLOR]]=GameInfoTypes.PROMOTION_CULTURAL_DEFENSE_[COLOR="Red"][B]2[/B][/COLOR]}
    local tCulturePerTurnLevels = {[[COLOR="green"][B]1[/B][/COLOR]]=10,[[COLOR="red"][B]2[/B][/COLOR]]=30}
    
    function AssignPromotionsBasedOnCulture(iPlayer)
    	local pPlayer = Players[iPlayer]
    	if pPlayer:GetCivilizationType() == iRequiredCivilization then
    		local iCulturePerTurn = pPlayer:GetTotalJONSCulturePerTurn()
    		local iCultureLevel = 0
    		if iCulturePerTurn >= tCulturePerTurnLevels[1] then
    			for i = 1, #tCulturePerTurnLevels do
    				if iCulturePerTurn >= tCulturePerTurnLevels[i] then
    					iCultureLevel = i
    				end
    			end
    		end
    		for pUnit in pPlayer:Units() do
    			if pUnit:IsCombatUnit() and (pUnit:GetDomainType() ~= iDomainAir) then
    				for k,v in pairs(tPromotionsToGive) do
    					if k == iCultureLevel then
    						pUnit:SetHasPromotion(v, true) 
    					else
    						pUnit:SetHasPromotion(v, false) 
    					end
    				end
    			end
    		end
    	end
    end
    GameEvents.PlayerDoTurn.Add(AssignPromotionsBasedOnCulture)
  1. So if the player's Total Culture per turn is 10 or higher, units would get the level #1 "PROMOTION_CULTURAL_DEFENSE_1"
  2. If, however, the player's Total Culture Per Turn is 30 or higher, the units would get the level #2 "PROMOTION_CULTURAL_DEFENSE_2" instead of the level #1 "PROMOTION_CULTURAL_DEFENSE_1"
  3. To make more 'levels', you just follow the same pattern, and state the level number as the integer within the brackets "[ ]" in both tables, and then assign the corresponding promotion and CulturePerTurn amounts in the two tables.
  4. You need the end product within the two tables to always have 'sequential arrays' of integers as the "keys" (the integers in the brackets "[ ]"), starting with "[1]" and continuing to the highest 'level #' desired.
  5. Change the civilization name in this line
    Code:
    local iRequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
    to whatever is correct for your civilization.

----------------------------------------------------------------------------------------------------------------------------------

The reasons for this suggested approach are:
  1. There is no reliable lua 'hook event' that can intercept when a combat is about to occur. Most (all?) of those that seem like they might accomplish the task only fire when a human can see the action or are part of the action (ie, AI's attacking a human player, or a human player about to attack an enemy), and even these are wonky at best.
  2. A single promotion cannot be assigned variable 'attributes' dynamically. But swapping in defense-enhancing promotions at the start of the player's turn will have the same effect.
  3. The promotions will only have an effect when the unit is defending, and therefore fooling with the unit's base combat strength will not be required.

* Note that color-coding I put in is meant to highlight and to show (for the green and the red) how the info in the two tables match up to each other. The colors chosen aren't meant to signify good, bad, or indifferent in and of themselves so far as the colors chosen goes.
 
I was actually thinking about using the DefenseMod part from the Survivalism promotions but wasn't sure how to go about doing it. And I agree, it's better not to mess with the combat mechanics. So your method of creating a dynamic effect out of a static promotion is extremely ingenious. I would have never thought of it myself. Thank you so much! This is my first time trying to use LUA in a mod so I'm rather inexperienced.
 
Back
Top Bottom