init.lua 2.4 KB

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