summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorh-v-smacker <hans-von-smacker+github@gmail.com>2018-07-15 01:21:50 +0300
committerh-v-smacker <hans-von-smacker+github@gmail.com>2018-07-15 01:21:50 +0300
commitfaf128e5b560becab1f182de817adbe981201c29 (patch)
tree4a0a1cf34748486a636fa3b547f8ba36141a9521
parent4b267bcde860f081616748d38010a38352672a00 (diff)
first working barrel
-rw-r--r--nodes_barrel.lua275
1 files changed, 205 insertions, 70 deletions
diff --git a/nodes_barrel.lua b/nodes_barrel.lua
index 4a2d096..b0f85cd 100644
--- a/nodes_barrel.lua
+++ b/nodes_barrel.lua
@@ -25,22 +25,50 @@ local S = cottages.S
barrel = {};
+local barrel_max = 50
+
+local liquids = {
+ lava = { name = "Lava", bucket = "bucket:bucket_lava" },
+ water = { name = "Water", bucket = "bucket:bucket_water" },
+ rwater = { name = "River water", bucket = "bucket:bucket_river_water" },
+ cactus = { name = "Cactus pulp", bucket = "ethereal:bucket_cactus" },
+ milk = { name = "Milk", bucket = "mobs:bucket_milk" },
+}
+
+
+barrel.prepare_formspec = function(fill, contents)
+ local label = "nothing"
+ local item = "air"
+ local hint = ""
+ local percent = 0
+ if contents then
+ hint = liquids[contents].name
+ label = contents
+ item = liquids[contents].bucket
+ end
+ if fill then
+ percent = 100 * fill / barrel_max
+ end
+
+ local formspec = "size[8,9]"..
+ "image[2.6,2;2,3;default_sandstone.png^[lowpart:"..
+ percent .. ":default_desert_stone.png]"..
+ "label[2.2,0;"..S("Pour:").."]"..
+ "list[current_name;input;3,0.5;1,1;]"..
+ "item_image_button[5,2;1,1;" .. item .. ";" .. label .. ";" .. hint .. "]" ..
+ "label[5,3.3;"..S("Fill:").."]"..
+ "list[current_name;output;5,3.8;1,1;]"..
+ "list[current_player;main;0,5;8,4;]"
+
+ return (formspec)
+end
+
-- prepare formspec
barrel.on_construct = function( pos )
local meta = minetest.get_meta(pos);
- local percent = math.random( 1, 100 ); -- TODO: show real filling
-
- meta:set_string( 'formspec',
- "size[8,9]"..
- "image[2.6,2;2,3;default_sandstone.png^[lowpart:"..
- (100-percent)..":default_desert_stone.png]".. -- TODO: better images
- "label[2.2,0;"..S("Pour:").."]"..
- "list[current_name;input;3,0.5;1,1;]"..
- "label[5,3.3;"..S("Fill:").."]"..
- "list[current_name;output;5,3.8;1,1;]"..
- "list[current_player;main;0,5;8,4;]");
-
+-- local percent = math.random( 1, 100 ); -- TODO: show real filling
+ local percent = 100
meta:set_string( 'liquid_type', '' ); -- which liquid is in the barrel?
meta:set_int( 'liquid_level', 0 ); -- how much of the liquid is in there?
@@ -48,85 +76,194 @@ barrel.on_construct = function( pos )
local inv = meta:get_inventory()
inv:set_size("input", 1); -- to fill in new liquid
inv:set_size("output", 1); -- to extract liquid
-end
+
+ meta:set_string( 'formspec', barrel.prepare_formspec())
+
+-- meta:set_string( 'formspec',
+-- "size[8,9]"..
+-- "image[2.6,2;2,3;default_sandstone.png^[lowpart:"..
+-- (100-percent)..":default_desert_stone.png]".. -- TODO: better images
+-- "label[2.2,0;"..S("Pour:").."]"..
+-- "list[current_name;input;3,0.5;1,1;]"..
+-- "item_image_button[5,2;1,1;air;nothing;]" ..
+-- "label[5,3.3;"..S("Fill:").."]"..
+-- "list[current_name;output;5,3.8;1,1;]"..
+-- "list[current_player;main;0,5;8,4;]");
+
+end
+
-- can only be digged if there are no more vessels/buckets in any of the slots
-- TODO: allow digging of a filled barrel? this would disallow stacking of them
barrel.can_dig = function( pos, player )
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
- return ( inv:is_empty('input')
- and inv:is_empty('output'));
+ return true
+
+-- return ( inv:is_empty('input')
+-- and inv:is_empty('output')
+-- and meta:get_string('liquid_level') == 0);
end
+-- allow to put into "pour": if barrel is empty or has the same type
+-- allow to put into "fill": if empty bucket and barrel is not empty
+barrel.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
+ local iname = stack:get_name()
+
+ local meta = minetest.get_meta(pos)
+
+ if listname == "input" then
+
+ local ltype = false
+
+ for l,d in pairs(liquids) do
+ if d.bucket == iname then
+ ltype = l
+ break
+ end
+ end
+
+ if not ltype then
+ return 0
+ end
+
+ local lt = meta:get_string('liquid_type')
+ if lt == "" or (ltype == lt and meta:get_int('liquid_level') < barrel_max) then
+ return 1
+ else
+ return 0
+ end
+ end
+
+ if listname == "output" then
+ if iname == "bucket:bucket_empty" and meta:get_int('liquid_level') > 0 then
+ return 1
+ else
+ return 0
+ end
+ end
+
+ return 0
+
+end
+
+
+
-- the barrel received input; either a new liquid that is to be poured in or a vessel that is to be filled
barrel.on_metadata_inventory_put = function( pos, listname, index, stack, player )
+
+ local iname = stack:get_name()
+ local meta = minetest.get_meta(pos)
+ local inv = meta:get_inventory()
+
+ if listname == "input" then
+ local liquid = ""
+ for l,d in pairs(liquids) do
+ if d.bucket == iname then
+ liquid = d.bucket
+ inv:set_stack("input", 1, {name = "bucket:bucket_empty"})
+ local level = meta:get_int("liquid_level") + 1
+ minetest.chat_send_all(level)
+ meta:set_int("liquid_level", level)
+ meta:set_string("liquid_type", l)
+
+ meta:set_string( 'formspec', barrel.prepare_formspec(level, l))
+ minetest.swap_node(pos, {name = "cottages:barrel"})
+ break
+ end
+ end
+ end
+
+
+ if listname == "output" then
+ local lt = meta:get_string("liquid_type")
+
+ inv:set_stack("output", 1, {name = liquids[lt].bucket})
+ local level = meta:get_int("liquid_level") - 1
+ minetest.chat_send_all(level)
+ if level == 0 then
+ minetest.swap_node(pos, {name = "cottages:barrel_open"})
+ meta:set_string("liquid_type", "")
+ meta:set_int("liquid_level", 0)
+ lt = nil
+ else
+ meta:set_int("liquid_level", level)
+ end
+
+ meta:set_string( 'formspec', barrel.prepare_formspec(level, lt))
+
+ end
+
+
end
-- right-click to open/close barrel; punch to switch between horizontal/vertical position
minetest.register_node("cottages:barrel", {
- description = S("Barrel (closed)"),
- paramtype = "light",
- drawtype = "mesh",
- mesh = "cottages_barrel_closed.obj",
- tiles = {"cottages_barrel.png" },
- groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2
- },
+ description = S("Barrel (closed)"),
+ paramtype = "light",
+ drawtype = "mesh",
+ mesh = "cottages_barrel_closed.obj",
+ tiles = {"cottages_barrel.png" },
+ groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory = 1 },
drop = "cottages:barrel",
-- on_rightclick = function(pos, node, puncher)
-- minetest.add_node(pos, {name = "cottages:barrel_open", param2 = node.param2})
-- end,
-- TODO: on_rightclick is no longer available - maybe open if empty and closed if full?
- on_punch = function(pos, node, puncher)
- minetest.add_node(pos, {name = "cottages:barrel_lying", param2 = node.param2})
- end,
+-- on_punch = function(pos, node, puncher)
+-- minetest.add_node(pos, {name = "cottages:barrel_lying", param2 = node.param2})
+-- end,
+
+ on_construct = function( pos )
+ return barrel.on_construct( pos );
+ end,
+ can_dig = function(pos,player)
+ return barrel.can_dig( pos, player );
+ end,
+ allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
+ on_metadata_inventory_put = barrel.on_metadata_inventory_put,
- on_construct = function( pos )
- return barrel.on_construct( pos );
- end,
- can_dig = function(pos,player)
- return barrel.can_dig( pos, player );
- end,
- on_metadata_inventory_put = function(pos, listname, index, stack, player)
- return barrel.on_metadata_inventory_put( pos, listname, index, stack, player );
- end,
is_ground_content = false,
})
-- this barrel is opened at the top
minetest.register_node("cottages:barrel_open", {
- description = S("Barrel (open)"),
- paramtype = "light",
- drawtype = "mesh",
- mesh = "cottages_barrel.obj",
- tiles = {"cottages_barrel.png" },
- groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory=1,
- },
+ description = S("Barrel (open)"),
+ paramtype = "light",
+ drawtype = "mesh",
+ mesh = "cottages_barrel.obj",
+ tiles = {"cottages_barrel.png" },
+ groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
drop = "cottages:barrel",
--- on_rightclick = function(pos, node, puncher)
--- minetest.add_node(pos, {name = "cottages:barrel", param2 = node.param2})
--- end,
- on_punch = function(pos, node, puncher)
- minetest.add_node(pos, {name = "cottages:barrel_lying_open", param2 = node.param2})
- end,
+-- on_punch = function(pos, node, puncher)
+-- minetest.add_node(pos, {name = "cottages:barrel_lying_open", param2 = node.param2})
+-- end,
+ on_construct = function( pos )
+ return barrel.on_construct( pos );
+ end,
+ can_dig = function(pos,player)
+ return barrel.can_dig( pos, player );
+ end,
+ allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
+ on_metadata_inventory_put = barrel.on_metadata_inventory_put,
+
is_ground_content = false,
})
-- horizontal barrel
minetest.register_node("cottages:barrel_lying", {
- description = S("Barrel (closed), lying somewhere"),
- paramtype = "light",
- paramtype2 = "facedir",
- drawtype = "mesh",
- mesh = "cottages_barrel_closed_lying.obj",
- tiles = {"cottages_barrel.png" },
- groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory=1,
- },
+ description = S("Barrel (closed), lying somewhere"),
+ paramtype = "light",
+ paramtype2 = "facedir",
+ drawtype = "mesh",
+ mesh = "cottages_barrel_closed_lying.obj",
+ tiles = {"cottages_barrel.png" },
+ groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory = 1 },
drop = "cottages:barrel",
on_rightclick = function(pos, node, puncher)
minetest.add_node(pos, {name = "cottages:barrel_lying_open", param2 = node.param2})
@@ -143,14 +280,13 @@ end
-- horizontal barrel, open
minetest.register_node("cottages:barrel_lying_open", {
- description = S("Barrel (opened), lying somewhere"),
- paramtype = "light",
- paramtype2 = "facedir",
- drawtype = "mesh",
- mesh = "cottages_barrel_lying.obj",
- tiles = {"cottages_barrel.png" },
- groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory=1,
- },
+ description = S("Barrel (opened), lying somewhere"),
+ paramtype = "light",
+ paramtype2 = "facedir",
+ drawtype = "mesh",
+ mesh = "cottages_barrel_lying.obj",
+ tiles = {"cottages_barrel.png" },
+ groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory = 1 },
drop = "cottages:barrel",
on_rightclick = function(pos, node, puncher)
minetest.add_node(pos, {name = "cottages:barrel_lying", param2 = node.param2})
@@ -168,11 +304,11 @@ end
-- let's hope "tub" is the correct english word for "bottich"
minetest.register_node("cottages:tub", {
- description = S("Tub"),
- paramtype = "light",
- drawtype = "mesh",
- mesh = "cottages_tub.obj",
- tiles = {"cottages_barrel.png" },
+ description = S("Tub"),
+ paramtype = "light",
+ drawtype = "mesh",
+ mesh = "cottages_tub.obj",
+ tiles = {"cottages_barrel.png" },
selection_box = {
type = "fixed",
fixed = {
@@ -183,8 +319,7 @@ end
fixed = {
{-0.5, -0.5, -0.5, 0.5,-0.1, 0.5},
}},
- groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2
- },
+ groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2 },
is_ground_content = false,
})