//! library TreeChopper
globals
destructable array DestAr
integer Int = 0
destructable ClosestDest = null
rect ToCheck
endglobals
function SetDestArray takes nothing returns nothing
set DestAr[Int] = GetEnumDestructable()
set Int = Int + 1
endfunction
function XYInRect takes rect whichRect, real x, real y returns boolean
return ((GetRectMinX(whichRect) <= x) and (GetRectMaxX(whichRect) >= x) and (GetRectMinY(whichRect) <= y) and (GetRectMaxY(whichRect) >= y))
endfunction
function GetClosestDest takes real x, real y, boolexpr e returns destructable
local real md = 100000
local real d
local unit u
local real dx
local real dy
local integer i = 0
set ClosestDest = null
set Int = 0
call EnumDestructablesInRect(bj_mapInitialPlayableArea, e, function SetDestArray)
loop
exitwhen i == Int
// call GroupRemoveUnit(g, u)
set dx = GetDestructableX(DestAr[i]) - x
set dy = GetDestructableY(DestAr[i]) - y
if (dx * dx + dy * dy) / 100000 < md and XYInRect(ToCheck,x,y) then
set ClosestDest = DestAr[i]
set md = (dx * dx + dy * dy) / 100000
endif
set i = i + 1
endloop
call DestroyBoolExpr(e)
return ClosestDest
endfunction
function TreeFilter takes nothing returns boolean
local integer d = GetDestructableTypeId(GetFilterDestructable())
return d == 'ATtr' or d == 'BTtw' or d == 'KTtw' or d == 'YTft' or d == 'JTct' or d == 'YTst' or d == 'YTct' or d == 'YTwt' or d == 'JTwt' or d == 'JTwt' or d == 'FTtw' or d == 'CTtr' or d == 'ITtw' or d == 'NTtw' or d == 'OTtw' or d == 'ZTtw' or d == 'WTst' or d == 'LTlt' or d == 'GTsh' or d == 'Xtlt' or d == 'WTtw' or d == 'Attc' or d == 'BTtc' or d == 'CTtc' or d == 'ITtc' or d == 'NTtc' or d == 'ZTtc'
endfunction
function PeasantChopClosestTree takes unit TownHall, unit Harvester, rect HarvestArea returns nothing
local real x = GetUnitX(TownHall)
local real y = GetUnitY(TownHall)
local boolexpr b = Condition(function TreeFilter)
set ToCheck = HarvestArea
call IssueTargetOrder(Harvester, "harvest", GetClosestDest(x, y, b))
call DestroyBoolExpr(b)
set b = null
endfunction
//! endlibrary