functions.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. local pi = math.pi
  2. local is_sp = minetest.is_singleplayer()
  3. local enable_respawn = minetest.settings:get_bool("enable_bed_respawn")
  4. if enable_respawn == nil then
  5. enable_respawn = true
  6. end
  7. -- support for MT game translation.
  8. local S = beds.get_translator
  9. -- Helper functions
  10. local function get_look_yaw(pos)
  11. local rotation = minetest.get_node(pos).param2
  12. if rotation > 3 then
  13. rotation = rotation % 4 -- Mask colorfacedir values
  14. end
  15. if rotation == 1 then
  16. return pi / 2, rotation
  17. elseif rotation == 3 then
  18. return -pi / 2, rotation
  19. elseif rotation == 0 then
  20. return pi, rotation
  21. else
  22. return 0, rotation
  23. end
  24. end
  25. local function is_night_skip_enabled()
  26. local enable_night_skip = minetest.settings:get_bool("enable_bed_night_skip")
  27. if enable_night_skip == nil then
  28. enable_night_skip = true
  29. end
  30. return enable_night_skip
  31. end
  32. local function check_in_beds(players)
  33. local in_bed = beds.player
  34. if not players then
  35. players = minetest.get_connected_players()
  36. end
  37. for n, player in ipairs(players) do
  38. local name = player:get_player_name()
  39. if not in_bed[name] then
  40. return false
  41. end
  42. end
  43. return #players > 0
  44. end
  45. local function lay_down(player, pos, bed_pos, state, skip)
  46. local name = player:get_player_name()
  47. local hud_flags = player:hud_get_flags()
  48. if not player or not name then
  49. return
  50. end
  51. -- stand up
  52. if state ~= nil and not state then
  53. if not beds.player[name] then
  54. -- player not in bed, do nothing
  55. return false
  56. end
  57. beds.bed_position[name] = nil
  58. -- skip here to prevent sending player specific changes (used for leaving players)
  59. if skip then
  60. return
  61. end
  62. player:set_pos(beds.pos[name])
  63. -- physics, eye_offset, etc
  64. local physics_override = beds.player[name].physics_override
  65. beds.player[name] = nil
  66. player:set_physics_override({
  67. speed = physics_override.speed,
  68. jump = physics_override.jump,
  69. gravity = physics_override.gravity
  70. })
  71. player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  72. player:set_look_horizontal(math.random(1, 180) / 100)
  73. player_api.player_attached[name] = false
  74. hud_flags.wielditem = true
  75. player_api.set_animation(player, "stand" , 30)
  76. -- lay down
  77. else
  78. -- Check if bed is occupied
  79. for _, other_pos in pairs(beds.bed_position) do
  80. if vector.distance(bed_pos, other_pos) < 0.1 then
  81. minetest.chat_send_player(name, S("This bed is already occupied!"))
  82. return false
  83. end
  84. end
  85. -- Check if player is moving
  86. if vector.length(player:get_velocity()) > 0.001 then
  87. minetest.chat_send_player(name, S("You have to stop moving before going to bed!"))
  88. return false
  89. end
  90. -- Check if player is attached to an object
  91. if player:get_attach() then
  92. return false
  93. end
  94. if beds.player[name] then
  95. -- player already in bed, do nothing
  96. return false
  97. end
  98. beds.pos[name] = pos
  99. beds.bed_position[name] = bed_pos
  100. beds.player[name] = {physics_override = player:get_physics_override()}
  101. local yaw, param2 = get_look_yaw(bed_pos)
  102. player:set_look_horizontal(yaw)
  103. local dir = minetest.facedir_to_dir(param2)
  104. -- p.y is just above the nodebox height of the 'Simple Bed' (the highest bed),
  105. -- to avoid sinking down through the bed.
  106. local p = {
  107. x = bed_pos.x + dir.x / 2,
  108. y = bed_pos.y + 0.07,
  109. z = bed_pos.z + dir.z / 2
  110. }
  111. player:set_physics_override({speed = 0, jump = 0, gravity = 0})
  112. player:set_pos(p)
  113. player_api.player_attached[name] = true
  114. hud_flags.wielditem = false
  115. player_api.set_animation(player, "lay" , 0)
  116. end
  117. player:hud_set_flags(hud_flags)
  118. end
  119. local function get_player_in_bed_count()
  120. local c = 0
  121. for _, _ in pairs(beds.player) do
  122. c = c + 1
  123. end
  124. return c
  125. end
  126. local function update_formspecs(finished)
  127. local ges = #minetest.get_connected_players()
  128. local player_in_bed = get_player_in_bed_count()
  129. local is_majority = (ges / 2) < player_in_bed
  130. local form_n
  131. local esc = minetest.formspec_escape
  132. if finished then
  133. form_n = beds.formspec .. "label[2.7,9;" .. esc(S("Good morning.")) .. "]"
  134. else
  135. form_n = beds.formspec .. "label[2.2,9;" ..
  136. esc(S("@1 of @2 players are in bed", player_in_bed, ges)) .. "]"
  137. if is_majority and is_night_skip_enabled() then
  138. form_n = form_n .. "button_exit[2,6;4,0.75;force;" ..
  139. esc(S("Force night skip")) .. "]"
  140. end
  141. end
  142. for name,_ in pairs(beds.player) do
  143. minetest.show_formspec(name, "beds_form", form_n)
  144. end
  145. end
  146. -- Public functions
  147. function beds.kick_players()
  148. for name, _ in pairs(beds.player) do
  149. local player = minetest.get_player_by_name(name)
  150. lay_down(player, nil, nil, false)
  151. end
  152. end
  153. function beds.skip_night()
  154. minetest.set_timeofday(0.23)
  155. end
  156. local update_scheduled = false
  157. local function schedule_update()
  158. if update_scheduled then
  159. -- there already is an update scheduled; don't schedule more to prevent races
  160. return
  161. end
  162. update_scheduled = true
  163. minetest.after(2, function()
  164. update_scheduled = false
  165. if not is_sp then
  166. update_formspecs(is_night_skip_enabled())
  167. end
  168. if is_night_skip_enabled() then
  169. -- skip the night and let all players stand up
  170. beds.skip_night()
  171. beds.kick_players()
  172. end
  173. end)
  174. end
  175. function beds.on_rightclick(pos, player)
  176. local name = player:get_player_name()
  177. local ppos = player:get_pos()
  178. local tod = minetest.get_timeofday()
  179. if tod > beds.day_interval.start and tod < beds.day_interval.finish then
  180. if beds.player[name] then
  181. lay_down(player, nil, nil, false)
  182. end
  183. minetest.chat_send_player(name, S("You can only sleep at night."))
  184. return
  185. end
  186. -- move to bed
  187. if not beds.player[name] then
  188. lay_down(player, ppos, pos)
  189. beds.set_spawns() -- save respawn positions when entering bed
  190. else
  191. lay_down(player, nil, nil, false)
  192. end
  193. if not is_sp then
  194. update_formspecs(false)
  195. end
  196. if check_in_beds() then
  197. schedule_update()
  198. end
  199. end
  200. function beds.can_dig(bed_pos)
  201. -- Check all players in bed which one is at the expected position
  202. for _, player_bed_pos in pairs(beds.bed_position) do
  203. if vector.equals(bed_pos, player_bed_pos) then
  204. return false
  205. end
  206. end
  207. return true
  208. end
  209. -- Callbacks
  210. -- Only register respawn callback if respawn enabled
  211. if enable_respawn then
  212. -- Respawn player at bed if valid position is found
  213. spawn.register_on_spawn(function(player, is_new)
  214. local pos = beds.spawn[player:get_player_name()]
  215. if pos then
  216. player:set_pos(pos)
  217. return true
  218. end
  219. end)
  220. end
  221. minetest.register_on_leaveplayer(function(player)
  222. local name = player:get_player_name()
  223. lay_down(player, nil, nil, false, true)
  224. beds.player[name] = nil
  225. if check_in_beds() then
  226. schedule_update()
  227. end
  228. end)
  229. minetest.register_on_dieplayer(function(player)
  230. local name = player:get_player_name()
  231. local in_bed = beds.player
  232. local pos = player:get_pos()
  233. local yaw = get_look_yaw(pos)
  234. if in_bed[name] then
  235. lay_down(player, nil, pos, false)
  236. player:set_look_horizontal(yaw)
  237. player:set_pos(pos)
  238. end
  239. end)
  240. minetest.register_on_player_receive_fields(function(player, formname, fields)
  241. if formname ~= "beds_form" then
  242. return
  243. end
  244. -- Because "Force night skip" button is a button_exit, it will set fields.quit
  245. -- and lay_down call will change value of player_in_bed, so it must be taken
  246. -- earlier.
  247. local last_player_in_bed = get_player_in_bed_count()
  248. if fields.quit or fields.leave then
  249. lay_down(player, nil, nil, false)
  250. update_formspecs(false)
  251. end
  252. if fields.force then
  253. local is_majority = (#minetest.get_connected_players() / 2) < last_player_in_bed
  254. if is_majority and is_night_skip_enabled() then
  255. update_formspecs(true)
  256. beds.skip_night()
  257. beds.kick_players()
  258. else
  259. update_formspecs(false)
  260. end
  261. end
  262. end)