Border and Area plot iterators

Most compiled languages only permit "return scalar" (eg, return 1; ), most script langauges permit "return list" (eg, return 1, 2, 3; )

coroutine.resume() returns a true/false value and also all the parameters passed to the corresponding coroutine.yield() call
 
OK got ya. I'm totally familiar with the first line. The second line was the part that had me confused. I was wondering where the value for success was coming from.
 
I've updated the code in the initial post to include the changes outlined above
 
Just found this, nice lua tricks with the coroutine stuff...

In case anyone might be interested I had written an IndexPlot function purely math based, which might be more flexible to use:
Code:
--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
--]]
 
Hi, whoward, is it possible to help me write a code using your script that allows nuclear missiles, with a unique promotion, extends their blast radius range up to 3 adjacent tiles? In order to create fallout and damage units in a radius of 3 (instead of 2). I want this to use it on ICBMs missiles. Thanks you!
I think it's such an easy task for an experienced modder, but if it is not, I am willing to make a donation.
 
Last edited:
Back
Top Bottom