]> git.lizzy.rs Git - xdecor.git/blob - cooking.lua
Phase-out some aliases
[xdecor.git] / cooking.lua
1 local cauldron = {}
2
3 -- Add more ingredients here that make a soup.
4 local ingredients_list = {
5         "apple", "mushroom", "honey", "pumpkin", "egg", "bread", "meat",
6         "chicken", "carrot", "potato", "melon", "rhubarb", "cucumber",
7         "corn", "beans", "berries", "grapes", "tomato", "wheat"
8 }
9
10 cauldron.cbox = {
11         {0,  0, 0,  16, 16, 0},
12         {0,  0, 16, 16, 16, 0},
13         {0,  0, 0,  0,  16, 16},
14         {16, 0, 0,  0,  16, 16},
15         {0,  0, 0,  16, 8,  16}
16 }
17
18 function cauldron.idle_construct(pos)
19         local timer = minetest.get_node_timer(pos)
20         timer:start(10.0)
21 end
22
23 function cauldron.boiling_construct(pos)
24         local meta = minetest.get_meta(pos)
25         local timer = minetest.get_node_timer(pos)
26         meta:set_string("infotext", "Cauldron (active) - Drop some foods inside to make a soup")
27         timer:start(5.0)
28 end
29
30 function cauldron.filling(pos, node, clicker, itemstack)
31         local inv = clicker:get_inventory()
32         local wield_item = clicker:get_wielded_item():get_name()
33
34         if wield_item:sub(1,7) == "bucket:" then
35                 if wield_item:sub(-6) == "_empty" and not (node.name:sub(-6) == "_empty") then
36                         if itemstack:get_count() > 1 then
37                                 if inv:room_for_item("main", "bucket:bucket_water 1") then
38                                         itemstack:take_item()
39                                         inv:add_item("main", "bucket:bucket_water 1")
40                                 else
41                                         minetest.chat_send_player(clicker:get_player_name(),
42                                                 "No room in your inventory to add a bucket of water.")
43                                         return
44                                 end
45                         else
46                                 itemstack:replace("bucket:bucket_water")
47                         end
48                         minetest.set_node(pos, {name="xdecor:cauldron_empty", param2=node.param2})
49                 elseif wield_item:sub(-6) == "_water" and node.name:sub(-6) == "_empty" then
50                         minetest.set_node(pos, {name="xdecor:cauldron_idle", param2=node.param2})
51                         itemstack:replace("bucket:bucket_empty")
52                 end
53                 return itemstack
54         end
55 end
56
57 function cauldron.idle_timer(pos)
58         local below_node = {x=pos.x, y=pos.y-1, z=pos.z}
59         if not minetest.get_node(below_node).name:find("fire") then
60                 return true
61         end
62
63         local node = minetest.get_node(pos)
64         minetest.set_node(pos, {name="xdecor:cauldron_boiling", param2=node.param2})
65         return true
66 end
67
68 -- Ugly hack to determine if an item has `minetest.item_eat` in its definition.
69 local function eatable(itemstring)
70         local item = itemstring:match("[%w_:]+")
71         local on_use_def = minetest.registered_items[item].on_use
72         if not on_use_def then return end
73         return string.format("%q", string.dump(on_use_def)):find("item_eat")
74 end
75
76 function cauldron.boiling_timer(pos)
77         local node = minetest.get_node(pos)
78         local objs = minetest.get_objects_inside_radius(pos, 0.5)
79         if objs == {} then return true end
80
81         local ingredients = {}
82         for _, obj in pairs(objs) do
83                 if obj and not obj:is_player() and obj:get_luaentity().itemstring then
84                         local itemstring = obj:get_luaentity().itemstring
85                         local food = itemstring:match(":([%w_]+)")
86
87                         for _, ingredient in pairs(ingredients_list) do
88                                 if food and (eatable(itemstring) or food:find(ingredient)) then
89                                         ingredients[#ingredients+1] = food break
90                                 end
91                         end
92                 end
93         end
94
95         if #ingredients >= 2 then
96                 for _, obj in pairs(objs) do obj:remove() end
97                 minetest.set_node(pos, {name="xdecor:cauldron_soup", param2=node.param2})
98         end
99
100         local node_under = {x=pos.x, y=pos.y-1, z=pos.z}
101         if not minetest.get_node(node_under).name:find("fire") then
102                 minetest.set_node(pos, {name="xdecor:cauldron_idle", param2=node.param2})
103         end
104         return true
105 end
106
107 function cauldron.take_soup(pos, node, clicker, itemstack)
108         local inv = clicker:get_inventory()
109         local wield_item = clicker:get_wielded_item()
110
111         if wield_item:get_name() == "xdecor:bowl" then
112                 if wield_item:get_count() > 1 then
113                         if inv:room_for_item("main", "xdecor:bowl_soup 1") then
114                                 itemstack:take_item()
115                                 inv:add_item("main", "xdecor:bowl_soup 1")
116                         else
117                                 minetest.chat_send_player(clicker:get_player_name(),
118                                         "No room in your inventory to add a bowl of soup.")
119                                 return
120                         end
121                 else
122                         itemstack:replace("xdecor:bowl_soup 1")
123                 end
124
125                 minetest.set_node(pos, {name="xdecor:cauldron_empty", param2=node.param2})
126                 return itemstack
127         end
128 end
129
130 xdecor.register("cauldron_empty", {
131         description = "Cauldron",
132         groups = {cracky=2, oddly_breakable_by_hand=1},
133         on_rotate = screwdriver.rotate_simple,
134         tiles = {"xdecor_cauldron_top_empty.png", "xdecor_cauldron_sides.png"},
135         infotext = "Cauldron (empty)",
136         on_rightclick = cauldron.filling,
137         collision_box = xdecor.pixelbox(16, cauldron.cbox)
138 })
139
140 xdecor.register("cauldron_idle", {
141         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
142         on_rotate = screwdriver.rotate_simple,
143         tiles = {"xdecor_cauldron_top_idle.png", "xdecor_cauldron_sides.png"},
144         drop = "xdecor:cauldron_empty",
145         infotext = "Cauldron (idle)",
146         collision_box = xdecor.pixelbox(16, cauldron.cbox),
147         on_rightclick = cauldron.filling,
148         on_construct = cauldron.idle_construct,
149         on_timer = cauldron.idle_timer
150 })
151
152 xdecor.register("cauldron_boiling", {
153         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
154         on_rotate = screwdriver.rotate_simple,
155         drop = "xdecor:cauldron_empty",
156         infotext = "Cauldron (active) - Drop foods inside to make a soup",
157         damage_per_second = 2,
158         tiles = {{name="xdecor_cauldron_top_anim_boiling_water.png",
159                         animation={type="vertical_frames", length=3.0}},
160                 "xdecor_cauldron_sides.png"},
161         collision_box = xdecor.pixelbox(16, cauldron.cbox),
162         on_rightclick = cauldron.filling,
163         on_construct = cauldron.boiling_construct,
164         on_timer = cauldron.boiling_timer
165 })
166
167 xdecor.register("cauldron_soup", {
168         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
169         on_rotate = screwdriver.rotate_simple,
170         drop = "xdecor:cauldron_empty",
171         infotext = "Cauldron (active) - Use a bowl to eat the soup",
172         damage_per_second = 2,
173         tiles = {{name="xdecor_cauldron_top_anim_soup.png",
174                         animation={type="vertical_frames", length=3.0}},
175                 "xdecor_cauldron_sides.png"},
176         collision_box = xdecor.pixelbox(16, cauldron.cbox),
177         on_rightclick = cauldron.take_soup
178 })
179