placing kelp now creates water sources, small refactoring.

This commit is contained in:
iliekprogrammar 2021-03-21 12:16:56 +08:00
parent 23f69dfd1e
commit ebf9f8c918

View File

@ -1,7 +1,9 @@
local S = minetest.get_translator("mcl_ocean") local S = minetest.get_translator("mcl_ocean")
local mod_doc = minetest.get_modpath("doc") ~= nil local mod_doc = minetest.get_modpath("doc") ~= nil
-- NOTE: whenever it becomes possible to fully implement kelp without the
-- plantlike_rooted limitation, please adapt the code accordingly.
-- List of supported surfaces for seagrass and kelp -- List of supported surfaces for seagrass and kelp.
local surfaces = { local surfaces = {
{ "dirt", "mcl_core:dirt" }, { "dirt", "mcl_core:dirt" },
{ "sand", "mcl_core:sand", 1 }, { "sand", "mcl_core:sand", 1 },
@ -12,22 +14,13 @@ local surfaces = {
-- Is this water? -- Is this water?
local function is_submerged(node, nodedef) local function is_submerged(node, nodedef)
if minetest.get_item_group(node.name, "water") ~= 0 then if minetest.get_item_group(node.name, "water") ~= 0 then
local liquidtype = nodedef.liquidtype return nodedef.liquidtype -- Expected only "source" and "flowing" from water liquids
-- TODO: is it OK to optimize this to:
-- return nodedef.liquidtype
if liquidtype == "source" then
return "source"
elseif liquidtype == "flowing" then
return "flowing"
else
minetest.chat_send_all("its NOT OK to optimize into return nodedef.liquidtype :(")
end
end end
return false return false
end end
-- Is the water downward flowing? -- Is the water downward flowing?
-- (kelp can grow inside downward flowing water) -- (kelp can grow/be placed inside downward flowing water)
local function is_downward_flowing(pos, node, nodedef, is_above) local function is_downward_flowing(pos, node, nodedef, is_above)
result = (math.floor(node.param2 / 8) % 2) == 1 result = (math.floor(node.param2 / 8) % 2) == 1
@ -41,10 +34,12 @@ local function is_downward_flowing(pos, node, nodedef, is_above)
return result return result
end end
-- Converts param2 to kelp height.
local function get_kelp_height(param2) local function get_kelp_height(param2)
return math.floor(param2 / 16) return math.floor(param2 / 16)
end end
-- Obtain pos and node of the top of kelp.
local function get_kelp_top(pos, node) local function get_kelp_top(pos, node)
local size = math.ceil(node.param2 / 16) local size = math.ceil(node.param2 / 16)
local pos_top = table.copy(pos) local pos_top = table.copy(pos)
@ -52,7 +47,7 @@ local function get_kelp_top(pos, node)
return pos_top, minetest.get_node(pos_top) return pos_top, minetest.get_node(pos_top)
end end
-- Obtain position of the first kelp unsubmerged -- Obtain position of the first kelp unsubmerged.
local function get_kelp_unsubmerged(pos, node) local function get_kelp_unsubmerged(pos, node)
local x,y,z = pos.x, pos.y, pos.z local x,y,z = pos.x, pos.y, pos.z
local height = get_kelp_height(node.param2) local height = get_kelp_height(node.param2)
@ -65,20 +60,32 @@ local function get_kelp_unsubmerged(pos, node)
return nil return nil
end end
local function grow_param2_step(param2, snap_into_grid) -- Obtain next param2 if grown
local old_param2 = param2 local function grow_param2_step(param2)
param2 = param2 + 16
-- TODO: allow kelp to grow bypass this limit according to MC rules. -- TODO: allow kelp to grow bypass this limit according to MC rules.
-- https://minecraft.gamepedia.com/Kelp -- https://minecraft.gamepedia.com/Kelp
local old_param2 = param2
param2 = param2+16 - param2 % 16
if param2 > 240 then if param2 > 240 then
param2 = 240 param2 = 240
end end
if snap_into_grid and (param2 % 16 ~= 0) then
param2 = param2 - (param2 % 16)
end
return param2, param2 ~= old_param2 return param2, param2 ~= old_param2
end end
local function kelp_place(pos, node, pos_top, def_top, is_downward_flowing)
-- Liquid source: Grow normally
minetest.set_node(pos, node)
-- Flowing liquid: Grow 1 step, but also turn the top node into a liquid source
if is_downward_flowing then
local alt_liq = def_top.liquid_alternative_source
if alt_liq then
minetest.set_node(pos_top, {name=alt_liq})
end
end
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
@ -89,9 +96,7 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
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 nu_name = node_under.name
local node_above = minetest.get_node(pos_above)
local def_under = minetest.registered_nodes[nu_name] local def_under = minetest.registered_nodes[nu_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
return def_under.on_rightclick(pos_under, node_under, return def_under.on_rightclick(pos_under, node_under,
@ -109,59 +114,52 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
end end
local new_kelp = false local new_kelp = false
local is_downward_flowing = false
local pos_top, node_top, def_top
-- When placed on kelp. -- When placed on kelp.
if minetest.get_item_group(nu_name, "kelp") == 1 then if minetest.get_item_group(nu_name, "kelp") == 1 then
node_under.param2, new_kelp = grow_param2_step(node_under.param2) node_under.param2, new_kelp = grow_param2_step(node_under.param2)
-- Kelp must not reach the height limit. -- Kelp must not reach the height limit.
-- Kelp must be placed on top of kelp to add kelp. -- Kelp must also be placed on top of kelp to add kelp.
if not new_kelp or if not new_kelp or pos_under.y >= pos_above.y then
pos_under.y >= pos_above.y or pos_under.x ~= pos_above.x or pos_under.z ~= pos_above.z then
return itemstack
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 return itemstack
end end
pos_top, node_top = get_kelp_top(pos_under, node_under)
def_top = minetest.registered_nodes[node_top.name]
-- When placed on surface. -- When placed on surface.
else else
-- Surface must support kelp
for _,surface in pairs(surfaces) do for _,surface in pairs(surfaces) do
-- Surface must support kelp
if nu_name == surface[2] then if nu_name == surface[2] then
node_under.name = "mcl_ocean:kelp_" ..surface[1] node_under.name = "mcl_ocean:kelp_" ..surface[1]
node_under.param2 = minetest.registered_items[nu_name].place_param2 or 16
new_kelp = true new_kelp = true
end end
end end
-- The surface must support kelp -- Kelp must also be placed on top of surface to add new 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 if not new_kelp or pos_under.y >= pos_above.y then
return itemstack return itemstack
end end
pos_top = pos_above
-- New kelp must also be submerged in water. node_top = minetest.get_node(pos_above)
if not (is_submerged(node_above, def_above) and is_downward_flowing(pos_above, node_above, def_above)) then def_top = minetest.registered_nodes[node_above.name]
return itemstack
end
node_under.param2 = minetest.registered_items[nu_name].place_param2 or 16
end end
-- Play sound, set surface/kelp and take away an item -- New kelp must also be submerged in water.
is_downward_flowing = is_downward_flowing(pos_top, node_top, def_top)
if not (is_submerged(node_top, def_top) and is_downward_flowing) then
return itemstack
end
-- Play sound, place surface/kelp and take away an item
local def_node = minetest.registered_items[nu_name] local def_node = minetest.registered_items[nu_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
minetest.set_node(pos_under, node_under) kelp_place(pos_under, node_under, pos_top, def_top, is_downward_flowing)
if not minetest.is_creative_enabled(player_name) then if not minetest.is_creative_enabled(player_name) then
itemstack:take_item() itemstack:take_item()
end end
@ -169,6 +167,7 @@ local function kelp_on_place(itemstack, placer, pointed_thing)
return itemstack return itemstack
end end
-- From kelp at pos, drop kelp until reaching its height.
local function kelp_drop(pos, height) local function kelp_drop(pos, height)
local x,y,z = pos.x,pos.y,pos.z local x,y,z = pos.x,pos.y,pos.z
for i=1,height do for i=1,height do
@ -265,8 +264,6 @@ for s=1, #surfaces do
after_dig_node = function(pos) after_dig_node = function(pos)
minetest.set_node(pos, {name=surface[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)
local is_drop = true local is_drop = true
if digger and minetest.is_creative_enabled(digger:get_player_name()) then if digger and minetest.is_creative_enabled(digger:get_player_name()) then
@ -360,25 +357,14 @@ minetest.register_abm({
chance = 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 grow
-- 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, grow = grow_param2_step(node.param2, true) node.param2, grow = grow_param2_step(node.param2)
local pos_top, node_top = get_kelp_top(pos, node) local pos_top, node_top = get_kelp_top(pos, node)
local def_top = minetest.registered_nodes[node_top.name] local def_top = minetest.registered_nodes[node_top.name]
local submerged = is_submerged(node_top, def_top) if grow and is_submerged(node_top, def_top) then
if grow then kelp_place(pos, node, pos_top, def_top,
if submerged == "source" then is_downward_flowing(pos_top, node_top, def_top))
-- Liquid source: Grow normally
minetest.set_node(pos, node)
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
minetest.set_node(pos, node)
local alt_liq = def_top.liquid_alternative_source
if alt_liq then
minetest.set_node(pos_top, {name=alt_liq})
end
end
end end
end, end,
}) })