init.lua 13 KB

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