world border: replace diameter with radius

This commit is contained in:
Bixilon 2023-05-25 15:25:08 +02:00
parent 753aed8650
commit 29838bd5d3
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 33 additions and 30 deletions

View File

@ -19,7 +19,6 @@ import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
import de.bixilon.kutil.math.interpolation.DoubleInterpolation.interpolateLinear
import de.bixilon.kutil.time.TimeUtil
import de.bixilon.kutil.time.TimeUtil.millis
import de.bixilon.minosoft.data.world.World
import de.bixilon.minosoft.data.world.positions.BlockPosition
@ -28,7 +27,7 @@ import kotlin.math.abs
class WorldBorder {
var center = Vec2d.EMPTY
var diameter = DEFAULT_DIAMETER
var radius = DEFAULT_RADIUS
var warningTime = 0
var warningBlocks = 0
var portalBound = 0
@ -36,10 +35,14 @@ class WorldBorder {
var state = WorldBorderState.STATIC
private set
private var interpolationStart = -1L
private var interpolationEnd = -1L
private var oldDiameter = DEFAULT_DIAMETER
private var newDiameter = DEFAULT_DIAMETER
var interpolationStart = -1L
private set
var interpolationEnd = -1L
private set
var oldRadius = DEFAULT_RADIUS
private set
var newRadius = DEFAULT_RADIUS
private set
val lock = SimpleLock()
@ -57,7 +60,7 @@ class WorldBorder {
fun isOutside(x: Double, z: Double): Boolean {
lock.acquire()
val radius = diameter / 2
val radius = radius
val inside = x in (center.x - radius)..(radius + center.x) && z in (center.y - radius)..(radius + center.y)
lock.release()
return !inside
@ -74,7 +77,7 @@ class WorldBorder {
fun getDistanceTo(x: Double, z: Double): Double {
lock.acquire()
val radius = diameter / 2
val radius = radius
val closestDistance = minOf(
radius - abs(x) - abs(center.x),
@ -90,17 +93,17 @@ class WorldBorder {
lock.unlock()
}
fun interpolate(oldDiameter: Double, newDiameter: Double, millis: Long) {
fun interpolate(oldRadius: Double, newRadius: Double, millis: Long) {
if (millis == 0L) {
stopInterpolating()
diameter = newDiameter
radius = newRadius
}
lock.lock()
val time = millis()
interpolationStart = time
interpolationEnd = time + millis
this.oldDiameter = oldDiameter
this.newDiameter = newDiameter
this.oldRadius = oldRadius
this.newRadius = newRadius
lock.unlock()
}
@ -117,16 +120,16 @@ class WorldBorder {
lock.unlock()
return
}
val oldDiameter = diameter
val oldRadius = radius
val remaining = interpolationEnd - time
val totalTime = (interpolationEnd - interpolationStart)
val diameter = interpolateLinear(remaining.toDouble() / totalTime.toDouble(), this.newDiameter, this.oldDiameter)
this.diameter = diameter
val radius = interpolateLinear(remaining.toDouble() / totalTime.toDouble(), this.newRadius, this.oldRadius)
this.radius = radius
state = if (oldDiameter > diameter) {
state = if (oldRadius > radius) {
WorldBorderState.SHRINKING
} else if (oldDiameter < diameter) {
} else if (oldRadius < radius) {
WorldBorderState.GROWING
} else {
interpolationStart = -1L
@ -137,12 +140,12 @@ class WorldBorder {
fun reset() {
lock.lock()
diameter = DEFAULT_DIAMETER
radius = DEFAULT_RADIUS
interpolationStart = -1L
lock.unlock()
}
companion object {
const val DEFAULT_DIAMETER = World.MAX_SIZE.toDouble() * 2
const val DEFAULT_RADIUS = World.MAX_SIZE.toDouble()
}
}

View File

@ -23,8 +23,8 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket(parent = true)
class InitializeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
val center = buffer.readVec2d()
val oldDiameter = buffer.readDouble()
val newDiameter = buffer.readDouble()
val oldRadius = buffer.readDouble() / 2.0
val newRadius = buffer.readDouble() / 2.0
val millis = buffer.readVarLong()
val portalBound = buffer.readVarInt()
val warningTime = buffer.readVarInt()
@ -32,13 +32,13 @@ class InitializeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
override fun handle(connection: PlayConnection) {
connection.world.border.center = center
connection.world.border.interpolate(oldDiameter, newDiameter, millis)
connection.world.border.interpolate(oldRadius, newRadius, millis)
connection.world.border.portalBound = portalBound
connection.world.border.warningTime = warningTime
connection.world.border.warningBlocks = warningBlocks
}
override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Initialize world border (center=$center, oldDiameter=$oldDiameter, newDiameter=$newDiameter, speed=$millis, portalBound=$portalBound, warningTime=$warningTime, warningBlocks=$warningBlocks)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Initialize world border (center=$center, oldRadius=$oldRadius, newRadius=$newRadius, speed=$millis, portalBound=$portalBound, warningTime=$warningTime, warningBlocks=$warningBlocks)" }
}
}

View File

@ -22,15 +22,15 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket(parent = true)
class InterpolateWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
val oldDiameter = buffer.readDouble()
val newDiameter = buffer.readDouble()
val oldRadius = buffer.readDouble() / 2.0
val newRadius = buffer.readDouble() / 2.0
val millis = buffer.readVarLong()
override fun handle(connection: PlayConnection) {
connection.world.border.interpolate(oldDiameter, newDiameter, millis)
connection.world.border.interpolate(oldRadius, newRadius, millis)
}
override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Interpolate size world border (oldDiameter=$oldDiameter, newDiameter=$newDiameter, millis=$millis)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Interpolate size world border (oldRadius=$oldRadius, newRadius=$newRadius, millis=$millis)" }
}
}

View File

@ -22,14 +22,14 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket(parent = true)
class SizeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
val diameter = buffer.readDouble()
val radius = buffer.readDouble() / 2.0
override fun handle(connection: PlayConnection) {
connection.world.border.stopInterpolating()
connection.world.border.diameter = diameter
connection.world.border.radius = radius
}
override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Size set world border (diameter=$diameter)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Size set world border (diameter=$radius)" }
}
}