Exemple #1
0
        public static void ItemSlotDraw(ILContext il)
        {
            ILCursor cursor = new ILCursor(il);

            if (!cursor.TryGotoNext(i => i.MatchLdcI4(-1) && i.Next.Next.MatchLdarg(2) && i.Next.Next.Next.MatchLdcI4(13)))
            {
                throw new Exception("Could not locate int [x] = -1; if (context == 13) in IL.Terraria.UI.ItemSlot.Draw");
            }

            byte indx = (byte)((VariableDefinition)cursor.Next.Next.Operand).Index;

            System.Reflection.MethodInfo callTo = typeof(Int32).GetMethod(nameof(Int32.ToString), new Type[] { });

            if (!cursor.TryGotoNext(i => i.MatchLdloca(indx) && i.Next.MatchCall(callTo)))
            {
                throw new Exception("Could not locate call to ChatManager.DrawColorCodedStringWithShadow");
            }

            cursor.Index += 2;
            cursor.Emit(Ldloc_1);
            cursor.EmitDelegate <Func <string, Item, string> >((ammoCount, weapon) => {
                EndlessAmmoPlayer modPlayer = Main.LocalPlayer.GetModPlayer <EndlessAmmoPlayer>();
                Item ammo = modPlayer.GetItemForEndlessAmmoType(weapon.useAmmo);
                if (modPlayer.useEndlessAmmoFirst)
                {
                    return(ammo.type > ItemID.None ? "Inf" : ammoCount);
                }

                if (ammoCount != "0")
                {
                    return(ammoCount);
                }

                return(ammo.type > ItemID.None ? "Inf" : "0");
            });

            /* Resize text?
             * cursor.Emit(Stloc_S, indx);
             * cursor.Emit(Ldloc_S, indx);
             *
             * if (cursor.TryGotoNext(i => i.MatchLdcR4(0.8f))) {
             *      cursor.Remove();
             *      cursor.Emit(Ldloc_S, indx);
             *      cursor.EmitDelegate<Func<string, float>>((count) => {
             *
             *              if (count != "∞")
             *                      return 0.8f;
             *
             *              return 1.6f;
             *      });
             * }
             */
        }
Exemple #2
0
        public static void HasAmmo(ILContext il)
        {
            ILCursor cursor = new ILCursor(il);

            cursor.Emit(Ldarg_0);
            cursor.Emit(Ldarg_1);
            cursor.EmitDelegate <Func <Player, Item, bool> >((player, item) => {
                EndlessAmmoPlayer modPlayer = player.GetModPlayer <EndlessAmmoPlayer>();
                return(modPlayer.GetItemForEndlessAmmoType(item.useAmmo).type > ItemID.None);
            });

            ILLabel label = il.DefineLabel();

            cursor.Emit(Brfalse, label);
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Ret);
            cursor.MarkLabel(label);
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            EndlessAmmoPlayer modPlayer = Main.LocalPlayer.GetModPlayer <EndlessAmmoPlayer>();

            if (!modPlayer.hasEndlessAmmo)
            {
                base.Draw(spriteBatch);
                return;
            }

            Main.inventoryScale = 0.6f;

            string mouseText = "";
            Item   hoverAmmo = null;

            if (AmmoPicker != AmmoID.None)
            {
                DrawAmmoPicker(spriteBatch, modPlayer, ref mouseText, ref hoverAmmo);
            }
            else
            {
                ScrollPosition.SetValueImmediate(0);
                DrawUnlockedAmmo(spriteBatch, modPlayer, ref mouseText, ref hoverAmmo);
            }

            if (hoverAmmo != null)
            {
                Main.HoverItem            = hoverAmmo.Clone();
                Main.HoverItem.ammo       = 0;
                Main.HoverItem.material   = false;
                Main.HoverItem.consumable = false;
                try {
                    Main.instance.MouseText(hoverAmmo.Name, hoverAmmo.rare, 0);                     // hoverItem.Name, hoverItem.rare, 0
                } catch {}
            }
            else if (mouseText != "")
            {
                Main.instance.MouseText(mouseText);
            }
        }
Exemple #4
0
        public static void PickAmmo(ILContext il)
        {
            ILLabel UseNormalAmmoLabel  = il.DefineLabel();
            ILLabel UseEndlessAmmoLabel = il.DefineLabel();
            ILLabel CanShootLabel       = il.DefineLabel();

            // Item item = new Item();
            // bool flag = false;
            ILCursor cursor = new ILCursor(il);

            if (!cursor.TryGotoNext(i => i.MatchLdcI4(0) && i.Next.MatchStloc(1)))
            {
                throw new Exception("Could not locate flag = false");
            }

            cursor.Index += 2;

            // bool useEndlessAmmoFirst = Delegate(this);
            cursor.Emit(Ldarg_0);             // Player player
            cursor.EmitDelegate <Func <Player, bool> >((player) => {
                EndlessAmmoPlayer modPlayer = player.GetModPlayer <EndlessAmmoPlayer>();
                return(modPlayer.useEndlessAmmoFirst);
            });

            cursor.Emit(Stloc_2);

            // if (useEndlessAmmoFirst == false) goto USE_NORMAL_AMMO;
            cursor.Emit(Ldloc_2);
            cursor.Emit(Brfalse, UseNormalAmmoLabel);

            // USE_ENDLESS_AMMO:
            cursor.MarkLabel(UseEndlessAmmoLabel);

            // item = Delegate(this, sItem);
            cursor.Emit(Ldarg_0);             // Player player
            cursor.Emit(Ldarg_1);             // Item weapon
            cursor.EmitDelegate <Func <Player, Item, Item> >((player, weapon) => {
                EndlessAmmoPlayer modPlayer = player.GetModPlayer <EndlessAmmoPlayer>();
                Item playerAmmo             = modPlayer.GetItemForEndlessAmmoType(weapon.useAmmo);
                return(playerAmmo);
            });
            cursor.Emit(Stloc_0);

            ILLabel IfItemIsAirLabel = il.DefineLabel();

            // if (item.type == 0) goto ITEM_IS_AIR;
            cursor.Emit(Ldloc_0);
            cursor.Emit(Ldfld, typeof(Item).GetField(nameof(Item.type)));
            cursor.Emit(Brfalse, IfItemIsAirLabel);
            // canShoot = true;
            cursor.Emit(Ldarg, 4);
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Stind_I1);
            // dontConsume = true;
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Starg, 7);

            // goto CAN_SHOOT;
            cursor.Emit(Br, CanShootLabel);

            // ITEM_IS_AIR:
            cursor.MarkLabel(IfItemIsAirLabel);

            // if (useEndlessAmmoFirst == false) goto CAN_SHOOT;
            cursor.Emit(Ldloc_2);
            cursor.Emit(Brfalse, CanShootLabel);

            // USE_NORMAL_AMMO:
            cursor.MarkLabel(UseNormalAmmoLabel);

            // ..
            if (!cursor.TryGotoNext(i => i.MatchLdarg(4) && i.Next.MatchLdindU1()))
            {
                throw new Exception("Could not locate canShoot == false conditional.");
            }

            // if (useEndlessAmmoFirst) goto CAN_SHOOT;
            ILLabel SkipAheadLabel = il.DefineLabel();

            cursor.Emit(Ldloc_2);
            cursor.Emit(Brtrue, CanShootLabel);

            // if (item.type == 0) goto USE_ENDLESS_AMMO;
            cursor.Emit(Ldloc_0);
            cursor.Emit(Ldfld, typeof(Item).GetField(nameof(Item.type)));
            cursor.Emit(Brfalse, UseEndlessAmmoLabel);

            // CAN_SHOOT:
            cursor.MarkLabel(CanShootLabel);

            // if (!canShoot) return;
            // ...
        }
        private void DrawAmmoPicker(SpriteBatch spriteBatch, EndlessAmmoPlayer modPlayer, ref string mouseText, ref Item hoverItem)
        {
            Main.HidePlayerCraftingMenu = true;

            int   dx         = LeftX;
            Color titleColor = new Color(Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor);
            Point mousePoint = new Point(Main.mouseX, Main.mouseY);

            Main.LocalPlayer.mouseInterface = true;
            Color locked = new Color(187, 187, 187, 187);

            float stringScale = 0.75f;
            float gap         = 8;

            List <AmmoData> ammoData       = new List <AmmoData>();
            float           widestAmmoName = 0;
            float           stringHeight   = Main.fontMouseText.LineSpacing;

            foreach (Item ammoItem in DataContainer.AmmoItems)
            {
                if (ammoItem.ammo != AmmoPicker)
                {
                    continue;
                }

                bool     unlocked = modPlayer.HasEndlessAmmoItemUnlocked(ammoItem.type);
                int      count    = modPlayer.CountItemsInInventory(ammoItem.type);
                AmmoData data     = new AmmoData(ammoItem, unlocked, count, UNLOCK_AMOUNT);
                ammoData.Add(data);

                widestAmmoName = Math.Max(
                    widestAmmoName,
                    Math.Max(
                        Main.fontMouseText.MeasureString(ammoItem.Name).X,
                        Main.fontMouseText.MeasureString(data.UnlockString).X
                        )
                    );
            }

            Vector2 stringLeftAlignedVertCenter = new Vector2(0, stringHeight / 2);

            widestAmmoName *= stringScale;
            stringHeight   *= stringScale;

            float lineHeight = stringHeight * 0.6f;

            foreach (EndlessAmmoType ammoType in DataContainer.EndlessAmmoTypes)
            {
                if (ammoType.Type == AmmoPicker)
                {
                    ChatManager.DrawColorCodedString(spriteBatch, Main.fontMouseText, ammoType.Title, new Vector2(dx, 84f), titleColor, 0, Vector2.Zero, Vector2.One * 0.75f);
                    break;
                }
            }

            float unit         = 56 * Main.inventoryScale;
            Rect  dropdownRect = new Rect(
                dx,
                105f,
                unit + widestAmmoName + gap,
                Main.inventoryScale * (52 + (56 * Math.Min(3, ammoData.Count - 1)))
                );

            // float scrollMin = 0;
            float scrollTop = (float)(ScrollPosition.GetValue() * unit);
            // float scrollMax = (ammoData.Count - 4) * unit;

            float scrollbarWidth = 20 * Main.inventoryScale;
            bool  drawScrollbar  = ammoData.Count > 4;

            if (drawScrollbar)
            {
                dropdownRect.Width += scrollbarWidth + 4;
            }

            ScalableItemSlot.DrawPanel(spriteBatch, dropdownRect, Main.inventoryScale);

            if (drawScrollbar)
            {
                Rect scrollbarRect = new Rect(dropdownRect.Right - scrollbarWidth - 4, dropdownRect.Y + 4, scrollbarWidth, dropdownRect.Height - 8);

                float offsetHeight = dropdownRect.Height;
                float scrollHeight = Main.inventoryScale * (52 + (56 * (ammoData.Count - 1)));
                Scrollbar.Draw(spriteBatch, scrollbarRect, offsetHeight, scrollHeight, scrollTop, Main.inventoryBack, Main.inventoryScale);

                MaskDrawTo(spriteBatch,
                           1 + (int)(Main.UIScale * dropdownRect.X),
                           1 + (int)(Main.UIScale * dropdownRect.Y),
                           ((int)(Main.UIScale * dropdownRect.Width)) - 2,
                           ((int)(Main.UIScale * dropdownRect.Height)) - 2
                           );
            }

            Rect itemRect = new Rect(dx, 105 - scrollTop, 52, 52);

            itemRect.Dimensions *= Main.inventoryScale;

            Rect textRect = new Rect(itemRect.Position, unit + widestAmmoName, 52);

            textRect.Dimensions *= Main.inventoryScale;

            for (int i = 0; i < ammoData.Count; i++)
            {
                if (itemRect.Bottom < dropdownRect.Y)
                {
                    itemRect.Y += unit;
                    textRect.Y += unit;
                    continue;
                }

                if (itemRect.Y > dropdownRect.Bottom)
                {
                    break;
                }

                AmmoData ammo = ammoData[i];
                ScalableItemSlot.DrawItem(spriteBatch, itemRect, ammo.Item, Main.inventoryBack, Main.inventoryScale);

                Vector2 position = itemRect.ClonePosition();
                position.X += unit;
                position.Y += unit / 2 + 2;

                if (ammo.Unlocked)
                {
                    ChatManager.DrawColorCodedStringWithShadow(
                        spriteBatch,
                        Main.fontMouseText,
                        ammo.Item.Name,
                        position,
                        Color.White,
                        0f,
                        stringLeftAlignedVertCenter,
                        Vector2.One * stringScale
                        );
                }
                else
                {
                    position.Y -= lineHeight / 2;
                    ChatManager.DrawColorCodedStringWithShadow(
                        spriteBatch,
                        Main.fontMouseText,
                        ammo.Item.Name,
                        position,
                        ammo.CanUnlock ? UnlockColor : locked,
                        0f,
                        stringLeftAlignedVertCenter,
                        Vector2.One * stringScale
                        );

                    position.Y += lineHeight;
                    ChatManager.DrawColorCodedStringWithShadow(
                        spriteBatch,
                        Main.fontMouseText,
                        ammo.UnlockString,
                        position,
                        ammo.CanUnlock ? Color.White : locked,
                        0f,
                        stringLeftAlignedVertCenter,
                        Vector2.One * stringScale
                        );
                }

                if (textRect.Contains(mousePoint))
                {
                    if (Main.mouseLeft && Main.mouseLeftRelease)
                    {
                        if (ammo.Unlocked)
                        {
                            if (modPlayer.SelectUnlockedAmmo(ammo.Item.type))
                            {
                                Main.PlaySound(SoundID.Grab);
                            }

                            AmmoPicker = AmmoID.None;
                            break;
                        }

                        if (ammo.CanUnlock && !itemRect.Contains(mousePoint))
                        {
                            if (modPlayer.UnlockEndlessAmmo(ammo.Item.type))
                            {
                                Main.PlaySound(SoundID.Grab);
                            }

                            AmmoPicker = AmmoID.None;
                            break;
                        }
                    }

                    if (itemRect.Contains(mousePoint))
                    {
                        hoverItem = ammo.Item;
                    }
                    else if (ammo.Unlocked)
                    {
                        mouseText = string.Format("Select {0}", ammo.Item.Name);
                    }
                    else if (ammo.CanUnlock)
                    {
                        mouseText = string.Format("Unlock {0}", ammo.Item.Name);
                    }
                    else
                    {
                        mouseText = string.Format("Unlock {0} ({1})", ammo.Item.Name, ammo.UnlockString);
                    }
                }

                itemRect.Y += unit;
                textRect.Y += unit;
            }

            if (AmmoPicker != AmmoID.None && !dropdownRect.Contains(mousePoint) && Main.mouseLeft && Main.mouseLeftRelease)
            {
                Main.PlaySound(SoundID.MenuClose);
                AmmoPicker = AmmoID.None;
            }
            else if (PlayerInput.ScrollWheelDeltaForUI != 0)
            {
                int delta         = PlayerInput.ScrollWheelDeltaForUI < 0 ? 1 : -1;
                int currentTarget = (int)ScrollPosition.Target;
                int newTarget     = Math.Max(0, Math.Min(ammoData.Count - 4, delta + currentTarget));

                if (currentTarget != newTarget)
                {
                    Main.PlaySound(SoundID.MenuTick);
                    ScrollPosition.SetValue(newTarget);
                }

                PlayerInput.ScrollWheelDeltaForUI = 0;
            }

            ResetMask(spriteBatch);
        }
        private void DrawUnlockedAmmo(SpriteBatch spriteBatch, EndlessAmmoPlayer modPlayer, ref string mouseText, ref Item hoverAmmo)
        {
            Vector2 stringScale = Vector2.One * 0.75f;

            int   dx         = LeftX;
            Color titleColor = new Color(Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor);
            Point mousePoint = new Point(Main.mouseX, Main.mouseY);

            string EndlessAmmoLabel = "Endless Ammo";

            ChatManager.DrawColorCodedString(spriteBatch, Main.fontMouseText, EndlessAmmoLabel, new Vector2(dx, 84f), titleColor, 0, Vector2.Zero, stringScale);
            Rect checkboxRect = new Rect(dx, 84, 16, 16);

            checkboxRect.X += (Main.fontMouseText.MeasureString(EndlessAmmoLabel) * stringScale).X + 4;

            EndlessAmmoInventory.SmallItemSlotTexture.Draw(spriteBatch, checkboxRect, Color.White, Main.inventoryScale);
            if (modPlayer.useEndlessAmmoFirst)
            {
                string checkmark = "✓";

                Vector2 stringRect = Main.fontMouseText.MeasureString(checkmark) * stringScale;
                ChatManager.DrawColorCodedStringWithShadow(
                    spriteBatch,
                    Main.fontMouseText,
                    checkmark,
                    checkboxRect.Center(),
                    Color.White,
                    0f,
                    stringRect / 2,
                    stringScale
                    );
            }

            if (checkboxRect.Contains(mousePoint))
            {
                Main.LocalPlayer.mouseInterface = true;
                mouseText = "Use endless ammo first.";

                if (Main.mouseLeft && Main.mouseLeftRelease)
                {
                    Main.PlaySound(SoundID.MenuTick);
                    modPlayer.useEndlessAmmoFirst = !modPlayer.useEndlessAmmoFirst;
                }
            }

            Rect slotRect = new Rect(52, 52);

            slotRect.Dimensions *= Main.inventoryScale;

            int typeCount = DataContainer.EndlessAmmoTypes.Length;

            for (int i = 0; i < typeCount; i++)
            {
                int x = i / 4;
                int y = i % 4;

                slotRect.X = dx + x * DeltaX;
                slotRect.Y = (int)(105f + y * 56 * Main.inventoryScale);

                EndlessAmmoType AmmoType = DataContainer.EndlessAmmoTypes[i];
                Item            ammo     = modPlayer.GetItemForEndlessAmmoType(AmmoType.Type);

                if (ammo.type == ItemID.None)
                {
                    Item refItem = new Item();
                    refItem.SetDefaults(AmmoType.PreviewItemType);
                    ScalableItemSlot.DrawPanel(spriteBatch, slotRect, Main.inventoryScale);
                    ScalableItemSlot.DrawItem(spriteBatch, slotRect, refItem, PreviewColor, Main.inventoryScale);
                }
                else
                {
                    ScalableItemSlot.DrawPanel(spriteBatch, slotRect, Main.inventoryScale);
                    ScalableItemSlot.DrawItem(spriteBatch, slotRect, ammo, Main.inventoryBack, Main.inventoryScale);
                }

                if (modPlayer.CanUnlockAmmoForType(AmmoType.Type))
                {
                    Vector2 hPosition = slotRect.ClonePosition();
                    hPosition.X += 24;
                    hPosition.Y += 4;
                    ChatManager.DrawColorCodedStringWithShadow(
                        spriteBatch,
                        Main.fontMouseText,
                        "!",
                        hPosition,
                        UnlockColor,
                        0f,
                        Vector2.Zero,
                        Vector2.One * 0.6f
                        );
                }

                if (AmmoPicker == AmmoID.None && slotRect.Contains(mousePoint))
                {
                    Main.LocalPlayer.mouseInterface = true;
                    if (ammo.type != ItemID.None)
                    {
                        hoverAmmo = ammo;
                    }
                    else
                    {
                        mouseText = DataContainer.EndlessAmmoTypes[i].NoSelected;
                    }

                    if (Main.mouseLeft && Main.mouseLeftRelease)
                    {
                        Main.PlaySound(SoundID.MenuOpen);
                        AmmoPicker = AmmoType.Type;
                    }
                }
            }
        }