functions.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. local pi = math.pi
  2. local player_in_bed = 0
  3. local is_sp = minetest.is_singleplayer()
  4. local enable_respawn = minetest.settings:get_bool("enable_bed_respawn")
  5. if enable_respawn == nil then
  6. enable_respawn = true
  7. end
  8. -- Helper functions
  9. local function get_look_yaw(pos)
  10. local rotation = minetest.get_node(pos).param2
  11. if rotation > 3 then
  12. rotation = rotation % 4 -- Mask colorfacedir values
  13. end
  14. if rotation == 1 then
  15. return pi / 2, rotation
  16. elseif rotation == 3 then
  17. return -pi / 2, rotation
  18. elseif rotation == 0 then
  19. return pi, rotation
  20. else
  21. return 0, rotation
  22. end
  23. end
  24. local function is_night_skip_enabled()
  25. local enable_night_skip = minetest.settings:get_bool("enable_bed_night_skip")
  26. if enable_night_skip == nil then
  27. enable_night_skip = true
  28. end
  29. return enable_night_skip
  30. end
  31. local function check_in_beds(players)
  32. local in_bed = beds.player
  33. if not players then
  34. players = minetest.get_connected_players()
  35. end
  36. for n, player in ipairs(players) do
  37. local name = player:get_player_name()
  38. if not in_bed[name] then
  39. return false
  40. end
  41. end
  42. return #players > 0
  43. end
  44. local function lay_down(player, pos, bed_pos, state, skip)
  45. local name = player:get_player_name()
  46. local hud_flags = player:hud_get_flags()
  47. if not player or not name then
  48. return
  49. end
  50. -- stand up
  51. if state ~= nil and not state then
  52. local p = beds.pos[name] or nil
  53. if beds.player[name] ~= nil then
  54. beds.player[name] = nil
  55. player_in_bed = player_in_bed - 1
  56. end
  57. -- skip here to prevent sending player specific changes (used for leaving players)
  58. if skip then
  59. return
  60. end
  61. if p then
  62. player:setpos(p)
  63. end
  64. -- physics, eye_offset, etc
  65. player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  66. player:set_look_horizontal(math.random(1, 180) / 100)
  67. default.player_attached[name] = false
  68. player:set_physics_override(1, 1, 1)
  69. hud_flags.wielditem = true
  70. default.player_set_animation(player, "stand" , 30)
  71. -- lay down
  72. else
  73. beds.player[name] = 1
  74. beds.pos[name] = pos
  75. player_in_bed = player_in_bed + 1
  76. -- physics, eye_offset, etc
  77. player:set_eye_offset({x = 0, y = -13, z = 0}, {x = 0, y = 0, z = 0})
  78. local yaw, param2 = get_look_yaw(bed_pos)
  79. player:set_look_horizontal(yaw)
  80. local dir = minetest.facedir_to_dir(param2)
  81. local p = {x = bed_pos.x + dir.x / 2, y = bed_pos.y, z = bed_pos.z + dir.z / 2}
  82. player:set_physics_override(0, 0, 0)
  83. player:setpos(p)
  84. default.player_attached[name] = true
  85. hud_flags.wielditem = false
  86. default.player_set_animation(player, "lay" , 0)
  87. end
  88. player:hud_set_flags(hud_flags)
  89. end
  90. local function update_formspecs(finished)
  91. local ges = #minetest.get_connected_players()
  92. local form_n
  93. local is_majority = (ges / 2) < player_in_bed
  94. if finished then
  95. form_n = beds.formspec .. "label[2.7,11; Good morning.]"
  96. else
  97. form_n = beds.formspec .. "label[2.2,11;" .. tostring(player_in_bed) ..
  98. " of " .. tostring(ges) .. " players are in bed]"
  99. if is_majority and is_night_skip_enabled() then
  100. form_n = form_n .. "button_exit[2,8;4,0.75;force;Force night skip]"
  101. end
  102. end
  103. for name,_ in pairs(beds.player) do
  104. minetest.show_formspec(name, "beds_form", form_n)
  105. end
  106. end
  107. -- Public functions
  108. function beds.kick_players()
  109. for name, _ in pairs(beds.player) do
  110. local player = minetest.get_player_by_name(name)
  111. lay_down(player, nil, nil, false)
  112. end
  113. end
  114. function beds.skip_night()
  115. minetest.set_timeofday(0.23)
  116. end
  117. function beds.on_rightclick(pos, player)
  118. local name = player:get_player_name()
  119. local ppos = player:getpos()
  120. local tod = minetest.get_timeofday()
  121. if tod > 0.2 and tod < 0.805 then
  122. if beds.player[name] then
  123. lay_down(player, nil, nil, false)
  124. end
  125. minetest.chat_send_player(name, "You can only sleep at night.")
  126. return
  127. end
  128. -- move to bed
  129. if not beds.player[name] then
  130. lay_down(player, ppos, pos)
  131. beds.set_spawns() -- save respawn positions when entering bed
  132. else
  133. lay_down(player, nil, nil, false)
  134. end
  135. if not is_sp then
  136. update_formspecs(false)
  137. end
  138. -- skip the night and let all players stand up
  139. if check_in_beds() then
  140. minetest.after(2, function()
  141. if not is_sp then
  142. update_formspecs(is_night_skip_enabled())
  143. end
  144. if is_night_skip_enabled() then
  145. beds.skip_night()
  146. beds.kick_players()
  147. end
  148. end)
  149. end
  150. end
  151. -- Callbacks
  152. -- Only register respawn callback if respawn enabled
  153. if enable_respawn then
  154. -- respawn player at bed if enabled and valid position is found
  155. minetest.register_on_respawnplayer(function(player)
  156. local name = player:get_player_name()
  157. local pos = beds.spawn[name]
  158. if pos then
  159. player:setpos(pos)
  160. return true
  161. end
  162. end)
  163. end
  164. minetest.register_on_leaveplayer(function(player)
  165. local name = player:get_player_name()
  166. lay_down(player, nil, nil, false, true)
  167. beds.player[name] = nil
  168. if check_in_beds() then
  169. minetest.after(2, function()
  170. update_formspecs(is_night_skip_enabled())
  171. if is_night_skip_enabled() then
  172. beds.skip_night()
  173. beds.kick_players()
  174. end
  175. end)
  176. end
  177. end)
  178. minetest.register_on_player_receive_fields(function(player, formname, fields)
  179. if formname ~= "beds_form" then
  180. return
  181. end
  182. if fields.quit or fields.leave then
  183. lay_down(player, nil, nil, false)
  184. update_formspecs(false)
  185. end
  186. if fields.force then
  187. update_formspecs(is_night_skip_enabled())
  188. if is_night_skip_enabled() then
  189. beds.skip_night()
  190. beds.kick_players()
  191. end
  192. end
  193. end)