position_logger.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --[[
  2. -- Localize vector.distance() for performance.
  3. local vector_distance = vector.distance
  4. local math_floor = math.floor
  5. gdac.position_logger_path = minetest.get_worldpath() .. "/positions.txt"
  6. gdac.position_logger_players = gdac.position_logger_players or {}
  7. function gdac.position_logger_record(pname, pos, time, attached, sneak, sprint, jump)
  8. local s = pname .. "|" ..
  9. math_floor(pos.x) .. "," .. math_floor(pos.y) .. "," .. math_floor(pos.z) .. "|" ..
  10. time .. "|" .. attached .. "|" .. sneak .. "|" .. sprint .. "|" .. jump .. "\n"
  11. gdac.position_logger_file:write(s)
  12. end
  13. local yes_attached = "attached"
  14. local not_attached = "detached"
  15. local yes_sneak = "sneaking"
  16. local not_sneak = "no_sneak"
  17. local yes_sprint = "running"
  18. local not_sprint = "not_run"
  19. local yes_jump = "jumping"
  20. local not_jump = "no_jump"
  21. local time = 0
  22. function gdac.position_logger_step(dtime)
  23. time = time + dtime
  24. if time < 1 then
  25. return
  26. end
  27. time = 0
  28. local players = minetest.get_connected_players()
  29. for i=1, #players, 1 do
  30. local pref = players[i]
  31. local pname = pref:get_player_name()
  32. if not gdac.player_is_admin(pname) then
  33. local prev_pos = gdac.position_logger_players[pname]
  34. local pos = pref:get_pos()
  35. if not prev_pos or vector_distance(prev_pos, pos) >= 1 then
  36. local ctrl = pref:get_player_control()
  37. local sneak = not_sneak
  38. local attach = not_attached
  39. local sprint = not_sprint
  40. local jump = not_jump
  41. if ctrl.sneak then
  42. sneak = yes_sneak
  43. end
  44. if ctrl.aux1 then
  45. sprint = yes_sprint
  46. end
  47. if ctrl.jump then
  48. jump = yes_jump
  49. end
  50. if default.player_attached[pname] then
  51. attach = yes_attached
  52. end
  53. local time = os.time()
  54. gdac.position_logger_record(pname, pos, time, attach, sneak, sprint, jump)
  55. end
  56. gdac.position_logger_players[pname] = pos
  57. end
  58. end
  59. end
  60. -- Code which runs only once per session.
  61. if not gdac.position_logger_registered then
  62. -- Open log file.
  63. gdac.position_logger_file = io.open(gdac.position_logger_path, "a")
  64. -- Register globalstep function.
  65. minetest.register_globalstep(function(...)
  66. return gdac.position_logger_step(...)
  67. end)
  68. gdac.position_logger_registered = true
  69. end
  70. --]]