From 8d68fba505cf2b17c9ed8d105c04b5c2d3aab9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Thu, 23 Nov 2017 13:37:00 +0100 Subject: added Milker --- fakeplayer.lua | 21 +++++- init.lua | 152 ++++++++++++++++++++++++++++++--------- textures/feedlot_milker_side.png | Bin 0 -> 268 bytes textures/feedlot_milker_top.png | Bin 0 -> 185 bytes 4 files changed, 137 insertions(+), 36 deletions(-) create mode 100644 textures/feedlot_milker_side.png create mode 100644 textures/feedlot_milker_top.png diff --git a/fakeplayer.lua b/fakeplayer.lua index d56f29a..1e1e2ab 100644 --- a/fakeplayer.lua +++ b/fakeplayer.lua @@ -49,15 +49,30 @@ local function get_feed (pos) if not inv or not inv:get_list("main") then return ItemStack(nil) end - -- minetest.chat_send_all(inv:get_stack("main", 1):get_name()) + if inv:get_list("bucket") then + return inv:get_stack("bucket", 1) + end return inv:get_stack("main", 1) end end + +local function get_inv(pos) + return function () + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv + end +end + local function set_feed (pos) return function (_, stack) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - inv:set_stack("main", 1, stack) + if inv:get_list("bucket") then + inv:set_stack("bucket", 1, stack) + else + inv:set_stack("main", 1, stack) + end return end end @@ -76,7 +91,7 @@ function feedlotFakePlayer.create(pos, player_name) self.right_click = return_nil self.get_hp = return_value(10) self.set_hp = return_nil - self.get_inventory = return_nil -- returns an `InvRef` + self.get_inventory = get_inv(pos) -- returns an `InvRef` self.get_wield_list = return_empty_string self.get_wield_index = return_value(1) -- self.get_wielded_item = return_value(ItemStack(nil)) diff --git a/init.lua b/init.lua index bb2c35b..5162179 100644 --- a/init.lua +++ b/init.lua @@ -9,12 +9,86 @@ local function get_formspec(pos) -- local detached_inv = minetest.create_detached_inventory("mailbox_"..owner) return "size[8,5.5]" .. xbg .. default.get_hotbar_bg(0, 1.5) .. "label[0,0;Feedlot]" .. - "list[nodemeta:" .. spos .. ";main;3.5,0;1,1;]" .. + "list[current_name;main;3.5,0;1,1;]" .. "list[current_player;main;0,1.5;8,1;]" .. "list[current_player;main;0,2.75;8,3;8]" .. -- "listring[detached:mailbox_" .. owner .. ";drop]" .. "listring[current_player;main]" end +local function get_milker_formspec(pos) + return "size[8,8.5]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "label[0,0;Milker]".. + "label[0,1;Buckets:]".. + "label[3.5,1;Milk:]".. + "list[current_name;bucket;1.5,1;1,1;]".. + "list[current_name;main;4.75,0.96;3,2;]".. + "list[current_player;main;0,4.25;8,1;]".. + "list[current_player;main;0,5.5;8,3;8]".. + "listring[current_name;dst]".. + "listring[current_player;main]".. + "listring[current_name;src]".. + "listring[current_player;main]".. + default.get_hotbar_bg(0, 4.25) +end + + +local function on_construct(pos) + local node = minetest.get_node(pos) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if node.name == "feedlot:milker" then + meta:set_string("formspec", get_milker_formspec(pos)) + inv:set_size("bucket", 1) + inv:set_size("main", 6) + else + meta:set_string("formspec", get_formspec(pos)) + inv:set_size("main", 1) + end +end + +local function can_dig(pos) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv:is_empty("main") +end +local tube = { + insert_object = function(pos, node, stack, direction) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if node.name == "feedlot:milker" then + if stack:get_name() == "bucket:bucket_empty" and inv:room_for_item("bucket", stack) then + inv:add_item("bucket", stack) + return ItemStack(nil) + else + return stack + end + end + + if inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty" then + inv:set_stack("main", 1, stack) + return ItemStack(nil) + else + return stack + end + end, + can_insert = function(pos, node, stack, direction) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + if node.name == "feedlot:milker" then + if stack:get_name() == "bucket:bucket_empty" then + return inv:room_for_item("bucket", stack) + else + return false + end + else + return inv:room_for_item("main", stack) and stack:get_name() ~= "bucket:bucket_empty" + end + end, + input_inventory = "main", + connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}} minetest.register_node("feedlot:feedlot", { description = "Feedlot", @@ -38,53 +112,65 @@ minetest.register_node("feedlot:feedlot", { {-0.4375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox10 } }, - - on_construct = function (pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - meta:set_string("formspec", get_formspec(pos)) - inv:set_size("main", 1) + allow_metadata_inventory_put = function (_, _, _, stack, _) + if stack:get_name() == "bucket:bucket_empty" then + return 0 + else + return stack:get_count() + end + end, + on_construct = on_construct, + can_dig = can_dig, + groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1}, + tube = tube +}) +minetest.register_node("feedlot:milker", { + description = "Feedlot", + tiles = { + "feedlot_milker_top.png", + "feedlot_milker_top.png", + "feedlot_milker_side.png", + "feedlot_milker_side.png", + "feedlot_milker_side.png", + "feedlot_milker_side.png", + }, + paramtype = "light", + allow_metadata_inventory_put = function (_, listname, _, stack, _) + if listname == "bucket" and stack:get_name() == "bucket:bucket_empty" then + return stack:get_count() + else + return 0 + end end, - can_dig = function (pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - return inv:is_empty("main") + allow_metadata_inventory_move = function (_, _, _, dst, _, count) + return 0 end, + on_construct = on_construct, + can_dig = can_dig, groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1}, - tube = { - insert_object = function(pos, node, stack, direction) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - if inv:room_for_item("main", stack) then - inv:set_stack("main", 1, stack) - return ItemStack(nil) - else - return stack - end - end, - can_insert = function(pos, node, stack, direction) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - return inv:room_for_item("main", stack) - end, - input_inventory = "main", - connect_sides = {left = 0, right = 0, front = 0, back = 0, top = 0, bottom = 1}}, + tube = tube }) + + local function is_mob(obj) return obj.get_luaentity and obj:get_luaentity().name and string.sub(obj:get_luaentity().name,1,string.len("mob"))=="mob" end minetest.register_abm({ - label = "lava cooling", - nodenames = {"feedlot:feedlot"}, + label = "Feeding and milking", + nodenames = {"feedlot:feedlot", "feedlot:milker"}, interval = 5, chance = 1, - action = function (pos) + action = function (pos, node) local fake_player = feedlotFakePlayer.create(pos, "fake_player") - local objs = minetest.get_objects_inside_radius(pos, 3) + local radius = 3 + if node.name == "feedlot:milker" then + radius = 5 + end + local objs = minetest.get_objects_inside_radius(pos, radius) for _,obj in ipairs(objs) do if not obj:is_player() and obj:get_armor_groups().fleshy and obj:get_armor_groups().fleshy > 0 and is_mob(obj) then -- obj:on_rightclick(fake_player) diff --git a/textures/feedlot_milker_side.png b/textures/feedlot_milker_side.png new file mode 100644 index 0000000..6009d70 Binary files /dev/null and b/textures/feedlot_milker_side.png differ diff --git a/textures/feedlot_milker_top.png b/textures/feedlot_milker_top.png new file mode 100644 index 0000000..526e5ca Binary files /dev/null and b/textures/feedlot_milker_top.png differ -- cgit v1.2.3