]> git.lizzy.rs Git - xdecor.git/blob - src/cooking.lua
Fix "xpanes_space.png" texture not found (#125)
[xdecor.git] / src / cooking.lua
1 local cauldron, sounds = {}, {}
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.stop_sound(pos)
19         local spos = minetest.hash_node_position(pos)
20         if sounds[spos] then
21                 minetest.sound_stop(sounds[spos])
22         end
23 end
24
25 function cauldron.idle_construct(pos)
26         local timer = minetest.get_node_timer(pos)
27         timer:start(10.0)
28         cauldron.stop_sound(pos)
29 end
30
31 function cauldron.boiling_construct(pos)
32         local spos = minetest.hash_node_position(pos)
33         sounds[spos] = minetest.sound_play("xdecor_boiling_water", {
34                 pos = pos,
35                 max_hear_distance = 5,
36                 gain = 0.8,
37                 loop = true
38         })
39
40         local meta = minetest.get_meta(pos)
41         meta:set_string("infotext", "Cauldron (active) - Drop some foods inside to make a soup")
42
43         local timer = minetest.get_node_timer(pos)
44         timer:start(5.0)
45 end
46
47 function cauldron.filling(pos, node, clicker, itemstack)
48         local inv = clicker:get_inventory()
49         local wield_item = clicker:get_wielded_item():get_name()
50
51         if wield_item:sub(1,7) == "bucket:" then
52                 if wield_item:sub(-6) == "_empty" and not (node.name:sub(-6) == "_empty") then
53                         if itemstack:get_count() > 1 then
54                                 if inv:room_for_item("main", "bucket:bucket_water 1") then
55                                         itemstack:take_item()
56                                         inv:add_item("main", "bucket:bucket_water 1")
57                                 else
58                                         minetest.chat_send_player(clicker:get_player_name(),
59                                                 "No room in your inventory to add a bucket of water.")
60                                         return itemstack
61                                 end
62                         else
63                                 itemstack:replace("bucket:bucket_water")
64                         end
65                         minetest.set_node(pos, {name = "xdecor:cauldron_empty", param2 = node.param2})
66
67                 elseif wield_item:sub(-6) == "_water" and node.name:sub(-6) == "_empty" then
68                         minetest.set_node(pos, {name = "xdecor:cauldron_idle", param2 = node.param2})
69                         itemstack:replace("bucket:bucket_empty")
70                 end
71
72                 return itemstack
73         end
74 end
75
76 function cauldron.idle_timer(pos)
77         local below_node = {x = pos.x, y = pos.y - 1, z = pos.z}
78         if not minetest.get_node(below_node).name:find("fire") then
79                 return true
80         end
81
82         local node = minetest.get_node(pos)
83         minetest.set_node(pos, {name = "xdecor:cauldron_boiling", param2 = node.param2})
84         return true
85 end
86
87 -- Ugly hack to determine if an item has the function `minetest.item_eat` in its definition.
88 local function eatable(itemstring)
89         local item = itemstring:match("[%w_:]+")
90         local on_use_def = minetest.registered_items[item].on_use
91         if not on_use_def then return end
92
93         return string.format("%q", string.dump(on_use_def)):find("item_eat")
94 end
95
96 function cauldron.boiling_timer(pos)
97         local node = minetest.get_node(pos)
98         local objs = minetest.get_objects_inside_radius(pos, 0.5)
99
100         if not next(objs) then
101                 return true
102         end
103
104         local ingredients = {}
105         for _, obj in pairs(objs) do
106                 if obj and not obj:is_player() and obj:get_luaentity().itemstring then
107                         local itemstring = obj:get_luaentity().itemstring
108                         local food = itemstring:match(":([%w_]+)")
109
110                         for _, ingredient in ipairs(ingredients_list) do
111                                 if food and (eatable(itemstring) or food:find(ingredient)) then
112                                         ingredients[#ingredients + 1] = food
113                                         break
114                                 end
115                         end
116                 end
117         end
118
119         if #ingredients >= 2 then
120                 for _, obj in pairs(objs) do
121                         obj:remove()
122                 end
123
124                 minetest.set_node(pos, {name = "xdecor:cauldron_soup", param2 = node.param2})
125         end
126
127         local node_under = {x = pos.x, y = pos.y - 1, z = pos.z}
128
129         if not minetest.get_node(node_under).name:find("fire") then
130                 minetest.set_node(pos, {name = "xdecor:cauldron_idle", param2 = node.param2})
131         end
132
133         return true
134 end
135
136 function cauldron.take_soup(pos, node, clicker, itemstack)
137         local inv = clicker:get_inventory()
138         local wield_item = clicker:get_wielded_item()
139         local item_name = wield_item:get_name()
140
141         if item_name == "xdecor:bowl" or item_name == "farming:bowl" then
142                 if wield_item:get_count() > 1 then
143                         if inv:room_for_item("main", "xdecor:bowl_soup 1") then
144                                 itemstack:take_item()
145                                 inv:add_item("main", "xdecor:bowl_soup 1")
146                         else
147                                 minetest.chat_send_player(clicker:get_player_name(),
148                                         "No room in your inventory to add a bowl of soup.")
149                                 return itemstack
150                         end
151                 else
152                         itemstack:replace("xdecor:bowl_soup 1")
153                 end
154
155                 minetest.set_node(pos, {name = "xdecor:cauldron_empty", param2 = node.param2})
156         end
157
158         return itemstack
159 end
160
161 xdecor.register("cauldron_empty", {
162         description = "Cauldron",
163         groups = {cracky=2, oddly_breakable_by_hand=1},
164         on_rotate = screwdriver.rotate_simple,
165         tiles = {"xdecor_cauldron_top_empty.png", "xdecor_cauldron_sides.png"},
166         infotext = "Cauldron (empty)",
167         collision_box = xdecor.pixelbox(16, cauldron.cbox),
168         on_rightclick = cauldron.filling,
169         on_construct = function(pos)
170                 cauldron.stop_sound(pos)
171         end,
172 })
173
174 xdecor.register("cauldron_idle", {
175         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
176         on_rotate = screwdriver.rotate_simple,
177         tiles = {"xdecor_cauldron_top_idle.png", "xdecor_cauldron_sides.png"},
178         drop = "xdecor:cauldron_empty",
179         infotext = "Cauldron (idle)",
180         collision_box = xdecor.pixelbox(16, cauldron.cbox),
181         on_rightclick = cauldron.filling,
182         on_construct = cauldron.idle_construct,
183         on_timer = cauldron.idle_timer,
184 })
185
186 xdecor.register("cauldron_boiling", {
187         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
188         on_rotate = screwdriver.rotate_simple,
189         drop = "xdecor:cauldron_empty",
190         infotext = "Cauldron (active) - Drop foods inside to make a soup",
191         damage_per_second = 2,
192         tiles = {
193                 {
194                         name = "xdecor_cauldron_top_anim_boiling_water.png",
195                         animation = {type = "vertical_frames", length = 3.0}
196                 },
197                 "xdecor_cauldron_sides.png"
198         },
199         collision_box = xdecor.pixelbox(16, cauldron.cbox),
200         on_rightclick = cauldron.filling,
201         on_construct = cauldron.boiling_construct,
202         on_timer = cauldron.boiling_timer,
203         on_destruct = function(pos)
204                 cauldron.stop_sound(pos)
205         end,
206 })
207
208 xdecor.register("cauldron_soup", {
209         groups = {cracky = 2, oddly_breakable_by_hand = 1, not_in_creative_inventory = 1},
210         on_rotate = screwdriver.rotate_simple,
211         drop = "xdecor:cauldron_empty",
212         infotext = "Cauldron (active) - Use a bowl to eat the soup",
213         damage_per_second = 2,
214         tiles = {
215                 {
216                         name = "xdecor_cauldron_top_anim_soup.png",
217                         animation = {type = "vertical_frames", length = 3.0}
218                 },
219                 "xdecor_cauldron_sides.png"
220         },
221         collision_box = xdecor.pixelbox(16, cauldron.cbox),
222         on_rightclick = cauldron.take_soup,
223         on_destruct = function(pos)
224                 cauldron.stop_sound(pos)
225         end,
226 })
227
228 -- Craft items
229
230 minetest.register_craftitem("xdecor:bowl", {
231         description = "Bowl",
232         inventory_image = "xdecor_bowl.png",
233         wield_image = "xdecor_bowl.png",
234         groups = {food_bowl = 1, flammable = 2},
235 })
236
237 minetest.register_craftitem("xdecor:bowl_soup", {
238         description = "Bowl of soup",
239         inventory_image = "xdecor_bowl_soup.png",
240         wield_image = "xdecor_bowl_soup.png",
241         groups = {not_in_creative_inventory=1},
242         stack_max = 1,
243         on_use = minetest.item_eat(30, "xdecor:bowl")
244 })
245
246 -- Recipes
247
248 minetest.register_craft({
249         output = "xdecor:bowl 3",
250         recipe = {
251                 {"group:wood", "", "group:wood"},
252                 {"", "group:wood", ""}
253         }
254 })
255
256 minetest.register_craft({
257         output = "xdecor:cauldron_empty",
258         recipe = {
259                 {"default:iron_lump", "", "default:iron_lump"},
260                 {"default:iron_lump", "", "default:iron_lump"},
261                 {"default:iron_lump", "default:iron_lump", "default:iron_lump"}
262         }
263 })