item.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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.get_player_name then
  304. local v = dropper:get_look_dir()
  305. local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
  306. local obj = core.add_item(p, itemstack)
  307. if obj then
  308. v.x = v.x*2
  309. v.y = v.y*2 + 1
  310. v.z = v.z*2
  311. obj:setvelocity(v)
  312. end
  313. else
  314. core.add_item(pos, itemstack)
  315. end
  316. return ItemStack("")
  317. end
  318. function core.item_eat(hp_change, replace_with_item)
  319. return function(itemstack, user, pointed_thing) -- closure
  320. if itemstack:take_item() ~= nil then
  321. user:set_hp(user:get_hp() + hp_change)
  322. itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
  323. end
  324. return itemstack
  325. end
  326. end
  327. function core.node_punch(pos, node, puncher, pointed_thing)
  328. -- Run script hook
  329. for _, callback in ipairs(core.registered_on_punchnodes) do
  330. -- Copy pos and node because callback can modify them
  331. local pos_copy = vector.new(pos)
  332. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  333. local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
  334. callback(pos_copy, node_copy, puncher, pointed_thing_copy)
  335. end
  336. end
  337. function core.handle_node_drops(pos, drops, digger)
  338. -- Add dropped items to object's inventory
  339. if digger:get_inventory() then
  340. local _, dropped_item
  341. for _, dropped_item in ipairs(drops) do
  342. local left = digger:get_inventory():add_item("main", dropped_item)
  343. if not left:is_empty() then
  344. local p = {
  345. x = pos.x + math.random()/2-0.25,
  346. y = pos.y + math.random()/2-0.25,
  347. z = pos.z + math.random()/2-0.25,
  348. }
  349. core.add_item(p, left)
  350. end
  351. end
  352. end
  353. end
  354. function core.node_dig(pos, node, digger)
  355. local def = ItemStack({name=node.name}):get_definition()
  356. if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
  357. core.log("info", digger:get_player_name() .. " tried to dig "
  358. .. node.name .. " which is not diggable "
  359. .. core.pos_to_string(pos))
  360. return
  361. end
  362. if core.is_protected(pos, digger:get_player_name()) then
  363. core.log("action", digger:get_player_name()
  364. .. " tried to dig " .. node.name
  365. .. " at protected position "
  366. .. core.pos_to_string(pos))
  367. core.record_protection_violation(pos, digger:get_player_name())
  368. return
  369. end
  370. core.log('action', digger:get_player_name() .. " digs "
  371. .. node.name .. " at " .. core.pos_to_string(pos))
  372. local wielded = digger:get_wielded_item()
  373. local drops = core.get_node_drops(node.name, wielded:get_name())
  374. local wdef = wielded:get_definition()
  375. local tp = wielded:get_tool_capabilities()
  376. local dp = core.get_dig_params(def.groups, tp)
  377. if wdef and wdef.after_use then
  378. wielded = wdef.after_use(wielded, digger, node, dp) or wielded
  379. else
  380. -- Wear out tool
  381. if not core.setting_getbool("creative_mode") then
  382. wielded:add_wear(dp.wear)
  383. end
  384. end
  385. digger:set_wielded_item(wielded)
  386. -- Handle drops
  387. core.handle_node_drops(pos, drops, digger)
  388. local oldmetadata = nil
  389. if def.after_dig_node then
  390. oldmetadata = core.get_meta(pos):to_table()
  391. end
  392. -- Remove node and update
  393. core.remove_node(pos)
  394. -- Run callback
  395. if def.after_dig_node then
  396. -- Copy pos and node because callback can modify them
  397. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  398. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  399. def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
  400. end
  401. -- Run script hook
  402. local _, callback
  403. for _, callback in ipairs(core.registered_on_dignodes) do
  404. -- Copy pos and node because callback can modify them
  405. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  406. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  407. callback(pos_copy, node_copy, digger)
  408. end
  409. end
  410. -- This is used to allow mods to redefine core.item_place and so on
  411. -- NOTE: This is not the preferred way. Preferred way is to provide enough
  412. -- callbacks to not require redefining global functions. -celeron55
  413. local function redef_wrapper(table, name)
  414. return function(...)
  415. return table[name](...)
  416. end
  417. end
  418. --
  419. -- Item definition defaults
  420. --
  421. core.nodedef_default = {
  422. -- Item properties
  423. type="node",
  424. -- name intentionally not defined here
  425. description = "",
  426. groups = {},
  427. inventory_image = "",
  428. wield_image = "",
  429. wield_scale = {x=1,y=1,z=1},
  430. stack_max = 99,
  431. usable = false,
  432. liquids_pointable = false,
  433. tool_capabilities = nil,
  434. node_placement_prediction = nil,
  435. -- Interaction callbacks
  436. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  437. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  438. on_use = nil,
  439. can_dig = nil,
  440. on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
  441. on_rightclick = nil,
  442. on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
  443. on_receive_fields = nil,
  444. on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
  445. on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
  446. on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
  447. -- Node properties
  448. drawtype = "normal",
  449. visual_scale = 1.0,
  450. -- Don't define these because otherwise the old tile_images and
  451. -- special_materials wouldn't be read
  452. --tiles ={""},
  453. --special_tiles = {
  454. -- {name="", backface_culling=true},
  455. -- {name="", backface_culling=true},
  456. --},
  457. alpha = 255,
  458. post_effect_color = {a=0, r=0, g=0, b=0},
  459. paramtype = "none",
  460. paramtype2 = "none",
  461. is_ground_content = true,
  462. sunlight_propagates = false,
  463. walkable = true,
  464. pointable = true,
  465. diggable = true,
  466. climbable = false,
  467. buildable_to = false,
  468. liquidtype = "none",
  469. liquid_alternative_flowing = "",
  470. liquid_alternative_source = "",
  471. liquid_viscosity = 0,
  472. drowning = 0,
  473. light_source = 0,
  474. damage_per_second = 0,
  475. selection_box = {type="regular"},
  476. legacy_facedir_simple = false,
  477. legacy_wallmounted = false,
  478. }
  479. core.craftitemdef_default = {
  480. type="craft",
  481. -- name intentionally not defined here
  482. description = "",
  483. groups = {},
  484. inventory_image = "",
  485. wield_image = "",
  486. wield_scale = {x=1,y=1,z=1},
  487. stack_max = 99,
  488. liquids_pointable = false,
  489. tool_capabilities = nil,
  490. -- Interaction callbacks
  491. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  492. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  493. on_use = nil,
  494. }
  495. core.tooldef_default = {
  496. type="tool",
  497. -- name intentionally not defined here
  498. description = "",
  499. groups = {},
  500. inventory_image = "",
  501. wield_image = "",
  502. wield_scale = {x=1,y=1,z=1},
  503. stack_max = 1,
  504. liquids_pointable = false,
  505. tool_capabilities = nil,
  506. -- Interaction callbacks
  507. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  508. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  509. on_use = nil,
  510. }
  511. core.noneitemdef_default = { -- This is used for the hand and unknown items
  512. type="none",
  513. -- name intentionally not defined here
  514. description = "",
  515. groups = {},
  516. inventory_image = "",
  517. wield_image = "",
  518. wield_scale = {x=1,y=1,z=1},
  519. stack_max = 99,
  520. liquids_pointable = false,
  521. tool_capabilities = nil,
  522. -- Interaction callbacks
  523. on_place = redef_wrapper(core, 'item_place'),
  524. on_drop = nil,
  525. on_use = nil,
  526. }