Loop through the plots 2 tiles away from a plot (Lua)

turingmachine

Emperor
Joined
May 4, 2008
Messages
1,438
Hi guys, I'm wondering if you can help me out with something.

I know I can loop through all the plots adjacent to a plot by doing something like:

Code:
directions = {DirectionTypes.DIRECTION_NORTHEAST, DirectionTypes.DIRECTION_EAST, DirectionTypes.DIRECTION_SOUTHEAST,
              DirectionTypes.DIRECTION_SOUTHWEST, DirectionTypes.DIRECTION_WEST, DirectionTypes.DIRECTION_NORTHWEST}

      for loop, direction in ipairs(directions) do
        local pPlot = Map.PlotDirection(pCityPlot:GetX(), pCityPlot:GetY(), direction)

But I need to loop through all the plots in a two tile radius, not just one. I'm not sure how to do this. Is it something like doing a separate loop for the X and Y plot and then combining them?
 
LUA:
Code:
-- thisX, thisY are integers representing the X,Y coordinates of the center plot
local iRange = 2;
for iDX = -iRange, iRange do
	for iDY = -iRange, iRange do
		local pTargetPlot = Map.GetPlotXY(thisX, thisY, iDX, iDY);
		
		if pTargetPlot then
			-- stuff
		end
	end
end

C++:
Code:
CvPlot* pLoopPlot;

int iRange = 2;

// pCenterPlot is a CvPlot* object that represents the center plot
for(iLoopX = -(iRange); iLoopX <= iRange; iLoopX++)
{
	for(iLoopY = -(iRange); iLoopY <= iRange; iLoopY++)
	{
		pPlot = plotXY(pCenterPlot->getX(), pCenterPlot->getY(), iLoopX, iLoopY);
		
		if(pPlot != NULL)
		{
			// do stuff
		}
	}
}
 
Neither of those work, as they superimpose a rectangular grid over a hex grid. Simple maths shows they must be wrong - both the x and y loops take 5 values -2, -1, 0, 1 and 2 each for a total of 5x5 = 25 plots. A hex grid has 1 central plot, 6 plots in ring 1 and 12 plots in ring 2 for a total of 19 plots - the extra 6 all come from ring 3.

You can either do as Firaxis does
Code:
pTargetPlot = Map.PlotXYWithRangeCheck(thisX, thisY, iDX, iDY, iRange);
or if you need the plots in a determined order, use the Plot Iterators I wrote
 
Back
Top Bottom