GameEvents.UnitPreKill(iPlayer, iUnit, _, iX, iY, bDelay, iByPlayer) fires whenever a unit dies.
iByPlayer will be -1 if you're disbanding a unit. However, it will also be -1 in some other cases, such as when expending a Great Person! As far as I know, there is no way to distinguish between the two. (Limiting the ability to Combat Units only could solve this issue, but I'm unsure whether things such as upgrading a combat unit or embarking it also makes this gameEvent fire with iByPlayer = -1)
I can't exactly remember if you need to check for 'bDelay' or 'not bDelay' to prevent your code from firing twice, so you'd have to experiment with that for a bit.
You can get the plot where the unit died using Map.GetPlot(iX, iY)
You can loop over adjacent plots using:
Code:
--pPlot has been defined somewhere over here (using pPlot = Map.GetPlot(...) in your case)
for i = 0, DirectionTypes.NUM_DIRECTION_TYPES - 1, 1 do
local pPlotAdj = Map.PlotDirection(pPlot:GetX(), pPlot:GetY(), i)
if pPlotAdj then
--do sth
end
end
If you want units which are further than 1 tile away to be affected too, then you'd need Whoward's PlotIterators to loop over all those plots as well.
You can loop over all units on a plot using:
Code:
--pPlot has been defined somewhere over here
for j = 0, pPlot:GetNumLayerUnits()-1 do
--this also loops over trade units!
local pAdjUnit = pPlot:GetLayerUnit(j)
--do sth with the adjacent unit
end
Setting and changing HP (or rather, how much damage a unit has received) can be done using:
Code:
pUnit:SetDamage(iNewValue)
pUnit:ChangeDamage(iChange)
Getting a unit's current HP can be done using:
Code:
pUnit:GetMaxHitPoints() - pUnit:GetDamage()