Finish FastVector2 Optimization

This commit is contained in:
Gurge44 committed 2026-02-11 10:09:42 +01:00
1 parent a51be2d60b
commit b9198b9639
81 files changed
+91 -179

No files matched your search

-1
View File
@@ -5,7 +5,6 @@ using System.Diagnostics;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using Hazel;
using UnityEngine;
-1
View File
@@ -5,7 +5,6 @@ using System.Diagnostics;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using Hazel;
using UnityEngine;
-1
View File
@@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using UnityEngine;
-1
View File
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
-1
View File
@@ -4,7 +4,6 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using HarmonyLib;
using UnityEngine;
-1
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EHR.Modules.Extensions;
using EHR.Roles;
using Hazel;
using UnityEngine;
-1
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EHR.Modules.Extensions;
using UnityEngine;
namespace EHR.Modules;
-1
View File
@@ -6,7 +6,6 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using Hazel;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
-1
View File
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EHR.Modules.Extensions;
using EHR.Roles;
using HarmonyLib;
using Hazel;
-1
View File
@@ -5,7 +5,6 @@ using System.Linq;
using AmongUs.GameOptions;
using AmongUs.QuickChat;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using EHR.Modules.Extensions;
using HarmonyLib;
using Hazel;
using InnerNet;
-1
View File
@@ -8,7 +8,6 @@ using System.Reflection;
using AmongUs.GameOptions;
using EHR.Gamemodes;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using Hazel;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
+33 -2
View File
@@ -44,7 +44,7 @@ public static class FastVector2
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetClosestInRange(
in Vector2 origin,
IReadOnlyList<Vector2> positions,
IEnumerable<Vector2> positions,
float range,
out Vector2 closest)
{
@@ -72,6 +72,37 @@ public static class FastVector2
return found;
}
/// <summary>
/// Finds the closest position to origin.
/// Returns true if one was found.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetClosest(
in Vector2 origin,
IEnumerable<Vector2> positions,
out Vector2 closest)
{
float minSq = float.MaxValue;
bool found = false;
closest = default(Vector2);
foreach (Vector2 p in positions)
{
float dx = p.x - origin.x;
float dy = p.y - origin.y;
float sq = dx * dx + dy * dy;
if (sq < minSq)
{
minSq = sq;
closest = p;
found = true;
}
}
return found;
}
/// <summary>
/// Finds the closest living player to origin.
/// Returns true if one was found.
@@ -80,7 +111,7 @@ public static class FastVector2
public static bool TryGetClosestPlayer(
in Vector2 origin,
out PlayerControl closest,
Predicate<PlayerControl> predicate = null)
[Annotations.InstantHandle] Predicate<PlayerControl> predicate = null)
{
predicate ??= _ => true;
float minSq = float.MaxValue;
-1
View File
@@ -6,7 +6,6 @@ using AmongUs.GameOptions;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using EHR.Gamemodes;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Patches;
using EHR.Roles;
using HarmonyLib;
-1
View File
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using EHR.Modules.Extensions;
using HarmonyLib;
using Hazel;
using UnityEngine;
+1 -3
View File
@@ -1,10 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Gamemodes;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using HarmonyLib;
using Hazel;
@@ -199,7 +197,7 @@ internal static class ExternalRpcPetPatch
if (target.Is(CustomRoles.Detour))
{
PlayerControl tempTarget = target;
target = Main.EnumerateAlivePlayerControls().Where(x => x.PlayerId != target.PlayerId && x.PlayerId != pc.PlayerId).MinBy(x => Vector2.Distance(x.Pos(), target.Pos()));
FastVector2.TryGetClosestPlayer(target.Pos(), out target, x => x.PlayerId != target.PlayerId && x.PlayerId != pc.PlayerId);
Logger.Info($"Target was {tempTarget.GetNameWithRole()}, new target is {target.GetNameWithRole()}", "Detour");
if (tempTarget.AmOwner)
+2 -3
View File
@@ -137,7 +137,7 @@ internal static class CheckMurderPatch
if (target.Is(CustomRoles.Detour))
{
PlayerControl tempTarget = target;
target = Main.EnumerateAlivePlayerControls().Where(x => x.PlayerId != target.PlayerId && x.PlayerId != killer.PlayerId).MinBy(x => Vector2.Distance(x.Pos(), target.Pos()));
FastVector2.TryGetClosestPlayer(target.Pos(), out target, x => x.PlayerId != target.PlayerId && x.PlayerId != killer.PlayerId);
Logger.Info($"Target was {tempTarget.GetNameWithRole()}, new target is {target.GetNameWithRole()}", "Detour");
if (tempTarget.AmOwner)
@@ -2366,8 +2366,7 @@ static class PlayerControlRevivePatch
var sender = CustomRpcSender.Create($"LIReviveSync:{__instance.GetRealName()}", SendOption.Reliable);
var hasValue = sender.SyncGeneralOptions(__instance);
sender.SendMessage(dispose: !hasValue);
Vector2 pos = __instance.Pos();
__instance.TP(Main.EnumerateAlivePlayerControls().Without(__instance).Select(x => x.Pos()).Concat(ShipStatus.Instance.AllVents.Select(x => new Vector2(x.transform.position.x, x.transform.position.y + 0.3636f))).MinBy(x => Vector2.Distance(pos, x)));
__instance.TP(FastVector2.TryGetClosest(__instance.Pos(), Main.EnumerateAlivePlayerControls().Without(__instance).Select(x => x.Pos()).Concat(ShipStatus.Instance.AllVents.Select(x => new Vector2(x.transform.position.x, x.transform.position.y + 0.3636f))), out Vector2 closest) ? closest : Vector2.zero);
}
}, 0.2f);
}
-1
View File
@@ -2,7 +2,6 @@ using System;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Roles;
using HarmonyLib;
using Hazel;
-1
View File
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using EHR.Modules.Extensions;
using UnityEngine;
namespace EHR.Roles;
-1
View File
@@ -1,6 +1,5 @@
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
namespace EHR.Roles;
-1
View File
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
namespace EHR.Roles;
-1
View File
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using EHR.Modules.Extensions;
using UnityEngine;
using static EHR.Options;
-1
View File
@@ -2,7 +2,6 @@
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
-2
View File
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
-1
View File
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules.Extensions;
namespace EHR.Roles;
-1
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
-1
View File
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using EHR.Modules;
using EHR.Modules.Extensions;
using UnityEngine;
namespace EHR.Roles;
-1
View File
@@ -4,7 +4,6 @@ using System.Linq;
using AmongUs.GameOptions;
using EHR.Gamemodes;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using UnityEngine;
using static EHR.Options;
-1
View File
@@ -1,6 +1,5 @@
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
namespace EHR.Roles;
@@ -1,7 +1,5 @@
using System;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
using HarmonyLib;
namespace EHR.Roles;
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
@@ -83,12 +82,7 @@ public class Whisperer : RoleBase
public override void OnPet(PlayerControl pc)
{
if (pc.GetAbilityUseLimit() < 1 || Souls.Exists(x => x.IsQuestioning)) return;
Vector2 pos = pc.Pos();
List<Soul> souls = Souls.FindAll(x => x.IsQuestionAble && FastVector2.DistanceWithinRange(pos, x.Position, 1f));
if (souls.Count == 0) return;
Soul soul = souls.MinBy(x => Vector2.Distance(pos, x.Position));
if (!FastVector2.TryGetClosestInRange(pc.Pos(), Souls.FindAll(x => x.IsQuestionAble).ToDictionary(x => x.Position, x => x), 1f, out Soul soul)) return;
soul.QuestioningTime = Duration.GetInt();
CurrentlyQuestioning.Name = soul.Player.PlayerId.ColoredPlayerName();
Utils.SendRPC(CustomRPC.SyncRoleData, WhispererId, 1, CurrentlyQuestioning.Name);
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using AmongUs.GameOptions;
using EHR.Modules;
using UnityEngine;
using EHR.Modules.Extensions;
namespace EHR.Roles;
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using Color = UnityEngine.Color;
@@ -1,5 +1,4 @@
using System.Linq;
using static EHR.Options;
using static EHR.Options;
namespace EHR.Roles;
@@ -37,7 +36,7 @@ internal class SuperStar : RoleBase
public override bool OnCheckMurderAsTarget(PlayerControl killer, PlayerControl target)
{
return !Main.EnumerateAlivePlayerControls().Any(x => x.PlayerId != killer.PlayerId && x.PlayerId != target.PlayerId && Vector2.Distance(x.Pos(), target.Pos()) < 2f);
return !FastVector2.TryGetClosestPlayerInRange(target.Pos(), 2f, out _, x => x.PlayerId != killer.PlayerId && x.PlayerId != target.PlayerId);
}
public override bool KnowRole(PlayerControl seer, PlayerControl target)
@@ -1,6 +1,4 @@
using System.Linq;
namespace EHR.Roles;
namespace EHR.Roles;
public class Transmitter : RoleBase
{
@@ -24,6 +22,6 @@ public class Transmitter : RoleBase
public override void OnTaskComplete(PlayerControl pc, int completedTaskCount, int totalTaskCount)
{
pc.TP(Main.EnumerateAlivePlayerControls().OrderBy(x => Vector2.Distance(x.Pos(), pc.Pos())).FirstOrDefault(x => x.PlayerId != pc.PlayerId) ?? pc);
pc.TP(FastVector2.TryGetClosestPlayer(pc.Pos(), out PlayerControl closest, x => x.PlayerId != pc.PlayerId) ? closest : pc);
}
}
+2 -4
View File
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using EHR.Patches;
using Hazel;
using UnityEngine;
@@ -245,8 +244,7 @@ internal class Adventurer : RoleBase
RemoveAndNotify();
break;
case Weapon.Prediction:
PlayerControl closest = Main.EnumerateAlivePlayerControls().Where(x => x.PlayerId != pc.PlayerId).MinBy(x => Vector2.Distance(pc.Pos(), x.Pos()));
case Weapon.Prediction when FastVector2.TryGetClosestPlayer(pc.Pos(), out PlayerControl closest, x => x.PlayerId != pc.PlayerId):
RevealedPlayers.Add(closest.PlayerId);
RemoveAndNotify(closest);
break;
@@ -297,7 +295,7 @@ internal class Adventurer : RoleBase
Utils.NotifyRoles(SpecifySeer: AdventurerPC, SpecifyTarget: AdventurerPC);
}
if (LastGroupingResourceTimeStamp + 20 <= now && Main.EnumerateAlivePlayerControls().Count(x => x.PlayerId != pc.PlayerId && Vector2.Distance(x.Pos(), pc.Pos()) < 2f) >= 2)
if (LastGroupingResourceTimeStamp + 20 <= now && Main.EnumerateAlivePlayerControls().Count(x => x.PlayerId != pc.PlayerId && FastVector2.DistanceWithinRange(x.Pos(), pc.Pos(), 2f)) >= 2)
{
if (ResourceLocations.TryGetValue(Resource.Grouping, out Vector2 location))
{
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
@@ -103,7 +102,7 @@ public class Astral : RoleBase
if (!pc.IsInsideMap())
{
Vector2 playerPosition = pc.Pos();
Vector2 closestSpawnPosition = RandomSpawn.SpawnMap.GetSpawnMap().Positions.Values.MinBy(x => Vector2.Distance(playerPosition, x));
Vector2 closestSpawnPosition = FastVector2.TryGetClosest(playerPosition, RandomSpawn.SpawnMap.GetSpawnMap().Positions.Values, out Vector2 closest1) ? closest1 : new Vector2(50f, 50f);
Vector3 closestVentPosition = pc.GetClosestVent()?.transform.position ?? closestSpawnPosition;
pc.TP(Vector2.Distance(playerPosition, closestVentPosition) < Vector2.Distance(playerPosition, closestSpawnPosition) ? closestVentPosition : closestSpawnPosition);
}
+2 -4
View File
@@ -1,6 +1,4 @@
using System.Linq;
namespace EHR.Roles;
namespace EHR.Roles;
internal class Autocrat : RoleBase
{
@@ -26,6 +24,6 @@ internal class Autocrat : RoleBase
{
if (!pc.IsAlive()) return;
Main.EnumerateAlivePlayerControls().OrderBy(x => Vector2.Distance(x.Pos(), pc.Pos())).FirstOrDefault(x => x.PlayerId != pc.PlayerId)?.TP(pc);
(FastVector2.TryGetClosestPlayer(pc.Pos(), out PlayerControl closest, x => x.PlayerId != pc.PlayerId) ? closest : pc).TP(pc);
}
}
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using EHR.Modules;
using EHR.Modules.Extensions;
namespace EHR.Roles;
+1 -2
View File
@@ -52,8 +52,7 @@ internal class Bodyguard : RoleBase
{
if (bodyguard.BodyguardPC == null || bodyguard.BodyguardPC.PlayerId == target.PlayerId) continue;
float dis = Vector2.Distance(bodyguard.BodyguardPC.Pos(), target.Pos());
if (dis > BodyguardProtectRadius.GetFloat()) return true;
if (!FastVector2.DistanceWithinRange(bodyguard.BodyguardPC.Pos(), target.Pos(), BodyguardProtectRadius.GetFloat())) continue;
if (bodyguard.BodyguardPC.IsMadmate() && killer.Is(Team.Impostor))
{
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using EHR.Modules.Extensions;
namespace EHR.Roles;
@@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
using static EHR.Options;
-1
View File
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using Hazel;
@@ -117,12 +116,7 @@ public class Ambusher : RoleBase
if (!DontCheck)
{
Vector2 pos = pc.Pos();
float radius = FollowRadius.GetFloat();
(PlayerControl pc, float distance)[] nearPlayers = Main.EnumerateAlivePlayerControls().Without(pc).Select(x => (pc: x, distance: Vector2.Distance(x.Pos(), pos))).Where(x => x.distance <= radius).ToArray();
PlayerControl closestPlayer = nearPlayers.Length == 0 ? null : nearPlayers.MinBy(x => x.distance).pc;
if (closestPlayer == null)
if (!FastVector2.TryGetClosestPlayerInRange(pc.Pos(), FollowRadius.GetFloat(), out PlayerControl closestPlayer, x => x.PlayerId != pc.PlayerId))
{
TargetId = byte.MaxValue;
TargetTimer = FollowDuration.GetFloat();
@@ -168,8 +168,7 @@ public class Lightning : RoleBase
if (pc.PlayerId != gs.PlayerId && pc.IsAlive() && !pc.Is(CustomRoles.Lightning) && !IsGhost(pc) && !Pelican.IsEaten(pc.PlayerId))
{
Vector3 pos = gs.Pos();
float dis = Vector2.Distance(pos, pc.Pos());
if (dis > 0.3f) continue;
if (!FastVector2.DistanceWithinRange(pos, pc.Pos(), 0.3f)) continue;
deList.Add(gs.PlayerId);
gs.Suicide(PlayerState.DeathReason.Quantization, RealKiller[gs.PlayerId]);
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using static EHR.Options;
using static EHR.Translator;
@@ -99,7 +98,7 @@ public class RiftMaker : RoleBase
if (LastTP + 5 > now) return;
Vector2 pos = player.Pos();
if (!Marks.FindFirst(x => Vector2.Distance(x, pos) <= 1f, out Vector2 nearMark)) return;
if (!Marks.FindFirst(x => FastVector2.DistanceWithinRange(x, pos, 1f), out Vector2 nearMark)) return;
int index = Marks.IndexOf(nearMark);
Vector2 target = Marks[1 - index];
@@ -52,7 +52,7 @@ internal class Scavenger : RoleBase
{
while (dur > 0)
{
if (killer == null || target == null || Vector2.Distance(killer.Pos(), target.Pos()) > 2f) yield break;
if (killer == null || target == null || !FastVector2.DistanceWithinRange(killer.Pos(), target.Pos(), 2f)) yield break;
dur -= Time.fixedDeltaTime;
yield return null;
}
@@ -2,7 +2,6 @@
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
@@ -169,9 +168,7 @@ public class Abyssbringer : RoleBase
continue;
}
PlayerControl nearestPlayer = Main.EnumerateAlivePlayerControls().Without(pc).MinBy(x => Vector2.Distance(x.Pos(), blackHole.Position));
if (nearestPlayer != null)
if (FastVector2.TryGetClosestPlayer(blackHole.Position, out PlayerControl nearestPlayer, x => x.PlayerId != pc.PlayerId))
{
Vector2 pos = nearestPlayer.Pos();
+1 -6
View File
@@ -151,13 +151,8 @@ internal class Bomber : RoleBase
{
if (!tg.IsModdedClient()) tg.KillFlash();
Vector2 pos = pc.Pos();
float dis = Vector2.Distance(pos, tg.Pos());
if (!tg.IsAlive() || Pelican.IsEaten(tg.PlayerId) || Medic.ProtectList.Contains(tg.PlayerId) || (tg.Is(CustomRoleTypes.Impostor) && ImpostorsSurviveBombs.GetBool()) || tg.inVent || tg.Is(CustomRoles.Pestilence)) continue;
if (dis > radius) continue;
if (!FastVector2.DistanceWithinRange(pc.Pos(), tg.Pos(), radius)) continue;
if (tg.PlayerId == pc.PlayerId) continue;
tg.Suicide(PlayerState.DeathReason.Bombed, pc);
@@ -164,8 +164,7 @@ public class Fireworker : RoleBase
{
foreach (Vector3 pos in fireworksPosition)
{
float dis = Vector2.Distance(pos, target.Pos());
if (dis > FireworksRadius) continue;
if (!FastVector2.DistanceWithinRange(pos, target.Pos(), FireworksRadius)) continue;
if (target == pc)
suicide = true;
@@ -34,11 +34,8 @@ public class Psychopath : RoleBase
public override void OnFixedUpdate(PlayerControl pc)
{
if (!GameStates.IsInTask || ExileController.Instance || AntiBlackout.SkipTasks || !pc.IsAlive() || Main.KillTimers[pc.PlayerId] > 0f || Count++ < 10) return;
var pos = pc.Pos();
var killRange = NormalGameOptionsV10.KillDistances[Mathf.Clamp(Main.NormalOptions.KillDistance, 0, 2)];
var nearPlayers = Main.EnumerateAlivePlayerControls().Without(pc).Where(x => !x.IsImpostor()).Select(x => (pc: x, distance: Vector2.Distance(x.Pos(), pos))).Where(x => x.distance <= killRange).ToArray();
PlayerControl closestPlayer = nearPlayers.Length == 0 ? null : nearPlayers.MinBy(x => x.distance).pc;
if (closestPlayer == null) return;
if (!FastVector2.TryGetClosestPlayerInRange(pc.Pos(), killRange, out PlayerControl closestPlayer, x => x.PlayerId != pc.PlayerId && !x.IsImpostor())) return;
Count = 0;
pc.RpcCheckAndMurder(closestPlayer);
}
@@ -2,7 +2,6 @@
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using static EHR.Options;
using static EHR.Translator;
using static EHR.Utils;
+1 -4
View File
@@ -140,11 +140,8 @@ public class Witch : RoleBase
{
if (NowSwitchTrigger == SwitchTrigger.Vanish)
{
var pos = pc.Pos();
var killRange = NormalGameOptionsV10.KillDistances[Mathf.Clamp(Main.NormalOptions.KillDistance, 0, 2)];
var nearPlayers = Main.EnumerateAlivePlayerControls().Without(pc).Where(x => !x.IsImpostor()).Select(x => (pc: x, distance: Vector2.Distance(x.Pos(), pos))).Where(x => x.distance <= killRange).ToArray();
PlayerControl target = nearPlayers.Length == 0 ? null : nearPlayers.MinBy(x => x.distance).pc;
if (target == null) return false;
if (!FastVector2.TryGetClosestPlayerInRange(pc.Pos(), killRange, out PlayerControl target, x => x.PlayerId != pc.PlayerId && !x.IsImpostor())) return false;
SetSpelled(pc, target);
}
@@ -48,17 +48,12 @@ internal class Crewpostor : RoleBase
SendRPC(player.PlayerId, TasksDone[player.PlayerId]);
PlayerControl[] list = Main.EnumerateAlivePlayerControls().Where(x => x.PlayerId != player.PlayerId && (CrewpostorCanKillAllies.GetBool() || !x.GetCustomRole().IsImpostorTeam())).ToArray();
if (list.Length == 0)
if (!FastVector2.TryGetClosestPlayer(player.Pos(), out PlayerControl target, x => x.PlayerId != player.PlayerId && (CrewpostorCanKillAllies.GetBool() || !x.GetCustomRole().IsImpostorTeam())))
Logger.Info("No target to kill", "Crewpostor");
else if (TasksDone[player.PlayerId] % CrewpostorKillAfterTask.GetInt() != 0 && TasksDone[player.PlayerId] != 0)
Logger.Info($"Crewpostor task done but kill skipped, {TasksDone[player.PlayerId]} tasks completed, but it kills after {CrewpostorKillAfterTask.GetInt()} tasks", "Crewpostor");
else
{
list = [.. list.OrderBy(x => Vector2.Distance(player.Pos(), x.Pos()))];
PlayerControl target = list[0];
if (!target.Is(CustomRoles.Pestilence))
{
if (!CrewpostorLungeKill.GetBool())
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
namespace EHR.Roles;
+2 -1
View File
@@ -123,7 +123,8 @@ public class Librarian : RoleBase
PlayerControl pc = GetPlayerById(id);
if (pc == null || !pc.IsAlive()) continue;
if (Vector2.Distance(pc.Pos(), reporter.Pos()) <= silenceRadius) librarian = pc;
if (FastVector2.DistanceWithinRange(pc.Pos(), reporter.Pos(), silenceRadius))
librarian = pc;
}
if (librarian == null) return true;
+1 -4
View File
@@ -125,11 +125,8 @@ public class Silencer : RoleBase
{
if (SilenceMode.GetValue() == 2)
{
var pos = pc.Pos();
var killRange = NormalGameOptionsV10.KillDistances[Mathf.Clamp(Main.NormalOptions.KillDistance, 0, 2)];
var nearPlayers = Main.EnumerateAlivePlayerControls().Without(pc).Select(x => (pc: x, distance: Vector2.Distance(x.Pos(), pos))).Where(x => x.distance <= killRange).ToArray();
PlayerControl target = nearPlayers.Length == 0 ? null : nearPlayers.MinBy(x => x.distance).pc;
if (target == null) return false;
if (!FastVector2.TryGetClosestPlayerInRange(pc.Pos(), killRange, out PlayerControl target, x => x.PlayerId != pc.PlayerId)) return false;
ForSilencer = [target.PlayerId];
Utils.SendRPC(CustomRPC.SyncRoleData, pc.PlayerId, 1, target.PlayerId);
+12 -21
View File
@@ -110,31 +110,22 @@ public class YinYanger : RoleBase
SendRPC(true);
}
public override void OnGlobalFixedUpdate(PlayerControl player, bool lowLoad)
public override void OnFixedUpdate(PlayerControl pc)
{
if (!GameStates.IsInTask) return;
if (!GameStates.IsInTask || !IsEnable || YinYangedPlayers.Count < 2) return;
PlayerControl yyPc = YinYanger_;
PlayerControl pc1 = GetPlayerById(YinYangedPlayers[0]);
PlayerControl pc2 = GetPlayerById(YinYangedPlayers[1]);
foreach (KeyValuePair<byte, PlayerState> state in Main.PlayerStates)
if (pc1 == null || pc2 == null || yyPc == null || !pc1.IsAlive() || !pc2.IsAlive() || !yyPc.IsAlive()) return;
if (FastVector2.DistanceWithinRange(pc1.Pos(), pc2.Pos(), 2f))
{
if (state.Value.Role is not YinYanger { IsEnable: true, YinYangedPlayers.Count: 2 } yy) continue;
if (!yyPc.RpcCheckAndMurder(pc1, true) || !yyPc.RpcCheckAndMurder(pc2, true)) return;
PlayerControl yyPc = yy.YinYanger_;
PlayerControl pc1 = GetPlayerById(yy.YinYangedPlayers[0]);
PlayerControl pc2 = GetPlayerById(yy.YinYangedPlayers[1]);
if (pc1 == null || pc2 == null || yyPc == null) return;
if (!pc1.IsAlive() || !pc2.IsAlive() || !yyPc.IsAlive()) return;
if (Vector2.Distance(pc1.Pos(), pc2.Pos()) <= 2f)
{
if (!yyPc.RpcCheckAndMurder(pc1, true)
|| !yyPc.RpcCheckAndMurder(pc2, true))
return;
pc1.Suicide(PlayerState.DeathReason.YinYanged, yyPc);
pc2.Suicide(PlayerState.DeathReason.YinYanged, yyPc);
}
pc1.Suicide(PlayerState.DeathReason.YinYanged, yyPc);
pc2.Suicide(PlayerState.DeathReason.YinYanged, yyPc);
}
}
@@ -177,9 +177,8 @@ internal class Revolutionist : RoleBase
else
{
float range = NormalGameOptionsV10.KillDistances[Mathf.Clamp(player.Is(CustomRoles.Reach) ? 2 : Main.NormalOptions.KillDistance, 0, 2)] + 0.5f;
float dis = Vector2.Distance(player.Pos(), rvTarget.Pos());
if (dis <= range)
if (FastVector2.DistanceWithinRange(player.Pos(), rvTarget.Pos(), range))
RevolutionistTimer[playerId] = (rvTarget, rvTime + Time.fixedDeltaTime);
else
{
+1 -2
View File
@@ -227,9 +227,8 @@ internal class Arsonist : RoleBase
else
{
float range = NormalGameOptionsV10.KillDistances[Mathf.Clamp(player.Is(CustomRoles.Reach) ? 2 : Main.NormalOptions.KillDistance, 0, 2)] + 0.5f;
float dis = Vector2.Distance(player.Pos(), arTarget.Pos());
if (dis <= range)
if (FastVector2.DistanceWithinRange(player.Pos(), arTarget.Pos(), range))
ArsonistTimer[playerId] = (arTarget, arTime + Time.fixedDeltaTime);
else
{
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
using static EHR.Options;
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
+1 -2
View File
@@ -2,7 +2,6 @@
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using static EHR.Options;
using static EHR.Utils;
@@ -171,7 +170,7 @@ internal class Bubble : RoleBase
if (ts + NotifyDelay.GetInt() < now)
{
Main.EnumerateAlivePlayerControls().Where(x => (!LastUpdates.TryGetValue(x.PlayerId, out long la) || la != now) && Vector2.Distance(x.Pos(), encasedPc.Pos()) < 5f).Do(x =>
Main.EnumerateAlivePlayerControls().Where(x => (!LastUpdates.TryGetValue(x.PlayerId, out long la) || la != now) && FastVector2.DistanceWithinRange(x.Pos(), encasedPc.Pos(), 5f)).Do(x =>
{
NotifyRoles(SpecifySeer: x, SpecifyTarget: encasedPc);
LastUpdates[x.PlayerId] = now;
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
using static EHR.Options;
@@ -1,5 +1,4 @@
using AmongUs.GameOptions;
using EHR.Modules.Extensions;
namespace EHR.Roles;
+1 -2
View File
@@ -216,8 +216,7 @@ public class Infection : RoleBase
if (oldRate >= 100) continue;
// Exclude players outside the range
float distance = Vector2.Distance(player.Pos(), target.Pos());
if (distance > InfectDistance) continue;
if (!FastVector2.DistanceWithinRange(player.Pos(), target.Pos(), InfectDistance)) continue;
float newRate = oldRate + (Time.fixedDeltaTime / InfectTime * 100);
newRate = Math.Clamp(newRate, 0, 100);
+1 -3
View File
@@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using UnityEngine;
using static EHR.Options;
using static EHR.Translator;
@@ -335,8 +334,7 @@ public class Magician : RoleBase
foreach (Vector2 mark in PortalMarks.ToArray())
{
float dis = Vector2.Distance(mark, position);
if (dis > 2f) continue;
if (!FastVector2.DistanceWithinRange(mark, position, 2f)) continue;
isTP = true;
from = mark;
@@ -2,7 +2,6 @@
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using static EHR.Options;
using static EHR.Translator;
+1 -2
View File
@@ -255,8 +255,7 @@ public class Pelican : RoleBase
if (target == null) continue;
Vector2 pos = GetBlackRoomPS();
float dis = Vector2.Distance(pos, target.Pos());
if (dis < 2f) continue;
if (FastVector2.DistanceWithinRange(pos, target.Pos(), 2f)) continue;
target.TP(pos, log: false);
Utils.NotifyRoles(SpecifySeer: target, SpecifyTarget: pc);
-1
View File
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
+1 -1
View File
@@ -114,7 +114,7 @@ internal class Samurai : RoleBase
PlayerControl target = Utils.GetPlayerById(Target.Id);
if (target == null) return;
if (Vector2.Distance(target.Pos(), pc.Pos()) > NormalGameOptionsV10.KillDistances[Mathf.Clamp(pc.Is(CustomRoles.Reach) ? 2 : Main.NormalOptions.KillDistance, 0, 2)] + 0.5f)
if (!FastVector2.DistanceWithinRange(target.Pos(), pc.Pos(), NormalGameOptionsV10.KillDistances[Mathf.Clamp(pc.Is(CustomRoles.Reach) ? 2 : Main.NormalOptions.KillDistance, 0, 2)] + 0.5f))
{
Target = (byte.MaxValue, 0);
pc.RpcCheckAndMurder(target);
@@ -125,11 +125,8 @@ public class Sharpshooter : RoleBase
}
else
{
var pos = pc.Pos();
var killRange = NormalGameOptionsV10.KillDistances[Mathf.Clamp(Main.NormalOptions.KillDistance, 0, 2)];
var nearPlayers = Main.EnumerateAlivePlayerControls().Without(pc).Select(x => (pc: x, distance: Vector2.Distance(x.Pos(), pos))).Where(x => x.distance <= killRange).ToArray();
PlayerControl closestPlayer = nearPlayers.Length == 0 ? null : nearPlayers.MinBy(x => x.distance).pc;
if (closestPlayer == null || !pc.RpcCheckAndMurder(closestPlayer, check: true)) return;
if (!FastVector2.TryGetClosestPlayerInRange(pc.Pos(), killRange, out PlayerControl closestPlayer, x => x.PlayerId != pc.PlayerId) || !pc.RpcCheckAndMurder(closestPlayer, check: true)) return;
if (!Options.UsePets.GetBool()) pc.RpcResetAbilityCooldown();
RevertAbility();
pc.SetKillCooldown();
+1 -1
View File
@@ -82,7 +82,7 @@ public class Slenderman : RoleBase
return;
}
bool inRange = Vector2.Distance(pc.Pos(), SlendermanPC.Pos()) <= BlindRange.GetFloat();
bool inRange = FastVector2.DistanceWithinRange(pc.Pos(), SlendermanPC.Pos(), BlindRange.GetFloat());
if ((inRange && Blinded.Add(pc.PlayerId)) || (!inRange && Blinded.Remove(pc.PlayerId)))
{
-1
View File
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Roles;
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using AmongUs.GameOptions;
using EHR.Modules.Extensions;
using static EHR.Options;
using static EHR.Translator;
using static EHR.Utils;
+3 -5
View File
@@ -160,12 +160,10 @@ internal class Tiger : RoleBase
public override void OnMurder(PlayerControl killer, PlayerControl target)
{
if (float.IsNaN(EnrageTimer)) return;
if (float.IsNaN(EnrageTimer) || !FastVector2.TryGetClosestPlayerInRange(killer.Pos(), Radius.GetFloat(), out PlayerControl victim, x => x.PlayerId != killer.PlayerId && x.PlayerId != target.PlayerId)) return;
PlayerControl victim = Main.EnumerateAlivePlayerControls().Where(x => x.PlayerId != killer.PlayerId && x.PlayerId != target.PlayerId).MinBy(x => Vector2.Distance(killer.Pos(), x.Pos()));
if (victim == null || Vector2.Distance(killer.Pos(), victim.Pos()) > Radius.GetFloat()) return;
if (killer.RpcCheckAndMurder(victim, true)) victim.Suicide(realKiller: killer);
if (killer.RpcCheckAndMurder(victim, true))
victim.Suicide(realKiller: killer);
}
public override string GetSuffix(PlayerControl seer, PlayerControl target, bool hud = false, bool meeting = false)
+6 -8
View File
@@ -88,13 +88,14 @@ public class Tremor : RoleBase
if (!GameStates.IsInTask || ExileController.Instance || !pc.IsAlive()) return;
bool wasDoom = IsDoom;
long now = Utils.TimeStamp;
if (!IsDoom && LastUpdate != Utils.TimeStamp)
if (!IsDoom && LastUpdate != now)
{
Timer--;
Utils.SendRPC(CustomRPC.SyncRoleData, pc.PlayerId, Timer);
Utils.NotifyRoles(SpecifySeer: pc, SpecifyTarget: pc);
LastUpdate = Utils.TimeStamp;
LastUpdate = now;
}
if (wasDoom != IsDoom)
@@ -113,14 +114,11 @@ public class Tremor : RoleBase
Count = 0;
Vector2 pos = pc.Pos();
Main.EnumerateAlivePlayerControls()
.Where(x => x.PlayerId != pc.PlayerId && Vector2.Distance(pos, x.Pos()) <= 2.5f)
FastVector2.GetPlayersInRange(pc.Pos(), 2.5f, x => x.PlayerId != pc.PlayerId)
.Do(pc.Kill);
if (LastUpdate == Utils.TimeStamp) return;
LastUpdate = Utils.TimeStamp;
if (LastUpdate == now) return;
LastUpdate = now;
DoomTimer--;
Utils.SendRPC(CustomRPC.SyncRoleData, pc.PlayerId, DoomTimer);
@@ -194,7 +194,7 @@ public class WeaponMaster : RoleBase
{
if (Pelican.IsEaten(player.PlayerId) || player == killer || target == player || player.Is(CustomRoles.Pestilence) || Veteran.VeteranInProtect.Contains(target.PlayerId)) continue;
if (Vector2.Distance(killer.Pos(), player.Pos()) <= Radius.GetFloat())
if (FastVector2.DistanceWithinRange(killer.Pos(), player.Pos(), Radius.GetFloat()))
player.Suicide(PlayerState.DeathReason.Kill, killer);
if (killer.AmOwner && player.Is(CustomRoles.Tree))