--usage1: IndexPlot( centerPlot, hexAreaPlotIndex )
-- returns a plot corresponding to hexAreaPlotIndex (similar to Firaxis's GetCityIndexPlot)
--OR usage2: IndexPlot( centerPlot, ringPlotIndex, ringDistanceFromPlot )
-- returns a plot at ringDistanceFromPlot centerPlot, indexed along that ring
-- 0 to d is NE edge, d to d*2 is SE, d*2 to d*3 is S, d*3 to d*4 is SW, d*4 to d*5 is NW, d*5 to d*6 is N (0 and d*6 reference the same plot, convenient)
-- Both variants may return nil if plot falls outside the map or if parameters are invalid
function IndexPlot( plot, i, r )
-- determine if input parameters are valid - you can delete this part for a tiny performance boost
if not plot or not i or i<0 or (r and (r<0 or i>6*r)) then
print("IndexPlot error - invalid parameters")
return nil
end
-- area plot index mode ?
if not r then
-- area plot index 0 is center plot
if i == 0 then
return plot
else
-- which ring are we on ?
r = math.ceil( ( math.sqrt( 12*i + 9 ) - 3 ) / 6 );
-- determine ring plot index (substract inside area)
i = i - ( 3 * (r-1) * r ) - 1
end
end
-- determine coordinate offsets corresponding to ring index
local dx, dy;
if i <= 2*r then
dx = math.min( i, r )
elseif i<= 4*r then
dx = 3*r-i
else
dx = math.max( i-6*r, -r )
end
if i <= 3*r then
dy = math.max( r-i, -r )
else
dy = math.min( i-4*r, r )
end
-- return plot offset by (dx,dy) from center plot
return Map.GetPlotXY( plot:GetX(), plot:GetY(), dx, dy );
end
--[[
--usage: returns number of plots in hexagonal area of specified radius, minus center plot
function PlotArea( r )
return (r+1) * r * 3
end
-- Example usage1: scan all plots within "distance" of "centerPlot"; start at 0 to include center plot, 1 otherwise
for i=0, PlotArea( distance ) do
local plot = IndexPlot( centerPlot, i );
<<< do something with plot >>>
end
-- Example usage2: scan plots at distance "d" of "centerPlot"
-- loop up (+1) to go clockwise, loop down (-1) for counterclockwise
-- 0 to d is NE edge, d to d*2 is SE, d*2 to d*3 is S, d*3 to d*4 is SW, d*4 to d*5 is NW, d*5 to d*6 is N
-- but you can do any part of the ring you want, plots are numbered from 0 to d*6 clockwise (these last 2 refer to the same plot)
-- clockwise ring example:
for i=1, d*6 do
local plot = IndexPlot( centerPlot, i, d );
<<< do something with plot >>>
end
-- SW radar example:
for r=1, d do
for i=r*3, r*4 do
local plot = IndexPlot( centerPlot, i, r );
<<< do something with plot >>>
end
end
--]]