init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. -- Ensure player's inventory has enough room.
  92. if inv and inv:room_for_item("main", item) then
  93. inv:add_item("main", item)
  94. itempickup.sound(pos)
  95. luaent.itemstring = "" -- Prevents item duplication.
  96. n:remove()
  97. end
  98. end
  99. end
  100. end
  101. ::next_drop::
  102. end
  103. end
  104. end
  105. end
  106. -- Anything not listed here is assumed to have an XP value of 0.
  107. -- Be carefull not to include nodes that can be dug over and over again.
  108. -- This is a list of *drops* that, when obtained, increase XP.
  109. local drop_xp_list = {
  110. ["akalin:lump"] = 1.0,
  111. ["alatro:lump"] = 1.0,
  112. ["arol:lump"] = 1.0,
  113. ["chromium:lump"] = 1.0,
  114. ["gems:raw_ruby"] = 10.0,
  115. ["gems:raw_emerald"] = 10.0,
  116. ["gems:raw_sapphire"] = 10.0,
  117. ["gems:raw_amethyst"] = 10.0,
  118. ["kalite:lump"] = 0.1,
  119. ["lead:lump"] = 1.0,
  120. ["default:coal_lump"] = 0.1,
  121. ["default:iron_lump"] = 1.0,
  122. ["default:copper_lump"] = 1.0,
  123. ["default:gold_lump"] = 2.0,
  124. ["default:mese_crystal"] = 2.0,
  125. ["default:diamond"] = 5.0,
  126. ["moreores:tin_lump"] = 0.5,
  127. ["moreores:silver_lump"] = 1.0,
  128. ["moreores:mithril_lump"] = 20.0,
  129. ["glowstone:glowing_dust"] = 1.0,
  130. ["quartz:quartz_crystal"] = 0.5,
  131. ["talinite:lump"] = 1.0,
  132. ["thorium:lump"] = 1.0,
  133. ["titanium:titanium"] = 1.0,
  134. ["uranium:lump"] = 1.0,
  135. ["zinc:lump"] = 1.0,
  136. ["sulfur:lump"] = 0.1,
  137. }
  138. local drop_xp_multiplier = 1.0
  139. for k, v in pairs(drop_xp_list) do
  140. drop_xp_list[k] = v * drop_xp_multiplier
  141. end
  142. -- Stuff listed here is what player can actually get, if XP is near max.
  143. local drop_extra_item_list = {
  144. ["akalin:lump"] = true,
  145. ["alatro:lump"] = true,
  146. ["arol:lump"] = true,
  147. ["chromium:lump"] = true,
  148. ["gems:raw_ruby"] = true,
  149. ["gems:raw_emerald"] = true,
  150. ["gems:raw_sapphire"] = true,
  151. ["gems:raw_amethyst"] = true,
  152. ["kalite:lump"] = true,
  153. ["lead:lump"] = true,
  154. ["default:coal_lump"] = true,
  155. ["default:iron_lump"] = true,
  156. ["default:copper_lump"] = true,
  157. ["default:gold_lump"] = true,
  158. ["default:mese_crystal"] = true,
  159. ["default:diamond"] = true,
  160. ["default:mese"] = true,
  161. ["mese_crystals:mese_crystal_ore1"] = true,
  162. ["mese_crystals:mese_crystal_ore2"] = true,
  163. ["mese_crystals:mese_crystal_ore3"] = true,
  164. ["mese_crystals:mese_crystal_ore4"] = true,
  165. ["moreores:tin_lump"] = true,
  166. ["moreores:silver_lump"] = true,
  167. ["moreores:mithril_lump"] = true,
  168. ["glowstone:glowing_dust"] = true,
  169. ["quartz:quartz_crystal"] = true,
  170. ["talinite:lump"] = true,
  171. ["thorium:lump"] = true,
  172. ["titanium:titanium"] = true,
  173. ["uranium:lump"] = true,
  174. ["zinc:lump"] = true,
  175. ["sulfur:lump"] = true,
  176. }
  177. -- List of nodes capable of providing extra drops.
  178. -- Player can only get extra drop for mineral XP if they dug one of these nodes.
  179. -- Also, node must be one of these in order to increase mining XP.
  180. local drop_node_list = {
  181. ["default:stone_with_coal"] = true,
  182. ["default:desert_stone_with_coal"] = true,
  183. ["default:stone_with_iron"] = true,
  184. ["default:stone_with_copper"] = true,
  185. ["default:desert_stone_with_copper"] = true,
  186. ["default:desert_stone_with_iron"] = true,
  187. ["default:desert_stone_with_diamond"] = true,
  188. ["default:stone_with_mese"] = true,
  189. ["default:stone_with_gold"] = true,
  190. ["default:stone_with_diamond"] = true,
  191. ["rackstone:redrack_with_coal"] = true,
  192. ["rackstone:redrack_with_iron"] = true,
  193. ["rackstone:redrack_with_copper"] = true,
  194. ["rackstone:redrack_with_tin"] = true,
  195. ["akalin:ore"] = true,
  196. ["alatro:ore"] = true,
  197. ["arol:ore"] = true,
  198. ["talinite:ore"] = true,
  199. ["thorium:ore"] = true,
  200. ["titanium:ore"] = true,
  201. ["uranium:ore"] = true,
  202. ["sulfur:ore"] = true,
  203. ["kalite:ore"] = true,
  204. ["chromium:ore"] = true,
  205. ["zinc:ore"] = true,
  206. ["lead:ore"] = true,
  207. ["glowstone:luxore"] = true,
  208. ["glowstone:minerals"] = true,
  209. ["glowstone:glowstone"] = true,
  210. ["moreores:mineral_tin"] = true,
  211. ["moreores:mineral_silver"] = true,
  212. ["moreores:mineral_mithril"] = true,
  213. ["gems:ruby_ore"] = true,
  214. ["gems:emerald_ore"] = true,
  215. ["gems:sapphire_ore"] = true,
  216. ["gems:amethyst_ore"] = true,
  217. ["mese_crystals:mese_crystal_ore1"] = true,
  218. ["mese_crystals:mese_crystal_ore2"] = true,
  219. ["mese_crystals:mese_crystal_ore3"] = true,
  220. ["mese_crystals:mese_crystal_ore4"] = true,
  221. ["quartz:quartz_ore"] = true,
  222. }
  223. function itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  224. local pp = utility.get_middle_pos(digger:get_pos())
  225. -- Some tools always make their drops go directly to player's inventory.
  226. local direct = tool_capabilities.direct_to_inventory
  227. -- Stack goes directly into inventory if player close enough.
  228. if vector_distance(pp, pos) < 3.5 or direct then
  229. local inv = digger:get_inventory()
  230. if inv then
  231. stack = inv:add_item("main", stack)
  232. -- If stack couldn't be added because of full inventory, then material is sometimes lost.
  233. if not stack:is_empty() and math_random(0, 3) == 0 then
  234. -- Don't drop anything on the ground, 25% chance.
  235. -- Give particle feedback to player.
  236. digger:set_hp(digger:get_hp() - 1)
  237. effect(pos, math_random(2, 5), "tnt_smoke.png")
  238. return
  239. end
  240. end
  241. end
  242. if not stack:is_empty() then
  243. local obj = minetest.add_item(pos, stack)
  244. -- Make the drop fly a bit.
  245. if obj then
  246. obj:set_velocity({
  247. x=math_random(-10, 10) / 5,
  248. y=3,
  249. z=math_random(-10, 10) / 5,
  250. })
  251. end
  252. end
  253. end
  254. -- Table of XP gain multipliers based on tool's rank (toolranks mod).
  255. local xp_gain_ranks = {
  256. [1] = 0.1,
  257. [2] = 0.3,
  258. [3] = 0.5,
  259. [4] = 1.0,
  260. [5] = 1.1,
  261. [6] = 1.2,
  262. [7] = 1.5,
  263. }
  264. function itempickup.handle_node_drops(pos, drops, digger)
  265. -- Nil check.
  266. if not digger or not digger:is_player() then
  267. return
  268. end
  269. -- Node hasn't been removed yet, we can make use of it. GOOD!
  270. local node = minetest.get_node(pos) -- Node to be dug.
  271. local tool = digger:get_wielded_item()
  272. local tool_meta = tool:get_meta()
  273. local tool_level = tonumber(tool_meta:get_string("tr_lastlevel")) or 1
  274. local tn = tool:get_name()
  275. local xp_drop_enabled = true
  276. -- Node definition.
  277. local ndef = minetest.reg_ns_nodes[node.name] or minetest.registered_nodes[node.name]
  278. -- Nil check.
  279. if not ndef then
  280. return
  281. end
  282. local ngroups = ndef.groups or {}
  283. -- We have to get tool capabilities directly from the itemdef in order to access custom data.
  284. -- If tool capabilities are not present, use those from the HAND. Note: not doing this
  285. -- properly was the cause of an embarrassing bug where players could not get items that they
  286. -- had dug with the hand while wielding another non-tool item.
  287. local idef = tool:get_definition()
  288. if not idef then
  289. return
  290. end
  291. local tool_capabilities = idef.tool_capabilities
  292. if not tool_capabilities then
  293. tool_capabilities = tooldata["hand_hand"]
  294. assert(tool_capabilities)
  295. end
  296. if tool_capabilities.node_overrides then
  297. if tool_capabilities.node_overrides[node.name] then
  298. tool_capabilities = tool_capabilities.node_overrides[node.name]
  299. end
  300. end
  301. -- Max level (toolranks) tool gets its `max_drop_level` improved by 1!
  302. local max_drop_level = (tool_capabilities.max_drop_level or 0)
  303. if tool_level >= 7 then
  304. max_drop_level = max_drop_level + 1
  305. --minetest.log("max_drop_level increased!") -- Tested, works.
  306. end
  307. -- Player does not get node drop if tool doesn't have sufficient level.
  308. if (max_drop_level) < (ndef.groups.level or 0) then
  309. -- 1 in 4 chance player will get the node anyway.
  310. if math_random(1, 4) > 1 then
  311. -- Particle feedback to player.
  312. effect(pos, math_random(2, 5), "tnt_smoke.png")
  313. return
  314. end
  315. end
  316. -- Test tool's chance to destroy node regardless of node/tool levels.
  317. if tool_capabilities.destroy_chance then
  318. if math_random(1, 1000) < tool_capabilities.destroy_chance then
  319. -- Particle feedback to player.
  320. effect(pos, math_random(2, 5), "tnt_smoke.png")
  321. return
  322. end
  323. end
  324. -- Tool's (toolranks) rank modifies the mineral XP gain rate!
  325. local xp_gain_multiplier = (tool_capabilities.xp_gain or 1.0)
  326. if xp_gain_ranks[tool_level] then
  327. xp_gain_multiplier = (xp_gain_multiplier * xp_gain_ranks[tool_level])
  328. --minetest.log("xp gain: " .. xp_gain_multiplier) -- Tested, works!
  329. end
  330. local is_basic_tool = (tn:find("pick_") or tn:find("sword_") or tn:find("shovel_") or tn:find("axe_") or tn:find(":axe"))
  331. -- If node has a drop string/table for silver tools, override drop table.
  332. -- Player doesn't get XP for nodes dug this way, but that's good (prevents exploit).
  333. if is_basic_tool and tn:find("silver") then
  334. if ndef.silverpick_drop then
  335. local newdrop = ndef.silverpick_drop
  336. if type(newdrop) == "table" then
  337. drops = newdrop
  338. elseif type(newdrop) == "string" then
  339. drops = {newdrop}
  340. elseif type(newdrop) == "boolean" and newdrop == true then
  341. drops = {node.name}
  342. end
  343. end
  344. elseif tn:find("shears") then
  345. if ndef.shears_drop then
  346. local newdrop = ndef.shears_drop
  347. if type(newdrop) == "table" then
  348. drops = newdrop
  349. elseif type(newdrop) == "string" then
  350. drops = {newdrop}
  351. elseif type(newdrop) == "boolean" and newdrop == true then
  352. drops = {node.name}
  353. end
  354. end
  355. end
  356. --minetest.chat_send_all(dump(drops))
  357. for _, item in pairs(drops) do
  358. local stack = ItemStack(item) -- Itemstring to itemstack.
  359. local sname = stack:get_name()
  360. -- Give drop to player, or drop on ground.
  361. itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  362. if xp_drop_enabled and drop_xp_list[sname] then
  363. local value = drop_xp_list[sname]
  364. local pname = digger:get_player_name()
  365. local digxp = xp.get_xp(pname, "digxp")
  366. -- Reward player more when Mineral XP is higher.
  367. -- But only for ore nodes.
  368. if drop_node_list[node.name] then
  369. -- Both X's should be in range [0, 1].
  370. -- If player's XP is > 1000, then clamp to 1000 for purposes of this calculation.
  371. local max = 1000
  372. local x1 = math_min(math_max(0, digxp), max) / max
  373. local x2 = math_random(0, 10000)/10000
  374. if x1*x1 >= x2 then
  375. if drop_extra_item_list[sname] then
  376. -- Give drop to player, or drop on ground.
  377. itempickup.drop_an_item(pos, stack, digger, tool_capabilities)
  378. end
  379. end
  380. end
  381. -- Increase player's XP if not at max yet.
  382. if drop_node_list[node.name] then
  383. if digxp < xp.digxp_max then
  384. digxp = digxp + (value * xp_gain_multiplier)
  385. if digxp > xp.digxp_max then
  386. digxp = xp.digxp_max
  387. end
  388. xp.set_xp(pname, "digxp", digxp)
  389. hud_clock.update_xp(pname)
  390. end
  391. end
  392. end -- If item in drop_xp list.
  393. end
  394. end
  395. -- First-time initialization code.
  396. if not itempickup.run_once then
  397. minetest.register_globalstep(function(...) itempickup.update(...) end)
  398. local name = "itempickup:core"
  399. local file = itempickup.modpath .. "/init.lua"
  400. reload.register_file(name, file, false)
  401. function minetest.handle_node_drops(pos, drops, player)
  402. return itempickup.handle_node_drops(pos, drops, player)
  403. end
  404. itempickup.run_once = true
  405. end