Skip to content

Commit

Permalink
code cleanup and better handling of yet to be loaded areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeb187 committed Nov 7, 2019
1 parent 4ca9fbf commit 1b325c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 99 deletions.
24 changes: 23 additions & 1 deletion const.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--
-- switch for debugging
--
settlements.debug = true
settlements.debug = false
--
-- switch for lvm
settlements.lvm = false
Expand All @@ -26,6 +26,28 @@ wallmaterial = {
"default:desert_cobble",
"default:sandstone"
}
settlements.surface_mat = {}
-------------------------------------------------------------------------------
-- Set array to list
-- https://stackoverflow.com/questions/656199/search-for-an-item-in-a-lua-list
-------------------------------------------------------------------------------
function settlements.grundstellungen ()
settlements.surface_mat = settlements.Set {
"default:dirt_with_grass",
"default:dry_dirt_with_grass",
"default:dirt_with_snow",
"default:dirt_with_dry_grass",
"default:dirt_with_coniferous_litter",
"default:sand",
"default:silver_sand",
"default:desert_sand",
"default:snow_block"
}
end
--
-- possible surfaces where buildings can be built
--

--
-- path to schematics
--
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dofile(settlements.modpath.."/convert_lua_mts.lua")
-- load settlements on server
--
settlements_in_world = settlements.load()
settlements.grundstellungen()
--
-- register block for npc spawn
--
Expand Down Expand Up @@ -96,7 +97,6 @@ minetest.register_on_generated(function(minp, maxp)
-- don't build settlements on (too) uneven terrain
--
local height_difference = settlements.evaluate_heightmap(minp, maxp)
-- local height_difference = settlements.determine_heightmap(data, va, minp, maxp)
if height_difference > max_height_difference
then
return
Expand Down
122 changes: 25 additions & 97 deletions utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,7 @@ function settlements.round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
function settlements.find_surface_heightmap(pos, minp)
local surface_mat = {
c_dirt_with_grass,
c_dirt_with_snow ,
c_dirt_with_dry_grass,
c_dirt_with_coniferous_litter,
c_sand,
c_desert_sand,
c_silver_sand
}
local p6 = settlements.shallowCopy(pos)
local heightmap = minetest.get_mapgen_object("heightmap")
-- get height of current pos p6
local hm_i = (p6.x - minp.x + 1) + (((p6.z - minp.z)) * 80)
p6.y = heightmap[hm_i]
local vi = va:index(p6.x, p6.y, p6.z)
local viname = minetest.get_name_from_content_id(data[vi])

for i, mats in ipairs(surface_mat) do
local node_check = va:index(p6.x, p6.y+1, p6.z)
if node_check and vi and data[vi] == mats and
(data[node_check] ~= c_water_source
)
then
-- local tmp = minetest.get_name_from_content_id(data[node_check])
return p6, mats
end
end
end
-------------------------------------------------------------------------------
-- function to find surface block y coordinate
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -128,20 +97,6 @@ end
-------------------------------------------------------------------------------
function settlements.find_surface(pos)
local p6 = settlements.shallowCopy(pos)
--
-- possible surfaces where buildings can be built
--
local surface_mat = settlements.Set {
"default:dirt_with_grass",
"default:dry_dirt_with_grass",
"default:dirt_with_snow",
"default:dirt_with_dry_grass",
"default:dirt_with_coniferous_litter",
"default:sand",
"default:silver_sand",
"default:desert_sand",
"default:snow_block"
}
local cnt = 0
local itter -- count up or down
local cnt_max = 200
Expand All @@ -156,19 +111,32 @@ function settlements.find_surface(pos)
while cnt < cnt_max do
cnt = cnt+1
surface_node = minetest.get_node_or_nil(p6)
if surface_node == nil or surface_node.name == "ignore" then
if settlements.debug == true then
minetest.chat_send_all("find_surface1: nil or ignore")

if not surface_node then
-- Load the map at pos and try again
minetest.get_voxel_manip():read_from_map(p6, p6)
surface_node = minetest.get_node(p6)
if surface_node.name == "ignore" then
if settlements.debug == true then
minetest.chat_send_all("find_surface1: nil or ignore")
end
return nil
end
return nil
end

-- if surface_node == nil or surface_node.name == "ignore" then
-- --return nil
-- local fl = minetest.forceload_block(p6)
-- if not fl then
--
-- return nil
-- end
-- end
--
-- Check Surface_node and Node above
--
local surface_node_plus_1 = minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z})
-- for i, mats in ipairs(surface_mat) do
--minetest.chat_send_all(surface_mat[surface_node.name])
if surface_mat[surface_node.name] then
if settlements.surface_mat[surface_node.name] then
local surface_node_plus_1 = minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z})
if surface_node_plus_1 and surface_node and
(string.find(surface_node_plus_1.name,"air") or
string.find(surface_node_plus_1.name,"snow") or
Expand All @@ -178,6 +146,9 @@ function settlements.find_surface(pos)
string.find(surface_node_plus_1.name,"tree") or
string.find(surface_node_plus_1.name,"grass"))
then
if settlements.debug == true then
minetest.chat_send_all("find_surface7: " ..surface_node.name.. " " .. surface_node_plus_1.name)
end
return p6, surface_node.name
else
if settlements.debug == true then
Expand Down Expand Up @@ -368,49 +339,6 @@ function shuffle(tbl)
return table
end
-------------------------------------------------------------------------------
-- get heightmap
-------------------------------------------------------------------------------
function settlements.determine_heightmap(data, va, minp, maxp)
-- max height and min height, initialize with impossible values for easier first time setting
local max_y = -100
local min_y = 100
--
-- only analyze the center 40x40 of a chunk
--
local cmaxp = {
x=maxp.x-quarter_map_chunk_size,
y=maxp.y, -- -quarter_map_chunk_size,
z=maxp.z-quarter_map_chunk_size
}
local cminp = {
x=minp.x+quarter_map_chunk_size,
y=minp.y, -- +quarter_map_chunk_size,
z=minp.z+quarter_map_chunk_size
}
--
-- walk through chunk and find surfaces
--
for xi = cminp.x,cmaxp.x do
for zi = cminp.z,cmaxp.z do
local pos_surface = settlements.find_surface({x=xi, y=cmaxp.y, z=zi}, data, va)
-- check, if new found surface is higher or lower stored min_y or max_y
if pos_surface
then
if pos_surface.y < min_y
then
min_y = pos_surface.y
end
if pos_surface.y > max_y
then
max_y = pos_surface.y
end
end
end
end
-- return the difference between highest and lowest pos in chunk
return max_y - min_y
end
-------------------------------------------------------------------------------
-- evaluate heightmap
-------------------------------------------------------------------------------
function settlements.evaluate_heightmap()
Expand Down Expand Up @@ -484,4 +412,4 @@ function settlements.Set (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
end

0 comments on commit 1b325c3

Please sign in to comment.