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:get_pos()
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 % 32]]
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 % 8]
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.is_colored_paramtype(ptype)
159 return (ptype == "color") or (ptype == "colorfacedir") or
160 (ptype == "colorwallmounted")
163 function core.strip_param2_color(param2, paramtype2)
164 if not core.is_colored_paramtype(paramtype2) then
167 if paramtype2 == "colorfacedir" then
168 param2 = math.floor(param2 / 32) * 32
169 elseif paramtype2 == "colorwallmounted" then
170 param2 = math.floor(param2 / 8) * 8
172 -- paramtype2 == "color" requires no modification.
176 function core.get_node_drops(node, toolname)
177 -- Compatibility, if node is string
178 local nodename = node
180 -- New format, if node is table
181 if (type(node) == "table") then
185 local def = core.registered_nodes[nodename]
186 local drop = def and def.drop
187 local ptype = def and def.paramtype2
188 -- get color, if there is color (otherwise nil)
189 local palette_index = core.strip_param2_color(param2, ptype)
192 if palette_index then
193 local stack = ItemStack(nodename)
194 stack:get_meta():set_int("palette_index", palette_index)
195 return {stack:to_string()}
198 elseif type(drop) == "string" then
200 return drop ~= "" and {drop} or {}
201 elseif drop.items == nil then
202 -- drop = {} to disable default drop
206 -- Extended drop table
209 for _, item in ipairs(drop.items) do
210 local good_rarity = true
211 local good_tool = true
212 if item.rarity ~= nil then
213 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
215 if item.tools ~= nil then
218 if item.tools ~= nil and toolname then
219 for _, tool in ipairs(item.tools) do
220 if tool:sub(1, 1) == '~' then
221 good_tool = toolname:find(tool:sub(2)) ~= nil
223 good_tool = toolname == tool
230 if good_rarity and good_tool then
231 got_count = got_count + 1
232 for _, add_item in ipairs(item.items) do
233 -- add color, if necessary
234 if item.inherit_color and palette_index then
235 local stack = ItemStack(add_item)
236 stack:get_meta():set_int("palette_index", palette_index)
237 add_item = stack:to_string()
239 got_items[#got_items+1] = add_item
241 if drop.max_items ~= nil and got_count == drop.max_items then
249 local function user_name(user)
250 return user and user:get_player_name() or ""
253 -- Returns a logging function. For empty names, does not log.
254 local function make_log(name)
255 return name ~= "" and core.log or function() end
258 function core.item_place_node(itemstack, placer, pointed_thing, param2,
260 local def = itemstack:get_definition()
261 if def.type ~= "node" or pointed_thing.type ~= "node" then
262 return itemstack, false
265 local under = pointed_thing.under
266 local oldnode_under = core.get_node_or_nil(under)
267 local above = pointed_thing.above
268 local oldnode_above = core.get_node_or_nil(above)
269 local playername = user_name(placer)
270 local log = make_log(playername)
272 if not oldnode_under or not oldnode_above then
273 log("info", playername .. " tried to place"
274 .. " node in unloaded position " .. core.pos_to_string(above))
275 return itemstack, false
278 local olddef_under = core.registered_nodes[oldnode_under.name]
279 olddef_under = olddef_under or core.nodedef_default
280 local olddef_above = core.registered_nodes[oldnode_above.name]
281 olddef_above = olddef_above or core.nodedef_default
283 if not olddef_above.buildable_to and not olddef_under.buildable_to then
284 log("info", playername .. " tried to place"
285 .. " node in invalid position " .. core.pos_to_string(above)
286 .. ", replacing " .. oldnode_above.name)
287 return itemstack, false
290 -- Place above pointed node
291 local place_to = {x = above.x, y = above.y, z = above.z}
293 -- If node under is buildable_to, place into it instead (eg. snow)
294 if olddef_under.buildable_to then
295 log("info", "node under is buildable to")
296 place_to = {x = under.x, y = under.y, z = under.z}
299 if core.is_protected(place_to, playername) then
300 log("action", playername
301 .. " tried to place " .. def.name
302 .. " at protected position "
303 .. core.pos_to_string(place_to))
304 core.record_protection_violation(place_to, playername)
308 log("action", playername .. " places node "
309 .. def.name .. " at " .. core.pos_to_string(place_to))
311 local oldnode = core.get_node(place_to)
312 local newnode = {name = def.name, param1 = 0, param2 = param2 or 0}
314 -- Calculate direction for wall mounted stuff like torches and signs
315 if def.place_param2 ~= nil then
316 newnode.param2 = def.place_param2
317 elseif (def.paramtype2 == "wallmounted" or
318 def.paramtype2 == "colorwallmounted") and not param2 then
320 x = under.x - above.x,
321 y = under.y - above.y,
322 z = under.z - above.z
324 newnode.param2 = core.dir_to_wallmounted(dir)
325 -- Calculate the direction for furnaces and chests and stuff
326 elseif (def.paramtype2 == "facedir" or
327 def.paramtype2 == "colorfacedir") and not param2 then
328 local placer_pos = placer and placer:get_pos()
331 x = above.x - placer_pos.x,
332 y = above.y - placer_pos.y,
333 z = above.z - placer_pos.z
335 newnode.param2 = core.dir_to_facedir(dir)
336 log("action", "facedir: " .. newnode.param2)
340 local metatable = itemstack:get_meta():to_table().fields
342 -- Transfer color information
343 if metatable.palette_index and not def.place_param2 then
344 local color_divisor = nil
345 if def.paramtype2 == "color" then
347 elseif def.paramtype2 == "colorwallmounted" then
349 elseif def.paramtype2 == "colorfacedir" then
352 if color_divisor then
353 local color = math.floor(metatable.palette_index / color_divisor)
354 local other = newnode.param2 % color_divisor
355 newnode.param2 = color * color_divisor + other
359 -- Check if the node is attached and if it can be placed there
360 if core.get_item_group(def.name, "attached_node") ~= 0 and
361 not builtin_shared.check_attached_node(place_to, newnode) then
362 log("action", "attached node " .. def.name ..
363 " can not be placed at " .. core.pos_to_string(place_to))
364 return itemstack, false
367 -- Add node and update
368 core.add_node(place_to, newnode)
370 local take_item = true
373 if def.after_place_node and not prevent_after_place then
374 -- Deepcopy place_to and pointed_thing because callback can modify it
375 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
376 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
377 if def.after_place_node(place_to_copy, placer, itemstack,
378 pointed_thing_copy) then
384 for _, callback in ipairs(core.registered_on_placenodes) do
385 -- Deepcopy pos, node and pointed_thing because callback can modify them
386 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
387 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
388 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
389 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
390 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
396 itemstack:take_item()
398 return itemstack, true
401 function core.item_place_object(itemstack, placer, pointed_thing)
402 local pos = core.get_pointed_thing_position(pointed_thing, true)
404 local item = itemstack:take_item()
405 core.add_item(pos, item)
410 function core.item_place(itemstack, placer, pointed_thing, param2)
411 -- Call on_rightclick if the pointed node defines it
412 if pointed_thing.type == "node" and placer and
413 not placer:get_player_control().sneak then
414 local n = core.get_node(pointed_thing.under)
416 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
417 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
418 placer, itemstack, pointed_thing) or itemstack, false
422 if itemstack:get_definition().type == "node" then
423 return core.item_place_node(itemstack, placer, pointed_thing, param2)
428 function core.item_secondary_use(itemstack, placer)
432 function core.item_drop(itemstack, dropper, pos)
433 local dropper_is_player = dropper and dropper:is_player()
434 local p = table.copy(pos)
435 local cnt = itemstack:get_count()
436 if dropper_is_player then
439 local item = itemstack:take_item(cnt)
440 local obj = core.add_item(p, item)
442 if dropper_is_player then
443 local dir = dropper:get_look_dir()
445 dir.y = dir.y * 2.9 + 2
447 obj:set_velocity(dir)
448 obj:get_luaentity().dropped_by = dropper:get_player_name()
452 -- If we reach this, adding the object to the
453 -- environment failed
456 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
457 for _, callback in pairs(core.registered_on_item_eats) do
458 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
463 if itemstack:take_item() ~= nil then
464 user:set_hp(user:get_hp() + hp_change)
466 local def = itemstack:get_definition()
467 if def and def.sound and def.sound.eat then
468 minetest.sound_play(def.sound.eat, { pos = user:get_pos(), max_hear_distance = 16 })
471 if replace_with_item then
472 if itemstack:is_empty() then
473 itemstack:add_item(replace_with_item)
475 local inv = user:get_inventory()
476 -- Check if inv is null, since non-players don't have one
477 if inv and inv:room_for_item("main", {name=replace_with_item}) then
478 inv:add_item("main", replace_with_item)
480 local pos = user:get_pos()
481 pos.y = math.floor(pos.y + 0.5)
482 core.add_item(pos, replace_with_item)
490 function core.item_eat(hp_change, replace_with_item)
491 return function(itemstack, user, pointed_thing) -- closure
493 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
498 function core.node_punch(pos, node, puncher, pointed_thing)
500 for _, callback in ipairs(core.registered_on_punchnodes) do
501 -- Copy pos and node because callback can modify them
502 local pos_copy = vector.new(pos)
503 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
504 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
505 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
509 function core.handle_node_drops(pos, drops, digger)
510 -- Add dropped items to object's inventory
511 local inv = digger and digger:get_inventory()
514 give_item = function(item)
515 return inv:add_item("main", item)
518 give_item = function(item)
519 -- itemstring to ItemStack for left:is_empty()
520 return ItemStack(item)
524 for _, dropped_item in pairs(drops) do
525 local left = give_item(dropped_item)
526 if not left:is_empty() then
528 x = pos.x + math.random()/2-0.25,
529 y = pos.y + math.random()/2-0.25,
530 z = pos.z + math.random()/2-0.25,
532 core.add_item(p, left)
537 function core.node_dig(pos, node, digger)
538 local diggername = user_name(digger)
539 local log = make_log(diggername)
540 local def = core.registered_nodes[node.name]
541 if def and (not def.diggable or
542 (def.can_dig and not def.can_dig(pos, digger))) then
543 log("info", diggername .. " tried to dig "
544 .. node.name .. " which is not diggable "
545 .. core.pos_to_string(pos))
549 if core.is_protected(pos, diggername) then
550 log("action", diggername
551 .. " tried to dig " .. node.name
552 .. " at protected position "
553 .. core.pos_to_string(pos))
554 core.record_protection_violation(pos, diggername)
558 log('action', diggername .. " digs "
559 .. node.name .. " at " .. core.pos_to_string(pos))
561 local wielded = digger and digger:get_wielded_item()
562 local drops = core.get_node_drops(node, wielded and wielded:get_name())
565 local wdef = wielded:get_definition()
566 local tp = wielded:get_tool_capabilities()
567 local dp = core.get_dig_params(def and def.groups, tp)
568 if wdef and wdef.after_use then
569 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
572 if not core.settings:get_bool("creative_mode") then
573 wielded:add_wear(dp.wear)
574 if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
575 core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
579 digger:set_wielded_item(wielded)
582 -- Check to see if metadata should be preserved.
583 if def and def.preserve_metadata then
584 local oldmeta = core.get_meta(pos):to_table().fields
585 -- Copy pos and node because the callback can modify them.
586 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
587 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
588 local drop_stacks = {}
589 for k, v in pairs(drops) do
590 drop_stacks[k] = ItemStack(v)
593 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
597 core.handle_node_drops(pos, drops, digger)
599 local oldmetadata = nil
600 if def and def.after_dig_node then
601 oldmetadata = core.get_meta(pos):to_table()
604 -- Remove node and update
605 core.remove_node(pos)
608 if def and def.after_dig_node then
609 -- Copy pos and node because callback can modify them
610 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
611 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
612 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
616 for _, callback in ipairs(core.registered_on_dignodes) do
617 local origin = core.callback_origins[callback]
619 core.set_last_run_mod(origin.mod)
622 -- Copy pos and node because callback can modify them
623 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
624 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
625 callback(pos_copy, node_copy, digger)
629 function core.itemstring_with_palette(item, palette_index)
630 local stack = ItemStack(item) -- convert to ItemStack
631 stack:get_meta():set_int("palette_index", palette_index)
632 return stack:to_string()
635 function core.itemstring_with_color(item, colorstring)
636 local stack = ItemStack(item) -- convert to ItemStack
637 stack:get_meta():set_string("color", colorstring)
638 return stack:to_string()
641 -- This is used to allow mods to redefine core.item_place and so on
642 -- NOTE: This is not the preferred way. Preferred way is to provide enough
643 -- callbacks to not require redefining global functions. -celeron55
644 local function redef_wrapper(table, name)
646 return table[name](...)
651 -- Item definition defaults
654 core.nodedef_default = {
657 -- name intentionally not defined here
660 inventory_image = "",
662 wield_scale = {x=1,y=1,z=1},
665 liquids_pointable = false,
666 tool_capabilities = nil,
667 node_placement_prediction = nil,
669 -- Interaction callbacks
670 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
671 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
675 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
677 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
679 on_receive_fields = nil,
681 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
682 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
683 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
688 -- Don't define these because otherwise the old tile_images and
689 -- special_materials wouldn't be read
692 -- {name="", backface_culling=true},
693 -- {name="", backface_culling=true},
696 post_effect_color = {a=0, r=0, g=0, b=0},
699 is_ground_content = true,
700 sunlight_propagates = false,
705 buildable_to = false,
708 liquid_alternative_flowing = "",
709 liquid_alternative_source = "",
710 liquid_viscosity = 0,
713 damage_per_second = 0,
714 selection_box = {type="regular"},
715 legacy_facedir_simple = false,
716 legacy_wallmounted = false,
719 core.craftitemdef_default = {
721 -- name intentionally not defined here
724 inventory_image = "",
726 wield_scale = {x=1,y=1,z=1},
728 liquids_pointable = false,
729 tool_capabilities = nil,
731 -- Interaction callbacks
732 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
733 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
734 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
738 core.tooldef_default = {
740 -- name intentionally not defined here
743 inventory_image = "",
745 wield_scale = {x=1,y=1,z=1},
747 liquids_pointable = false,
748 tool_capabilities = nil,
750 -- Interaction callbacks
751 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
752 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
753 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
757 core.noneitemdef_default = { -- This is used for the hand and unknown items
759 -- name intentionally not defined here
762 inventory_image = "",
764 wield_scale = {x=1,y=1,z=1},
766 liquids_pointable = false,
767 tool_capabilities = nil,
769 -- Interaction callbacks
770 on_place = redef_wrapper(core, 'item_place'),
771 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),