]> git.lizzy.rs Git - xdecor.git/blob - src/hive.lua
Fix "xpanes_space.png" texture not found (#125)
[xdecor.git] / src / 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[0.5,0;Bees are busy making honey...]
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                         listring[current_player;main]
15                         listring[context;honey] ]] ..
16                         xbg .. default.get_hotbar_bg(0,1.35)
17
18         meta:set_string("formspec", formspec)
19         meta:set_string("infotext", "Artificial Hive")
20         inv:set_size("honey", 1)
21
22         local timer = minetest.get_node_timer(pos)
23         timer:start(math.random(64, 128))
24 end
25
26 function hive.timer(pos)
27         local time = (minetest.get_timeofday() or 0) * 24000
28         if time < 5500 or time > 18500 then
29                 return true
30         end
31
32         local inv = minetest.get_meta(pos):get_inventory()
33         local honeystack = inv:get_stack("honey", 1)
34         local honey = honeystack:get_count()
35
36         local radius = 4
37         local minp = vector.add(pos, -radius)
38         local maxp = vector.add(pos, radius)
39         local flowers = minetest.find_nodes_in_area_under_air(minp, maxp, "group:flower")
40
41         if #flowers > 2 and honey < honey_max then
42                 inv:add_item("honey", "xdecor:honey")
43         elseif honey == honey_max then
44                 local timer = minetest.get_node_timer(pos)
45                 timer:stop()
46                 return true
47         end
48
49         return true
50 end
51
52 xdecor.register("hive", {
53         description = "Artificial Hive",
54         tiles = {"xdecor_hive_top.png", "xdecor_hive_top.png",
55                  "xdecor_hive_side.png", "xdecor_hive_side.png",
56                  "xdecor_hive_side.png", "xdecor_hive_front.png"},
57         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=1},
58         on_construct = hive.construct,
59         on_timer = hive.timer,
60
61         can_dig = function(pos)
62                 local inv = minetest.get_meta(pos):get_inventory()
63                 return inv:is_empty("honey")
64         end,
65
66         on_punch = function(_, _, puncher)
67                 puncher:set_hp(puncher:get_hp() - 2)
68         end,
69
70         allow_metadata_inventory_put = function()
71                 return 0
72         end,
73
74         on_metadata_inventory_take = function(pos, _, _, stack)
75                 if stack:get_count() == honey_max then
76                         local timer = minetest.get_node_timer(pos)
77                         timer:start(math.random(64, 128))
78                 end
79         end
80 })
81
82 -- Craft items
83
84 minetest.register_craftitem("xdecor:honey", {
85         description = "Honey",
86         inventory_image = "xdecor_honey.png",
87         wield_image = "xdecor_honey.png",
88         on_use = minetest.item_eat(2),
89         groups = {
90                 food_honey = 1,
91                 food_sugar = 1,
92                 flammable = 2,
93                 not_in_creative_inventory = 1,
94         },
95 })
96
97 -- Recipes
98
99 minetest.register_craft({
100         output = "xdecor:hive",
101         recipe = {
102                 {"group:stick", "group:stick", "group:stick"},
103                 {"default:paper", "default:paper", "default:paper"},
104                 {"group:stick", "group:stick", "group:stick"}
105         }
106 })