init.lua 5.5 KB

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