Code optimization

This commit is contained in:
Gurge44 committed 2025-12-26 12:45:13 +01:00
1 parent 4a520be228
commit e038c700a7
14 files changed
+87 -95

No files matched your search

+1 -1
View File
@@ -1455,7 +1455,7 @@ internal static class ExtendedPlayerControl
CustomGameMode.Snowdown => true,
CustomGameMode.Deathrace => Deathrace.CanUseVent(pc, pc.GetClosestVent().Id),
CustomGameMode.Standard when CopyCat.Instances.Any(x => x.CopyCatPC.PlayerId == pc.PlayerId) => true,
CustomGameMode.Standard when CopyCat.PlayerIdList.Contains(pc.PlayerId) => true,
CustomGameMode.Standard when pc.Is(CustomRoles.Nimble) || Options.EveryoneCanVent.GetBool() => true,
CustomGameMode.Standard when pc.Is(CustomRoles.Bloodlust) || pc.Is(CustomRoles.Renegade) => true,
+8
View File
@@ -0,0 +1,8 @@
using System.Diagnostics;
namespace EHR.Modules.Extensions;
public static class StopwatchHelper
{
public static int GetRemainingTime(this Stopwatch stopwatch, int totalTime) => totalTime - stopwatch.Elapsed.Seconds;
}
-6
View File
@@ -121,12 +121,6 @@ public static class FixedUpdateCaller
}
catch { }
try
{
if (CopyCat.Instances.Count > 0) CopyCat.Instances.RemoveAll(x => x.CopyCatPC == null);
}
catch { }
try
{
if (amongUsClient.AmHost && GameStates.InGame && !GameStates.IsEnded)
+1 -1
View File
@@ -179,7 +179,7 @@ public static class Statistics
case CustomRoles.Sheriff when won && lp.IsAlive() && Main.PlayerStates.Values.Where(x => x.IsDead && x.deathReason == PlayerState.DeathReason.Kill).All(x => x.GetRealKiller() == lp.PlayerId):
Achievements.Type.Superhero.CompleteAfterGameEnd();
break;
case CustomRoles.Snitch when lp.AllTasksCompleted() && CustomWinnerHolder.WinnerTeam == CustomWinner.Crewmate && won:
case CustomRoles.Snitch when lp.IsAlive() && lp.AllTasksCompleted() && CustomWinnerHolder.WinnerTeam == CustomWinner.Crewmate && won:
Achievements.Type.CrewHero.Complete();
break;
case CustomRoles.KillingMachine when Main.PlayerStates.Values.Count(x => x.GetRealKiller() == lp.PlayerId) >= 8:
+3 -3
View File
@@ -980,7 +980,7 @@ public static class Utils
break;
}
hasTasks &= !(CopyCat.Instances.Any(x => x.CopyCatPC.PlayerId == p.PlayerId) && forRecompute && (!Options.UsePets.GetBool() || CopyCat.UsePet.GetBool()));
hasTasks &= !(CopyCat.PlayerIdList.Contains(p.PlayerId) && forRecompute && (!Options.UsePets.GetBool() || CopyCat.UsePet.GetBool()));
hasTasks |= p.Object.UsesPetInsteadOfKill() && role is not (CustomRoles.Renegade or CustomRoles.Necromancer or CustomRoles.Deathknight or CustomRoles.Sidekick);
foreach (CustomRoles subRole in state.SubRoles)
@@ -2926,7 +2926,7 @@ public static class Utils
if (seer.Is(CustomRoleTypes.Impostor) && target.Is(CustomRoles.Snitch) && target.Is(CustomRoles.Madmate) && target.GetTaskState().IsTaskFinished)
TargetMark.Append(ColorString(GetRoleColor(CustomRoles.Impostor), "★"));
if (Marshall.CanSeeMarshall(seer) && target.Is(CustomRoles.Marshall) && target.GetTaskState().IsTaskFinished)
if (target.Is(CustomRoles.Marshall) && Marshall.CanSeeMarshall(seer) && target.GetTaskState().IsTaskFinished)
TargetMark.Append(ColorString(GetRoleColor(CustomRoles.Marshall), "★"));
TargetMark.Append(Executioner.TargetMark(seer, target));
@@ -3076,7 +3076,7 @@ public static class Utils
(target.Is(CustomRoles.Workaholic) && Workaholic.WorkaholicVisibleToEveryone.GetBool()) ||
(target.Is(CustomRoles.Doctor) && !target.HasEvilAddon() && Options.DoctorVisibleToEveryone.GetBool()) ||
(target.Is(CustomRoles.Mayor) && Mayor.MayorRevealWhenDoneTasks.GetBool() && target.GetTaskState().IsTaskFinished) ||
(Marshall.CanSeeMarshall(seer) && target.Is(CustomRoles.Marshall) && target.GetTaskState().IsTaskFinished) ||
(target.Is(CustomRoles.Marshall) && Marshall.CanSeeMarshall(seer) && target.GetTaskState().IsTaskFinished) ||
(Main.PlayerStates[target.PlayerId].deathReason == PlayerState.DeathReason.Vote && Options.SeeEjectedRolesInMeeting.GetBool()) ||
(CustomTeamManager.AreInSameCustomTeam(seer.PlayerId, target.PlayerId) && CustomTeamManager.IsSettingEnabledForPlayerTeam(seer.PlayerId, CTAOption.KnowRoles)) ||
Main.PlayerStates.Values.Any(x => x.Role.KnowRole(seer, target)) ||
+2 -2
View File
@@ -661,7 +661,7 @@ internal static class MurderPlayerPatch
if (killer.Is(CustomRoles.Sniper))
{
if (!Options.UsePets.GetBool())
if (!Options.UsePets.GetBool() || Options.UsePhantomBasis.GetBool())
killer.RpcResetAbilityCooldown();
else
{
@@ -2041,7 +2041,7 @@ internal static class FixedUpdatePatch
public static void LoversSuicide(byte deathId = 0x7f, bool exile = false, bool force = false, bool guess = false)
{
if (Options.CurrentGameMode != CustomGameMode.Standard) return;
if (Main.LoversPlayers.Count == 0 || Options.CurrentGameMode != CustomGameMode.Standard) return;
if (Lovers.LoverDieConsequence.GetValue() == 0 || Main.IsLoversDead || (Main.LoversPlayers.FindAll(x => x.IsAlive()).Count != 1 && !force)) return;
PlayerControl partnerPlayer = Main.LoversPlayers.FirstOrDefault(player => player.PlayerId != deathId && player.IsAlive());
+18 -10
View File
@@ -533,12 +533,14 @@ internal static class OnPlayerLeftPatch
{
if (AmongUsClient.Instance.AmHost && GameStates.IsInGame && data != null && data.Character != null)
{
ExtendedPlayerControl.TempExiled.Remove(data.Character.PlayerId);
byte id = data.Character.PlayerId;
ExtendedPlayerControl.TempExiled.Remove(id);
switch (Options.CurrentGameMode)
{
case CustomGameMode.HideAndSeek:
CustomHnS.PlayerRoles.Remove(data.Character.PlayerId);
CustomHnS.PlayerRoles.Remove(id);
break;
case CustomGameMode.Mingle when data.Character.IsAlive():
Mingle.HandleDisconnect();
@@ -556,13 +558,13 @@ internal static class OnPlayerLeftPatch
Main.PlayerStates[lovers.PlayerId].RemoveSubRole(CustomRoles.Lovers);
}
Main.LoversPlayers.RemoveAll(x => x.PlayerId == data.Character.PlayerId);
Main.LoversPlayers.RemoveAll(x => x.PlayerId == id);
}
switch (data.Character.GetCustomRole())
{
case CustomRoles.Pelican:
Pelican.OnPelicanDied(data.Character.PlayerId);
Pelican.OnPelicanDied(id);
break;
case CustomRoles.Markseeker:
Markseeker.OnDeath(data.Character);
@@ -572,23 +574,29 @@ internal static class OnPlayerLeftPatch
break;
}
if (Executioner.Target.ContainsValue(data.Character.PlayerId)) Executioner.ChangeRoleByTarget(data.Character);
if (Executioner.Target.ContainsValue(id))
Executioner.ChangeRoleByTarget(data.Character);
if (Lawyer.Target.ContainsValue(data.Character.PlayerId)) Lawyer.ChangeRoleByTarget(data.Character);
if (Lawyer.Target.ContainsValue(id))
Lawyer.ChangeRoleByTarget(data.Character);
if (Spiritualist.SpiritualistTarget == data.Character.PlayerId) Spiritualist.RemoveTarget();
if (Spiritualist.SpiritualistTarget == id)
Spiritualist.RemoveTarget();
if (CopyCat.PlayerIdList.Remove(id))
CopyCat.Instances.RemoveAll(x => x.CopyCatPC == null);
Postman.CheckAndResetTargets(data.Character);
GhostRolesManager.AssignedGhostRoles.Remove(data.Character.PlayerId);
GhostRolesManager.AssignedGhostRoles.Remove(id);
PlayerState state = Main.PlayerStates[data.Character.PlayerId];
PlayerState state = Main.PlayerStates[id];
if (state.deathReason == PlayerState.DeathReason.etc) state.deathReason = PlayerState.DeathReason.Disconnected;
if (!state.IsDead) state.SetDead();
Utils.AfterPlayerDeathTasks(data.Character, GameStates.IsMeeting, true);
NameNotifyManager.Notifies.Remove(data.Character.PlayerId);
NameNotifyManager.Notifies.Remove(id);
data.Character.RpcSetName(data.Character.GetRealName(true));
PlayerGameOptionsSender.RemoveSender(data.Character);
}
+1 -1
View File
@@ -316,7 +316,7 @@ public class Alchemist : RoleBase
public override string GetSuffix(PlayerControl seer, PlayerControl target, bool hud = false, bool meeting = false)
{
if (!hud || seer == null || seer.PlayerId != target.PlayerId || !GameStates.IsInTask || seer.PlayerId != AlchemistId) return string.Empty;
if (!hud || seer.PlayerId != target.PlayerId || !GameStates.IsInTask || seer.PlayerId != AlchemistId) return string.Empty;
var sb = new StringBuilder();
+4 -3
View File
@@ -8,6 +8,7 @@ public class CopyCat : RoleBase
{
private const int Id = 666420;
public static List<CopyCat> Instances = [];
public static List<byte> PlayerIdList = [];
private static bool Resetting;
private static OptionItem KillCooldown;
@@ -55,6 +56,7 @@ public class CopyCat : RoleBase
{
if (Resetting) return;
Instances = [];
PlayerIdList = [];
CurrentKillCooldown = AdjustedDefaultKillCooldown;
}
@@ -62,6 +64,7 @@ public class CopyCat : RoleBase
{
if (Resetting) return;
Instances.Add(this);
PlayerIdList.Add(playerId);
CopyCatPC = Utils.GetPlayerById(playerId);
CurrentKillCooldown = KillCooldown.GetFloat();
int limit = MiscopyLimitOpt.GetInt();
@@ -197,6 +200,4 @@ public class CopyCat : RoleBase
SetKillCooldown(pc.PlayerId);
return false;
}
}
}
+6 -1
View File
@@ -107,7 +107,12 @@ public class Decryptor : RoleBase
private void RevealLetter()
{
Dictionary<byte, char> nextLetters = AllRoleNames.ToDictionary(x => x.Key, x => x.Value.Except(KnownCharacters[x.Key]).RandomElement());
Dictionary<byte, char> nextLetters = AllRoleNames.ToDictionary(x => x.Key, x =>
{
List<char> list = x.Value.ToList();
KnownCharacters[x.Key].ForEach(kc => list.Remove(kc));
return list.RandomElement();
});
KnownCharacters.Do(x =>
{
+1 -1
View File
@@ -47,7 +47,7 @@ public class Marshall : RoleBase
public static string GetWarningMark(PlayerControl seer, PlayerControl target)
{
return CanSeeMarshall(seer) && PlayerIdList.Contains(target.PlayerId) && target.IsAlive() && target.GetTaskState().IsTaskFinished ? Utils.ColorString(Utils.GetRoleColor(CustomRoles.Marshall), "★") : string.Empty;
return PlayerIdList.Contains(target.PlayerId) && target.GetTaskState().IsTaskFinished && target.IsAlive() && CanSeeMarshall(seer) ? Utils.ColorString(Utils.GetRoleColor(CustomRoles.Marshall), "★") : string.Empty;
}
public static bool CanSeeMarshall(PlayerControl seer)
+2 -9
View File
@@ -19,7 +19,6 @@ public class Oxyman : RoleBase
private long LastUpdate;
private int OxygenLevel;
private byte OxymanId;
private float StartingSpeed;
public override bool IsEnable => On;
@@ -65,7 +64,6 @@ public class Oxyman : RoleBase
OxymanId = playerId;
OxygenLevel = 59;
LastUpdate = Utils.TimeStamp;
StartingSpeed = Main.AllPlayerSpeed[playerId];
}
public override void ApplyGameOptions(IGameOptions opt, byte playerId)
@@ -90,11 +88,10 @@ public class Oxyman : RoleBase
public override void OnFixedUpdate(PlayerControl pc)
{
if (!pc.IsAlive() || !GameStates.IsInTask || ExileController.Instance) return;
if (!pc.IsAlive() || !GameStates.IsInTask || ExileController.Instance || !Main.IntroDestroyed) return;
long now = Utils.TimeStamp;
if (now == LastUpdate) return;
LastUpdate = now;
if (OxygenLevel <= 0)
@@ -130,7 +127,7 @@ public class Oxyman : RoleBase
break;
case Level.None:
Main.PlayerStates[pc.PlayerId].IsBlackOut = false;
Main.AllPlayerSpeed[pc.PlayerId] = StartingSpeed;
Main.AllPlayerSpeed[pc.PlayerId] = Main.RealOptionsData.GetFloat(FloatOptionNames.PlayerSpeedMod);
pc.MarkDirtySettings();
break;
case Level.Invulnerable:
@@ -151,13 +148,9 @@ public class Oxyman : RoleBase
{
// ReSharper disable ConvertIfStatementToReturnStatement
if (OxygenLevel <= LevelSettings[Level.Blind].GetInt()) return Level.Blind;
if (OxygenLevel <= LevelSettings[Level.Slow].GetInt()) return Level.Slow;
if (OxygenLevel >= LevelSettings[Level.Invulnerable].GetInt()) return Level.Invulnerable;
if (OxygenLevel >= LevelSettings[Level.Fast].GetInt()) return Level.Fast;
return Level.None;
// ReSharper restore ConvertIfStatementToReturnStatement
}
+37 -53
View File
@@ -1,24 +1,24 @@
using System.Collections.Generic;
using System.Diagnostics;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
using UnityEngine;
namespace EHR.Impostor;
public class Mercenary : RoleBase
{
private const int Id = 1700;
public static List<byte> PlayerIdList = [];
public static bool On;
private static OptionItem KillCooldown;
private static OptionItem TimeLimit;
private static OptionItem WaitFor1Kill;
private float SuicideTimer;
private int Timer;
private Stopwatch Timer;
private long LastNotify;
private byte MercenaryId;
public override bool IsEnable => PlayerIdList.Count > 0;
public override bool IsEnable => On;
public override void SetupCustomOption()
{
@@ -38,22 +38,15 @@ public class Mercenary : RoleBase
public override void Init()
{
PlayerIdList = [];
SuicideTimer = 0f;
Timer = TimeLimit.GetInt();
On = false;
}
public override void Add(byte serial)
{
PlayerIdList.Add(serial);
Timer = TimeLimit.GetInt();
SuicideTimer = 0f;
On = true;
Timer = new();
MercenaryId = serial;
}
public override void Remove(byte playerId)
{
PlayerIdList.Remove(playerId);
if (!WaitFor1Kill.GetBool()) LateTask.New(() => Timer.Start(), 10f, log: false);
}
public override void SetKillCooldown(byte id)
@@ -61,77 +54,68 @@ public class Mercenary : RoleBase
Main.AllPlayerKillCooldown[id] = KillCooldown.GetFloat();
}
private static bool HasKilled(PlayerControl pc)
{
return pc != null && pc.Is(CustomRoles.Mercenary) && pc.IsAlive() && (Main.PlayerStates[pc.PlayerId].GetKillCount() > 0 || !WaitFor1Kill.GetBool());
}
public override bool OnCheckMurder(PlayerControl killer, PlayerControl target)
{
if (!killer.Is(CustomRoles.Mercenary)) return true;
SuicideTimer = 0f;
Timer = TimeLimit.GetInt();
killer.MarkDirtySettings();
Timer.Restart();
Utils.SendRPC(CustomRPC.SyncRoleData, MercenaryId, 1);
return true;
}
public override void OnReportDeadBody()
{
SuicideTimer = float.NaN;
Timer = TimeLimit.GetInt();
Timer.Reset();
Utils.SendRPC(CustomRPC.SyncRoleData, MercenaryId, 2);
}
public override void OnFixedUpdate(PlayerControl player)
{
if (!GameStates.IsInTask || !Main.IntroDestroyed || ExileController.Instance || AntiBlackout.SkipTasks) return;
if (!HasKilled(player))
{
SuicideTimer = float.NaN;
Timer = TimeLimit.GetInt();
return;
}
if (!Timer.IsRunning || (MeetingStates.FirstMeeting && WaitFor1Kill.GetBool())) return;
if (float.IsNaN(SuicideTimer)) return;
long remainingTime = Timer.GetRemainingTime(TimeLimit.GetInt());
if (SuicideTimer >= TimeLimit.GetFloat())
if (remainingTime <= 0)
{
player.Suicide();
SuicideTimer = float.NaN;
Timer = TimeLimit.GetInt();
Timer.Reset();
if (player.AmOwner)
Achievements.Type.OutOfTime.Complete();
}
else
{
SuicideTimer += Time.fixedDeltaTime;
int tempTimer = Timer;
Timer = TimeLimit.GetInt() - (int)SuicideTimer;
if (Timer != tempTimer && Timer <= 20 && !player.IsModdedClient())
{
Utils.NotifyRoles(SpecifySeer: player, SpecifyTarget: player);
Utils.SendRPC(CustomRPC.SyncRoleData, MercenaryId, Timer);
}
long now = Utils.TimeStamp;
if (now == LastNotify || remainingTime > 20) return;
LastNotify = now;
Utils.NotifyRoles(SpecifySeer: player, SpecifyTarget: player);
Utils.SendRPC(CustomRPC.SyncRoleData, MercenaryId, Timer);
}
}
public override void AfterMeetingTasks()
{
SuicideTimer = 0f;
Timer = TimeLimit.GetInt();
Timer.Restart();
Utils.SendRPC(CustomRPC.SyncRoleData, MercenaryId, 1);
}
public void ReceiveRPC(MessageReader reader)
{
Timer = reader.ReadPackedInt32();
switch (reader.ReadPackedInt32())
{
case 1:
Timer = Stopwatch.StartNew();
break;
case 2:
Timer.Reset();
break;
}
}
public override string GetSuffix(PlayerControl seer, PlayerControl target, bool hud = false, bool meeting = false)
{
if (seer.PlayerId != MercenaryId || seer.PlayerId != target.PlayerId || (seer.IsModdedClient() && !hud) || meeting || Timer - 1 > 20) return string.Empty;
return string.Format(Translator.GetString("SerialKillerTimeLeft"), Timer - 1);
if (seer.PlayerId != MercenaryId || seer.PlayerId != target.PlayerId || (seer.IsModdedClient() && !hud) || meeting) return string.Empty;
long remainingTime = Timer.GetRemainingTime(TimeLimit.GetInt());
return !Timer.IsRunning || remainingTime > 20 ? string.Empty : string.Format(Translator.GetString("SerialKillerTimeLeft"), remainingTime - 1);
}
}
+3 -4
View File
@@ -1,6 +1,7 @@
using System.Diagnostics;
using AmongUs.GameOptions;
using EHR.Modules;
using EHR.Modules.Extensions;
using Hazel;
namespace EHR.Neutral;
@@ -94,7 +95,7 @@ public class Duality : RoleBase
if (LastUpdateTS == now) return;
LastUpdateTS = now;
if (GetRemainingTime() <= 0)
if (Timer.GetRemainingTime(Time.GetInt()) <= 0)
{
pc.Suicide();
if (pc.AmOwner) Achievements.Type.OutOfTime.Complete();
@@ -120,8 +121,6 @@ public class Duality : RoleBase
Utils.SendRPC(CustomRPC.SyncRoleData, DualityId, KillingPhase);
}
long GetRemainingTime() => Time.GetInt() - Timer.Elapsed.Seconds;
public void ReceiveRPC(MessageReader reader)
{
KillingPhase = reader.ReadBoolean();
@@ -131,6 +130,6 @@ public class Duality : RoleBase
public override string GetSuffix(PlayerControl seer, PlayerControl target, bool hud = false, bool meeting = false)
{
if (seer.PlayerId != DualityId || seer.PlayerId != target.PlayerId || (!hud && seer.IsModdedClient()) || meeting || !seer.IsAlive()) return string.Empty;
return $"<size=80%>{string.Format(Translator.GetString(KillingPhase ? "Duality.MustKill" : "Duality.MustDoTask"), GetRemainingTime() - 1)}</size>";
return $"<size=80%>{string.Format(Translator.GetString(KillingPhase ? "Duality.MustKill" : "Duality.MustDoTask"), Timer.GetRemainingTime(Time.GetInt()) - 1)}</size>";
}
}