So I want to get the structure right before I start hammering away at code.
Here are some functions I think I will need to put into a new file, which pre-init will load for now:
Code: Select all
-- initialize world variables, including elevation, terrain, tectonic bias, and wetness
function _M:init(w, h, elev, terr, tect, wet)
self.w = w
self.h = h
self.elev = {}
self.terr = {}
self.tect = {}
self.wet = {}
for i = 1, self.w do
self.elev[i] = {}
self.terr[i] = {}
self.tect[i] = {}
self.wet[i] = {}
for j = 1, self.h do
self.elev[i][j] = 0
self.terr[i][j] = 0
self.tect[i][j] = 0
self.wet[i][j] = 0
end
end
end
-- generate initial elevation matrix
function _M:setelev()
-- use diamond square to fill elev with fractal
end
-- generate tectonic bias matrix
function _M:settect()
-- use diamond square to fill tect with fractal
end
-- generate initial humidity matrix
function _M:setwet()
-- use diamond square to fill wet with fractal
end
-- convert a single elevation value to initial terrain description
-- @param x the elevation value
-- @return the initial terrain description
function _M:elev2terr(x)
-- should have two lua tables or something sexy here for genericness
-- eg: {PLAINS, H_LOWHILLS, H_HILLS, H_MOUNTAINS} & {DEEP, PLAINS, LOWHILLS, HILLS, MOUNTAINS, PEAK}
-- where DEEP is anything below the first item in the first list, and PEAK is anything above the last item, etc.
if self.elev[i][j]<0 then self.terr[i][j] = DEEP end
if self.elev[i][j]>0 and self.elev[i][j]<H_LOWHILLS then self.terr[i][j] = PLAINS end
if self.elev[i][j]>H_LOWHILLS and self.elev[i][j]<H_HILLS then self.terr[i][j] = LOWHILLS end
if self.elev[i][j]>H_HILLS and self.elev[i][j]<H_MOUNTAINS then self.terr[i][j] = HILLS end
if self.elev[i][j]>H_MOUNTAINS and self.elev[i][j]<H_PEAK then self.terr[i][j] = MOUNTAINS end
if self.elev[i][j]>H_PEAK then self.terr[i][j] = PEAK end
end
-- convert elevation matrix to initial terrain matrix
function _M:setterr()
for i = 1, self.w do
for j = 1, self.h do
self.terr[i][j] = elev2terr(self.elev[i][j])
end
end
end
-- determine the continental geographic center
-- (will eventually be used multiple times for multiple continents/islands,
-- with different constraints each time)
function _M:getgeocenter() -- constraints as args?
-- impose any structure constraints here: eg, island in NW, etc.
local x_geo = rng.range(1, self.w)
local y_geo = rng.range(1, self.h)
return x_geo, y_geo
end
-- scale radially down from a continental geographic center
function _M:scaleradial()
x_geo, y_geo = self.getgeocenter()
-- get a 1d fractal for distance-trigger from geo center
-- get a 1d fractal for slope to use after trigger
-- loop through elevation matrix: get angle, interpolate, scale down by angle & distance
end
-- raise and lower random topographic cones
-- raise or lower map using eg quicksort so there is eg 70% land; adjust gini coefficient for correct plains amount
-- add mountain ranges
-- then turn to terrain features: rivers, woods, etc
Then I will have pre-init call all these functions, and I will output the elevation and terrain results to a file, finishing up with os.exit(), and then I will probably continue to use gnuplot for a while to pore over the resulting file.
I obviously haven't run the above code; I'm just trying to get a sense of the overall structure of what things will look like eventually. The first steps will just be doing a fractal fill of the elevation map and looking at it in gnuplot.
(1) Am I on the right track for the style of coding? If I'm right that I should move all the function declarations into a methods file, and then call them all from pre-init, what sort of header do I need to put at the top of the file with all the functions above--the class name information and any "require" statements? ... Then, in pre-init, I assume I need to say, eg, "require worldgen" or something to that effect. If you could be pretty explicit about what the header of the class file and the header I need in pre-init, that'd be great, since this is a bit new to me.
(2) What will the lua code to call the C diamond-square routine look like when plugged in above, filling up elevation with a fractal? By the way, DG, I'll need both FractalFills in 2d and 1d from my original file; the 1d one gets used for example to make mountain ranges look a little jiggly, and to make radial features look a little wiggly.
Once these initial structural things get going, I don't think this is going to take *too* long to convert to lua, although adding generality and genericness will take a bit more time, as we figure out what people want beyond just what I've been using this code for (especially long term will be civilizations and their claims and destroying towns into ruins etc).