Core implementation of MC-like kelp drops.

Highlights:
* Kelp should no longer be able to survive without water
(by decreasing its height)
* When kelp is destroyed, each segment now drop a single kelp similar to MC.
* Significantly refactor how kelp grows and dig. Possible optimizations might be
included.
This commit is contained in:
iliekprogrammar 2021-03-20 17:47:04 +08:00
parent c877d6e922
commit 23f69dfd1e
2 changed files with 169 additions and 107 deletions

View file

@ -74,6 +74,7 @@ function mesecon.is_mvps_unsticky(node, pulldir, stack, stackid)
end end
-- Functions to be called on mvps movement -- Functions to be called on mvps movement
-- See also the callback
function mesecon.register_on_mvps_move(callback) function mesecon.register_on_mvps_move(callback)
mesecon.on_mvps_move[#mesecon.on_mvps_move+1] = callback mesecon.on_mvps_move[#mesecon.on_mvps_move+1] = callback
end end
@ -405,17 +406,20 @@ mesecon.register_mvps_unsticky("mcl_colorblocks:glazed_terracotta_brown")
mesecon.register_mvps_unsticky("mcl_colorblocks:glazed_terracotta_light_blue") mesecon.register_mvps_unsticky("mcl_colorblocks:glazed_terracotta_light_blue")
mesecon.register_mvps_unsticky("mcl_colorblocks:glazed_terracotta_pink") mesecon.register_mvps_unsticky("mcl_colorblocks:glazed_terracotta_pink")
-- Includes node heat when moving them
mesecon.register_on_mvps_move(mesecon.move_hot_nodes) mesecon.register_on_mvps_move(mesecon.move_hot_nodes)
-- Check for falling after moving node
mesecon.register_on_mvps_move(function(moved_nodes) mesecon.register_on_mvps_move(function(moved_nodes)
for i = 1, #moved_nodes do for i = 1, #moved_nodes do
local moved_node = moved_nodes[i] local moved_node = moved_nodes[i]
-- Check for falling after moving node
mesecon.on_placenode(moved_node.pos, moved_node.node) mesecon.on_placenode(moved_node.pos, moved_node.node)
minetest.after(0, function() minetest.after(0, function()
minetest.check_for_falling(moved_node.oldpos) minetest.check_for_falling(moved_node.oldpos)
minetest.check_for_falling(moved_node.pos) minetest.check_for_falling(moved_node.pos)
end) end)
-- Callback for on_mvps_move stored in nodedef
local node_def = minetest.registered_nodes[moved_node.node.name] local node_def = minetest.registered_nodes[moved_node.node.name]
if node_def and node_def.mesecon and node_def.mesecon.on_mvps_move then if node_def and node_def.mesecon and node_def.mesecon.on_mvps_move then
node_def.mesecon.on_mvps_move(moved_node.pos, moved_node.node, node_def.mesecon.on_mvps_move(moved_node.pos, moved_node.node,

View file

@ -9,29 +9,67 @@ local surfaces = {
{ "gravel", "mcl_core:gravel", 1 }, { "gravel", "mcl_core:gravel", 1 },
} }
local function get_kelp_top(pos, node) -- Is this water?
local size = math.ceil(node.param2 / 16) local function is_submerged(node, nodedef)
local pos_water = table.copy(pos) if minetest.get_item_group(node.name, "water") ~= 0 then
pos_water.y = pos_water.y + size local liquidtype = nodedef.liquidtype
return pos_water, minetest.get_node(pos_water) -- TODO: is it OK to optimize this to:
end -- return nodedef.liquidtype
if liquidtype == "source" then
local function get_submerged(node_water)
local def_water = minetest.registered_nodes[node_water.name]
-- Submerged in water?
if minetest.get_item_group(node_water.name, "water") then
if def_water.liquidtype == "source" then
return "source" return "source"
elseif def_water.liquidtype == "flowing" then elseif liquidtype == "flowing" then
return "flowing" return "flowing"
else
minetest.chat_send_all("its NOT OK to optimize into return nodedef.liquidtype :(")
end end
end end
return false return false
end end
-- Is the water downward flowing?
-- (kelp can grow inside downward flowing water)
local function is_downward_flowing(pos, node, nodedef, is_above)
result = (math.floor(node.param2 / 8) % 2) == 1
if not (result or is_above) then
-- If not, also check node above (this is needed due a weird quirk in the definition of
-- "downwards flowing" liquids in Minetest)
local node_above = minetest.get_node({x=pos.x,y=pos.y+1,z=pos.z})
local nodedef_above = minetest.registered_nodes[node_above.name]
result = is_submerged(node_above, nodedef_above) or is_downward_flowing(pos, node_above, nodedef_above, true)
end
return result
end
local function get_kelp_height(param2)
return math.floor(param2 / 16)
end
local function get_kelp_top(pos, node)
local size = math.ceil(node.param2 / 16)
local pos_top = table.copy(pos)
pos_top.y = pos_top.y + size
return pos_top, minetest.get_node(pos_top)
end
-- Obtain position of the first kelp unsubmerged
local function get_kelp_unsubmerged(pos, node)
local x,y,z = pos.x, pos.y, pos.z
local height = get_kelp_height(node.param2)
for i=1,height do
local walk_pos = {x=x, y=y + i, z=z}
if minetest.get_item_group(minetest.get_node(walk_pos).name, "water") == 0 then
return walk_pos
end
end
return nil
end
local function grow_param2_step(param2, snap_into_grid) local function grow_param2_step(param2, snap_into_grid)
local old_param2 = param2 local old_param2 = param2
param2 = param2 + 16 param2 = param2 + 16
-- TODO: allow kelp to grow bypass this limit according to MC rules.
-- https://minecraft.gamepedia.com/Kelp
if param2 > 240 then if param2 > 240 then
param2 = 240 param2 = 240
end end
@ -41,30 +79,6 @@ local function grow_param2_step(param2, snap_into_grid)
return param2, param2 ~= old_param2 return param2, param2 ~= old_param2
end end
local function kelp_check_place(pos_above, node_above, def_above)
if minetest.get_item_group(node_above.name, "water") == 0 then
return false
end
local can_place = false
if (def_above.liquidtype == "source") then
can_place = true
elseif (def_above.liquidtype == "flowing") then
-- Check if bit 3 (downwards flowing) is set
can_place = (math.floor(node_above.param2 / 8) % 2) == 1
if not can_place then
-- If not, also check node above (this is needed due a weird quirk in the definition of
-- "downwards flowing" liquids in Minetest)
local node_above_above = minetest.get_node({x=pos_above.x,y=pos_above.y+1,z=pos_above.z})
local naa_def = minetest.registered_nodes[node_above_above.name]
can_place = naa_def.liquidtype == "source"
if not can_place then
can_place = (naa_def.liquidtype == "flowing") and ((math.floor(node_above_above.param2 / 8) % 2) == 1)
end
end
end
return can_place
end
local function kelp_on_place(itemstack, placer, pointed_thing) local function kelp_on_place(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" or not placer then if pointed_thing.type ~= "node" or not placer then
return itemstack return itemstack
@ -74,8 +88,9 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
local pos_under = pointed_thing.under local pos_under = pointed_thing.under
local pos_above = pointed_thing.above local pos_above = pointed_thing.above
local node_under = minetest.get_node(pos_under) local node_under = minetest.get_node(pos_under)
local nu_name = node_under.name
local node_above = minetest.get_node(pos_above) local node_above = minetest.get_node(pos_above)
local def_under = minetest.registered_nodes[node_under.name] local def_under = minetest.registered_nodes[nu_name]
local def_above = minetest.registered_nodes[node_above.name] local def_above = minetest.registered_nodes[node_above.name]
if def_under and def_under.on_rightclick and not placer:get_player_control().sneak then if def_under and def_under.on_rightclick and not placer:get_player_control().sneak then
@ -93,56 +108,56 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
return itemstack return itemstack
end end
local grow_kelp = false local new_kelp = false
-- Select a kelp node when placed on surface node
if node_under.name == "mcl_core:dirt" then -- When placed on kelp.
node_under.name = "mcl_ocean:kelp_dirt" if minetest.get_item_group(nu_name, "kelp") == 1 then
elseif node_under.name == "mcl_core:sand" then node_under.param2, new_kelp = grow_param2_step(node_under.param2)
node_under.name = "mcl_ocean:kelp_sand"
elseif node_under.name == "mcl_core:redsand" then -- Kelp must not reach the height limit.
node_under.name = "mcl_ocean:kelp_redsand" -- Kelp must be placed on top of kelp to add kelp.
elseif node_under.name == "mcl_core:gravel" then if not new_kelp or
node_under.name = "mcl_ocean:kelp_gravel" pos_under.y >= pos_above.y or pos_under.x ~= pos_above.x or pos_under.z ~= pos_above.z then
elseif minetest.get_item_group(node_under.name, "kelp") == 1 then
-- Place kelp on kelp = grow kelp by 1 node length
node_under.param2, grow_kelp = grow_param2_step(node_under.param2)
if not grow_kelp then
return itemstack return itemstack
end end
-- TODO: is this even possible???
if pos_under.y < pos_above.y and (pos_under.x ~= pos_above.x or pos_under.z ~= pos_above.z) then
minetest.chat_send_all(dump2(pos_under, "pos_under")) --DEBUG
end
-- New kelp top must also be submerged in water.
local pos_top, node_top = get_kelp_top(pos_under, node_under)
local def_top = minetest.registered_nodes[node_top.name]
if not (is_submerged(node_top, def_top) and is_downward_flowing(pos_top, node_top, def_top)) then
return itemstack
end
-- When placed on surface.
else else
return itemstack -- Surface must support kelp
for _,surface in pairs(surfaces) do
if nu_name == surface[2] then
node_under.name = "mcl_ocean:kelp_" ..surface[1]
new_kelp = true
end
end
-- The surface must support kelp
-- Kelp must be placed on top of surface to add new kelp.
if not new_kelp or pos_under.y >= pos_above.y then
return itemstack
end
-- New kelp must also be submerged in water.
if not (is_submerged(node_above, def_above) and is_downward_flowing(pos_above, node_above, def_above)) then
return itemstack
end
node_under.param2 = minetest.registered_items[nu_name].place_param2 or 16
end end
local submerged = false
if grow_kelp then -- Play sound, set surface/kelp and take away an item
-- Kelp placed on kelp ... local def_node = minetest.registered_items[nu_name]
-- Kelp can be placed on top of another kelp to make it grow
if pos_under.y >= pos_above.y or pos_under.x ~= pos_above.x or pos_under.z ~= pos_above.z then
-- Placed on side or below node, abort
return itemstack
end
-- New kelp top must also be submerged in water
local top_pos, top_node = get_kelp_top(pos_under, node_under)
local top_def = minetest.registered_nodes[top_node.name]
submerged = kelp_check_place(top_pos, top_node, top_def)
if not submerged then
-- Not submerged in water, abort
return itemstack
end
else
-- New kelp placed ...
if pos_under.y >= pos_above.y then
-- Placed on side or below node, abort
return itemstack
end
-- Kelp can be placed inside a water source or water flowing downwards on top of a surface node
local can_place = kelp_check_place(pos_above, node_above, def_above)
if not can_place then
return itemstack
end
node_under.param2 = minetest.registered_items[node_under.name].place_param2 or 16
end
-- Place or grow kelp
local def_node = minetest.registered_items[node_under.name]
if def_node.sounds then if def_node.sounds then
minetest.sound_play(def_node.sounds.place, { gain = 0.5, pos = pos_under }, true) minetest.sound_play(def_node.sounds.place, { gain = 0.5, pos = pos_under }, true)
end end
@ -154,8 +169,36 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
return itemstack return itemstack
end end
local get_kelp_height = function(param2) local function kelp_drop(pos, height)
return math.floor(param2 / 16) local x,y,z = pos.x,pos.y,pos.z
for i=1,height do
minetest.add_item({x=x, y=y+i, z=z}, "mcl_ocean:kelp")
end
end
-- Dig kelp:
-- Each kelp from broken stem until the top drop a single item
-- Kelp's height decreases to the height below dig_pos
local function kelp_dig(dig_pos, pos, node, is_drop)
local param2 = node.param2
local height = get_kelp_height(param2)
-- pos.y points to the surface, offset needed to point to the first kelp
local new_height = dig_pos.y - (pos.y+1)
-- Digs the entire kelp: invoke after_dig_node to set_node
if new_height == 0 then
if is_drop then
kelp_drop(dig_pos, height)
end
minetest.set_node(pos, {name=minetest.registered_nodes[node.name].node_dig_prediction})
-- Digs the kelp beginning at a height
else
if is_drop then
kelp_drop(dig_pos, height - new_height)
end
minetest.set_node(pos, {name=node.name, param=node.param, param2=16*new_height})
end
end end
minetest.register_craftitem("mcl_ocean:kelp", { minetest.register_craftitem("mcl_ocean:kelp", {
@ -220,19 +263,16 @@ for s=1, #surfaces do
sounds = sounds, sounds = sounds,
node_dig_prediction = surfaces[s][2], node_dig_prediction = surfaces[s][2],
after_dig_node = function(pos) after_dig_node = function(pos)
minetest.set_node(pos, {name=surfaces[s][2]}) minetest.set_node(pos, {name=surface[s][2]})
end, end,
-- NOTE: whenever it becomes possible to fully implement kelp without the
-- plantlike_rooted limitation, please adapt the code accordingly.
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
-- Drop kelp as item; item count depends on height local is_drop = true
local dname = "" if digger and minetest.is_creative_enabled(digger:get_player_name()) then
if digger then is_drop = false
dname = digger:get_player_name()
end end
local creative = minetest.is_creative_enabled(dname) kelp_dig(pos, pos, node, is_drop)
if not creative then
minetest.add_item({x=pos.x, y=pos.y+1, z=pos.z}, "mcl_ocean:kelp "..get_kelp_height(node.param2))
end
minetest.node_dig(pos, node, digger)
end, end,
drop = "", -- drops are handled in on_dig drop = "", -- drops are handled in on_dig
_mcl_falling_node_alternative = alt, _mcl_falling_node_alternative = alt,
@ -314,28 +354,46 @@ minetest.register_craft({
minetest.register_abm({ minetest.register_abm({
label = "Kelp growth", label = "Kelp growth",
nodenames = { "group:kelp" }, nodenames = { "group:kelp" },
interval = 45, -- interval = 45,
chance = 12, -- chance = 12,
interval = 1,
chance = 1,
catch_up = false, catch_up = false,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local grown local grown
-- Grow kelp by 1 node length if it would grow inside water -- Grow kelp by 1 node length if it would grow inside water
node.param2, grown = grow_param2_step(node.param2, true) node.param2, grow = grow_param2_step(node.param2, true)
local top, top_node = get_kelp_top(pos, node) local pos_top, node_top = get_kelp_top(pos, node)
local submerged = get_submerged(top_node) local def_top = minetest.registered_nodes[node_top.name]
if grown then local submerged = is_submerged(node_top, def_top)
if grow then
if submerged == "source" then if submerged == "source" then
-- Liquid source: Grow normally -- Liquid source: Grow normally
minetest.set_node(pos, node) minetest.set_node(pos, node)
elseif submerged == "flowing" then
elseif submerged == "flowing" and is_downward_flowing(pos_top, node_top, def_top) then
-- Flowing liquid: Grow 1 step, but also turn the top node into a liquid source -- Flowing liquid: Grow 1 step, but also turn the top node into a liquid source
minetest.set_node(pos, node) minetest.set_node(pos, node)
local def_liq = minetest.registered_nodes[top_node.name] local alt_liq = def_top.liquid_alternative_source
local alt_liq = def_liq and def_liq.liquid_alternative_source
if alt_liq then if alt_liq then
minetest.set_node(top, {name=alt_liq}) minetest.set_node(pos_top, {name=alt_liq})
end end
end end
end end
end, end,
}) })
-- Break kelp not underwater.
minetest.register_abm({
label = "Kelp drops",
nodenames = { "group:kelp" },
interval = 0.25,
chance = 1,
catch_up = false,
action = function(pos, node)
local dig_pos = get_kelp_unsubmerged(pos, node)
if dig_pos then
kelp_dig(dig_pos, pos, node, true)
end
end
})