]> git.lizzy.rs Git - minetest.git/blob - builtin/item.lua
Add pointed_thing to minetest.register_on_placenode
[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, is6d)
38         --account for y if requested
39         if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
40
41                 --from above
42                 if dir.y < 0 then
43                         if math.abs(dir.x) > math.abs(dir.z) then
44                                 if dir.x < 0 then
45                                         return 19
46                                 else
47                                         return 13
48                                 end
49                         else
50                                 if dir.z < 0 then
51                                         return 10
52                                 else
53                                         return 4
54                                 end
55                         end
56
57                 --from below
58                 else
59                         if math.abs(dir.x) > math.abs(dir.z) then
60                                 if dir.x < 0 then
61                                         return 15
62                                 else
63                                         return 17
64                                 end
65                         else
66                                 if dir.z < 0 then
67                                         return 6
68                                 else
69                                         return 8
70                                 end
71                         end
72                 end
73
74         --otherwise, place horizontally
75         elseif math.abs(dir.x) > math.abs(dir.z) then
76                 if dir.x < 0 then
77                         return 3
78                 else
79                         return 1
80                 end
81         else
82                 if dir.z < 0 then
83                         return 2
84                 else
85                         return 0
86                 end
87         end
88 end
89
90 function minetest.facedir_to_dir(facedir)
91         --a table of possible dirs
92         return ({{x=0, y=0, z=1},
93                                         {x=1, y=0, z=0},
94                                         {x=0, y=0, z=-1},
95                                         {x=-1, y=0, z=0},
96                                         {x=0, y=-1, z=0},
97                                         {x=0, y=1, z=0}})
98
99                                         --indexed into by a table of correlating facedirs
100                                         [({[0]=1, 2, 3, 4, 
101                                                 5, 2, 6, 4,
102                                                 6, 2, 5, 4,
103                                                 1, 5, 3, 6,
104                                                 1, 6, 3, 5,
105                                                 1, 4, 3, 2})
106
107                                                 --indexed into by the facedir in question
108                                                 [facedir]]
109 end
110
111 function minetest.dir_to_wallmounted(dir)
112         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
113                 if dir.y < 0 then
114                         return 1
115                 else
116                         return 0
117                 end
118         elseif math.abs(dir.x) > math.abs(dir.z) then
119                 if dir.x < 0 then
120                         return 3
121                 else
122                         return 2
123                 end
124         else
125                 if dir.z < 0 then
126                         return 5
127                 else
128                         return 4
129                 end
130         end
131 end
132
133 function minetest.get_node_drops(nodename, toolname)
134         local drop = ItemStack({name=nodename}):get_definition().drop
135         if drop == nil then
136                 -- default drop
137                 return {nodename}
138         elseif type(drop) == "string" then
139                 -- itemstring drop
140                 return {drop}
141         elseif drop.items == nil then
142                 -- drop = {} to disable default drop
143                 return {}
144         end
145
146         -- Extended drop table
147         local got_items = {}
148         local got_count = 0
149         local _, item, tool
150         for _, item in ipairs(drop.items) do
151                 local good_rarity = true
152                 local good_tool = true
153                 if item.rarity ~= nil then
154                         good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
155                 end
156                 if item.tools ~= nil then
157                         good_tool = false
158                         for _, tool in ipairs(item.tools) do
159                                 if tool:sub(1, 1) == '~' then
160                                         good_tool = toolname:find(tool:sub(2)) ~= nil
161                                 else
162                                         good_tool = toolname == tool
163                                 end
164                                 if good_tool then
165                                         break
166                                 end
167                         end
168                 end
169                 if good_rarity and good_tool then
170                         got_count = got_count + 1
171                         for _, add_item in ipairs(item.items) do
172                                 got_items[#got_items+1] = add_item
173                         end
174                         if drop.max_items ~= nil and got_count == drop.max_items then
175                                 break
176                         end
177                 end
178         end
179         return got_items
180 end
181
182 function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
183         local item = itemstack:peek_item()
184         local def = itemstack:get_definition()
185         if def.type ~= "node" or pointed_thing.type ~= "node" then
186                 return itemstack, false
187         end
188
189         local under = pointed_thing.under
190         local oldnode_under = minetest.get_node_or_nil(under)
191         local above = pointed_thing.above
192         local oldnode_above = minetest.get_node_or_nil(above)
193
194         if not oldnode_under or not oldnode_above then
195                 minetest.log("info", placer:get_player_name() .. " tried to place"
196                         .. " node in unloaded position " .. minetest.pos_to_string(above))
197                 return itemstack, false
198         end
199
200         local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
201         olddef_under = olddef_under or minetest.nodedef_default
202         local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
203         olddef_above = olddef_above or minetest.nodedef_default
204
205         if not olddef_above.buildable_to and not olddef_under.buildable_to then
206                 minetest.log("info", placer:get_player_name() .. " tried to place"
207                         .. " node in invalid position " .. minetest.pos_to_string(above)
208                         .. ", replacing " .. oldnode_above.name)
209                 return itemstack, false
210         end
211
212         -- Place above pointed node
213         local place_to = {x = above.x, y = above.y, z = above.z}
214
215         -- If node under is buildable_to, place into it instead (eg. snow)
216         if olddef_under.buildable_to then
217                 minetest.log("info", "node under is buildable to")
218                 place_to = {x = under.x, y = under.y, z = under.z}
219         end
220
221         if minetest.is_protected(place_to, placer:get_player_name()) then
222                 minetest.log("action", placer:get_player_name()
223                                 .. " tried to place " .. def.name
224                                 .. " at protected position "
225                                 .. minetest.pos_to_string(place_to))
226                 minetest.record_protection_violation(place_to, placer:get_player_name())
227                 return itemstack
228         end
229
230         minetest.log("action", placer:get_player_name() .. " places node "
231                 .. def.name .. " at " .. minetest.pos_to_string(place_to))
232         
233         local oldnode = minetest.get_node(place_to)
234         local newnode = {name = def.name, param1 = 0, param2 = param2}
235
236         -- Calculate direction for wall mounted stuff like torches and signs
237         if def.paramtype2 == 'wallmounted' and not param2 then
238                 local dir = {
239                         x = under.x - above.x,
240                         y = under.y - above.y,
241                         z = under.z - above.z
242                 }
243                 newnode.param2 = minetest.dir_to_wallmounted(dir)
244         -- Calculate the direction for furnaces and chests and stuff
245         elseif def.paramtype2 == 'facedir' and not param2 then
246                 local placer_pos = placer:getpos()
247                 if placer_pos then
248                         local dir = {
249                                 x = above.x - placer_pos.x,
250                                 y = above.y - placer_pos.y,
251                                 z = above.z - placer_pos.z
252                         }
253                         newnode.param2 = minetest.dir_to_facedir(dir)
254                         minetest.log("action", "facedir: " .. newnode.param2)
255                 end
256         end
257
258         -- Check if the node is attached and if it can be placed there
259         if minetest.get_item_group(def.name, "attached_node") ~= 0 and
260                 not check_attached_node(place_to, newnode) then
261                 minetest.log("action", "attached node " .. def.name ..
262                         " can not be placed at " .. minetest.pos_to_string(place_to))
263                 return itemstack, false
264         end
265
266         -- Add node and update
267         minetest.add_node(place_to, newnode)
268
269         local take_item = true
270
271         -- Run callback
272         if def.after_place_node then
273                 -- Deepcopy place_to and pointed_thing because callback can modify it
274                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
275                 local pointed_thing_copy = {
276                         type = pointed_thing.type,
277                         under = {
278                                 x = pointed_thing.under.x,
279                                 y = pointed_thing.under.y,
280                                 z = pointed_thing.under.z},
281                         above = {
282                                 x = pointed_thing.above.x,
283                                 y = pointed_thing.above.y,
284                                 z = pointed_thing.above.z}
285                 }
286                 if def.after_place_node(place_to_copy, placer, itemstack,
287                                 pointed_thing_copy) then
288                         take_item = false
289                 end
290         end
291
292         -- Run script hook
293         local _, callback
294         for _, callback in ipairs(minetest.registered_on_placenodes) do
295                 -- Deepcopy pos, node and poined_thing because callback can modify them
296                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
297                 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
298                 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
299                 local pointed_thing_copy = {
300                         type = pointed_thing.type,
301                         under = {
302                                 x = pointed_thing.under.x,
303                                 y = pointed_thing.under.y,
304                                 z = pointed_thing.under.z},
305                         above = {
306                                 x = pointed_thing.above.x,
307                                 y = pointed_thing.above.y,
308                                 z = pointed_thing.above.z}
309                 }
310                 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, poined_thing_copy) then
311                         take_item = false
312                 end
313         end
314
315         if take_item then
316                 itemstack:take_item()
317         end
318         return itemstack, true
319 end
320
321 function minetest.item_place_object(itemstack, placer, pointed_thing)
322         local pos = minetest.get_pointed_thing_position(pointed_thing, true)
323         if pos ~= nil then
324                 local item = itemstack:take_item()
325                 minetest.add_item(pos, item)
326         end
327         return itemstack
328 end
329
330 function minetest.item_place(itemstack, placer, pointed_thing, param2)
331         -- Call on_rightclick if the pointed node defines it
332         if pointed_thing.type == "node" and placer and
333                         not placer:get_player_control().sneak then
334                 local n = minetest.get_node(pointed_thing.under)
335                 local nn = n.name
336                 if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
337                         return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
338                                         placer, itemstack, pointed_thing) or itemstack, false
339                 end
340         end
341
342         if itemstack:get_definition().type == "node" then
343                 return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
344         end
345         return itemstack
346 end
347
348 function minetest.item_drop(itemstack, dropper, pos)
349         if dropper.get_player_name then
350                 local v = dropper:get_look_dir()
351                 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
352                 local obj = minetest.add_item(p, itemstack)
353                 if obj then
354                         v.x = v.x*2
355                         v.y = v.y*2 + 1
356                         v.z = v.z*2
357                         obj:setvelocity(v)
358                 end
359         else
360                 minetest.add_item(pos, itemstack)
361         end
362         return ItemStack("")
363 end
364
365 function minetest.item_eat(hp_change, replace_with_item)
366         return function(itemstack, user, pointed_thing)  -- closure
367                 if itemstack:take_item() ~= nil then
368                         user:set_hp(user:get_hp() + hp_change)
369                         itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
370                 end
371                 return itemstack
372         end
373 end
374
375 function minetest.node_punch(pos, node, puncher)
376         -- Run script hook
377         local _, callback
378         for _, callback in ipairs(minetest.registered_on_punchnodes) do
379                 -- Copy pos and node because callback can modify them
380                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
381                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
382                 callback(pos_copy, node_copy, puncher)
383         end
384 end
385
386 function minetest.handle_node_drops(pos, drops, digger)
387         -- Add dropped items to object's inventory
388         if digger:get_inventory() then
389                 local _, dropped_item
390                 for _, dropped_item in ipairs(drops) do
391                         local left = digger:get_inventory():add_item("main", dropped_item)
392                         if not left:is_empty() then
393                                 local p = {
394                                         x = pos.x + math.random()/2-0.25,
395                                         y = pos.y + math.random()/2-0.25,
396                                         z = pos.z + math.random()/2-0.25,
397                                 }
398                                 minetest.add_item(p, left)
399                         end
400                 end
401         end
402 end
403
404 function minetest.node_dig(pos, node, digger)
405         local def = ItemStack({name=node.name}):get_definition()
406         if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
407                 minetest.log("info", digger:get_player_name() .. " tried to dig "
408                         .. node.name .. " which is not diggable "
409                         .. minetest.pos_to_string(pos))
410                 return
411         end
412
413         if minetest.is_protected(pos, digger:get_player_name()) then
414                 minetest.log("action", digger:get_player_name()
415                                 .. " tried to dig " .. node.name
416                                 .. " at protected position "
417                                 .. minetest.pos_to_string(pos))
418                 minetest.record_protection_violation(pos, digger:get_player_name())
419                 return
420         end
421
422         minetest.log('action', digger:get_player_name() .. " digs "
423                 .. node.name .. " at " .. minetest.pos_to_string(pos))
424
425         local wielded = digger:get_wielded_item()
426         local drops = minetest.get_node_drops(node.name, wielded:get_name())
427         
428         local wdef = wielded:get_definition()
429         local tp = wielded:get_tool_capabilities()
430         local dp = minetest.get_dig_params(def.groups, tp)
431         if wdef and wdef.after_use then
432                 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
433         else
434                 -- Wear out tool
435                 if not minetest.setting_getbool("creative_mode") then
436                         wielded:add_wear(dp.wear)
437                 end
438         end
439         digger:set_wielded_item(wielded)
440         
441         -- Handle drops
442         minetest.handle_node_drops(pos, drops, digger)
443
444         local oldmetadata = nil
445         if def.after_dig_node then
446                 oldmetadata = minetest.get_meta(pos):to_table()
447         end
448
449         -- Remove node and update
450         minetest.remove_node(pos)
451         
452         -- Run callback
453         if def.after_dig_node then
454                 -- Copy pos and node because callback can modify them
455                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
456                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
457                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
458         end
459
460         -- Run script hook
461         local _, callback
462         for _, callback in ipairs(minetest.registered_on_dignodes) do
463                 -- Copy pos and node because callback can modify them
464                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
465                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
466                 callback(pos_copy, node_copy, digger)
467         end
468 end
469
470 -- This is used to allow mods to redefine minetest.item_place and so on
471 -- NOTE: This is not the preferred way. Preferred way is to provide enough
472 --       callbacks to not require redefining global functions. -celeron55
473 local function redef_wrapper(table, name)
474         return function(...)
475                 return table[name](...)
476         end
477 end
478
479 --
480 -- Item definition defaults
481 --
482
483 minetest.nodedef_default = {
484         -- Item properties
485         type="node",
486         -- name intentionally not defined here
487         description = "",
488         groups = {},
489         inventory_image = "",
490         wield_image = "",
491         wield_scale = {x=1,y=1,z=1},
492         stack_max = 99,
493         usable = false,
494         liquids_pointable = false,
495         tool_capabilities = nil,
496         node_placement_prediction = nil,
497
498         -- Interaction callbacks
499         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
500         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
501         on_use = nil,
502         can_dig = nil,
503
504         on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
505         on_rightclick = nil,
506         on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
507
508         on_receive_fields = nil,
509         
510         on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
511         on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
512         on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
513
514         -- Node properties
515         drawtype = "normal",
516         visual_scale = 1.0,
517         -- Don't define these because otherwise the old tile_images and
518         -- special_materials wouldn't be read
519         --tiles ={""},
520         --special_tiles = {
521         --      {name="", backface_culling=true},
522         --      {name="", backface_culling=true},
523         --},
524         alpha = 255,
525         post_effect_color = {a=0, r=0, g=0, b=0},
526         paramtype = "none",
527         paramtype2 = "none",
528         is_ground_content = true,
529         sunlight_propagates = false,
530         walkable = true,
531         pointable = true,
532         diggable = true,
533         climbable = false,
534         buildable_to = false,
535         liquidtype = "none",
536         liquid_alternative_flowing = "",
537         liquid_alternative_source = "",
538         liquid_viscosity = 0,
539         drowning = 0,
540         light_source = 0,
541         damage_per_second = 0,
542         selection_box = {type="regular"},
543         legacy_facedir_simple = false,
544         legacy_wallmounted = false,
545 }
546
547 minetest.craftitemdef_default = {
548         type="craft",
549         -- name intentionally not defined here
550         description = "",
551         groups = {},
552         inventory_image = "",
553         wield_image = "",
554         wield_scale = {x=1,y=1,z=1},
555         stack_max = 99,
556         liquids_pointable = false,
557         tool_capabilities = nil,
558
559         -- Interaction callbacks
560         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
561         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
562         on_use = nil,
563 }
564
565 minetest.tooldef_default = {
566         type="tool",
567         -- name intentionally not defined here
568         description = "",
569         groups = {},
570         inventory_image = "",
571         wield_image = "",
572         wield_scale = {x=1,y=1,z=1},
573         stack_max = 1,
574         liquids_pointable = false,
575         tool_capabilities = nil,
576
577         -- Interaction callbacks
578         on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
579         on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
580         on_use = nil,
581 }
582
583 minetest.noneitemdef_default = {  -- This is used for the hand and unknown items
584         type="none",
585         -- name intentionally not defined here
586         description = "",
587         groups = {},
588         inventory_image = "",
589         wield_image = "",
590         wield_scale = {x=1,y=1,z=1},
591         stack_max = 99,
592         liquids_pointable = false,
593         tool_capabilities = nil,
594
595         -- Interaction callbacks
596         on_place = redef_wrapper(minetest, 'item_place'),
597         on_drop = nil,
598         on_use = nil,
599 }
600