Skip to content

Commit

Permalink
added feature to manipulate armor and structure points, see #45
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Jul 29, 2018
1 parent eb4fa92 commit 52487d4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
11 changes: 11 additions & 0 deletions source/Features/ArmorStructureChanges/ArmorStructureChanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CustomComponents;

namespace MechEngineer
{
[CustomComponent("ArmorStructureChanges")]
public class ArmorStructureChanges : SimpleCustomComponent
{
public float StructureFactor { get; set; } = 1;
public float ArmorFactor { get; set; } = 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using BattleTech;
using Harmony;
using UnityEngine;

namespace MechEngineer
{
// fixing factor not being considerd for campaign stuff, this removes the factor again before switching back to campaign
[HarmonyPatch(typeof(Mech), "ToMechDef")]
public static class Mech_ToMechDef_Patch
{
public static void Postfix(Mech __instance, MechDef __result)
{
var mech = __instance;
var mechDef = __result;
var armorFactor = Mech_get_ArmorMultiplier_Patch.GetFactorForMech(mech);
var structureFactor = Mech_get_StructureMultiplier_Patch.GetFactorForMech(mech);

var adapter = new MechDefAdapter(mechDef);
foreach (var mechLocationDef in adapter.Locations)
{
if (mechLocationDef.CurrentInternalStructure > 0)
{
mechLocationDef.CurrentInternalStructure /= structureFactor;
var chassisLocationDef = mechDef.Chassis.GetLocationDef(mechLocationDef.Location);
if (Mathf.Approximately(mechLocationDef.CurrentInternalStructure, chassisLocationDef.InternalStructure))
{
mechLocationDef.CurrentInternalStructure = chassisLocationDef.InternalStructure;
}
}

if (mechLocationDef.CurrentArmor > 0)
{
mechLocationDef.CurrentArmor /= armorFactor;
if (Mathf.Approximately(mechLocationDef.CurrentArmor, mechLocationDef.AssignedArmor))
{
mechLocationDef.CurrentArmor = mechLocationDef.AssignedArmor;
}
}

if (mechLocationDef.CurrentRearArmor > 0)
{
mechLocationDef.CurrentRearArmor /= armorFactor;
if (Mathf.Approximately(mechLocationDef.CurrentRearArmor, mechLocationDef.AssignedRearArmor))
{
mechLocationDef.CurrentRearArmor = mechLocationDef.AssignedRearArmor;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Linq;
using BattleTech;
using CustomComponents;
using Harmony;

namespace MechEngineer
{
[HarmonyPatch(typeof(Mech), "get_ArmorMultiplier")]
public static class Mech_get_ArmorMultiplier_Patch
{
public static void Postfix(Mech __instance, ref float __result)
{
__result = __result * GetFactorForMech(__instance);
}

internal static float GetFactorForMech(Mech mech)
{
const string key = "ArmorMultiplier";
var statistic = mech.StatCollection.GetStatistic(key)
?? mech.StatCollection.AddStatistic(key, GetFactorForMechDef(mech.MechDef));
return statistic.Value<float>();
}

internal static float GetFactorForMechDef(MechDef mechDef)
{
return mechDef.Inventory
.Select(r => r.GetComponent<ArmorStructureChanges>())
.Where(c => c != null)
.Select(c => c.ArmorFactor)
.Aggregate(1.0f, (acc, val) => acc * val);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Linq;
using BattleTech;
using CustomComponents;
using Harmony;

namespace MechEngineer
{
[HarmonyPatch(typeof(Mech), "get_StructureMultiplier")]
public static class Mech_get_StructureMultiplier_Patch
{
public static void Postfix(Mech __instance, ref float __result)
{
__result = __result * GetFactorForMech(__instance);
}

internal static float GetFactorForMech(Mech mech)
{
const string key = "StructureMultiplier";
var statistic = mech.StatCollection.GetStatistic(key)
?? mech.StatCollection.AddStatistic(key, GetFactorForMechDef(mech.MechDef));
return statistic.Value<float>();
}

internal static float GetFactorForMechDef(MechDef mechDef)
{
return mechDef.Inventory
.Select(r => r.GetComponent<ArmorStructureChanges>())
.Where(c => c != null)
.Select(c => c.StructureFactor)
.Aggregate(1.0f, (acc, val) => acc * val);
}
}
}
4 changes: 3 additions & 1 deletion source/Features/HardpointFix/sorting/MechDefAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ internal MechDefAdapter(MechDef instance)
_traverse = Traverse.Create(instance);
}

internal LocationLoadoutDef[] Locations => _traverse.Field("Locations").GetValue() as LocationLoadoutDef[];

internal MechComponentRef[] Inventory => _traverse.Field("inventory").GetValue() as MechComponentRef[];

internal DataManager DataManager => _traverse.Field("dataManager").GetValue() as DataManager;

internal ChassisDef Chassis => _instance.Chassis;
}
}
}
4 changes: 4 additions & 0 deletions source/MechEngineer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Features\ArmorStructureChanges\Patches\Mech_ToMechDef_Patch.cs" />
<Compile Include="Features\ArmorStructureChanges\Patches\Mech_get_ArmorMultiplier_Patch.cs" />
<Compile Include="Features\ArmorStructureChanges\Patches\Mech_get_StructureMultiplier_Patch.cs" />
<Compile Include="Features\ArmorStructureChanges\ArmorStructureChanges.cs" />
<Compile Include="Misc\CCValidationAdapter.cs" />
<Compile Include="Engine\Handler\EngineValidation.cs" />
<Compile Include="Misc\LogManager.cs" />
Expand Down

0 comments on commit 52487d4

Please sign in to comment.