Skip to content

Commit

Permalink
add some support for special handling of leaf decay
Browse files Browse the repository at this point in the history
  • Loading branch information
FaceDeer committed Jan 17, 2020
1 parent e406791 commit e6e3150
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
41 changes: 38 additions & 3 deletions settlements/buildings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ local function get_corner_pos(center_pos, schematic, rotation)
return corner1, corner2
end

local group_ids = {}
local is_in_group = function(c_id, groupname)
local grouplist = group_ids[groupname]
if grouplist then
return grouplist[c_id]
end
grouplist = {}
for name, def in pairs(minetest.registered_nodes) do
if minetest.get_item_group(name, groupname) > 0 then
grouplist[minetest.get_content_id(name)] = true
end
end
group_ids[groupname] = grouplist
return grouplist[c_id]
end

-- function clear space above baseplate
local function terraform(data, va, settlement_info)
local c_air = minetest.get_content_id(settlement_info.def.platform_air or "air")
Expand All @@ -56,7 +72,12 @@ local function terraform(data, va, settlement_info)
end
if build_platform == nil then
build_platform = true
end
end

local skip_group_above = schematic_data.platform_ignore_group_above
if skip_group_above then
skip_group_above = skip_group_above:gsub("^group:", "")
end

local size = schematic_data.schematic.size
local pos = built_house.build_pos_min
Expand All @@ -82,8 +103,11 @@ local function terraform(data, va, settlement_info)
local p = {x=pos.x+xi, y=pos.y, z=pos.z+zi}
ground(p, data, va, c_shallow, c_deep)
elseif replace_air then
local vi = va:index(pos.x+xi, pos.y+yi, pos.z+zi)
data[vi] = c_air
local p = vector.new(pos.x+xi, pos.y+yi, pos.z+zi)
local vi = va:indexp(p)
if not (skip_group_above and is_in_group(data[vi], skip_group_above)) then
data[vi] = c_air
end
end
end
end
Expand Down Expand Up @@ -539,6 +563,16 @@ function settlements.place_building(vm, built_house, settlement_info)
force_place)
end

local trigger_timer_for_group = function(minp, maxp, nodenames)
if not nodenames then
return
end
local targets = minetest.find_nodes_in_area(minp, maxp, nodenames)
for _, pos in ipairs(targets) do
minetest.get_node_timer(pos):start(math.random(20, 120) / 10)
end
end

local data = {} -- for better memory management, use externally-allocated buffer
settlements.generate_settlement_vm = function(vm, va, minp, maxp, existing_settlement_name)
vm:get_data(data)
Expand Down Expand Up @@ -568,6 +602,7 @@ settlements.generate_settlement_vm = function(vm, va, minp, maxp, existing_settl

-- evaluate settlement_info and initialize furnaces and chests
initialize_nodes(settlement_info)
trigger_timer_for_group(minp, maxp, settlement_info.def.trigger_timers_for_nodes)
return true
end

Expand Down
20 changes: 19 additions & 1 deletion settlements_medieval/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ local townhall_schematic = {
-- So for example, 0.1 means at most 1 of these buildings in a 10-building settlement and 2 in a 20-building settlement.
replace_nodes_optional = true, -- If true, default:cobble will be replaced with a random wall material
initialize_node = initialize_node, -- allows additional post-creation actions to be executed on schematic nodes once they're constructed
platform_ignore_group_above = "group:leafdecay", -- causes special handling of nodes belonging to this group. If
-- the node is in the space above the schematic being placed it will not be turned into air.
-- For leaves, make sure to add trigger_timers_for_nodes = "group:leafdecay" to the settlement settings to clean
-- up blobs of leaves that might be left over from trunks that have been removed.
}
local kingsmarket_schematic = {
name = "kingsmarket",
Expand All @@ -97,6 +101,7 @@ local kingsmarket_schematic = {
max_num = 0.1,
replace_nodes_optional = true,
initialize_node = initialize_node,
platform_ignore_group_above = "group:leafdecay",
}

-- list of schematics
Expand All @@ -107,6 +112,7 @@ local schematic_table = {
buffer = 2,
max_num = 0.045,
height_adjust = -2, -- adjusts the y axis of where the schematic is built, to allow for "basement" stuff
platform_ignore_group_above = "group:leafdecay",
},
{
name = "hut",
Expand All @@ -115,36 +121,42 @@ local schematic_table = {
max_num = 0.9,
replace_nodes_optional = true,
initialize_node = initialize_node,
platform_ignore_group_above = "group:leafdecay",
},
{
name = "garden",
schematic = dofile(schem_path.."medieval_garden.lua"),
max_num = 0.1,
initialize_node = initialize_node,
platform_ignore_group_above = "group:leafdecay",
},
{
name = "lamp",
schematic = dofile(schem_path.."medieval_lamp.lua"),
buffer = 3,
max_num = 0.05,
platform_ignore_group_above = "group:leafdecay",
},
{
name = "tower",
schematic = dofile(schem_path.."medieval_tower.lua"),
buffer = 3,
max_num = 0.055,
platform_ignore_group_above = "group:leafdecay",
},
{
name = "church",
schematic = dofile(schem_path.."medieval_church.lua"),
buffer = 2,
max_num = 0.075,
platform_ignore_group_above = "group:leafdecay",
},
{
name = "blacksmith",
schematic = dofile(schem_path.."medieval_blacksmith.lua"),
buffer = 2,
max_num = 0.050,
platform_ignore_group_above = "group:leafdecay",
},
kingsmarket_schematic,
{
Expand All @@ -154,6 +166,7 @@ local schematic_table = {
max_num = 0.025,
replace_nodes_optional = true,
initialize_node = initialize_node,
platform_ignore_group_above = "group:leafdecay",
},
}

Expand All @@ -170,7 +183,7 @@ local medieval_settlements = {
"default:silver_sand",
"default:snow_block",
},

-- TODO: add a biome list. The tricky part here is, what if a biome list but not a surface materials list is provided?
-- How to find the surface, and how to know what to replace surface material nodes with in the schematic?

Expand Down Expand Up @@ -242,6 +255,11 @@ local medieval_settlements = {
end,

generate_book = generate_book,

-- This is a special-purpose property used for cleaning up leaf blobs that might have been left behind
-- when tree trunks got removed by buildings. It goes through every node in the mapchunk and sets the
-- node's timer going if it matches (using find_nodes_in_area).
trigger_timers_for_nodes = "group:leafdecay",
}

settlements.register_settlement("medieval", medieval_settlements)
Expand Down

0 comments on commit e6e3150

Please sign in to comment.