This is going to be really hard to explain, but let's give it a shot:
Code:
local unitPromotionMuniID = GameInfoTypes["PROMOTION_SHOCK_1"]
local unitPromotionMuniGaverID = GameInfoTypes["PROMOTION_WHATEVER"]
local unitCombatLandID = {[GameInfoTypes.UNITCOMBAT_MELEE]=GameInfoTypes.UNITCOMBAT_MELEE, [GameInfoTypes.UNITCOMBAT_GUN]=GameInfoTypes.UNITCOMBAT_GUN}
function MunitionsGunBoost(playerID)
local player = Players[playerID]
local MunitionsNearby = false
[COLOR="DarkRed"]for unit in player:Units() do[/COLOR]
[COLOR="Blue"]if( [/COLOR]unitCombatLandID[unit:GetUnitCombatType()] and not (unit:IsEmbarked())[COLOR="blue"]) then[/COLOR]
[COLOR="Green"]for munitionsPromotion in player:Units() do[/COLOR]
if munitionsPromotion:IsHasPromotion(unitPromotionMuniID) then
[COLOR="Orange"]if [/COLOR]Map.PlotDistance(unit:GetX(), unit:GetY(), munitionsPromotion:GetX(), munitionsPromotion:GetY()) < 3 [COLOR="orange"]then[/COLOR]
MunitionsNearby = true
end
end
[COLOR="green"]end[/COLOR]
if MunitionsNearby then
if not unit:IsHasPromotion(unitPromotionMuniGaverID) then
unit:SetHasPromotion(unitPromotionMuniGaverID, true)
end
else
if unit:IsHasPromotion(unitPromotionMuniGaverID) then
unit:SetHasPromotion(unitPromotionMuniGaverID, false)
end
end
else
if unit:IsHasPromotion(unitPromotionMuniGaverID) then
unit:SetHasPromotion(unitPromotionMuniGaverID, false)
end
end
[COLOR="darkred"]end[/COLOR]
end
GameEvents.UnitSetXY.Add(MunitionsGunBoost)
GameEvents.PlayerDoTurn.Add(MunitionsGunBoost)
So in the red loop, you loop through every unit, and only start the green loop if the unit is a gun/melee unit. In the green loop, you loop through every unit again, and check if that unit has the IbuffMyAllies-promotion and is in range of the unit of the red loop. If that is the case, the ImBuffed-promotion is given.
So why is the promotion also given to the IbuffMyAllies-unit as well when it's a gun/melee unit?
It's because you loop through
every unit in the green loop, which includes
the exact same unit. And since the distance in hexes/in range is 0 (because the units are on the same tile (because they are the same unit) and because the code doesn't care that it's the same unit), the ImBuffed-promotion is also given to that unit!
There are two possible fixes for this depending on what you want:
1. Check if "unit ~= munitionsPromotion" and only apply the promotion if that's the case (because if this is true, it means that the IbuffMyAllies is not checking itself). This does however mean that IbuffMyAllies_A can give the promotion to IbuffMyAllies_B, and the other way around. (Add it to the
orange if-statement)
OR
2. Check if "not unit:IsHasPromotion(IbuffMyAllies)", which would prevent the unit from getting buffed if it is a IbuffMyAllies-unit. (Add it to the
blue if-statement)