diff --git a/src/chunk.zig b/src/chunk.zig index a264ea06..b9bf6f03 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -93,6 +93,10 @@ pub const ChunkPosition = struct { dz = @maximum(0, dz - halfWidth); return dx*dx + dy*dy + dz*dz; } + + pub fn getPriority(self: ChunkPosition, playerPos: Vec3d) f32 { + return -@floatCast(f32, self.getMinDistanceSquared(playerPos))/@intToFloat(f32, self.voxelSize*self.voxelSize) + 2*@intToFloat(f32, std.math.log2_int(UChunkCoordinate, self.voxelSize)*chunkSize*chunkSize); + } }; pub const Chunk = struct { diff --git a/src/network.zig b/src/network.zig index b7ae9f06..d54332cc 100644 --- a/src/network.zig +++ b/src/network.zig @@ -341,6 +341,7 @@ pub const ConnectionManager = struct { errdefer Socket.deinit(result.socket); result.thread = try std.Thread.spawn(.{}, run, .{result}); + try result.thread.setName("Network Thread"); if(online) { result.makeOnline(); } @@ -743,27 +744,6 @@ pub const Protocols = blk: { // Bits.putInt(data, 8, ch.wz); // Bits.putInt(data, 12, ch.voxelSize); // conn.sendImportant(this, data); -// } -// -// private static class ChunkLoadTask extends ThreadPool.Task { -// private final VisibleChunk ch; -// public ChunkLoadTask(VisibleChunk ch) { -// this.ch = ch; -// } -// @Override -// public float getPriority() { -// return ch.getPriority(Cubyz.player); -// } -// -// @Override -// public boolean isStillNeeded() { -// return Cubyz.chunkTree.findNode(ch) != null; -// } -// -// @Override -// public void run() { -// ch.load(); -// } // } }), playerPosition: type = addProtocol(&comptimeList, struct { diff --git a/src/renderer.zig b/src/renderer.zig index 8ce6a450..4a682b9d 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -658,7 +658,17 @@ pub const RenderStructure = struct { mutex.lock(); defer mutex.unlock(); while(updatableList.items.len != 0) { - const pos = updatableList.orderedRemove(0); + // TODO: Find a faster solution than going through the entire list. + var closestPriority: f32 = -std.math.floatMax(f32); + var closestIndex: usize = 0; + for(updatableList.items) |pos, i| { + const priority = pos.getPriority(game.playerPos); + if(priority > closestPriority) { + closestPriority = priority; + closestIndex = i; + } + } + const pos = updatableList.orderedRemove(closestIndex); mutex.unlock(); defer mutex.lock(); const nullNode = _getNode(pos); @@ -698,7 +708,7 @@ pub const RenderStructure = struct { } pub fn getPriority(self: *MeshGenerationTask) f32 { - return -@floatCast(f32, self.mesh.pos.getMinDistanceSquared(game.playerPos))/@intToFloat(f32, self.mesh.pos.voxelSize*self.mesh.pos.voxelSize); + return self.mesh.pos.getPriority(game.playerPos); } pub fn isStillNeeded(self: *MeshGenerationTask) bool {