legacy.lua 804 B

12345678910111213141516171819202122232425262728293031323334353637
  1. -- Localize for performance.
  2. local math_floor = math.floor
  3. local armor_org_func = armor.set_player_armor
  4. local function get_armor_lvl(def)
  5. -- Displays percentage of current wear.
  6. local state = def.state or 0
  7. local count = def.count or 0
  8. local total = count * 65535
  9. -- D'T div by 0.
  10. if count == 0 then return 0 end
  11. local percent = state / total
  12. local invperc = 1 - percent
  13. if invperc > 1 then invperc = 1 end
  14. if invperc < 0 then invperc = 0 end
  15. return math_floor(tonumber(20 * (invperc)))
  16. end
  17. function armor.set_player_armor(self, player)
  18. armor_org_func(self, player)
  19. local name = player:get_player_name()
  20. local def = self.def
  21. local armor_lvl = 0
  22. if def[name] then
  23. armor_lvl = get_armor_lvl(def[name])
  24. end
  25. hud.change_item(player, "armor", {number = armor_lvl, max = 20})
  26. end