private static void QuickHeal(ILContext il)
    {
        ILCursor cursor = new ILCursor(il);

        if (cursor.TryGotoNext(i => i.MatchLdcI4(0), i => i.MatchStloc(3), i => i.MatchBr(out _)))
        {
            cursor.Emit(OpCodes.Ldarg, 0);
            cursor.Emit(OpCodes.Ldloc, 0);
            cursor.Emit(OpCodes.Ldloca, 2);
            cursor.Emit(OpCodes.Ldloca, 1);

            cursor.EmitDelegate <QuickHeal_Del>((Player player, int lostHealth, ref int healthGain, ref Item result) =>
            {
                if (!ModContent.GetInstance <PortableStorageConfig>().AlchemistBagQuickHeal)
                {
                    return;
                }

                foreach (AlchemistBag bag in GetAlchemistBags(player))
                {
                    ItemStorage storage = bag.GetItemStorage();

                    for (int i = 0; i < AlchemistBag.PotionSlots; i++)
                    {
                        Item item = storage[i];
                        if (item.IsAir || !item.potion || item.healLife <= 0 || !CombinedHooks.CanUseItem(player, item))
                        {
                            continue;
                        }

                        int healWaste = player.GetHealLife(item, true) - lostHealth;
                        if (item.type == ItemID.RestorationPotion && healWaste < 0)
                        {
                            healWaste += 30;
                            if (healWaste > 0)
                            {
                                healWaste = 0;
                            }
                        }

                        if (healthGain < 0)
                        {
                            if (healWaste > healthGain)
                            {
                                result     = item;
                                healthGain = healWaste;
                            }
                        }
                        else if (healWaste < healthGain && healWaste >= 0)
                        {
                            result     = item;
                            healthGain = healWaste;
                        }
                    }
                }
            });
        }
    }
    private static void QuickBuff(ILContext il)
    {
        ILCursor cursor = new ILCursor(il);

        if (cursor.TryGotoNext(MoveType.AfterLabel, i => i.MatchLdloca(0), i => i.MatchCall(typeof(SoundStyle?).GetMethod("get_HasValue", ReflectionUtility.DefaultFlags)), i => i.MatchBrfalse(out _)))
        {
            cursor.Emit(OpCodes.Ldarg, 0);
            cursor.Emit(OpCodes.Ldloca, 0);

            cursor.EmitDelegate <QuickBuff_Del>((Player player, ref SoundStyle? sound) =>
            {
                if (!ModContent.GetInstance <PortableStorageConfig>().AlchemistBagQuickBuff)
                {
                    return;
                }

                if (player.CountBuffs() == Player.MaxBuffs)
                {
                    return;
                }

                foreach (AlchemistBag bag in GetAlchemistBags(player))
                {
                    ItemStorage storage = bag.GetItemStorage();

                    for (int i = 0; i < AlchemistBag.PotionSlots; i++)
                    {
                        Item item = storage[i];

                        if (item.IsAir || item.buffType <= 0 || item.DamageType == DamageClass.Summon)
                        {
                            continue;
                        }

                        int buffType = item.buffType;
                        bool canUse  = CombinedHooks.CanUseItem(player, item) && QuickBuff_ShouldBotherUsingThisBuff.Invoke <bool>(player, buffType);
                        if (item.mana > 0 && canUse)
                        {
                            if (player.CheckMana(item, -1, true, true))
                            {
                                player.manaRegenDelay = (int)player.maxRegenDelay;
                            }
                        }

                        if (player.whoAmI == Main.myPlayer && item.type == ItemID.Carrot && !Main.runningCollectorsEdition)
                        {
                            canUse = false;
                        }

                        if (buffType == 27)
                        {
                            buffType = Main.rand.Next(3);
                            if (buffType == 0)
                            {
                                buffType = 27;
                            }
                            if (buffType == 1)
                            {
                                buffType = 101;
                            }
                            if (buffType == 2)
                            {
                                buffType = 102;
                            }
                        }

                        if (!canUse)
                        {
                            continue;
                        }

                        ItemLoader.UseItem(item, player);
                        sound        = item.UseSound;
                        int buffTime = item.buffTime;
                        if (buffTime == 0)
                        {
                            buffTime = 3600;
                        }

                        player.AddBuff(buffType, buffTime);
                        if (item.consumable && ItemLoader.ConsumeItem(item, player))
                        {
                            storage.ModifyStackSize(player, i, -1);
                        }

                        if (player.CountBuffs() == Player.MaxBuffs)
                        {
                            return;
                        }
                    }
                }
            });
        }
    }
    private static void QuickMana(ILContext il)
    {
        ILCursor cursor = new ILCursor(il);
        ILLabel  label  = cursor.DefineLabel();

        if (cursor.TryGotoNext(MoveType.AfterLabel, i => i.MatchLdcI4(0), i => i.MatchStloc(0)))
        {
            cursor.Emit(OpCodes.Ldarg, 0);

            cursor.EmitDelegate <Func <Player, bool> >(player =>
            {
                if (!ModContent.GetInstance <PortableStorageConfig>().AlchemistBagQuickMana)
                {
                    return(false);
                }

                foreach (AlchemistBag bag in GetAlchemistBags(player))
                {
                    ItemStorage storage = bag.GetItemStorage();

                    for (int i = 0; i < AlchemistBag.PotionSlots; i++)
                    {
                        Item item = storage[i];
                        if (item.IsAir || item.healMana <= 0 || (player.potionDelay > 0 && item.potion) || !CombinedHooks.CanUseItem(player, item))
                        {
                            continue;
                        }

                        if (item.UseSound != null)
                        {
                            SoundEngine.PlaySound(item.UseSound.Value, player.position);
                        }
                        if (item.potion)
                        {
                            if (item.type == ItemID.RestorationPotion)
                            {
                                player.potionDelay = player.restorationDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                            else if (item.type == ItemID.Mushroom)
                            {
                                player.potionDelay = player.mushroomDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                            else
                            {
                                player.potionDelay = player.potionDelayTime;
                                player.AddBuff(21, player.potionDelay);
                            }
                        }

                        ItemLoader.UseItem(item, player);
                        int healLife     = player.GetHealLife(item, true);
                        int healMana     = player.GetHealMana(item, true);
                        player.statLife += healLife;
                        player.statMana += healMana;

                        if (player.statLife > player.statLifeMax2)
                        {
                            player.statLife = player.statLifeMax2;
                        }
                        if (player.statMana > player.statManaMax2)
                        {
                            player.statMana = player.statManaMax2;
                        }

                        if (healLife > 0 && Main.myPlayer == player.whoAmI)
                        {
                            player.HealEffect(healLife);
                        }

                        if (healMana > 0)
                        {
                            player.AddBuff(94, Player.manaSickTime);
                            if (Main.myPlayer == player.whoAmI)
                            {
                                player.ManaEffect(healMana);
                            }
                        }

                        if (ItemLoader.ConsumeItem(item, player))
                        {
                            storage.ModifyStackSize(player, i, -1);
                        }

                        Recipe.FindRecipes();

                        return(true);
                    }
                }

                return(false);
            });

            cursor.Emit(OpCodes.Brfalse, label);
            cursor.Emit(OpCodes.Ret);
            cursor.MarkLabel(label);
        }
    }
Example #4
0
    private static void PlayerOnItemCheck_CheckFishingBobber_PickAndConsumeBait(On.Terraria.Player.orig_ItemCheck_CheckFishingBobber_PickAndConsumeBait orig, Player player, Projectile bobber, out bool pullTheBobber, out int baitTypeUsed)
    {
        pullTheBobber = false;
        baitTypeUsed  = 0;
        int foundBaitSlot = -1;

        for (int i = 54; i < 58; i++)
        {
            Item item = player.inventory[i];
            if (item.stack > 0 && item.bait > 0)
            {
                foundBaitSlot = i;
                break;
            }
        }

        if (foundBaitSlot == -1)
        {
            for (int i = 0; i < 50; i++)
            {
                Item item = player.inventory[i];
                if (item.stack > 0 && item.bait > 0)
                {
                    foundBaitSlot = i;
                    break;
                }
            }
        }

        // found bait in player inventory
        if (foundBaitSlot > -1)
        {
            Item  bait = player.inventory[foundBaitSlot];
            bool  flag = false;
            float num2 = 1f + bait.bait / 6f;
            if (num2 < 1f)
            {
                num2 = 1f;
            }

            if (player.accTackleBox)
            {
                num2 += 1f;
            }

            if (Main.rand.NextFloat() * num2 < 1f)
            {
                flag = true;
            }

            if (bobber.localAI[1] == -1f)
            {
                flag = true;
            }

            if (bobber.localAI[1] > 0f)
            {
                Item fishedItem = new Item();
                fishedItem.SetDefaults((int)bobber.localAI[1]);
                if (fishedItem.rare < ItemRarityID.White)
                {
                    flag = false;
                }
            }

            baitTypeUsed = bait.type;
            if (baitTypeUsed == 2673)
            {
                flag = true;
            }

            if (CombinedHooks.CanConsumeBait(player, bait) ?? flag)
            {
                if (bait.type is 4361 or 4362)
                {
                    NPC.LadyBugKilled(player.Center, bait.type == 4362);
                }

                bait.stack--;
                if (bait.stack <= 0)
                {
                    bait.SetDefaults();
                }
            }

            pullTheBobber = true;
        }
        else
        {
            foreach (FishingBelt belt in GetFishingBelts(player))
            {
                ItemStorage storage = belt.GetItemStorage();
                for (int i = 0; i < storage.Count; i++)
                {
                    Item bait = storage[i];
                    if (bait.IsAir || bait.bait <= 0)
                    {
                        continue;
                    }

                    bool  useBait = false;
                    float num2    = 1f + bait.bait / 6f;
                    if (num2 < 1f)
                    {
                        num2 = 1f;
                    }

                    if (player.accTackleBox)
                    {
                        num2 += 1f;
                    }

                    if (Main.rand.NextFloat() * num2 < 1f)
                    {
                        useBait = true;
                    }

                    if (bobber.localAI[1] == -1f)
                    {
                        useBait = true;
                    }

                    if (bobber.localAI[1] > 0f)
                    {
                        Item fishedItem = new Item();
                        fishedItem.SetDefaults((int)bobber.localAI[1]);
                        if (fishedItem.rare < ItemRarityID.White)
                        {
                            useBait = false;
                        }
                    }

                    baitTypeUsed = bait.type;
                    if (baitTypeUsed == 2673)
                    {
                        useBait = true;
                    }

                    if (CombinedHooks.CanConsumeBait(player, bait) ?? useBait)
                    {
                        if (bait.type is 4361 or 4362)
                        {
                            NPC.LadyBugKilled(player.Center, bait.type == 4362);
                        }

                        belt.GetItemStorage().ModifyStackSize(player, i, -1);
                    }

                    pullTheBobber = true;
                }
            }
        }
    }