So, I simplified things a bit. I am trying to add an exception to the following code so that the leader Kupe does not receive attrition damage on water tiles. Any ideas how to go about that?
Code:
local BaseRiverHeal = -10
local BaseAttMin,BaseAttMax = 2,10
local TerrainAttMul,CataChance,TerrainCataMul = 1,10,2
local TerrainAttMulLow,TerrainAttMulLowAvg,TerrainAttMulAvg,TerrainAttMulStrong,TerrainAttMulHuge = 5,8,12,20,40
local CataChanceLow,CataChanceAvg,CataChanceHighAvg,CataChanceStrong,CataChanceHuge = 100,125,250,500,800
local TerrainCataMulLow,TerrainCataMulLowAvg,TerrainCataMulAvg,TerrainCataMulStrong,TerrainCataMulHuge = 10,16,24,40,100
local TerrainDataTable = { [GameInfo.Terrains["TERRAIN_GRASS"].Index] = { TerrainAttMul = TerrainAttMulLow,
CataChance = CataChanceLow,
TerrainCataMul = TerrainCataMulLow },
[GameInfo.Terrains["TERRAIN_GRASS_HILLS"].Index] = { TerrainAttMul = TerrainAttMulLowAvg,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulLow },
[GameInfo.Terrains["TERRAIN_GRASS_MOUNTAIN"].Index] = { TerrainAttMul = TerrainAttMulHuge,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_PLAINS"].Index] = { TerrainAttMul = TerrainAttMulLow,
CataChance = CataChanceLow,
TerrainCataMul = TerrainCataMulLow },
[GameInfo.Terrains["TERRAIN_PLAINS_HILLS"].Index] = { TerrainAttMul = TerrainAttMulAvg,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulLow },
[GameInfo.Terrains["TERRAIN_PLAINS_MOUNTAIN"].Index] = { TerrainAttMul = TerrainAttMulHuge,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_DESERT"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_DESERT_HILLS"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_DESERT_MOUNTAIN"].Index] = { TerrainAttMul = TerrainAttMulHuge,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_SNOW"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceStrong,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_SNOW_HILLS"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceStrong,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_SNOW_MOUNTAIN"].Index] = { TerrainAttMul = TerrainAttMulHuge,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_TUNDRA"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulStrong },
[GameInfo.Terrains["TERRAIN_TUNDRA_HILLS"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulStrong },
[GameInfo.Terrains["TERRAIN_TUNDRA_MOUNTAIN"].Index] = { TerrainAttMul = TerrainAttMulHuge,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Terrains["TERRAIN_COAST"].Index] = { TerrainAttMul = TerrainAttMulLow,
CataChance = CataChanceLow,
TerrainCataMul = TerrainCataMulLow },
[GameInfo.Terrains["TERRAIN_OCEAN"].Index] = { TerrainAttMul = TerrainAttMulAvg,
CataChance = CataChanceStrong,
TerrainCataMul = TerrainCataMulHuge },
};
local FeatureDataTable = { [GameInfo.Features["FEATURE_ICE"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Features["FEATURE_MARSH"].Index] = { TerrainAttMul = TerrainAttMulStrong,
CataChance = CataChanceHuge,
TerrainCataMul = TerrainCataMulAvg },
[GameInfo.Features["FEATURE_BARRIER_REEF"].Index] = { TerrainAttMul = TerrainAttMulLow,
CataChance = CataChanceStrong,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Features["FEATURE_JUNGLE"].Index] = { TerrainAttMul = TerrainAttMulAvg,
CataChance = CataChanceStrong,
TerrainCataMul = TerrainCataMulHuge },
[GameInfo.Features["FEATURE_FOREST"].Index] = { TerrainAttMul = TerrainAttMulLowAvg,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulStrong },
[GameInfo.Features["FEATURE_OASIS"].Index] = { TerrainAttMul = 0,
CataChance = CataChanceAvg,
TerrainCataMul = TerrainCataMulHuge },
};
function GetPlotFeature (pUnitPlot)
if (pUnitPlot ~= nil) then
local fFeatureType = pUnitPlot:GetFeatureType()
if (fFeatureType ~= -1) and (fFeatureType ~= nil) then
--the plot has a FeatureType registered within xml table <Features>
fFeature = GameInfo.Features[fFeatureType].FeatureType
isFeatureWonder = GameInfo.Features[fFeatureType].NaturalWonder
else
fFeature = "FEATURE_NONE"
isFeatureWonder = "FEATURE_NONE"
end
return fFeature, isWonder
end
end
function damageAndDestroyUnit(pPlayer,iUnit,minDam,maxDam)
local tKillUnits = {}
local pPlayerToCheck = Players[pPlayer]
local pUnits = pPlayerToCheck:GetUnits()
local pUnit = pUnits:FindID(iUnit)
local pPlayerEra = pPlayerToCheck:GetEra()+1
if (pUnit ~= nil) and (pUnit:IsDead() == false) and (pUnit:IsDelayedDeath() == false) then
local UnitPlot = Map.GetPlot(pUnit:GetX(), pUnit:GetY())
local UnitPlotOwner = UnitPlot:GetOwner()
if (UnitPlot ~= nil) and (UnitPlotOwner ~= pPlayer) then
local unitDamage = math.random(minDam,maxDam)
if (pUnit:GetDamage() + unitDamage) >= 150 then
table.insert(tKillUnits, pUnit)
else
pUnit:ChangeDamage(unitDamage)
print("inflicted "..unitDamage.." to unit")
end
end
end
--now we've completed the iteration through table pUnits:Members() and can safely delete units listed as members
for k,pUnit in ipairs(tKillUnits) do
pUnits:Destroy(pUnit)
print("a unit was destroyed because of attrition")
end
end
function IdleDamage()
local players = Game.GetPlayers()
for k, pPlayer in ipairs (players) do
local pUnits = pPlayer:GetUnits()
for i, pUnit in pUnits:Members() do
if (pUnit ~= nil) and (pUnit:IsDead() == false) and (pUnit:IsDelayedDeath() == false) then
local pUnitID = pUnit:GetID()
local pUnitPlot = Map.GetPlot(pUnit:GetX(), pUnit:GetY())
--Check for attrition exceptions NOTE: need to add diplomatic exceptoins and for Maoi civ, barbarians have attrition reduced
local pUnitOwnerID = pPlayer:GetID()
local pUnitOwnerName = GameInfo.Leaders[pUnitOwnerID].LeaderType
local pUnitPlotOwner = pUnitPlot:GetOwner()
local IsFreshWaterAdj = pUnitPlot:IsFreshWater()
local tTerrain = GameInfo.Terrains[pUnitPlot:GetTerrainType()].TerrainType
local TerrainAttMul,CataChance,TerrainCataMul = 0, 0, 0
GetPlotFeature(pUnitPlot)
local pUnitOwnerDiplo = pPlayer:GetAi_Diplomacy():GetDiplomaticState(pUnitPlotOwner)
if pUnitOwnerDiplo == nil then
pUnitOwnerDiplo = "NONE"
end
if (pUnitOwner == pUnitPlotOwner) or
string.match(pUnitOwnerDiplo, "FRIENDLY") or
string.match(pUnitOwnerDiplo, "ALLIANCE") or
(isFeatureWonder == true ) then
elseif IsFreshWaterAdj == true then
pUnit:ChangeDamage(BaseRiverHeal)
else
--Damage mulitpliers
local PlotTerrainDamages = TerrainDataTable[pUnitPlot:GetTerrainType()]
if (fFeature ~= "FEATURE_NONE") then
--Get damage multipliers from feature
TerrainAttMul,CataChance,TerrainCataMul = PlotFeatureDamages.TerrainAttMul, PlotFeatureDamages.CataChance, PlotFeatureDamages.TerrainCataMul
else
--Get damage mulitpliers from terrain
TerrainAttMul,CataChance,TerrainCataMul = PlotTerrainDamages.TerrainAttMul, PlotTerrainDamages.CataChance, PlotTerrainDamages.TerrainCataMul
end
local FinalAttMin = (BaseAttMin*TerrainAttMul)/2
local FinalAttMax = (BaseAttMax*TerrainAttMul)/2
damageAndDestroyUnit(pUnitOwnerID,pUnitID,FinalAttMin,FinalAttMax)
end
print("-----------------------------")
print("-----------------------------")
print("----------UNIT INFO----------")
print("Unit Owner:")
print(pUnitOwnerName)
print("Unit relations to plot owner:")
print(pUnitOwnerDiplo)
print("Unit ID:")
print(pUnitID)
print("Unit plot:")
print(pUnitPlotIndex)
print("----------PLOT INFO----------")
print("Plot Owner:")
print(pUnitPlotOwner)
print("Terrain type")
print(tTerrain)
print("Feature type")
print(fFeature)
print("Is river:")
print(IsFreshWaterAdj)
print("Is feature wonder:")
print(isFeatureWonder)
print("-----------------------------")
print("-----------------------------")
end
end
end
end