Skip to content

Instantly share code, notes, and snippets.

@billw2012
Created July 4, 2020 16:38
Show Gist options
  • Save billw2012/f06852ee9368f766c92fb4645db19d3f to your computer and use it in GitHub Desktop.
Save billw2012/f06852ee9368f766c92fb4645db19d3f to your computer and use it in GitHub Desktop.
Troop cost/wage inflation
public static class Util
{
struct PriceInflation
{
public int UpdatedDay;
public bool NeedsUpdate => (int)Campaign.Current.CampaignStartTime.ElapsedDaysUntilNow > UpdatedDay;
public float Inflation;
};
private static Dictionary<object, PriceInflation> categoryInflation = new Dictionary<object, PriceInflation>();
private static float GetGlobalAverageBuyPrice(ItemObject item)
{
return Town.AllTowns.Select(t => (float)t.MarketData.GetPrice(item)).Average();
}
private static float GetBasePrice(ItemObject item)
{
return item.Value * 1.5f; // 1.5 is just a hack to estimate an base game "real" average price from the base price
}
public static float GetInflation(object key, IEnumerable<ItemObject> items)
{
if(!categoryInflation.TryGetValue(key, out PriceInflation inf) || inf.NeedsUpdate)
{
inf = new PriceInflation
{
UpdatedDay = (int)Campaign.Current.CampaignStartTime.ElapsedDaysUntilNow,
Inflation =
items
.Select(i => GetGlobalAverageBuyPrice(i) / GetBasePrice(i))
.Average()
};
categoryInflation[key] = inf;
}
return inf.Inflation;
}
}
[HarmonyPatch(typeof(DefaultPartyWageModel), "GetTroopRecruitmentCost")]
public class DefaultPartyWageModel_GetTroopRecruitmentCost_Patch
{
static void Postfix(CharacterObject troop, Hero buyerHero, bool withoutItemCost, ref int __result)
{
var cats = troop.AllEquipments
.SelectMany(e => Enumerable.Range(0, 12).Select(i => e[i].Item).Where(ei => ei != null));
__result = (int)Math.Ceiling(__result * Util.GetInflation(troop, cats));
}
}
[HarmonyPatch(typeof(CharacterObject), "TroopWage", MethodType.Getter)]
public class CharacterObject_TroopWage_Getter_Patch
{
static void Postfix(CharacterObject __instance, ref int __result)
{
var cats = __instance.AllEquipments
.SelectMany(e => Enumerable.Range(0, 12).Select(i => e[i].Item).Where(ei => ei != null));
__result = (int)Math.Ceiling(__result * Util.GetInflation(__instance, cats));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment