Skip to content

Commit

Permalink
- Update to HarmonyX
Browse files Browse the repository at this point in the history
- Remove Traverse, AccessTools
  • Loading branch information
IceRaptor committed Apr 16, 2023
1 parent c50f756 commit 0c78965
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 164 deletions.
38 changes: 24 additions & 14 deletions DisorderlyWithdrawal/DisorderlyWithdrawal/Helper/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
using BattleTech;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using us.frostraptor.modUtils.math;

namespace DisorderlyWithdrawal {
namespace DisorderlyWithdrawal
{

public class ExpensesSorter : IComparer<KeyValuePair<string, int>> {
public int Compare(KeyValuePair<string, int> x, KeyValuePair<string, int> y) {
public class ExpensesSorter : IComparer<KeyValuePair<string, int>>
{
public int Compare(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
{
int cmp = y.Value.CompareTo(x.Value);
if (cmp == 0) {
if (cmp == 0)
{
cmp = y.Key.CompareTo(x.Key);
}
return cmp;
}
}

public static class Helper {
public static class Helper
{

public static int RoundsToWaitByAerospace() {
public static int RoundsToWaitByAerospace()
{
SimGameState simGameState = UnityGameInstance.BattleTechGame.Simulation;
Statistic aerospaceAssets = simGameState.CompanyStats.GetStatistic("AerospaceAssets");
int aerospaceSupport = aerospaceAssets != null ? aerospaceAssets.Value<int>() : 0;
Mod.Log.Info?.Write($"Player has aerospace support:{aerospaceSupport}");

int flightTimeRoll = 0;
switch (aerospaceSupport) {
switch (aerospaceSupport)
{
case 3:
flightTimeRoll = Mod.Random.Next(Mod.Config.HeavyWingMinRounds, Mod.Config.HeavyWingMaxRounds);
break;
Expand All @@ -48,25 +54,29 @@ public static int RoundsToWaitByAerospace() {

// Damage Functions Below

public static float CalculateCombatDamage() {
public static float CalculateCombatDamage()
{
CombatGameState Combat = UnityGameInstance.BattleTechGame.Combat;

Mod.Log.Debug?.Write($"Finding player units centroid");
List<AbstractActor> playerUnits = Combat.LocalPlayerTeam.units;
List<AbstractActor> playerUnits = Combat.LocalPlayerTeam.units;
List<Vector3> playerPositions = playerUnits.Select(aa => aa.CurrentPosition).ToList();
Vector3 playerUnitsCentroid = GeometryUtils.FindCentroid(playerPositions);

Mod.Log.Debug?.Write($"Finding enemy units");
List<AbstractActor> enemyUnits = Combat.AllEnemies;
float totalDamage = 0.0f;
foreach (AbstractActor enemy in enemyUnits) {
foreach (AbstractActor enemy in enemyUnits)
{
float enemyDamage = 0.0f;
float distance = Vector3.Distance(playerUnitsCentroid, enemy.CurrentPosition);
Mod.Log.Debug?.Write($"Enemy:{enemy.DisplayName}_{enemy?.GetPilot()?.Name} is distance:{distance}m " +
$"at position:{enemy.CurrentPosition} from centroid:{playerUnitsCentroid}");

foreach (Weapon weapon in enemy.Weapons) {
if (weapon.MaxRange >= distance) {
foreach (Weapon weapon in enemy.Weapons)
{
if (weapon.MaxRange >= distance)
{
Mod.Log.Debug?.Write($"Enemy:{enemy.DisplayName}_{enemy?.GetPilot()?.Name} " +
$"has weapon:{weapon.Name} in range. Adding {weapon.DamagePerShot} damage for {weapon.ShotsWhenFired} shots.");
enemyDamage += (weapon.DamagePerShot * weapon.ShotsWhenFired);
Expand Down
9 changes: 6 additions & 3 deletions DisorderlyWithdrawal/DisorderlyWithdrawal/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

namespace DisorderlyWithdrawal {
namespace DisorderlyWithdrawal
{

public class ModConfig {
public class ModConfig
{

// If true, troubleshooting logging will be enabled
public bool Debug = false;
Expand Down Expand Up @@ -32,7 +34,8 @@ public class ModConfig {
public int NoWingMaxRounds = 6;
public int NoWingMinRounds = 4;

public void LogConfig() {
public void LogConfig()
{
Mod.Log.Info?.Write("=== MOD CONFIG BEGIN ===");
Mod.Log.Info?.Write($" DEBUG: {this.Debug}");
Mod.Log.Info?.Write($" LeopardRepairCostPerDamage:{LeopardRepairCostPerDamage}");
Expand Down
29 changes: 18 additions & 11 deletions DisorderlyWithdrawal/DisorderlyWithdrawal/ModInit.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Harmony;
using IRBTModUtils.Logging;
using IRBTModUtils.Logging;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Reflection;

namespace DisorderlyWithdrawal {
namespace DisorderlyWithdrawal
{

public class Mod {
public class Mod
{

public const string HarmonyPackage = "us.frostraptor.DisorderlyWithdrawal";
public const string LogName = "disorderly_withdrawal";
Expand All @@ -18,13 +19,17 @@ public class Mod {

public static readonly Random Random = new Random();

public static void Init(string modDirectory, string settingsJSON) {
public static void Init(string modDirectory, string settingsJSON)
{
ModDir = modDirectory;

Exception settingsE = null;
try {
try
{
Mod.Config = JsonConvert.DeserializeObject<ModConfig>(settingsJSON);
} catch (Exception e) {
}
catch (Exception e)
{
settingsE = e;
Mod.Config = new ModConfig();
}
Expand All @@ -39,14 +44,16 @@ public static void Init(string modDirectory, string settingsJSON) {
Log.Debug?.Write($"mod.json settings are:({settingsJSON})");
Mod.Config.LogConfig();

if (settingsE != null) {
if (settingsE != null)
{
Log.Info?.Write($"ERROR reading settings file! Error was: {settingsE}");
} else {
}
else
{
Log.Info?.Write($"INFO: No errors reading settings file.");
}

var harmony = HarmonyInstance.Create(HarmonyPackage);
harmony.PatchAll(Assembly.GetExecutingAssembly());
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), HarmonyPackage);
}
}
}
12 changes: 7 additions & 5 deletions DisorderlyWithdrawal/DisorderlyWithdrawal/ModState.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using BattleTech;
using BattleTech.UI;
using BattleTech.UI;
using TMPro;

namespace DisorderlyWithdrawal {
namespace DisorderlyWithdrawal
{

public static class ModState {
public static class ModState
{

public static bool WithdrawStarted = false;
public static bool WithdrawIsAvailable = false;
Expand All @@ -19,7 +20,8 @@ public static class ModState {

public static float CombatDamage = 0f;

public static void Reset() {
public static void Reset()
{
// Reinitialize state
WithdrawStarted = false;
WithdrawIsAvailable = false;
Expand Down
Loading

0 comments on commit 0c78965

Please sign in to comment.