Add node particles when leafdecay removes a node (#2686)
[oweals/minetest_game.git] / mods / default / chests.lua
1 default.chest = {}
2
3 -- support for MT game translation.
4 local S = default.get_translator
5
6 function default.chest.get_chest_formspec(pos)
7         local spos = pos.x .. "," .. pos.y .. "," .. pos.z
8         local formspec =
9                 "size[8,9]" ..
10                 "list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
11                 "list[current_player;main;0,4.85;8,1;]" ..
12                 "list[current_player;main;0,6.08;8,3;8]" ..
13                 "listring[nodemeta:" .. spos .. ";main]" ..
14                 "listring[current_player;main]" ..
15                 default.get_hotbar_bg(0,4.85)
16         return formspec
17 end
18
19 function default.chest.chest_lid_obstructed(pos)
20         local above = {x = pos.x, y = pos.y + 1, z = pos.z}
21         local def = minetest.registered_nodes[minetest.get_node(above).name]
22         -- allow ladders, signs, wallmounted things and torches to not obstruct
23         if def and
24                         (def.drawtype == "airlike" or
25                         def.drawtype == "signlike" or
26                         def.drawtype == "torchlike" or
27                         (def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
28                 return false
29         end
30         return true
31 end
32
33 function default.chest.chest_lid_close(pn)
34         local chest_open_info = default.chest.open_chests[pn]
35         local pos = chest_open_info.pos
36         local sound = chest_open_info.sound
37         local swap = chest_open_info.swap
38
39         default.chest.open_chests[pn] = nil
40         for k, v in pairs(default.chest.open_chests) do
41                 if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
42                         return true
43                 end
44         end
45
46         local node = minetest.get_node(pos)
47         minetest.after(0.2, minetest.swap_node, pos, { name = swap,
48                         param2 = node.param2 })
49         minetest.sound_play(sound, {gain = 0.3, pos = pos,
50                 max_hear_distance = 10}, true)
51 end
52
53 default.chest.open_chests = {}
54
55 minetest.register_on_player_receive_fields(function(player, formname, fields)
56         if formname ~= "default:chest" then
57                 return
58         end
59         if not player or not fields.quit then
60                 return
61         end
62         local pn = player:get_player_name()
63
64         if not default.chest.open_chests[pn] then
65                 return
66         end
67
68         default.chest.chest_lid_close(pn)
69         return true
70 end)
71
72 minetest.register_on_leaveplayer(function(player)
73         local pn = player:get_player_name()
74         if default.chest.open_chests[pn] then
75                 default.chest.chest_lid_close(pn)
76         end
77 end)
78
79 function default.chest.register_chest(prefixed_name, d)
80         local name = prefixed_name:sub(1,1) == ':' and prefixed_name:sub(2,-1) or prefixed_name
81         local def = table.copy(d)
82         def.drawtype = "mesh"
83         def.visual = "mesh"
84         def.paramtype = "light"
85         def.paramtype2 = "facedir"
86         def.legacy_facedir_simple = true
87         def.is_ground_content = false
88
89         if def.protected then
90                 def.on_construct = function(pos)
91                         local meta = minetest.get_meta(pos)
92                         meta:set_string("infotext", S("Locked Chest"))
93                         meta:set_string("owner", "")
94                         local inv = meta:get_inventory()
95                         inv:set_size("main", 8*4)
96                 end
97                 def.after_place_node = function(pos, placer)
98                         local meta = minetest.get_meta(pos)
99                         meta:set_string("owner", placer:get_player_name() or "")
100                         meta:set_string("infotext", S("Locked Chest (owned by @1)", meta:get_string("owner")))
101                 end
102                 def.can_dig = function(pos,player)
103                         local meta = minetest.get_meta(pos);
104                         local inv = meta:get_inventory()
105                         return inv:is_empty("main") and
106                                         default.can_interact_with_node(player, pos)
107                 end
108                 def.allow_metadata_inventory_move = function(pos, from_list, from_index,
109                                 to_list, to_index, count, player)
110                         if not default.can_interact_with_node(player, pos) then
111                                 return 0
112                         end
113                         return count
114                 end
115                 def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
116                         if not default.can_interact_with_node(player, pos) then
117                                 return 0
118                         end
119                         return stack:get_count()
120                 end
121                 def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
122                         if not default.can_interact_with_node(player, pos) then
123                                 return 0
124                         end
125                         return stack:get_count()
126                 end
127                 def.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
128                         if not default.can_interact_with_node(clicker, pos) then
129                                 return itemstack
130                         end
131
132                         minetest.sound_play(def.sound_open, {gain = 0.3,
133                                         pos = pos, max_hear_distance = 10}, true)
134                         if not default.chest.chest_lid_obstructed(pos) then
135                                 minetest.swap_node(pos,
136                                                 { name = name .. "_open",
137                                                 param2 = node.param2 })
138                         end
139                         minetest.after(0.2, minetest.show_formspec,
140                                         clicker:get_player_name(),
141                                         "default:chest", default.chest.get_chest_formspec(pos))
142                         default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
143                                         sound = def.sound_close, swap = name }
144                 end
145                 def.on_blast = function() end
146                 def.on_key_use = function(pos, player)
147                         local secret = minetest.get_meta(pos):get_string("key_lock_secret")
148                         local itemstack = player:get_wielded_item()
149                         local key_meta = itemstack:get_meta()
150
151                         if itemstack:get_metadata() == "" then
152                                 return
153                         end
154
155                         if key_meta:get_string("secret") == "" then
156                                 key_meta:set_string("secret", minetest.parse_json(itemstack:get_metadata()).secret)
157                                 itemstack:set_metadata("")
158                         end
159
160                         if secret ~= key_meta:get_string("secret") then
161                                 return
162                         end
163
164                         minetest.show_formspec(
165                                 player:get_player_name(),
166                                 "default:chest_locked",
167                                 default.chest.get_chest_formspec(pos)
168                         )
169                 end
170                 def.on_skeleton_key_use = function(pos, player, newsecret)
171                         local meta = minetest.get_meta(pos)
172                         local owner = meta:get_string("owner")
173                         local pn = player:get_player_name()
174
175                         -- verify placer is owner of lockable chest
176                         if owner ~= pn then
177                                 minetest.record_protection_violation(pos, pn)
178                                 minetest.chat_send_player(pn, S("You do not own this chest."))
179                                 return nil
180                         end
181
182                         local secret = meta:get_string("key_lock_secret")
183                         if secret == "" then
184                                 secret = newsecret
185                                 meta:set_string("key_lock_secret", secret)
186                         end
187
188                         return secret, S("a locked chest"), owner
189                 end
190         else
191                 def.on_construct = function(pos)
192                         local meta = minetest.get_meta(pos)
193                         meta:set_string("infotext", S("Chest"))
194                         local inv = meta:get_inventory()
195                         inv:set_size("main", 8*4)
196                 end
197                 def.can_dig = function(pos,player)
198                         local meta = minetest.get_meta(pos);
199                         local inv = meta:get_inventory()
200                         return inv:is_empty("main")
201                 end
202                 def.on_rightclick = function(pos, node, clicker)
203                         minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
204                                         max_hear_distance = 10}, true)
205                         if not default.chest.chest_lid_obstructed(pos) then
206                                 minetest.swap_node(pos, {
207                                                 name = name .. "_open",
208                                                 param2 = node.param2 })
209                         end
210                         minetest.after(0.2, minetest.show_formspec,
211                                         clicker:get_player_name(),
212                                         "default:chest", default.chest.get_chest_formspec(pos))
213                         default.chest.open_chests[clicker:get_player_name()] = { pos = pos,
214                                         sound = def.sound_close, swap = name }
215                 end
216                 def.on_blast = function(pos)
217                         local drops = {}
218                         default.get_inventory_drops(pos, "main", drops)
219                         drops[#drops+1] = name
220                         minetest.remove_node(pos)
221                         return drops
222                 end
223         end
224
225         def.on_metadata_inventory_move = function(pos, from_list, from_index,
226                         to_list, to_index, count, player)
227                 minetest.log("action", player:get_player_name() ..
228                         " moves stuff in chest at " .. minetest.pos_to_string(pos))
229         end
230         def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
231                 minetest.log("action", player:get_player_name() ..
232                         " moves " .. stack:get_name() ..
233                         " to chest at " .. minetest.pos_to_string(pos))
234         end
235         def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
236                 minetest.log("action", player:get_player_name() ..
237                         " takes " .. stack:get_name() ..
238                         " from chest at " .. minetest.pos_to_string(pos))
239         end
240
241         local def_opened = table.copy(def)
242         local def_closed = table.copy(def)
243
244         def_opened.mesh = "chest_open.obj"
245         for i = 1, #def_opened.tiles do
246                 if type(def_opened.tiles[i]) == "string" then
247                         def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
248                 elseif def_opened.tiles[i].backface_culling == nil then
249                         def_opened.tiles[i].backface_culling = true
250                 end
251         end
252         def_opened.drop = name
253         def_opened.groups.not_in_creative_inventory = 1
254         def_opened.selection_box = {
255                 type = "fixed",
256                 fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
257         }
258         def_opened.can_dig = function()
259                 return false
260         end
261         def_opened.on_blast = function() end
262
263         def_closed.mesh = nil
264         def_closed.drawtype = nil
265         def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
266         def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
267         def_closed.tiles[3] = def.tiles[3].."^[transformFX"
268
269         minetest.register_node(prefixed_name, def_closed)
270         minetest.register_node(prefixed_name .. "_open", def_opened)
271
272         -- convert old chests to this new variant
273         if name == "default:chest" or name == "default:chest_locked" then
274                 minetest.register_lbm({
275                         label = "update chests to opening chests",
276                         name = "default:upgrade_" .. name:sub(9,-1) .. "_v2",
277                         nodenames = {name},
278                         action = function(pos, node)
279                                 local meta = minetest.get_meta(pos)
280                                 meta:set_string("formspec", nil)
281                                 local inv = meta:get_inventory()
282                                 local list = inv:get_list("default:chest")
283                                 if list then
284                                         inv:set_size("main", 8*4)
285                                         inv:set_list("main", list)
286                                         inv:set_list("default:chest", nil)
287                                 end
288                         end
289                 })
290         end
291 end
292
293 default.chest.register_chest("default:chest", {
294         description = S("Chest"),
295         tiles = {
296                 "default_chest_top.png",
297                 "default_chest_top.png",
298                 "default_chest_side.png",
299                 "default_chest_side.png",
300                 "default_chest_front.png",
301                 "default_chest_inside.png"
302         },
303         sounds = default.node_sound_wood_defaults(),
304         sound_open = "default_chest_open",
305         sound_close = "default_chest_close",
306         groups = {choppy = 2, oddly_breakable_by_hand = 2},
307 })
308
309 default.chest.register_chest("default:chest_locked", {
310         description = S("Locked Chest"),
311         tiles = {
312                 "default_chest_top.png",
313                 "default_chest_top.png",
314                 "default_chest_side.png",
315                 "default_chest_side.png",
316                 "default_chest_lock.png",
317                 "default_chest_inside.png"
318         },
319         sounds = default.node_sound_wood_defaults(),
320         sound_open = "default_chest_open",
321         sound_close = "default_chest_close",
322         groups = {choppy = 2, oddly_breakable_by_hand = 2},
323         protected = true,
324 })
325
326 minetest.register_craft({
327         output = "default:chest",
328         recipe = {
329                 {"group:wood", "group:wood", "group:wood"},
330                 {"group:wood", "", "group:wood"},
331                 {"group:wood", "group:wood", "group:wood"},
332         }
333 })
334
335 minetest.register_craft({
336         output = "default:chest_locked",
337         recipe = {
338                 {"group:wood", "group:wood", "group:wood"},
339                 {"group:wood", "default:steel_ingot", "group:wood"},
340                 {"group:wood", "group:wood", "group:wood"},
341         }
342 })
343
344 minetest.register_craft( {
345         type = "shapeless",
346         output = "default:chest_locked",
347         recipe = {"default:chest", "default:steel_ingot"},
348 })
349
350 minetest.register_craft({
351         type = "fuel",
352         recipe = "default:chest",
353         burntime = 30,
354 })
355
356 minetest.register_craft({
357         type = "fuel",
358         recipe = "default:chest_locked",
359         burntime = 30,
360 })