mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 18:34:56 -04:00
world border: replace diameter with radius
This commit is contained in:
parent
753aed8650
commit
29838bd5d3
@ -19,7 +19,6 @@ import de.bixilon.kotlinglm.vec3.Vec3d
|
|||||||
import de.bixilon.kotlinglm.vec3.Vec3i
|
import de.bixilon.kotlinglm.vec3.Vec3i
|
||||||
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
|
import de.bixilon.kutil.concurrent.lock.simple.SimpleLock
|
||||||
import de.bixilon.kutil.math.interpolation.DoubleInterpolation.interpolateLinear
|
import de.bixilon.kutil.math.interpolation.DoubleInterpolation.interpolateLinear
|
||||||
import de.bixilon.kutil.time.TimeUtil
|
|
||||||
import de.bixilon.kutil.time.TimeUtil.millis
|
import de.bixilon.kutil.time.TimeUtil.millis
|
||||||
import de.bixilon.minosoft.data.world.World
|
import de.bixilon.minosoft.data.world.World
|
||||||
import de.bixilon.minosoft.data.world.positions.BlockPosition
|
import de.bixilon.minosoft.data.world.positions.BlockPosition
|
||||||
@ -28,7 +27,7 @@ import kotlin.math.abs
|
|||||||
|
|
||||||
class WorldBorder {
|
class WorldBorder {
|
||||||
var center = Vec2d.EMPTY
|
var center = Vec2d.EMPTY
|
||||||
var diameter = DEFAULT_DIAMETER
|
var radius = DEFAULT_RADIUS
|
||||||
var warningTime = 0
|
var warningTime = 0
|
||||||
var warningBlocks = 0
|
var warningBlocks = 0
|
||||||
var portalBound = 0
|
var portalBound = 0
|
||||||
@ -36,10 +35,14 @@ class WorldBorder {
|
|||||||
var state = WorldBorderState.STATIC
|
var state = WorldBorderState.STATIC
|
||||||
private set
|
private set
|
||||||
|
|
||||||
private var interpolationStart = -1L
|
var interpolationStart = -1L
|
||||||
private var interpolationEnd = -1L
|
private set
|
||||||
private var oldDiameter = DEFAULT_DIAMETER
|
var interpolationEnd = -1L
|
||||||
private var newDiameter = DEFAULT_DIAMETER
|
private set
|
||||||
|
var oldRadius = DEFAULT_RADIUS
|
||||||
|
private set
|
||||||
|
var newRadius = DEFAULT_RADIUS
|
||||||
|
private set
|
||||||
|
|
||||||
val lock = SimpleLock()
|
val lock = SimpleLock()
|
||||||
|
|
||||||
@ -57,7 +60,7 @@ class WorldBorder {
|
|||||||
|
|
||||||
fun isOutside(x: Double, z: Double): Boolean {
|
fun isOutside(x: Double, z: Double): Boolean {
|
||||||
lock.acquire()
|
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)
|
val inside = x in (center.x - radius)..(radius + center.x) && z in (center.y - radius)..(radius + center.y)
|
||||||
lock.release()
|
lock.release()
|
||||||
return !inside
|
return !inside
|
||||||
@ -74,7 +77,7 @@ class WorldBorder {
|
|||||||
|
|
||||||
fun getDistanceTo(x: Double, z: Double): Double {
|
fun getDistanceTo(x: Double, z: Double): Double {
|
||||||
lock.acquire()
|
lock.acquire()
|
||||||
val radius = diameter / 2
|
val radius = radius
|
||||||
|
|
||||||
val closestDistance = minOf(
|
val closestDistance = minOf(
|
||||||
radius - abs(x) - abs(center.x),
|
radius - abs(x) - abs(center.x),
|
||||||
@ -90,17 +93,17 @@ class WorldBorder {
|
|||||||
lock.unlock()
|
lock.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun interpolate(oldDiameter: Double, newDiameter: Double, millis: Long) {
|
fun interpolate(oldRadius: Double, newRadius: Double, millis: Long) {
|
||||||
if (millis == 0L) {
|
if (millis == 0L) {
|
||||||
stopInterpolating()
|
stopInterpolating()
|
||||||
diameter = newDiameter
|
radius = newRadius
|
||||||
}
|
}
|
||||||
lock.lock()
|
lock.lock()
|
||||||
val time = millis()
|
val time = millis()
|
||||||
interpolationStart = time
|
interpolationStart = time
|
||||||
interpolationEnd = time + millis
|
interpolationEnd = time + millis
|
||||||
this.oldDiameter = oldDiameter
|
this.oldRadius = oldRadius
|
||||||
this.newDiameter = newDiameter
|
this.newRadius = newRadius
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,16 +120,16 @@ class WorldBorder {
|
|||||||
lock.unlock()
|
lock.unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val oldDiameter = diameter
|
val oldRadius = radius
|
||||||
|
|
||||||
val remaining = interpolationEnd - time
|
val remaining = interpolationEnd - time
|
||||||
val totalTime = (interpolationEnd - interpolationStart)
|
val totalTime = (interpolationEnd - interpolationStart)
|
||||||
val diameter = interpolateLinear(remaining.toDouble() / totalTime.toDouble(), this.newDiameter, this.oldDiameter)
|
val radius = interpolateLinear(remaining.toDouble() / totalTime.toDouble(), this.newRadius, this.oldRadius)
|
||||||
this.diameter = diameter
|
this.radius = radius
|
||||||
|
|
||||||
state = if (oldDiameter > diameter) {
|
state = if (oldRadius > radius) {
|
||||||
WorldBorderState.SHRINKING
|
WorldBorderState.SHRINKING
|
||||||
} else if (oldDiameter < diameter) {
|
} else if (oldRadius < radius) {
|
||||||
WorldBorderState.GROWING
|
WorldBorderState.GROWING
|
||||||
} else {
|
} else {
|
||||||
interpolationStart = -1L
|
interpolationStart = -1L
|
||||||
@ -137,12 +140,12 @@ class WorldBorder {
|
|||||||
|
|
||||||
fun reset() {
|
fun reset() {
|
||||||
lock.lock()
|
lock.lock()
|
||||||
diameter = DEFAULT_DIAMETER
|
radius = DEFAULT_RADIUS
|
||||||
interpolationStart = -1L
|
interpolationStart = -1L
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val DEFAULT_DIAMETER = World.MAX_SIZE.toDouble() * 2
|
const val DEFAULT_RADIUS = World.MAX_SIZE.toDouble()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,8 @@ import de.bixilon.minosoft.util.logging.LogMessageType
|
|||||||
@LoadPacket(parent = true)
|
@LoadPacket(parent = true)
|
||||||
class InitializeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
class InitializeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
||||||
val center = buffer.readVec2d()
|
val center = buffer.readVec2d()
|
||||||
val oldDiameter = buffer.readDouble()
|
val oldRadius = buffer.readDouble() / 2.0
|
||||||
val newDiameter = buffer.readDouble()
|
val newRadius = buffer.readDouble() / 2.0
|
||||||
val millis = buffer.readVarLong()
|
val millis = buffer.readVarLong()
|
||||||
val portalBound = buffer.readVarInt()
|
val portalBound = buffer.readVarInt()
|
||||||
val warningTime = buffer.readVarInt()
|
val warningTime = buffer.readVarInt()
|
||||||
@ -32,13 +32,13 @@ class InitializeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
|||||||
|
|
||||||
override fun handle(connection: PlayConnection) {
|
override fun handle(connection: PlayConnection) {
|
||||||
connection.world.border.center = center
|
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.portalBound = portalBound
|
||||||
connection.world.border.warningTime = warningTime
|
connection.world.border.warningTime = warningTime
|
||||||
connection.world.border.warningBlocks = warningBlocks
|
connection.world.border.warningBlocks = warningBlocks
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun log(reducedLog: Boolean) {
|
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)" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,15 +22,15 @@ import de.bixilon.minosoft.util.logging.LogMessageType
|
|||||||
|
|
||||||
@LoadPacket(parent = true)
|
@LoadPacket(parent = true)
|
||||||
class InterpolateWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
class InterpolateWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
||||||
val oldDiameter = buffer.readDouble()
|
val oldRadius = buffer.readDouble() / 2.0
|
||||||
val newDiameter = buffer.readDouble()
|
val newRadius = buffer.readDouble() / 2.0
|
||||||
val millis = buffer.readVarLong()
|
val millis = buffer.readVarLong()
|
||||||
|
|
||||||
override fun handle(connection: PlayConnection) {
|
override fun handle(connection: PlayConnection) {
|
||||||
connection.world.border.interpolate(oldDiameter, newDiameter, millis)
|
connection.world.border.interpolate(oldRadius, newRadius, millis)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun log(reducedLog: Boolean) {
|
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)" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,14 @@ import de.bixilon.minosoft.util.logging.LogMessageType
|
|||||||
|
|
||||||
@LoadPacket(parent = true)
|
@LoadPacket(parent = true)
|
||||||
class SizeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
class SizeWorldBorderS2CP(buffer: PlayInByteBuffer) : WorldBorderS2CP {
|
||||||
val diameter = buffer.readDouble()
|
val radius = buffer.readDouble() / 2.0
|
||||||
|
|
||||||
override fun handle(connection: PlayConnection) {
|
override fun handle(connection: PlayConnection) {
|
||||||
connection.world.border.stopInterpolating()
|
connection.world.border.stopInterpolating()
|
||||||
connection.world.border.diameter = diameter
|
connection.world.border.radius = radius
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun log(reducedLog: Boolean) {
|
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)" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user