Pablostuka
King
@Prof. Garfield I had to code this generic function to retrieve a random coordinate (just x,y) inside a bigger rectangle. If you find it useful feel free to add it to the generalLibrary.lua
It doesn't take into account the Z coordinate and doesn't convert to specific tile objects as this is done by any calling/wrapper function. It's been successfully tested in the Lua online interpreter with a couple of examples - https://www.lua.org/cgi-bin/demo
Regards,
Pablo

SQL:
-- getRandomTileInRectangle (cornersTable)
-- cornersTable -> Table of rectangle corners
-- returns -> a random valid coordinate (x,y) inside the rectangle passed by
--[[
EXAMPLE:
cornersTable = {{62,0},{76,0},{76,6},{62,6}}
valid return locations are (62,2),(64,4),(63,3) or (65,5)
In Civ2 tiles X and Y have to be of the same type (both evens or both odds)
--]]
local function getRandomTileInRectangle(cornersTable)
local x,y = 0,0
local xCoords = {}
local yCoords = {}
-- First, loop through cornersTable to split X and Y from all corners of the rectangle
for _,tile in pairs(cornersTable) do
-- add to x,y tables
table.insert(xCoords, tile[1])
table.insert(yCoords, tile[2])
end
-- Sort to get mins and max limits
table.sort(xCoords)
table.sort(yCoords)
-- Define Min and Max values for both X and Y (the limits for the rectangle)
local minX,maxX,minY,maxY = xCoords[1],xCoords[#xCoords],yCoords[1],yCoords[#yCoords]
-- Generate the random X and Y keeping in mind in Civ2 tiles X and Y have to be of the same type (both evens or both odds)
math.randomseed(os.time())
x,y = math.random(minX,maxX),math.random(minY,maxY)
-- Check that X and Y are both evens or odds
while x % 2 ~= y % 2 do
-- Try again
x,y = math.random(minX,maxX),math.random(minY,maxY)
end
return x,y
end
Regards,
Pablo