Use local variables for math.* functions

and remove an unnecessary index calculation
This commit is contained in:
Gael-de-Sailly 2022-01-03 11:56:16 +01:00
parent 2e29474686
commit f8f467ac3f
3 changed files with 43 additions and 34 deletions

View File

@ -8,6 +8,10 @@ local riverbed_slope = mapgen_rivers.settings.riverbed_slope * mapgen_rivers.set
local MAP_BOTTOM = -31000 local MAP_BOTTOM = -31000
-- Localize for performance
local floor, min, max = math.floor, math.min, math.max
local unpk = unpack
-- Linear interpolation -- Linear interpolation
local function interp(v00, v01, v11, v10, xf, zf) local function interp(v00, v01, v11, v10, xf, zf)
local v0 = v01*xf + v00*(1-xf) local v0 = v01*xf + v00*(1-xf)
@ -30,11 +34,11 @@ local function heightmaps(minp, maxp)
if poly then if poly then
local xf, zf = transform_quadri(poly.x, poly.z, x, z) 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 -- Load river width on 4 edges and corners
local r_west, r_north, r_east, r_south = unpack(poly.rivers) local r_west, r_north, r_east, r_south = unpk(poly.rivers)
local c_NW, c_NE, c_SE, c_SW = unpack(poly.river_corners) local c_NW, c_NE, c_SE, c_SW = unpk(poly.river_corners)
-- Calculate the depth factor for each edge and corner. -- Calculate the depth factor for each edge and corner.
-- Depth factor: -- 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) -- 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 if imax == 0 then
local x0 = math.max(r_west, c_NW-zf, zf-c_SW) local x0 = max(r_west, c_NW-zf, zf-c_SW)
local x1 = math.min(r_east, c_NE+zf, c_SE-zf) local x1 = min(r_east, c_NE+zf, c_SE-zf)
local z0 = math.max(r_north, c_NW-xf, xf-c_NE) local z0 = max(r_north, c_NW-xf, xf-c_NE)
local z1 = math.min(r_south, c_SW+xf, c_SE-xf) local z1 = min(r_south, c_SW+xf, c_SE-xf)
xf = (xf-x0) / (x1-x0) xf = (xf-x0) / (x1-x0)
zf = (zf-z0) / (z1-z0) zf = (zf-z0) / (z1-z0)
elseif imax == 1 then elseif imax == 1 then
@ -90,7 +94,7 @@ local function heightmaps(minp, maxp)
-- Determine elevation by interpolation -- Determine elevation by interpolation
local vdem = poly.dem local vdem = poly.dem
local terrain_height = math.floor(0.5+interp( local terrain_height = floor(0.5+interp(
vdem[1], vdem[1],
vdem[2], vdem[2],
vdem[3], vdem[3],
@ -115,10 +119,10 @@ local function heightmaps(minp, maxp)
lake_id = 1 lake_id = 1
end end
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 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 end
terrain_height_map[i] = terrain_height terrain_height_map[i] = terrain_height

View File

@ -32,6 +32,9 @@ local function interp(v00, v01, v11, v10, xf, zf)
return v1*zf + v0*(1-zf) return v1*zf + v0*(1-zf)
end end
-- Localize for performance
local floor, min = math.floor, math.min
local data = {} local data = {}
local noise_x_obj, noise_z_obj, noise_distort_obj, noise_heat_obj, noise_heat_blend_obj 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
end end
local pminp = {x=math.floor(xmin), z=math.floor(zmin)} local pminp = {x=floor(xmin), z=floor(zmin)}
local pmaxp = {x=math.floor(xmax)+1, z=math.floor(zmax)+1} local pmaxp = {x=floor(xmax)+1, z=floor(zmax)+1}
incr = pmaxp.x-pminp.x+1 incr = pmaxp.x-pminp.x+1
i_origin = 1 - pminp.z*incr - pminp.x i_origin = 1 - pminp.z*incr - pminp.x
terrain_map, lake_map = heightmaps(pminp, pmaxp) 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 z = minp.z, maxp.z do
for x = minp.x, maxp.x 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 ground_above = false
local temperature local temperature
if use_biomes then if use_biomes then
@ -161,8 +164,8 @@ local function generate(minp, maxp, seed)
if use_distort then if use_distort then
local xn = noise_x_map[nid] local xn = noise_x_map[nid]
local zn = noise_z_map[nid] local zn = noise_z_map[nid]
local x0 = math.floor(xn) local x0 = floor(xn)
local z0 = math.floor(zn) local z0 = floor(zn)
local i0 = i_origin + z0*incr + x0 local i0 = i_origin + z0*incr + x0
local i1 = i0+1 local i1 = i0+1
@ -170,13 +173,12 @@ local function generate(minp, maxp, seed)
local i3 = i2-1 local i3 = i2-1
terrain = interp(terrain_map[i0], terrain_map[i1], terrain_map[i2], terrain_map[i3], xn-x0, zn-z0) 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 end
if y <= maxp.y then if y <= maxp.y then
local is_lake = lake > terrain local is_lake = lake > terrain
local ivm = a:index(x, y, z)
if y <= terrain then if y <= terrain then
if not use_biomes or y <= terrain-1 or ground_above then if not use_biomes or y <= terrain-1 or ground_above then
data[ivm] = c_stone data[ivm] = c_stone
@ -205,7 +207,7 @@ local function generate(minp, maxp, seed)
ground_above = y <= terrain ground_above = y <= terrain
ivm = ivm + ystride ivm = ivm - ystride
if use_distort then if use_distort then
nid = nid + incrY nid = nid + incrY
end end

View File

@ -90,16 +90,19 @@ if mapgen_rivers.settings.center then
map_offset.z = blocksize*Z/2 map_offset.z = blocksize*Z/2
end 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 min_catchment = mapgen_rivers.settings.min_catchment / (blocksize*blocksize)
local wpower = mapgen_rivers.settings.river_widening_power local wpower = mapgen_rivers.settings.river_widening_power
local wfactor = 1/(2*blocksize * min_catchment^wpower) local wfactor = 1/(2*blocksize * min_catchment^wpower)
local function river_width(flow) local function river_width(flow)
flow = math.abs(flow) flow = abs(flow)
if flow < min_catchment then if flow < min_catchment then
return 0 return 0
end end
return math.min(wfactor * flow ^ wpower, 1) return min(wfactor * flow ^ wpower, 1)
end end
local noise_heat -- Need a large-scale noise here so no heat blend 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 = {} 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. -- 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 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 = 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 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 -- Iterate over the polygons
for xp = xpmin, xpmax do 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) 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 -- Calculate the min and max Z positions
local zmin = math.max(math.floor(math.min(unpack(poly_z)))+1, minp.z) local zmin = max(floor(min(unpack(poly_z)))+1, minp.z)
local zmax = math.min(math.floor(math.max(unpack(poly_z))), maxp.z) local zmax = min(floor(max(unpack(poly_z))), maxp.z)
-- And initialize the arrays -- And initialize the arrays
for z=zmin, zmax do for z=zmin, zmax do
bounds[z] = {} bounds[z] = {}
@ -176,14 +179,14 @@ local function make_polygons(minp, maxp)
for i2=1, 4 do -- Loop on 4 edges for i2=1, 4 do -- Loop on 4 edges
local z1, z2 = poly_z[i1], poly_z[i2] local z1, z2 = poly_z[i1], poly_z[i2]
-- Calculate the integer Z positions over which this edge spans -- Calculate the integer Z positions over which this edge spans
local lzmin = math.floor(math.min(z1, z2))+1 local lzmin = floor(min(z1, z2))+1
local lzmax = math.floor(math.max(z1, z2)) local lzmax = floor(max(z1, z2))
if lzmin <= lzmax then -- If there is at least one position in it if lzmin <= lzmax then -- If there is at least one position in it
local x1, x2 = poly_x[i1], poly_x[i2] local x1, x2 = poly_x[i1], poly_x[i2]
-- Calculate coefficient of the equation defining the edge: X=aZ+b -- Calculate coefficient of the equation defining the edge: X=aZ+b
local a = (x1-x2) / (z1-z2) local a = (x1-x2) / (z1-z2)
local b = (x1 - a*z1) 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 -- For every Z position involved, add the intercepted X position in the table
table.insert(bounds[z], a*z+b) table.insert(bounds[z], a*z+b)
end end
@ -194,11 +197,11 @@ local function make_polygons(minp, maxp)
-- Now sort the bounds list -- Now sort the bounds list
local zlist = bounds[z] local zlist = bounds[z]
table.sort(zlist) table.sort(zlist)
local c = math.floor(#zlist/2) local c = floor(#zlist/2)
for l=1, c do for l=1, c do
-- Take pairs of X coordinates: all positions between them belong to the polygon. -- 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 xmin = max(floor(zlist[l*2-1])+1, minp.x)
local xmax = math.min(math.floor(zlist[l*2]), maxp.x) local xmax = min(floor(zlist[l*2]), maxp.x)
local i = (z-minp.z) * chulens + (xmin-minp.x) + 1 local i = (z-minp.z) * chulens + (xmin-minp.x) + 1
for x=xmin, xmax do for x=xmin, xmax do
-- Fill the map at these places -- Fill the map at these places
@ -220,16 +223,16 @@ local function make_polygons(minp, maxp)
local riverD = river_width(rivers[iD]) local riverD = river_width(rivers[iD])
if glaciers then -- Widen the river if glaciers then -- Widen the river
if get_temperature(poly_x[1], poly_dem[1], poly_z[1]) < 0 then 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 end
if get_temperature(poly_x[2], poly_dem[2], poly_z[2]) < 0 then 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 end
if get_temperature(poly_x[3], poly_dem[3], poly_z[3]) < 0 then 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 end
if get_temperature(poly_x[4], poly_dem[4], poly_z[4]) < 0 then 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
end end