From f207849e5efbcc654c7cf189f92401a08f647645 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Mon, 2 Sep 2024 00:12:50 -0400 Subject: [PATCH] fix neutral killing issues and rename some properties in ICustomRole to clear confusion --- MiraAPI.Example/Roles/NeutralKillerRole.cs | 27 +++++++++ MiraAPI/Networking/CustomMurderRpc.cs | 20 +++---- MiraAPI/Patches/HudManagerPatches.cs | 2 - MiraAPI/Patches/Roles/KillButtonPatches.cs | 65 ++++++++++++++++++++++ MiraAPI/Roles/CustomRoleManager.cs | 2 +- MiraAPI/Roles/ICustomRole.cs | 8 +-- 6 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 MiraAPI.Example/Roles/NeutralKillerRole.cs create mode 100644 MiraAPI/Patches/Roles/KillButtonPatches.cs diff --git a/MiraAPI.Example/Roles/NeutralKillerRole.cs b/MiraAPI.Example/Roles/NeutralKillerRole.cs new file mode 100644 index 0000000..0cbc6fd --- /dev/null +++ b/MiraAPI.Example/Roles/NeutralKillerRole.cs @@ -0,0 +1,27 @@ +using MiraAPI.Roles; +using UnityEngine; + +namespace MiraAPI.Example.Roles; + +[RegisterCustomRole] +public class NeutralKillerRole : ImpostorRole, ICustomRole +{ + public string RoleName => "Neutral Killer"; + public string RoleDescription => "Neutral who can kill."; + public string RoleLongDescription => RoleDescription; + public Color RoleColor => Color.magenta; + public ModdedRoleTeams Team => ModdedRoleTeams.Neutral; + public bool CanUseKill => true; + public bool CanGetKilled => true; + public bool CanUseVent => true; + + public override void SpawnTaskHeader(PlayerControl playerControl) + { + // remove existing task header. + } + + public override bool DidWin(GameOverReason gameOverReason) + { + return GameManager.Instance.DidHumansWin(gameOverReason); + } +} diff --git a/MiraAPI/Networking/CustomMurderRpc.cs b/MiraAPI/Networking/CustomMurderRpc.cs index b36810a..619a0ab 100644 --- a/MiraAPI/Networking/CustomMurderRpc.cs +++ b/MiraAPI/Networking/CustomMurderRpc.cs @@ -1,10 +1,11 @@ -using AmongUs.GameOptions; +using System.Collections; +using System.Linq; +using AmongUs.GameOptions; using Assets.CoreScripts; +using BepInEx.Unity.IL2CPP.Utils; using Reactor.Networking.Attributes; using Reactor.Utilities; using Reactor.Utilities.Extensions; -using System.Collections; -using System.Linq; using UnityEngine; namespace MiraAPI.Networking; @@ -71,7 +72,7 @@ public static class CustomMurderRpc bool playKillSound = true) { source.isKilling = false; - Logger.Debug($"{source.PlayerId} trying to murder {target.PlayerId}"); + Logger.Error($"{source.PlayerId} trying to murder {target.PlayerId}"); var data = target.Data; if (resultFlags.HasFlag(MurderResultFlags.FailedError)) { @@ -104,7 +105,7 @@ public static class CustomMurderRpc target.RemoveProtection(); } - Logger.Debug($"{source.PlayerId} failed to murder {target.PlayerId} due to guardian angel protection"); + Logger.Error($"{source.PlayerId} failed to murder {target.PlayerId} due to guardian angel protection"); return; } @@ -134,8 +135,7 @@ public static class CustomMurderRpc if (resetKillTimer) { - source.SetKillTimer( - GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown)); + source.SetKillTimer(GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown)); } } @@ -172,8 +172,8 @@ public static class CustomMurderRpc source.CurrentOutfitType == PlayerOutfitType.Shapeshifted, source.shapeshiftTargetPlayerId, target.PlayerId); - Coroutines.Start(source.KillAnimations.Random()?.CoPerformCustomKill(source, target, createDeadBody, teleportMurderer)); - Logger.Debug($"{source.PlayerId} succeeded in murdering {target.PlayerId}"); + source.MyPhysics.StartCoroutine(source.KillAnimations.Random()?.CoPerformCustomKill(source, target, createDeadBody, teleportMurderer)); + Logger.Error($"{source.PlayerId} succeeded in murdering {target.PlayerId}"); } /// @@ -262,7 +262,7 @@ public static class CustomMurderRpc if (cam != null) { - cam.Locked = true; + cam.Locked = false; } PlayerControl.LocalPlayer.isKilling = false; diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index 45bd114..cdef7cc 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -1,7 +1,5 @@ using HarmonyLib; -using InnerNet; using MiraAPI.Hud; -using MiraAPI.Roles; using Reactor.Utilities.Extensions; using UnityEngine; using Object = UnityEngine.Object; diff --git a/MiraAPI/Patches/Roles/KillButtonPatches.cs b/MiraAPI/Patches/Roles/KillButtonPatches.cs new file mode 100644 index 0000000..52b5cba --- /dev/null +++ b/MiraAPI/Patches/Roles/KillButtonPatches.cs @@ -0,0 +1,65 @@ +using HarmonyLib; +using Il2CppSystem; +using MiraAPI.Networking; +using MiraAPI.Roles; +using UnityEngine; + +namespace MiraAPI.Patches.Roles; + +/// +/// Fix kill button issues for neutral killers. +/// +[HarmonyPatch(typeof(KillButton))] +public static class KillButtonPatches +{ + [HarmonyPrefix] + [HarmonyPatch(nameof(KillButton.SetTarget))] + public static bool SetTargetPrefix(KillButton __instance, PlayerControl target) + { + if (!PlayerControl.LocalPlayer || PlayerControl.LocalPlayer.Data == null || !PlayerControl.LocalPlayer.Data.Role) + { + return false; + } + + if (PlayerControl.LocalPlayer.Data.Role is not ICustomRole customRole) + { + return true; + } + + if (__instance.currentTarget && __instance.currentTarget != target) + { + __instance.currentTarget.cosmetics.SetOutline(false, new Nullable(Color.clear)); + } + __instance.currentTarget = target; + if (__instance.currentTarget) + { + __instance.currentTarget.cosmetics.SetOutline(true, new Nullable(customRole.RoleColor)); + __instance.SetEnabled(); + return false; + } + __instance.SetDisabled(); + + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(KillButton.DoClick))] + public static bool DoClickPrefix(KillButton __instance) + { + if (PlayerControl.LocalPlayer.Data.Role is not ICustomRole) + { + return true; + } + + if (!__instance.isActiveAndEnabled || !__instance.currentTarget || __instance.isCoolingDown || + PlayerControl.LocalPlayer.Data.IsDead || !PlayerControl.LocalPlayer.CanMove) + { + return false; + } + + PlayerControl.LocalPlayer.RpcCustomMurder(__instance.currentTarget); + __instance.SetTarget(null); + + return false; + } +} diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index a3d36ec..7fad146 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -84,7 +84,7 @@ public static class CustomRoleManager roleBehaviour.StringName = CustomStringName.CreateAndRegister(customRole.RoleName); roleBehaviour.BlurbName = CustomStringName.CreateAndRegister(customRole.RoleDescription); roleBehaviour.BlurbNameLong = CustomStringName.CreateAndRegister(customRole.RoleLongDescription); - roleBehaviour.AffectedByLightAffectors = customRole.AffectedByLight; + roleBehaviour.AffectedByLightAffectors = customRole.AffectedByLightOnAirship; roleBehaviour.CanBeKilled = customRole.CanGetKilled; roleBehaviour.CanUseKillButton = customRole.CanUseKill; roleBehaviour.TasksCountTowardProgress = customRole.TasksCountForProgress; diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index 08875f9..0ac41bb 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -68,9 +68,9 @@ public interface ICustomRole ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); /// - /// Gets a value indicating whether the role is affected by light sabotages. + /// Gets a value indicating whether the role is affected by light affectors on Airship. /// - bool AffectedByLight => Team == ModdedRoleTeams.Crewmate; + bool AffectedByLightOnAirship => Team == ModdedRoleTeams.Crewmate; /// /// Gets a value indicating whether the role can be killed by others. @@ -80,7 +80,7 @@ public interface ICustomRole /// /// Gets a value indicating whether the role can kill others. /// - bool CanKill => Team == ModdedRoleTeams.Impostor; + bool CanUseKill => Team == ModdedRoleTeams.Impostor; /// /// Gets a value indicating whether the role can use vents. @@ -90,7 +90,7 @@ public interface ICustomRole /// /// Gets a value indicating whether the role's tasks count towards task progress. /// - bool TasksCount => Team == ModdedRoleTeams.Crewmate; + bool TasksCountForProgress => Team == ModdedRoleTeams.Crewmate; /// /// Gets a value indicating whether the role is a Ghost.