item.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. -- Minetest: builtin/item.lua
  2. local function copy_pointed_thing(pointed_thing)
  3. return {
  4. type = pointed_thing.type,
  5. above = vector.new(pointed_thing.above),
  6. under = vector.new(pointed_thing.under),
  7. ref = pointed_thing.ref,
  8. }
  9. end
  10. --
  11. -- Item definition helpers
  12. --
  13. function core.inventorycube(img1, img2, img3)
  14. img2 = img2 or img1
  15. img3 = img3 or img1
  16. return "[inventorycube"
  17. .. "{" .. img1:gsub("%^", "&")
  18. .. "{" .. img2:gsub("%^", "&")
  19. .. "{" .. img3:gsub("%^", "&")
  20. end
  21. function core.get_pointed_thing_position(pointed_thing, above)
  22. if pointed_thing.type == "node" then
  23. if above then
  24. -- The position where a node would be placed
  25. return pointed_thing.above
  26. end
  27. -- The position where a node would be dug
  28. return pointed_thing.under
  29. elseif pointed_thing.type == "object" then
  30. return pointed_thing.ref and pointed_thing.ref:getpos()
  31. end
  32. end
  33. function core.dir_to_facedir(dir, is6d)
  34. --account for y if requested
  35. if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
  36. --from above
  37. if dir.y < 0 then
  38. if math.abs(dir.x) > math.abs(dir.z) then
  39. if dir.x < 0 then
  40. return 19
  41. else
  42. return 13
  43. end
  44. else
  45. if dir.z < 0 then
  46. return 10
  47. else
  48. return 4
  49. end
  50. end
  51. --from below
  52. else
  53. if math.abs(dir.x) > math.abs(dir.z) then
  54. if dir.x < 0 then
  55. return 15
  56. else
  57. return 17
  58. end
  59. else
  60. if dir.z < 0 then
  61. return 6
  62. else
  63. return 8
  64. end
  65. end
  66. end
  67. --otherwise, place horizontally
  68. elseif math.abs(dir.x) > math.abs(dir.z) then
  69. if dir.x < 0 then
  70. return 3
  71. else
  72. return 1
  73. end
  74. else
  75. if dir.z < 0 then
  76. return 2
  77. else
  78. return 0
  79. end
  80. end
  81. end
  82. -- Table of possible dirs
  83. local facedir_to_dir = {
  84. {x= 0, y=0, z= 1},
  85. {x= 1, y=0, z= 0},
  86. {x= 0, y=0, z=-1},
  87. {x=-1, y=0, z= 0},
  88. {x= 0, y=-1, z= 0},
  89. {x= 0, y=1, z= 0},
  90. }
  91. -- Mapping from facedir value to index in facedir_to_dir.
  92. local facedir_to_dir_map = {
  93. [0]=1, 2, 3, 4,
  94. 5, 2, 6, 4,
  95. 6, 2, 5, 4,
  96. 1, 5, 3, 6,
  97. 1, 6, 3, 5,
  98. 1, 4, 3, 2,
  99. }
  100. function core.facedir_to_dir(facedir)
  101. return facedir_to_dir[facedir_to_dir_map[facedir]]
  102. end
  103. function core.dir_to_wallmounted(dir)
  104. if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
  105. if dir.y < 0 then
  106. return 1
  107. else
  108. return 0
  109. end
  110. elseif math.abs(dir.x) > math.abs(dir.z) then
  111. if dir.x < 0 then
  112. return 3
  113. else
  114. return 2
  115. end
  116. else
  117. if dir.z < 0 then
  118. return 5
  119. else
  120. return 4
  121. end
  122. end
  123. end
  124. -- table of dirs in wallmounted order
  125. local wallmounted_to_dir = {
  126. [0] = {x = 0, y = 1, z = 0},
  127. {x = 0, y = -1, z = 0},
  128. {x = 1, y = 0, z = 0},
  129. {x = -1, y = 0, z = 0},
  130. {x = 0, y = 0, z = 1},
  131. {x = 0, y = 0, z = -1},
  132. }
  133. function core.wallmounted_to_dir(wallmounted)
  134. return wallmounted_to_dir[wallmounted]
  135. end
  136. function core.get_node_drops(nodename, toolname)
  137. local drop = ItemStack({name=nodename}):get_definition().drop
  138. if drop == nil then
  139. -- default drop
  140. return {nodename}
  141. elseif type(drop) == "string" then
  142. -- itemstring drop
  143. return {drop}
  144. elseif drop.items == nil then
  145. -- drop = {} to disable default drop
  146. return {}
  147. end
  148. -- Extended drop table
  149. local got_items = {}
  150. local got_count = 0
  151. local _, item, tool
  152. for _, item in ipairs(drop.items) do
  153. local good_rarity = true
  154. local good_tool = true
  155. if item.rarity ~= nil then
  156. good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
  157. end
  158. if item.tools ~= nil then
  159. good_tool = false
  160. for _, tool in ipairs(item.tools) do
  161. if tool:sub(1, 1) == '~' then
  162. good_tool = toolname:find(tool:sub(2)) ~= nil
  163. else
  164. good_tool = toolname == tool
  165. end
  166. if good_tool then
  167. break
  168. end
  169. end
  170. end
  171. if good_rarity and good_tool then
  172. got_count = got_count + 1
  173. for _, add_item in ipairs(item.items) do
  174. got_items[#got_items+1] = add_item
  175. end
  176. if drop.max_items ~= nil and got_count == drop.max_items then
  177. break
  178. end
  179. end
  180. end
  181. return got_items
  182. end
  183. function core.item_place_node(itemstack, placer, pointed_thing, param2)
  184. local item = itemstack:peek_item()
  185. local def = itemstack:get_definition()
  186. if def.type ~= "node" or pointed_thing.type ~= "node" then
  187. return itemstack, false
  188. end
  189. local under = pointed_thing.under
  190. local oldnode_under = core.get_node_or_nil(under)
  191. local above = pointed_thing.above
  192. local oldnode_above = core.get_node_or_nil(above)
  193. if not oldnode_under or not oldnode_above then
  194. core.log("info", placer:get_player_name() .. " tried to place"
  195. .. " node in unloaded position " .. core.pos_to_string(above))
  196. return itemstack, false
  197. end
  198. local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
  199. olddef_under = olddef_under or core.nodedef_default
  200. local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
  201. olddef_above = olddef_above or core.nodedef_default
  202. if not olddef_above.buildable_to and not olddef_under.buildable_to then
  203. core.log("info", placer:get_player_name() .. " tried to place"
  204. .. " node in invalid position " .. core.pos_to_string(above)
  205. .. ", replacing " .. oldnode_above.name)
  206. return itemstack, false
  207. end
  208. -- Place above pointed node
  209. local place_to = {x = above.x, y = above.y, z = above.z}
  210. -- If node under is buildable_to, place into it instead (eg. snow)
  211. if olddef_under.buildable_to then
  212. core.log("info", "node under is buildable to")
  213. place_to = {x = under.x, y = under.y, z = under.z}
  214. end
  215. if core.is_protected(place_to, placer:get_player_name()) then
  216. core.log("action", placer:get_player_name()
  217. .. " tried to place " .. def.name
  218. .. " at protected position "
  219. .. core.pos_to_string(place_to))
  220. core.record_protection_violation(place_to, placer:get_player_name())
  221. return itemstack
  222. end
  223. core.log("action", placer:get_player_name() .. " places node "
  224. .. def.name .. " at " .. core.pos_to_string(place_to))
  225. local oldnode = core.get_node(place_to)
  226. local newnode = {name = def.name, param1 = 0, param2 = param2}
  227. -- Calculate direction for wall mounted stuff like torches and signs
  228. if def.paramtype2 == 'wallmounted' and not param2 then
  229. local dir = {
  230. x = under.x - above.x,
  231. y = under.y - above.y,
  232. z = under.z - above.z
  233. }
  234. newnode.param2 = core.dir_to_wallmounted(dir)
  235. -- Calculate the direction for furnaces and chests and stuff
  236. elseif def.paramtype2 == 'facedir' and not param2 then
  237. local placer_pos = placer:getpos()
  238. if placer_pos then
  239. local dir = {
  240. x = above.x - placer_pos.x,
  241. y = above.y - placer_pos.y,
  242. z = above.z - placer_pos.z
  243. }
  244. newnode.param2 = core.dir_to_facedir(dir)
  245. core.log("action", "facedir: " .. newnode.param2)
  246. end
  247. end
  248. -- Check if the node is attached and if it can be placed there
  249. if core.get_item_group(def.name, "attached_node") ~= 0 and
  250. not check_attached_node(place_to, newnode) then
  251. core.log("action", "attached node " .. def.name ..
  252. " can not be placed at " .. core.pos_to_string(place_to))
  253. return itemstack, false
  254. end
  255. -- Add node and update
  256. core.add_node(place_to, newnode)
  257. local take_item = true
  258. -- Run callback
  259. if def.after_place_node then
  260. -- Deepcopy place_to and pointed_thing because callback can modify it
  261. local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
  262. local pointed_thing_copy = copy_pointed_thing(pointed_thing)
  263. if def.after_place_node(place_to_copy, placer, itemstack,
  264. pointed_thing_copy) then
  265. take_item = false
  266. end
  267. end
  268. -- Run script hook
  269. local _, callback
  270. for _, callback in ipairs(core.registered_on_placenodes) do
  271. -- Deepcopy pos, node and pointed_thing because callback can modify them
  272. local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
  273. local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
  274. local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
  275. local pointed_thing_copy = copy_pointed_thing(pointed_thing)
  276. if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
  277. take_item = false
  278. end
  279. end
  280. if take_item then
  281. itemstack:take_item()
  282. end
  283. return itemstack, true
  284. end
  285. function core.item_place_object(itemstack, placer, pointed_thing)
  286. local pos = core.get_pointed_thing_position(pointed_thing, true)
  287. if pos ~= nil then
  288. local item = itemstack:take_item()
  289. core.add_item(pos, item)
  290. end
  291. return itemstack
  292. end
  293. function core.item_place(itemstack, placer, pointed_thing, param2)
  294. -- Call on_rightclick if the pointed node defines it
  295. if pointed_thing.type == "node" and placer and
  296. not placer:get_player_control().sneak then
  297. local n = core.get_node(pointed_thing.under)
  298. local nn = n.name
  299. if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
  300. return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
  301. placer, itemstack, pointed_thing) or itemstack, false
  302. end
  303. end
  304. if itemstack:get_definition().type == "node" then
  305. return core.item_place_node(itemstack, placer, pointed_thing, param2)
  306. end
  307. return itemstack
  308. end
  309. function core.item_secondary_use(itemstack, placer)
  310. return itemstack
  311. end
  312. function core.item_drop(itemstack, dropper, pos)
  313. if dropper and dropper:is_player() then
  314. local v = dropper:get_look_dir()
  315. local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
  316. local cs = itemstack:get_count()
  317. if dropper:get_player_control().sneak then
  318. cs = 1
  319. end
  320. local item = itemstack:take_item(cs)
  321. local obj = core.add_item(p, item)
  322. if obj then
  323. v.x = v.x*2
  324. v.y = v.y*2 + 2
  325. v.z = v.z*2
  326. obj:setvelocity(v)
  327. obj:get_luaentity().dropped_by = dropper:get_player_name()
  328. return itemstack
  329. end
  330. else
  331. if core.add_item(pos, itemstack) then
  332. return itemstack
  333. end
  334. end
  335. -- If we reach this, adding the object to the
  336. -- environment failed
  337. end
  338. function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  339. for _, callback in pairs(core.registered_on_item_eats) do
  340. local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
  341. if result then
  342. return result
  343. end
  344. end
  345. if itemstack:take_item() ~= nil then
  346. user:set_hp(user:get_hp() + hp_change)
  347. if replace_with_item then
  348. if itemstack:is_empty() then
  349. itemstack:add_item(replace_with_item)
  350. else
  351. local inv = user:get_inventory()
  352. if inv:room_for_item("main", {name=replace_with_item}) then
  353. inv:add_item("main", replace_with_item)
  354. else
  355. local pos = user:getpos()
  356. pos.y = math.floor(pos.y + 0.5)
  357. core.add_item(pos, replace_with_item)
  358. end
  359. end
  360. end
  361. end
  362. return itemstack
  363. end
  364. function core.item_eat(hp_change, replace_with_item)
  365. return function(itemstack, user, pointed_thing) -- closure
  366. return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  367. end
  368. end
  369. function core.node_punch(pos, node, puncher, pointed_thing)
  370. -- Run script hook
  371. for _, callback in ipairs(core.registered_on_punchnodes) do
  372. -- Copy pos and node because callback can modify them
  373. local pos_copy = vector.new(pos)
  374. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  375. local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
  376. callback(pos_copy, node_copy, puncher, pointed_thing_copy)
  377. end
  378. end
  379. function core.handle_node_drops(pos, drops, digger)
  380. -- Add dropped items to object's inventory
  381. if digger:get_inventory() then
  382. local _, dropped_item
  383. for _, dropped_item in ipairs(drops) do
  384. local left = digger:get_inventory():add_item("main", dropped_item)
  385. if not left:is_empty() then
  386. local p = {
  387. x = pos.x + math.random()/2-0.25,
  388. y = pos.y + math.random()/2-0.25,
  389. z = pos.z + math.random()/2-0.25,
  390. }
  391. core.add_item(p, left)
  392. end
  393. end
  394. end
  395. end
  396. function core.node_dig(pos, node, digger)
  397. local def = ItemStack({name=node.name}):get_definition()
  398. if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
  399. core.log("info", digger:get_player_name() .. " tried to dig "
  400. .. node.name .. " which is not diggable "
  401. .. core.pos_to_string(pos))
  402. return
  403. end
  404. if core.is_protected(pos, digger:get_player_name()) then
  405. core.log("action", digger:get_player_name()
  406. .. " tried to dig " .. node.name
  407. .. " at protected position "
  408. .. core.pos_to_string(pos))
  409. core.record_protection_violation(pos, digger:get_player_name())
  410. return
  411. end
  412. core.log('action', digger:get_player_name() .. " digs "
  413. .. node.name .. " at " .. core.pos_to_string(pos))
  414. local wielded = digger:get_wielded_item()
  415. local drops = core.get_node_drops(node.name, wielded:get_name())
  416. local wdef = wielded:get_definition()
  417. local tp = wielded:get_tool_capabilities()
  418. local dp = core.get_dig_params(def.groups, tp)
  419. if wdef and wdef.after_use then
  420. wielded = wdef.after_use(wielded, digger, node, dp) or wielded
  421. else
  422. -- Wear out tool
  423. if not core.setting_getbool("creative_mode") then
  424. wielded:add_wear(dp.wear)
  425. end
  426. end
  427. digger:set_wielded_item(wielded)
  428. -- Handle drops
  429. core.handle_node_drops(pos, drops, digger)
  430. local oldmetadata = nil
  431. if def.after_dig_node then
  432. oldmetadata = core.get_meta(pos):to_table()
  433. end
  434. -- Remove node and update
  435. core.remove_node(pos)
  436. -- Run callback
  437. if def.after_dig_node then
  438. -- Copy pos and node because callback can modify them
  439. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  440. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  441. def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
  442. end
  443. -- Run script hook
  444. local _, callback
  445. for _, callback in ipairs(core.registered_on_dignodes) do
  446. local origin = core.callback_origins[callback]
  447. if origin then
  448. core.set_last_run_mod(origin.mod)
  449. --print("Running " .. tostring(callback) ..
  450. -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
  451. else
  452. --print("No data associated with callback")
  453. end
  454. -- Copy pos and node because callback can modify them
  455. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  456. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  457. callback(pos_copy, node_copy, digger)
  458. end
  459. end
  460. -- This is used to allow mods to redefine core.item_place and so on
  461. -- NOTE: This is not the preferred way. Preferred way is to provide enough
  462. -- callbacks to not require redefining global functions. -celeron55
  463. local function redef_wrapper(table, name)
  464. return function(...)
  465. return table[name](...)
  466. end
  467. end
  468. --
  469. -- Item definition defaults
  470. --
  471. core.nodedef_default = {
  472. -- Item properties
  473. type="node",
  474. -- name intentionally not defined here
  475. description = "",
  476. groups = {},
  477. inventory_image = "",
  478. wield_image = "",
  479. wield_scale = {x=1,y=1,z=1},
  480. stack_max = 99,
  481. usable = false,
  482. liquids_pointable = false,
  483. tool_capabilities = nil,
  484. node_placement_prediction = nil,
  485. -- Interaction callbacks
  486. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  487. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  488. on_use = nil,
  489. can_dig = nil,
  490. on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
  491. on_rightclick = nil,
  492. on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
  493. on_receive_fields = nil,
  494. on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
  495. on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
  496. on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
  497. -- Node properties
  498. drawtype = "normal",
  499. visual_scale = 1.0,
  500. -- Don't define these because otherwise the old tile_images and
  501. -- special_materials wouldn't be read
  502. --tiles ={""},
  503. --special_tiles = {
  504. -- {name="", backface_culling=true},
  505. -- {name="", backface_culling=true},
  506. --},
  507. alpha = 255,
  508. post_effect_color = {a=0, r=0, g=0, b=0},
  509. paramtype = "none",
  510. paramtype2 = "none",
  511. is_ground_content = true,
  512. sunlight_propagates = false,
  513. walkable = true,
  514. pointable = true,
  515. diggable = true,
  516. climbable = false,
  517. buildable_to = false,
  518. floodable = false,
  519. liquidtype = "none",
  520. liquid_alternative_flowing = "",
  521. liquid_alternative_source = "",
  522. liquid_viscosity = 0,
  523. drowning = 0,
  524. light_source = 0,
  525. damage_per_second = 0,
  526. selection_box = {type="regular"},
  527. legacy_facedir_simple = false,
  528. legacy_wallmounted = false,
  529. }
  530. core.craftitemdef_default = {
  531. type="craft",
  532. -- name intentionally not defined here
  533. description = "",
  534. groups = {},
  535. inventory_image = "",
  536. wield_image = "",
  537. wield_scale = {x=1,y=1,z=1},
  538. stack_max = 99,
  539. liquids_pointable = false,
  540. tool_capabilities = nil,
  541. -- Interaction callbacks
  542. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  543. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  544. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  545. on_use = nil,
  546. }
  547. core.tooldef_default = {
  548. type="tool",
  549. -- name intentionally not defined here
  550. description = "",
  551. groups = {},
  552. inventory_image = "",
  553. wield_image = "",
  554. wield_scale = {x=1,y=1,z=1},
  555. stack_max = 1,
  556. liquids_pointable = false,
  557. tool_capabilities = nil,
  558. -- Interaction callbacks
  559. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  560. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  561. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  562. on_use = nil,
  563. }
  564. core.noneitemdef_default = { -- This is used for the hand and unknown items
  565. type="none",
  566. -- name intentionally not defined here
  567. description = "",
  568. groups = {},
  569. inventory_image = "",
  570. wield_image = "",
  571. wield_scale = {x=1,y=1,z=1},
  572. stack_max = 99,
  573. liquids_pointable = false,
  574. tool_capabilities = nil,
  575. -- Interaction callbacks
  576. on_place = redef_wrapper(core, 'item_place'),
  577. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  578. on_drop = nil,
  579. on_use = nil,
  580. }