Skip to content

Commit

Permalink
Fixed Inventory rounding. Handle Variance when upgrading items.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnanFay committed Nov 17, 2021
1 parent bad6fc6 commit 569bb91
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
35 changes: 34 additions & 1 deletion CraftingSkill/CraftingSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Pipakin.SkillInjectorMod;
using System;
using UnityEngine;
using UnityEngine.UI;
using ExtendedItemDataFramework;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -318,6 +319,12 @@ public static class InventoryGuiPatcherDoCrafting
static int m_crafts;
static int craftLevel;

// Before upgrading an item we store quality here
// so we can access it when the new item is created!
// Item is created between prefix and postfix in the original DoCrafting
// In postfix this field is nullified
public static StackableQuality preUpgradeQuality;

// private void DoCrafting(Player player)
static void Prefix(InventoryGui __instance, Player player,
// private fields
Expand All @@ -326,15 +333,26 @@ int ___m_craftVariant
)
{
// ZLog.LogError($"[{nameof(InventoryGui)}] pre crafting hook");
craftLevel = ((___m_craftUpgradeItem == null) ? 1 : (___m_craftUpgradeItem.m_quality + 1));
if (___m_craftUpgradeItem != null) {
craftLevel = ___m_craftUpgradeItem.m_quality + 1;
QualityComponent comp = ___m_craftUpgradeItem.Extended()?.GetComponent<QualityComponent>();
if (comp != null) {
preUpgradeQuality = comp.Quality;
}
} else {
craftLevel = 1;
}
m_crafts = Game.instance.GetPlayerProfile().m_playerStats.m_crafts;

}

static void Postfix(InventoryGui __instance, Player player,
// private fields
Recipe ___m_craftRecipe
)
{
// this MUST be nullified or new items will be bugged
preUpgradeQuality = null;

// ZLog.LogError($"[{nameof(InventoryGui)}] post crafting hook");
int new_m_crafts = Game.instance.GetPlayerProfile().m_playerStats.m_crafts;
Expand Down Expand Up @@ -392,6 +410,21 @@ static void Prefix(InventoryGui __instance,
}
}

[HarmonyPatch(typeof(InventoryGui), "UpdateCharacterStats")]
public static class InventoryGuiPatcherUpdateCharacterStats
{
// private void UpdateCharacterStats(Player player)
static void Postfix(InventoryGui __instance,
Player player,
// private fields
Text ___m_armor
)
{
float bodyArmor = player.GetBodyArmor();
___m_armor.text = $"{bodyArmor:0.00}";
}
}

[HarmonyPatch]
static class ItemDataClonePatches
{
Expand Down
8 changes: 8 additions & 0 deletions CraftingSkill/Quality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public Quality()
{
Variance = UnityEngine.Random.value;
}

public Quality(float skill, int quantity, int stationLevel) : this()
{
Skill = skill;
Quantity = quantity;
StationLevel = stationLevel;
}

public Quality Clone()
{
return MemberwiseClone() as Quality;
Expand Down
10 changes: 8 additions & 2 deletions CraftingSkill/QualityComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using static CraftingSkill.CraftingSkillsPlugin;
using Object = UnityEngine.Object;

namespace CraftingSkill
Expand Down Expand Up @@ -154,9 +155,14 @@ public static void OnExtendedItemData(ExtendedItemData itemdata)
CraftingStation station = player.GetCurrentCraftingStation();
int stationLevel = station == null ? 0 : station.GetLevel();

var quality = new StackableQuality(skill, quantity, stationLevel);
var quality = new Quality(skill, quantity, stationLevel);
// if we are upgrading an item
if (InventoryGuiPatcherDoCrafting.preUpgradeQuality != null) {
var prevVariance = InventoryGuiPatcherDoCrafting.preUpgradeQuality.Variance;
quality.Variance = Math.Max(quality.Variance, prevVariance);
}

itemdata.AddComponent<QualityComponent>().SetQuality(quality);
itemdata.AddComponent<QualityComponent>().SetQuality(new StackableQuality(quality));

// Quality may have changed durability on our new item, so fix it
itemdata.m_durability = itemdata.GetMaxDurability();
Expand Down
11 changes: 2 additions & 9 deletions CraftingSkill/StackableQuality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ public StackableQuality()
{
this.Qualities = new List<Quality>();
}

public StackableQuality(float skill, int quantity, int stationLevel)
{
this.Qualities = new List<Quality>();

var quality = new Quality();
quality.Skill = skill;
quality.Quantity = quantity;
quality.StationLevel = stationLevel;

public StackableQuality(Quality quality) : this()
{
Qualities.Add(quality);
}

Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ Default configuration:

#### Features:

* Variance should never decrease
* 'Reforge' an item at max quality to reroll variance and update craft level
* Armour number rounding in inventory

#### Known Bugs

Expand Down

0 comments on commit 569bb91

Please sign in to comment.