[BNW] Lowering & Raising Combat Strength

OnlyHeStandsHere

Chieftain
Joined
May 4, 2018
Messages
46
So I've got some working code that lowers a unit's combat strength when in enemy territory... continuously and permanently. I'm not confused by the outcome, since it's doing what I intended for the time being. But is there a way to reverse the penalty when the unit leaves enemy territory (without, for example, continuously buffing the unit every turn)?

Here's what I got by the way:
Code:
function NeutralityPromotion(iPlayer, iUnitID, iX, iY)
    local pPlayer = Players[iPlayer]
    local pUnit = pPlayer:GetUnitByID( iUnitID );
    local pPlot = Map.GetPlot(iX, iY)
   
    local Promotion = GameInfoTypes["PROMOTION_NEUTRALITY"]
    local iBaseStrength = pUnit:GetBaseCombatStrength()

    local iOurTeam = pPlayer:GetTeam()

    if pUnit:IsHasPromotion(Promotion) and pPlot:GetOwner() ~= iPlayer then
        if (Teams[iOurTeam]:IsAtWar(pPlot:GetTeam())) then
            pUnit:SetBaseCombatStrength(iBaseStrength * .75)            --Neutrality penalty
        end
    end
end

GameEvents.UnitSetXY.Add(NeutralityPromotion)

One thought I had to at least stop the "continuously" part of the problem would be to make another promotion that is given to the unit when entering enemy territory. Then I'd add a bit of code to check if the unit already has the penalty promotion. But, my understanding is that you cannot make a unit lose its promotion, so it'd still be permanently debuffed.
 
you can make a unit lose its promotion by

Code:
pUnit:SetHasPromotion(PromotionName, false)

But that won't help much if you're lowering the Unit's strength through Lua, because after you remove that promotion, their baseStrength would still be the same.

Hence, what I would do is any time you lower a unit's strength, store that unitID in a table and how many times you lowered that unit's strength in another table. As soon as you detected that your unit reentered friendly territory, do some reverse math on that unit and however many times you lowered its strength.
 
Last edited:
Well imagine that. I had read somewhere that units couldn't unlearn a promotion, but I must have misinterpreted it. That alone gives me a couple ideas.

As for the second part, I had thought it might be something like that, though I wasn't honestly sure how it works with Lua and Civ. I'm not too familiar with creating tables for that kind of thing, but it's something I'll look into.

EDIT: I was messing around with the removing promotions and I was able to get something where the penalty only applies once and then is removed when leaving enemy territory:

Code:
function NeutralityPromotion(iPlayer, iUnitID, iX, iY)
    local pPlayer = Players[iPlayer]
    local pUnit = pPlayer:GetUnitByID( iUnitID );
    local pPlot = Map.GetPlot(iX, iY)
   
    local Promotion = GameInfoTypes["PROMOTION_NEUTRALITY"]
    local PromotionPenalty = GameInfoTypes["PROMOTION_NEUTRALITY_PENALTY"]
    local iBaseStrength = pUnit:GetBaseCombatStrength()

    local iOurTeam = pPlayer:GetTeam()

    if pUnit:IsHasPromotion(PromotionPenalty) then            --Check if penalty is already applied
        if (Teams[iOurTeam]:IsAtWar(pPlot:GetTeam())) then
            return
        else
            pUnit:SetBaseCombatStrength(iBaseStrength / .75)
            pUnit:SetHasPromotion(PromotionPenalty, false)
            return
        end
    elseif pUnit:IsHasPromotion(Promotion) and pPlot:GetOwner() ~= iPlayer then
        if (Teams[iOurTeam]:IsAtWar(pPlot:GetTeam())) then
            pUnit:SetBaseCombatStrength(iBaseStrength * .75)            --Neutrality penalty
            pUnit:SetHasPromotion(PromotionPenalty, true)
        end
    end
end

GameEvents.UnitSetXY.Add(NeutralityPromotion)

It's a little inelegant, but it works! :0
 
Last edited:
How are you planning to have this change in combat strength show up in the user interface? Are you planning to have something like the other promotions where if you mouse your unit to attack, the UI will say "Neutrality combat modifier: 75%"?
 
Right now I have a promotion show when the penalty is in effect. If you mouse over it it just states that the combat strength is lowered by whatever percentage, and the combat strength of the unit itself will update when entering/leaving enemy territory. Unfortunately, it does not appear as one of the percentages in the combat UI. I imagine that would require a different method, and one that might be beyond what I'm capable of tackling at the moment.
 
Top Bottom