static bool Prefix(SpellBookMetamagicMixer __instance, Kingmaker.UnitLogic.Feature feature, MetamagicBuilder ___m_MetamagicBuilder)
        {
            var unit = ___m_MetamagicBuilder?.Spellbook?.Owner;

            if (unit == null)
            {
                return(true);
            }

            var arcanist_part = unit.Get <SpellManipulationMechanics.UnitPartArcanistPreparedMetamagic>();

            if (arcanist_part == null)
            {
                return(true);
            }

            if (arcanist_part.spellbook != ___m_MetamagicBuilder.Spellbook.Blueprint)
            {
                return(true);
            }

            var current_metamagic_data = __instance.CurrentTemporarySpell?.MetamagicData;

            if (current_metamagic_data == null)
            {
                return(true);
            }

            var metamagic = feature.Get <AddMetamagicFeat>().Metamagic;
            var metamagic_after_removal = current_metamagic_data.MetamagicMask & ~metamagic;

            if (!arcanist_part.authorisedMetamagic(__instance.CurrentTemporarySpell.Blueprint, metamagic_after_removal))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        static public bool Prefix(UnitUseAbility __instance, UnitCommand.CommandType commandType, ref AbilityData spell, TargetWrapper target)
        {
            //base ctor

            /*this.Type = commandType;
             * this.Target = target;
             * this.Cutscene = (ElementsContext.GetData<CutsceneParametersContext.ContextData>() != null);*/

            //modify spell
            if (spell == null)
            {
                return(true);
            }
            if (spell.Spellbook == null)
            {
                return(true);
            }
            if (spell.Spellbook.Blueprint.CharacterClass != ArcanistClass.arcanist)
            {
                return(true);
            }
            UnitDescriptor unit          = spell.Caster;
            bool           hadMetamagic  = spell.MetamagicData != null;
            bool           hadMetamixing = unit.Buffs.HasFact(Metamixing.buff);

            //An arcanist can't combine prepared metamagic and spontaneous metamagic, if she doesn't have
            //the Metamixing exploit.
            if (hadMetamagic && !hadMetamixing)
            {
                return(true);
            }
            //UnityModManager.Logger.Log($"spell {spell.Name} has metamagic {(spell.MetamagicData!=null?spell.MetamagicData.MetamagicMask:0)}");
            MetamagicBuilder builder = new MetamagicBuilder(spell.Spellbook, spell);
            Dictionary <Metamagic, Feature> meta_feat = new Dictionary <Metamagic, Feature>();

            foreach (var ft in builder.SpellMetamagicFeatures)
            {
                AddMetamagicFeat comp = ft.Get <AddMetamagicFeat>();
                if (comp == null)
                {
                    continue;
                }
                Metamagic metaId = comp.Metamagic;
                meta_feat[metaId] = ft;
            }
            bool flag = false;

            foreach (var kv in SponMetamagic.buffDict)
            {
                Metamagic metaId        = (Metamagic)(kv.Key.first);
                int       HeightenLevel = kv.Key.second;
                if (!meta_feat.ContainsKey(metaId))
                {
                    continue;
                }
                BlueprintBuff buff = kv.Value;
                if (!unit.HasFact(buff))
                {
                    continue;
                }

                //If the arcanist has the Metamixing exploit, she can add ONE
                //metamagic feat to a prepared spell, without using extra time to cast.
                //(regardless if the spell had metamagic feats when prepared)
                if (metaId == Metamagic.Heighten)
                {
                    int originalLevel = spell.Spellbook.GetSpellLevel(spell.Blueprint);
                    if (HeightenLevel <= originalLevel)
                    {
                        continue;
                    }
                    builder.AddHeightenMetamagic(meta_feat[metaId], HeightenLevel - originalLevel);
                    unit.RemoveFact(buff);
                    flag = true;
                    if (hadMetamixing)
                    {
                        unit.Resources.Spend(ArcaneReservoir.resource, 1);
                        break;
                    }
                }
                else
                {
                    builder.AddMetamagic(meta_feat[metaId]);
                    unit.RemoveFact(buff);
                    flag = true;
                    if (hadMetamixing)
                    {
                        unit.Resources.Spend(ArcaneReservoir.resource, 1);
                        break;
                    }
                }
            }
            if (flag && !hadMetamixing)
            {
                unit.AddBuff(SponMetamagic.flagBuff, unit.Unit);
            }
            //fix ConvertedFrom. this is used in checking if a subspell is existed in memorized spells.
            //if spell is a subspell, spell.IsAvailableInSpellbook will use its m_ConvertedFrom(which is its master spell)
            //to get the return value.
            var newSpell = builder.ResultAbilityData;

            newSpell.ConvertedFrom = spell.ConvertedFrom;
            spell = newSpell;
            //UnityModManager.Logger.Log($"new spell {spell.Name} has metamagic {(spell.MetamagicData != null ? spell.MetamagicData.MetamagicMask : 0)}");
            return(true);
        }