Significantly lower Snowdown network pressure by reusing CNOs

This commit is contained in:
Gurge44 committed 2025-12-18 11:04:29 +01:00
1 parent c964c83a44
commit d171567c4f
2 files changed
+55 -25

No files matched your search

+13 -6
View File
@@ -59,7 +59,7 @@ public static class Snowdown
.SetGameMode(gameMode)
.SetValueFormat(OptionFormat.Multiplier);
SnowballGainFrequencyOption = new IntegerOptionItem(id++, "Snowdown.SnowballGainFrequencyOption", new(1, 30, 1), 10, tab)
SnowballGainFrequencyOption = new IntegerOptionItem(id++, "Snowdown.SnowballGainFrequencyOption", new(1, 30, 1), 5, tab)
.SetColor(color)
.SetGameMode(gameMode)
.SetValueFormat(OptionFormat.Seconds);
@@ -255,13 +255,13 @@ public static class Snowdown
if (!AmongUsClient.Instance.AmHost || !GameStates.IsInTask || ExileController.Instance || AntiBlackout.SkipTasks || Options.CurrentGameMode != CustomGameMode.Snowdown || !Main.IntroDestroyed) return;
if (!__instance.IsAlive() || !Data.TryGetValue(__instance.PlayerId, out PlayerData data)) return;
var now = Utils.TimeStamp;
var pos = __instance.Pos();
var touchingSnowball = Snowballs.Find(x => x.Thrower != __instance && Vector2.Distance(x.Position, pos) < 1.5f);
long now = Utils.TimeStamp;
Vector2 pos = __instance.Pos();
Snowball touchingSnowball = Snowballs.Find(x => x.Active && x.Thrower != __instance && Vector2.Distance(x.Position, pos) < 1.5f);
if (touchingSnowball != null)
{
touchingSnowball.Despawn();
touchingSnowball.SetInactive();
if (touchingSnowball.Thrower != null && Data.TryGetValue(touchingSnowball.Thrower.PlayerId, out PlayerData throwerData) && throwerData.Coins < throwerData.MaxCoins)
throwerData.Coins++;
@@ -391,7 +391,14 @@ public static class Snowdown
{
if (SnowballsReady <= 0) return;
SnowballsReady--;
Snowballs.Add(new Snowball(pc.Pos(), (LastPosition - LastLastPosition).normalized, pc));
Vector2 from = pc.Pos();
Vector2 direction = (LastPosition - LastLastPosition).normalized;
Snowball inactive = Snowballs.Find(x => !x.Active);
if (inactive != null) inactive.Reuse(from, direction, pc);
else Snowballs.Add(new Snowball(from, direction, pc));
}
}
+42 -19
View File
@@ -81,15 +81,6 @@ namespace EHR
public void TP(Vector2 position)
{
if (!AmongUsClient.Instance.AmHost) return;
if (AmongUsClient.Instance.AmClient) playerControl.NetTransform.SnapTo(position, (ushort)(playerControl.NetTransform.lastSequenceId + 1U));
ushort num = (ushort)(playerControl.NetTransform.lastSequenceId + 2U);
MessageWriter messageWriter = AmongUsClient.Instance.StartRpcImmediately(playerControl.NetTransform.NetId, 21, SendOption.None);
NetHelpers.WriteVector2(position, messageWriter);
messageWriter.Write(num);
AmongUsClient.Instance.FinishRpcImmediately(messageWriter);
Position = position;
}
@@ -160,7 +151,16 @@ namespace EHR
writer.Recycle();
}
protected virtual void OnFixedUpdate() { }
protected virtual void OnFixedUpdate()
{
if (!AmongUsClient.Instance.AmHost) return;
if (AmongUsClient.Instance.AmClient) playerControl.NetTransform.SnapTo(Position, (ushort)(playerControl.NetTransform.lastSequenceId + 1U));
ushort num = (ushort)(playerControl.NetTransform.lastSequenceId + 2U);
MessageWriter messageWriter = AmongUsClient.Instance.StartRpcImmediately(playerControl.NetTransform.NetId, 21, SendOption.None);
NetHelpers.WriteVector2(Position, messageWriter);
messageWriter.Write(num);
AmongUsClient.Instance.FinishRpcImmediately(messageWriter);
}
protected void CreateNetObject(string sprite, Vector2 position)
{
@@ -191,6 +191,10 @@ namespace EHR
playerControl.PlayerId = 254;
playerControl.isNew = false;
playerControl.notRealPlayer = true;
try { playerControl.NetTransform.SnapTo(new Vector2(50f, 50f)); }
catch (Exception e) { Utils.ThrowException(e); }
AmongUsClient.Instance.NetIdCnt += 1U;
MessageWriter msg = MessageWriter.Get(SendOption.Reliable);
msg.StartMessage(5);
@@ -364,6 +368,10 @@ namespace EHR
playerControl.PlayerId = 254;
playerControl.isNew = false;
playerControl.notRealPlayer = true;
try { playerControl.NetTransform.SnapTo(new Vector2(50f, 50f)); }
catch (Exception e) { Utils.ThrowException(e); }
AmongUsClient.Instance.NetIdCnt += 1U;
MessageWriter msg = MessageWriter.Get(SendOption.Reliable);
msg.StartMessage(5);
@@ -914,33 +922,48 @@ namespace EHR
internal sealed class Snowball : CustomNetObject
{
public readonly PlayerControl Thrower;
private readonly Vector2 Direction;
public PlayerControl Thrower;
private Vector2 Direction;
public bool Active;
public Snowball(Vector2 from, Vector2 direction, PlayerControl thrower)
{
Thrower = thrower;
Direction = direction;
if (GameStates.CurrentServerType == GameStates.ServerType.Vanilla) return;
CreateNetObject("<line-height=97%><cspace=0.16em><#0000>W</color><mark=#e4fdff>WWWW</mark><#0000>W</color>\n<mark=#e4fdff>WWWWWW</mark>\n<mark=#e4fdff>WWWWWW</mark>\n<mark=#e4fdff>WWWWWW</mark>\n<mark=#e4fdff>WWWWWW</mark>\n<#0000>W</color><mark=#e4fdff>WWWW</mark><#0000>W", from);
Active = true;
}
protected override void OnFixedUpdate()
{
base.OnFixedUpdate();
if (!Active) return;
Vector2 newPos = Position + Direction * Time.fixedDeltaTime * Snowdown.SnowballThrowSpeed;
bool vanilla = GameStates.CurrentServerType == GameStates.ServerType.Vanilla;
if ((PhysicsHelpers.AnythingBetween(Position, newPos, Constants.ShipOnlyMask, false)) ||
newPos.x < Snowdown.MapBounds.X.Left || newPos.x > Snowdown.MapBounds.X.Right || newPos.y < Snowdown.MapBounds.Y.Bottom || newPos.y > Snowdown.MapBounds.Y.Top)
{
if (vanilla) AllObjects.Remove(this);
else Despawn();
SetInactive();
return;
}
if (vanilla) Position = newPos;
else TP(newPos);
TP(newPos);
}
public void SetInactive()
{
TP(new(50f, 50f));
Active = false;
}
public void Reuse(Vector2 from, Vector2 direction, PlayerControl thrower)
{
TP(from);
Thrower = thrower;
Direction = direction;
Active = true;
}
}
}