2018-08-28 22:24:43 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tubelib Addons 1
|
|
|
|
================
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-08-28 22:24:43 +02:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-08-28 22:24:43 +02:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
liquidsampler.lua
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2020-11-03 20:19:38 +01:00
|
|
|
-- Load support for I18n
|
|
|
|
local S = tubelib_addons1.S
|
|
|
|
|
2018-12-27 22:37:13 +01:00
|
|
|
-- for lazy programmers
|
|
|
|
local P = minetest.string_to_pos
|
|
|
|
local M = minetest.get_meta
|
|
|
|
|
|
|
|
local STANDBY_TICKS = 4
|
|
|
|
local COUNTDOWN_TICKS = 2
|
2018-08-28 22:24:43 +02:00
|
|
|
local CYCLE_TIME = 8
|
|
|
|
|
2019-01-05 18:04:56 +01:00
|
|
|
local function formspec(self, pos, meta)
|
2018-12-27 22:37:13 +01:00
|
|
|
return "size[9,8.5]"..
|
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
|
|
|
"list[context;src;0,0;1,4;]"..
|
|
|
|
"image[0,0;1,1;bucket.png]"..
|
|
|
|
"image[1,1;1,1;tubelib_gui_arrow.png]"..
|
2019-01-05 18:04:56 +01:00
|
|
|
"image_button[1,3;1,1;".. self:get_state_button_image(meta) ..";state_button;]"..
|
2018-12-27 22:37:13 +01:00
|
|
|
"list[context;dst;2,0;7,4;]"..
|
|
|
|
"list[current_player;main;0.5,4.5;8,4;]"..
|
|
|
|
"listring[current_player;main]"..
|
|
|
|
"listring[context;src]" ..
|
|
|
|
"listring[current_player;main]"..
|
|
|
|
"listring[context;dst]" ..
|
|
|
|
"listring[current_player;main]"
|
|
|
|
end
|
|
|
|
|
2019-01-05 18:04:56 +01:00
|
|
|
local State = tubelib.NodeStates:new({
|
|
|
|
node_name_passive = "tubelib_addons1:liquidsampler",
|
|
|
|
node_name_active = "tubelib_addons1:liquidsampler_active",
|
|
|
|
node_name_defect = "tubelib_addons1:liquidsampler_defect",
|
2020-11-03 20:19:38 +01:00
|
|
|
infotext_name = S("Liquid Sampler"),
|
2019-01-05 18:04:56 +01:00
|
|
|
cycle_time = CYCLE_TIME,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
has_item_meter = true,
|
|
|
|
aging_factor = 8,
|
|
|
|
formspec_func = formspec,
|
|
|
|
})
|
2018-12-27 22:37:13 +01:00
|
|
|
|
2018-08-28 22:24:43 +02:00
|
|
|
local function get_pos(pos, facedir, side)
|
|
|
|
local offs = {F=0, R=1, B=2, L=3, D=4, U=5}
|
|
|
|
local dst_pos = table.copy(pos)
|
|
|
|
facedir = (facedir + offs[side]) % 4
|
|
|
|
local dir = minetest.facedir_to_dir(facedir)
|
|
|
|
return vector.add(dst_pos, dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function test_liquid(node)
|
|
|
|
local liquiddef = bucket.liquids[node.name]
|
|
|
|
if liquiddef ~= nil and liquiddef.itemname ~= nil and
|
|
|
|
node.name == liquiddef.source then
|
|
|
|
return liquiddef.itemname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sample_liquid(pos, meta)
|
2018-12-27 22:37:13 +01:00
|
|
|
local water_pos = P(meta:get_string("water_pos"))
|
|
|
|
local giving_back = test_liquid(minetest.get_node(water_pos))
|
2018-08-28 22:24:43 +02:00
|
|
|
if giving_back then
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
if inv:room_for_item("dst", ItemStack(giving_back)) and
|
|
|
|
inv:contains_item("src", ItemStack("bucket:bucket_empty")) then
|
2018-12-27 22:37:13 +01:00
|
|
|
minetest.remove_node(water_pos)
|
2018-08-28 22:24:43 +02:00
|
|
|
inv:remove_item("src", ItemStack("bucket:bucket_empty"))
|
|
|
|
inv:add_item("dst", ItemStack(giving_back))
|
2018-12-27 22:37:13 +01:00
|
|
|
State:keep_running(pos, meta, COUNTDOWN_TICKS)
|
2018-08-28 22:24:43 +02:00
|
|
|
else
|
2020-03-03 22:25:34 +01:00
|
|
|
State:blocked(pos, meta)
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
|
|
|
else
|
2020-03-03 22:25:34 +01:00
|
|
|
State:idle(pos, meta)
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
2018-12-27 22:37:13 +01:00
|
|
|
State:idle(pos, meta)
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return 0
|
|
|
|
end
|
2018-12-27 22:37:13 +01:00
|
|
|
if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then
|
|
|
|
State:start(pos, M(pos))
|
|
|
|
end
|
2018-08-28 22:24:43 +02:00
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
|
2018-12-27 22:37:13 +01:00
|
|
|
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
local inv = M(pos):get_inventory()
|
|
|
|
local stack = inv:get_stack(from_list, from_index)
|
|
|
|
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
|
|
|
|
end
|
|
|
|
|
2018-08-28 22:24:43 +02:00
|
|
|
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
|
2018-12-27 22:37:13 +01:00
|
|
|
local function keep_running(pos, elapsed)
|
2019-04-23 21:46:08 +02:00
|
|
|
if tubelib.data_not_corrupted(pos) then
|
|
|
|
local meta = M(pos)
|
|
|
|
sample_liquid(pos, meta)
|
|
|
|
return State:is_active(meta)
|
|
|
|
end
|
|
|
|
return false
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
|
|
|
|
2018-12-27 22:37:13 +01:00
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
2018-08-28 22:24:43 +02:00
|
|
|
return
|
|
|
|
end
|
2018-12-27 22:37:13 +01:00
|
|
|
State:state_button_event(pos, fields)
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_node("tubelib_addons1:liquidsampler", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("Liquid Sampler"),
|
2018-08-28 22:24:43 +02:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
'tubelib_addons1_liquidsampler_passive.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
},
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer)
|
2019-01-06 15:03:22 +01:00
|
|
|
local number = tubelib.add_node(pos, "tubelib_addons1:liquidsampler")
|
2018-12-27 22:37:13 +01:00
|
|
|
State:node_init(pos, number)
|
|
|
|
local meta = M(pos)
|
2018-08-28 22:24:43 +02:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local water_pos = get_pos(pos, node.param2, "L")
|
|
|
|
meta:set_string("water_pos", minetest.pos_to_string(water_pos))
|
2018-12-27 22:37:13 +01:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("src", 4)
|
|
|
|
inv:set_size("dst", 28)
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
|
2019-01-06 15:03:22 +01:00
|
|
|
can_dig = function(pos, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return false
|
2018-12-27 22:37:13 +01:00
|
|
|
end
|
2019-01-06 15:03:22 +01:00
|
|
|
local inv = M(pos):get_inventory()
|
|
|
|
return inv:is_empty("dst") and inv:is_empty("src")
|
2018-12-27 22:37:13 +01:00
|
|
|
end,
|
2018-08-28 22:24:43 +02:00
|
|
|
|
2020-08-12 20:15:26 +02:00
|
|
|
on_dig = function(pos, node, player)
|
|
|
|
State:on_dig_node(pos, node, player)
|
2019-01-06 15:03:22 +01:00
|
|
|
tubelib.remove_node(pos)
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_rotate = screwdriver.disallow,
|
2018-12-27 22:37:13 +01:00
|
|
|
on_timer = keep_running,
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
2018-08-28 22:24:43 +02:00
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=2, cracky=2, crumbly=2},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_node("tubelib_addons1:liquidsampler_active", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("Liquid Sampler"),
|
2018-08-28 22:24:43 +02:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
{
|
|
|
|
image = "tubelib_addons1_liquidsampler_active.png",
|
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png',
|
|
|
|
},
|
|
|
|
|
|
|
|
on_rotate = screwdriver.disallow,
|
2018-12-27 22:37:13 +01:00
|
|
|
on_timer = keep_running,
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
|
2018-08-28 22:24:43 +02:00
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {crumbly=0, not_in_creative_inventory=1},
|
|
|
|
is_ground_content = false,
|
2018-12-27 22:37:13 +01:00
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_node("tubelib_addons1:liquidsampler_defect", {
|
2020-11-03 20:19:38 +01:00
|
|
|
description = S("Liquid Sampler"),
|
2018-12-27 22:37:13 +01:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_front.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
|
|
|
|
'tubelib_addons1_liquidsampler_passive.png^tubelib_defect.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
|
|
|
|
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
|
|
|
|
},
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer)
|
|
|
|
local number = tubelib.add_node(pos, "tubelib_addons1:liquidsampler")
|
|
|
|
State:node_init(pos, number)
|
|
|
|
local meta = M(pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local water_pos = get_pos(pos, node.param2, "L")
|
|
|
|
meta:set_string("water_pos", minetest.pos_to_string(water_pos))
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("src", 4)
|
|
|
|
inv:set_size("dst", 28)
|
|
|
|
State:defect(pos, meta)
|
|
|
|
end,
|
|
|
|
|
2019-01-06 15:03:22 +01:00
|
|
|
can_dig = function(pos, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return false
|
2018-12-27 22:37:13 +01:00
|
|
|
end
|
2019-01-06 15:03:22 +01:00
|
|
|
local inv = M(pos):get_inventory()
|
|
|
|
return inv:is_empty("dst") and inv:is_empty("src")
|
|
|
|
end,
|
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
tubelib.remove_node(pos)
|
2018-12-27 22:37:13 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
|
|
|
|
is_ground_content = false,
|
2018-08-28 22:24:43 +02:00
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "tubelib_addons1:liquidsampler",
|
|
|
|
recipe = {
|
|
|
|
{"group:wood", "default:steel_ingot", "group:wood"},
|
2018-12-31 14:59:02 +01:00
|
|
|
{"default:mese_crystal", "bucket:bucket_empty", "tubelib:tubeS"},
|
2018-08-28 22:24:43 +02:00
|
|
|
{"group:wood", "default:steel_ingot", "group:wood"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-27 22:37:13 +01:00
|
|
|
tubelib.register_node("tubelib_addons1:liquidsampler",
|
|
|
|
{"tubelib_addons1:liquidsampler_active", "tubelib_addons1:liquidsampler_defect"}, {
|
2018-08-28 22:24:43 +02:00
|
|
|
on_pull_item = function(pos, side)
|
2018-12-27 22:37:13 +01:00
|
|
|
return tubelib.get_item(M(pos), "dst")
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
on_push_item = function(pos, side, item)
|
2018-12-27 22:37:13 +01:00
|
|
|
return tubelib.put_item(M(pos), "src", item)
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
on_unpull_item = function(pos, side, item)
|
2018-12-27 22:37:13 +01:00
|
|
|
return tubelib.put_item(M(pos), "dst", item)
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
on_recv_message = function(pos, topic, payload)
|
2018-12-27 22:37:13 +01:00
|
|
|
local resp = State:on_receive_message(pos, topic, payload)
|
|
|
|
if resp then
|
|
|
|
return resp
|
2018-08-28 22:24:43 +02:00
|
|
|
else
|
2018-12-27 22:37:13 +01:00
|
|
|
return "unsupported"
|
2018-08-28 22:24:43 +02:00
|
|
|
end
|
|
|
|
end,
|
2018-12-22 16:58:11 +01:00
|
|
|
on_node_load = function(pos)
|
2018-12-27 22:37:13 +01:00
|
|
|
State:on_node_load(pos)
|
|
|
|
end,
|
|
|
|
on_node_repair = function(pos)
|
|
|
|
return State:on_node_repair(pos)
|
2018-12-22 16:58:11 +01:00
|
|
|
end,
|
2018-08-28 22:24:43 +02:00
|
|
|
})
|