init.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ap = ap or {}
  2. ap.modpath = ap.modpath or minetest.get_modpath("ap")
  3. ap.players = ap.players or {}
  4. -- Number of seconds to keep track of player's reported positions.
  5. -- This must be at least 1 (though such a small value is NOT useful).
  6. ap.record_time = 60
  7. function ap.get_record_time()
  8. return ap.record_time
  9. end
  10. function ap.update_players()
  11. local players = minetest.get_connected_players()
  12. for i=1, #players, 1 do
  13. local pref = players[i]
  14. local p = pref:get_pos() -- Note: position is NOT rounded.
  15. local t = ap.players[pref:get_player_name()].positions
  16. -- Don't add position to list of last recorded positions if the player
  17. -- hasn't moved since last time.
  18. local add = true
  19. if #t > 0 then
  20. local op = t[#t].pos
  21. if vector.distance(op, p) < 1 then
  22. add = false
  23. end
  24. end
  25. -- Insert position into player's record (for this session) and remove old
  26. -- entries from the beginning.
  27. if add then
  28. table.insert(t, {pos=p, time=os.time()})
  29. if #t > ap.record_time then
  30. table.remove(t, 1)
  31. end
  32. end
  33. end
  34. end
  35. -- Returns a list of positions for this player in the last few seconds,
  36. -- or an empty table if that player wasn't loaded.
  37. -- Each entry is a table in the format {pos, time}.
  38. function ap.get_position_list(pname)
  39. return ap.players[pname].positions or {}
  40. end
  41. function ap.on_joinplayer(pref)
  42. ap.players[pref:get_player_name()] = {
  43. positions = {},
  44. }
  45. end
  46. function ap.on_leaveplayer(pref)
  47. ap.players[pref:get_player_name()] = nil
  48. end
  49. local time = 0
  50. function ap.global_step(dtime)
  51. time = time + dtime
  52. if time < 1 then return end
  53. time = 0
  54. ap.update_players()
  55. end
  56. if not ap.registered then
  57. local c = "ap:core"
  58. local f = ap.modpath .. "/init.lua"
  59. reload.register_file(c, f, false)
  60. minetest.register_on_joinplayer(function(...)
  61. ap.on_joinplayer(...) end)
  62. minetest.register_on_leaveplayer(function(...)
  63. ap.on_leaveplayer(...) end)
  64. minetest.register_globalstep(function(...)
  65. ap.global_step(...) end)
  66. ap.registered = true
  67. end