init.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. xp = xp or {}
  2. xp.modpath = minetest.get_modpath("xp")
  3. xp.digxp_max = 1000000
  4. xp.data = xp.data or {} -- Data is stored in string form.
  5. xp.dirty = true
  6. xp.dirty_players = xp.dirty_players or {}
  7. -- Localize for performance.
  8. local math_floor = math.floor
  9. local math_min = math.min
  10. local math_max = math.max
  11. -- This code supports multiple types of XP.
  12. -- Different types of XP are stored seperately.
  13. function xp.set_xp(pname, kind, amount)
  14. local key = pname .. ":" .. kind
  15. xp.data[key] = tostring(amount)
  16. xp.dirty = true
  17. xp.dirty_players[pname] = true
  18. end
  19. function xp.get_xp(pname, kind)
  20. local key = pname .. ":" .. kind
  21. if not xp.data[key] then
  22. return 0
  23. end
  24. return tonumber(xp.data[key])
  25. end
  26. function xp.update_players_max_hp(pname)
  27. local pref = minetest.get_player_by_name(pname)
  28. if not pref then
  29. return
  30. end
  31. local amount = math_max(math_min(xp.get_xp(pname, "digxp"), 50000), 0)
  32. local kilos = (amount / 1000)
  33. local hpinc = math_floor(kilos * 2)
  34. local max_hp = pref:get_properties().hp_max
  35. local hp = pref:get_hp()
  36. local percent = (hp / max_hp)
  37. local new_max_hp = (minetest.PLAYER_MAX_HP_DEFAULT + hpinc)
  38. local new_hp = math_min((percent * new_max_hp), new_max_hp)
  39. --minetest.chat_send_all('new max hp: ' .. new_max_hp .. ', new hp: ' .. new_hp)
  40. pref:set_properties({hp_max = new_max_hp})
  41. pref:set_hp(new_hp)
  42. end
  43. function xp.on_joinplayer(player)
  44. minetest.after(10, xp.update_players_max_hp, player:get_player_name())
  45. end
  46. function xp.write_xp()
  47. if xp.dirty then
  48. local temp = {}
  49. for k, v in pairs(xp.data) do
  50. -- Only save XP if >= min XP.
  51. local n = tonumber(v)
  52. if n >= 5.0 then
  53. temp[k] = v
  54. end
  55. end
  56. xp.storage:from_table({fields=temp})
  57. end
  58. xp.dirty = false
  59. end
  60. local timer = 0
  61. local delay = 60*5
  62. function xp.globalstep(dtime)
  63. timer = timer + dtime
  64. if timer >= delay then
  65. timer = 0
  66. xp.write_xp()
  67. local done_players = {}
  68. for k, v in pairs(xp.dirty_players) do
  69. xp.update_players_max_hp(k)
  70. done_players[k] = true
  71. end
  72. for k, v in pairs(done_players) do
  73. xp.dirty_players[k] = nil
  74. end
  75. end
  76. end
  77. if not xp.run_once then
  78. xp.storage = minetest.get_mod_storage()
  79. -- Load data.
  80. local data = xp.storage:to_table() or {}
  81. xp.data = data.fields or {}
  82. -- Save data.
  83. minetest.register_on_shutdown(function() xp.write_xp() end)
  84. minetest.register_globalstep(function(...) xp.globalstep(...) end)
  85. minetest.register_on_joinplayer(function(...) xp.on_joinplayer(...) end)
  86. local c = "xp:core"
  87. local f = xp.modpath .. "/init.lua"
  88. reload.register_file(c, f, false)
  89. xp.run_once = true
  90. end