Node IO API Support (experimental)

This commit is contained in:
Evert Prants 2018-08-22 16:39:03 +03:00
parent b521fc134e
commit a587717bd8
No known key found for this signature in database
GPG Key ID: 1688DA83D222D0B5
5 changed files with 107 additions and 0 deletions

1
fluid_lib/depends.txt Normal file

@ -0,0 +1 @@
node_io?

@ -44,3 +44,4 @@ function fluid_lib.comma_value(n) -- credit http://richard.warburton.it
end
dofile(modpath.."/buffer.lua")
dofile(modpath.."/nodeio.lua")

@ -1 +1,2 @@
name = fluid_lib
optional_depends = node_io

98
fluid_lib/nodeio.lua Normal file

@ -0,0 +1,98 @@
-- Node IO System
local nodeiodef = {
node_io_can_put_liquid = function (pos, node, side)
return ele.helpers.get_item_group(node.name, 'fluid_container')
end,
node_io_can_take_liquid = function (pos, node, side)
return ele.helpers.get_item_group(node.name, 'fluid_container')
end,
-- if false, transfer node should only put and take in 1000 increments
-- inventory nodes that don't accept milibuckets should:
-- return zero in node_io_room_for_liquid() if non-1000 increment
-- return millibuckets parameter in node_io_put_liquid() if non-1000 increment
-- only return upto a 1000 increment in node_io_take_liquid()
-- transfer nodes that can put non-1000 increments should always check this or the inventory node might pretend to be full
node_io_accepts_millibuckets = function(pos, node, side) return true end,
node_io_put_liquid = function(pos, node, side, putter, liquid, millibuckets)
local buffers = fluid_lib.get_node_buffers(pos)
local leftovers = 0
for buffer,data in pairs(buffers) do
if millibuckets == 0 then break end
local didnt_fit = fluid_lib.insert_into_buffer(pos, buffer, liquid, millibuckets)
millibuckets = millibuckets - (millibuckets - didnt_fit)
leftovers = leftovers + didnt_fit
end
return leftovers
end,
-- returns millibuckets if inventory can hold entire amount, else returns amount the inventory can hold
-- use millibuckets=1 to check if not full, then call put_liquid() with actual amount to transfer
-- use millibuckets=1000 with room_for_liquid() and put_liquid() to only insert full buckets
node_io_room_for_liquid = function(pos, node, side, liquid, millibuckets)
local buffers = fluid_lib.get_node_buffers(pos)
local insertable = 0
for buffer,data in pairs(buffers) do
local insert = fluid_lib.can_insert_into_buffer(pos, buffer, liquid, millibuckets)
if insert > 0 then
insertable = insert
break
end
end
return insertable
end,
-- returns {name:string, millibuckets:int} with <= want_millibuckets or nil if inventory is empty or doesn't have want_liquid
-- want_liquid should be the name of a source liquid (in bucket.liquids of bucket mod)
node_io_take_liquid = function(pos, node, side, taker, want_liquid, want_millibuckets)
local buffers = fluid_lib.get_node_buffers(pos)
local took = 0
local name = ""
for buffer,data in pairs(buffers) do
local bfdata = fluid_lib.get_buffer_data(pos, buffer)
local storage = bfdata.amount
local fluid = bfdata.fluid
if (fluid == want_liquid or want_liquid == "") and storage >= want_millibuckets then
name,took = fluid_lib.take_from_buffer(pos, buffer, want_millibuckets)
if took > 0 then break end
end
end
return {name = name, millibuckets = took}
end,
node_io_get_liquid_size = function (pos, node, side)
-- this is always 1 unless inventory can hold multiple liquid types
local cnt = 0
local bfs = fluid_lib.get_node_buffers(pos)
for _ in pairs(bfs) do
cnt = cnt + 1
end
return cnt
end,
node_io_get_liquid_name = function(pos, node, side, index)
local cnt = {}
local bfs = fluid_lib.get_node_buffers(pos)
for buf in pairs(bfs) do
cnt[#cnt + 1] = buf
end
local meta = minetest.get_meta(pos)
return meta:get_string(cnt[index] .. "_fluid")
end,
node_io_get_liquid_stack = function(pos, node, side, index)
local cnt = {}
local bfs = fluid_lib.get_node_buffers(pos)
for buf in pairs(bfs) do
cnt[#cnt + 1] = buf
end
local meta = minetest.get_meta(pos)
return ItemStack(meta:get_string(cnt[index] .. "_fluid") .. " " ..
meta:get_int(cnt[index] .. "_fluid_storage"))
end,
}
function fluid_lib.register_node(nodename)
minetest.override_item(nodename, nodeiodef)
end

@ -144,6 +144,12 @@ local function create_tank_node(tankname, def, fluid_name)
preserve_metadata = preserve_metadata,
after_place_node = after_place_node,
})
if tankname:match("^:") then
tankname = tankname:sub(2)
end
fluid_lib.register_node(tankname)
end
function fluid_tanks.register_tank(tankname, def)