function GenerateTerrain()
print("Generating terrain - (Planet Simulator)")
-- Terrain Type key:
-- 0.0 == Water/Mountain
-- 1.0 == Snow
-- 2.0 == Tundra
-- 3.0 == Desert
-- 4.0 == Plains
-- 5.0 == Grass
--set up a blank floatMap for terrain types
local terrainMap = FloatMap:New(mapWidth, mapHeight, true, false)
for i = 0, mapLength, 1 do
terrainMap.data[i] = 0.0
end
--### Snow ###--
--the coldest tiles outside of the tropics become snow regardless of rainfall
local snowMap = FloatMap:New(mapWidth, mapHeight, true, false)
for i = 0, mapLength, 1 do
local y = math_floor(i / mapWidth)
local zone = terrainMap:GetZone(y)
local tropics = (zone == mc.NEQUATOR or zone == mc.SEQUATOR)
local plot = Map_GetPlotByIndex(i)
if ElevationMap:IsBelowSeaLevel(i) then
snowMap.data[i] = 0.0 --we'll disregard zeros during threshold determination
elseif tropics or plot:GetPlotType() == PlotTypes.PLOT_MOUNTAIN then
snowMap.data[i] = 1.0 --cludge the tropic latitudes to prevent snow here
else
snowMap.data[i] = temperatureMap.data[i]
end
end
local snowThreshold = snowMap:FindThresholdFromPercent(mc.snowPercent,false,true)
--set the coldest tiles to snow in our terrainMap
for k = 1, #landTab, 1 do
local i = landTab[k]
if snowMap.data[i] < snowThreshold then
terrainMap.data[i] = 1.0
end
end
--### Tundra ###--
--the next coldest tiles become tundra regardless of rainfall
local tundraMap = FloatMap:New(mapWidth, mapHeight, true, false)
for i = 0, mapLength, 1 do
local plot = Map_GetPlotByIndex(i)
if ElevationMap:IsBelowSeaLevel(i) then
tundraMap.data[i] = 0.0 --we'll disregard zeros during threshold determination
elseif terrainMap.data[i] ~= 0.0 or plot:GetPlotType() == PlotTypes.PLOT_MOUNTAIN then
tundraMap.data[i] = 1.0 --cludge the tiles that have already been assigned on our terrainMap
else
tundraMap.data[i] = temperatureMap.data[i]
end
end
local tundraThreshold = tundraMap:FindThresholdFromPercent(mc.tundraPercent,false,true)
--set the next coldest tiles to tundra in our terrainMap
for k = 1, #landTab, 1 do
local i = landTab[k]
if tundraMap.data[i] < tundraThreshold then
terrainMap.data[i] = 2.0
end
end
--### Desert ###--
--find the average temperature on the horse latitudes to determine the minimum Desert temperature
local Ys = {terrainMap:GetYForLatitude(mc.horseLatitudes), terrainMap:GetYForLatitude(-mc.horseLatitudes)}
local sum = 0
local count = 0
for k, y in next, Ys do
local i = y * mapWidth
for x = 0, mapRight do
sum = sum + temperatureMap.data[i + x]
count = count + 1
end
end
local desertMinTemperature = sum / count
print(string.format("desertMinTemperature = %.6f", desertMinTemperature))
--the driest tiles above desertMinTemperature become desert
local desertMap = FloatMap:New(mapWidth, mapHeight, true, false)
for i = 0, mapLength, 1 do
local plot = Map_GetPlotByIndex(i)
if ElevationMap:IsBelowSeaLevel(i) then
desertMap.data[i] = 0.0 --we'll disregard zeros during threshold determination
elseif terrainMap.data[i] ~= 0.0 or temperatureMap.data[i] < desertMinTemperature or plot:GetPlotType() == PlotTypes.PLOT_MOUNTAIN then
desertMap.data[i] = 1.0 --cludge the tiles that have already been assigned on our terrainMap or are too cold for desert
else
desertMap.data[i] = rainfallMap.data[i]
end
end
local desertThreshold = desertMap:FindThresholdFromPercent(mc.desertPercent,false,true)
--apply the desert tiles to terrainMap
for k = 1, #landTab, 1 do
local i = landTab[k]
if desertMap.data[i] < desertThreshold then
terrainMap.data[i] = 3.0
end
end
--### Plains ###--
--the driest tiles left after placing desert become plains
local plainsMap = FloatMap:New(mapWidth, mapHeight, true, false)
for i = 0, mapLength, 1 do
local plot = Map_GetPlotByIndex(i)
if ElevationMap:IsBelowSeaLevel(i) then
plainsMap.data[i] = 0.0 --we'll disregard zeros during threshold determination
elseif terrainMap.data[i] ~= 0.0 or plot:GetPlotType() == PlotTypes.PLOT_MOUNTAIN then
plainsMap.data[i] = 1.0
else
plainsMap.data[i] = rainfallMap.data[i]
end
end
local plainsThreshold = plainsMap:FindThresholdFromPercent(mc.plainsPercent,false,true)
--put plains tiles in our terrainMap
for k = 1, #landTab, 1 do
local i = landTab[k]
if plainsMap.data[i] < plainsThreshold then
terrainMap.data[i] = 4.0
end
end
--### Grass ###--
--everything left is warm and rainy enough to be grassland
for k = 1, #landTab, 1 do
local i = landTab[k]
local plot = Map_GetPlotByIndex(i)
if terrainMap.data[i] == 0.0 or plot:GetPlotType() == PlotTypes.PLOT_MOUNTAIN then
terrainMap.data[i] = 5.0
end
end
-- set our plot types
for k = 1, #landTab do
local i = landTab[k]
local plot = Map_GetPlotByIndex(i)
if terrainMap.data[i] == 1.0 then
plot:SetTerrainType(TerrainTypes.TERRAIN_SNOW)
table.insert(snowTab,i)
elseif terrainMap.data[i] == 2.0 then
plot:SetTerrainType(TerrainTypes.TERRAIN_TUNDRA)
elseif terrainMap.data[i] == 3.0 then
plot:SetTerrainType(TerrainTypes.TERRAIN_DESERT)
table.insert(desertTab,i)
elseif terrainMap.data[i] == 4.0 then
plot:SetTerrainType(TerrainTypes.TERRAIN_PLAINS)
table.insert(plainsTab,i)
elseif terrainMap.data[i] == 5.0 then
plot:SetTerrainType(TerrainTypes.TERRAIN_GRASS)
table.insert(grassTab,i)
end
end
end