init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. toolranks = {
  2. colors = {
  3. grey = minetest.get_color_escape_sequence("#9d9d9d"),
  4. green = minetest.get_color_escape_sequence("#1eff00"),
  5. gold = minetest.get_color_escape_sequence("#ffdf00"),
  6. white = minetest.get_color_escape_sequence("#ffffff")
  7. }
  8. }
  9. function toolranks.create_description(name, uses, level)
  10. return toolranks.colors.green .. (name or "") .. "\n"
  11. .. toolranks.colors.gold .. "Level: " .. (level or 1) .. "\n"
  12. .. toolranks.colors.grey .. "Used: " .. (uses or 0) .. " times"
  13. end
  14. function toolranks.get_level(uses)
  15. if uses >= 3200 then
  16. return 6
  17. elseif uses >= 2000 then
  18. return 5
  19. elseif uses >= 1000 then
  20. return 4
  21. elseif uses >= 400 then
  22. return 3
  23. elseif uses >= 200 then
  24. return 2
  25. else
  26. return 1
  27. end
  28. end
  29. function toolranks.new_afteruse(itemstack, user, node, digparams)
  30. -- Get tool metadata and number of times used
  31. local itemmeta = itemstack:get_meta()
  32. local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
  33. -- Only count nodes that spend the tool
  34. if digparams.wear > 0 then
  35. dugnodes = dugnodes + 1
  36. itemmeta:set_string("dug", dugnodes)
  37. else
  38. return
  39. end
  40. -- Get tool description and last level
  41. local itemdef = itemstack:get_definition()
  42. local itemdesc = itemdef.original_description or itemdef.description or "Tool"
  43. local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 1
  44. local name = user:get_player_name()
  45. -- Warn player when tool is almost broken
  46. if itemstack:get_wear() > 60100 then
  47. minetest.chat_send_player(name,
  48. toolranks.colors.gold .. "Your tool is almost broken!")
  49. minetest.sound_play("default_tool_breaks", {
  50. to_player = name,
  51. gain = 2.0
  52. })
  53. end
  54. local level = toolranks.get_level(dugnodes)
  55. -- Alert player when tool has leveled up
  56. if lastlevel < level then
  57. minetest.chat_send_player(name, "Your "
  58. .. toolranks.colors.green .. itemdesc
  59. .. toolranks.colors.white .. " just leveled up!")
  60. minetest.sound_play("toolranks_levelup", {
  61. to_player = name,
  62. gain = 1.0
  63. })
  64. itemmeta:set_string("lastlevel", level)
  65. end
  66. local newdesc = toolranks.create_description(itemdesc, dugnodes, level)
  67. -- Set new meta
  68. itemmeta:set_string("description", newdesc)
  69. local wear = digparams.wear
  70. -- Set wear level
  71. if level > 1 then
  72. wear = digparams.wear * 4 / (4 + level)
  73. end
  74. itemstack:add_wear(wear)
  75. return itemstack
  76. end
  77. -- Helper function
  78. local function add_tool(name, desc, afteruse)
  79. minetest.override_item(name, {
  80. original_description = desc,
  81. description = toolranks.create_description(desc, 0, 1),
  82. after_use = afteruse and toolranks.new_afteruse
  83. })
  84. end
  85. -- Sword
  86. add_tool("default:sword_wood", "Wooden Sword", true)
  87. add_tool("default:sword_stone", "Stone Sword", true)
  88. add_tool("default:sword_steel", "Steel Sword", true)
  89. add_tool("default:sword_bronze", "Bronze Sword", true)
  90. add_tool("default:sword_mese", "Mese Sword", true)
  91. add_tool("default:sword_diamond", "Diamond Sword", true)
  92. -- Pickaxe
  93. add_tool("default:pick_wood", "Wooden Pickaxe", true)
  94. add_tool("default:pick_stone", "Stone Pickaxe", true)
  95. add_tool("default:pick_steel", "Steel Pickaxe", true)
  96. add_tool("default:pick_bronze", "Bronze Pickaxe", true)
  97. add_tool("default:pick_mese", "Mese Pickaxe", true)
  98. add_tool("default:pick_diamond", "Diamond Pickaxe", true)
  99. -- Axe
  100. add_tool("default:axe_wood", "Wooden Axe", true)
  101. add_tool("default:axe_stone", "Stone Axe", true)
  102. add_tool("default:axe_steel", "Steel Axe", true)
  103. add_tool("default:axe_bronze", "Bronze Axe", true)
  104. add_tool("default:axe_mese", "Mese Axe", true)
  105. add_tool("default:axe_diamond", "Diamond Axe", true)
  106. -- Shovel
  107. add_tool("default:shovel_wood", "Wooden Shovel", true)
  108. add_tool("default:shovel_stone", "Stone Shovel", true)
  109. add_tool("default:shovel_steel", "Steel Shovel", true)
  110. add_tool("default:shovel_bronze", "Bronze Shovel", true)
  111. add_tool("default:shovel_mese", "Mese Shovel", true)
  112. add_tool("default:shovel_diamond", "Diamond Shovel", true)