init.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. itempickup = itempickup or {}
  2. itempickup.modpath = minetest.get_modpath("itempickup")
  3. -- Localize vector.distance() for performance.
  4. local vector_distance = vector.distance
  5. local math_random = math.random
  6. local math_min = math.min
  7. local math_max = math.max
  8. -- custom particle effects
  9. local function effect(pos, amount, texture, min_size, max_size, radius, gravity, glow)
  10. radius = radius or 2
  11. min_size = min_size or 2.0
  12. max_size = max_size or 6.0
  13. gravity = gravity or -10
  14. glow = glow or 0
  15. minetest.add_particlespawner({
  16. amount = amount,
  17. time = 0.25,
  18. minpos = pos,
  19. maxpos = pos,
  20. minvel = {x = -radius, y = -radius, z = -radius},
  21. maxvel = {x = radius, y = radius, z = radius},
  22. minacc = {x = 0, y = gravity, z = 0},
  23. maxacc = {x = 0, y = gravity, z = 0},
  24. minexptime = 0.1,
  25. maxexptime = 1,
  26. minsize = min_size,
  27. maxsize = max_size,
  28. texture = texture,
  29. glow = glow,
  30. })
  31. end
  32. itempickup.sound = function(pos)
  33. ambiance.sound_play("itempickup_pickup", pos, 0.07, 20)
  34. end
  35. local pickuptimer = 0
  36. itempickup.update = function(dtime)
  37. -- Time delay 0.5 second.
  38. pickuptimer = pickuptimer + dtime
  39. if pickuptimer < 0.5 then
  40. return
  41. end
  42. pickuptimer = 0
  43. --collectgarbage("step") -- Do not enable - causes huge issues.
  44. local players = minetest.get_connected_players()
  45. for k, v in ipairs(players) do
  46. rc.check_position(v) -- Check position before calling `get_objects_inside_radius'.
  47. local pname = v:get_player_name()
  48. if v:is_player() and v:get_hp() > 0 and not gdac_invis.is_invisible(pname) then
  49. -- Basic range, when player is standing still.
  50. local range = 0
  51. local sneak = false
  52. local control = v:get_player_control()
  53. -- Sneaking increases pickup range.
  54. if control.sneak or control.aux1 then
  55. range = 3.5
  56. sneak = true
  57. end
  58. -- Range is increased a bit when digging.
  59. -- This is because items are dropped when nodes are dug,
  60. -- but it would be too annoying if even nodes right next
  61. -- to the player were dropped on the ground.
  62. if control.LMB then
  63. range = 2.5
  64. end
  65. local pos -- Initialized only if items are searched.
  66. local items = {}
  67. if range > 0 then
  68. pos = utility.get_middle_pos(v:get_pos())
  69. items = minetest.get_objects_inside_radius(pos, range)
  70. end
  71. local inv
  72. if #items > 0 then
  73. inv = v:get_inventory()
  74. end
  75. for m, n in ipairs(items) do
  76. -- Ensure object found is an item-drop.
  77. local luaent = n:get_luaentity()
  78. if not n:is_player() and luaent and luaent.name == "__builtin:item" then
  79. local name = luaent.itemstring
  80. if name ~= "" then -- Some itemstacks have empty names.
  81. if not control.aux1 then
  82. if luaent.dropped_by and type(luaent.dropped_by) == "string" then
  83. if luaent.dropped_by == pname then
  84. goto next_drop
  85. end
  86. end
  87. end
  88. local item = ItemStack(name)
  89. local ndef = minetest.registered_items[item:get_name()]
  90. if ndef and (not ndef._no_auto_pop or control.aux1) then
  91. -- Disable pickup for items inside fire.
  92. local ip = vector.round(n:get_pos())
  93. local np = minetest.get_node(ip).name
  94. if minetest.get_item_group(np, "fire") == 0 then
  95. -- Ensure player's inventory has enough room.
  96. if inv and inv:room_for_item("main", item) then
  97. inv:add_item("main", item)
  98. itempickup.sound(pos)
  99. luaent.itemstring = "" -- Prevents item duplication.
  100. n:remove()
  101. end
  102. end
  103. end
  104. end
  105. end
  106. ::next_drop::
  107. end
  108. end
  109. end
  110. end
  111. -- Anything not listed here is assumed to have an XP value of 0.
  112. -- Be carefull not to include nodes that can be dug over and over again.
  113. -- This is a list of *drops* that, when obtained, increase XP.
  114. local drop_xp_list = {
  115. ["akalin:lump"] = 1.0,
  116. ["alatro:lump"] = 1.0,
  117. ["arol:lump"] = 1.0,
  118. ["chromium:lump"] = 1.0,
  119. ["gems:raw_ruby"] = 10.0,
  120. ["gems:raw_emerald"] = 10.0,
  121. ["gems:raw_sapphire"] = 10.0,
  122. ["gems:raw_amethyst"] = 10.0,
  123. ["kalite:lump"] = 0.1,
  124. ["lead:lump"] = 1.0,
  125. ["default:coal_lump"] = 0.1,
  126. ["default:iron_lump"] = 1.0,
  127. ["default:copper_lump"] = 1.0,
  128. ["default:gold_lump"] = 2.0,
  129. ["default:mese_crystal"] = 2.0,
  130. ["default:diamond"] = 5.0,
  131. ["moreores:tin_lump"] = 0.5,
  132. ["moreores:silver_lump"] = 1.0,
  133. ["moreores:mithril_lump"] = 20.0,
  134. ["glowstone:glowing_dust"] = 1.0,
  135. ["quartz:quartz_crystal"] = 0.5,
  136. ["talinite:lump"] = 1.0,
  137. ["thorium:lump"] = 1.0,
  138. ["titanium:titanium"] = 1.0,
  139. ["uranium:lump"] = 1.0,
  140. ["zinc:lump"] = 1.0,
  141. ["sulfur:lump"] = 0.1,
  142. }
  143. local drop_xp_multiplier = 1.0
  144. for k, v in pairs(drop_xp_list) do
  145. drop_xp_list[k] = v * drop_xp_multiplier
  146. end
  147. -- Stuff listed here is what player can actually get, if XP is near max.
  148. local drop_extra_item_list = {
  149. ["akalin:lump"] = true,
  150. ["alatro:lump"] = true,
  151. ["arol:lump"] = true,
  152. ["chromium:lump"] = true,
  153. ["gems:raw_ruby"] = true,
  154. ["gems:raw_emerald"] = true,
  155. ["gems:raw_sapphire"] = true,
  156. ["gems:raw_amethyst"] = true,
  157. ["kalite:lump"] = true,
  158. ["lead:lump"] = true,
  159. ["default:coal_lump"] = true,
  160. ["default:iron_lump"] = true,
  161. ["default:copper_lump"] = true,
  162. ["default:gold_lump"] = true,
  163. ["default:mese_crystal"] = true,
  164. ["default:diamond"] = true,
  165. ["default:mese"] = true,
  166. ["mese_crystals:mese_crystal_ore1"] = true,
  167. ["mese_crystals:mese_crystal_ore2"] = true,
  168. ["mese_crystals:mese_crystal_ore3"] = true,
  169. ["mese_crystals:mese_crystal_ore4"] = true,
  170. ["moreores:tin_lump"] = true,
  171. ["moreores:silver_lump"] = true,
  172. ["moreores:mithril_lump"] = true,
  173. ["glowstone:glowing_dust"] = true,
  174. ["quartz:quartz_crystal"] = true,
  175. ["talinite:lump"] = true,
  176. ["thorium:lump"] = true,
  177. ["titanium:titanium"] = true,
  178. ["uranium:lump"] = true,
  179. ["zinc:lump"] = true,
  180. ["sulfur:lump"] = true,
  181. }
  182. -- List of nodes capable of providing extra drops.
  183. -- Player can only get extra drop for mineral XP if they dug one of these nodes.
  184. -- Also, node must be one of these in order to increase mining XP.
  185. local drop_node_list = {
  186. ["default:stone_with_coal"] = true,
  187. ["default:desert_stone_with_coal"] = true,
  188. ["default:stone_with_iron"] = true,
  189. ["default:stone_with_copper"] = true,
  190. ["default:desert_stone_with_copper"] = true,
  191. ["default:desert_stone_with_iron"] = true,
  192. ["default:desert_stone_with_diamond"] = true,
  193. ["default:stone_with_mese"] = true,
  194. ["default:stone_with_gold"] = true,
  195. ["default:stone_with_diamond"] = true,
  196. ["rackstone:redrack_with_coal"] = true,
  197. ["rackstone:redrack_with_iron"] = true,
  198. ["rackstone:redrack_with_copper"] = true,
  199. ["rackstone:redrack_with_tin"] = true,
  200. ["akalin:ore"] = true,
  201. ["alatro:ore"] = true,
  202. ["arol:ore"] = true,
  203. ["talinite:ore"] = true,
  204. ["thorium:ore"] = true,
  205. ["titanium:ore"] = true,
  206. ["uranium:ore"] = true,
  207. ["sulfur:ore"] = true,
  208. ["kalite:ore"] = true,
  209. ["chromium:ore"] = true,
  210. ["zinc:ore"] = true,
  211. ["lead:ore"] = true,
  212. ["glowstone:luxore"] = true,
  213. ["glowstone:minerals"] = true,
  214. ["glowstone:glowstone"] = true,
  215. ["moreores:mineral_tin"] = true,
  216. ["moreores:mineral_silver"] = true,
  217. ["moreores:mineral_mithril"] = true,
  218. ["gems:ruby_ore"] = true,
  219. ["gems:emerald_ore"] = true,
  220. ["gems:sapphire_ore"] = true,
  221. ["gems:amethyst_ore"] = true,
  222. ["mese_crystals:mese_crystal_ore1"] = true,
  223. ["mese_crystals:mese_crystal_ore2"] = true,
  224. ["mese_crystals:mese_crystal_ore3"] = true,
  225. ["mese_crystals:mese_crystal_ore4"] = true,
  226. ["quartz:quartz_ore"] = true,
  227. }
  228. function itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  229. local pp = utility.get_middle_pos(digger:get_pos())
  230. -- Some tools always make their drops go directly to player's inventory.
  231. local direct = tool_capabilities.direct_to_inventory
  232. -- Stack goes directly into inventory if player close enough.
  233. if vector_distance(pp, pos) < 3.5 or direct then
  234. local inv = digger:get_inventory()
  235. if inv then
  236. stack = inv:add_item("main", stack)
  237. -- If stack couldn't be added because of full inventory, then material is sometimes lost.
  238. if not stack:is_empty() and math_random(0, 3) == 0 then
  239. -- Don't drop anything on the ground, 25% chance.
  240. -- Give particle feedback to player.
  241. digger:set_hp(digger:get_hp() - 1)
  242. effect(pos, math_random(2, 5), "tnt_smoke.png")
  243. return
  244. end
  245. end
  246. end
  247. if not stack:is_empty() then
  248. local obj = minetest.add_item(pos, stack)
  249. -- Make the drop fly a bit.
  250. if obj then
  251. obj:set_velocity({
  252. x=math_random(-10, 10) / 5,
  253. y=3,
  254. z=math_random(-10, 10) / 5,
  255. })
  256. end
  257. end
  258. end
  259. -- Table of XP gain multipliers based on tool's rank (toolranks mod).
  260. local xp_gain_ranks = {
  261. [1] = 0.1,
  262. [2] = 0.3,
  263. [3] = 0.5,
  264. [4] = 1.0,
  265. [5] = 1.1,
  266. [6] = 1.2,
  267. [7] = 1.5,
  268. }
  269. function itempickup.handle_node_drops(pos, drops, digger)
  270. -- Nil check.
  271. if not digger or not digger:is_player() then
  272. return
  273. end
  274. -- Node hasn't been removed yet, we can make use of it. GOOD!
  275. local node = minetest.get_node(pos) -- Node to be dug.
  276. local tool = digger:get_wielded_item()
  277. local tool_meta = tool:get_meta()
  278. local tool_level = tonumber(tool_meta:get_string("tr_lastlevel")) or 1
  279. local tn = tool:get_name()
  280. local xp_drop_enabled = true
  281. -- Node definition.
  282. local ndef = minetest.reg_ns_nodes[node.name] or minetest.registered_nodes[node.name]
  283. -- Nil check.
  284. if not ndef then
  285. return
  286. end
  287. local ngroups = ndef.groups or {}
  288. -- We have to get tool capabilities directly from the itemdef in order to access custom data.
  289. -- If tool capabilities are not present, use those from the HAND. Note: not doing this
  290. -- properly was the cause of an embarrassing bug where players could not get items that they
  291. -- had dug with the hand while wielding another non-tool item.
  292. local idef = tool:get_definition()
  293. if not idef then
  294. return
  295. end
  296. local tool_capabilities = idef.tool_capabilities
  297. if not tool_capabilities then
  298. tool_capabilities = tooldata["hand_hand"]
  299. assert(tool_capabilities)
  300. end
  301. if tool_capabilities.node_overrides then
  302. if tool_capabilities.node_overrides[node.name] then
  303. tool_capabilities = tool_capabilities.node_overrides[node.name]
  304. end
  305. end
  306. -- Max level (toolranks) tool gets its `max_drop_level` improved by 1!
  307. local max_drop_level = (tool_capabilities.max_drop_level or 0)
  308. if tool_level >= 7 then
  309. max_drop_level = max_drop_level + 1
  310. --minetest.log("max_drop_level increased!") -- Tested, works.
  311. end
  312. -- Player does not get node drop if tool doesn't have sufficient level.
  313. if (max_drop_level) < (ndef.groups.level or 0) then
  314. -- 1 in 4 chance player will get the node anyway.
  315. if math_random(1, 4) > 1 then
  316. -- Particle feedback to player.
  317. effect(pos, math_random(2, 5), "tnt_smoke.png")
  318. return
  319. end
  320. end
  321. -- Test tool's chance to destroy node regardless of node/tool levels.
  322. if tool_capabilities.destroy_chance then
  323. if math_random(1, 1000) < tool_capabilities.destroy_chance then
  324. -- Particle feedback to player.
  325. effect(pos, math_random(2, 5), "tnt_smoke.png")
  326. return
  327. end
  328. end
  329. -- Tool's (toolranks) rank modifies the mineral XP gain rate!
  330. local xp_gain_multiplier = (tool_capabilities.xp_gain or 1.0)
  331. if xp_gain_ranks[tool_level] then
  332. xp_gain_multiplier = (xp_gain_multiplier * xp_gain_ranks[tool_level])
  333. --minetest.log("xp gain: " .. xp_gain_multiplier) -- Tested, works!
  334. end
  335. local is_basic_tool = (tn:find("pick_") or tn:find("sword_") or tn:find("shovel_") or tn:find("axe_") or tn:find(":axe"))
  336. -- If node has a drop string/table for silver tools, override drop table.
  337. -- Player doesn't get XP for nodes dug this way, but that's good (prevents exploit).
  338. if is_basic_tool and tn:find("silver") then
  339. if ndef.silverpick_drop then
  340. local newdrop = ndef.silverpick_drop
  341. if type(newdrop) == "table" then
  342. drops = newdrop
  343. elseif type(newdrop) == "string" then
  344. drops = {newdrop}
  345. elseif type(newdrop) == "boolean" and newdrop == true then
  346. drops = {node.name}
  347. end
  348. end
  349. elseif tn:find("shears") then
  350. if ndef.shears_drop then
  351. local newdrop = ndef.shears_drop
  352. if type(newdrop) == "table" then
  353. drops = newdrop
  354. elseif type(newdrop) == "string" then
  355. drops = {newdrop}
  356. elseif type(newdrop) == "boolean" and newdrop == true then
  357. drops = {node.name}
  358. end
  359. end
  360. end
  361. --minetest.chat_send_all(dump(drops))
  362. for _, item in ipairs(drops) do
  363. local stack = ItemStack(item) -- Itemstring to itemstack.
  364. local sname = stack:get_name()
  365. -- Give drop to player, or drop on ground.
  366. itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  367. if xp_drop_enabled and drop_xp_list[sname] then
  368. local value = drop_xp_list[sname]
  369. local pname = digger:get_player_name()
  370. local digxp = xp.get_xp(pname, "digxp")
  371. -- Reward player more when Mineral XP is higher.
  372. -- But only for ore nodes.
  373. if drop_node_list[node.name] then
  374. -- Both X's should be in range [0, 1].
  375. -- If player's XP is > 1000, then clamp to 1000 for purposes of this calculation.
  376. local max = 1000
  377. local x1 = math_min(math_max(0, digxp), max) / max
  378. local x2 = math_random(0, 10000)/10000
  379. if x1*x1 >= x2 then
  380. if drop_extra_item_list[sname] then
  381. -- Give drop to player, or drop on ground.
  382. itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  383. end
  384. end
  385. end
  386. -- Increase player's XP if not at max yet.
  387. if drop_node_list[node.name] then
  388. if digxp < xp.digxp_max then
  389. digxp = digxp + (value * xp_gain_multiplier)
  390. if digxp > xp.digxp_max then
  391. digxp = xp.digxp_max
  392. end
  393. xp.set_xp(pname, "digxp", digxp)
  394. hud_clock.update_xp(pname)
  395. end
  396. end
  397. end -- If item in drop_xp list.
  398. end
  399. end
  400. -- First-time initialization code.
  401. if not itempickup.run_once then
  402. minetest.register_globalstep(function(...) itempickup.update(...) end)
  403. local name = "itempickup:core"
  404. local file = itempickup.modpath .. "/init.lua"
  405. reload.register_file(name, file, false)
  406. function minetest.handle_node_drops(pos, drops, player)
  407. return itempickup.handle_node_drops(pos, drops, player)
  408. end
  409. itempickup.run_once = true
  410. end