Okay, here's what I got so far. This is looking way too complicated. Obviously there's some means in the game to determine what the flanking bonus is while previewing combat odds. I think I just need to steal that code instead of this mess. Any tips?
Spoiler :
Code:
print("Flanking XP Award loaded")
function AddFlankingXP(iAttackingPlayer, iAttackingUnit, iAttackingUnitDamage, iAttackingUnitFinalDamage, iDefendingPlayer, iDefendingUnit, iDefendingUnitDamage, iDefendingUnitFinalDamage, iDefendingUnitMaxHitPoints)
print("Awarding Flanking XP") --Debug message
-- Is the attacker a city or ranged? If so, none of this applies.
if iAttackingUnit > 0 then
if not Game.GetPlayer(iAttackingPlayer).GetUnit(iAttackingUnit).CanRangeStrike() then
-- Okay, we're in business, this attacker can benefit from flanking.
-- Now let's iterate over the surrounding plots.
local AttackingUnit = Players[iAttackingUnit].Units[iDefendingUnit]
local DefendingUnit = Players[iDefendingPlayer].Units[iDefendingUnit]-- Need to get the init instance here
local DefendingPlot = DefendingUnit.GetPlot()
local DX,DY = DefendingUnit.GetPlot().GetX(), DefendingUnit.GetPlot().GetY()
-- This routine may be easier to do using Map.PlotXYWithRangeCheck(), but this function is not documented
for x=-1, 1, 1 do
for y=-1, 1, 1 do
--Is this plot Is this plot adjacent? Are the coords valid?
if not x=0 and y=0 do -- Skip the defending unit's plot itself.
if DX + x < 0 or DX + x > Map.GetGridSize().[1] do --This won't work through a wrap-around.
if DY + y < 0 or DY + y > Map.GetGridSize().[2] do --This could be much more efficient, should only need to check on the corners for 4 checks total, not 4 checks on all 8 plots
if (DX % 2 = 0 and x = 1 and not (y = -1 or y = 1)) or (DefendingPlot.GetX() % 2 > 0 and x = -1 and not (y = -1 or y = 1)) do --Skip nonadjacent plots
local Plot = Map.GetPlot(DX + x, DY + y)
--Get the unit at this plot, determine if it belongs to the same player, then award exp.
--Taking a break here.
end
end
end
end
end
end
end
end
end
end
Events.EndCombatSim.Add( AddFlankingXP )