init.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. -- Interval between movement checks (in seconds).
  2. local INTERVAL = 5
  3. -- Minimum distance to move to register as not AFK (in blocks).
  4. local MINDIST = 0.4
  5. -- If player does not move within this time, 'sit' player (in seconds) and label them as afk.
  6. local TIMEOUT = 300 -- 300 = 5 minutes
  7. local time_afk = {} -- a table indexed by plname that indicates the approximate time since last moved
  8. local last_pos = {} -- a table of position vectors that are indexed by plname
  9. local chat_afk = {} -- a table of booleans indexed by plname that indicate if players are afk, used to indicate if we have placed a chat message that indicates them as afk
  10. local chat_noafk = {} -- a table of booleans indexed by plname that indicate if players are not afk
  11. local beds_path = minetest.get_modpath("beds")
  12. local attached_before_afk = {}
  13. -- Function to check if the player is afk.
  14. local function check_moved()
  15. for _, p in ipairs(minetest.get_connected_players()) do
  16. local plname = p:get_player_name()
  17. local in_bed = false
  18. if beds_path then
  19. if beds.player[plname] then
  20. in_bed = true
  21. end
  22. end
  23. local pos = p:getpos()
  24. local sit = false -- for now, assume that they are not afk. Well change that if we need to later on in this function
  25. -- (below) if the player is not registered on the afk and no afk list, add them with them not being afk
  26. if chat_noafk[plname] == nil then
  27. attached_before_afk[plname] = default.player_attached[plname]
  28. chat_afk[plname] = false
  29. chat_noafk[plname] = true
  30. end
  31. -- if the plname index can be found on the last_pos table then: (the plname index would not be found if they joined since the last interval afk check... we'll get them next time around bc of setting thire name o the table)
  32. if last_pos[plname] then
  33. --get the distance between their current position and their last postion, d is distance
  34. local d = vector.distance(last_pos[plname], pos)
  35. -- print("Player: "..plname..", Dist: "..d)
  36. if d < MINDIST then -- if d is less than minimum distance then
  37. time_afk[plname] = (time_afk[plname] or 0) + INTERVAL -- add the checking interval to the player's time afk (eg not moved) number
  38. --(below)and if the time not moved (time_afk at index plname) is more than the timeout time then sit them and post afk message to chat if we haven't already
  39. if time_afk[plname] >= TIMEOUT then --if we've reached timeout on the afk time table or are beyond that timeout then
  40. default.player_set_animation(p, "sit") -- make sure that the player is sitting
  41. -- ^^^THIS COULD CAUSE A PROBLEM... what if they are already laying in bed and are afk? now they will be sitting in bed.-- fixed
  42. sit = true --indicate that the player is sitting and that we have caused the sitting, for this iteration (used to keep player's animation as lay if they are dead)
  43. chat_noafk[plname] = false --take them off the list of players that are not afk
  44. if chat_afk[plname] == false then -- so they ARE afk after the timeout, but if they are not on the list of players that we have given an afk chat message about, then give the chat message now, and then indicate that we have given an afk message about them, so that we don't do it again.
  45. minetest.chat_send_all("*** "..plname.." is AFK.") -- changed the number of stars to differentiate it from user-generated messages
  46. chat_afk[plname] = true
  47. attached_before_afk[plname] = default.player_attached[plname] -- for first time afk, the attached state has not been messed with yet by this mod. Save its state now to know whether to unattach the player when they come back
  48. end
  49. default.player_attached[plname] = true --I switched this in its order to be after the test for first time afk, so we could determine the original attached state
  50. end
  51. else -- oh, wait, if they have moved more than the minumum distance, then reset their afk time
  52. time_afk[plname] = 0
  53. end
  54. end
  55. if not sit then --if we have not made them sit this iteration then
  56. last_pos[plname] = pos --set their last postion indicator to their current position for next iteration so we can detect movement
  57. chat_afk[plname] = false --make sure we know that they are not afk for future reference
  58. -- If a player returns and the status changes, we print a message to chat.
  59. if chat_noafk[plname] == false then --if we didn't make them sit this iteration, its bc they are active. If their index on the not_afk list says that they ARE afk, then we haven't updated that info. SO,
  60. minetest.chat_send_all("*** "..plname.." came back from AFK.") --let everyone know
  61. chat_noafk[plname] = true --and update their status
  62. if not(attached_before_afk[plname]) then -- only change animation and attached state to false IF they were not attached before they went afk. Let other mods handle setting them to not attached if they caused it
  63. default.player_attached[plname] = false -- let the server know that they are not attached anymore - the server will stand them up.
  64. default.player_set_animation(p, "stand") -- and make them stand up. I moved this from where it was setting the stand animation every check, to where it sets it only when coming back from afk
  65. end
  66. end
  67. end
  68. -- If players are dead and AFK or if they are in bed, keep their model in the lay position, not sit. Spawn particles
  69. if (p:get_hp() == 0 or in_bed) and sit then --if they are dead or are lying in bed and would otherwise be sitting bc of afk, revoke the sit anim command, instead set it to lay
  70. default.player_set_animation(p, "lay")
  71. if p:get_hp() == 0 then
  72. --spawn fly and stink particles here if they are afk and dead
  73. minetest.add_particlespawner({
  74. amount = 15,
  75. time = INTERVAL,
  76. minpos = {x=pos.x -.8 , y=pos.y + .1, z=pos.z -.8},
  77. maxpos = {x=pos.x +.8 , y=pos.y + 1, z=pos.z +.8},
  78. minvel = {x=0, y=0, z=0},
  79. maxvel = {x=-.1, y=-.1, z=-0.1},
  80. minacc = {x=0, y=0, z=0},
  81. maxacc = {x=0, y=0, z=0},
  82. minexptime = INTERVAL- .5*INTERVAL,
  83. maxexptime = INTERVAL,
  84. minsize = 4,
  85. maxsize = 4,
  86. collisiondetection = true,
  87. vertical = false,
  88. texture = "afk_fly_anim.png",
  89. animation = {
  90. type = "vertical_frames",
  91. aspect_w = 16,
  92. -- ^ specify width of a frame in pixels
  93. aspect_h = 16,
  94. -- ^ specify height of a frame in pixels
  95. length = .2,
  96. -- ^ specify full loop length
  97. },
  98. })
  99. end
  100. end
  101. if (sit and not(p:get_hp() == 0)) or (in_bed and (not(p:get_hp() == 0))) then-- if they are afk and not dead then
  102. -- spawn zzz particles here
  103. local bed_offset = 0
  104. if in_bed then
  105. bed_offset = -1
  106. end
  107. minetest.add_particlespawner({
  108. amount = 5,
  109. time = INTERVAL,
  110. minpos = {x=pos.x -.4 , y=pos.y + 1.2 + bed_offset, z=pos.z -.4},
  111. maxpos = {x=pos.x +.4 , y=pos.y + 1.5 + bed_offset, z=pos.z +.4},
  112. minvel = {x=0, y=.1, z=0},
  113. maxvel = {x=0.1, y=.3, z=0.1},
  114. minacc = {x=0, y=.1, z=0},
  115. maxacc = {x=0.01, y=.3, z=0.01},
  116. minexptime = INTERVAL- .5*INTERVAL,
  117. maxexptime = INTERVAL,
  118. minsize = 1,
  119. maxsize = 5,
  120. collisiondetection = true,
  121. vertical = false,
  122. texture = "afk_z_anim.png",
  123. animation = {
  124. type = "vertical_frames",
  125. aspect_w = 16,
  126. -- ^ specify width of a frame in pixels
  127. aspect_h = 16,
  128. -- ^ specify height of a frame in pixels
  129. length = INTERVAL,
  130. -- ^ specify full loop length
  131. },
  132. })
  133. end
  134. end
  135. minetest.after(INTERVAL, check_moved)
  136. end
  137. minetest.after(INTERVAL, check_moved)
  138. minetest.register_on_leaveplayer(function(player) -- clean things up
  139. local plname = player:get_player_name()
  140. time_afk[plname] = nil
  141. last_pos[plname] = nil
  142. chat_afk[plname] = nil
  143. chat_noafk[plname] = nil
  144. attached_before_afk[plname] = nil
  145. end)