diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/server.lua | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/server/server.lua b/server/server.lua index 097bc99..24b49ae 100644 --- a/server/server.lua +++ b/server/server.lua @@ -7,6 +7,8 @@ local Pos = coords.Pos local worldgen = require"worldgen" local MapS = require"map".MapS local posix_time = require"posix.time" +local posix_signal = require"posix.signal" + math.randomseed(os.time()) local host = enet.host_create("*:8473",nil,2) @@ -181,20 +183,34 @@ local function player_near_chunk(cp) return false end +local function save_things() + for cp,ch in map:iter_chunks() do + map:save_chunk(cp) + if not player_near_chunk(cp) then + -- print("unloading chunk",cp) + map:remove_chunk(cp) + end + end +end + local function tick(ntick) if ntick % 30 == 0 then -- print("saving things") - for cp,ch in map:iter_chunks() do - map:save_chunk(cp) - if not player_near_chunk(cp) then - -- print("unloading chunk",cp) - map:remove_chunk(cp) - end - end + save_things() end end +local stopping=false + +posix_signal.signal(posix_signal.SIGINT, function() stopping=true end) + while true do + if stopping then + print("stopping...") + save_things() + break + end + local now = timenow() local dt = now - last_tick_time local time_till_next_tick = (last_tick_time+tick_interval)-now |