[GS] Replacing mountains in the map generation script

10101100

Chieftain
Joined
Oct 14, 2019
Messages
52
I'm trying to replace mountains in the pangea.lua file

I'm adding this snippet to the GenerateMap() function which I assume is the main function called when I start the game. I'm trying to iterate through all the tiles and replace the mountain tiles

Code:
for x = 0, g_iW - 1 do
        for y = 0, g_iH - 1 do
            local i = y * g_iW + x;
            if(plotTypes[i] == g_PLOT_TYPE_MOUNTAIN) then
                plotTypes[i] = g_PLOT_TYPE_HILLS;
            end
        end
end
However this does literally nothing as I still have mountains


Even this does literally nothing as not all tiles get converted to hills:
Code:
for x = 0, g_iW - 1 do
        for y = 0, g_iH - 1 do
            local i = y * g_iW + x;
            plotTypes[i] = g_PLOT_TYPE_HILLS;
        end
end
 
Last edited:
It depends entirely upon where you are placing the extra code within the function as well as which version of Pangea.lua you are altering. If you are directly-editing the file in the game folders, there are two versions of Pangea.lua -- the one for the Base Vanilla game and the one for Gathering Storm.

For the Gathering Storm version, the portion that actually creates the Terrain on Plots appears to be done in a function called "ApplyBaseTerrain" whereas for Vanilla it appears to be done directly within the main GenerateMap function.

Vanilla
Code:
	plotTypes = GeneratePlotTypes();
	terrainTypes = GenerateTerrainTypes(plotTypes, g_iW, g_iH, g_iFlags, true, temperature);

	for i = 0, (g_iW * g_iH) - 1, 1 do
		pPlot = Map.GetPlotByIndex(i);
		if (plotTypes[i] == g_PLOT_TYPE_HILLS) then
			terrainTypes[i] = terrainTypes[i] + 1;
		end
		TerrainBuilder.SetTerrainType(pPlot, terrainTypes[i]);
	end

	-- Temp
	AreaBuilder.Recalculate();
	local biggest_area = Areas.FindBiggestArea(false);
Gathering Storm
Code:
	plotTypes = GeneratePlotTypes(world_age);
	terrainTypes = GenerateTerrainTypes(plotTypes, g_iW, g_iH, g_iFlags, true, temperature);
	ApplyBaseTerrain(plotTypes, terrainTypes, g_iW, g_iH);

	AreaBuilder.Recalculate();
	TerrainBuilder.AnalyzeChokepoints();
 
After analyzing so much uncommented code I finally came to the working solution

Code:
    local x;
    local y;
    for x = 0, g_iW - 1 do
        for y = 0, g_iH - 1 do
            local i = y * g_iW + x;
            pPlot = Map.GetPlotByIndex(i);
            TerrainBuilder.SetTerrainType(pPlot, ConvertMountainToHills(terrainTypes[i]));
        end
    end

.SetTerrainType is the function that creates the actual map, simply changing the terrainTypes[] or plotTypes[] arrays doesn't do anything unless you run that function again

So I iterate through all tiles, get pPlot which I assume is the tile in the actual map, and run the .SetTerrainType on that tile, the second argument is a function that returns the tile converted to hill if it was a mountain. I could do a more efficient solution but I don't care
 
Yes indeed TerrainBuilder.SetTerrainType(PlotObject, TerrainTypeIndex) is the API method that actually sets a plot's terrain-type.

"pPlot" in your last quoted code is an lua object variable reflecting the individual plot but the values associated with the object will change when you make changes to the actual map-plot by functions such as TerrainBuilder.SetTerrainType() so as a warning for any future lua code you may attempt, always regrab the "pPlot" object like you are doing here
Code:
pPlot = Map.GetPlotByIndex(i);
just before running lua methods using that object variable to ensure you are retrieving the current information that applies to that plot.

----------------------------

The plotTypes and terrainTypes lua tables were being used to store data about the plot "elevation" (water, flat, hill, mountain, etc) and then compare to the base terrain (Grass, Plains, Snow, etc.) to determine which TerrainType should applied to a tile via TerrainBuilder.SetTerrainType()
 
Top Bottom