diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 0b676c169..edfc07485 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -177,6 +177,41 @@ game { variable = 30 } } + + # Limits the dispatch of PlayerStateMessage packets from a session to its client. + # Specifically, a packet will only be dispatched based on whether + # it is to be dispatched over a threshold time after the previous dispatch time. + # The delay between packets is determined by a distance between the observer player and the target player. + # Only affects PlayerStateMessage. + player-draw = { + # Minimum number of players within a given region before scaling down the range. + # Total population - population threshold = overpopulation + population-threshold = 20 + # Number of players over threshold before the reduction in range is scaled down. + # Population step / overpopulation = range step factor (integer) + population-step = 5 + # Always send packets regarding target players within the given distance. + range-min = 50 + # Do not send packets regarding target players beyond this distance. (sq.m) + # Treat this as an absolute render distance. + range-max = 550 + # Number of meters reduced from range based on population count over threshold value. (m) + # Range step * range step factor = total distance to remove from actual distance + range-step = 25 + # The distances above min-range where different delays are allowed before a successful packet must be dispatched. [m] + # Test distances against entries to find the last one that is not more than the sample distance. + # Use the index of that sample distance from this sequence in the sequence `delays` below. + ranges = [150, 300, 400] + # The absolute time delay before a successful packet must be dispatched regardless of distance. (s) + delay-max = 1000 + # The time delays for each distance range before a successful packet must be dispatched. [s] + # The index for an entry in this sequence is expected to be discovered using the `ranges` sequence above. + # Delays between packets may not be as precise as desired + # as the heartbeat of upstream packets are measured in quanta of 250ms usually. + # As a consequence, additional entries with proper time spacing will push back the next proper update considerably + # while additional entries with insufficient time spacing may result in no change in behavior. + delays = [350, 600, 800] + } } anti-cheat { diff --git a/src/main/scala/net/psforever/actors/session/support/SessionAvatarHandlers.scala b/src/main/scala/net/psforever/actors/session/support/SessionAvatarHandlers.scala index 5f6d03ce0..e70b69954 100644 --- a/src/main/scala/net/psforever/actors/session/support/SessionAvatarHandlers.scala +++ b/src/main/scala/net/psforever/actors/session/support/SessionAvatarHandlers.scala @@ -70,31 +70,74 @@ class SessionAvatarHandlers( jumpThrust, isCloaking, spectating, - _ + canSeeReallyFar ) if isNotSameTarget => - val now = System.currentTimeMillis() - val (location, time, distanceSq): (Vector3, Long, Float) = if (spectating) { - val r2 = 2 + hidingPlayerRandomizer.nextInt(4000).toFloat - (Vector3(r2, r2, 1f), 0L, 0f) - } else { - val before = lastSeenStreamMessage(guid.guid).time - val dist = Vector3.DistanceSquared(player.Position, pos) - (pos, now - before, dist) + val (lastMsg, lastTime, lastPosition, wasSpectating) = lastSeenStreamMessage(guid.guid) match { + case SessionAvatarHandlers.LastUpstream(Some(msg), time) => (Some(msg), time, msg.pos, msg.spectator) + case _ => (None, 0L, Vector3.Zero, false) } - if (distanceSq < 302500 || time > 5000) { // Render distance seems to be approx 525m. Reduce update rate at ~550m to be safe + val drawConfig = Config.app.game.playerDraw + val maxRange = drawConfig.rangeMax * drawConfig.rangeMax //sq.m + val ourPosition = player.Position + val currentDistance = Vector3.DistanceSquared(ourPosition, pos) //sq.m + val inVisibleRange = currentDistance <= maxRange + val wasInVisibleRange = Vector3.DistanceSquared(lastPosition, pos) <= maxRange + val now = System.currentTimeMillis() //ms + val durationSince = now - lastTime //ms + if ((!spectating && inVisibleRange && !lastMsg.contains(reply)) || + (inVisibleRange && wasSpectating && !spectating)) { //this condition is unlikely, but ... + lazy val targetDelay = { + val populationOver = math.max( + 0, + continent.blockMap.sector(ourPosition, range=drawConfig.rangeMax.toFloat).livePlayerList.size - drawConfig.populationThreshold + ) + val distanceAdjustment = math.pow(populationOver / drawConfig.populationStep * drawConfig.rangeStep, 2) //sq.m + val adjustedDistance = currentDistance + distanceAdjustment //sq.m + drawConfig.ranges.lastIndexWhere { dist => adjustedDistance > dist * dist } match { + case -1 => 1 + case index => drawConfig.delays(index) + } + } //ms + if (canSeeReallyFar || + currentDistance < drawConfig.rangeMin * drawConfig.rangeMin || + durationSince > drawConfig.delayMax || + sessionData.canSeeReallyFar || + durationSince > targetDelay) { + //must draw + sendResponse( + PlayerStateMessage( + guid, + pos, + vel, + yaw, + pitch, + yawUpper, + timestamp = 0, //is this okay? + isCrouching, + isJumping, + jumpThrust, + isCloaking + ) + ) + lastSeenStreamMessage(guid.guid) = SessionAvatarHandlers.LastUpstream(Some(pstate), now) + } else { + lastSeenStreamMessage(guid.guid) = SessionAvatarHandlers.LastUpstream(Some(pstate), lastTime) + } + } else if ( + (inVisibleRange && !wasSpectating && spectating) || + (!spectating && wasInVisibleRange && !inVisibleRange)) { + //must hide + val lat = (1 + hidingPlayerRandomizer.nextInt(continent.map.scale.height.toInt)).toFloat sendResponse( PlayerStateMessage( guid, - location, - vel, - yaw, - pitch, - yawUpper, - timestamp = 0, - isCrouching, - isJumping, - jumpThrust, - isCloaking + Vector3(1f, lat, 1f), + vel=None, + facingYaw=0f, + facingPitch=0f, + facingYawUpper=0f, + timestamp=0, //is this okay? + is_cloaked = isCloaking ) ) lastSeenStreamMessage(guid.guid) = SessionAvatarHandlers.LastUpstream(Some(pstate), now) diff --git a/src/main/scala/net/psforever/actors/session/support/SessionData.scala b/src/main/scala/net/psforever/actors/session/support/SessionData.scala index 37913f07d..33b17805d 100644 --- a/src/main/scala/net/psforever/actors/session/support/SessionData.scala +++ b/src/main/scala/net/psforever/actors/session/support/SessionData.scala @@ -2908,19 +2908,28 @@ class SessionData( } def canSeeReallyFar: Boolean = { - findEquipment().exists { - case weapon: Tool - if weapon.Size == EquipmentSize.Rifle && - (weapon.Projectile ne GlobalDefinitions.no_projectile) && - player.Crouching && - player.avatar - .implants - .exists { p => - p.collect { implant => implant.definition.implantType == ImplantType.RangeMagnifier && implant.initialized }.nonEmpty - } => - true - case item => - item.Definition == GlobalDefinitions.bolt_driver || item.Definition == GlobalDefinitions.heavy_sniper + shooting.FindContainedWeapon match { + case (Some(_: Vehicle), weapons) if weapons.nonEmpty => + player.avatar + .implants + .exists { p => + p.collect { implant => implant.definition.implantType == ImplantType.RangeMagnifier && implant.active }.nonEmpty + } + case (Some(_: Player), weapons) if weapons.nonEmpty => + val wep = weapons.head + wep.Definition == GlobalDefinitions.bolt_driver || + wep.Definition == GlobalDefinitions.heavy_sniper || + ( + (wep.Projectile ne GlobalDefinitions.no_projectile) && + player.Crouching && + player.avatar + .implants + .exists { p => + p.collect { implant => implant.definition.implantType == ImplantType.RangeMagnifier && implant.active }.nonEmpty + } + ) + case _ => + false } } diff --git a/src/main/scala/net/psforever/util/Config.scala b/src/main/scala/net/psforever/util/Config.scala index bd2d1c718..f8a6de807 100644 --- a/src/main/scala/net/psforever/util/Config.scala +++ b/src/main/scala/net/psforever/util/Config.scala @@ -159,7 +159,8 @@ case class GameConfig( baseCertifications: Seq[Certification], warpGates: WarpGateConfig, cavernRotation: CavernRotationConfig, - savedMsg: SavedMessageEvents + savedMsg: SavedMessageEvents, + playerDraw: PlayerStateDrawSettings ) case class NewAvatar( @@ -216,3 +217,17 @@ case class SavedMessageTimings( fixed: Long, variable: Long ) + +case class PlayerStateDrawSettings( + populationThreshold: Int, + populationStep: Int, + rangeMin: Int, + rangeMax: Int, + rangeStep: Int, + ranges: Seq[Int], + delayMax: Long, + delays: Seq[Long] +) { + assert(ranges.nonEmpty) + assert(ranges.size == delays.size) +}