Skip to content

Commit

Permalink
Merge 1c6a127 into 680ab48
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokachi authored May 30, 2021
2 parents 680ab48 + 1c6a127 commit 2497a91
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 104 deletions.
141 changes: 141 additions & 0 deletions DifficultyMod/Core/DifficultyPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using Boardgame;
using Boardgame.BoardEntities;
using Boardgame.Data;
using Boardgame.Networking;
using Boardgame.SerializableEvents;
using HarmonyLib;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;

namespace DemeoMods.DifficultyMod.Core
{
static class DifficultyPatcher
{
private static GameStateMachine StateMachine { get; set; }

public static void SetGameStateMachine()
{
StateMachine = GameObject.Find("GameLogic").GetComponent<GameStateMachine>();
}

public static int ModifyEnemyHPMultiplier(int defaultHP, PieceConfig pieceConfig)
{
// Do not modify the HP if the game is public (i.e. anyone can join without room code)
if (!IsPrivateGame())
{
return defaultHP;
}

if (pieceConfig.HasPieceType(DataKeys.PieceType.Enemy))
{
return (int)(defaultHP * DifficultySettings.EnemyHPMultiplier);
}

return defaultHP;
}

public static int ModifyEnemyAttackMultiplier(int defaultAttack, PieceConfig pieceConfig)
{

// Do not modify the Attack if the game is public (i.e. anyone can join without room code)
if (!IsPrivateGame())
{
return defaultAttack;
}

if (pieceConfig.HasPieceType(DataKeys.PieceType.Enemy))
{
return (int)(defaultAttack * DifficultySettings.EnemyAttackMultiplier);
}

return defaultAttack;
}

private static bool IsPrivateGame()
{
CreateGameMode gameMode = (CreateGameMode)(typeof(GameStateMachine).GetField("createGameMode", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(StateMachine));
return gameMode == CreateGameMode.Private;
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
class EnemyHPMultiplierPatcher
{
static MethodInfo m_MyExtraMethod = AccessTools.Method(typeof(DifficultyPatcher), nameof(ModifyEnemyHPMultiplier), new[] { typeof(int), typeof(PieceConfig) });

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt)
{
string strOperand = instruction.operand.ToString();
if (strOperand.Contains("get_StartHealth"))
{
yield return instruction;

yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);

continue;
}
}

yield return instruction;
}
}
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
class EnemyAttackMultiplierPatcher
{
static MethodInfo m_MyExtraMethod = AccessTools.Method(typeof(DifficultyPatcher), nameof(ModifyEnemyAttackMultiplier), new[] { typeof(int), typeof(PieceConfig) });

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt)
{
string strOperand = instruction.operand.ToString();
if (strOperand.Contains("get_AttackDamage"))
{
yield return instruction;

yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);

continue;
}
}
yield return instruction;
}
}
}

[HarmonyPatch(typeof(CardPowder), "GetPowderScale")]
class EnergyGainMultiplierPatcher
{
static void Postfix(ref float __result)
{
if (IsPrivateGame())
{
__result *= DifficultySettings.EnergyGainMultiplier;
}
}
}

[HarmonyPatch(typeof(SerializableEventPickup), MethodType.Constructor, typeof(int), typeof(IntPoint2D), typeof(bool))]
class GoldPileGainMultiplierPatcher
{
static void Postfix(SerializableEventPickup __instance)
{
if (IsPrivateGame())
{
__instance.goldAmount = (int) (__instance.goldAmount * DifficultySettings.GoldPileGainMultiplier);
}
}
}
}
}
165 changes: 94 additions & 71 deletions DifficultyMod/Core/DifficultySettings.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,109 @@
using Boardgame;
using Boardgame.BoardEntities;
using HarmonyLib;
using MelonLoader;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace DemeoMods.DifficultyMod.Core
{
public static class DifficultySettings
{
private const string MELON_PREF_NAME = "DemeoDifficultyMod";
private const string MELON_PREF_ENEMY_HP_MULTIPLIER_NAME = "EnemyHpMultiplier";
private const string MELON_PREF_ENEMY_ATTACK_MULTIPLIER_NAME = "EnemyAttackMultiplier";
private const string MELON_PREF_ENERGY_GAIN_MULTIPLIER_NAME = "EnergyGainMultiplier";
private const string MELON_PREF_GOLD_PILE_GAIN_MULTIPLIER_NAME = "GoldGainMultiplier";

private const float ENEMY_HP_MULTIPLIER_MIN = 0.25f;
private const float ENEMY_HP_MULTIPLIER_MAX = 5f;
private const float ENEMY_ATTACK_MULTIPLIER_MIN = 0.25f;
private const float ENEMY_ATTACK_MULTIPLIER_MAX = 5f;
private const float ENERGY_GAIN_MULTIPLIER_MIN = 0.1f;
private const float ENERGY_GAIN_MULTIPLIER_MAX = 5f;
private const float GOLD_PILE_GAIN_MULTIPLIER_MIN = 0.1f;
private const float GOLD_PILE_GAIN_MULTIPLIER_MAX = 5f;

public static float EnemyHPMultiplier { get; set; } = 2;
public static float EnemyAttackMultiplier { get; set; } = 2;
public static void RegisterSettings()
{
MelonPreferences.CreateCategory(MELON_PREF_NAME, "Demeo Difficulty Settings");
MelonPreferences.CreateEntry(MELON_PREF_NAME, MELON_PREF_ENEMY_HP_MULTIPLIER_NAME, 1f, "Enemy HP Multiplier");
MelonPreferences.CreateEntry(MELON_PREF_NAME, MELON_PREF_ENEMY_ATTACK_MULTIPLIER_NAME, 1f, "Enemy Attack Multiplier");
MelonPreferences.CreateEntry(MELON_PREF_NAME, MELON_PREF_ENERGY_GAIN_MULTIPLIER_NAME, 1f, "Energy Gain Multiplier");
MelonPreferences.CreateEntry(MELON_PREF_NAME, MELON_PREF_GOLD_PILE_GAIN_MULTIPLIER_NAME, 1f, "Gold Pile Gain Multiplier");
}

public static void DecreaseEnemyHPMultiplier(Action<float> callBack)
public static float EnemyHPMultiplier
{
if (EnemyHPMultiplier > ENEMY_HP_MULTIPLIER_MIN)
get
{
return MelonPreferences.GetEntryValue<float>(MELON_PREF_NAME, MELON_PREF_ENEMY_HP_MULTIPLIER_NAME);
}
set
{
EnemyHPMultiplier -= 0.25f;
MelonPreferences.SetEntryValue(MELON_PREF_NAME, MELON_PREF_ENEMY_HP_MULTIPLIER_NAME, value);
}
}

callBack(EnemyHPMultiplier);
public static float EnemyAttackMultiplier
{
get
{
return MelonPreferences.GetEntryValue<float>(MELON_PREF_NAME, MELON_PREF_ENEMY_ATTACK_MULTIPLIER_NAME);
}
set
{
MelonPreferences.SetEntryValue(MELON_PREF_NAME, MELON_PREF_ENEMY_ATTACK_MULTIPLIER_NAME, value);
}
}

public static void IncreaseEnemyHPMultiplier(Action<float> callBack)
public static float EnergyGainMultiplier
{
if (EnemyHPMultiplier < ENEMY_HP_MULTIPLIER_MAX)
get
{
EnemyHPMultiplier += 0.25f;
return MelonPreferences.GetEntryValue<float>(MELON_PREF_NAME, MELON_PREF_ENERGY_GAIN_MULTIPLIER_NAME);
}
set
{
MelonPreferences.SetEntryValue(MELON_PREF_NAME, MELON_PREF_ENERGY_GAIN_MULTIPLIER_NAME, value);
}

callBack(EnemyHPMultiplier);
}

public static int ModifyEnemyHPMultiplier(int defaultHP, PieceConfig pieceConfig)
public static float GoldPileGainMultiplier
{
if (pieceConfig.HasPieceType(DataKeys.PieceType.Enemy))
get
{
return (int)(defaultHP * EnemyHPMultiplier);
return MelonPreferences.GetEntryValue<float>(MELON_PREF_NAME, MELON_PREF_GOLD_PILE_GAIN_MULTIPLIER_NAME);
}
set
{
MelonPreferences.SetEntryValue(MELON_PREF_NAME, MELON_PREF_GOLD_PILE_GAIN_MULTIPLIER_NAME, value);
}

return defaultHP;
}

public static void DecreaseEnemyHPMultiplier(Action<float> callBack)
{
if (EnemyHPMultiplier > ENEMY_HP_MULTIPLIER_MIN)
{
EnemyHPMultiplier = (float) Math.Round(EnemyHPMultiplier - 0.25f, 2);
}

callBack(EnemyHPMultiplier);
}

public static void IncreaseEnemyHPMultiplier(Action<float> callBack)
{
if (EnemyHPMultiplier < ENEMY_HP_MULTIPLIER_MAX)
{
EnemyHPMultiplier = (float)Math.Round(EnemyHPMultiplier + 0.25f, 2);
}

callBack(EnemyHPMultiplier);
}

public static void DecreaseEnemyAttackMultiplier(Action<float> callBack)
{
if (EnemyAttackMultiplier > ENEMY_ATTACK_MULTIPLIER_MIN)
{
EnemyAttackMultiplier -= 0.25f;
EnemyAttackMultiplier = (float)Math.Round(EnemyAttackMultiplier - 0.25f, 2);
}

callBack(EnemyAttackMultiplier);
Expand All @@ -63,77 +113,50 @@ public static void IncreaseEnemyAttackMultiplier(Action<float> callBack)
{
if (EnemyAttackMultiplier < ENEMY_ATTACK_MULTIPLIER_MAX)
{
EnemyAttackMultiplier += 0.25f;
EnemyAttackMultiplier = (float)Math.Round(EnemyAttackMultiplier + 0.25f, 2);
}

callBack(EnemyAttackMultiplier);
}

public static int ModifyEnemyAttackMultiplier(int defaultAttack, PieceConfig pieceConfig)
public static void DecreaseEnergyGainMultiplier(Action<float> callBack)
{
if (pieceConfig.HasPieceType(DataKeys.PieceType.Enemy))
if (EnergyGainMultiplier > ENERGY_GAIN_MULTIPLIER_MIN)
{
return (int)(defaultAttack * EnemyAttackMultiplier);
EnergyGainMultiplier = (float)Math.Round(EnergyGainMultiplier - 0.1f, 2);
}

return defaultAttack;
callBack(EnergyGainMultiplier);
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
public static class EnemyHPMultiplierPatcher
public static void IncreaseEnergyGainMultiplier(Action<float> callBack)
{
private const int numInstructionToWait = 1;
static MethodInfo m_MyExtraMethod = AccessTools.Method(typeof(DifficultySettings), nameof(ModifyEnemyHPMultiplier), new[] { typeof(int), typeof(PieceConfig) });

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
if (EnergyGainMultiplier < ENERGY_GAIN_MULTIPLIER_MAX)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt)
{
string strOperand = instruction.operand.ToString();
if (strOperand.Contains("get_StartHealth"))
{
yield return instruction;

yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);

continue;
}
}

yield return instruction;
}
EnergyGainMultiplier = (float)Math.Round(EnergyGainMultiplier + 0.1f, 2);
}

callBack(EnergyGainMultiplier);
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
public static class EnemyAttackMultiplierPatcher
public static void DecreaseGoldPileGainMultiplier(Action<float> callBack)
{
private const int numInstructionToWait = 1;
static MethodInfo m_MyExtraMethod = AccessTools.Method(typeof(DifficultySettings), nameof(ModifyEnemyAttackMultiplier), new[] { typeof(int), typeof(PieceConfig) });

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
if (GoldPileGainMultiplier > GOLD_PILE_GAIN_MULTIPLIER_MIN)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt)
{
string strOperand = instruction.operand.ToString();
if (strOperand.Contains("get_AttackDamage"))
{
yield return instruction;
GoldPileGainMultiplier = (float)Math.Round(GoldPileGainMultiplier - 0.1f, 2);
}

yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);
callBack(EnergyGainMultiplier);
}

continue;
}
}
yield return instruction;
}
public static void IncreaseGoldPileGainMultiplier(Action<float> callBack)
{
if (GoldPileGainMultiplier < GOLD_PILE_GAIN_MULTIPLIER_MAX)
{
GoldPileGainMultiplier = (float)Math.Round(GoldPileGainMultiplier + 0.1f, 2);
}

callBack(GoldPileGainMultiplier);
}
}
}
Loading

0 comments on commit 2497a91

Please sign in to comment.