functions.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. if beds.player[name] then
  91. -- player already in bed, do nothing
  92. return false
  93. end
  94. beds.pos[name] = pos
  95. beds.bed_position[name] = bed_pos
  96. beds.player[name] = {physics_override = player:get_physics_override()}
  97. -- physics, eye_offset, etc
  98. player:set_eye_offset({x = 0, y = -13, z = 0}, {x = 0, y = 0, z = 0})
  99. local yaw, param2 = get_look_yaw(bed_pos)
  100. player:set_look_horizontal(yaw)
  101. local dir = minetest.facedir_to_dir(param2)
  102. -- p.y is just above the nodebox height of the 'Simple Bed' (the highest bed),
  103. -- to avoid sinking down through the bed.
  104. local p = {
  105. x = bed_pos.x + dir.x / 2,
  106. y = bed_pos.y + 0.07,
  107. z = bed_pos.z + dir.z / 2
  108. }
  109. player:set_physics_override({speed = 0, jump = 0, gravity = 0})
  110. player:set_pos(p)
  111. player_api.player_attached[name] = true
  112. hud_flags.wielditem = false
  113. player_api.set_animation(player, "lay" , 0)
  114. end
  115. player:hud_set_flags(hud_flags)
  116. end
  117. local function get_player_in_bed_count()
  118. local c = 0
  119. for _, _ in pairs(beds.player) do
  120. c = c + 1
  121. end
  122. return c
  123. end
  124. local function update_formspecs(finished)
  125. local ges = #minetest.get_connected_players()
  126. local player_in_bed = get_player_in_bed_count()
  127. local is_majority = (ges / 2) < player_in_bed
  128. local form_n
  129. local esc = minetest.formspec_escape
  130. if finished then
  131. form_n = beds.formspec .. "label[2.7,9;" .. esc(S("Good morning.")) .. "]"
  132. else
  133. form_n = beds.formspec .. "label[2.2,9;" ..
  134. esc(S("@1 of @2 players are in bed", player_in_bed, ges)) .. "]"
  135. if is_majority and is_night_skip_enabled() then
  136. form_n = form_n .. "button_exit[2,6;4,0.75;force;" ..
  137. esc(S("Force night skip")) .. "]"
  138. end
  139. end
  140. for name,_ in pairs(beds.player) do
  141. minetest.show_formspec(name, "beds_form", form_n)
  142. end
  143. end
  144. -- Public functions
  145. function beds.kick_players()
  146. for name, _ in pairs(beds.player) do
  147. local player = minetest.get_player_by_name(name)
  148. lay_down(player, nil, nil, false)
  149. end
  150. end
  151. function beds.skip_night()
  152. minetest.set_timeofday(0.23)
  153. end
  154. function beds.on_rightclick(pos, player)
  155. local name = player:get_player_name()
  156. local ppos = player:get_pos()
  157. local tod = minetest.get_timeofday()
  158. if tod > 0.2 and tod < 0.805 then
  159. if beds.player[name] then
  160. lay_down(player, nil, nil, false)
  161. end
  162. minetest.chat_send_player(name, S("You can only sleep at night."))
  163. return
  164. end
  165. -- move to bed
  166. if not beds.player[name] then
  167. lay_down(player, ppos, pos)
  168. beds.set_spawns() -- save respawn positions when entering bed
  169. else
  170. lay_down(player, nil, nil, false)
  171. end
  172. if not is_sp then
  173. update_formspecs(false)
  174. end
  175. -- skip the night and let all players stand up
  176. if check_in_beds() then
  177. minetest.after(2, function()
  178. if not is_sp then
  179. update_formspecs(is_night_skip_enabled())
  180. end
  181. if is_night_skip_enabled() then
  182. beds.skip_night()
  183. beds.kick_players()
  184. end
  185. end)
  186. end
  187. end
  188. function beds.can_dig(bed_pos)
  189. -- Check all players in bed which one is at the expected position
  190. for _, player_bed_pos in pairs(beds.bed_position) do
  191. if vector.equals(bed_pos, player_bed_pos) then
  192. return false
  193. end
  194. end
  195. return true
  196. end
  197. -- Callbacks
  198. -- Only register respawn callback if respawn enabled
  199. if enable_respawn then
  200. -- respawn player at bed if enabled and valid position is found
  201. minetest.register_on_respawnplayer(function(player)
  202. local name = player:get_player_name()
  203. local pos = beds.spawn[name]
  204. if pos then
  205. player:set_pos(pos)
  206. return true
  207. end
  208. end)
  209. end
  210. minetest.register_on_leaveplayer(function(player)
  211. local name = player:get_player_name()
  212. lay_down(player, nil, nil, false, true)
  213. beds.player[name] = nil
  214. if check_in_beds() then
  215. minetest.after(2, function()
  216. update_formspecs(is_night_skip_enabled())
  217. if is_night_skip_enabled() then
  218. beds.skip_night()
  219. beds.kick_players()
  220. end
  221. end)
  222. end
  223. end)
  224. minetest.register_on_dieplayer(function(player)
  225. local name = player:get_player_name()
  226. local in_bed = beds.player
  227. local pos = player:get_pos()
  228. local yaw = get_look_yaw(pos)
  229. if in_bed[name] then
  230. lay_down(player, nil, pos, false)
  231. player:set_look_horizontal(yaw)
  232. player:set_pos(pos)
  233. end
  234. end)
  235. minetest.register_on_player_receive_fields(function(player, formname, fields)
  236. if formname ~= "beds_form" then
  237. return
  238. end
  239. -- Because "Force night skip" button is a button_exit, it will set fields.quit
  240. -- and lay_down call will change value of player_in_bed, so it must be taken
  241. -- earlier.
  242. local last_player_in_bed = get_player_in_bed_count()
  243. if fields.quit or fields.leave then
  244. lay_down(player, nil, nil, false)
  245. update_formspecs(false)
  246. end
  247. if fields.force then
  248. local is_majority = (#minetest.get_connected_players() / 2) < last_player_in_bed
  249. if is_majority and is_night_skip_enabled() then
  250. update_formspecs(true)
  251. beds.skip_night()
  252. beds.kick_players()
  253. else
  254. update_formspecs(false)
  255. end
  256. end
  257. end)