summaryrefslogtreecommitdiff
path: root/server/map.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-02-06 11:46:31 +0000
committerubq323 <ubq323@ubq323.website>2023-02-06 11:46:50 +0000
commitc74c2a3133ad4d1f03b83021e53fb9e8a67b3914 (patch)
tree22d415e85381c9a09c471afa4a0a6f1824f67b87 /server/map.lua
parentc2184634b6d3c3268c08c8d430506cfb60245fdf (diff)
tick timing in main server loop, refactor, start on saving/loading chunks
Diffstat (limited to 'server/map.lua')
-rw-r--r--server/map.lua44
1 files changed, 44 insertions, 0 deletions
diff --git a/server/map.lua b/server/map.lua
new file mode 100644
index 0000000..845c644
--- /dev/null
+++ b/server/map.lua
@@ -0,0 +1,44 @@
+local Map = require"common.map".Map
+local class = require"common.class"
+local worldgen = require"worldgen"
+
+local MapS = class.extend(Map)
+function MapS.obtain(self,cp)
+ -- obtain chunk at chunkpos cp by any means necessary
+ -- if already loaded, just return it
+ -- if available on disk, load that then return it
+ -- otherwise, generate a new chunk, load it, then return it.
+
+ -- false is not used on serverside. yet.
+
+ local ch = self:chunk(cp)
+ if ch then return ch end
+
+ local f = io.open(cp:filename(),"r")
+ if f then
+ local j = json.decode(f:read("a"))
+ ch = Chunk:from_packet_data(j)
+ f:close()
+ else
+ ch = worldgen.gen_chunk(cp)
+ end
+
+ self:add_chunk(ch)
+ return ch
+end
+function MapS.save_chunk(self,cp)
+ -- any attempt to save not-loaded chunks is silently ignored
+ local ch = self:chunk(cp)
+ if not ch then return end
+
+ local f = io.open(cp:filename(),"w")
+ f:write(ch:data_packet())
+ f:flush()
+ f:close()
+end
+function MapS.save_and_unload(self,cp)
+ self:save_chunk(cp)
+ self:remove_chunk(cp)
+end
+
+return {MapS=MapS}