Skip to content

Commit

Permalink
ICheckIsDead interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Mar 30, 2019
1 parent 42fe10f commit 161d12e
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"Settings" : {
"LogLevel": "Debug",
"DebugInfo" : "ClearInventory, DefaultHandle, ComponentInstall",
"DebugInfo" : "ClearInventory, DefaultHandle, ComponentInstall, IsDestroyed, ShowConfig",

"Categories": [ ],
"TagRestrictions": [ ],
Expand Down
23 changes: 21 additions & 2 deletions source/Components/Flags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> flags;

Expand All @@ -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; }
Expand Down Expand Up @@ -63,6 +64,7 @@ public virtual void OnLoaded(Dictionary<string, object> values)
NotDestroyed = false;
Invalid = false;
DontShowMessage = false;
IsVital = false;

if (flags == null)
{
Expand Down Expand Up @@ -111,6 +113,9 @@ public virtual void OnLoaded(Dictionary<string, object> values)
case "hide_remove_message":
DontShowMessage = true;
break;
case "vital":
IsVital = true;
break;
}
}

Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions source/CustomComponentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public enum DType
DefaultHandle = 1 << 9,
ClearInventory = 1 << 10,
CustomResource = 1 << 11,
IsDestroyed = 1 << 12,
}


Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
<Compile Include="Helpers\SettingsResourceTools.cs" />
<Compile Include="Interfaces\IAdjustDescription.cs" />
<Compile Include="Defaults\IDefault.cs" />
<Compile Include="Interfaces\ICheckIsDead.cs" />
<Compile Include="Patches\Turret_IsDead.cs" />
<Compile Include="Patches\Vehicle_IsDead.cs" />
<Compile Include="Patches\Mech_IsDead.cs" />
<Compile Include="SorterLabInventory\InventorySorter.cs" />
<Compile Include="SorterLabInventory\InventorySorterComparer.cs" />
<Compile Include="SorterLabInventory\InventorySorterListComparer.cs" />
Expand Down
11 changes: 11 additions & 0 deletions source/Interfaces/ICheckIsDead.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
54 changes: 54 additions & 0 deletions source/Patches/Mech_IsDead.cs
Original file line number Diff line number Diff line change
@@ -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<ICheckIsDead>(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);
}
}
}
}
54 changes: 54 additions & 0 deletions source/Patches/Turret_IsDead.cs
Original file line number Diff line number Diff line change
@@ -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<ICheckIsDead>(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);
}
}
}
}
54 changes: 54 additions & 0 deletions source/Patches/Vehicle_IsDead.cs
Original file line number Diff line number Diff line change
@@ -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<ICheckIsDead>(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);
}
}
}
}

0 comments on commit 161d12e

Please sign in to comment.