From c5af7c2480eeff15b9961d6d6904850280f542e1 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 8 Apr 2017 17:05:09 +0000 Subject: [PATCH] Unload chunks that fall outside a certain radius --- chunks.py | 4 ++-- maps.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/chunks.py b/chunks.py index 034ba24..6107c21 100644 --- a/chunks.py +++ b/chunks.py @@ -170,14 +170,14 @@ class ChunkPool(): for pos in coords: 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: #coords = [pos for pos, chunk in self._chunks.items() if not pos in except_for and condition(chunk)] coords = [] 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) self.unload_list(coords) diff --git a/maps.py b/maps.py index 2464399..043d243 100644 --- a/maps.py +++ b/maps.py @@ -15,7 +15,8 @@ class Map(): def __init__(self, width, height, chunkpool, drawevent): 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.worldx = 0 self.worldy = 0 @@ -77,11 +78,20 @@ class Map(): s = "."*CHUNK_WIDTH 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): with self.chunkpool as pool: coords = self.visible_chunk_coords() pool.load_list(coords) - #pool.clean_up(except_for=coords) + pool.clean_up(except_for=coords, condition=self._unload_condition) self.drawevent.set()