diff options
author | ubq323 <ubq323@ubq323.website> | 2023-02-04 23:28:08 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-02-04 23:28:08 +0000 |
commit | 9339e47c79f40fe9c4541e638d7fd67ab634c600 (patch) | |
tree | ad1efddc3608ca506534d3e7ab7b2ae4a35a0476 /common/map.lua | |
parent | 0dc1276df57aa16b4f0eaecf54fb5cd8f00115c6 (diff) |
improve tracking of loading chunks, and increase load dist around player
Diffstat (limited to 'common/map.lua')
-rw-r--r-- | common/map.lua | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/common/map.lua b/common/map.lua index 5bcd09e..d32cbf3 100644 --- a/common/map.lua +++ b/common/map.lua @@ -1,6 +1,11 @@ -- a Map is a 2d array of chunks -- it handles loading and unloading of chunks --- the specifics of which are then implemented separately for client and server +-- each slot in the Map is either a Chunk object (if that chunk is loaded), +-- nil (if not loaded), or false (if 'loading'). on the client 'loading' means +-- the request for the chunk is currently in flight. on the server it might one day +-- mean that terrain generation for that chunk is currently in progress. +-- to test whether a chunk is loaded you can do if map:chunk(cp) then ... end +-- to test whether a chunk needs to be loaded you do if map:chunk(cp) == nil then ... end. -- it will probably also do things relating to entities and multiblock things local class = require"common.class" @@ -11,13 +16,21 @@ local Map = class() function Map.make(cls) return setmetatable({chunks={}},cls) end -function Map.add_chunk(self,cp,the_chunk) - print("adding chunk",cp) - assert(the_chunk.cp == cp,"attempting to add chunk with the wrong cp") +function Map._set_chunk(self,cp,value) if not self.chunks[cp.u] then self.chunks[cp.u] = {} end - self.chunks[cp.u][cp.v] = the_chunk + self.chunks[cp.u][cp.v] = value +end +function Map.add_chunk(self,the_chunk) + local cp = the_chunk.cp + print("adding chunk",cp) + self:_set_chunk(cp,the_chunk) +end +function Map.mark_chunk_loading(self,cp) + print("marking chunk as loading",cp) + self:_set_chunk(cp,false) end function Map.unload_chunk(self,cp) + print("unloading chunk",cp) if not self.chunks[cp.u] then return end self.chunks[cp.u][cp.v] = nil -- remove list if empty @@ -42,7 +55,7 @@ function Map.at(self,hpos) local cpu,cpv = math.floor(hpos.q/CHUNK_SIZE),math.floor(hpos.r/CHUNK_SIZE) local hoffq,hoffr = hpos.q-(cpu*CHUNK_SIZE), hpos.r-(cpv*CHUNK_SIZE) local ch = self:_chunkuv(cpu,cpv) - if ch == nil then return nil end + if not ch then return nil end return ch:_atqr(hoffq,hoffr) end function Map.set_at(self,hpos,tile) @@ -50,7 +63,7 @@ function Map.set_at(self,hpos,tile) local ch = self:chunk(cp) -- setting a tile in an unloaded chunk is silently ignored -- this might change one day - if ch == nil then return nil end + if not ch then return nil end ch:set_at(hoffs,tile) end |