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.dir_to_yaw(dir)
151 return -math.atan2(dir.x, dir.z)
154 function core.yaw_to_dir(yaw)
155 return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
158 function core.get_node_drops(nodename, toolname)
159 local drop = ItemStack({name=nodename}):get_definition().drop
163 elseif type(drop) == "string" then
166 elseif drop.items == nil then
167 -- drop = {} to disable default drop
171 -- Extended drop table
175 for _, item in ipairs(drop.items) do
176 local good_rarity = true
177 local good_tool = true
178 if item.rarity ~= nil then
179 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
181 if item.tools ~= nil then
183 for _, tool in ipairs(item.tools) do
184 if tool:sub(1, 1) == '~' then
185 good_tool = toolname:find(tool:sub(2)) ~= nil
187 good_tool = toolname == tool
194 if good_rarity and good_tool then
195 got_count = got_count + 1
196 for _, add_item in ipairs(item.items) do
197 got_items[#got_items+1] = add_item
199 if drop.max_items ~= nil and got_count == drop.max_items then
207 function core.item_place_node(itemstack, placer, pointed_thing, param2)
208 local item = itemstack:peek_item()
209 local def = itemstack:get_definition()
210 if def.type ~= "node" or pointed_thing.type ~= "node" then
211 return itemstack, false
214 local under = pointed_thing.under
215 local oldnode_under = core.get_node_or_nil(under)
216 local above = pointed_thing.above
217 local oldnode_above = core.get_node_or_nil(above)
219 if not oldnode_under or not oldnode_above then
220 core.log("info", placer:get_player_name() .. " tried to place"
221 .. " node in unloaded position " .. core.pos_to_string(above))
222 return itemstack, false
225 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
226 olddef_under = olddef_under or core.nodedef_default
227 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
228 olddef_above = olddef_above or core.nodedef_default
230 if not olddef_above.buildable_to and not olddef_under.buildable_to then
231 core.log("info", placer:get_player_name() .. " tried to place"
232 .. " node in invalid position " .. core.pos_to_string(above)
233 .. ", replacing " .. oldnode_above.name)
234 return itemstack, false
237 -- Place above pointed node
238 local place_to = {x = above.x, y = above.y, z = above.z}
240 -- If node under is buildable_to, place into it instead (eg. snow)
241 if olddef_under.buildable_to then
242 core.log("info", "node under is buildable to")
243 place_to = {x = under.x, y = under.y, z = under.z}
246 if core.is_protected(place_to, placer:get_player_name()) and
247 not minetest.check_player_privs(placer, "protection_bypass") then
248 core.log("action", placer:get_player_name()
249 .. " tried to place " .. def.name
250 .. " at protected position "
251 .. core.pos_to_string(place_to))
252 core.record_protection_violation(place_to, placer:get_player_name())
256 core.log("action", placer:get_player_name() .. " places node "
257 .. def.name .. " at " .. core.pos_to_string(place_to))
259 local oldnode = core.get_node(place_to)
260 local newnode = {name = def.name, param1 = 0, param2 = param2}
262 -- Calculate direction for wall mounted stuff like torches and signs
263 if def.place_param2 ~= nil then
264 newnode.param2 = def.place_param2
265 elseif def.paramtype2 == 'wallmounted' and not param2 then
267 x = under.x - above.x,
268 y = under.y - above.y,
269 z = under.z - above.z
271 newnode.param2 = core.dir_to_wallmounted(dir)
272 -- Calculate the direction for furnaces and chests and stuff
273 elseif def.paramtype2 == 'facedir' and not param2 then
274 local placer_pos = placer:getpos()
277 x = above.x - placer_pos.x,
278 y = above.y - placer_pos.y,
279 z = above.z - placer_pos.z
281 newnode.param2 = core.dir_to_facedir(dir)
282 core.log("action", "facedir: " .. newnode.param2)
286 -- Check if the node is attached and if it can be placed there
287 if core.get_item_group(def.name, "attached_node") ~= 0 and
288 not builtin_shared.check_attached_node(place_to, newnode) then
289 core.log("action", "attached node " .. def.name ..
290 " can not be placed at " .. core.pos_to_string(place_to))
291 return itemstack, false
294 -- Add node and update
295 core.add_node(place_to, newnode)
297 local take_item = true
300 if def.after_place_node then
301 -- Deepcopy place_to and pointed_thing because callback can modify it
302 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
303 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
304 if def.after_place_node(place_to_copy, placer, itemstack,
305 pointed_thing_copy) then
312 for _, callback in ipairs(core.registered_on_placenodes) do
313 -- Deepcopy pos, node and pointed_thing because callback can modify them
314 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
315 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
316 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
317 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
318 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
324 itemstack:take_item()
326 return itemstack, true
329 function core.item_place_object(itemstack, placer, pointed_thing)
330 local pos = core.get_pointed_thing_position(pointed_thing, true)
332 local item = itemstack:take_item()
333 core.add_item(pos, item)
338 function core.item_place(itemstack, placer, pointed_thing, param2)
339 -- Call on_rightclick if the pointed node defines it
340 if pointed_thing.type == "node" and placer and
341 not placer:get_player_control().sneak then
342 local n = core.get_node(pointed_thing.under)
344 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
345 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
346 placer, itemstack, pointed_thing) or itemstack, false
350 if itemstack:get_definition().type == "node" then
351 return core.item_place_node(itemstack, placer, pointed_thing, param2)
356 function core.item_secondary_use(itemstack, placer)
360 function core.item_drop(itemstack, dropper, pos)
361 if dropper and dropper:is_player() then
362 local v = dropper:get_look_dir()
363 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
364 local cs = itemstack:get_count()
365 if dropper:get_player_control().sneak then
368 local item = itemstack:take_item(cs)
369 local obj = core.add_item(p, item)
375 obj:get_luaentity().dropped_by = dropper:get_player_name()
380 if core.add_item(pos, itemstack) then
384 -- If we reach this, adding the object to the
385 -- environment failed
388 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
389 for _, callback in pairs(core.registered_on_item_eats) do
390 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
395 if itemstack:take_item() ~= nil then
396 user:set_hp(user:get_hp() + hp_change)
398 if replace_with_item then
399 if itemstack:is_empty() then
400 itemstack:add_item(replace_with_item)
402 local inv = user:get_inventory()
403 if inv:room_for_item("main", {name=replace_with_item}) then
404 inv:add_item("main", replace_with_item)
406 local pos = user:getpos()
407 pos.y = math.floor(pos.y + 0.5)
408 core.add_item(pos, replace_with_item)
416 function core.item_eat(hp_change, replace_with_item)
417 return function(itemstack, user, pointed_thing) -- closure
418 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
422 function core.node_punch(pos, node, puncher, pointed_thing)
424 for _, callback in ipairs(core.registered_on_punchnodes) do
425 -- Copy pos and node because callback can modify them
426 local pos_copy = vector.new(pos)
427 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
428 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
429 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
433 function core.handle_node_drops(pos, drops, digger)
434 -- Add dropped items to object's inventory
435 if digger:get_inventory() then
436 local _, dropped_item
437 for _, dropped_item in ipairs(drops) do
438 local left = digger:get_inventory():add_item("main", dropped_item)
439 if not left:is_empty() then
441 x = pos.x + math.random()/2-0.25,
442 y = pos.y + math.random()/2-0.25,
443 z = pos.z + math.random()/2-0.25,
445 core.add_item(p, left)
451 function core.node_dig(pos, node, digger)
452 local def = ItemStack({name=node.name}):get_definition()
453 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
454 core.log("info", digger:get_player_name() .. " tried to dig "
455 .. node.name .. " which is not diggable "
456 .. core.pos_to_string(pos))
460 if core.is_protected(pos, digger:get_player_name()) and
461 not minetest.check_player_privs(digger, "protection_bypass") then
462 core.log("action", digger:get_player_name()
463 .. " tried to dig " .. node.name
464 .. " at protected position "
465 .. core.pos_to_string(pos))
466 core.record_protection_violation(pos, digger:get_player_name())
470 core.log('action', digger:get_player_name() .. " digs "
471 .. node.name .. " at " .. core.pos_to_string(pos))
473 local wielded = digger:get_wielded_item()
474 local drops = core.get_node_drops(node.name, wielded:get_name())
476 local wdef = wielded:get_definition()
477 local tp = wielded:get_tool_capabilities()
478 local dp = core.get_dig_params(def.groups, tp)
479 if wdef and wdef.after_use then
480 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
483 if not core.setting_getbool("creative_mode") then
484 wielded:add_wear(dp.wear)
485 if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
486 core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
490 digger:set_wielded_item(wielded)
493 core.handle_node_drops(pos, drops, digger)
495 local oldmetadata = nil
496 if def.after_dig_node then
497 oldmetadata = core.get_meta(pos):to_table()
500 -- Remove node and update
501 core.remove_node(pos)
504 if def.after_dig_node then
505 -- Copy pos and node because callback can modify them
506 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
507 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
508 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
513 for _, callback in ipairs(core.registered_on_dignodes) do
514 local origin = core.callback_origins[callback]
516 core.set_last_run_mod(origin.mod)
517 --print("Running " .. tostring(callback) ..
518 -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
520 --print("No data associated with callback")
523 -- Copy pos and node because callback can modify them
524 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
525 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
526 callback(pos_copy, node_copy, digger)
530 -- This is used to allow mods to redefine core.item_place and so on
531 -- NOTE: This is not the preferred way. Preferred way is to provide enough
532 -- callbacks to not require redefining global functions. -celeron55
533 local function redef_wrapper(table, name)
535 return table[name](...)
540 -- Item definition defaults
543 core.nodedef_default = {
546 -- name intentionally not defined here
549 inventory_image = "",
551 wield_scale = {x=1,y=1,z=1},
554 liquids_pointable = false,
555 tool_capabilities = nil,
556 node_placement_prediction = nil,
558 -- Interaction callbacks
559 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
560 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
564 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
566 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
568 on_receive_fields = nil,
570 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
571 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
572 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
577 -- Don't define these because otherwise the old tile_images and
578 -- special_materials wouldn't be read
581 -- {name="", backface_culling=true},
582 -- {name="", backface_culling=true},
585 post_effect_color = {a=0, r=0, g=0, b=0},
588 is_ground_content = true,
589 sunlight_propagates = false,
594 buildable_to = false,
597 liquid_alternative_flowing = "",
598 liquid_alternative_source = "",
599 liquid_viscosity = 0,
602 damage_per_second = 0,
603 selection_box = {type="regular"},
604 legacy_facedir_simple = false,
605 legacy_wallmounted = false,
608 core.craftitemdef_default = {
610 -- name intentionally not defined here
613 inventory_image = "",
615 wield_scale = {x=1,y=1,z=1},
617 liquids_pointable = false,
618 tool_capabilities = nil,
620 -- Interaction callbacks
621 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
622 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
623 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
627 core.tooldef_default = {
629 -- name intentionally not defined here
632 inventory_image = "",
634 wield_scale = {x=1,y=1,z=1},
636 liquids_pointable = false,
637 tool_capabilities = nil,
639 -- Interaction callbacks
640 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
641 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
642 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
646 core.noneitemdef_default = { -- This is used for the hand and unknown items
648 -- name intentionally not defined here
651 inventory_image = "",
653 wield_scale = {x=1,y=1,z=1},
655 liquids_pointable = false,
656 tool_capabilities = nil,
658 -- Interaction callbacks
659 on_place = redef_wrapper(core, 'item_place'),
660 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),