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.get_node_drops(nodename, toolname)
143 local drop = ItemStack({name=nodename}):get_definition().drop
147 elseif type(drop) == "string" then
150 elseif drop.items == nil then
151 -- drop = {} to disable default drop
155 -- Extended drop table
159 for _, item in ipairs(drop.items) do
160 local good_rarity = true
161 local good_tool = true
162 if item.rarity ~= nil then
163 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
165 if item.tools ~= nil then
167 for _, tool in ipairs(item.tools) do
168 if tool:sub(1, 1) == '~' then
169 good_tool = toolname:find(tool:sub(2)) ~= nil
171 good_tool = toolname == tool
178 if good_rarity and good_tool then
179 got_count = got_count + 1
180 for _, add_item in ipairs(item.items) do
181 got_items[#got_items+1] = add_item
183 if drop.max_items ~= nil and got_count == drop.max_items then
191 function core.item_place_node(itemstack, placer, pointed_thing, param2)
192 local item = itemstack:peek_item()
193 local def = itemstack:get_definition()
194 if def.type ~= "node" or pointed_thing.type ~= "node" then
195 return itemstack, false
198 local under = pointed_thing.under
199 local oldnode_under = core.get_node_or_nil(under)
200 local above = pointed_thing.above
201 local oldnode_above = core.get_node_or_nil(above)
203 if not oldnode_under or not oldnode_above then
204 core.log("info", placer:get_player_name() .. " tried to place"
205 .. " node in unloaded position " .. core.pos_to_string(above))
206 return itemstack, false
209 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
210 olddef_under = olddef_under or core.nodedef_default
211 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
212 olddef_above = olddef_above or core.nodedef_default
214 if not olddef_above.buildable_to and not olddef_under.buildable_to then
215 core.log("info", placer:get_player_name() .. " tried to place"
216 .. " node in invalid position " .. core.pos_to_string(above)
217 .. ", replacing " .. oldnode_above.name)
218 return itemstack, false
221 -- Place above pointed node
222 local place_to = {x = above.x, y = above.y, z = above.z}
224 -- If node under is buildable_to, place into it instead (eg. snow)
225 if olddef_under.buildable_to then
226 core.log("info", "node under is buildable to")
227 place_to = {x = under.x, y = under.y, z = under.z}
230 if core.is_protected(place_to, placer:get_player_name()) then
231 core.log("action", placer:get_player_name()
232 .. " tried to place " .. def.name
233 .. " at protected position "
234 .. core.pos_to_string(place_to))
235 core.record_protection_violation(place_to, placer:get_player_name())
239 core.log("action", placer:get_player_name() .. " places node "
240 .. def.name .. " at " .. core.pos_to_string(place_to))
242 local oldnode = core.get_node(place_to)
243 local newnode = {name = def.name, param1 = 0, param2 = param2}
245 -- Calculate direction for wall mounted stuff like torches and signs
246 if def.paramtype2 == 'wallmounted' and not param2 then
248 x = under.x - above.x,
249 y = under.y - above.y,
250 z = under.z - above.z
252 newnode.param2 = core.dir_to_wallmounted(dir)
253 -- Calculate the direction for furnaces and chests and stuff
254 elseif def.paramtype2 == 'facedir' and not param2 then
255 local placer_pos = placer:getpos()
258 x = above.x - placer_pos.x,
259 y = above.y - placer_pos.y,
260 z = above.z - placer_pos.z
262 newnode.param2 = core.dir_to_facedir(dir)
263 core.log("action", "facedir: " .. newnode.param2)
267 -- Check if the node is attached and if it can be placed there
268 if core.get_item_group(def.name, "attached_node") ~= 0 and
269 not check_attached_node(place_to, newnode) then
270 core.log("action", "attached node " .. def.name ..
271 " can not be placed at " .. core.pos_to_string(place_to))
272 return itemstack, false
275 -- Add node and update
276 core.add_node(place_to, newnode)
278 local take_item = true
281 if def.after_place_node then
282 -- Deepcopy place_to and pointed_thing because callback can modify it
283 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
284 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
285 if def.after_place_node(place_to_copy, placer, itemstack,
286 pointed_thing_copy) then
293 for _, callback in ipairs(core.registered_on_placenodes) do
294 -- Deepcopy pos, node and pointed_thing because callback can modify them
295 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
296 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
297 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
298 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
299 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
305 itemstack:take_item()
307 return itemstack, true
310 function core.item_place_object(itemstack, placer, pointed_thing)
311 local pos = core.get_pointed_thing_position(pointed_thing, true)
313 local item = itemstack:take_item()
314 core.add_item(pos, item)
319 function core.item_place(itemstack, placer, pointed_thing, param2)
320 -- Call on_rightclick if the pointed node defines it
321 if pointed_thing.type == "node" and placer and
322 not placer:get_player_control().sneak then
323 local n = core.get_node(pointed_thing.under)
325 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
326 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
327 placer, itemstack, pointed_thing) or itemstack, false
331 if itemstack:get_definition().type == "node" then
332 return core.item_place_node(itemstack, placer, pointed_thing, param2)
337 function core.item_drop(itemstack, dropper, pos)
338 if dropper.is_player then
339 local v = dropper:get_look_dir()
340 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
341 local cs = itemstack:get_count()
342 if dropper:get_player_control().sneak then
345 local item = itemstack:take_item(cs)
346 local obj = core.add_item(p, item)
355 core.add_item(pos, itemstack)
360 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
361 for _, callback in pairs(core.registered_on_item_eats) do
362 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
367 if itemstack:take_item() ~= nil then
368 user:set_hp(user:get_hp() + hp_change)
370 if replace_with_item then
371 if itemstack:is_empty() then
372 itemstack:add_item(replace_with_item)
374 local inv = user:get_inventory()
375 if inv:room_for_item("main", {name=replace_with_item}) then
376 inv:add_item("main", replace_with_item)
378 local pos = user:getpos()
379 pos.y = math.floor(pos.y + 0.5)
380 core.add_item(pos, replace_with_item)
388 function core.item_eat(hp_change, replace_with_item)
389 return function(itemstack, user, pointed_thing) -- closure
390 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
394 function core.node_punch(pos, node, puncher, pointed_thing)
396 for _, callback in ipairs(core.registered_on_punchnodes) do
397 -- Copy pos and node because callback can modify them
398 local pos_copy = vector.new(pos)
399 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
400 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
401 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
405 function core.handle_node_drops(pos, drops, digger)
406 -- Add dropped items to object's inventory
407 if digger:get_inventory() then
408 local _, dropped_item
409 for _, dropped_item in ipairs(drops) do
410 local left = digger:get_inventory():add_item("main", dropped_item)
411 if not left:is_empty() then
413 x = pos.x + math.random()/2-0.25,
414 y = pos.y + math.random()/2-0.25,
415 z = pos.z + math.random()/2-0.25,
417 core.add_item(p, left)
423 function core.node_dig(pos, node, digger)
424 local def = ItemStack({name=node.name}):get_definition()
425 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
426 core.log("info", digger:get_player_name() .. " tried to dig "
427 .. node.name .. " which is not diggable "
428 .. core.pos_to_string(pos))
432 if core.is_protected(pos, digger:get_player_name()) then
433 core.log("action", digger:get_player_name()
434 .. " tried to dig " .. node.name
435 .. " at protected position "
436 .. core.pos_to_string(pos))
437 core.record_protection_violation(pos, digger:get_player_name())
441 core.log('action', digger:get_player_name() .. " digs "
442 .. node.name .. " at " .. core.pos_to_string(pos))
444 local wielded = digger:get_wielded_item()
445 local drops = core.get_node_drops(node.name, wielded:get_name())
447 local wdef = wielded:get_definition()
448 local tp = wielded:get_tool_capabilities()
449 local dp = core.get_dig_params(def.groups, tp)
450 if wdef and wdef.after_use then
451 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
454 if not core.setting_getbool("creative_mode") then
455 wielded:add_wear(dp.wear)
458 digger:set_wielded_item(wielded)
461 core.handle_node_drops(pos, drops, digger)
463 local oldmetadata = nil
464 if def.after_dig_node then
465 oldmetadata = core.get_meta(pos):to_table()
468 -- Remove node and update
469 core.remove_node(pos)
472 if def.after_dig_node then
473 -- Copy pos and node because callback can modify them
474 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
475 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
476 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
481 for _, callback in ipairs(core.registered_on_dignodes) do
482 -- Copy pos and node because callback can modify them
483 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
484 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
485 callback(pos_copy, node_copy, digger)
489 -- This is used to allow mods to redefine core.item_place and so on
490 -- NOTE: This is not the preferred way. Preferred way is to provide enough
491 -- callbacks to not require redefining global functions. -celeron55
492 local function redef_wrapper(table, name)
494 return table[name](...)
499 -- Item definition defaults
502 core.nodedef_default = {
505 -- name intentionally not defined here
508 inventory_image = "",
510 wield_scale = {x=1,y=1,z=1},
513 liquids_pointable = false,
514 tool_capabilities = nil,
515 node_placement_prediction = nil,
517 -- Interaction callbacks
518 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
519 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
523 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
525 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
527 on_receive_fields = nil,
529 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
530 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
531 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
536 -- Don't define these because otherwise the old tile_images and
537 -- special_materials wouldn't be read
540 -- {name="", backface_culling=true},
541 -- {name="", backface_culling=true},
544 post_effect_color = {a=0, r=0, g=0, b=0},
547 is_ground_content = true,
548 sunlight_propagates = false,
553 buildable_to = false,
555 liquid_alternative_flowing = "",
556 liquid_alternative_source = "",
557 liquid_viscosity = 0,
560 damage_per_second = 0,
561 selection_box = {type="regular"},
562 legacy_facedir_simple = false,
563 legacy_wallmounted = false,
566 core.craftitemdef_default = {
568 -- name intentionally not defined here
571 inventory_image = "",
573 wield_scale = {x=1,y=1,z=1},
575 liquids_pointable = false,
576 tool_capabilities = nil,
578 -- Interaction callbacks
579 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
580 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
584 core.tooldef_default = {
586 -- name intentionally not defined here
589 inventory_image = "",
591 wield_scale = {x=1,y=1,z=1},
593 liquids_pointable = false,
594 tool_capabilities = nil,
596 -- Interaction callbacks
597 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
598 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
602 core.noneitemdef_default = { -- This is used for the hand and unknown items
604 -- name intentionally not defined here
607 inventory_image = "",
609 wield_scale = {x=1,y=1,z=1},
611 liquids_pointable = false,
612 tool_capabilities = nil,
614 -- Interaction callbacks
615 on_place = redef_wrapper(core, 'item_place'),