Unload chunks that fall outside a certain radius

This commit is contained in:
Joscha 2017-04-08 17:05:09 +00:00
parent c364faf8ff
commit c5af7c2480
2 changed files with 14 additions and 4 deletions

View file

@ -170,14 +170,14 @@ class ChunkPool():
for pos in coords: for pos in coords:
self.unload(pos) self.unload(pos)
def clean_up(self, except_for=[], condition=lambda chunk: True): def clean_up(self, except_for=[], condition=lambda pos, chunk: True):
# old list comprehension which became too long: # old list comprehension which became too long:
#coords = [pos for pos, chunk in self._chunks.items() if not pos in except_for and condition(chunk)] #coords = [pos for pos, chunk in self._chunks.items() if not pos in except_for and condition(chunk)]
coords = [] coords = []
for pos, chunk in self._chunks.items(): for pos, chunk in self._chunks.items():
if not pos in except_for and condition(chunk): if not pos in except_for and condition(pos, chunk):
coords.append(pos) coords.append(pos)
self.unload_list(coords) self.unload_list(coords)

14
maps.py
View file

@ -15,7 +15,8 @@ class Map():
def __init__(self, width, height, chunkpool, drawevent): def __init__(self, width, height, chunkpool, drawevent):
self._lock = threading.RLock() self._lock = threading.RLock()
self.chunkpreload = 0 self.chunkpreload = 0 # preload chunks in this radius (they will count as "visible")
self.chunkunload = 5 # don't unload chunks within this radius
self.cursorpadding = 2 self.cursorpadding = 2
self.worldx = 0 self.worldx = 0
self.worldy = 0 self.worldy = 0
@ -77,11 +78,20 @@ class Map():
s = "."*CHUNK_WIDTH s = "."*CHUNK_WIDTH
self._pad.addstr(y+dy, x, s) self._pad.addstr(y+dy, x, s)
def _unload_condition(self, pos, chunk):
xstart = chunkx(self.worldx) - self.chunkunload
ystart = chunky(self.worldy) - self.chunkunload
xend = xstart + chunkx(self.width)+2 + 2*self.chunkunload
yend = ystart + chunky(self.height)+2 + 2*self.chunkunload
in_range = pos.x >= xstart and pos.x < xend and pos.y >= ystart and pos.y < yend
return not in_range
def load_visible(self): def load_visible(self):
with self.chunkpool as pool: with self.chunkpool as pool:
coords = self.visible_chunk_coords() coords = self.visible_chunk_coords()
pool.load_list(coords) pool.load_list(coords)
#pool.clean_up(except_for=coords) pool.clean_up(except_for=coords, condition=self._unload_condition)
self.drawevent.set() self.drawevent.set()