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 % 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.get_node_drops(nodename, toolname)
159 local def = core.registered_nodes[nodename]
160 local drop = def and def.drop
164 elseif type(drop) == "string" then
167 elseif drop.items == nil then
168 -- drop = {} to disable default drop
172 -- Extended drop table
176 for _, item in ipairs(drop.items) do
177 local good_rarity = true
178 local good_tool = true
179 if item.rarity ~= nil then
180 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
182 if item.tools ~= nil then
184 for _, tool in ipairs(item.tools) do
185 if tool:sub(1, 1) == '~' then
186 good_tool = toolname:find(tool:sub(2)) ~= nil
188 good_tool = toolname == tool
195 if good_rarity and good_tool then
196 got_count = got_count + 1
197 for _, add_item in ipairs(item.items) do
198 got_items[#got_items+1] = add_item
200 if drop.max_items ~= nil and got_count == drop.max_items then
208 function core.item_place_node(itemstack, placer, pointed_thing, param2)
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)
218 local playername = placer:get_player_name()
220 if not oldnode_under or not oldnode_above then
221 core.log("info", playername .. " tried to place"
222 .. " node in unloaded position " .. core.pos_to_string(above))
223 return itemstack, false
226 local olddef_under = core.registered_nodes[oldnode_under.name]
227 olddef_under = olddef_under or core.nodedef_default
228 local olddef_above = core.registered_nodes[oldnode_above.name]
229 olddef_above = olddef_above or core.nodedef_default
231 if not olddef_above.buildable_to and not olddef_under.buildable_to then
232 core.log("info", playername .. " tried to place"
233 .. " node in invalid position " .. core.pos_to_string(above)
234 .. ", replacing " .. oldnode_above.name)
235 return itemstack, false
238 -- Place above pointed node
239 local place_to = {x = above.x, y = above.y, z = above.z}
241 -- If node under is buildable_to, place into it instead (eg. snow)
242 if olddef_under.buildable_to then
243 core.log("info", "node under is buildable to")
244 place_to = {x = under.x, y = under.y, z = under.z}
247 if core.is_protected(place_to, playername) and
248 not minetest.check_player_privs(placer, "protection_bypass") then
249 core.log("action", playername
250 .. " tried to place " .. def.name
251 .. " at protected position "
252 .. core.pos_to_string(place_to))
253 core.record_protection_violation(place_to, playername)
257 core.log("action", playername .. " places node "
258 .. def.name .. " at " .. core.pos_to_string(place_to))
260 local oldnode = core.get_node(place_to)
261 local newnode = {name = def.name, param1 = 0, param2 = param2}
263 -- Calculate direction for wall mounted stuff like torches and signs
264 if def.place_param2 ~= nil then
265 newnode.param2 = def.place_param2
266 elseif (def.paramtype2 == "wallmounted" or
267 def.paramtype2 == "colorwallmounted") and not param2 then
269 x = under.x - above.x,
270 y = under.y - above.y,
271 z = under.z - above.z
273 newnode.param2 = core.dir_to_wallmounted(dir)
274 -- Calculate the direction for furnaces and chests and stuff
275 elseif (def.paramtype2 == "facedir" or
276 def.paramtype2 == "colorfacedir") and not param2 then
277 local placer_pos = placer:getpos()
280 x = above.x - placer_pos.x,
281 y = above.y - placer_pos.y,
282 z = above.z - placer_pos.z
284 newnode.param2 = core.dir_to_facedir(dir)
285 core.log("action", "facedir: " .. newnode.param2)
289 -- Check if the node is attached and if it can be placed there
290 if core.get_item_group(def.name, "attached_node") ~= 0 and
291 not builtin_shared.check_attached_node(place_to, newnode) then
292 core.log("action", "attached node " .. def.name ..
293 " can not be placed at " .. core.pos_to_string(place_to))
294 return itemstack, false
297 -- Add node and update
298 core.add_node(place_to, newnode)
300 local take_item = true
303 if def.after_place_node then
304 -- Deepcopy place_to and pointed_thing because callback can modify it
305 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
306 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
307 if def.after_place_node(place_to_copy, placer, itemstack,
308 pointed_thing_copy) then
314 for _, callback in ipairs(core.registered_on_placenodes) do
315 -- Deepcopy pos, node and pointed_thing because callback can modify them
316 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
317 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
318 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
319 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
320 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
326 itemstack:take_item()
328 return itemstack, true
331 function core.item_place_object(itemstack, placer, pointed_thing)
332 local pos = core.get_pointed_thing_position(pointed_thing, true)
334 local item = itemstack:take_item()
335 core.add_item(pos, item)
340 function core.item_place(itemstack, placer, pointed_thing, param2)
341 -- Call on_rightclick if the pointed node defines it
342 if pointed_thing.type == "node" and placer and
343 not placer:get_player_control().sneak then
344 local n = core.get_node(pointed_thing.under)
346 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
347 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
348 placer, itemstack, pointed_thing) or itemstack, false
352 if itemstack:get_definition().type == "node" then
353 return core.item_place_node(itemstack, placer, pointed_thing, param2)
358 function core.item_secondary_use(itemstack, placer)
362 function core.item_drop(itemstack, dropper, pos)
363 if dropper and dropper:is_player() then
364 local v = dropper:get_look_dir()
365 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
366 local cs = itemstack:get_count()
367 if dropper:get_player_control().sneak then
370 local item = itemstack:take_item(cs)
371 local obj = core.add_item(p, item)
377 obj:get_luaentity().dropped_by = dropper:get_player_name()
382 if core.add_item(pos, itemstack) then
386 -- If we reach this, adding the object to the
387 -- environment failed
390 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
391 for _, callback in pairs(core.registered_on_item_eats) do
392 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
397 if itemstack:take_item() ~= nil then
398 user:set_hp(user:get_hp() + hp_change)
400 if replace_with_item then
401 if itemstack:is_empty() then
402 itemstack:add_item(replace_with_item)
404 local inv = user:get_inventory()
405 if inv:room_for_item("main", {name=replace_with_item}) then
406 inv:add_item("main", replace_with_item)
408 local pos = user:getpos()
409 pos.y = math.floor(pos.y + 0.5)
410 core.add_item(pos, replace_with_item)
418 function core.item_eat(hp_change, replace_with_item)
419 return function(itemstack, user, pointed_thing) -- closure
420 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
424 function core.node_punch(pos, node, puncher, pointed_thing)
426 for _, callback in ipairs(core.registered_on_punchnodes) do
427 -- Copy pos and node because callback can modify them
428 local pos_copy = vector.new(pos)
429 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
430 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
431 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
435 function core.handle_node_drops(pos, drops, digger)
436 -- Add dropped items to object's inventory
437 if digger:get_inventory() then
438 local _, dropped_item
439 for _, dropped_item in ipairs(drops) do
440 local left = digger:get_inventory():add_item("main", dropped_item)
441 if not left:is_empty() then
443 x = pos.x + math.random()/2-0.25,
444 y = pos.y + math.random()/2-0.25,
445 z = pos.z + math.random()/2-0.25,
447 core.add_item(p, left)
453 function core.node_dig(pos, node, digger)
454 local def = core.registered_nodes[node.name]
455 if def and (not def.diggable or
456 (def.can_dig and not def.can_dig(pos, digger))) then
457 core.log("info", digger:get_player_name() .. " tried to dig "
458 .. node.name .. " which is not diggable "
459 .. core.pos_to_string(pos))
463 if core.is_protected(pos, digger:get_player_name()) and
464 not minetest.check_player_privs(digger, "protection_bypass") then
465 core.log("action", digger:get_player_name()
466 .. " tried to dig " .. node.name
467 .. " at protected position "
468 .. core.pos_to_string(pos))
469 core.record_protection_violation(pos, digger:get_player_name())
473 core.log('action', digger:get_player_name() .. " digs "
474 .. node.name .. " at " .. core.pos_to_string(pos))
476 local wielded = digger:get_wielded_item()
477 local drops = core.get_node_drops(node.name, wielded:get_name())
479 local wdef = wielded:get_definition()
480 local tp = wielded:get_tool_capabilities()
481 local dp = core.get_dig_params(def and def.groups, tp)
482 if wdef and wdef.after_use then
483 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
486 if not core.setting_getbool("creative_mode") then
487 wielded:add_wear(dp.wear)
488 if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
489 core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
493 digger:set_wielded_item(wielded)
496 core.handle_node_drops(pos, drops, digger)
498 local oldmetadata = nil
499 if def and def.after_dig_node then
500 oldmetadata = core.get_meta(pos):to_table()
503 -- Remove node and update
504 core.remove_node(pos)
507 if def and def.after_dig_node then
508 -- Copy pos and node because callback can modify them
509 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
510 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
511 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
516 for _, callback in ipairs(core.registered_on_dignodes) do
517 local origin = core.callback_origins[callback]
519 core.set_last_run_mod(origin.mod)
520 --print("Running " .. tostring(callback) ..
521 -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
523 --print("No data associated with callback")
526 -- Copy pos and node because callback can modify them
527 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
528 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
529 callback(pos_copy, node_copy, digger)
533 -- This is used to allow mods to redefine core.item_place and so on
534 -- NOTE: This is not the preferred way. Preferred way is to provide enough
535 -- callbacks to not require redefining global functions. -celeron55
536 local function redef_wrapper(table, name)
538 return table[name](...)
543 -- Item definition defaults
546 core.nodedef_default = {
549 -- name intentionally not defined here
552 inventory_image = "",
554 wield_scale = {x=1,y=1,z=1},
557 liquids_pointable = false,
558 tool_capabilities = nil,
559 node_placement_prediction = nil,
561 -- Interaction callbacks
562 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
563 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
567 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
569 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
571 on_receive_fields = nil,
573 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
574 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
575 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
580 -- Don't define these because otherwise the old tile_images and
581 -- special_materials wouldn't be read
584 -- {name="", backface_culling=true},
585 -- {name="", backface_culling=true},
588 post_effect_color = {a=0, r=0, g=0, b=0},
591 is_ground_content = true,
592 sunlight_propagates = false,
597 buildable_to = false,
600 liquid_alternative_flowing = "",
601 liquid_alternative_source = "",
602 liquid_viscosity = 0,
605 damage_per_second = 0,
606 selection_box = {type="regular"},
607 legacy_facedir_simple = false,
608 legacy_wallmounted = false,
611 core.craftitemdef_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
626 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
630 core.tooldef_default = {
632 -- name intentionally not defined here
635 inventory_image = "",
637 wield_scale = {x=1,y=1,z=1},
639 liquids_pointable = false,
640 tool_capabilities = nil,
642 -- Interaction callbacks
643 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
644 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
645 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
649 core.noneitemdef_default = { -- This is used for the hand and unknown items
651 -- name intentionally not defined here
654 inventory_image = "",
656 wield_scale = {x=1,y=1,z=1},
658 liquids_pointable = false,
659 tool_capabilities = nil,
661 -- Interaction callbacks
662 on_place = redef_wrapper(core, 'item_place'),
663 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),