Handle loading / unloading chunks better

This commit is contained in:
2021-05-07 00:14:25 +00:00
parent 5184d4a173
commit e5fd062a4a
6 changed files with 48 additions and 38 deletions
+13 -21
View File
@@ -70,7 +70,6 @@ class ChunksManager:
for item_id, locations in chunk.sub_index.items():
if item_id not in self.index:
self.index[item_id] = []
for l in locations:
coords = (dx + l%16, dy + l//256, dz + l%256//16)
self.index[item_id].append(coords)
@@ -122,28 +121,21 @@ class ChunksManager:
self.index[block] = []
self.index[block].append((x, y, z))
def check_loaded(self, position, steps=1):
x, y, z = utils.pint(position)
player_chunk = (x//16, 1, z//16)
for i in range(steps): # TODO: base off render_distance?
offset = utils.spiral(i)
check = utils.padd(player_chunk, offset)
def check_loaded(self, chunk_distance):
num = (chunk_distance * 2 + 1) ** 2
num_subchunks = num * 16
return len(self.chunks) >= num_subchunks
if check not in self.chunks:
return False
return True
def unload_chunks(self, position):
x, y, z = utils.pint(position)
player_chunk = (x//16, 0, z//16)
loaded_chunks = list(self.chunks.keys())
for chunk in loaded_chunks:
check = (chunk[0], 0, chunk[2])
if utils.phyp_king(player_chunk, check) > 20:
del self.chunks[chunk]
def unload_chunk(self, x, z):
for y in range(16):
try:
del self.chunks[(x, y, z)]
except KeyError:
pass
def unload_all_chunks(self):
self.chunks = {}
self.index = {}
class ChunkNotLoadedException(Exception):
def __str__(self):