blob: 0bb09c70560606406221cff42eedb7cc2ff5bea3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
local Map = require"common.map".Map
local class = require"common.class"
local worldgen = require"worldgen"
local json = require"common.dkjson"
local Chunk = require"common.chunk".Chunk
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
print("loading from file",cp)
local j = json.decode(f:read("a"))
print(j)
for k in pairs(j) do print(k) end
ch = Chunk.from_packet_data(j)
f:close()
else
ch = worldgen.gen_chunk(cp)
end
print(ch.cp.u,ch.cp.v)
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}
|