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