item.lua 16 KB

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