summaryrefslogtreecommitdiff
path: root/server/chunk.lua
blob: 8e71327250d318e0aad9305aa897e048f71b3d39 (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
local class = require"common.class"
local Chunk = require"common.chunk".Chunk
local json = require"common.dkjson"

local ChunkS = class.extend(Chunk)
function ChunkS.make(cls,...)
	local self = Chunk.make(cls,...)
	self.dirty = false
	return self
end
function ChunkS.load_from_disk(cls,cp)
	-- returns nil if not there
	local filename = cp:filename()
	local f = io.open(filename,"r")
	if not f then return nil end
	local j = json.decode(f:read("a"))
	self = cls:from_packet_data(j)
	for i,t in ipairs(self.tiles) do
		if t == false then self.tiles[i] = 0
		elseif t == true then self.tiles[i] = 9 end
	end
	f:close()
	return self
end
function ChunkS.save_if_dirty(self)
	if self.dirty then
		print("saving chunk",self.cp)
		local filename = self.cp:filename()
		local f = io.open(filename,"w")
		f:write(self:data_packet())
		f:flush()
		f:close()
		self.dirty = false
	end
end
function ChunkS.set_at(self,...)
	Chunk.set_at(self,...)
	self.dirty = true
end

return {ChunkS=ChunkS}