//***
function GetTileCenterCoordinate takes real a returns real
if (a >= 0.) then
set a = R2I((a/128) + .5)
else
set a = R2I((a/128) - .5)
endif
return a*128
endfunction
//***
function IsTerrainType takes real x, real y, string tt, integer ic returns integer
if tt != "blight" and tt != "water" then
//actions when a terrain type is searched
if GetTerrainType( x, y) == S2I(tt) then
set ic = ic + 1
return ic
else
return ic
endif
elseif tt == "blight" then
if IsPointBlighted( x, y) == true then
set ic = ic + 1
return ic
else
return ic
endif
elseif tt == "water" then
if IsTerrainPathable(x,y,PATHING_TYPE_WALKABILITY) and not IsTerrainPathable(x,y,PATHING_TYPE_AMPHIBIOUSPATHING) then
set ic = ic + 1
return ic
else
return ic
endif
endif
return ic
endfunction
function GetTerrainTypeInRange takes location p, string tt, integer range, string s returns integer
local integer pn = 1 // algebraic sign +/-
local integer ic = 0 // counts matching tiles
local real x
local real y
local real x2
local real y2
local real y3
set x = GetLocationX(p)
set y = GetLocationY(p)
set x = GetTileCenterCoordinate(x) // x of the next tile center to a point
set y = GetTileCenterCoordinate(y) // y of the next tile center to a point
set ic = IsTerrainType( x, y, tt, ic) // check the initial tile
if s == "circle" then
loop // decide direction, first positive, then negative
exitwhen pn < -1 // runs two times, once positive and once negative
set x = GetTileCenterCoordinate(GetLocationX(p)) // reset to inital tile
set x2 = x
set y = GetTileCenterCoordinate(GetLocationY(p)) // reset to inital tile
set y2 = y
set y3 = y
loop // update x tile, moving along the x axis dependent on pn
exitwhen DistanceBetweenPoints( p, Location(x2, y)) > range
set x2 = x2 + 128*pn
set ic = IsTerrainType( x2, y, tt, ic) // check tiles on the x axis
// go up and down in y direction
loop
exitwhen DistanceBetweenPoints( p, Location(x2, y2)) > range
set y2 = y2 + 128
set ic = IsTerrainType( x2, y2, tt, ic) // check tiles on the positive y branch
endloop
loop
exitwhen DistanceBetweenPoints( p, Location(x2, y3)) > range
set y3 = y3 - 128
set ic = IsTerrainType( x2, y3, tt, ic) // check tiles on the negative y branch
endloop
endloop
set pn = -1 // reverse direction, run again once
endloop
endif
return ic
endfunction