1 -- Minetest: builtin/item.lua
3 local function copy_pointed_thing(pointed_thing)
5 type = pointed_thing.type,
6 above = vector.new(pointed_thing.above),
7 under = vector.new(pointed_thing.under),
8 ref = pointed_thing.ref,
13 -- Item definition helpers
16 function core.inventorycube(img1, img2, img3)
19 return "[inventorycube"
20 .. "{" .. img1:gsub("%^", "&")
21 .. "{" .. img2:gsub("%^", "&")
22 .. "{" .. img3:gsub("%^", "&")
25 function core.get_pointed_thing_position(pointed_thing, above)
26 if pointed_thing.type == "node" then
28 -- The position where a node would be placed
29 return pointed_thing.above
31 -- The position where a node would be dug
32 return pointed_thing.under
34 elseif pointed_thing.type == "object" then
35 obj = pointed_thing.ref
46 function core.dir_to_facedir(dir, is6d)
47 --account for y if requested
48 if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
52 if math.abs(dir.x) > math.abs(dir.z) then
68 if math.abs(dir.x) > math.abs(dir.z) then
83 --otherwise, place horizontally
84 elseif math.abs(dir.x) > math.abs(dir.z) then
99 function core.facedir_to_dir(facedir)
100 --a table of possible dirs
101 return ({{x=0, y=0, z=1},
108 --indexed into by a table of correlating facedirs
116 --indexed into by the facedir in question
120 function core.dir_to_wallmounted(dir)
121 if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
127 elseif math.abs(dir.x) > math.abs(dir.z) then
142 function core.wallmounted_to_dir(wallmounted)
143 -- table of dirs in wallmounted order
144 return ({[0] = {x = 0, y = 1, z = 0},
145 {x = 0, y = -1, z = 0},
146 {x = 1, y = 0, z = 0},
147 {x = -1, y = 0, z = 0},
148 {x = 0, y = 0, z = 1},
149 {x = 0, y = 0, z = -1}})
151 --indexed into by the wallmounted in question
155 function core.get_node_drops(nodename, toolname)
156 local drop = ItemStack({name=nodename}):get_definition().drop
160 elseif type(drop) == "string" then
163 elseif drop.items == nil then
164 -- drop = {} to disable default drop
168 -- Extended drop table
172 for _, item in ipairs(drop.items) do
173 local good_rarity = true
174 local good_tool = true
175 if item.rarity ~= nil then
176 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
178 if item.tools ~= nil then
180 for _, tool in ipairs(item.tools) do
181 if tool:sub(1, 1) == '~' then
182 good_tool = toolname:find(tool:sub(2)) ~= nil
184 good_tool = toolname == tool
191 if good_rarity and good_tool then
192 got_count = got_count + 1
193 for _, add_item in ipairs(item.items) do
194 got_items[#got_items+1] = add_item
196 if drop.max_items ~= nil and got_count == drop.max_items then
204 function core.item_place_node(itemstack, placer, pointed_thing, param2)
205 local item = itemstack:peek_item()
206 local def = itemstack:get_definition()
207 if def.type ~= "node" or pointed_thing.type ~= "node" then
208 return itemstack, false
211 local under = pointed_thing.under
212 local oldnode_under = core.get_node_or_nil(under)
213 local above = pointed_thing.above
214 local oldnode_above = core.get_node_or_nil(above)
216 if not oldnode_under or not oldnode_above then
217 core.log("info", placer:get_player_name() .. " tried to place"
218 .. " node in unloaded position " .. core.pos_to_string(above))
219 return itemstack, false
222 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
223 olddef_under = olddef_under or core.nodedef_default
224 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
225 olddef_above = olddef_above or core.nodedef_default
227 if not olddef_above.buildable_to and not olddef_under.buildable_to then
228 core.log("info", placer:get_player_name() .. " tried to place"
229 .. " node in invalid position " .. core.pos_to_string(above)
230 .. ", replacing " .. oldnode_above.name)
231 return itemstack, false
234 -- Place above pointed node
235 local place_to = {x = above.x, y = above.y, z = above.z}
237 -- If node under is buildable_to, place into it instead (eg. snow)
238 if olddef_under.buildable_to then
239 core.log("info", "node under is buildable to")
240 place_to = {x = under.x, y = under.y, z = under.z}
243 if core.is_protected(place_to, placer:get_player_name()) then
244 core.log("action", placer:get_player_name()
245 .. " tried to place " .. def.name
246 .. " at protected position "
247 .. core.pos_to_string(place_to))
248 core.record_protection_violation(place_to, placer:get_player_name())
252 core.log("action", placer:get_player_name() .. " places node "
253 .. def.name .. " at " .. core.pos_to_string(place_to))
255 local oldnode = core.get_node(place_to)
256 local newnode = {name = def.name, param1 = 0, param2 = param2}
258 -- Calculate direction for wall mounted stuff like torches and signs
259 if def.paramtype2 == 'wallmounted' and not param2 then
261 x = under.x - above.x,
262 y = under.y - above.y,
263 z = under.z - above.z
265 newnode.param2 = core.dir_to_wallmounted(dir)
266 -- Calculate the direction for furnaces and chests and stuff
267 elseif def.paramtype2 == 'facedir' and not param2 then
268 local placer_pos = placer:getpos()
271 x = above.x - placer_pos.x,
272 y = above.y - placer_pos.y,
273 z = above.z - placer_pos.z
275 newnode.param2 = core.dir_to_facedir(dir)
276 core.log("action", "facedir: " .. newnode.param2)
280 -- Check if the node is attached and if it can be placed there
281 if core.get_item_group(def.name, "attached_node") ~= 0 and
282 not check_attached_node(place_to, newnode) then
283 core.log("action", "attached node " .. def.name ..
284 " can not be placed at " .. core.pos_to_string(place_to))
285 return itemstack, false
288 -- Add node and update
289 core.add_node(place_to, newnode)
291 local take_item = true
294 if def.after_place_node then
295 -- Deepcopy place_to and pointed_thing because callback can modify it
296 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
297 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
298 if def.after_place_node(place_to_copy, placer, itemstack,
299 pointed_thing_copy) then
306 for _, callback in ipairs(core.registered_on_placenodes) do
307 -- Deepcopy pos, node and pointed_thing because callback can modify them
308 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
309 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
310 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
311 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
312 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
318 itemstack:take_item()
320 return itemstack, true
323 function core.item_place_object(itemstack, placer, pointed_thing)
324 local pos = core.get_pointed_thing_position(pointed_thing, true)
326 local item = itemstack:take_item()
327 core.add_item(pos, item)
332 function core.item_place(itemstack, placer, pointed_thing, param2)
333 -- Call on_rightclick if the pointed node defines it
334 if pointed_thing.type == "node" and placer and
335 not placer:get_player_control().sneak then
336 local n = core.get_node(pointed_thing.under)
338 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
339 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
340 placer, itemstack, pointed_thing) or itemstack, false
344 if itemstack:get_definition().type == "node" then
345 return core.item_place_node(itemstack, placer, pointed_thing, param2)
350 function core.item_drop(itemstack, dropper, pos)
351 if dropper and dropper:is_player() then
352 local v = dropper:get_look_dir()
353 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
354 local cs = itemstack:get_count()
355 if dropper:get_player_control().sneak then
358 local item = itemstack:take_item(cs)
359 local obj = core.add_item(p, item)
365 obj:get_luaentity().dropped_by = dropper:get_player_name()
370 if core.add_item(pos, itemstack) then
374 -- If we reach this, adding the object to the
375 -- environment failed
378 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
379 for _, callback in pairs(core.registered_on_item_eats) do
380 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
385 if itemstack:take_item() ~= nil then
386 user:set_hp(user:get_hp() + hp_change)
388 if replace_with_item then
389 if itemstack:is_empty() then
390 itemstack:add_item(replace_with_item)
392 local inv = user:get_inventory()
393 if inv:room_for_item("main", {name=replace_with_item}) then
394 inv:add_item("main", replace_with_item)
396 local pos = user:getpos()
397 pos.y = math.floor(pos.y + 0.5)
398 core.add_item(pos, replace_with_item)
406 function core.item_eat(hp_change, replace_with_item)
407 return function(itemstack, user, pointed_thing) -- closure
408 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
412 function core.node_punch(pos, node, puncher, pointed_thing)
414 for _, callback in ipairs(core.registered_on_punchnodes) do
415 -- Copy pos and node because callback can modify them
416 local pos_copy = vector.new(pos)
417 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
418 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
419 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
423 function core.handle_node_drops(pos, drops, digger)
424 -- Add dropped items to object's inventory
425 if digger:get_inventory() then
426 local _, dropped_item
427 for _, dropped_item in ipairs(drops) do
428 local left = digger:get_inventory():add_item("main", dropped_item)
429 if not left:is_empty() then
431 x = pos.x + math.random()/2-0.25,
432 y = pos.y + math.random()/2-0.25,
433 z = pos.z + math.random()/2-0.25,
435 core.add_item(p, left)
441 function core.node_dig(pos, node, digger)
442 local def = ItemStack({name=node.name}):get_definition()
443 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
444 core.log("info", digger:get_player_name() .. " tried to dig "
445 .. node.name .. " which is not diggable "
446 .. core.pos_to_string(pos))
450 if core.is_protected(pos, digger:get_player_name()) then
451 core.log("action", digger:get_player_name()
452 .. " tried to dig " .. node.name
453 .. " at protected position "
454 .. core.pos_to_string(pos))
455 core.record_protection_violation(pos, digger:get_player_name())
459 core.log('action', digger:get_player_name() .. " digs "
460 .. node.name .. " at " .. core.pos_to_string(pos))
462 local wielded = digger:get_wielded_item()
463 local drops = core.get_node_drops(node.name, wielded:get_name())
465 local wdef = wielded:get_definition()
466 local tp = wielded:get_tool_capabilities()
467 local dp = core.get_dig_params(def.groups, tp)
468 if wdef and wdef.after_use then
469 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
472 if not core.setting_getbool("creative_mode") then
473 wielded:add_wear(dp.wear)
476 digger:set_wielded_item(wielded)
479 core.handle_node_drops(pos, drops, digger)
481 local oldmetadata = nil
482 if def.after_dig_node then
483 oldmetadata = core.get_meta(pos):to_table()
486 -- Remove node and update
487 core.remove_node(pos)
490 if def.after_dig_node then
491 -- Copy pos and node because callback can modify them
492 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
493 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
494 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
499 for _, callback in ipairs(core.registered_on_dignodes) do
500 local origin = core.callback_origins[callback]
502 core.set_last_run_mod(origin.mod)
503 --print("Running " .. tostring(callback) ..
504 -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
506 --print("No data associated with callback")
509 -- Copy pos and node because callback can modify them
510 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
511 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
512 callback(pos_copy, node_copy, digger)
516 -- This is used to allow mods to redefine core.item_place and so on
517 -- NOTE: This is not the preferred way. Preferred way is to provide enough
518 -- callbacks to not require redefining global functions. -celeron55
519 local function redef_wrapper(table, name)
521 return table[name](...)
526 -- Item definition defaults
529 core.nodedef_default = {
532 -- name intentionally not defined here
535 inventory_image = "",
537 wield_scale = {x=1,y=1,z=1},
540 liquids_pointable = false,
541 tool_capabilities = nil,
542 node_placement_prediction = nil,
544 -- Interaction callbacks
545 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
546 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
550 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
552 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
554 on_receive_fields = nil,
556 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
557 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
558 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
563 -- Don't define these because otherwise the old tile_images and
564 -- special_materials wouldn't be read
567 -- {name="", backface_culling=true},
568 -- {name="", backface_culling=true},
571 post_effect_color = {a=0, r=0, g=0, b=0},
574 is_ground_content = true,
575 sunlight_propagates = false,
580 buildable_to = false,
582 liquid_alternative_flowing = "",
583 liquid_alternative_source = "",
584 liquid_viscosity = 0,
587 damage_per_second = 0,
588 selection_box = {type="regular"},
589 legacy_facedir_simple = false,
590 legacy_wallmounted = false,
593 core.craftitemdef_default = {
595 -- name intentionally not defined here
598 inventory_image = "",
600 wield_scale = {x=1,y=1,z=1},
602 liquids_pointable = false,
603 tool_capabilities = nil,
605 -- Interaction callbacks
606 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
607 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
611 core.tooldef_default = {
613 -- name intentionally not defined here
616 inventory_image = "",
618 wield_scale = {x=1,y=1,z=1},
620 liquids_pointable = false,
621 tool_capabilities = nil,
623 -- Interaction callbacks
624 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
625 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
629 core.noneitemdef_default = { -- This is used for the hand and unknown items
631 -- name intentionally not defined here
634 inventory_image = "",
636 wield_scale = {x=1,y=1,z=1},
638 liquids_pointable = false,
639 tool_capabilities = nil,
641 -- Interaction callbacks
642 on_place = redef_wrapper(core, 'item_place'),