diff --git a/mod.json b/mod.json index 151ccb0..b991f98 100644 --- a/mod.json +++ b/mod.json @@ -12,7 +12,7 @@ "Settings" : { "LogLevel": "Debug", - "DebugInfo" : "ClearInventory, DefaultHandle, ComponentInstall", + "DebugInfo" : "ClearInventory, DefaultHandle, ComponentInstall, IsDestroyed, ShowConfig", "Categories": [ ], "TagRestrictions": [ ], diff --git a/source/Components/Flags.cs b/source/Components/Flags.cs index a40f2d3..8ff5e78 100644 --- a/source/Components/Flags.cs +++ b/source/Components/Flags.cs @@ -7,7 +7,7 @@ namespace CustomComponents { [CustomComponent("Flags")] - public class Flags : SimpleCustomComponent, IAfterLoad, IMechLabFilter, IMechValidate + public class Flags : SimpleCustomComponent, IAfterLoad, IMechLabFilter, IMechValidate, ICheckIsDead { public List flags; @@ -34,7 +34,8 @@ public class Flags : SimpleCustomComponent, IAfterLoad, IMechLabFilter, IMechVal public bool Invalid { get; private set; } [JsonIgnore] public bool DontShowMessage { get; private set; } - + [JsonIgnore] + public bool IsVital { get; private set; } public string ErrorCannotRemove { get; set; } @@ -63,6 +64,7 @@ public virtual void OnLoaded(Dictionary values) NotDestroyed = false; Invalid = false; DontShowMessage = false; + IsVital = false; if (flags == null) { @@ -111,6 +113,9 @@ public virtual void OnLoaded(Dictionary values) case "hide_remove_message": DontShowMessage = true; break; + case "vital": + IsVital = true; + break; } } @@ -156,5 +161,19 @@ public override string ToString() return flags.Aggregate("Flags: [", (current, flag) => current + flag + " ") + "]"; } + public bool IsMechDestroyed(MechComponentRef item, Mech mech) + { + return IsVital && item.DamageLevel == ComponentDamageLevel.Destroyed; + } + + public bool IsVechicleDestroyed(VehicleComponentRef item, Vehicle mech) + { + return IsVital && item.DamageLevel == ComponentDamageLevel.Destroyed; + } + + public bool IsTurretDestroyed(TurretComponentRef item, Turret mech) + { + return IsVital && item.DamageLevel == ComponentDamageLevel.Destroyed; + } } } diff --git a/source/Control.cs b/source/Control.cs index a3c9883..033ae04 100644 --- a/source/Control.cs +++ b/source/Control.cs @@ -55,7 +55,7 @@ public static void Init(string directory, string settingsJSON) Registry.RegisterSimpleCustomComponents(Assembly.GetExecutingAssembly()); Validator.RegisterMechValidator(CategoryController.Shared.ValidateMech, CategoryController.Shared.ValidateMechCanBeFielded); - Logger.Log("Loaded CustomComponents v0.9.4 for bt 1.5.1"); + Logger.Log("Loaded CustomComponents v0.9.5 for bt 1.5.1"); Validator.RegisterMechValidator(TagRestrictionsHandler.Shared.ValidateMech, TagRestrictionsHandler.Shared.ValidateMechCanBeFielded); Validator.RegisterDropValidator(check: TagRestrictionsHandler.Shared.ValidateDrop); diff --git a/source/CustomComponentSettings.cs b/source/CustomComponentSettings.cs index 198aaae..a0079f0 100644 --- a/source/CustomComponentSettings.cs +++ b/source/CustomComponentSettings.cs @@ -95,6 +95,7 @@ public enum DType DefaultHandle = 1 << 9, ClearInventory = 1 << 10, CustomResource = 1 << 11, + IsDestroyed = 1 << 12, } @@ -129,6 +130,7 @@ public class CustomComponentSettings public bool FixSaveGameMech = false; public bool TagRestrictionDropValidateRequiredTags = false; public bool TagRestrictionDropValidateIncompatibleTags = true; + public bool CheckCriticalComponent = true; public bool UseDefaultFixedColor = false; public UIColor DefaultFlagBackgroundColor = UIColor.UpgradeColor; diff --git a/source/CustomComponents.csproj b/source/CustomComponents.csproj index 8c698db..74970c7 100644 --- a/source/CustomComponents.csproj +++ b/source/CustomComponents.csproj @@ -106,6 +106,10 @@ + + + + diff --git a/source/Interfaces/ICheckIsDead.cs b/source/Interfaces/ICheckIsDead.cs new file mode 100644 index 0000000..eb2e3dc --- /dev/null +++ b/source/Interfaces/ICheckIsDead.cs @@ -0,0 +1,11 @@ +using BattleTech; + +namespace CustomComponents +{ + public interface ICheckIsDead + { + bool IsMechDestroyed(MechComponentRef item, Mech mech); + bool IsVechicleDestroyed(VehicleComponentRef item, Vehicle mech); + bool IsTurretDestroyed(TurretComponentRef item, Turret mech); + } +} \ No newline at end of file diff --git a/source/Patches/Mech_IsDead.cs b/source/Patches/Mech_IsDead.cs new file mode 100644 index 0000000..fe92631 --- /dev/null +++ b/source/Patches/Mech_IsDead.cs @@ -0,0 +1,54 @@ +using System; +using BattleTech; +using Harmony; + +namespace CustomComponents.Patches +{ + [HarmonyPatch(typeof(Mech), "IsDead")] + [HarmonyPatch(MethodType.Getter)] + public static class Mech_IsDead + { + [HarmonyPostfix] + public static void IsDestroyedChecks(Mech __instance, ref bool __result) + { + if (__instance == null) return; + + try + { + Control.LogDebug(DType.IsDestroyed, $"Check if dead for mech {__instance.MechDef.Name}{__instance.MechDef.Chassis.Description.Id}"); + + + if (__result) + { + Control.LogDebug(DType.IsDestroyed, $"- Vanila destroyed"); + return ; + } + + foreach (var item in __instance.MechDef.Inventory) + { + if (Control.Settings.CheckCriticalComponent && item.Def.CriticalComponent && + item.DamageLevel == ComponentDamageLevel.Destroyed) + { + Control.LogDebug(DType.IsDestroyed, $"- Destroyed by CriticalComponent {item.ComponentDefID}"); + __result = true; + return; + } + + if (item.Is(out var d) && d.IsMechDestroyed(item, __instance)) + { + __result = true; + Control.LogDebug(DType.IsDestroyed, + $"- Destroyed by CheckDestroyed {item.ComponentDefID} of {d.GetType()}"); + return; + } + } + + Control.LogDebug(DType.IsDestroyed, $"- not destroyed"); + } + catch (Exception e) + { + Control.LogError(e); + } + } + } +} \ No newline at end of file diff --git a/source/Patches/Turret_IsDead.cs b/source/Patches/Turret_IsDead.cs new file mode 100644 index 0000000..0d213bc --- /dev/null +++ b/source/Patches/Turret_IsDead.cs @@ -0,0 +1,54 @@ +using System; +using BattleTech; +using Harmony; + +namespace CustomComponents.Patches +{ + [HarmonyPatch(typeof(Turret), "IsDead")] + [HarmonyPatch(MethodType.Getter)] + public static class Turret_IsDead + { + [HarmonyPostfix] + public static void IsDestroyedChecks(Turret __instance, ref bool __result) + { + if (__instance == null) return; + + try + { + Control.LogDebug(DType.IsDestroyed, $"Check if dead for turret {__instance.TurretDef.Description.Id}"); + + + if (__result) + { + Control.LogDebug(DType.IsDestroyed, $"- Vanila destroyed"); + return ; + } + + foreach (var item in __instance.TurretDef.Inventory) + { + if (Control.Settings.CheckCriticalComponent && item.Def.CriticalComponent && + item.DamageLevel == ComponentDamageLevel.Destroyed) + { + Control.LogDebug(DType.IsDestroyed, $"- Destroyed by CriticalComponent {item.ComponentDefID}"); + __result = true; + return; + } + + if (item.Def.Is(out var d) && d.IsTurretDestroyed(item, __instance)) + { + __result = true; + Control.LogDebug(DType.IsDestroyed, + $"- Destroyed by CheckDestroyed {item.ComponentDefID} of {d.GetType()}"); + return; + } + } + + Control.LogDebug(DType.IsDestroyed, $"- not destroyed"); + } + catch (Exception e) + { + Control.LogError(e); + } + } + } +} \ No newline at end of file diff --git a/source/Patches/Vehicle_IsDead.cs b/source/Patches/Vehicle_IsDead.cs new file mode 100644 index 0000000..2956373 --- /dev/null +++ b/source/Patches/Vehicle_IsDead.cs @@ -0,0 +1,54 @@ +using System; +using BattleTech; +using Harmony; + +namespace CustomComponents.Patches +{ + [HarmonyPatch(typeof(Vehicle), "IsDead")] + [HarmonyPatch(MethodType.Getter)] + public static class Vehicle_IsDead + { + [HarmonyPostfix] + public static void IsDestroyedChecks(Vehicle __instance, ref bool __result) + { + if (__instance == null) return; + + try + { + Control.LogDebug(DType.IsDestroyed, $"Check if dead for vehicle {__instance.VehicleDef.Description.Id}"); + + + if (__result) + { + Control.LogDebug(DType.IsDestroyed, $"- Vanila destroyed"); + return ; + } + + foreach (var item in __instance.VehicleDef.Inventory) + { + if (Control.Settings.CheckCriticalComponent && item.Def.CriticalComponent && + item.DamageLevel == ComponentDamageLevel.Destroyed) + { + Control.LogDebug(DType.IsDestroyed, $"- Destroyed by CriticalComponent {item.ComponentDefID}"); + __result = true; + return; + } + + if (item.Def.Is(out var d) && d.IsVechicleDestroyed(item, __instance)) + { + __result = true; + Control.LogDebug(DType.IsDestroyed, + $"- Destroyed by CheckDestroyed {item.ComponentDefID} of {d.GetType()}"); + return; + } + } + + Control.LogDebug(DType.IsDestroyed, $"- not destroyed"); + } + catch (Exception e) + { + Control.LogError(e); + } + } + } +} \ No newline at end of file