Add oldnode parameter to minetest.register_on_placenode callback
[oweals/minetest.git] / builtin / item.lua
1 -- Minetest: builtin/item.lua
2
3 --
4 -- Item definition helpers
5 --
6
7 function minetest.inventorycube(img1, img2, img3)
8         img2 = img2 or img1
9         img3 = img3 or img1
10         return "[inventorycube"
11                         .. "{" .. img1:gsub("%^", "&")
12                         .. "{" .. img2:gsub("%^", "&")
13                         .. "{" .. img3:gsub("%^", "&")
14 end
15
16 function minetest.get_pointed_thing_position(pointed_thing, above)
17         if pointed_thing.type == "node" then
18                 if above then
19                         -- The position where a node would be placed
20                         return pointed_thing.above
21                 else
22                         -- The position where a node would be dug
23                         return pointed_thing.under
24                 end
25         elseif pointed_thing.type == "object" then
26                 obj = pointed_thing.ref
27                 if obj ~= nil then
28                         return obj:getpos()
29                 else
30                         return nil
31                 end
32         else
33                 return nil
34         end
35 end
36
37 function minetest.dir_to_facedir(dir)
38         if math.abs(dir.x) > math.abs(dir.z) then
39                 if dir.x < 0 then
40                         return 3
41                 else
42                         return 1
43                 end
44         else
45                 if dir.z < 0 then
46                         return 2
47                 else
48                         return 0
49                 end
50         end
51 end
52
53 function minetest.dir_to_wallmounted(dir)
54         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
55                 if dir.y < 0 then
56                         return 1
57                 else
58                         return 0
59                 end
60         elseif math.abs(dir.x) > math.abs(dir.z) then
61                 if dir.x < 0 then
62                         return 3
63                 else
64                         return 2
65                 end
66         else
67                 if dir.z < 0 then
68                         return 5
69                 else
70                         return 4
71                 end
72         end
73 end
74
75 function minetest.get_node_drops(nodename, toolname)
76         local drop = ItemStack({name=nodename}):get_definition().drop
77         if drop == nil then
78                 -- default drop
79                 return {ItemStack({name=nodename})}
80         elseif type(drop) == "string" then
81                 -- itemstring drop
82                 return {ItemStack(drop)}
83         elseif drop.items == nil then
84                 -- drop = {} to disable default drop
85                 return {}
86         end
87
88         -- Extended drop table
89         local got_items = {}
90         local got_count = 0
91         local _, item, tool
92         for _, item in ipairs(drop.items) do
93                 local good_rarity = true
94                 local good_tool = true
95                 if item.rarity ~= nil then
96                         good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
97                 end
98                 if item.tools ~= nil then
99                         good_tool = false
100                         for _, tool in ipairs(item.tools) do
101                                 if tool:sub(1, 1) == '~' then
102                                         good_tool = toolname:find(tool:sub(2)) ~= nil
103                                 else
104                                         good_tool = toolname == tool
105                                 end
106                                 if good_tool then
107                                         break
108                                 end
109                         end
110                 end
111                 if good_rarity and good_tool then
112                         got_count = got_count + 1
113                         for _, add_item in ipairs(item.items) do
114                                 got_items[#got_items+1] = add_item
115                         end
116                         if drop.max_items ~= nil and got_count == drop.max_items then
117                                 break
118                         end
119                 end
120         end
121         return got_items
122 end
123
124 function minetest.item_place_node(itemstack, placer, pointed_thing)
125         local item = itemstack:peek_item()
126         local def = itemstack:get_definition()
127         if def.type == "node" and pointed_thing.type == "node" then
128                 local under = pointed_thing.under
129                 local oldnode_under = minetest.env:get_node(under)
130                 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
131                 olddef_under = olddef_under or minetest.nodedef_default
132                 local above = pointed_thing.above
133                 local oldnode_above = minetest.env:get_node(above)
134                 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
135                 olddef_above = olddef_above or minetest.nodedef_default
136
137                 if not olddef_above.buildable_to and not olddef_under.buildable_to then
138                         minetest.log("info", placer:get_player_name() .. " tried to place"
139                                 .. " node in invalid position " .. minetest.pos_to_string(above)
140                                 .. ", replacing " .. oldnode_above.name)
141                         return
142                 end
143
144                 -- Place above pointed node
145                 local place_to = {x = above.x, y = above.y, z = above.z}
146
147                 -- If node under is buildable_to, place into it instead (eg. snow)
148                 if olddef_under.buildable_to then
149                         minetest.log("info", "node under is buildable to")
150                         place_to = {x = under.x, y = under.y, z = under.z}
151                 end
152
153                 minetest.log("action", placer:get_player_name() .. " places node "
154                         .. def.name .. " at " .. minetest.pos_to_string(place_to))
155                 
156                 local oldnode = minetest.env:get_node(place_to)
157                 local newnode = {name = def.name, param1 = 0, param2 = 0}
158
159                 -- Calculate direction for wall mounted stuff like torches and signs
160                 if def.paramtype2 == 'wallmounted' then
161                         local dir = {
162                                 x = under.x - above.x,
163                                 y = under.y - above.y,
164                                 z = under.z - above.z
165                         }
166                         newnode.param2 = minetest.dir_to_wallmounted(dir)
167                 -- Calculate the direction for furnaces and chests and stuff
168                 elseif def.paramtype2 == 'facedir' then
169                         local placer_pos = placer:getpos()
170                         if placer_pos then
171                                 local dir = {
172                                         x = above.x - placer_pos.x,
173                                         y = above.y - placer_pos.y,
174                                         z = above.z - placer_pos.z
175                                 }
176                                 newnode.param2 = minetest.dir_to_facedir(dir)
177                                 minetest.log("action", "facedir: " .. newnode.param2)
178                         end
179                 end
180
181                 -- Add node and update
182                 minetest.env:add_node(place_to, newnode)
183
184                 -- Run callback
185                 if def.after_place_node then
186                         def.after_place_node(place_to, placer)
187                 end
188
189                 -- Run script hook (deprecated)
190                 local _, callback
191                 for _, callback in ipairs(minetest.registered_on_placenodes) do
192                         callback(place_to, newnode, placer, oldnode)
193                 end
194
195                 itemstack:take_item()
196         end
197         return itemstack
198 end
199
200 function minetest.item_place_object(itemstack, placer, pointed_thing)
201         local pos = minetest.get_pointed_thing_position(pointed_thing, true)
202         if pos ~= nil then
203                 local item = itemstack:take_item()
204                 minetest.env:add_item(pos, item)
205         end
206         return itemstack
207 end
208
209 function minetest.item_place(itemstack, placer, pointed_thing)
210         if itemstack:get_definition().type == "node" then
211                 return minetest.item_place_node(itemstack, placer, pointed_thing)
212         else
213                 return minetest.item_place_object(itemstack, placer, pointed_thing)
214         end
215 end
216
217 function minetest.item_drop(itemstack, dropper, pos)
218         if dropper.get_player_name then
219                 local v = dropper:get_look_dir()
220                 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
221                 local obj = minetest.env:add_item(p, itemstack)
222                 v.x = v.x*2
223                 v.y = v.y*2 + 1
224                 v.z = v.z*2
225                 obj:setvelocity(v)
226         else
227                 minetest.env:add_item(pos, itemstack)
228         end
229         return ""
230 end
231
232 function minetest.item_eat(hp_change, replace_with_item)
233         return function(itemstack, user, pointed_thing)  -- closure
234                 if itemstack:take_item() ~= nil then
235                         user:set_hp(user:get_hp() + hp_change)
236                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
237                 end
238                 return itemstack
239         end
240 end
241
242 function minetest.node_punch(pos, node, puncher)
243         -- Run script hook
244         local _, callback
245         for _, callback in ipairs(minetest.registered_on_punchnodes) do
246                 callback(pos, node, puncher)
247         end
248
249 end
250
251 function minetest.node_dig(pos, node, digger)
252         minetest.debug("node_dig")
253
254         local def = ItemStack({name=node.name}):get_definition()
255         if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
256                 minetest.debug("not diggable")
257                 minetest.log("info", digger:get_player_name() .. " tried to dig "
258                         .. node.name .. " which is not diggable "
259                         .. minetest.pos_to_string(pos))
260                 return
261         end
262
263         minetest.log('action', digger:get_player_name() .. " digs "
264                 .. node.name .. " at " .. minetest.pos_to_string(pos))
265
266         if not minetest.setting_getbool("creative_mode") then
267                 local wielded = digger:get_wielded_item()
268                 local drops = minetest.get_node_drops(node.name, wielded:get_name())
269
270                 -- Wear out tool
271                 tp = wielded:get_tool_capabilities()
272                 dp = minetest.get_dig_params(def.groups, tp)
273                 wielded:add_wear(dp.wear)
274                 digger:set_wielded_item(wielded)
275
276                 -- Add dropped items to object's inventory
277                 if digger:get_inventory() then
278                         local _, dropped_item
279                         for _, dropped_item in ipairs(drops) do
280                                 digger:get_inventory():add_item("main", dropped_item)
281                         end
282                 end
283         end
284         
285         local oldnode = nil
286         local oldmetadata = nil
287         if def.after_dig_node then
288                 oldnode = node;
289                 oldmetadata = minetest.env:get_meta(pos):to_table()
290         end
291
292         -- Remove node and update
293         minetest.env:remove_node(pos)
294         
295         -- Run callback
296         if def.after_dig_node then
297                 def.after_dig_node(pos, oldnode, oldmetadata, digger)
298         end
299
300         -- Run script hook (deprecated)
301         local _, callback
302         for _, callback in ipairs(minetest.registered_on_dignodes) do
303                 callback(pos, node, digger)
304         end
305 end
306
307 function minetest.node_metadata_inventory_move_allow_all(pos, from_list,
308                 from_index, to_list, to_index, count, player)
309         minetest.log("verbose", "node_metadata_inventory_move_allow_all")
310         local meta = minetest.env:get_meta(pos)
311         local inv = meta:get_inventory()
312
313         local from_stack = inv:get_stack(from_list, from_index)
314         local taken_items = from_stack:take_item(count)
315         inv:set_stack(from_list, from_index, from_stack)
316
317         local to_stack = inv:get_stack(to_list, to_index)
318         to_stack:add_item(taken_items)
319         inv:set_stack(to_list, to_index, to_stack)
320 end
321
322 function minetest.node_metadata_inventory_offer_allow_all(pos, listname, index, stack, player)
323         minetest.log("verbose", "node_metadata_inventory_offer_allow_all")
324         local meta = minetest.env:get_meta(pos)
325         local inv = meta:get_inventory()
326         local the_stack = inv:get_stack(listname, index)
327         the_stack:add_item(stack)
328         inv:set_stack(listname, index, the_stack)
329         return ItemStack("")
330 end
331
332 function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, count, player)
333         minetest.log("verbose", "node_metadata_inventory_take_allow_all")
334         local meta = minetest.env:get_meta(pos)
335         local inv = meta:get_inventory()
336         local the_stack = inv:get_stack(listname, index)
337         local taken_items = the_stack:take_item(count)
338         inv:set_stack(listname, index, the_stack)
339         return taken_items
340 end
341
342 -- This is used to allow mods to redefine minetest.item_place and so on
343 -- NOTE: This is not the preferred way. Preferred way is to provide enough
344 --       callbacks to not require redefining global functions. -celeron55
345 local function redef_wrapper(table, name)
346         return function(...)
347                 return table[name](...)
348         end
349 end
350
351 --
352 -- Item definition defaults
353 --
354
355 minetest.nodedef_default = {
356         -- Item properties
357         type="node",
358         -- name intentionally not defined here
359         description = "",
360         groups = {},
361         inventory_image = "",
362         wield_image = "",
363         wield_scale = {x=1,y=1,z=1},
364         stack_max = 99,
365         usable = false,
366         liquids_pointable = false,
367         tool_capabilities = nil,
368         node_placement_prediction = nil,
369
370         -- Interaction callbacks
371         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
372         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
373         on_use = nil,
374         can_dig = nil,
375
376         on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
377         on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
378
379         on_receive_fields = nil,
380         
381         on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
382         on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
383         on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
384
385         -- Node properties
386         drawtype = "normal",
387         visual_scale = 1.0,
388         -- Don't define these because otherwise the old tile_images and
389         -- special_materials wouldn't be read
390         --tiles ={""},
391         --special_tiles = {
392         --      {name="", backface_culling=true},
393         --      {name="", backface_culling=true},
394         --},
395         alpha = 255,
396         post_effect_color = {a=0, r=0, g=0, b=0},
397         paramtype = "none",
398         paramtype2 = "none",
399         is_ground_content = false,
400         sunlight_propagates = false,
401         walkable = true,
402         pointable = true,
403         diggable = true,
404         climbable = false,
405         buildable_to = false,
406         liquidtype = "none",
407         liquid_alternative_flowing = "",
408         liquid_alternative_source = "",
409         liquid_viscosity = 0,
410         light_source = 0,
411         damage_per_second = 0,
412         selection_box = {type="regular"},
413         legacy_facedir_simple = false,
414         legacy_wallmounted = false,
415 }
416
417 minetest.craftitemdef_default = {
418         type="craft",
419         -- name intentionally not defined here
420         description = "",
421         groups = {},
422         inventory_image = "",
423         wield_image = "",
424         wield_scale = {x=1,y=1,z=1},
425         stack_max = 99,
426         liquids_pointable = false,
427         tool_capabilities = nil,
428
429         -- Interaction callbacks
430         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
431         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
432         on_use = nil,
433 }
434
435 minetest.tooldef_default = {
436         type="tool",
437         -- name intentionally not defined here
438         description = "",
439         groups = {},
440         inventory_image = "",
441         wield_image = "",
442         wield_scale = {x=1,y=1,z=1},
443         stack_max = 1,
444         liquids_pointable = false,
445         tool_capabilities = nil,
446
447         -- Interaction callbacks
448         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
449         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
450         on_use = nil,
451 }
452
453 minetest.noneitemdef_default = {  -- This is used for the hand and unknown items
454         type="none",
455         -- name intentionally not defined here
456         description = "",
457         groups = {},
458         inventory_image = "",
459         wield_image = "",
460         wield_scale = {x=1,y=1,z=1},
461         stack_max = 99,
462         liquids_pointable = false,
463         tool_capabilities = nil,
464
465         -- Interaction callbacks
466         on_place = nil,
467         on_drop = nil,
468         on_use = nil,
469 }
470