]> git.lizzy.rs Git - xdecor.git/blob - hive.lua
Cauldron: stop sound on destruct
[xdecor.git] / hive.lua
1 local hive = {}
2 local honey_max = 16
3
4 function hive.construct(pos)
5         local meta = minetest.get_meta(pos)
6         local inv = meta:get_inventory()
7
8         local formspec = [[ size[8,5;]
9                         label[1.35,0;Bees are making honey]
10                         label[1.35,0.5;with pollen around...]
11                         image[6,0;1,1;hive_bee.png]
12                         image[5,0;1,1;hive_layout.png]
13                         list[context;honey;5,0;1,1;]
14                         list[current_player;main;0,1.35;8,4;] ]]
15                         ..xbg..default.get_hotbar_bg(0,1.35)
16
17         meta:set_string("formspec", formspec)
18         meta:set_string("infotext", "Artificial Hive")
19         inv:set_size("honey", 1)
20
21         local timer = minetest.get_node_timer(pos)
22         timer:start(math.random(64, 128))
23 end
24
25 function hive.timer(pos)
26         local time = (minetest.get_timeofday() or 0) * 24000
27         if time < 5500 or time > 18500 then return true end
28
29         local inv = minetest.get_meta(pos):get_inventory()      
30         local honeystack = inv:get_stack("honey", 1)
31         local honey = honeystack:get_count()
32
33         local radius = 4
34         local minp = vector.add(pos, -radius)
35         local maxp = vector.add(pos, radius)
36         local flowers = minetest.find_nodes_in_area_under_air(minp, maxp, "group:flower")
37
38         if #flowers > 2 and honey < honey_max then
39                 inv:add_item("honey", "xdecor:honey")
40         elseif honey == honey_max then
41                 local timer = minetest.get_node_timer(pos)
42                 timer:stop() return true
43         end
44         return true
45 end
46
47 xdecor.register("hive", {
48         description = "Artificial Hive",
49         tiles = {"xdecor_hive_top.png", "xdecor_hive_top.png",
50                  "xdecor_hive_side.png", "xdecor_hive_side.png",
51                  "xdecor_hive_side.png", "xdecor_hive_front.png"},
52         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=1},
53         on_construct = hive.construct,
54         on_timer = hive.timer,
55         can_dig = function(pos)
56                 local inv = minetest.get_meta(pos):get_inventory()
57                 return inv:is_empty("honey")
58         end,
59         on_punch = function(_, _, puncher)
60                 puncher:set_hp(puncher:get_hp() - 2)
61         end,
62         allow_metadata_inventory_put = function() return 0 end,
63         on_metadata_inventory_take = function(pos, _, _, stack)
64                 if stack:get_count() == honey_max then
65                         local timer = minetest.get_node_timer(pos)
66                         timer:start(math.random(64, 128))
67                 end
68         end
69 })
70