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