[LUA] Adding combat bonus according to yield output?

Hypereon

He was a Consul of Rome!
Joined
Jun 19, 2012
Messages
414
Location
Helsinki, Finland
I'm completely new to Lua and I'm working on a complete overhaul to my Finnish Civilization mod that will add alternative leaders and complete updates to current playstyle.

When pondering a new UA for Mannerheim to replace a dull straigth strength bonus in own lands I came to this:

Northern Stalwart: Units gain combat bonus (up to 40%) in friendly territory according to nation's cultural strength.

The amounts I thought are 1% bonus for every 5 Culture per turn on friendly territory.
Is there a way to make this in Lua easily and how? Any help would be great, I'm especially asking Lua experts how to make the code.
Thanks in advance!:)
 
Take a look at the Condottiere from Pouakai's Tuscany civ (which increases combat strength proportional to gold per turn) or the Yamato Swordsman from my own Moriya Shrine civ (which increases combat strength proportional to faith per turn) -- they should give you a good starting point to use.

Basically, what you'd have to do is give your units a free promotion, and then have a Lua function that modifies the combat strength of units with that promotion. I don't have the code handy to me at the moment, but the civs I mentioned should help you figure out how to write the loop.

The trickier part might be the "friendly lands" component, although I imagine you just need to include a check to see who the owner of the plot the unit is on is, and only assign the bonus if it's your civ.

EDIT: I just remembered I did have some of the Lua code from an as-yet unfinished civ that uses the basic loop:

Code:
local StrengthofAges = GameInfo.UnitPromotions["PROMOTION_STRENGTH_OF_AGES"].ID

function StrengthofAgesCalc(iPlayer)
	local pPlayer = Players[iPlayer];
	for unit in pPlayer:Units() do
		if unit:IsHasPromotion(StrengthofAges) then
			local iBaseStrength = unit:GetBaseCombatStrength();
			local eraValue = pPlayer:GetCurrentEra();
			if (eraValue == GameInfo.Eras["ERA_CLASSICAL"].ID) then
				iDeltaS = 2;
			end
			if (eraValue == GameInfo.Eras["ERA_MEDIEVAL"].ID) then
				iDeltaS = 4;
			end
			if (eraValue == GameInfo.Eras["ERA_RENAISSANCE"].ID) then
				iDeltaS = 6;
			end
			print("Strength of Ages: " .. iDeltaS)
			unit:SetBaseCombatStrength(iBaseStrength + iDeltaS)
		end
	end
end
GameEvents.PlayerDoTurn.Add(StrengthOfAgesCalc)

This loop increases a unit's combat strength by what era it's in, but you could adjust the code so that the modifier would instead be proportional to how much culture you generate per turn.

(It doesn't include the friendly lands check, though...)

Hope this helps!
 
Okay, now it looks like this; it should add 1% combat strength for every 5 CPT up to 50%:
Code:
print("loaded")

local StalwartSpirit = GameInfo.UnitPromotions["PROMOTION_STALWART_SPIRIT"].ID

function StalwartSpiritCalc(iPlayer)
	local pPlayer = Players[iPlayer];
	for unit in pPlayer:Units() do
		if unit:IsHasPromotion(StalwartSpirit) then
			local iStrength = GameInfo.Units[unit:GetUnitType()].Combat
			local iDeltaS = math.floor((pPlayer:CalculateCultureRate() * 0.002 ))
			if iDeltaS < 0 then
				return
			elseif iDeltaS > 0.5 then
				unit:SetBaseCombatStrength(iStrength + iStrength * 0.5)
			else
				unit:SetBaseCombatStrength(iStrength + iDeltaS * iStrength)
			end
		else
		end
	end
end

GameEvents.PlayerDoTurn.Add(StalwartSpiritCalc)
I'm not sure if the references are right, could somebody check this out.

Now I needed to add a friendly territory check for the unit - could somebody help me out?
 
Slight correction. Tuscany's mine!

Anyway. I'm guessing you've based that off my code? That old version has a flaw in which the strength will not be corrected if the Gold jumps suddenly from a high positive to a negative. I doubt that'd be an issue with culture, but it may be good to keep in mind.

If position actually matters, you'll need to trigger the effect when a unit moves as well (GameEvents.UnitSetXY). You'll also need to define what "friendly" actually means as well. (Your team's territory only? What about CSs, etc).
 
Slight correction. Tuscany's mine!

Ahhh, my apologies! In hindsight, I recall Pouakai specifically corrected me on that same mistake once before. Although, in my defense, I think Tuscany is available under Pouakai's name both here and on Steam, so it does make it easier to track down the civ for download.

Still, it is a very nicely done civ!

Anyway. I'm guessing you've based that off my code? That old version has a flaw in which the strength will not be corrected if the Gold jumps suddenly from a high positive to a negative. I doubt that'd be an issue with culture, but it may be good to keep in mind.

If position actually matters, you'll need to trigger the effect when a unit moves as well (GameEvents.UnitSetXY). You'll also need to define what "friendly" actually means as well. (Your team's territory only? What about CSs, etc).

I assume this is because it's tied to the PlayerDoTurn event? It does mean any changes mid-turn won't be reflected in stats.

For the second part of the ability, my first thought was to simply add an if...then statement to the PlayerDoTurn function, checking to see which units were in your territory at the start of the turn and giving them the promotion. This does mean that units can have the promotion and retain it upon leaving territory, or not have the promotion and not gain it by moving in, so it may not be quite what was desired, but is a simpler version to code. It could be covered with a bit of appropriate text ("Units starting within friendly territory gain X bonus"), but you're right, it would be better to tie it to UnitSetXY. In this case, it would also actually help the combat strength stay more accurate to mid-turn changes, since the strength would be more dynamically updated as the unit moves.

I suspect a UnitSetXY version wouldn't be too much harder to code, since you wouldn't even need the unit loop... just check for the promotion and who the owner of the plot is...
 
Meh. SetBaseCombatStrength has no usage for ranged attacks and it is actually something very different to % combat bonus.

To do what you wanted to do:
GameEvents PlayerDoTurn is enough, SetXY is optional (probably, it matters for Landknechts).
%bonus = math.min(math.floor(player:Get Total Culture per turn / 5), 50);
Tricky part is making several promotions with different FriendlyCombatModifier (or sth like that, look for Himeji or Shoshone promotion): 1%, 2%, 4%, 8%, 16%, 32%.
Then you are adding/removing those promotions to achieve %bonus.

You can of course reconsider your ability, but it is the answer to OP.
 
I assume this is because it's tied to the PlayerDoTurn event? It does mean any changes mid-turn won't be reflected in stats.

Actually because I just used return if the GPT was less than zero; if there was a significant enough jump (say +13 to -1) then the Combat Strength wouldn't have been corrected back to base strength. (you can see something similar in the code Hypereon posted).
 
Meh. SetBaseCombatStrength has no usage for ranged attacks and it is actually something very different to % combat bonus.

Ahh, you do have a point there. I suppose it's because I've been messing around with SetBaseCombatStrength a lot lately that it was the first thought that came to mind. For a start, you could probably make it simpler by just saying something like, "For every 50 culture you generate per turn, gain a 10% combat bonus, up to a maximum of 40%". Then you'd only need to make 10%, 20%, 30%, and 40% promotions and assign them accordingly.
 
The easy way:
Okay, seems that I'll drop the Friendly lands bonus out and connect it into the Finnish UB. To not make it too OP, I'll also drop the max bonus to 30% with +300 CPT. What reference gives combat strength to ranged units also?

The hard and more interesting way:
OR
Assign different cumulative promotions based on cultural output; and name those individually, like for example
Finnish Spirit (no pun intended:crazyeye:) +10% (50 cpt)
National Unity +10% (100 cpt)
Heritage of Kalevala: Survivalism and +10% (150 cpt)
Finnish Toughness +10% (250 cpt)
Patriotic Fervor +10%, +25% outside own territory (500 cpt)

And the UA could be like: Military Units get unique promotions based on nation's Culture output.
But, how can it be done in Lua? How do I grant (and disable) a promotion to every unit when culture per turn reaches a certain amount?
 
Back
Top Bottom