Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
#36 Added max health, supercharge health fields and hard damage intak…
Browse files Browse the repository at this point in the history
…e percent field
  • Loading branch information
eternalUnion committed May 26, 2023
1 parent 7c97d80 commit 23f6905
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Ultrapain/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public static void AddMissingPresets()
public static FloatField rocketFreezeRegSpeedMulti;
public static FloatField rocketCannonballRegSpeedMulti;

public static IntField maxPlayerHp;
public static IntField playerHpSupercharge;
public static FloatSliderField hardDamagePercent;

// ENEMY PANEL
public static ConfigPanel globalEnemyPanel;
public static ConfigPanel cerberusPanel;
Expand Down Expand Up @@ -542,6 +546,10 @@ public static void Initialize()
playerStatEditorPanel = new ConfigPanel(playerPanel, "Player stat editor", "playerStatEditorPanel");
new ConfigHeader(playerStatEditorPanel, "Movement");
staminaRegSpeedMulti = new FloatField(playerStatEditorPanel, "Stamina regen speed", "staminaRegSpeedMulti", 1f, 0.01f, float.MaxValue);
new ConfigHeader(playerStatEditorPanel, "Stats");
maxPlayerHp = new IntField(playerStatEditorPanel, "Max HP", "maxPlayerHp", 100, 1, int.MaxValue);
playerHpSupercharge = new IntField(playerStatEditorPanel, "Max overcharge HP", "playerHpSupercharge", 200, 1, int.MaxValue);
hardDamagePercent = new FloatSliderField(playerStatEditorPanel, "Hard damage taken percent", "hardDamagePercent", new Tuple<float, float>(0, 100), 100);
new ConfigHeader(playerStatEditorPanel, "Revolver");
chargedRevRegSpeedMulti = new FloatField(playerStatEditorPanel, "Charged revolver regen speed", "chargedRevRegSpeedMulti", 1f, 0.01f, float.MaxValue);
coinRegSpeedMulti = new FloatField(playerStatEditorPanel, "Coin regen speed", "coinRegSpeedMulti", 1f, 0.01f, float.MaxValue);
Expand All @@ -564,7 +572,24 @@ void AddDirtyFlagToFloatFieldValueChange(FloatField field)
dirtyField = true;
};
}
void AddDirtyFlagToFloatSliderFieldValueChange(FloatSliderField field)
{
field.onValueChange += (FloatSliderField.FloatSliderValueChangeEvent e) =>
{
dirtyField = true;
};
}
void AddDirtyFlagToIntFieldValueChange(IntField field)
{
field.onValueChange += (IntField.IntValueChangeEvent e) =>
{
dirtyField = true;
};
}
AddDirtyFlagToFloatFieldValueChange(staminaRegSpeedMulti);
AddDirtyFlagToIntFieldValueChange(maxPlayerHp);
AddDirtyFlagToIntFieldValueChange(playerHpSupercharge);
AddDirtyFlagToFloatSliderFieldValueChange(hardDamagePercent);
AddDirtyFlagToFloatFieldValueChange(chargedRevRegSpeedMulti);
AddDirtyFlagToFloatFieldValueChange(coinRegSpeedMulti);
AddDirtyFlagToFloatFieldValueChange(sharpshooterRegSpeedMulti);
Expand Down
220 changes: 219 additions & 1 deletion Ultrapain/Patches/PlayerStatTweaks.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System;
using HarmonyLib;
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using UnityEngine;

Expand Down Expand Up @@ -91,4 +96,217 @@ static bool Prefix(WeaponCharges __instance, float __0)
return true;
}
}

class NewMovement_GetHurt
{
static bool Prefix(NewMovement __instance, out float __state)
{
__state = __instance.antiHp;
return true;
}

static void Postfix(NewMovement __instance, float __state)
{
float deltaAnti = __instance.antiHp - __state;
if (deltaAnti <= 0)
return;

deltaAnti *= ConfigManager.hardDamagePercent.normalizedValue;
__instance.antiHp = __state + deltaAnti;
}

static FieldInfo hpField = typeof(NewMovement).GetField("hp");

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Ldfld && (FieldInfo)code[i].operand == hpField)
{
i += 1;
if (code[i].opcode == OpCodes.Ldc_I4_S)
{
code[i] = new CodeInstruction(OpCodes.Ldc_I4, (Int32)ConfigManager.maxPlayerHp.value);
}
}
else if (code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == (Single)99f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)(ConfigManager.maxPlayerHp.value - 1));
}
}

return code.AsEnumerable();
}
}

class HookArm_FixedUpdate
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == 66f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)(66f * (ConfigManager.maxPlayerHp.value / 100f)));
}
else if (code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == 50f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)(ConfigManager.maxPlayerHp.value / 2));
}
}

return code.AsEnumerable();
}
}

class NewMovement_ForceAntiHP
{
static FieldInfo hpField = typeof(NewMovement).GetField("hp");

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> code = new List<CodeInstruction>(instructions);

for (int i = 0; i < code.Count; i++)
{
if (code[i].opcode == OpCodes.Ldfld && (FieldInfo)code[i].operand == hpField)
{
i += 1;
if (i < code.Count && code[i].opcode == OpCodes.Ldc_I4_S && (SByte)code[i].operand == (SByte)100)
{
code[i] = new CodeInstruction(OpCodes.Ldc_I4, (Int32)ConfigManager.maxPlayerHp.value);
}
}
else if (code[i].opcode == OpCodes.Ldarg_1)
{
i += 2;
if (i < code.Count && code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == 99f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)(ConfigManager.maxPlayerHp.value - 1));
}
}
else if (code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == 100f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)ConfigManager.maxPlayerHp.value);
}
else if (code[i].opcode == OpCodes.Ldc_R4 && (Single)code[i].operand == 50f)
{
code[i] = new CodeInstruction(OpCodes.Ldc_R4, (Single)ConfigManager.maxPlayerHp.value / 2);
}
else if (code[i].opcode == OpCodes.Ldc_I4_S && (SByte)code[i].operand == (SByte)100)
{
code[i] = new CodeInstruction(OpCodes.Ldc_I4, (Int32)ConfigManager.maxPlayerHp.value);
}
}

return code.AsEnumerable();
}
}

class NewMovement_GetHealth
{
static bool Prefix(NewMovement __instance, int __0, bool __1, ref AudioSource ___greenHpAud, Canvas ___fullHud)
{
if (__instance.dead || __instance.exploded)
return false;

int maxHp = Mathf.RoundToInt(ConfigManager.maxPlayerHp.value - __instance.antiHp);
int maxDelta = maxHp - __instance.hp;
if (maxDelta <= 0)
return true;

if (!__1 && __0 > 5 && MonoSingleton<PrefsManager>.Instance.GetBoolLocal("bloodEnabled", false))
{
GameObject.Instantiate<GameObject>(__instance.scrnBlood, ___fullHud.transform);
}

__instance.hp = Mathf.Min(maxHp, __instance.hp + __0);
__instance.hpFlash.Flash(1f);

if (!__1 && __0 > 5)
{
if (___greenHpAud == null)
{
___greenHpAud = __instance.hpFlash.GetComponent<AudioSource>();
}
___greenHpAud.Play();
}

return false;
}
}

class NewMovement_SuperCharge
{
static bool Prefix(NewMovement __instance)
{
__instance.hp = Mathf.Max(ConfigManager.maxPlayerHp.value, ConfigManager.playerHpSupercharge.value);
return false;
}
}

class NewMovement_Respawn
{
static void Postfix(NewMovement __instance)
{
__instance.hp = ConfigManager.maxPlayerHp.value;
}
}

class NewMovement_Start
{
static void Postfix(NewMovement __instance)
{
__instance.hp = ConfigManager.maxPlayerHp.value;
}
}

class HealthBarTracker : MonoBehaviour
{
public static List<HealthBarTracker> instances = new List<HealthBarTracker>();
private HealthBar hb;

private void Awake()
{
if (hb == null)
hb = GetComponent<HealthBar>();

instances.Add(this);

for (int i = instances.Count - 1; i >= 0; i--)
{
if (instances[i] == null)
instances.RemoveAt(i);
}
}

private void OnDestroy()
{
if (instances.Contains(this))
instances.Remove(this);
}

public void SetSliderRange()
{
if (hb == null)
hb = GetComponent<HealthBar>();

hb.hpSliders[0].maxValue = hb.afterImageSliders[0].maxValue = ConfigManager.maxPlayerHp.value;
hb.hpSliders[1].minValue = hb.afterImageSliders[1].minValue = ConfigManager.maxPlayerHp.value;
hb.hpSliders[1].maxValue = hb.afterImageSliders[1].maxValue = Mathf.Max(ConfigManager.maxPlayerHp.value, ConfigManager.playerHpSupercharge.value);
hb.antiHpSlider.maxValue = ConfigManager.maxPlayerHp.value;
}
}

class HealthBar_Start
{
static void Postfix(HealthBar __instance)
{
__instance.gameObject.AddComponent<HealthBarTracker>().SetSliderRange();
}
}
}
21 changes: 21 additions & 0 deletions Ultrapain/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,27 @@ private static void PatchAllPlayers()
harmonyTweaks.Patch(GetMethod<Nailgun>("Update"), prefix: GetHarmonyMethod(GetMethod<NailGun_Update>("Prefix")));
if(ConfigManager.staminaRegSpeedMulti.value != 1)
harmonyTweaks.Patch(GetMethod<NewMovement>("Update"), prefix: GetHarmonyMethod(GetMethod<NewMovement_Update>("Prefix")));

if(ConfigManager.maxPlayerHp.value != 100 || ConfigManager.playerHpSupercharge.value != 200)
{
harmonyTweaks.Patch(GetMethod<NewMovement>("GetHealth"), prefix: GetHarmonyMethod(GetMethod<NewMovement_GetHealth>("Prefix")));
harmonyTweaks.Patch(GetMethod<NewMovement>("SuperCharge"), prefix: GetHarmonyMethod(GetMethod<NewMovement_SuperCharge>("Prefix")));
harmonyTweaks.Patch(GetMethod<NewMovement>("Respawn"), postfix: GetHarmonyMethod(GetMethod<NewMovement_Respawn>("Postfix")));
harmonyTweaks.Patch(GetMethod<NewMovement>("Start"), postfix: GetHarmonyMethod(GetMethod<NewMovement_Start>("Postfix")));
harmonyTweaks.Patch(GetMethod<NewMovement>("GetHurt"), transpiler: GetHarmonyMethod(GetMethod<NewMovement_GetHurt>("Transpiler")));
harmonyTweaks.Patch(GetMethod<HookArm>("FixedUpdate"), transpiler: GetHarmonyMethod(GetMethod<HookArm_FixedUpdate>("Transpiler")));
harmonyTweaks.Patch(GetMethod<NewMovement>("ForceAntiHP"), transpiler: GetHarmonyMethod(GetMethod<NewMovement_ForceAntiHP>("Transpiler")));
}

if (ConfigManager.hardDamagePercent.normalizedValue != 1)
harmonyTweaks.Patch(GetMethod<NewMovement>("GetHurt"), prefix: GetHarmonyMethod(GetMethod<NewMovement_GetHurt>("Prefix")), postfix: GetHarmonyMethod(GetMethod<NewMovement_GetHurt>("Postfix")));

harmonyTweaks.Patch(GetMethod<HealthBar>("Start"), postfix: GetHarmonyMethod(GetMethod<HealthBar_Start>("Postfix")));
foreach (HealthBarTracker hb in HealthBarTracker.instances)
{
if (hb != null)
hb.SetSliderRange();
}
}

private static void PatchAllMemes()
Expand Down

0 comments on commit 23f6905

Please sign in to comment.