Looping through cutom global table?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
First up, I still have my learner plates on so be gentle.....

I need to know the proper way to loop through a custom global table containing coordinates for tiles that are PLAINS terrain on my custom map.

The reason for this is that I have code that converts grass and plains tiles to snow during winter, but then needs to know which tiles to convert back to plains and grass when converting back to summer.

Here is an example of the global table:
Code:
g_SeasonalTerrain{
{X = 71, Y = 0, Type = "Plains"},
{X = 72, Y = 0, Type = "Plains"},
{X = 73, Y = 0, Type = "Plains"},
{X = 74, Y = 0, Type = "Plains"},
{X = 75, Y = 0, Type = "Plains"},
{X = 76, Y = 0, Type = "Plains"},
{X = 77, Y = 0, Type = "Plains"},
}

Can I match up the current plot X,Y values against such a table in any logical way or does the structure need to be modified?

Assuming the x and y values are met in the table then I can write code to change that particular tile back to plains instead of grassland.

Many thanks in advance for any assistance
 
Im using plotKey for such tables to save me some times, the table structure would be like that :

Code:
g_SeasonalTerrain{
{PlotKey = "71,0", Type = "Plains"},
{PlotKey = "72,0", Type = "Plains"},
}

or my preference :

Code:
g_SeasonalTerrain{
["71,0"] = { Type = "Plains"},
["72,0"] = { Type = "Plains"},
}


and I use those functions before adding a new row in the table or to extract one :

Spoiler :
Code:
--	here (x,y) = (0,0) is bottom left of map in Worldbuilder.
function GetPlot (x,y)
	local plot = Map:GetPlotXY(y,x)
	return plot
end

-- return a key string used in saved table refering to a plot position (can't save object in table...)
function GetPlotKey ( plot )
	-- set the key string used in TerritoryMap
	local x = plot:GetX()
	local y = plot:GetY()
	local plotKey = x..","..y
	return plotKey
end

-- return the plot x and y refered by the key string
function GetPlotXYFromKey ( plotKey )
	local pos = string.find(plotKey, ",")
	local x = string.sub(plotKey, 1 , pos -1)
	local y = string.sub(plotKey, pos +1)
	return x, y
end

-- return the plot refered by the key string
function GetPlotFromKey ( plotKey )
	local pos = string.find(plotKey, ",")
	local x = string.sub(plotKey, 1 , pos -1)
	local y = string.sub(plotKey, pos +1)
	local plot = Map:GetPlotXY(y,x)
	return plot
end

note : yes, I never learned how to use properly Map:GetPlotXY() or Map:GetPlot() :blush:

then to fill your table (structured as on the second example), I would use :

Code:
for iPlotLoop = 0, Map.GetNumPlots()-1, 1 do
	local plot = Map.GetPlotByIndex(iPlotLoop)
	local terrainType = plot:GetTerrainType()
	if terrainType == TerrainTypes.TERRAIN_PLAINS then
		g_SeasonalTerrain[GetPlotKey ( plot )] = { Type = "Plains" }
	end
end

and if I want to know what was the previous terrain of a specific plot, I'll just check

Code:
g_SeasonalTerrain[GetPlotKey ( plot )].Type


I'm not a programmer, so surely there's something a lot simpler, but at least I almost know how this work, and it does work :D
 
Huge thanks Gedemon. This info really helps out given I am mostly learning using your R.E.D. code.

I am familiar with the functions you mention so will give it a try the way you outlined. Best part is the values are hard coded (static map for scenario) so don't need to write to table but others will most likely use this info as well.

As for the old 'I'm not a programmer' routine.....we all know your modding and programming skills rock :goodjob:
 
You can also index a plot by its index. I use these two functions to get a plot index from either plot object or x, y:

Code:
function GetPlotIndexFromPlot(plot)
	local GetPlotIndexFromXY = GetPlotIndexFromXY
	local x, y = plot:GetX(), plot:GetY()
	return GetPlotIndexFromXY(x, y)
end

local iW, iH = Map.GetGridSize()

function GetPlotIndexFromXY(x, y)
	return y * iW + x
end

Then use core functions:

Map.GetPlotByIndex(iPlot)
Map.GetPlot(x, y)

to get the plot object from plot index or x,y.
 
Back
Top Bottom