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