1
0
Fork 0

Ensure index for heightmap entry is within bounds

This commit is contained in:
ancientmarinerdev 2023-02-03 22:03:49 +00:00 committed by Johannes Fritz
parent 9aeafe6053
commit ace6f8db43
2 changed files with 46 additions and 12 deletions

View File

@ -80,20 +80,29 @@ end
local mg_name = minetest.get_mapgen_setting("mg_name") local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name ~= "singlenode" then if mg_name ~= "singlenode" then
mcl_mapgen_core.register_generator("villages", nil, function(minp, maxp, blockseed) mcl_mapgen_core.register_generator("villages", nil, function(minp, maxp, blockseed)
-- don't build settlement underground
if maxp.y < 0 then return end if maxp.y < 0 then return end
-- randomly try to build settlements -- randomly try to build settlements
if blockseed % 77 ~= 17 then return end if blockseed % 77 ~= 17 then return end
--minetest.log("Rng good. Generate attempt")
-- needed for manual and automated settlement building -- needed for manual and automated settlement building
-- don't build settlements on (too) uneven terrain -- don't build settlements on (too) uneven terrain
local n=minetest.get_node_or_nil(minp) local n=minetest.get_node_or_nil(minp)
if n and n.name == "mcl_villages:structblock" then return end if n and n.name == "mcl_villages:structblock" then return end
--minetest.log("No existing village attempt here")
if villagegen[minetest.pos_to_string(minp)] ~= nil then return end if villagegen[minetest.pos_to_string(minp)] ~= nil then return end
--minetest.log("Not in village gen. Put down placeholder: " .. minetest.pos_to_string(minp) .. " || " .. minetest.pos_to_string(maxp))
minetest.set_node(minp,{name="mcl_villages:structblock"}) minetest.set_node(minp,{name="mcl_villages:structblock"})
local height_difference = settlements.evaluate_heightmap() local height_difference = settlements.evaluate_heightmap()
if height_difference > max_height_difference then return end if not height_difference or height_difference > max_height_difference then
minetest.log("action", "Do not spawn village here as heightmap not good")
return
end
--minetest.log("Build me a village: " .. minetest.pos_to_string(minp) .. " || " .. minetest.pos_to_string(maxp))
villagegen[minetest.pos_to_string(minp)]={minp=vector.new(minp), maxp=vector.new(maxp), blockseed=blockseed} villagegen[minetest.pos_to_string(minp)]={minp=vector.new(minp), maxp=vector.new(maxp), blockseed=blockseed}
end) end)
end end

View File

@ -199,7 +199,12 @@ end
function settlements.evaluate_heightmap() function settlements.evaluate_heightmap()
local heightmap = minetest.get_mapgen_object("heightmap") local heightmap = minetest.get_mapgen_object("heightmap")
if not heightmap then return max_height_difference + 1 end if not heightmap then
minetest.log("action", "No heightmap. That should not happen")
return max_height_difference + 1
end
--minetest.log("action", "heightmap size: " .. tostring(#heightmap))
-- max height and min height, initialize with impossible values for easier first time setting -- max height and min height, initialize with impossible values for easier first time setting
local max_y = -50000 local max_y = -50000
@ -208,16 +213,32 @@ function settlements.evaluate_heightmap()
local square_start = 1621 local square_start = 1621
local square_end = 1661 local square_end = 1661
for j = 1 , 40, 1 do for j = 1 , 40, 1 do
if square_start >= #heightmap then
--minetest.log("action", "Heightmap size reached. Go no further outside")
break
end
for i = square_start, square_end, 1 do for i = square_start, square_end, 1 do
-- skip buggy heightmaps, return high value --minetest.log("action", "current hm index: " .. tostring(i) .. "current hm entry: " .. tostring(heightmap[i]))
if heightmap[i] == -31000 or heightmap[i] == 31000 then
return max_height_difference + 1 if i >= #heightmap then
--minetest.log("action", "Heightmap size reached. Go no further")
break
end end
if heightmap[i] < min_y then local current_hm_entry = heightmap[i]
min_y = heightmap[i] if current_hm_entry then
end -- skip buggy heightmaps, return high value. Converted mcl5 maps can be -31007
if heightmap[i] > max_y then if current_hm_entry == -31000 or heightmap[i] == 31000 then
max_y = heightmap[i] --minetest.log("action", "incorrect heighmap values. abandon")
return max_height_difference + 1
end
if current_hm_entry < min_y then
min_y = current_hm_entry
end
if current_hm_entry > max_y then
max_y = current_hm_entry
end
else
--minetest.log("action", "Failed to get hm index: " .. tostring(i) .. "and ... " .. tostring(#heightmap))
end end
end end
-- set next line -- set next line
@ -226,10 +247,14 @@ function settlements.evaluate_heightmap()
end end
-- return the difference between highest and lowest pos in chunk -- return the difference between highest and lowest pos in chunk
local height_diff = max_y - min_y local height_diff = max_y - min_y
--minetest.log("action", "height_diff = " .. tostring(height_diff))
-- filter buggy heightmaps -- filter buggy heightmaps
if height_diff <= 1 then if height_diff <= 1 then
return max_height_difference + 1 return max_height_difference + 1
end end
--minetest.log("action", "return heigh diff = " .. tostring(height_diff))
-- debug info -- debug info
settlements.debug("heightdiff ".. height_diff) settlements.debug("heightdiff ".. height_diff)
return height_diff return height_diff