optimizations

This commit is contained in:
Kyler 2024-04-22 20:57:13 -06:00
parent 6a446e1b18
commit ab83443632
1 changed files with 9 additions and 8 deletions

View File

@ -1,4 +1,5 @@
# Yeahbut Apr 2024 # Yeahbut Apr 2024
import math
import certifi, os import certifi, os
os.environ["SSL_CERT_FILE"] = certifi.where() os.environ["SSL_CERT_FILE"] = certifi.where()
@ -183,6 +184,7 @@ class Player:
self.load_chunk(*i) self.load_chunk(*i)
for i in old_chunks: for i in old_chunks:
self.unload_chunk(*i) self.unload_chunk(*i)
self._load_queue.sort(key=lambda o: math.sqrt(pow(self.cx - o[0],2) + pow(self.cz - o[1],2)))
self._conn.send_update_view_position(self.cx,self.cz) self._conn.send_update_view_position(self.cx,self.cz)
def load_chunk(self, x: int, z: int): def load_chunk(self, x: int, z: int):
@ -212,7 +214,7 @@ class YTDServerProtocol(ServerProtocol):
# in-game, and does some logging. # in-game, and does some logging.
ServerProtocol.player_joined(self) ServerProtocol.player_joined(self)
self.buff_type = Buffer_1_18_2 self.buff_type = Buffer_1_18_2
self.view_distance = 2 self.view_distance = 5
self.player = Player( self.player = Player(
self, self,
@ -242,7 +244,7 @@ class YTDServerProtocol(ServerProtocol):
# Start sending "Keep Alive" packets # Start sending "Keep Alive" packets
self.ticker.add_loop(20, self.update_keep_alive) self.ticker.add_loop(20, self.update_keep_alive)
self.ticker.add_loop(1, self.manage_queue) self.ticker.add_loop(.5/self.ticker.interval, self.manage_queue)
# Announce player join to other players # Announce player join to other players
self.factory.broadcast_player_join(self) self.factory.broadcast_player_join(self)
@ -297,7 +299,7 @@ class YTDServerProtocol(ServerProtocol):
join_game.append(self.buff_type.pack_string(world_name)) join_game.append(self.buff_type.pack_string(world_name))
join_game.append(self.buff_type.pack("q", hashed_seed)) join_game.append(self.buff_type.pack("q", hashed_seed))
join_game.append(self.buff_type.pack_varint(max_players)) join_game.append(self.buff_type.pack_varint(max_players))
join_game.append(self.buff_type.pack_varint(view_distance)), join_game.append(self.buff_type.pack_varint(view_distance))
join_game.append(self.buff_type.pack_varint(simulation_distance)) join_game.append(self.buff_type.pack_varint(simulation_distance))
@ -307,22 +309,21 @@ class YTDServerProtocol(ServerProtocol):
self.send_packet("join_game", *join_game) self.send_packet("join_game", *join_game)
def manage_queue(self): def manage_queue(self):
if self.closed: return
t = time.time() t = time.time()
if self.player._load_queue: if self.player._load_queue:
x,z = self.player._load_queue.pop(0) x,z = self.player._load_queue.pop(0)
while (x,z) in self.player._load_queue: while (x,z) in self.player._load_queue:
self.player._load_queue.remove((x,z)) self.player._load_queue.remove((x,z))
print(f"dequeuing loading chunk {x}, {z}") # print(f"dequeuing loading chunk {x}, {z}")
self.send_chunk(x,z) self.send_chunk(x,z)
print(f"generating {x}, {z} took {int(time.time()-t)}s") print(f"generating {x}, {z} took {int(time.time()-t)}s; {len(self.player._load_queue)} chunks remaining")
elif self.player._unload_queue: while self.player._unload_queue:
x,z = self.player._unload_queue.pop(0) x,z = self.player._unload_queue.pop(0)
while (x,z) in self.player._unload_queue: while (x,z) in self.player._unload_queue:
self.player._unload_queue.remove((x,z)) self.player._unload_queue.remove((x,z))
print(f"dequeuing unloading chunk {x}, {z}") print(f"dequeuing unloading chunk {x}, {z}")
self.send_unload_chunk(x,z) self.send_unload_chunk(x,z)
# else:
# print("queues empty")
def player_left(self): def player_left(self):
ServerProtocol.player_left(self) ServerProtocol.player_left(self)