Railroad Lua

Reptilius

Chieftain
Joined
Mar 26, 2013
Messages
9
I have been modding with xml for a while now but am just starting to use lua
what i am looking for is a script for my mod that automatically puts railroads on all viable tiles (flatland and hills) when the map is generated. Could someone please help with this?

Thanks in advance
 
As previously suggested, MapGenerator.lua is likely to be the best place to input the code you'd need to do this. You'd probably want to add a new function that runs some time after NWs have been placed but before players get placed. You'd cycle through every plot on the map checking them to see if they're hills or flat land and then add the improvement for railroads. I'll write a pseudo-code of how I'd do it:

Code:
function RailroadMadness()

 local W, H = map.getGridSize() [COLOR="Green"]-- Real, may have caps wrong though[/COLOR]
 local WH = W * H

 for i = 0, WH, 1 do
  local plot = Map.GetPlotByIndex(i) [COLOR="Green"]-- Real[/COLOR]
  if plot.GetPlotType() == FLAT or plot.GetPlotType() == HILLS then [COLOR="Green"]-- Real[/COLOR]
   plot.SetImproventType() == RAIL [COLOR="Blue"]-- Fake[/COLOR]
  end
 end
end

You'd then call this function from the appropriate place in MapGenerator.lua. Note that some of the methods used in my pseudo-code are real and some I just made up as needed but they most likely have real versions in the .lua. Additionally, you'll need to find the actual defines for "FLAT", "HILLS", and "RAIL".
 
You need to add a check for natural wonders. Otherwise you might get a crash.

FLAT = "PLOT_LAND"
HILLS = "PLOT_HILLS"
 
"RAIL" is a route not an improvement, so it's

Code:
plot:SetRouteType(GameInfoTypes.ROUTE_RAILROAD)

and on the line above it should be "plot:" not "plot." (two occurrences)
 
Back
Top Bottom