diff --git a/heightmap.lua b/heightmap.lua index b41236c..cbe8923 100644 --- a/heightmap.lua +++ b/heightmap.lua @@ -8,6 +8,10 @@ local riverbed_slope = mapgen_rivers.settings.riverbed_slope * mapgen_rivers.set local MAP_BOTTOM = -31000 +-- Localize for performance +local floor, min, max = math.floor, math.min, math.max +local unpk = unpack + -- Linear interpolation local function interp(v00, v01, v11, v10, xf, zf) local v0 = v01*xf + v00*(1-xf) @@ -30,11 +34,11 @@ local function heightmaps(minp, maxp) if poly then local xf, zf = transform_quadri(poly.x, poly.z, x, z) - local i00, i01, i11, i10 = unpack(poly.i) + local i00, i01, i11, i10 = unpk(poly.i) -- Load river width on 4 edges and corners - local r_west, r_north, r_east, r_south = unpack(poly.rivers) - local c_NW, c_NE, c_SE, c_SW = unpack(poly.river_corners) + local r_west, r_north, r_east, r_south = unpk(poly.rivers) + local c_NW, c_NE, c_SE, c_SW = unpk(poly.river_corners) -- Calculate the depth factor for each edge and corner. -- Depth factor: @@ -64,10 +68,10 @@ local function heightmaps(minp, maxp) -- Transform the coordinates to have xf and zf = 0 or 1 in rivers (to avoid rivers having lateral slope and to accomodate the surrounding smoothly) if imax == 0 then - local x0 = math.max(r_west, c_NW-zf, zf-c_SW) - local x1 = math.min(r_east, c_NE+zf, c_SE-zf) - local z0 = math.max(r_north, c_NW-xf, xf-c_NE) - local z1 = math.min(r_south, c_SW+xf, c_SE-xf) + local x0 = max(r_west, c_NW-zf, zf-c_SW) + local x1 = min(r_east, c_NE+zf, c_SE-zf) + local z0 = max(r_north, c_NW-xf, xf-c_NE) + local z1 = min(r_south, c_SW+xf, c_SE-xf) xf = (xf-x0) / (x1-x0) zf = (zf-z0) / (z1-z0) elseif imax == 1 then @@ -90,7 +94,7 @@ local function heightmaps(minp, maxp) -- Determine elevation by interpolation local vdem = poly.dem - local terrain_height = math.floor(0.5+interp( + local terrain_height = floor(0.5+interp( vdem[1], vdem[2], vdem[3], @@ -115,10 +119,10 @@ local function heightmaps(minp, maxp) lake_id = 1 end end - local lake_height = math.max(math.floor(poly.lake[lake_id]), terrain_height) + local lake_height = max(floor(poly.lake[lake_id]), terrain_height) if imax > 0 and depth_factor_max > 0 then - terrain_height = math.min(math.max(lake_height, sea_level) - math.floor(1+depth_factor_max*riverbed_slope), terrain_height) + terrain_height = min(max(lake_height, sea_level) - floor(1+depth_factor_max*riverbed_slope), terrain_height) end terrain_height_map[i] = terrain_height diff --git a/init.lua b/init.lua index 89065eb..71b1e15 100644 --- a/init.lua +++ b/init.lua @@ -32,6 +32,9 @@ local function interp(v00, v01, v11, v10, xf, zf) return v1*zf + v0*(1-zf) end +-- Localize for performance +local floor, min = math.floor, math.min + local data = {} local noise_x_obj, noise_z_obj, noise_distort_obj, noise_heat_obj, noise_heat_blend_obj @@ -111,8 +114,8 @@ local function generate(minp, maxp, seed) end end - local pminp = {x=math.floor(xmin), z=math.floor(zmin)} - local pmaxp = {x=math.floor(xmax)+1, z=math.floor(zmax)+1} + local pminp = {x=floor(xmin), z=floor(zmin)} + local pmaxp = {x=floor(xmax)+1, z=floor(zmax)+1} incr = pmaxp.x-pminp.x+1 i_origin = 1 - pminp.z*incr - pminp.x terrain_map, lake_map = heightmaps(pminp, pmaxp) @@ -145,7 +148,7 @@ local function generate(minp, maxp, seed) for z = minp.z, maxp.z do for x = minp.x, maxp.x do - local ivm = a:index(x, minp.y, z) + local ivm = a:index(x, maxp.y+1, z) local ground_above = false local temperature if use_biomes then @@ -161,8 +164,8 @@ local function generate(minp, maxp, seed) if use_distort then local xn = noise_x_map[nid] local zn = noise_z_map[nid] - local x0 = math.floor(xn) - local z0 = math.floor(zn) + local x0 = floor(xn) + local z0 = floor(zn) local i0 = i_origin + z0*incr + x0 local i1 = i0+1 @@ -170,13 +173,12 @@ local function generate(minp, maxp, seed) local i3 = i2-1 terrain = interp(terrain_map[i0], terrain_map[i1], terrain_map[i2], terrain_map[i3], xn-x0, zn-z0) - lake = math.min(lake_map[i0], lake_map[i1], lake_map[i2], lake_map[i3]) + lake = min(lake_map[i0], lake_map[i1], lake_map[i2], lake_map[i3]) end if y <= maxp.y then local is_lake = lake > terrain - local ivm = a:index(x, y, z) if y <= terrain then if not use_biomes or y <= terrain-1 or ground_above then data[ivm] = c_stone @@ -205,7 +207,7 @@ local function generate(minp, maxp, seed) ground_above = y <= terrain - ivm = ivm + ystride + ivm = ivm - ystride if use_distort then nid = nid + incrY end diff --git a/polygons.lua b/polygons.lua index 0cb4c45..9f38983 100644 --- a/polygons.lua +++ b/polygons.lua @@ -90,16 +90,19 @@ if mapgen_rivers.settings.center then map_offset.z = blocksize*Z/2 end +-- Localize for performance +local floor, ceil, min, max, abs = math.floor, math.ceil, math.min, math.max, math.abs + local min_catchment = mapgen_rivers.settings.min_catchment / (blocksize*blocksize) local wpower = mapgen_rivers.settings.river_widening_power local wfactor = 1/(2*blocksize * min_catchment^wpower) local function river_width(flow) - flow = math.abs(flow) + flow = abs(flow) if flow < min_catchment then return 0 end - return math.min(wfactor * flow ^ wpower, 1) + return min(wfactor * flow ^ wpower, 1) end local noise_heat -- Need a large-scale noise here so no heat blend @@ -138,8 +141,8 @@ local function make_polygons(minp, maxp) local polygons = {} -- Determine the minimum and maximum coordinates of the polygons that could be on the chunk, knowing that they have an average size of 'blocksize' and a maximal offset of 0.5 blocksize. - local xpmin, xpmax = math.max(math.floor((minp.x+map_offset.x)/blocksize - 0.5), 0), math.min(math.ceil((maxp.x+map_offset.x)/blocksize + 0.5), X-2) - local zpmin, zpmax = math.max(math.floor((minp.z+map_offset.z)/blocksize - 0.5), 0), math.min(math.ceil((maxp.z+map_offset.z)/blocksize + 0.5), Z-2) + local xpmin, xpmax = max(floor((minp.x+map_offset.x)/blocksize - 0.5), 0), min(ceil((maxp.x+map_offset.x)/blocksize + 0.5), X-2) + local zpmin, zpmax = max(floor((minp.z+map_offset.z)/blocksize - 0.5), 0), min(ceil((maxp.z+map_offset.z)/blocksize + 0.5), Z-2) -- Iterate over the polygons for xp = xpmin, xpmax do @@ -165,8 +168,8 @@ local function make_polygons(minp, maxp) local bounds = {} -- Will be a list of the intercepts of polygon edges for every Z position (scanline algorithm) -- Calculate the min and max Z positions - local zmin = math.max(math.floor(math.min(unpack(poly_z)))+1, minp.z) - local zmax = math.min(math.floor(math.max(unpack(poly_z))), maxp.z) + local zmin = max(floor(min(unpack(poly_z)))+1, minp.z) + local zmax = min(floor(max(unpack(poly_z))), maxp.z) -- And initialize the arrays for z=zmin, zmax do bounds[z] = {} @@ -176,14 +179,14 @@ local function make_polygons(minp, maxp) for i2=1, 4 do -- Loop on 4 edges local z1, z2 = poly_z[i1], poly_z[i2] -- Calculate the integer Z positions over which this edge spans - local lzmin = math.floor(math.min(z1, z2))+1 - local lzmax = math.floor(math.max(z1, z2)) + local lzmin = floor(min(z1, z2))+1 + local lzmax = floor(max(z1, z2)) if lzmin <= lzmax then -- If there is at least one position in it local x1, x2 = poly_x[i1], poly_x[i2] -- Calculate coefficient of the equation defining the edge: X=aZ+b local a = (x1-x2) / (z1-z2) local b = (x1 - a*z1) - for z=math.max(lzmin, minp.z), math.min(lzmax, maxp.z) do + for z=max(lzmin, minp.z), min(lzmax, maxp.z) do -- For every Z position involved, add the intercepted X position in the table table.insert(bounds[z], a*z+b) end @@ -194,11 +197,11 @@ local function make_polygons(minp, maxp) -- Now sort the bounds list local zlist = bounds[z] table.sort(zlist) - local c = math.floor(#zlist/2) + local c = floor(#zlist/2) for l=1, c do -- Take pairs of X coordinates: all positions between them belong to the polygon. - local xmin = math.max(math.floor(zlist[l*2-1])+1, minp.x) - local xmax = math.min(math.floor(zlist[l*2]), maxp.x) + local xmin = max(floor(zlist[l*2-1])+1, minp.x) + local xmax = min(floor(zlist[l*2]), maxp.x) local i = (z-minp.z) * chulens + (xmin-minp.x) + 1 for x=xmin, xmax do -- Fill the map at these places @@ -220,16 +223,16 @@ local function make_polygons(minp, maxp) local riverD = river_width(rivers[iD]) if glaciers then -- Widen the river if get_temperature(poly_x[1], poly_dem[1], poly_z[1]) < 0 then - riverA = math.min(riverA*glacier_factor, 1) + riverA = min(riverA*glacier_factor, 1) end if get_temperature(poly_x[2], poly_dem[2], poly_z[2]) < 0 then - riverB = math.min(riverB*glacier_factor, 1) + riverB = min(riverB*glacier_factor, 1) end if get_temperature(poly_x[3], poly_dem[3], poly_z[3]) < 0 then - riverC = math.min(riverC*glacier_factor, 1) + riverC = min(riverC*glacier_factor, 1) end if get_temperature(poly_x[4], poly_dem[4], poly_z[4]) < 0 then - riverD = math.min(riverD*glacier_factor, 1) + riverD = min(riverD*glacier_factor, 1) end end