Default/functions: Reduce lavacooling ABM/sound overload
[oweals/minetest_game.git] / mods / beds / spawns.lua
1 local world_path = minetest.get_worldpath()
2 local org_file = world_path .. "/beds_spawns"
3 local file = world_path .. "/beds_spawns"
4 local bkwd = false
5
6 -- check for PA's beds mod spawns
7 local cf = io.open(world_path .. "/beds_player_spawns", "r")
8 if cf ~= nil then
9         io.close(cf)
10         file = world_path .. "/beds_player_spawns"
11         bkwd = true
12 end
13
14 function beds.read_spawns()
15         local spawns = beds.spawn
16         local input = io.open(file, "r")
17         if input and not bkwd then
18                 repeat
19                         local x = input:read("*n")
20                         if x == nil then
21                                 break
22                         end
23                         local y = input:read("*n")
24                         local z = input:read("*n")
25                         local name = input:read("*l")
26                         spawns[name:sub(2)] = {x = x, y = y, z = z}
27                 until input:read(0) == nil
28                 io.close(input)
29         elseif input and bkwd then
30                 beds.spawn = minetest.deserialize(input:read("*all"))
31                 input:close()
32                 beds.save_spawns()
33                 os.rename(file, file .. ".backup")
34                 file = org_file
35         else
36                 spawns = {}
37         end
38 end
39
40 function beds.save_spawns()
41         if not beds.spawn then
42                 return
43         end
44         local output = io.open(org_file, "w")
45         for i, v in pairs(beds.spawn) do
46                 output:write(v.x.." "..v.y.." "..v.z.." "..i.."\n")
47         end
48         io.close(output)
49 end
50
51 function beds.set_spawns()
52         for name,_ in pairs(beds.player) do
53                 local player = minetest.get_player_by_name(name)
54                 local p = player:getpos()
55                 beds.spawn[name] = p
56         end
57         beds.save_spawns()
58 end