Return an ItemStack in minetest.item_place() if nodes' on rightclick doesnt return it
[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 {nodename}
80         elseif type(drop) == "string" then
81                 -- itemstring drop
82                 return {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" or pointed_thing.type ~= "node" then
128                 return itemstack
129         end
130
131         local under = pointed_thing.under
132         local oldnode_under = minetest.env:get_node_or_nil(under)
133         local above = pointed_thing.above
134         local oldnode_above = minetest.env:get_node_or_nil(above)
135
136         if not oldnode_under or not oldnode_above then
137                 minetest.log("info", placer:get_player_name() .. " tried to place"
138                         .. " node in unloaded position " .. minetest.pos_to_string(above))
139                 return itemstack
140         end
141
142         local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
143         olddef_under = olddef_under or minetest.nodedef_default
144         local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
145         olddef_above = olddef_above or minetest.nodedef_default
146
147         if not olddef_above.buildable_to and not olddef_under.buildable_to then
148                 minetest.log("info", placer:get_player_name() .. " tried to place"
149                         .. " node in invalid position " .. minetest.pos_to_string(above)
150                         .. ", replacing " .. oldnode_above.name)
151                 return itemstack
152         end
153
154         -- Place above pointed node
155         local place_to = {x = above.x, y = above.y, z = above.z}
156
157         -- If node under is buildable_to, place into it instead (eg. snow)
158         if olddef_under.buildable_to then
159                 minetest.log("info", "node under is buildable to")
160                 place_to = {x = under.x, y = under.y, z = under.z}
161         end
162
163         minetest.log("action", placer:get_player_name() .. " places node "
164                 .. def.name .. " at " .. minetest.pos_to_string(place_to))
165         
166         local oldnode = minetest.env:get_node(place_to)
167         local newnode = {name = def.name, param1 = 0, param2 = 0}
168
169         -- Calculate direction for wall mounted stuff like torches and signs
170         if def.paramtype2 == 'wallmounted' then
171                 local dir = {
172                         x = under.x - above.x,
173                         y = under.y - above.y,
174                         z = under.z - above.z
175                 }
176                 newnode.param2 = minetest.dir_to_wallmounted(dir)
177         -- Calculate the direction for furnaces and chests and stuff
178         elseif def.paramtype2 == 'facedir' then
179                 local placer_pos = placer:getpos()
180                 if placer_pos then
181                         local dir = {
182                                 x = above.x - placer_pos.x,
183                                 y = above.y - placer_pos.y,
184                                 z = above.z - placer_pos.z
185                         }
186                         newnode.param2 = minetest.dir_to_facedir(dir)
187                         minetest.log("action", "facedir: " .. newnode.param2)
188                 end
189         end
190
191         -- Check if the node is attached and if it can be placed there
192         if minetest.get_item_group(def.name, "attached_node") ~= 0 and
193                 not check_attached_node(place_to, newnode) then
194                 minetest.log("action", "attached node " .. def.name ..
195                         " can not be placed at " .. minetest.pos_to_string(place_to))
196                 return itemstack
197         end
198
199         -- Add node and update
200         minetest.env:add_node(place_to, newnode)
201
202         local take_item = true
203
204         -- Run callback
205         if def.after_place_node then
206                 -- Copy place_to because callback can modify it
207                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
208                 if def.after_place_node(place_to_copy, placer, itemstack) then
209                         take_item = false
210                 end
211         end
212
213         -- Run script hook
214         local _, callback
215         for _, callback in ipairs(minetest.registered_on_placenodes) do
216                 -- Copy pos and node because callback can modify them
217                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
218                 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
219                 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
220                 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack) then
221                         take_item = false
222                 end
223         end
224
225         if take_item then
226                 itemstack:take_item()
227         end
228         return itemstack
229 end
230
231 function minetest.item_place_object(itemstack, placer, pointed_thing)
232         local pos = minetest.get_pointed_thing_position(pointed_thing, true)
233         if pos ~= nil then
234                 local item = itemstack:take_item()
235                 minetest.env:add_item(pos, item)
236         end
237         return itemstack
238 end
239
240 function minetest.item_place(itemstack, placer, pointed_thing)
241         -- Call on_rightclick if the pointed node defines it
242         if pointed_thing.type == "node" and placer and
243                         not placer:get_player_control().sneak then
244                 local n = minetest.env:get_node(pointed_thing.under)
245                 local nn = n.name
246                 if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
247                         return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack
248                 end
249         end
250
251         if itemstack:get_definition().type == "node" then
252                 return minetest.item_place_node(itemstack, placer, pointed_thing)
253         end
254         return itemstack
255 end
256
257 function minetest.item_drop(itemstack, dropper, pos)
258         if dropper.get_player_name then
259                 local v = dropper:get_look_dir()
260                 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
261                 local obj = minetest.env:add_item(p, itemstack)
262                 if obj then
263                         v.x = v.x*2
264                         v.y = v.y*2 + 1
265                         v.z = v.z*2
266                         obj:setvelocity(v)
267                 end
268         else
269                 minetest.env:add_item(pos, itemstack)
270         end
271         return ItemStack("")
272 end
273
274 function minetest.item_eat(hp_change, replace_with_item)
275         return function(itemstack, user, pointed_thing)  -- closure
276                 if itemstack:take_item() ~= nil then
277                         user:set_hp(user:get_hp() + hp_change)
278                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
279                 end
280                 return itemstack
281         end
282 end
283
284 function minetest.node_punch(pos, node, puncher)
285         -- Run script hook
286         local _, callback
287         for _, callback in ipairs(minetest.registered_on_punchnodes) do
288                 -- Copy pos and node because callback can modify them
289                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
290                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
291                 callback(pos_copy, node_copy, puncher)
292         end
293 end
294
295 function minetest.handle_node_drops(pos, drops, digger)
296         -- Add dropped items to object's inventory
297         if digger:get_inventory() then
298                 local _, dropped_item
299                 for _, dropped_item in ipairs(drops) do
300                         local left = digger:get_inventory():add_item("main", dropped_item)
301                         if not left:is_empty() then
302                                 local p = {
303                                         x = pos.x + math.random()/2-0.25,
304                                         y = pos.y + math.random()/2-0.25,
305                                         z = pos.z + math.random()/2-0.25,
306                                 }
307                                 minetest.env:add_item(p, left)
308                         end
309                 end
310         end
311 end
312
313 function minetest.node_dig(pos, node, digger)
314         minetest.debug("node_dig")
315
316         local def = ItemStack({name=node.name}):get_definition()
317         -- Check if def ~= 0 because we always want to be able to remove unknown nodes
318         if #def ~= 0 and not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
319                 minetest.debug("not diggable")
320                 minetest.log("info", digger:get_player_name() .. " tried to dig "
321                         .. node.name .. " which is not diggable "
322                         .. minetest.pos_to_string(pos))
323                 return
324         end
325
326         minetest.log('action', digger:get_player_name() .. " digs "
327                 .. node.name .. " at " .. minetest.pos_to_string(pos))
328
329         local wielded = digger:get_wielded_item()
330         local drops = minetest.get_node_drops(node.name, wielded:get_name())
331
332         -- Wear out tool
333         local tp = wielded:get_tool_capabilities()
334         local dp = minetest.get_dig_params(def.groups, tp)
335         wielded:add_wear(dp.wear)
336         digger:set_wielded_item(wielded)
337         
338         -- Handle drops
339         minetest.handle_node_drops(pos, drops, digger)
340
341         local oldmetadata = nil
342         if def.after_dig_node then
343                 oldmetadata = minetest.env:get_meta(pos):to_table()
344         end
345
346         -- Remove node and update
347         minetest.env:remove_node(pos)
348         
349         -- Run callback
350         if def.after_dig_node then
351                 -- Copy pos and node because callback can modify them
352                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
353                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
354                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
355         end
356
357         -- Run script hook
358         local _, callback
359         for _, callback in ipairs(minetest.registered_on_dignodes) do
360                 -- Copy pos and node because callback can modify them
361                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
362                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
363                 callback(pos_copy, node_copy, digger)
364         end
365 end
366
367 -- This is used to allow mods to redefine minetest.item_place and so on
368 -- NOTE: This is not the preferred way. Preferred way is to provide enough
369 --       callbacks to not require redefining global functions. -celeron55
370 local function redef_wrapper(table, name)
371         return function(...)
372                 return table[name](...)
373         end
374 end
375
376 --
377 -- Item definition defaults
378 --
379
380 minetest.nodedef_default = {
381         -- Item properties
382         type="node",
383         -- name intentionally not defined here
384         description = "",
385         groups = {},
386         inventory_image = "",
387         wield_image = "",
388         wield_scale = {x=1,y=1,z=1},
389         stack_max = 99,
390         usable = false,
391         liquids_pointable = false,
392         tool_capabilities = nil,
393         node_placement_prediction = nil,
394
395         -- Interaction callbacks
396         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
397         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
398         on_use = nil,
399         can_dig = nil,
400
401         on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
402         on_rightclick = nil,
403         on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
404
405         on_receive_fields = nil,
406         
407         on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
408         on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
409         on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
410
411         -- Node properties
412         drawtype = "normal",
413         visual_scale = 1.0,
414         -- Don't define these because otherwise the old tile_images and
415         -- special_materials wouldn't be read
416         --tiles ={""},
417         --special_tiles = {
418         --      {name="", backface_culling=true},
419         --      {name="", backface_culling=true},
420         --},
421         alpha = 255,
422         post_effect_color = {a=0, r=0, g=0, b=0},
423         paramtype = "none",
424         paramtype2 = "none",
425         is_ground_content = false,
426         sunlight_propagates = false,
427         walkable = true,
428         pointable = true,
429         diggable = true,
430         climbable = false,
431         buildable_to = false,
432         liquidtype = "none",
433         liquid_alternative_flowing = "",
434         liquid_alternative_source = "",
435         liquid_viscosity = 0,
436         light_source = 0,
437         damage_per_second = 0,
438         selection_box = {type="regular"},
439         legacy_facedir_simple = false,
440         legacy_wallmounted = false,
441 }
442
443 minetest.craftitemdef_default = {
444         type="craft",
445         -- name intentionally not defined here
446         description = "",
447         groups = {},
448         inventory_image = "",
449         wield_image = "",
450         wield_scale = {x=1,y=1,z=1},
451         stack_max = 99,
452         liquids_pointable = false,
453         tool_capabilities = nil,
454
455         -- Interaction callbacks
456         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
457         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
458         on_use = nil,
459 }
460
461 minetest.tooldef_default = {
462         type="tool",
463         -- name intentionally not defined here
464         description = "",
465         groups = {},
466         inventory_image = "",
467         wield_image = "",
468         wield_scale = {x=1,y=1,z=1},
469         stack_max = 1,
470         liquids_pointable = false,
471         tool_capabilities = nil,
472
473         -- Interaction callbacks
474         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
475         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
476         on_use = nil,
477 }
478
479 minetest.noneitemdef_default = {  -- This is used for the hand and unknown items
480         type="none",
481         -- name intentionally not defined here
482         description = "",
483         groups = {},
484         inventory_image = "",
485         wield_image = "",
486         wield_scale = {x=1,y=1,z=1},
487         stack_max = 99,
488         liquids_pointable = false,
489         tool_capabilities = nil,
490
491         -- Interaction callbacks
492         on_place = redef_wrapper(minetest, 'item_place'),
493         on_drop = nil,
494         on_use = nil,
495 }
496