functions.lua 16 KB

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