functions.lua 6.6 KB

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