summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/chunk.lua36
-rw-r--r--server/map.lua34
-rw-r--r--server/server.lua28
-rw-r--r--server/worldgen.lua3
4 files changed, 56 insertions, 45 deletions
diff --git a/server/chunk.lua b/server/chunk.lua
new file mode 100644
index 0000000..4fb2960
--- /dev/null
+++ b/server/chunk.lua
@@ -0,0 +1,36 @@
+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 = true
+ 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)
+ 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()
+ end
+end
+function ChunkS.set_at(self,...)
+ Chunk.set_at(self,...)
+ self.dirty = true
+end
+
+return {ChunkS=ChunkS}
diff --git a/server/map.lua b/server/map.lua
index 0bb09c7..9627de9 100644
--- a/server/map.lua
+++ b/server/map.lua
@@ -2,7 +2,7 @@ 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 ChunkS = require"chunk".ChunkS
local MapS = class.extend(Map)
function MapS.obtain(self,cp)
@@ -12,36 +12,28 @@ function MapS.obtain(self,cp)
-- 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()
+ if ch then
+ return ch
else
- ch = worldgen.gen_chunk(cp)
- end
+ ch = ChunkS:load_from_disk(cp)
+ if not ch then
+ ch = worldgen.gen_chunk(cp)
+ end
-
- print(ch.cp.u,ch.cp.v)
- self:add_chunk(ch)
- return ch
+ self:add_chunk(ch)
+ return ch
+ end
+
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()
+ ch:save_if_dirty()
end
function MapS.save_and_unload(self,cp)
self:save_chunk(cp)
diff --git a/server/server.lua b/server/server.lua
index e9eef30..cc9903e 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -1,7 +1,6 @@
local enet = require"enet"
local json = require"common.dkjson"
local chunk = require"common.chunk"
-local Chunk = require"common.chunk".Chunk
local noise = require"noise"
local coords = require"common.coords"
local worldgen = require"worldgen"
@@ -170,30 +169,13 @@ end
while true do
local now = timenow()
local dt = now - last_tick_time
-
- if dt >= tick_interval then
- dts[ndt] = dt
- ndt = 1+(ndt%ndts)
+ local time_till_next_tick = (last_tick_time+tick_interval)-now
+ if time_till_next_tick > 0.001 then
+ local ev = host:service(time_till_next_tick*1000)
+ if ev then handle_ev(ev) end
+ else
last_tick_time = now
ntick = ntick + 1
-
tick(ntick)
-
- local tps = 0
- for _,d in ipairs(dts) do tps = tps + 1/d end
- tps = tps/#dts
-
-
- print(ndt,ndts)
- if 1 == ndt%ndts then
- print("tps",tps)
- end
- end
-
- local now2 = timenow()
- local time_till_next_tick = (last_tick_time+tick_interval)-now2
- if time_till_next_tick > 0 then
- local ev = host:service(time_till_next_tick/1000)
- if ev then handle_ev(ev) end
end
end
diff --git a/server/worldgen.lua b/server/worldgen.lua
index 5d496a0..2ef1ac0 100644
--- a/server/worldgen.lua
+++ b/server/worldgen.lua
@@ -1,6 +1,7 @@
local coords = require"common.coords"
local noise = require"noise"
local chunk = require"common.chunk"
+local ChunkS = require"chunk".ChunkS
local CHUNK_SIZE = require"common.constants".CHUNK_SIZE
local function p(amp,scale) return {scale=scale,amp=amp,gen=noise.PerlinNoise:make()} end
@@ -39,7 +40,7 @@ local function gen_chunk(chpos)
end
end
- local the_chunk = chunk.Chunk:make(chpos,tiles)
+ local the_chunk = ChunkS:make(chpos,tiles)
return the_chunk
end