Refactor snow cover code

This commit is contained in:
Wuzzy 2017-09-06 19:46:51 +02:00
parent 9fb148f8bf
commit 661b586a64
2 changed files with 26 additions and 14 deletions

View file

@ -1108,16 +1108,16 @@ minetest.register_abm({
}) })
---- FUNCTIONS FOR SNOWED NODES ---- ---- FUNCTIONS FOR SNOWED NODES ----
-- These are nodes which change their appearence when they are below a snow cover. -- These are nodes which change their appearence when they are below a snow cover
-- and turn back into “normal” when the snow cover is removed.
-- Lookup tables -- Registers a snowed variant of a node (e.g. grass block, podzol, mycelium).
mcl_core.snowed_nodes = {}
mcl_core.snowed_nodes_reverse = {}
-- Registers a snowed variant of a dirtlike node (e.g. grass block, podzol, mycelium).
-- * itemstring_snowed: Itemstring of the snowed node to add -- * itemstring_snowed: Itemstring of the snowed node to add
-- * itemstring_clear: Itemstring of the original “clear” node without snow -- * itemstring_clear: Itemstring of the original “clear” node without snow
-- * tiles: Optional custom tiles -- * tiles: Optional custom tiles
--
-- The snowable nodes also MUST have _mcl_snowed defined to contain the name
-- of the snowed node.
mcl_core.register_snowed_node = function(itemstring_snowed, itemstring_clear, tiles) mcl_core.register_snowed_node = function(itemstring_snowed, itemstring_clear, tiles)
local def = table.copy(minetest.registered_nodes[itemstring_clear]) local def = table.copy(minetest.registered_nodes[itemstring_clear])
-- Just some group clearing -- Just some group clearing
@ -1130,6 +1130,11 @@ mcl_core.register_snowed_node = function(itemstring_snowed, itemstring_clear, ti
-- Enderman must never take this because this block is supposed to be always buried below snow. -- Enderman must never take this because this block is supposed to be always buried below snow.
def.groups.enderman_takable = nil def.groups.enderman_takable = nil
-- Add the clear node to the item definition for easy lookup
def._mcl_snowless = itemstring_clear
-- Note: _mcl_snowed must be added to the clear node manually!
if not tiles then if not tiles then
def.tiles = {"default_snow.png", "default_dirt.png", "mcl_core_grass_side_snowed.png"} def.tiles = {"default_snow.png", "default_dirt.png", "mcl_core_grass_side_snowed.png"}
end end
@ -1137,9 +1142,6 @@ mcl_core.register_snowed_node = function(itemstring_snowed, itemstring_clear, ti
-- Register stuff -- Register stuff
minetest.register_node(itemstring_snowed, def) minetest.register_node(itemstring_snowed, def)
mcl_core.snowed_nodes[itemstring_snowed] = itemstring_clear
mcl_core.snowed_nodes_reverse[itemstring_clear] = itemstring_snowed
if minetest.get_modpath("doc") then if minetest.get_modpath("doc") then
doc.add_entry_alias("nodes", itemstring_clear, "nodes", itemstring_snowed) doc.add_entry_alias("nodes", itemstring_clear, "nodes", itemstring_snowed)
end end
@ -1149,8 +1151,9 @@ end
-- This function assumes there is no snow cover node above. This function -- This function assumes there is no snow cover node above. This function
-- MUST NOT be called if there is a snow cover node above pos. -- MUST NOT be called if there is a snow cover node above pos.
mcl_core.clear_snow_dirt = function(pos, node) mcl_core.clear_snow_dirt = function(pos, node)
if mcl_core.snowed_nodes[node.name] then local def = minetest.registered_nodes[node.name]
minetest.swap_node(pos, {name=mcl_core.snowed_nodes[node.name]}) if def._mcl_snowless then
minetest.swap_node(pos, {name = def._mcl_snowless})
end end
end end
@ -1169,10 +1172,14 @@ mcl_core.on_snowable_construct = function(pos)
-- Make snowed if needed -- Make snowed if needed
if minetest.get_item_group(anode.name, "snow_cover") == 1 then if minetest.get_item_group(anode.name, "snow_cover") == 1 then
minetest.swap_node(pos, {name=mcl_core.snowed_nodes_reverse[node.name]}) local def = minetest.registered_nodes[node.name]
if def._mcl_snowed then
minetest.swap_node(pos, {name = def._mcl_snowed})
end
end end
end end
---- [[[[[ Functions for snow cover nodes. ]]]]] ---- ---- [[[[[ Functions for snow cover nodes. ]]]]] ----
-- A snow cover node is a node which turns a snowed dirtlike -- -- A snow cover node is a node which turns a snowed dirtlike --
@ -1186,8 +1193,9 @@ end
mcl_core.on_snow_construct = function(pos) mcl_core.on_snow_construct = function(pos)
local npos = {x=pos.x, y=pos.y-1, z=pos.z} local npos = {x=pos.x, y=pos.y-1, z=pos.z}
local node = minetest.get_node(npos) local node = minetest.get_node(npos)
if mcl_core.snowed_nodes_reverse[node.name] then local def = minetest.registered_nodes[node.name]
minetest.swap_node(npos, {name=mcl_core.snowed_nodes_reverse[node.name]}) if def._mcl_snowed then
minetest.swap_node(npos, {name = def._mcl_snowed})
end end
end end
-- after_destruct -- after_destruct

View file

@ -308,6 +308,7 @@ minetest.register_node("mcl_core:dirt_with_grass", {
footstep = {name="default_grass_footstep", gain=0.4}, footstep = {name="default_grass_footstep", gain=0.4},
}), }),
on_construct = mcl_core.on_snowable_construct, on_construct = mcl_core.on_snowable_construct,
_mcl_snowed = "mcl_core:dirt_with_grass_snow",
_mcl_blast_resistance = 3, _mcl_blast_resistance = 3,
_mcl_hardness = 0.6, _mcl_hardness = 0.6,
}) })
@ -348,7 +349,9 @@ minetest.register_node("mcl_core:mycelium", {
sounds = mcl_sounds.node_sound_dirt_defaults({ sounds = mcl_sounds.node_sound_dirt_defaults({
footstep = {name="default_grass_footstep", gain=0.4}, footstep = {name="default_grass_footstep", gain=0.4},
}), }),
on_construct = mcl_core.on_snowable_construct, on_construct = mcl_core.on_snowable_construct,
_mcl_snowed = "mcl_core:mycelium_snow",
_mcl_blast_resistance = 2.5, _mcl_blast_resistance = 2.5,
_mcl_hardness = 0.6, _mcl_hardness = 0.6,
}) })
@ -364,6 +367,7 @@ minetest.register_node("mcl_core:podzol", {
drop = 'mcl_core:dirt', drop = 'mcl_core:dirt',
sounds = mcl_sounds.node_sound_dirt_defaults(), sounds = mcl_sounds.node_sound_dirt_defaults(),
on_construct = mcl_core.on_snowable_construct, on_construct = mcl_core.on_snowable_construct,
_mcl_snowed = "mcl_core:podzol_snow",
_mcl_blast_resistance = 2.5, _mcl_blast_resistance = 2.5,
_mcl_hardness = 0.6, _mcl_hardness = 0.6,
}) })