1 -- Minetest: builtin/item.lua
3 local builtin_shared = ...
5 local function copy_pointed_thing(pointed_thing)
7 type = pointed_thing.type,
8 above = vector.new(pointed_thing.above),
9 under = vector.new(pointed_thing.under),
10 ref = pointed_thing.ref,
15 -- Item definition helpers
18 function core.inventorycube(img1, img2, img3)
21 return "[inventorycube"
22 .. "{" .. img1:gsub("%^", "&")
23 .. "{" .. img2:gsub("%^", "&")
24 .. "{" .. img3:gsub("%^", "&")
27 function core.get_pointed_thing_position(pointed_thing, above)
28 if pointed_thing.type == "node" then
30 -- The position where a node would be placed
31 return pointed_thing.above
33 -- The position where a node would be dug
34 return pointed_thing.under
35 elseif pointed_thing.type == "object" then
36 return pointed_thing.ref and pointed_thing.ref:getpos()
40 function core.dir_to_facedir(dir, is6d)
41 --account for y if requested
42 if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
46 if math.abs(dir.x) > math.abs(dir.z) then
62 if math.abs(dir.x) > math.abs(dir.z) then
77 --otherwise, place horizontally
78 elseif math.abs(dir.x) > math.abs(dir.z) then
93 -- Table of possible dirs
94 local facedir_to_dir = {
102 -- Mapping from facedir value to index in facedir_to_dir.
103 local facedir_to_dir_map = {
111 function core.facedir_to_dir(facedir)
112 return facedir_to_dir[facedir_to_dir_map[facedir]]
115 function core.dir_to_wallmounted(dir)
116 if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
122 elseif math.abs(dir.x) > math.abs(dir.z) then
137 -- table of dirs in wallmounted order
138 local wallmounted_to_dir = {
139 [0] = {x = 0, y = 1, z = 0},
140 {x = 0, y = -1, z = 0},
141 {x = 1, y = 0, z = 0},
142 {x = -1, y = 0, z = 0},
143 {x = 0, y = 0, z = 1},
144 {x = 0, y = 0, z = -1},
146 function core.wallmounted_to_dir(wallmounted)
147 return wallmounted_to_dir[wallmounted]
150 function core.get_node_drops(nodename, toolname)
151 local drop = ItemStack({name=nodename}):get_definition().drop
155 elseif type(drop) == "string" then
158 elseif drop.items == nil then
159 -- drop = {} to disable default drop
163 -- Extended drop table
167 for _, item in ipairs(drop.items) do
168 local good_rarity = true
169 local good_tool = true
170 if item.rarity ~= nil then
171 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
173 if item.tools ~= nil then
175 for _, tool in ipairs(item.tools) do
176 if tool:sub(1, 1) == '~' then
177 good_tool = toolname:find(tool:sub(2)) ~= nil
179 good_tool = toolname == tool
186 if good_rarity and good_tool then
187 got_count = got_count + 1
188 for _, add_item in ipairs(item.items) do
189 got_items[#got_items+1] = add_item
191 if drop.max_items ~= nil and got_count == drop.max_items then
199 function core.item_place_node(itemstack, placer, pointed_thing, param2)
200 local item = itemstack:peek_item()
201 local def = itemstack:get_definition()
202 if def.type ~= "node" or pointed_thing.type ~= "node" then
203 return itemstack, false
206 local under = pointed_thing.under
207 local oldnode_under = core.get_node_or_nil(under)
208 local above = pointed_thing.above
209 local oldnode_above = core.get_node_or_nil(above)
211 if not oldnode_under or not oldnode_above then
212 core.log("info", placer:get_player_name() .. " tried to place"
213 .. " node in unloaded position " .. core.pos_to_string(above))
214 return itemstack, false
217 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
218 olddef_under = olddef_under or core.nodedef_default
219 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
220 olddef_above = olddef_above or core.nodedef_default
222 if not olddef_above.buildable_to and not olddef_under.buildable_to then
223 core.log("info", placer:get_player_name() .. " tried to place"
224 .. " node in invalid position " .. core.pos_to_string(above)
225 .. ", replacing " .. oldnode_above.name)
226 return itemstack, false
229 -- Place above pointed node
230 local place_to = {x = above.x, y = above.y, z = above.z}
232 -- If node under is buildable_to, place into it instead (eg. snow)
233 if olddef_under.buildable_to then
234 core.log("info", "node under is buildable to")
235 place_to = {x = under.x, y = under.y, z = under.z}
238 if core.is_protected(place_to, placer:get_player_name()) and
239 not minetest.check_player_privs(placer, "protection_bypass") then
240 core.log("action", placer:get_player_name()
241 .. " tried to place " .. def.name
242 .. " at protected position "
243 .. core.pos_to_string(place_to))
244 core.record_protection_violation(place_to, placer:get_player_name())
248 core.log("action", placer:get_player_name() .. " places node "
249 .. def.name .. " at " .. core.pos_to_string(place_to))
251 local oldnode = core.get_node(place_to)
252 local newnode = {name = def.name, param1 = 0, param2 = param2}
254 -- Calculate direction for wall mounted stuff like torches and signs
255 if def.place_param2 ~= nil then
256 newnode.param2 = def.place_param2
257 elseif def.paramtype2 == 'wallmounted' and not param2 then
259 x = under.x - above.x,
260 y = under.y - above.y,
261 z = under.z - above.z
263 newnode.param2 = core.dir_to_wallmounted(dir)
264 -- Calculate the direction for furnaces and chests and stuff
265 elseif def.paramtype2 == 'facedir' and not param2 then
266 local placer_pos = placer:getpos()
269 x = above.x - placer_pos.x,
270 y = above.y - placer_pos.y,
271 z = above.z - placer_pos.z
273 newnode.param2 = core.dir_to_facedir(dir)
274 core.log("action", "facedir: " .. newnode.param2)
278 -- Check if the node is attached and if it can be placed there
279 if core.get_item_group(def.name, "attached_node") ~= 0 and
280 not builtin_shared.check_attached_node(place_to, newnode) then
281 core.log("action", "attached node " .. def.name ..
282 " can not be placed at " .. core.pos_to_string(place_to))
283 return itemstack, false
286 -- Add node and update
287 core.add_node(place_to, newnode)
289 local take_item = true
292 if def.after_place_node then
293 -- Deepcopy place_to and pointed_thing because callback can modify it
294 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
295 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
296 if def.after_place_node(place_to_copy, placer, itemstack,
297 pointed_thing_copy) then
304 for _, callback in ipairs(core.registered_on_placenodes) do
305 -- Deepcopy pos, node and pointed_thing because callback can modify them
306 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
307 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
308 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
309 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
310 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
316 itemstack:take_item()
318 return itemstack, true
321 function core.item_place_object(itemstack, placer, pointed_thing)
322 local pos = core.get_pointed_thing_position(pointed_thing, true)
324 local item = itemstack:take_item()
325 core.add_item(pos, item)
330 function core.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 = core.get_node(pointed_thing.under)
336 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
337 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
338 placer, itemstack, pointed_thing) or itemstack, false
342 if itemstack:get_definition().type == "node" then
343 return core.item_place_node(itemstack, placer, pointed_thing, param2)
348 function core.item_secondary_use(itemstack, placer)
352 function core.item_drop(itemstack, dropper, pos)
353 if dropper and dropper:is_player() then
354 local v = dropper:get_look_dir()
355 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
356 local cs = itemstack:get_count()
357 if dropper:get_player_control().sneak then
360 local item = itemstack:take_item(cs)
361 local obj = core.add_item(p, item)
367 obj:get_luaentity().dropped_by = dropper:get_player_name()
372 if core.add_item(pos, itemstack) then
376 -- If we reach this, adding the object to the
377 -- environment failed
380 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
381 for _, callback in pairs(core.registered_on_item_eats) do
382 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
387 if itemstack:take_item() ~= nil then
388 user:set_hp(user:get_hp() + hp_change)
390 if replace_with_item then
391 if itemstack:is_empty() then
392 itemstack:add_item(replace_with_item)
394 local inv = user:get_inventory()
395 if inv:room_for_item("main", {name=replace_with_item}) then
396 inv:add_item("main", replace_with_item)
398 local pos = user:getpos()
399 pos.y = math.floor(pos.y + 0.5)
400 core.add_item(pos, replace_with_item)
408 function core.item_eat(hp_change, replace_with_item)
409 return function(itemstack, user, pointed_thing) -- closure
410 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
414 function core.node_punch(pos, node, puncher, pointed_thing)
416 for _, callback in ipairs(core.registered_on_punchnodes) do
417 -- Copy pos and node because callback can modify them
418 local pos_copy = vector.new(pos)
419 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
420 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
421 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
425 function core.handle_node_drops(pos, drops, digger)
426 -- Add dropped items to object's inventory
427 if digger:get_inventory() then
428 local _, dropped_item
429 for _, dropped_item in ipairs(drops) do
430 local left = digger:get_inventory():add_item("main", dropped_item)
431 if not left:is_empty() then
433 x = pos.x + math.random()/2-0.25,
434 y = pos.y + math.random()/2-0.25,
435 z = pos.z + math.random()/2-0.25,
437 core.add_item(p, left)
443 function core.node_dig(pos, node, digger)
444 local def = ItemStack({name=node.name}):get_definition()
445 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
446 core.log("info", digger:get_player_name() .. " tried to dig "
447 .. node.name .. " which is not diggable "
448 .. core.pos_to_string(pos))
452 if core.is_protected(pos, digger:get_player_name()) and
453 not minetest.check_player_privs(digger, "protection_bypass") then
454 core.log("action", digger:get_player_name()
455 .. " tried to dig " .. node.name
456 .. " at protected position "
457 .. core.pos_to_string(pos))
458 core.record_protection_violation(pos, digger:get_player_name())
462 core.log('action', digger:get_player_name() .. " digs "
463 .. node.name .. " at " .. core.pos_to_string(pos))
465 local wielded = digger:get_wielded_item()
466 local drops = core.get_node_drops(node.name, wielded:get_name())
468 local wdef = wielded:get_definition()
469 local tp = wielded:get_tool_capabilities()
470 local dp = core.get_dig_params(def.groups, tp)
471 if wdef and wdef.after_use then
472 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
475 if not core.setting_getbool("creative_mode") then
476 wielded:add_wear(dp.wear)
477 if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
478 core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
482 digger:set_wielded_item(wielded)
485 core.handle_node_drops(pos, drops, digger)
487 local oldmetadata = nil
488 if def.after_dig_node then
489 oldmetadata = core.get_meta(pos):to_table()
492 -- Remove node and update
493 core.remove_node(pos)
496 if def.after_dig_node then
497 -- Copy pos and node because callback can modify them
498 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
499 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
500 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
505 for _, callback in ipairs(core.registered_on_dignodes) do
506 local origin = core.callback_origins[callback]
508 core.set_last_run_mod(origin.mod)
509 --print("Running " .. tostring(callback) ..
510 -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
512 --print("No data associated with callback")
515 -- Copy pos and node because callback can modify them
516 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
517 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
518 callback(pos_copy, node_copy, digger)
522 -- This is used to allow mods to redefine core.item_place and so on
523 -- NOTE: This is not the preferred way. Preferred way is to provide enough
524 -- callbacks to not require redefining global functions. -celeron55
525 local function redef_wrapper(table, name)
527 return table[name](...)
532 -- Item definition defaults
535 core.nodedef_default = {
538 -- name intentionally not defined here
541 inventory_image = "",
543 wield_scale = {x=1,y=1,z=1},
546 liquids_pointable = false,
547 tool_capabilities = nil,
548 node_placement_prediction = nil,
550 -- Interaction callbacks
551 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
552 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
556 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
558 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
560 on_receive_fields = nil,
562 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
563 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
564 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
569 -- Don't define these because otherwise the old tile_images and
570 -- special_materials wouldn't be read
573 -- {name="", backface_culling=true},
574 -- {name="", backface_culling=true},
577 post_effect_color = {a=0, r=0, g=0, b=0},
580 is_ground_content = true,
581 sunlight_propagates = false,
586 buildable_to = false,
589 liquid_alternative_flowing = "",
590 liquid_alternative_source = "",
591 liquid_viscosity = 0,
594 damage_per_second = 0,
595 selection_box = {type="regular"},
596 legacy_facedir_simple = false,
597 legacy_wallmounted = false,
600 core.craftitemdef_default = {
602 -- name intentionally not defined here
605 inventory_image = "",
607 wield_scale = {x=1,y=1,z=1},
609 liquids_pointable = false,
610 tool_capabilities = nil,
612 -- Interaction callbacks
613 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
614 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
615 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
619 core.tooldef_default = {
621 -- name intentionally not defined here
624 inventory_image = "",
626 wield_scale = {x=1,y=1,z=1},
628 liquids_pointable = false,
629 tool_capabilities = nil,
631 -- Interaction callbacks
632 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
633 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
634 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
638 core.noneitemdef_default = { -- This is used for the hand and unknown items
640 -- name intentionally not defined here
643 inventory_image = "",
645 wield_scale = {x=1,y=1,z=1},
647 liquids_pointable = false,
648 tool_capabilities = nil,
650 -- Interaction callbacks
651 on_place = redef_wrapper(core, 'item_place'),
652 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),