[Help Needed] Adding and implementing custom PartyWageModel

Users who are viewing this thread

How would one add a custom PartyWageModel implementation and what do I need to patch in order for that implementation to be used by the Campaign System?

I would like to add my custom class to the game -

C#:
 public class BSIPartyWageModel : PartyWageModel
    {

        public override int GetTotalWage(MobileParty mobileParty, StatExplainer explanation = null)
        { MyCodeHere }

        private static void CalculatePartialGarrisonWageReduction(
            float troopRatio,
            MobileParty mobileParty,
            PerkObject perk,
            ref ExplainedNumber garrisonWageReductionMultiplier,
            bool isSecondaryEffect)
        {My Code Here }

public override int GetGoldCostForUpgrade(
          PartyBase party,
          CharacterObject characterObject,
          CharacterObject upgradeTarget) { My Code Here }
        public override int GetTroopRecruitmentCost(
          CharacterObject troop,
          Hero buyerHero,
          bool withoutItemCost = false
          )
        {My Code Here }
    }

So now that I have built it, I would like to substitute it for DefaultPartyWageModel which currently in use. How would one go about doing that?

I know that I will still need harmony to patch CharacterObject.TroopWage but would like to know if I can add my wage model without patching the already existing one.

Clarification: I have already added the model by using the following method, need help now telling the game to use it!

Code:
protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
        {
            if (game.GameType is Campaign)
            {
                CampaignGameStarter cgs = gameStarterObject as CampaignGameStarter;
                cgs.AddModel(new MyCustomPartyWageModel());
            }
        }

Thanks!
 
Last edited:
Solution
If you want to replace the PartyWageModel you will not need to use harmony.

The way you added that model should work, if not you can use Reflection to set the property after the game has started and loaded all models:
C#:
public override void OnGameInitializationFinished(Game game)
{
    typeof(GameModels).GetProperty("PartyWageModel").SetValue(Campaign.Current.Models, new CustomPartyWageModel());
}
Instead of patching CharacterObject.TroopWage you can simply replace that part with a call to the function you would have replaced it with. As an example with the normal wages:
C#:
        public int TroopWageFunction(CharacterObject co)
        {
                if (co.IsHero)
                {
                    return 2 +...
If you want to replace the PartyWageModel you will not need to use harmony.

The way you added that model should work, if not you can use Reflection to set the property after the game has started and loaded all models:
C#:
public override void OnGameInitializationFinished(Game game)
{
    typeof(GameModels).GetProperty("PartyWageModel").SetValue(Campaign.Current.Models, new CustomPartyWageModel());
}
Instead of patching CharacterObject.TroopWage you can simply replace that part with a call to the function you would have replaced it with. As an example with the normal wages:
C#:
        public int TroopWageFunction(CharacterObject co)
        {
                if (co.IsHero)
                {
                    return 2 + co.Level * 2;
                }
                int result = 26;
                if (co.Tier == 0)
                {
                    result = 1;
                }
                else if (co.Tier == 1)
                {
                    result = 2;
                }
                else if (co.Tier == 2)
                {
                    result = 3;
                }
                else if (co.Tier == 3)
                {
                    result = 5;
                }
                else if (co.Tier == 4)
                {
                    result = 8;
                }
                else if (co.Tier == 5)
                {
                    result = 12;
                }
                else if (co.Tier == 6)
                {
                    result = 18;
                }
                return result;
           
        }

If you insist on patching it would be:
C#:
public class TSubModule : MBSubModuleBase
    {

        public static void TroopWageFunction(ref CharacterObject __instance, ref int __result)
        {
            __result = 10000;
        }

        protected override void OnSubModuleLoad()
        {
            base.OnSubModuleLoad();
            Harmony h = new Harmony("mod.Testing");
            MethodInfo original = typeof(CharacterObject).GetProperty("TroopWage").GetGetMethod();
            MethodInfo postfix = typeof(TSubModule).GetMethod("TroopWageFunction");
            h.Patch(original, postfix: new HarmonyMethod(postfix));
        }

    }
 
Upvote 0
Solution
Back
Top Bottom