-- Moonwell
-- Author: Sigmaflux
-- Original Mod by Bamington.
-- DateCreated: 8/6/2013 3:10:48 PM
--------------------------------------------------------------

print("This is the Night Elf Moonwell Mod script");

--Tile Directions----------------------------
local MOONWELL = GameInfoTypes.IMPROVEMENT_MOON_WELL;
local NE = DirectionTypes.DIRECTION_NORTHEAST;
local E = DirectionTypes.DIRECTION_EAST;
local SE = DirectionTypes.DIRECTION_SOUTHEAST;
local SW = DirectionTypes.DIRECTION_SOUTHWEST;
local W = DirectionTypes.DIRECTION_WEST;
local NW = DirectionTypes.DIRECTION_NORTHWEST;
---------------------------------------------

GameEvents.PlayerDoTurn.Add(

function(iPlayer)
 local pPlayer = Players[iPlayer];
 print("Now running moonwell script.");
 --Check Alive
 if(pPlayer:IsAlive()) then
 print("Player is Alive");
  --Check Night Elf
 -- if(pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_NIGHT_ELF) then
  -- print("Player is Nelf");
   --For each unit
   for pUnit in pPLayer:Units() do
    print("Checking units");
    --Check Land unit.
    if(pUnit:GetDomainType() == DomainTypes.DOMAIN_LAND) then
	 print("They are land");
     --Check combat unit and not embarked.
     if pUnit:IsCombatUnit() and not pUnit:IsEmbarked() then
	  print("Not embarked");
      local uPlot = pUnit:GetPlot() --Units current tile.
      if(pUnit:GetOwner() == uPlot:GetOwner()) then
	   print("Determined owner");
       --Get Each direction around the unit.
       local nePlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), NE);
       local ePlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), E);
       local sePlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), SE);
       local swPlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), SW);
       local wPlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), W);
       local nwPlot = Map.PlotDirection(uPlot:GetX(), uPlot:GetY(), NW);
	    print("Plotting around unit");

       --Check each direction for a moonwell.
       --If found, heal for 5
       if (uPlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (nePlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (ePlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (sePlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (swPlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (wPlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
       elseif (nwPlot:GetImprovementType() == MOONWELL) then
        pUnit:ChangeDamage(-1 * math.min(pUnit:GetDamage(), 5));
		 print("Heals complete");
       end
      end
     end
    end
   end
  end
 end
end)