Minetest tool ranks mod
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.5KB

  1. local mod_storage = minetest.get_mod_storage()
  2. local S = minetest.get_translator("toolranks")
  3. toolranks = {}
  4. toolranks.colors = {
  5. grey = minetest.get_color_escape_sequence("#9d9d9d"),
  6. green = minetest.get_color_escape_sequence("#1eff00"),
  7. gold = minetest.get_color_escape_sequence("#ffdf00"),
  8. white = minetest.get_color_escape_sequence("#ffffff")
  9. }
  10. local max_speed = tonumber(minetest.settings:get("toolranks_speed_multiplier")) or 2.0
  11. local max_use = tonumber(minetest.settings:get("toolranks_use_multiplier")) or 2.0
  12. local max_level = tonumber(minetest.settings:get("toolranks_levels")) or 10
  13. local level_digs = tonumber(minetest.settings:get("toolranks_level_digs")) or 500
  14. local level_multiplier = 1 / max_level
  15. function toolranks.get_tool_type(description)
  16. if not description then
  17. return "tool"
  18. else
  19. local d = string.lower(description)
  20. if string.find(d, "pickaxe") then
  21. return "pickaxe"
  22. elseif string.find(d, "axe") then
  23. return "axe"
  24. elseif string.find(d, "shovel") then
  25. return "shovel"
  26. elseif string.find(d, "hoe") then
  27. return "hoe"
  28. elseif string.find(d, "sword") then
  29. return "sword"
  30. else
  31. return "tool"
  32. end
  33. end
  34. end
  35. function toolranks.create_description(name, uses, level)
  36. local description = name
  37. local tooltype = toolranks.get_tool_type(description)
  38. local newdesc = S(
  39. "@1@2\n@3Level @4 @5\n@6@Node dug: @7",
  40. toolranks.colors.green,
  41. description,
  42. toolranks.colors.gold,
  43. (level or 1),
  44. S(tooltype),
  45. toolranks.colors.grey,
  46. (uses or 0)
  47. )
  48. return newdesc
  49. end
  50. function toolranks.get_level(uses)
  51. return math.min(max_level, math.floor(uses / level_digs))
  52. end
  53. function toolranks.new_afteruse(itemstack, user, node, digparams)
  54. local itemmeta = itemstack:get_meta()
  55. local itemdef = itemstack:get_definition()
  56. local itemdesc = itemdef.original_description
  57. local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
  58. local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 0
  59. local most_digs = mod_storage:get_int("most_digs") or 0
  60. local most_digs_user = mod_storage:get_string("most_digs_user") or 0
  61. if digparams.wear > 0 then -- Only count nodes that spend the tool
  62. dugnodes = dugnodes + 1
  63. itemmeta:set_string("dug", dugnodes)
  64. end
  65. if dugnodes > most_digs then
  66. if most_digs_user ~= user:get_player_name() then -- Avoid spam.
  67. minetest.chat_send_all(S(
  68. "Most used tool is now a @1@2@3 owned by @4 with @5 uses.",
  69. toolranks.colors.green,
  70. itemdesc,
  71. toolranks.colors.white,
  72. user:get_player_name(),
  73. dugnodes
  74. ))
  75. end
  76. mod_storage:set_int("most_digs", dugnodes)
  77. mod_storage:set_string("most_digs_user", user:get_player_name())
  78. end
  79. if itemstack:get_wear() > 60135 then
  80. minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!"))
  81. minetest.sound_play("default_tool_breaks", {
  82. to_player = user:get_player_name(),
  83. gain = 2.0,
  84. })
  85. end
  86. local level = toolranks.get_level(dugnodes)
  87. if lastlevel < level then
  88. local levelup_text = S(
  89. "Your @1@2@3 just leveled up!",
  90. toolranks.colors.green,
  91. itemdesc,
  92. toolranks.colors.white
  93. )
  94. minetest.chat_send_player(user:get_player_name(), levelup_text)
  95. minetest.sound_play("toolranks_levelup", {
  96. to_player = user:get_player_name(),
  97. gain = 2.0,
  98. })
  99. itemmeta:set_string("lastlevel", level)
  100. local speed_multiplier = 1 + (level * level_multiplier * (max_speed - 1))
  101. local use_multiplier = 1 + (level * level_multiplier * (max_use - 1))
  102. local caps = table.copy(itemdef.tool_capabilities)
  103. caps.full_punch_interval = caps.full_punch_interval and (caps.full_punch_interval / speed_multiplier)
  104. caps.punch_attack_uses = caps.punch_attack_uses and (caps.punch_attack_uses * use_multiplier)
  105. for _,c in pairs(caps.groupcaps) do
  106. c.uses = c.uses * use_multiplier
  107. for i,t in ipairs(c.times) do
  108. c.times[i] = t / speed_multiplier
  109. end
  110. end
  111. itemmeta:set_tool_capabilities(caps)
  112. end
  113. itemmeta:set_string("description", toolranks.create_description(itemdesc, dugnodes, level))
  114. itemstack:add_wear(digparams.wear)
  115. return itemstack
  116. end
  117. -- Helper function
  118. function toolranks.add_tool(name)
  119. local desc = ItemStack(name):get_definition().description
  120. minetest.override_item(name, {
  121. original_description = desc,
  122. description = toolranks.create_description(desc, 0, 1),
  123. after_use = toolranks.new_afteruse
  124. })
  125. end
  126. -- Sword
  127. toolranks.add_tool("default:sword_wood")
  128. toolranks.add_tool("default:sword_stone")
  129. toolranks.add_tool("default:sword_steel")
  130. toolranks.add_tool("default:sword_bronze")
  131. toolranks.add_tool("default:sword_mese")
  132. toolranks.add_tool("default:sword_diamond")
  133. -- Pickaxe
  134. toolranks.add_tool("default:pick_wood")
  135. toolranks.add_tool("default:pick_stone")
  136. toolranks.add_tool("default:pick_steel")
  137. toolranks.add_tool("default:pick_bronze")
  138. toolranks.add_tool("default:pick_mese")
  139. toolranks.add_tool("default:pick_diamond")
  140. -- Axe
  141. toolranks.add_tool("default:axe_wood")
  142. toolranks.add_tool("default:axe_stone")
  143. toolranks.add_tool("default:axe_steel")
  144. toolranks.add_tool("default:axe_bronze")
  145. toolranks.add_tool("default:axe_mese")
  146. toolranks.add_tool("default:axe_diamond")
  147. -- Shovel
  148. toolranks.add_tool("default:shovel_wood")
  149. toolranks.add_tool("default:shovel_stone")
  150. toolranks.add_tool("default:shovel_steel")
  151. toolranks.add_tool("default:shovel_bronze")
  152. toolranks.add_tool("default:shovel_mese")
  153. toolranks.add_tool("default:shovel_diamond")
  154. minetest.log("action", "[toolranks] loaded.")