Move cauldron nodebox preset table outside of function, to be built once instead of with each function call.

Skip intermediate variable for returns.
Added mcl_cauldrons.get_substance() to get substance from cauldron.
Added explicit list of cauldron substances.
This commit is contained in:
Phaethon H 2021-11-05 20:39:08 -07:00
parent 5b03c604b3
commit a3de5b7cb0
2 changed files with 37 additions and 15 deletions

View File

@ -8,6 +8,14 @@ mcl_cauldrons.BUCKET_FILL_LEVELS = 3
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",
}
-- Find cauldron from registered cauldrons -> string, table
-- `substance` == nil for any cauldron content (incl. empty), "" = only empty.
-- `fill_level` == nil for any/first fill level satisfying `substance`.
@ -43,6 +51,25 @@ function mcl_cauldrons.has_substance(node, substance)
return (minetest.get_item_group(node.name, substance) ~= 0)
end
-- list of supported substances -> table
function mcl_cauldrons.list_substances()
return mcl_cauldron.substances
end
-- Get substance inside cauldron -> string or nil
function mcl_cauldrons.get_substance(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").
for i, probe_key in ipairs(mcl_cauldrons.substances) do
if minetest.get_item_group(node.name, probe_key) ~= 0 then
return probe_key
end
end
-- claim cauldron is empty.
return nil
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
@ -91,11 +118,7 @@ end
function mcl_cauldrons.set_level(node, fill_level, substance)
if substance == nil then
-- determine current substance.
if mcl_cauldrons.has_substance(node, "cauldron_water") then
substance = "cauldron_water"
elseif mcl_cauldrons.has_substance(node, "cauldron_river_water") then
substance = "cauldron_river_water"
end
substance = mcl_cauldrons.get_substance(node)
end
local nodename, nodedef = mcl_cauldrons.find_cauldron(substance, fill_level)
local newnode = { name = nodename, param1 = node.param1, param2 = node.param2 }
@ -109,8 +132,7 @@ function mcl_cauldrons.fill_levels(node, change_levels, substance)
local nodedef = mcl_cauldrons.registered_cauldrons[node.name]
local callback = nodedef._mcl_on_fill
-- use callback if available, else revert.
local newnode = callback and callback(node, change_levels, substance) or node
return newnode
return callback and callback(node, change_levels, substance) or node
end
-- drain counterpart to fill_levels()
@ -118,8 +140,7 @@ function mcl_cauldrons.drain_levels(node, change_levels, substance)
local nodedef = mcl_cauldrons.registered_cauldrons[node.name]
local callback = nodedef._mcl_on_drain
-- use callback if available, else revert.
local newnode = callback and callback(node, change_levels, substance) or node
return newnode
return callback and callback(node, change_levels, substance) or node
end

View File

@ -2,13 +2,14 @@ local S = minetest.get_translator(minetest.get_current_modname())
-- Convenience function because the cauldron nodeboxes are very similar
local floors_table = {
[0]=-0.1875, -- empty
[1]=1/16, -- 1/3 filled
[2]=4/16, -- 2/3 filled
[3]=7/16, -- full
}
local create_cauldron_nodebox = function(water_level)
local floors_table = {
[0]=-0.1875, -- empty
[1]=1/16, -- 1/3 filled
[2]=4/16, -- 2/3 filled
[3]=7/16, -- full
}
local floor_y = floors_table[water_level]
return {
type = "fixed",