init.lua 13 KB

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