API changes to facilitiate lava/new-substances cauldron.

This commit is contained in:
Phaethon H 2021-11-06 04:21:58 -07:00
parent a3de5b7cb0
commit 48eb27655a
2 changed files with 95 additions and 31 deletions

View File

@ -9,11 +9,8 @@ mcl_cauldrons.BOTTLE_FILL_LEVELS = 1
-- list of substances accepted by cauldron.
-- N.B. hopefully this will be obsoleted by the liquids API.
mcl_cauldrons.substances = {
"cauldron_water",
"cauldron_river_water",
}
-- filled by register_cauldron()
mcl_cauldrons.substances = {}
-- Find cauldron from registered cauldrons -> string, table
@ -42,22 +39,33 @@ function mcl_cauldrons.find_cauldron(substance, fill_level)
end
-- Test: node describes a cauldron -> boolean
function mcl_cauldrons.is_cauldron(node)
function mcl_cauldrons.is_cauldron_node(node)
if node == nil then return false end
return (minetest.get_item_group(node.name, "cauldron") ~= 0)
end
function mcl_cauldrons.is_cauldron_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.is_cauldron_node(node)
end
-- Test: cauldron node contains specified substance -> boolean
function mcl_cauldrons.has_substance(node, substance)
function mcl_cauldrons.has_substance_node(node, substance)
return (minetest.get_item_group(node.name, substance) ~= 0)
end
function mcl_cauldrons.has_substance_pos(pos, substance)
local node = minetest.get_node(pos)
return mcl_cauldrons.has_substance_node(node)
end
-- list of supported substances -> table
function mcl_cauldrons.list_substances()
return mcl_cauldron.substances
return mcl_cauldrons.substances
end
-- Get substance inside cauldron -> string or nil
function mcl_cauldrons.get_substance(node)
function mcl_cauldrons.get_substance_node(node)
local nodename = node.name
-- data-driven to avoid if-cascade.
-- until liquids API is available, the substance name matches the group key in the node definition (e.g. "cauldron_water").
@ -70,32 +78,36 @@ function mcl_cauldrons.get_substance(node)
return nil
end
function mcl_cauldrons.get_substance_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.get_substance_node(node)
end
-- Get maximum number of levels available for cauldron node -> number
-- Optionally specify `substance` (mainly to determine a change in cauldron substance, e.g. filling a full one with bucket of something else)
-- substance == nil to ignore substance criteria
function mcl_cauldrons.get_maximum(node, substance)
local nodename
if substance == nil then
nodename = node.name
else
nodename = mcl_cauldrons.find_cauldron(substance, nil)
end
function mcl_cauldrons.get_maximum_node(node)
local nodename = node.name
return minetest.get_item_group(nodename, "cauldron_maximum")
end
function mcl_cauldrons.get_maximum_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.get_maximum_node(node)
end
-- Get current level of cauldron -> number
-- Optionally specify `substance`, substance==nil to indicate current content.
function mcl_cauldrons.get_level(node, substance)
if substance and (minetest.get_item_group(node.name, substance) == 0) then
return 0
end
function mcl_cauldrons.get_level_node(node)
return minetest.get_item_group(node.name, "cauldron_filled")
end
function mcl_cauldrons.get_level_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.get_level_node(node)
end
-- Test: cauldron is empty -> boolean
-- (i.e. fill level is 0, or contents is empty)
function mcl_cauldrons.is_empty(node)
if not mcl_cauldrons.is_cauldron(node) then
function mcl_cauldrons.is_empty_node(node)
if not mcl_cauldrons.is_cauldron_node(node) then
error("not a cauldron", node.name)
end
if minetest.get_item_group(node.name, "cauldron_filled") == 0 then
@ -104,18 +116,40 @@ function mcl_cauldrons.is_empty(node)
return false
end
function mcl_cauldrons.is_empty_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.is_empty_node(node)
end
-- Test: cauldron is full -> boolean
-- (i.e. current fill level matches maximum level.
function mcl_cauldrons.is_full(node)
function mcl_cauldrons.is_full_node(node)
local maximum = mcl_cauldrons.get_maximum(node)
local current = mcl_cauldrons.get_level(node)
return (current >= maximum)
end
function mcl_cauldrons.is_full_pos(pos)
local node = minetest.get_node(pos)
return mcl_cauldrons.is_full_node(node)
end
function mcl_cauldrons.make_cauldron_node(substance, fill_levels)
local nodename, nodedef = mcl_cauldrons.find_cauldron(substance, fill_level)
local newnode = { name = nodename }
return newnode
end
function mcl_cauldrons.make_cauldron_pos(pos, substance, fill_level)
local node = mcl_cauldrons.make_cauldron_node(substance, fill_level)
minetest.set_node(pos, node)
end
-- Set fill level, bypassing game rules -> table
-- Optionally indicate substance (to change cauldron content); nil to preserve.
-- returns a node table as accepted by minetest.set_node()
function mcl_cauldrons.set_level(node, fill_level, substance)
function mcl_cauldrons.set_level_node(node, fill_level, substance)
if substance == nil then
-- determine current substance.
substance = mcl_cauldrons.get_substance(node)
@ -125,24 +159,51 @@ function mcl_cauldrons.set_level(node, fill_level, substance)
return newnode
end
function mcl_cauldrons.set_level_pos(pos, fill_level, substance)
local node = minetest.get_node(pos)
local newnode = mcl_cauldrons.set_level_node(node, fill_level, substance)
minetest.set_node(pos, newnode)
end
-- Change content of cauldron by number of levels with indicated substance -> table
-- Subject to game rules (delegated to node definition's on_fill callback)
-- returns a node table as accepted by minetest.set_node()
function mcl_cauldrons.fill_levels(node, change_levels, substance)
function mcl_cauldrons.fill_levels_node(node, change_levels, substance)
local nodedef = mcl_cauldrons.registered_cauldrons[node.name]
local callback = nodedef._mcl_on_fill
-- use callback if available, else revert.
return callback and callback(node, change_levels, substance) or node
end
function mcl_cauldrons.fill_levels_pos(pos, change_levels, substance)
local node = minetest.get_node(pos)
local newnode = mcl_cauldrons.fill_levels_node(node)
minetest.set_node(pos)
end
-- drain counterpart to fill_levels()
function mcl_cauldrons.drain_levels(node, change_levels, substance)
function mcl_cauldrons.drain_levels_node(node, change_levels, substance)
local nodedef = mcl_cauldrons.registered_cauldrons[node.name]
local callback = nodedef._mcl_on_drain
-- use callback if available, else revert.
return callback and callback(node, change_levels, substance) or node
end
function mcl_cauldrons.drain_levels_pos(pos, change_levels, substance)
local node = minetest.get_node(pos)
local newnode = mcl_cauldrons.drain_levels_node(node)
minetest.set_node(pos)
end
local function list_member(member, lis)
for i, v in ipairs(lis) do
if v == member then
return member
end
end
return nil
end
-- Wrapper to register cauldron-specific nodes.
-- additional bookkeeping specific to mcl_cauldrons.
@ -152,5 +213,8 @@ function mcl_cauldrons.register_cauldron_node(nodename, nodedef, doc_alias)
if doc_alias and has_doc then
doc.add_entry_alias("nodes", doc_alias, "nodes", nodename)
end
if not list_member(nodedef._mcl_cauldron_substance, mcl_cauldrons.substances) then
table.insert(mcl_cauldrons.substances, nodedef._mcl_cauldron_substance)
end
end

View File

@ -158,7 +158,6 @@ mcl_cauldrons.register_cauldron_node("mcl_cauldrons:cauldron_water_1",
cauldron = 1,
cauldron_filled = 1,
cauldron_maximum = 3,
cauldron_water = 1,
},
node_box = mcl_cauldrons.cauldron_nodeboxes[1],
collision_box = mcl_cauldrons.cauldron_nodeboxes[0],
@ -168,6 +167,7 @@ mcl_cauldrons.register_cauldron_node("mcl_cauldrons:cauldron_water_1",
"mcl_cauldrons_cauldron_side.png"
},
drop = "mcl_cauldrons:cauldron",
_mcl_cauldron_substance = "water",
}), "mcl_cauldrons:cauldron")
-- water cauldron 2/3 as extension of 1/3.
@ -200,7 +200,6 @@ if minetest.get_modpath("mclx_core") then
cauldron = 1,
cauldron_filled = 1,
cauldron_maximum = 3,
cauldron_river_water = 1,
},
node_box = mcl_cauldrons.cauldron_nodeboxes[1],
collision_box = mcl_cauldrons.cauldron_nodeboxes[0],
@ -210,6 +209,7 @@ if minetest.get_modpath("mclx_core") then
"mcl_cauldrons_cauldron_side.png"
},
drop = "mcl_cauldrons:cauldron",
_mcl_cauldron_substance = "river_water",
}), "mcl_cauldrons:cauldron")
-- water cauldron 2/3 as extension of 1/3.
mcl_cauldrons.register_cauldron_node("mcl_cauldrons:cauldron_river_water_2",