functions.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. function beds.on_rightclick(pos, player)
  157. local name = player:get_player_name()
  158. local ppos = player:get_pos()
  159. local tod = minetest.get_timeofday()
  160. if tod > beds.day_interval.start and tod < beds.day_interval.finish then
  161. if beds.player[name] then
  162. lay_down(player, nil, nil, false)
  163. end
  164. minetest.chat_send_player(name, S("You can only sleep at night."))
  165. return
  166. end
  167. -- move to bed
  168. if not beds.player[name] then
  169. lay_down(player, ppos, pos)
  170. beds.set_spawns() -- save respawn positions when entering bed
  171. else
  172. lay_down(player, nil, nil, false)
  173. end
  174. if not is_sp then
  175. update_formspecs(false)
  176. end
  177. -- skip the night and let all players stand up
  178. if check_in_beds() then
  179. minetest.after(2, function()
  180. if not is_sp then
  181. update_formspecs(is_night_skip_enabled())
  182. end
  183. if is_night_skip_enabled() then
  184. beds.skip_night()
  185. beds.kick_players()
  186. end
  187. end)
  188. end
  189. end
  190. function beds.can_dig(bed_pos)
  191. -- Check all players in bed which one is at the expected position
  192. for _, player_bed_pos in pairs(beds.bed_position) do
  193. if vector.equals(bed_pos, player_bed_pos) then
  194. return false
  195. end
  196. end
  197. return true
  198. end
  199. -- Callbacks
  200. -- Only register respawn callback if respawn enabled
  201. if enable_respawn then
  202. -- respawn player at bed if enabled and valid position is found
  203. minetest.register_on_respawnplayer(function(player)
  204. local name = player:get_player_name()
  205. local pos = beds.spawn[name]
  206. if pos then
  207. player:set_pos(pos)
  208. return true
  209. end
  210. end)
  211. end
  212. minetest.register_on_leaveplayer(function(player)
  213. local name = player:get_player_name()
  214. lay_down(player, nil, nil, false, true)
  215. beds.player[name] = nil
  216. if check_in_beds() then
  217. minetest.after(2, function()
  218. update_formspecs(is_night_skip_enabled())
  219. if is_night_skip_enabled() then
  220. beds.skip_night()
  221. beds.kick_players()
  222. end
  223. end)
  224. end
  225. end)
  226. minetest.register_on_dieplayer(function(player)
  227. local name = player:get_player_name()
  228. local in_bed = beds.player
  229. local pos = player:get_pos()
  230. local yaw = get_look_yaw(pos)
  231. if in_bed[name] then
  232. lay_down(player, nil, pos, false)
  233. player:set_look_horizontal(yaw)
  234. player:set_pos(pos)
  235. end
  236. end)
  237. minetest.register_on_player_receive_fields(function(player, formname, fields)
  238. if formname ~= "beds_form" then
  239. return
  240. end
  241. -- Because "Force night skip" button is a button_exit, it will set fields.quit
  242. -- and lay_down call will change value of player_in_bed, so it must be taken
  243. -- earlier.
  244. local last_player_in_bed = get_player_in_bed_count()
  245. if fields.quit or fields.leave then
  246. lay_down(player, nil, nil, false)
  247. update_formspecs(false)
  248. end
  249. if fields.force then
  250. local is_majority = (#minetest.get_connected_players() / 2) < last_player_in_bed
  251. if is_majority and is_night_skip_enabled() then
  252. update_formspecs(true)
  253. beds.skip_night()
  254. beds.kick_players()
  255. else
  256. update_formspecs(false)
  257. end
  258. end
  259. end)