]> git.lizzy.rs Git - xdecor.git/blob - cooking.lua
Update .gitignore
[xdecor.git] / 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
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 `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 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
123         if wield_item:get_name() == "xdecor:bowl" then
124                 if wield_item:get_count() > 1 then
125                         if inv:room_for_item("main", "xdecor:bowl_soup 1") then
126                                 itemstack:take_item()
127                                 inv:add_item("main", "xdecor:bowl_soup 1")
128                         else
129                                 minetest.chat_send_player(clicker:get_player_name(),
130                                         "No room in your inventory to add a bowl of soup.")
131                                 return
132                         end
133                 else
134                         itemstack:replace("xdecor:bowl_soup 1")
135                 end
136
137                 minetest.set_node(pos, {name="xdecor:cauldron_empty", param2=node.param2})
138                 return itemstack
139         end
140 end
141
142 xdecor.register("cauldron_empty", {
143         description = "Cauldron",
144         groups = {cracky=2, oddly_breakable_by_hand=1},
145         on_rotate = screwdriver.rotate_simple,
146         tiles = {"xdecor_cauldron_top_empty.png", "xdecor_cauldron_sides.png"},
147         infotext = "Cauldron (empty)",
148         on_construct = function(pos)
149                 cauldron.stop_sound(pos)
150         end,
151         on_rightclick = cauldron.filling,
152         collision_box = xdecor.pixelbox(16, cauldron.cbox)
153 })
154
155 xdecor.register("cauldron_idle", {
156         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
157         on_rotate = screwdriver.rotate_simple,
158         tiles = {"xdecor_cauldron_top_idle.png", "xdecor_cauldron_sides.png"},
159         drop = "xdecor:cauldron_empty",
160         infotext = "Cauldron (idle)",
161         collision_box = xdecor.pixelbox(16, cauldron.cbox),
162         on_rightclick = cauldron.filling,
163         on_construct = cauldron.idle_construct,
164         on_timer = cauldron.idle_timer
165 })
166
167 xdecor.register("cauldron_boiling", {
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) - Drop foods inside to make a soup",
172         damage_per_second = 2,
173         tiles = {{name="xdecor_cauldron_top_anim_boiling_water.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.filling,
178         on_construct = cauldron.boiling_construct,
179         on_destruct = function(pos)
180                 cauldron.stop_sound(pos)
181         end,
182         on_timer = cauldron.boiling_timer
183 })
184
185 xdecor.register("cauldron_soup", {
186         groups = {cracky=2, oddly_breakable_by_hand=1, not_in_creative_inventory=1},
187         on_rotate = screwdriver.rotate_simple,
188         drop = "xdecor:cauldron_empty",
189         infotext = "Cauldron (active) - Use a bowl to eat the soup",
190         damage_per_second = 2,
191         tiles = {{name="xdecor_cauldron_top_anim_soup.png",
192                         animation={type="vertical_frames", length=3.0}},
193                 "xdecor_cauldron_sides.png"},
194         collision_box = xdecor.pixelbox(16, cauldron.cbox),
195         on_rightclick = cauldron.take_soup,
196         on_destruct = function(pos)
197                 cauldron.stop_sound(pos)
198         end
199 })
200