-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added feature to manipulate armor and structure points, see #45
- Loading branch information
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
source/Features/ArmorStructureChanges/ArmorStructureChanges.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
source/Features/ArmorStructureChanges/Patches/Mech_ToMechDef_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
source/Features/ArmorStructureChanges/Patches/Mech_get_ArmorMultiplier_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
source/Features/ArmorStructureChanges/Patches/Mech_get_StructureMultiplier_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters