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