optimize code

This commit is contained in:
Gurge44 committed 2026-05-04 13:07:43 +02:00
1 parent c6d521b9f2
commit 1c5e6eb76f
10 files changed
+56 -67

No files matched your search

+6 -16
View File
@@ -1831,21 +1831,11 @@ internal static class CustomRolesHelper
};
}
public static bool CheckAddonConflict(CustomRoles role, PlayerControl pc)
{
return CheckAddonConflict(role, pc, validateExistingState: false);
}
public static bool IsAddonCompatibleWithCurrentState(CustomRoles role, PlayerControl pc)
{
return CheckAddonConflict(role, pc, validateExistingState: true);
}
private static bool CheckAddonConflict(CustomRoles role, PlayerControl pc, bool validateExistingState)
{
return role.IsAdditionRole() && !(role.IsGhostRole() && pc.IsAlive()) && (!Main.NeverSpawnTogetherCombos.TryGetValue(OptionItem.CurrentPreset, out Dictionary<CustomRoles, List<CustomRoles>> neverList) || !neverList.TryGetValue(pc.GetCustomRole(), out List<CustomRoles> bannedAddonList) || !bannedAddonList.Contains(role)) && pc.GetCustomRole() is not CustomRoles.GuardianAngelEHR and not CustomRoles.God && !pc.Is(CustomRoles.Madmate) && !pc.Is(CustomRoles.GM) && (validateExistingState || role is not CustomRoles.Lovers) && !pc.Is(CustomRoles.LazyGuy) && (validateExistingState || (Main.IntroDestroyed && Options.AddonAssigningRolesIgnoreMaxAddonsLimit.GetBool()) || !pc.HasSubRole() || pc.GetCustomSubRoles().Count < Options.NoLimitAddonsNumMax.GetInt()) && (!Options.AddonCanBeSettings.TryGetValue(role, out (OptionItem Imp, OptionItem Neutral, OptionItem Crew, OptionItem Coven) o) || ((o.Imp.GetBool() || !pc.GetCustomRole().IsImpostor()) && (o.Neutral.GetBool() || !pc.GetCustomRole().IsNeutral()) && (o.Crew.GetBool() || !pc.IsCrewmate()) && (o.Coven.GetBool() || !pc.Is(Team.Coven)))) && (!role.IsImpOnlyAddon() || (pc.IsImpostor() && !pc.Is(CustomRoles.DoubleAgent)) || (pc.Is(CustomRoles.Traitor) && Traitor.CanGetImpostorOnlyAddons.GetBool())) && role switch
{
CustomRoles.Blind when pc.Is(CustomRoles.Sensor) => false,
public static bool CheckAddonConflict(CustomRoles role, PlayerControl pc, bool validateExistingState = false)
{
return role.IsAdditionRole() && !(role.IsGhostRole() && pc.IsAlive()) && (!Main.NeverSpawnTogetherCombos.TryGetValue(OptionItem.CurrentPreset, out Dictionary<CustomRoles, List<CustomRoles>> neverList) || !neverList.TryGetValue(pc.GetCustomRole(), out List<CustomRoles> bannedAddonList) || !bannedAddonList.Contains(role)) && pc.GetCustomRole() is not CustomRoles.GuardianAngelEHR and not CustomRoles.God && !pc.Is(CustomRoles.Madmate) && !pc.Is(CustomRoles.GM) && (validateExistingState || role is not CustomRoles.Lovers) && !pc.Is(CustomRoles.LazyGuy) && (validateExistingState || (Main.IntroDestroyed && Options.AddonAssigningRolesIgnoreMaxAddonsLimit.GetBool()) || !pc.HasSubRole() || pc.GetCustomSubRoles().Count < Options.NoLimitAddonsNumMax.GetInt()) && (!Options.AddonCanBeSettings.TryGetValue(role, out (OptionItem Imp, OptionItem Neutral, OptionItem Crew, OptionItem Coven) o) || ((o.Imp.GetBool() || !pc.GetCustomRole().IsImpostor()) && (o.Neutral.GetBool() || !pc.GetCustomRole().IsNeutral()) && (o.Crew.GetBool() || !pc.IsCrewmate()) && (o.Coven.GetBool() || !pc.Is(Team.Coven)))) && (!role.IsImpOnlyAddon() || (pc.IsImpostor() && !pc.Is(CustomRoles.DoubleAgent)) || (pc.Is(CustomRoles.Traitor) && Traitor.CanGetImpostorOnlyAddons.GetBool())) && role switch
{
CustomRoles.Blind when pc.Is(CustomRoles.Sensor) => false,
CustomRoles.Composter when float.IsNaN(pc.GetAbilityUseLimit()) => false,
CustomRoles.TaskMaster when !pc.IsCrewmate() || !Utils.HasTasks(pc.Data, forRecompute: false) => false,
CustomRoles.Commited when pc.GetRoleTypes() is not (RoleTypes.Impostor or RoleTypes.Phantom or RoleTypes.Shapeshifter or RoleTypes.Viper) => false,
@@ -1868,7 +1858,7 @@ internal static class CustomRolesHelper
CustomRoles.Swift when pc.Is(CustomRoles.Magnet) => false,
CustomRoles.Oblivious when pc.Is(CustomRoles.Amnesiac) && Amnesiac.RememberMode.GetValue() == 0 => false,
CustomRoles.Rookie when pc.GetRoleTypes() is not (RoleTypes.Impostor or RoleTypes.Phantom or RoleTypes.Shapeshifter or RoleTypes.Viper) => false,
CustomRoles.Madmate when pc.Is(CustomRoles.Sidekick) => false,
CustomRoles.Madmate when pc.Is(CustomRoles.Sidekick) => false,
CustomRoles.Autopsy when pc.Is(CustomRoles.Doctor) || pc.Is(CustomRoles.Tracefinder) || pc.Is(CustomRoles.Scientist) || pc.Is(CustomRoles.ScientistEHR) || pc.Is(CustomRoles.Sunnyboy) => false,
CustomRoles.Necroview when pc.Is(CustomRoles.Doctor) => false,
CustomRoles.Lazy when pc.Is(CustomRoles.Speedrunner) => false,
+1 -1
View File
@@ -1500,7 +1500,7 @@ internal static class RPC
public static void ForceEndGame(CustomWinner win)
{
if (ShipStatus.Instance == null) return;
if (!ShipStatus.Instance) return;
try { CustomWinnerHolder.ResetAndSetWinner(win); }
catch { }
+2
View File
@@ -3783,6 +3783,8 @@ public static class Utils
CopyCat.ResetRoles();
Imitator.SetRoles();
Reroll.ResolveAfterMeeting(ExileControllerWrapUpPatch.LastExiled);
}
catch (Exception e) { ThrowException(e); }
+3
View File
@@ -551,7 +551,10 @@ internal static class ChatCommands
private static void RerollCommand(PlayerControl player, string text, string[] args)
{
if (Starspawn.IsDayBreak) return;
if (!Reroll.TryQueueCommandTrigger(player)) return;
MeetingManager.SendCommandUsedMessage(args[0]);
}
+8 -9
View File
@@ -192,14 +192,13 @@ internal static class ExileControllerWrapUpPatch
player.SetRealKiller(player, true);
Utils.AfterPlayerDeathTasks(player);
}
Main.AfterMeetingDeathPlayers.Clear();
Reroll.ResolveAfterMeeting(LastExiled);
Utils.AfterMeetingTasks();
Utils.MarkEveryoneDirtySettings();
Utils.CheckAndSetVentInteractions();
}
Main.AfterMeetingDeathPlayers.Clear();
Utils.AfterMeetingTasks();
Utils.MarkEveryoneDirtySettings();
Utils.CheckAndSetVentInteractions();
Main.Instance.StartCoroutine(Utils.NotifyEveryoneAsync());
@@ -238,4 +237,4 @@ internal static class PolusExileHatFixPatch
{
__instance.Player.cosmetics.hat.transform.localPosition = new(-0.2f, 0.6f, 1.1f);
}
}
}
+15 -15
View File
@@ -21,10 +21,10 @@ public static class ReactorSystemTypePatch
if (Main.CurrentMap is MapNames.Airship) return;
// When the sabotage ends
if (!__instance.IsActive || ShipStatus.Instance == null || !SetDurationForReactorSabotage)
{
if (!SetDurationForReactorSabotage && !__instance.IsActive)
SetDurationForReactorSabotage = true;
if (!__instance.IsActive || !ShipStatus.Instance || !SetDurationForReactorSabotage)
{
if (!SetDurationForReactorSabotage && !__instance.IsActive)
SetDurationForReactorSabotage = true;
return;
}
@@ -65,7 +65,7 @@ public static class HeliSabotageSystemPatch
if (Main.CurrentMap is not MapNames.Airship) return;
// When the sabotage ends
if (!__instance.IsActive || ShipStatus.Instance == null || !SetDurationForReactorSabotage)
if (!__instance.IsActive || !ShipStatus.Instance || !SetDurationForReactorSabotage)
{
if (!SetDurationForReactorSabotage && !__instance.IsActive)
SetDurationForReactorSabotage = true;
@@ -93,10 +93,10 @@ public static class LifeSuppSystemTypePatch
if (Main.CurrentMap is MapNames.Polus or MapNames.Airship or MapNames.Fungle) return;
// When the sabotage ends
if (!__instance.IsActive || ShipStatus.Instance == null || !SetDurationForO2Sabotage)
{
if (!SetDurationForO2Sabotage && !__instance.IsActive)
SetDurationForO2Sabotage = true;
if (!__instance.IsActive || !ShipStatus.Instance || !SetDurationForO2Sabotage)
{
if (!SetDurationForO2Sabotage && !__instance.IsActive)
SetDurationForO2Sabotage = true;
return;
}
@@ -155,10 +155,10 @@ public static class MushroomMixupSabotageSystemPatch
if (Main.CurrentMap is not MapNames.Fungle) return;
// When the sabotage ends
if (!__instance.IsActive || ShipStatus.Instance == null || !SetDurationMushroomMixupSabotage)
{
if (!SetDurationMushroomMixupSabotage && !__instance.IsActive)
SetDurationMushroomMixupSabotage = true;
if (!__instance.IsActive || !ShipStatus.Instance || !SetDurationMushroomMixupSabotage)
{
if (!SetDurationMushroomMixupSabotage && !__instance.IsActive)
SetDurationMushroomMixupSabotage = true;
return;
}
@@ -449,7 +449,7 @@ public static class SabotageSystemTypeRepairDamagePatch
if (Pelican.IsEaten(player.PlayerId)) return false;
if (systemTypes == SystemTypes.Electrical && Main.PlayerStates.Values.FindFirst(x => !x.IsDead && x.MainRole == CustomRoles.Battery && x.Player != null && x.Player.GetAbilityUseLimit() >= 1f, out var batteryState))
if (systemTypes == SystemTypes.Electrical && Main.PlayerStates.Values.FindFirst(x => !x.IsDead && x.MainRole == CustomRoles.Battery && x.Player && x.Player.GetAbilityUseLimit() >= 1f, out var batteryState))
{
batteryState.Player.RpcRemoveAbilityUse();
player.Notify(string.Format(Translator.GetString("BatteryNotify"), CustomRoles.Battery.ToColoredString()), 10f);
@@ -570,4 +570,4 @@ internal static class ShipStatusRepairCriticalSabotagesPatch
{
CustomSabotage.Reset();
}
}
}
+2 -2
View File
@@ -3860,7 +3860,7 @@
"ReachInfo": "You have a longer kill range",
"ReachInfoLong": "(Add-on)\\nOnly roles with a kill button can get this add-on. You have the longest kill range possible in the game, unlike everyone else.",
"Reroll": "Reroll",
"RerollInfo": "Use /reroll during a meeting to reroll after it ends",
"RerollInfo": "You can reroll your role via /reroll",
"RerollInfoLong": "(Add-ons):\\nIf you use /reroll during a meeting, you reroll your role after the meeting ends. If you survive the meeting, you become a new role from the configured pool. If you are exiled, no reroll happens.",
"Reroll.SameFactionOnly": "Can only become a role from the same faction",
"Reroll.AnnounceReroll": "Notify everyone when a reroll happens",
@@ -9693,7 +9693,7 @@
"CommandDescription.Ready": "Mark yourself as ready",
"CommandDescription.ReadyCheck": "Start a ready check (check who is AFK) (results displayed after 30s)",
"CommandDescription.Rename": "Gives you a new name",
"CommandDescription.Reroll": "Queue your Reroll add-on for after the current meeting.",
"CommandDescription.Reroll": "Reroll your role as the Reroll. Takes effect after the meeting ends.",
"CommandDescription.Retribute": "Guess who killed the player you camped as the Retributionist",
"CommandDescription.Revenge": "Take revenge on (kill) a player as the Nemesis",
"CommandDescription.Revive": "Revives a dead player. Requires special access or No Game End enabled.",
+17 -22
View File
@@ -1,8 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using AmongUs.Data;
using AmongUs.GameOptions;
using EHR.Modules;
namespace EHR.Roles;
@@ -64,7 +60,7 @@ public class Reroll : IAddon
public static bool TryQueueCommandTrigger(PlayerControl player)
{
if (!AmongUsClient.Instance.AmHost || player == null) return false;
if (!AmongUsClient.Instance.AmHost || !player) return false;
if (!player.Is(CustomRoles.Reroll) || !player.IsAlive() || Silencer.ForSilencer.Contains(player.PlayerId))
{
@@ -96,30 +92,29 @@ public class Reroll : IAddon
public static void ResolveAfterMeeting(NetworkedPlayerInfo lastExiled)
{
foreach ((byte playerId, PendingReroll pending) in Pending.ToArray())
foreach ((byte playerId, PendingReroll pending) in Pending)
{
PlayerControl player = Utils.GetPlayerById(playerId);
if (!player) continue;
bool changed = false;
if (!player)
{
Pending.Remove(playerId);
continue;
}
bool wasExiled = lastExiled != null && lastExiled.PlayerId == playerId;
bool wasExiled = lastExiled && lastExiled.PlayerId == playerId;
if (!wasExiled && player.IsAlive())
changed = TryResolveAliveRole(player, pending.TeamAtTrigger);
if (changed && ConsumeOnSuccess.GetBool())
Main.PlayerStates[playerId].RemoveSubRole(CustomRoles.Reroll);
if (!changed)
player.Notify(Translator.GetString("Reroll.Failed"));
Pending.Remove(playerId);
switch (changed)
{
case true when ConsumeOnSuccess.GetBool():
Main.PlayerStates[playerId].RemoveSubRole(CustomRoles.Reroll);
break;
case false:
player.Notify(Translator.GetString("Reroll.Failed"));
break;
}
}
Pending.Clear();
}
private static bool TryResolveAliveRole(PlayerControl player, Team teamAtTrigger)
@@ -183,7 +178,7 @@ public class Reroll : IAddon
if (index == -1) continue;
state.SubRoles.RemoveAt(index);
bool isCompatible = CustomRolesHelper.IsAddonCompatibleWithCurrentState(subRole, player);
bool isCompatible = CustomRolesHelper.CheckAddonConflict(subRole, player, true);
state.SubRoles.Insert(index, subRole);
if (!isCompatible)
+1 -1
View File
@@ -32,7 +32,7 @@ public class Tank : RoleBase
public override void Init()
{
On = false;
if (ShipStatus.Instance == null) return;
if (!ShipStatus.Instance) return;
AllVents = ShipStatus.Instance.AllVents.Select(x => x.Id).ToHashSet();
}
+1 -1
View File
@@ -41,7 +41,7 @@ public static class SubmergedCompatibility
if (_submarineStatus is null || _submarineStatus.WasCollected || !_submarineStatus || _submarineStatus == null)
{
if (ShipStatus.Instance is null || ShipStatus.Instance == null || !ShipStatus.Instance || ShipStatus.Instance.WasCollected)
if (ShipStatus.Instance is null || !ShipStatus.Instance || !ShipStatus.Instance || ShipStatus.Instance.WasCollected)
return _submarineStatus = null;
if (ShipStatus.Instance.Type == SUBMERGED_MAP_TYPE)