functions.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. beds = beds or {}
  2. -- Reloadable file.
  3. if not beds.run_functions_once then
  4. local c = "beds:functions"
  5. local f = beds.modpath .. "/functions.lua"
  6. reload.register_file(c, f, false)
  7. beds.run_functions_once = true
  8. end
  9. local pi = math.pi
  10. --local player_in_bed = 0
  11. local enable_respawn = true
  12. local count_players_in_bed = function()
  13. local count = 0
  14. for k, v in pairs(beds.player) do
  15. local nobeds = minetest.check_player_privs(k, {nobeds=true})
  16. -- Ignore AFK folks.
  17. if afk_removal.is_afk(k) then
  18. nobeds = true
  19. end
  20. local registered = passport.player_registered(k)
  21. if not nobeds and registered then
  22. count = count + 1
  23. end
  24. end
  25. return count
  26. end
  27. local get_participating_players = function()
  28. local players = minetest.get_connected_players()
  29. local outp = {}
  30. for k, v in ipairs(players) do
  31. local pname = v:get_player_name()
  32. local nobeds = minetest.check_player_privs(v, {nobeds=true})
  33. -- Ignore AFK folks.
  34. if afk_removal.is_afk(pname) then
  35. nobeds = true
  36. end
  37. local registered = passport.player_registered(pname)
  38. if not nobeds and registered then
  39. outp[#outp+1] = v
  40. end
  41. end
  42. return outp
  43. end
  44. local function get_look_yaw(pos)
  45. local n = minetest.get_node(pos)
  46. if n.param2 == 1 then
  47. return pi / 2, n.param2
  48. elseif n.param2 == 3 then
  49. return -pi / 2, n.param2
  50. elseif n.param2 == 0 then
  51. return pi, n.param2
  52. else
  53. return 0, n.param2
  54. end
  55. end
  56. local function is_night_skip_enabled()
  57. local tod = minetest.get_timeofday()
  58. if tod > 0.2 and tod < 0.805 then
  59. -- Consider nobody in beds during daytime.
  60. return false
  61. end
  62. local enable_night_skip = minetest.setting_getbool("enable_bed_night_skip")
  63. if enable_night_skip == nil then
  64. enable_night_skip = true
  65. end
  66. return enable_night_skip
  67. end
  68. local function check_in_beds()
  69. local in_bed = beds.player
  70. local players = get_participating_players()
  71. for n, player in ipairs(players) do
  72. local name = player:get_player_name()
  73. if not in_bed[name] then
  74. return false
  75. end
  76. end
  77. return #players > 0
  78. end
  79. local function lay_down(player, pos, bed_pos, state, skip)
  80. local name = player:get_player_name()
  81. local hud_flags = player:hud_get_flags()
  82. if not player or not name then
  83. return
  84. end
  85. -- stand up
  86. if state ~= nil and not state then
  87. local p = beds.pos[name] or nil
  88. if beds.player[name] ~= nil then
  89. beds.player[name] = nil
  90. --player_in_bed = player_in_bed - 1
  91. end
  92. -- skip here to prevent sending player specific changes (used for leaving players)
  93. if skip then
  94. return
  95. end
  96. if p then
  97. player:set_pos(p)
  98. end
  99. -- physics, eye_offset, etc
  100. player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  101. player:set_look_horizontal(math.random(1, 180) / 100)
  102. default.player_attached[name] = false
  103. player:set_physics_override(1, 1, 1)
  104. hud_flags.wielditem = true
  105. default.player_set_animation(player, "stand" , 30)
  106. -- lay down
  107. else
  108. beds.player[name] = 1
  109. beds.pos[name] = pos
  110. --player_in_bed = player_in_bed + 1
  111. -- physics, eye_offset, etc
  112. player:set_eye_offset({x = 0, y = -13, z = 0}, {x = 0, y = 0, z = 0})
  113. local yaw, param2 = get_look_yaw(bed_pos)
  114. player:set_look_horizontal(yaw)
  115. local dir = minetest.facedir_to_dir(param2)
  116. local p = {x = bed_pos.x + dir.x / 2, y = bed_pos.y, z = bed_pos.z + dir.z / 2}
  117. player:set_physics_override(0, 0, 0)
  118. player:set_pos(p)
  119. default.player_attached[name] = true
  120. hud_flags.wielditem = false
  121. default.player_set_animation(player, "lay" , 0)
  122. end
  123. player:hud_set_flags(hud_flags)
  124. end
  125. local function update_formspecs(finished)
  126. local ges = #get_participating_players()
  127. local form_n
  128. local ppl_in_bed = count_players_in_bed()
  129. local is_majority = (ges / 2) < ppl_in_bed
  130. if finished then
  131. form_n = beds.formspec .. "label[2.7,11;Good morning.]"
  132. else
  133. form_n = beds.formspec .. "label[2.2,11;" .. tostring(ppl_in_bed) ..
  134. " of " .. tostring(ges) .. " players are in bed.]"
  135. if is_majority and is_night_skip_enabled() then
  136. form_n = form_n .. "button_exit[2,8;4,0.75;force;Force Night Skip]"
  137. end
  138. end
  139. for name,_ in pairs(beds.player) do
  140. minetest.show_formspec(name, "beds:detatched_formspec", form_n)
  141. end
  142. end
  143. function beds.kick_players()
  144. for name, _ in pairs(beds.player) do
  145. local player = minetest.get_player_by_name(name)
  146. lay_down(player, nil, nil, false)
  147. end
  148. end
  149. function beds.kick_one_player(name)
  150. local player = minetest.get_player_by_name(name)
  151. if player and player:is_player() then
  152. if beds.player[name] ~= nil then
  153. beds.player[name] = nil
  154. lay_down(player, nil, nil, false)
  155. update_formspecs(false)
  156. return true
  157. end
  158. end
  159. end
  160. function beds.skip_night()
  161. minetest.set_timeofday(0.23)
  162. -- This assumes that players aren't kicked out of beds until after this function runs.
  163. for k, v in pairs(beds.player) do
  164. local pname = k
  165. -- HUD update is ignored if minetest.after() isn't used?
  166. -- Oh well. A little delay is nice, too.
  167. minetest.after(0.5, function()
  168. local player = minetest.get_player_by_name(pname)
  169. if player then
  170. -- Heal player 4 HP, but not if the player is dead.
  171. if player:get_hp() > 0 then
  172. player:set_hp(player:get_hp() + 4)
  173. end
  174. -- Increase player's hunger.
  175. hunger.increase_hunger(player, 6)
  176. -- Refill stamina.
  177. sprint.set_stamina(player, SPRINT_STAMINA)
  178. -- Notify portal sickness mod.
  179. --minetest.chat_send_player("MustTest", "# Server: <" .. rename.gpn(pname) .. ">!")
  180. portal_sickness.on_use_bed(pname)
  181. end
  182. end)
  183. end
  184. end
  185. function beds.report_respawn_status(name)
  186. --minetest.chat_send_player("MustTest", "# Server: Respawn report!")
  187. local good = false
  188. local pos = beds.spawn[name]
  189. if pos then
  190. local spawncount = beds.storage:get_int(name .. ":count")
  191. if spawncount > 0 then
  192. minetest.chat_send_player(name,
  193. "# Server: Your home position currently set in the " .. rc.realm_description_at_pos(pos) .. " @ " ..
  194. rc.pos_to_string(pos) .. " has " .. spawncount .. " respawn(s) left.")
  195. good = true
  196. end
  197. end
  198. if not good then
  199. minetest.chat_send_player(name, "# Server: You currently have no home/respawn position set.")
  200. end
  201. end
  202. local function node_blocks_bed(nn)
  203. if nn == "air" then return false end
  204. if string.find(nn, "ladder") or
  205. string.find(nn, "torch") or
  206. string.find(nn, "memorandum") then
  207. return false
  208. end
  209. local def = minetest.reg_ns_nodes[nn]
  210. if def then
  211. local dt = def.drawtype
  212. local pt2 = def.paramtype2
  213. if dt == "airlike" or
  214. dt == "signlike" or
  215. dt == "torchlike" or
  216. dt == "raillike" or
  217. dt == "plantlike" or
  218. (dt == "nodebox" and pt2 == "wallmounted") then
  219. return false
  220. end
  221. end
  222. -- All stairs nodes block bed respawning.
  223. return true
  224. end
  225. function beds.is_valid_bed_spawn(pos)
  226. local n1 = minetest.get_node(vector.add(pos, {x=0, y=1, z=0}))
  227. local n2 = minetest.get_node(vector.add(pos, {x=0, y=2, z=0}))
  228. if node_blocks_bed(n1.name) or node_blocks_bed(n2.name) then
  229. return false
  230. end
  231. return true
  232. end
  233. function beds.on_rightclick(pos, player)
  234. pos = vector.round(pos)
  235. local name = player:get_player_name()
  236. local meta = minetest.get_meta(pos)
  237. local owner = meta:get_string("owner") or ""
  238. -- Not while attached to something else!
  239. if default.player_attached[name] then
  240. return
  241. end
  242. if player:get_hp() == 0 then
  243. return
  244. end
  245. if owner == "" then
  246. -- If bed has no owner, and pos is not protected, player takes ownership.
  247. -- Note: this is to prevent player from taking ownership of an unowned bed
  248. -- in an area protected by someone else.
  249. if minetest.test_protection(pos, name) then
  250. minetest.chat_send_player(name, "# Server: You cannot take ownership of this bed due to protection.")
  251. return
  252. else
  253. local dname = rename.gpn(name)
  254. meta:set_string("owner", name)
  255. meta:set_string("rename", dname)
  256. meta:mark_as_private({"owner", "rename"})
  257. meta:set_string("infotext", "Bed (Owned by <" .. dname .. ">!)")
  258. end
  259. elseif owner == "server" then
  260. -- No code needed here so far.
  261. -- If owner is server, then bed is public and player may sleep here.
  262. -- But respawn position must not be set here.
  263. elseif owner ~= name then
  264. minetest.chat_send_player(name, "# Server: You cannot sleep here, this bed is not yours!")
  265. return
  266. end
  267. -- Otherwise, if bed is public OR the bed is owned by the player, then they
  268. -- are allowed to sleep, even if the bed is protected by someone else (and the
  269. -- protector wasn't shared, e.g., basic protection).
  270. if beds.monsters_nearby(pos, player) then
  271. minetest.chat_send_player(name, "# Server: You cannot sleep now, there are monsters nearby!")
  272. beds.report_respawn_status(name)
  273. return
  274. end
  275. if not beds.is_valid_bed_spawn(pos) then
  276. minetest.chat_send_player(name, "# Server: You cannot use this bed, there is not enough space above it to respawn!")
  277. beds.report_respawn_status(name)
  278. return
  279. end
  280. local ppos = player:getpos()
  281. local tod = minetest.get_timeofday()
  282. -- Player can sleep in bed anytime in the nether.
  283. if ppos.y > -25000 then
  284. if tod > 0.2 and tod < 0.805 then
  285. if beds.player[name] then
  286. lay_down(player, nil, nil, false)
  287. end
  288. minetest.chat_send_player(name, "# Server: You can only sleep at night.")
  289. beds.report_respawn_status(name)
  290. return
  291. end
  292. end
  293. -- move to bed
  294. if not beds.player[name] then
  295. lay_down(player, ppos, pos)
  296. -- If the bed is public, then player doesn't sethome here, and respawn count is not changed.
  297. if owner ~= "server" then
  298. beds.set_spawn(vector.round(pos), name)
  299. -- Sleeping in a bed refreshes the respawn count for this player.
  300. -- The player will respawn at this bed as long as their count is
  301. -- greater than 0.
  302. local spawncount = 8
  303. beds.storage:set_int(name .. ":count", spawncount)
  304. minetest.chat_send_player(name, "# Server: You will respawn in your bed at " .. rc.pos_to_namestr(pos) .. " up to " .. spawncount .. " times.")
  305. minetest.chat_send_player(name, "# Server: Afterward you will need to sleep again to refresh your respawn position.")
  306. minetest.chat_send_player(name, "# Server: You may safely dig your previous bed, if you had one set.")
  307. if survivalist.game_in_progress(name) then
  308. minetest.chat_send_player(name, "# Server: If you die during the Survival Challenge you will respawn here instead of failing the Challenge.")
  309. end
  310. else
  311. minetest.chat_send_player(name, "# Server: This bed is public, you cannot set-home here.")
  312. end
  313. else
  314. lay_down(player, nil, nil, false)
  315. end
  316. update_formspecs(false)
  317. -- skip the night and let all players stand up
  318. if check_in_beds() then
  319. minetest.after(2, function()
  320. update_formspecs(is_night_skip_enabled())
  321. if is_night_skip_enabled() then
  322. beds.skip_night()
  323. beds.kick_players()
  324. end
  325. end)
  326. end
  327. end
  328. function beds.has_respawn_bed(pname)
  329. if beds.spawn[pname] then
  330. return true
  331. end
  332. end
  333. function beds.clear_player_spawn(pname)
  334. beds.spawn[pname] = nil
  335. beds.save_spawns()
  336. end
  337. -- respawn player at bed if enabled and valid position is found
  338. function beds.on_respawnplayer(player)
  339. local name = player:get_player_name()
  340. local pos = beds.spawn[name]
  341. if pos then
  342. -- Don't preload area, that could allow a cheat.
  343. -- Update player's position immediately, without delay.
  344. wield3d.on_teleport()
  345. player:set_pos(pos)
  346. -- If player dies in a realm and their bed is in another, then they may
  347. -- change realms that way.
  348. rc.notify_realm_update(player, pos)
  349. local spawncount = beds.storage:get_int(name .. ":count")
  350. if spawncount <= 1 then
  351. beds.spawn[name] = nil
  352. beds.save_spawns()
  353. chat_core.alert_player_sound(name)
  354. local RED = core.get_color_escape_sequence("#ff0000")
  355. minetest.chat_send_player(name, RED .. "# Server: Warning! Your respawn position is lost!")
  356. else
  357. spawncount = spawncount - 1
  358. beds.storage:set_int(name .. ":count", spawncount)
  359. if spawncount > 1 then
  360. minetest.chat_send_player(name, "# Server: " .. spawncount .. " respawns left for that bed.")
  361. else
  362. chat_core.alert_player_sound(name)
  363. local RED = core.get_color_escape_sequence("#ff0000")
  364. minetest.chat_send_player(name, RED .. "# Server: Alert! Only 1 respawn left for that bed!")
  365. end
  366. end
  367. ambiance.sound_play("respawn", pos, 1.0, 10)
  368. else
  369. -- If the death position is not known, assume they died in the Abyss.
  370. local death_pos = rc.static_spawn("abyss")
  371. if bones.last_known_death_locations[name] then
  372. death_pos = bones.last_known_death_locations[name]
  373. end
  374. -- Tests show that `on_respawnplayer` is only called for existing players
  375. -- that die and respawn, NOT for newly-joined players!
  376. --minetest.chat_send_all("death at " .. minetest.pos_to_string(death_pos))
  377. --minetest.after(1, function() minetest.chat_send_all("on_respawnplayer was called!") end)
  378. -- Shall place player in the Outback, ALWAYS.
  379. randspawn.reposition_player(name, death_pos)
  380. -- If player died in a realm other than the abyss, then give them initial
  381. -- stuff upon respawning there.
  382. if rc.current_realm_at_pos(death_pos) ~= "abyss" then
  383. give_initial_stuff.give(player)
  384. end
  385. end
  386. return true -- Disable regular player placement.
  387. end
  388. function beds.on_joinplayer(player)
  389. local name = player:get_player_name()
  390. beds.player[name] = nil
  391. if check_in_beds() then
  392. update_formspecs(is_night_skip_enabled())
  393. if is_night_skip_enabled() then
  394. beds.skip_night()
  395. beds.kick_players()
  396. end
  397. else
  398. update_formspecs(false)
  399. end
  400. end
  401. function beds.on_leaveplayer(player)
  402. -- Bugfix: if player leaves game while dead, and in bed,
  403. -- resurrect them. Maybe this avoids issues with ppl logging in dead
  404. -- and unable to do anything?
  405. local name = player:get_player_name()
  406. -- Note: although a player who knows about this code could theoretically
  407. -- use it to cheat, the cheat is not game-breaking because they would respawn
  408. -- in their bed anyway.
  409. if beds.player[name] then
  410. if player:get_hp() == 0 then
  411. player:set_hp(1)
  412. end
  413. end
  414. lay_down(player, nil, nil, false, true)
  415. beds.player[name] = nil
  416. -- Wrapping this in minetest.after() is necessary.
  417. minetest.after(0, function()
  418. if check_in_beds() then
  419. update_formspecs(is_night_skip_enabled())
  420. if is_night_skip_enabled() then
  421. beds.skip_night()
  422. beds.kick_players()
  423. end
  424. else
  425. update_formspecs(false)
  426. end
  427. end)
  428. end
  429. function beds.on_player_receive_fields(player, formname, fields)
  430. if formname ~= "beds:detatched_formspec" then
  431. return
  432. end
  433. -- Because "Force night skip" button is a button_exit, it will set fields.quit
  434. -- and lay_down call will change value of player_in_bed, so it must be taken
  435. -- earlier.
  436. local pib = count_players_in_bed()
  437. local ges = get_participating_players()
  438. local is_majority = ((#ges) / 2) < pib
  439. if (fields.quit or fields.leave) and not fields.force then
  440. lay_down(player, nil, nil, false)
  441. update_formspecs(false)
  442. portal_sickness.check_sick(player:get_player_name())
  443. end
  444. if fields.force then
  445. if is_majority and is_night_skip_enabled() then
  446. update_formspecs(true)
  447. beds.skip_night()
  448. beds.kick_players()
  449. else
  450. update_formspecs(false)
  451. end
  452. end
  453. end
  454. -- Detect nearby monsters.
  455. function beds.monsters_nearby(pos, player)
  456. -- `pos` is the position of the bed.
  457. -- `player` is the person trying to sleep in a bed.
  458. local ents = minetest.get_objects_inside_radius(pos, 10)
  459. for k, v in ipairs(ents) do
  460. if not v:is_player() then
  461. local tb = v:get_luaentity()
  462. if tb and tb.mob then
  463. if tb.type and tb.type == "monster" then
  464. -- Found monster in radius.
  465. return true
  466. end
  467. end
  468. end
  469. end
  470. end