summaryrefslogtreecommitdiff
path: root/server/map.lua
blob: 7b2e62b69c6aa40915c78bada112bf6095d8ec52 (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 Map = require"common.map".Map
local class = require"common.class"
local worldgen = require"worldgen"
local json = require"common.dkjson"
local ChunkS = require"chunk".ChunkS

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
	else
		ch = ChunkS:load_from_disk(cp)
		if not ch then
			ch = worldgen.gen_chunk(cp)
		end

		self:add_chunk(ch)
		return ch
	end
	
end

function MapS.save_chunk(self,cp,txn)
	-- will only actually save anything if anything needs saving.
	-- any attempt to save not-loaded chunks is silently ignored
	local ch = self:chunk(cp)
	if not ch then return end

	ch:save_if_dirty(txn)
end

return {MapS=MapS}