diff --git a/src/server/terrain/SurfaceMap.zig b/src/server/terrain/SurfaceMap.zig index 5170b229..e2762268 100644 --- a/src/server/terrain/SurfaceMap.zig +++ b/src/server/terrain/SurfaceMap.zig @@ -7,6 +7,7 @@ const Chunk = main.chunk.Chunk; const ChunkPosition = main.chunk.ChunkPosition; const Cache = main.utils.Cache; const JsonElement = main.JsonElement; +const Vec3d = main.vec.Vec3d; const terrain = @import("terrain.zig"); const TerrainGenerationProfile = terrain.TerrainGenerationProfile; @@ -39,6 +40,19 @@ pub const MapFragmentPosition = struct { pub fn hashCode(self: MapFragmentPosition) u32 { return @bitCast((self.wx >> (MapFragment.mapShift + self.voxelSizeShift))*%33 +% (self.wz >> (MapFragment.mapShift + self.voxelSizeShift)) ^ self.voxelSize); } + + pub fn getMinDistanceSquared(self: MapFragmentPosition, playerPosition: Vec3d, comptime width: comptime_int) f64 { + const halfWidth: f64 = @floatFromInt(self.voxelSize*@divExact(width, 2)); + var dx = @abs(@as(f64, @floatFromInt(self.wx)) + halfWidth - playerPosition[0]); + var dz = @abs(@as(f64, @floatFromInt(self.wz)) + halfWidth - playerPosition[2]); + dx = @max(0, dx - halfWidth); + dz = @max(0, dz - halfWidth); + return dx*dx + dz*dz; + } + + pub fn getPriority(self: MapFragmentPosition, playerPos: Vec3d, comptime width: comptime_int) f32 { + return -@as(f32, @floatCast(self.getMinDistanceSquared(playerPos, width)))/@as(f32, @floatFromInt(self.voxelSize*self.voxelSize)) + 2*@as(f32, @floatFromInt(std.math.log2_int(u31, self.voxelSize)))*width*width; + } }; /// Generates and stores the height and Biome maps of the planet. diff --git a/src/server/world.zig b/src/server/world.zig index b59f02ff..79c04491 100644 --- a/src/server/world.zig +++ b/src/server/world.zig @@ -127,8 +127,7 @@ const ChunkManager = struct { pub fn getPriority(self: *LightMapLoadTask) f32 { if(self.source) |user| { - const pos = ChunkPosition{.wx = self.pos.wx, .wy = @intFromFloat(user.player.pos[1]), .wz = self.pos.wz, .voxelSize = self.pos.voxelSize}; - return pos.getPriority(user.player.pos) + 100000; + return self.pos.getPriority(user.player.pos, terrain.LightMap.LightMapFragment.mapSize) + 100; } else { return std.math.floatMax(f32); }