public override void Process(int whoAmI, byte[] readBuffer, int length, int num)
        {
            int playerId = readBuffer[num++]; //TODO: maybe check for forgery
            byte action = readBuffer[num];
            if (action == 1)
            {
                var player = Main.players[whoAmI];

                var ctx = new HookContext
                {
                    Connection = player.Connection,
                    Sender = player,
                    Player = player,
                };

                var args = new HookArgs.PlayerTriggeredEvent
                {
                    Type = WorldEventType.BOSS,
                    Name = "Skeletron",
                };

                HookPoints.PlayerTriggeredEvent.Invoke (ref ctx, ref args);

                if (ctx.CheckForKick () || ctx.Result == HookResult.IGNORE)
                    return;

                ProgramLog.Users.Log ("{0} @ {1}: Skeletron summoned by {2}.", player.IPAddress, whoAmI, player.Name);
                NetMessage.SendData (Packet.PLAYER_CHAT, -1, -1, string.Concat (player.Name, " has summoned Skeletron!"), 255, 255, 128, 150);
                NPC.SpawnSkeletron();
            }
            else if (action == 2)
            {
                NetMessage.SendData (51, -1, whoAmI, "", playerId, action, 0f, 0f, 0);
            }
        }
        public void ItemCheck(int i)
        {
            int dmg = this.inventory[this.selectedItemIndex].damage;
            if (dmg > 0)
            {
                if (this.inventory[this.selectedItemIndex].Melee)
                {
                    dmg = (int)((float)dmg * this.meleeDamage);
                }
                else
                {
                    if (this.inventory[this.selectedItemIndex].Ranged)
                    {
                        dmg = (int)((float)dmg * this.rangedDamage);
                    }
                    else
                    {
                        if (this.inventory[this.selectedItemIndex].Magic)
                        {
                            dmg = (int)((float)dmg * this.magicDamage);
                        }
                    }
                }
            }

            Item selectedItem = inventory[selectedItemIndex];
            if (selectedItem.AutoReuse && !this.noItems)
            {
                releaseUseItem = true;
                if (itemAnimation == 1 && selectedItem.Stack > 0)
                {
                    itemAnimation = 0;
                }
            }

            if (controlUseItem && itemAnimation == 0 && releaseUseItem && selectedItem.UseStyle > 0)
            {
                bool flag = true;
                if (this.noItems)
                {
                    flag = false;
                }
                if (selectedItem.Shoot == ProjectileType.BOOMERANG_ENCHANTED || selectedItem.Shoot == ProjectileType.FLAMARANG || selectedItem.Shoot == ProjectileType.CHAKRUM_THORN)
                {
                    for (int j = 0; j < Main.maxProjectiles; j++)
                    {
                        if (Main.projectile[j].Active && Main.projectile[j].Owner == Main.myPlayer && Main.projectile[j].type == selectedItem.Shoot)
                        {
                            flag = false;
                        }
                    }
                }
                if (selectedItem.Shoot == ProjectileType.HOOK || selectedItem.Shoot == ProjectileType.WHIP_IVY)
                {
                    for (int k = 0; k < 1000; k++)
                    {
                        if (Main.projectile[k].Active && Main.projectile[k].Owner == Main.myPlayer && Main.projectile[k].type == selectedItem.Shoot && Main.projectile[k].ai[0] != 2f)
                        {
                            flag = false;
                        }
                    }
                }
                if (selectedItem.Potion && flag)
                {
                    if (this.potionDelay <= 0)
                    {
                        potionDelay = Item.POTION_DELAY;
                        this.AddBuff(21, this.potionDelay, true);
                    }
                    else
                    {
                        flag = false;
                    }
                }

                if (selectedItem.Mana > 0 && flag)
                {
                    if (selectedItem.Type != 127 || !spaceGun)
                    {
                        if (statMana >= (int)((float)selectedItem.Mana * manaCost))
                        {
                            statMana -= (int)((float)selectedItem.Mana * manaCost);
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }

                if (selectedItem.Type == 43 && Main.dayTime)
                {
                    flag = false;
                }
                if (selectedItem.Type == 70 && !this.zoneEvil)
                {
                    flag = false;
                }

                if (flag)
                {
                    if (selectedItem.UseAmmo > 0)
                    {
                        flag = false;
                        for (int l = 0; l < 44; l++)
                        {
                            if (l < 4 && this.ammo[l].Ammo == selectedItem.UseAmmo && this.ammo[l].Stack > 0)
                            {
                                flag = true;
                                break;
                            }
                            if (this.inventory[l].Ammo == selectedItem.UseAmmo && this.inventory[l].Stack > 0)
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                }

                if (flag)
                {
                    if (grappling[0] > -1)
                    {
                        if (controlRight)
                        {
                            direction = 1;
                        }
                        else if (controlLeft)
                        {
                            direction = -1;
                        }
                    }

                    channel = selectedItem.Channel;
                    attackCD = 0;

                    if (selectedItem.Melee)
                    {
                        this.itemAnimation = (int)((float)selectedItem.UseAnimation * this.meleeSpeed);
                        this.itemAnimationMax = (int)((float)selectedItem.UseAnimation * this.meleeSpeed);
                    }
                    else
                    {
                        this.itemAnimation = selectedItem.UseAnimation;
                        this.itemAnimationMax = selectedItem.UseAnimation;
                    }
                }

                if (flag && selectedItem.Shoot == ProjectileType.ORB_OF_LIGHT)
                {
                    for (int j = 0; j < 1000; j++)
                    {
                        if (Main.projectile[j].Active && Main.projectile[j].Owner == i && Main.projectile[j].type == selectedItem.Shoot)
                        {
                            Main.projectile[j].Kill();
                        }
                    }
                }
            }

            if (!this.controlUseItem)
            {
                this.channel = false;
            }

            if (this.itemAnimation > 0)
            {
                if (selectedItem.Mana > 0)
                {
                    this.manaRegenDelay = (int)this.maxRegenDelay;
                }

                if (Main.dedServ)
                {
                    itemHeight = selectedItem.Height;
                    itemWidth = selectedItem.Width;
                }
                itemAnimation--;
            }
            else if (selectedItem.HoldStyle == 1)
            {
                if (Main.dedServ)
                {
                    this.itemLocation.X = this.Position.X + (float)this.Width * 0.5f + 20f * (float)this.direction;
                }
                this.itemLocation.Y = this.Position.Y + 24f;
                this.itemRotation = 0f;
                if (this.gravDir == -1f)
                {
                    this.itemRotation = -this.itemRotation;
                    this.itemLocation.Y = this.Position.Y + (float)this.Height + (this.Position.Y - this.itemLocation.Y);
                }
            }
            else if (selectedItem.HoldStyle == 2)
            {
                this.itemLocation.X = this.Position.X + (float)this.Width * 0.5f + (float)(6 * this.direction);
                this.itemLocation.Y = this.Position.Y + 16f;
                this.itemRotation = 0.79f * (float)(-(float)this.direction);
                if (this.gravDir == -1f)
                {
                    this.itemRotation = -this.itemRotation;
                    this.itemLocation.Y = this.Position.Y + (float)this.Height + (this.Position.Y - this.itemLocation.Y);
                }
            }

            releaseUseItem = !controlUseItem;

            if (this.itemTime > 0)
            {
                this.itemTime--;
            }
            if (i == Main.myPlayer)
            {
                if (selectedItem.Shoot > 0 && itemAnimation > 0 && itemTime == 0)
                {
                    ProjectileType shoot = selectedItem.Shoot;
                    float shootSpeed = selectedItem.ShootSpeed;

                    if (selectedItem.Melee && shoot != ProjectileType.BALL_O_HURT && shoot != ProjectileType.BLUE_MOON && shoot != ProjectileType.SUNFURY)
                    {
                        shootSpeed /= this.meleeSpeed;
                    }

                    bool flag2 = false;
                    int damage = dmg;
                    float knockBack = selectedItem.KnockBack;
                    if (shoot == ProjectileType.HOOK || shoot == ProjectileType.WHIP_IVY)
                    {
                        grappling[0] = -1;
                        grapCount = 0;
                        for (int j = 0; j < Main.maxProjectiles; j++)
                        {
                            if (Main.projectile[j].Active && Main.projectile[j].Owner == i)
                            {
                                if (Main.projectile[j].type == ProjectileType.HOOK)
                                {
                                    Main.projectile[j].Kill();
                                }
                            }
                        }
                    }

                    if (selectedItem.UseAmmo > 0)
                    {
                        Item item = null;
                        bool flag3 = false;
                        for (int num12 = 0; num12 < 4; num12++)
                        {
                            if (this.ammo[num12].Ammo == selectedItem.UseAmmo && this.ammo[num12].Stack > 0)
                            {
                                item = this.ammo[num12];
                                flag2 = true;
                                flag3 = true;
                                break;
                            }
                        }
                        if (!flag3)
                        {
                            for (int num13 = 0; num13 < 44; num13++)
                            {
                                if (this.inventory[num13].Ammo == selectedItem.UseAmmo && this.inventory[num13].Stack > 0)
                                {
                                    item = this.inventory[num13];
                                    flag2 = true;
                                    break;
                                }
                            }
                        }
                        if (flag2)
                        {
                            if (item != null && item.Shoot != 0)
                            {
                                shoot = item.Shoot;
                            }
                            shootSpeed += item.ShootSpeed;
                            if (item.Ranged)
                            {
                                damage += (int)((float)item.Damage * this.rangedDamage);
                            }
                            else
                            {
                                damage += item.damage;
                            }

                            if (selectedItem.UseAmmo == ProjectileType.ARROW_WOODEN && this.archery)
                            {
                                if (shootSpeed < 20f)
                                {
                                    shootSpeed *= 1.2f;
                                    if (shootSpeed > 20f)
                                    {
                                        shootSpeed = 20f;
                                    }
                                }
                                damage = (int)((double)((float)damage) * 1.2);
                            }
                            if (item != null) knockBack += item.KnockBack;
                            bool flag4 = false;
                            if (selectedItem.Type == 98 && Main.rand.Next(3) == 0)
                            {
                                flag4 = true;
                            }
                            if (this.ammoCost80 && Main.rand.Next(5) == 0)
                            {
                                flag4 = true;
                            }
                            if (!flag4 && item != null)
                            {
                                item.Stack--;
                                if (item.Stack <= 0)
                                {
                                    item.Active = false;
                                    item.Name = "";
                                    item.Type = 0;
                                }
                            }
                        }
                    }
                    else
                    {
                        flag2 = true;
                    }

                    if (flag2)
                    {
                        if (damage == 1 && selectedItem.Type == 120)
                        {
                            damage = 2;
                        }

                        // client-code snipped here
                    }
                    else if (selectedItem.UseStyle == 5)
                    {
                        itemRotation = 0f;
                        //NetMessage.SendData(41, -1, -1, "", this.whoAmi);
                    }
                }

            // client code depending on tileTargetX/Y
            //                if (selectedItem.Type >= 205 && selectedItem.Type <= 207)
            //                {
            //                    if (Position.X / 16f - (float)Player.tileRangeX - (float)selectedItem.TileBoost <= (float)Player.tileTargetX
            //                        && (Position.X + (float)Width) / 16f + (float)Player.tileRangeX + (float)selectedItem.TileBoost - 1f >= (float)Player.tileTargetX
            //                        && Position.Y / 16f - (float)Player.tileRangeY - (float)selectedItem.TileBoost <= (float)Player.tileTargetY
            //                        && (this.Position.Y + (float)this.Height) / 16f + (float)Player.tileRangeY + (float)selectedItem.TileBoost - 2f >= (float)Player.tileTargetY)
            //                    {
            //                        showItemIcon = true;
            //
            //                        if (itemTime == 0 && itemAnimation > 0 && controlUseItem)
            //                        {
            //                            if (selectedItem.Type == 205)
            //                            {
            //                                bool lava = Main.tile.At(Player.tileTargetX, Player.tileTargetY).Lava;
            //                                int num10 = 0;
            //                                for (int x = Player.tileTargetX - 1; x <= Player.tileTargetX + 1; x++)
            //                                {
            //                                    for (int y = Player.tileTargetY - 1; y <= Player.tileTargetY + 1; y++)
            //                                    {
            //                                        if (Main.tile.At(x, y).Lava == lava)
            //                                        {
            //                                            num10 += (int)Main.tile.At(x, y).Liquid;
            //                                        }
            //                                    }
            //                                }
            //
            //                                if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Liquid > 0 && num10 > 100)
            //                                {
            //                                    bool lava2 = Main.tile.At(Player.tileTargetX, Player.tileTargetY).Lava;
            //                                    if (!Main.tile.At(Player.tileTargetX, Player.tileTargetY).Lava)
            //                                    {
            //                                        selectedItem = Registries.Item.Create(206);
            //                                    }
            //                                    else
            //                                    {
            //                                        selectedItem = Registries.Item.Create(207);
            //                                    }
            //                                    inventory[selectedItemIndex] = selectedItem;
            //
            //                                    itemTime = selectedItem.UseTime;
            //                                    int num11 = (int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Liquid;
            //                                    Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLiquid (0);
            //                                    Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLava (false);
            //                                    WorldModify.SquareTileFrame(Player.tileTargetX, Player.tileTargetY, false);
            //
            //                                    Liquid.AddWater(Player.tileTargetX, Player.tileTargetY);
            //
            //                                    for (int x = Player.tileTargetX - 1; x <= Player.tileTargetX + 1; x++)
            //                                    {
            //                                        for (int y = Player.tileTargetY - 1; y <= Player.tileTargetY + 1; y++)
            //                                        {
            //                                            if (num11 < 256 && Main.tile.At(x, y).Lava == lava)
            //                                            {
            //                                                int num12 = (int)Main.tile.At(x, y).Liquid;
            //
            //                                                if (num12 + num11 > 255)
            //                                                {
            //                                                    num12 = 255 - num11;
            //                                                }
            //
            //                                                num11 += num12;
            //                                                TileRef expr_20A0 = Main.tile.At(x, y);
            //                                                expr_20A0.SetLiquid ((byte) (expr_20A0.Liquid - (byte)num12));
            //                                                Main.tile.At(x, y).SetLava (lava2);
            //
            //                                                if (Main.tile.At(x, y).Liquid == 0)
            //                                                {
            //                                                    Main.tile.At(x, y).SetLava (false);
            //                                                }
            //
            //                                                WorldModify.SquareTileFrame(x, y, false);
            //
            //                                                Liquid.AddWater(x, y);
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                            else if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Liquid < 200)
            //                            {
            //                                if (!Main.tile.At(Player.tileTargetX, Player.tileTargetY).Active || !Main.tileSolid[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type] || !Main.tileSolidTop[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type])
            //                                {
            //                                    if (selectedItem.Type == 207)
            //                                    {
            //                                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Liquid == 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Lava)
            //                                        {
            //                                            Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLava (true);
            //                                            Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLiquid (255);
            //                                            WorldModify.SquareTileFrame(Player.tileTargetX, Player.tileTargetY, true);
            //                                            selectedItem = Registries.Item.Create(205);
            //                                            inventory[selectedItemIndex] = selectedItem;
            //                                            this.itemTime = selectedItem.UseTime;
            //                                        }
            //                                    }
            //                                    else if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Liquid == 0 || !Main.tile.At(Player.tileTargetX, Player.tileTargetY).Lava)
            //                                    {
            //                                        Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLava (false);
            //                                        Main.tile.At(Player.tileTargetX, Player.tileTargetY).SetLiquid (255);
            //                                        WorldModify.SquareTileFrame(Player.tileTargetX, Player.tileTargetY, true);
            //                                        selectedItem = Registries.Item.Create(205);
            //                                        inventory[selectedItemIndex] = selectedItem;
            //                                        this.itemTime = selectedItem.UseTime;
            //                                    }
            //                                }
            //                            }
            //                        }
            //                    }
            //                }
            //
            //                if (selectedItem.Pick > 0 || selectedItem.Axe > 0 || selectedItem.Hammer > 0)
            //                {
            //                    if (Position.X / 16f - (float)Player.tileRangeX - (float)selectedItem.TileBoost <= (float)Player.tileTargetX
            //                        && (Position.X + (float)Width) / 16f + (float)Player.tileRangeX + (float)selectedItem.TileBoost - 1f >= (float)Player.tileTargetX
            //                        && Position.Y / 16f - (float)Player.tileRangeY - (float)selectedItem.TileBoost <= (float)Player.tileTargetY
            //                        && (Position.Y + (float)Height) / 16f + (float)Player.tileRangeY + (float)selectedItem.TileBoost - 2f >= (float)Player.tileTargetY)
            //                    {
            //                        showItemIcon = true;
            //                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Active)
            //                        {
            //                            if (itemTime == 0 && itemAnimation > 0 && controlUseItem)
            //                            {
            //
            //                                if (hitTileX != Player.tileTargetX || hitTileY != Player.tileTargetY)
            //                                {
            //                                    hitTile = 0;
            //                                    hitTileX = Player.tileTargetX;
            //                                    hitTileY = Player.tileTargetY;
            //                                }
            //                                if (Main.tileNoFail[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type])
            //                                {
            //                                    hitTile = 100;
            //                                }
            //                                if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type != 27)
            //                                {
            //                                    if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 4 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 10 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 11 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 12 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 13 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 14 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 15 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 16 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 17 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 18 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 19 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 21 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 26 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 28 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 29 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 31 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 33 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 34 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 35 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 36 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 42 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 48 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 49 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 50 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 54 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 55 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 77 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 78 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 79)
            //                                    {
            //                                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 48)
            //                                        {
            //                                            hitTile += selectedItem.Hammer / 3;
            //                                        }
            //                                        else
            //                                        {
            //                                            hitTile += selectedItem.Hammer;
            //                                        }
            //
            //                                        if ((double)Player.tileTargetY > Main.rockLayer
            //                                            && Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 77
            //                                            && selectedItem.Hammer < 60)
            //                                        {
            //                                            hitTile = 0;
            //                                        }
            //
            //                                        if (selectedItem.Hammer > 0)
            //                                        {
            //                                            if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 26)
            //                                            {
            //                                                Hurt(this.statLife / 2, -direction, false, false);
            //                                                WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, true, false, false);
            //                                            }
            //                                            else if (hitTile >= 100)
            //                                            {
            //                                                hitTile = 0;
            //                                                WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, false, false, false);
            //                                            }
            //                                            else
            //                                            {
            //                                                WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, true, false, false);
            //                                            }
            //
            //                                            itemTime = inventory[this.selectedItemIndex].UseTime;
            //                                        }
            //                                    }
            //                                    else
            //                                    {
            //                                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 5
            //                                            || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 30
            //                                            || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 72)
            //                                        {
            //                                            if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 30)
            //                                            {
            //                                                hitTile += selectedItem.Axe * 5;
            //                                            }
            //                                            else
            //                                            {
            //                                                hitTile += selectedItem.Axe;
            //                                            }
            //                                            if (selectedItem.Axe > 0)
            //                                            {
            //                                                if (hitTile >= 100)
            //                                                {
            //                                                    hitTile = 0;
            //                                                    WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, false, false, false);
            //                                                }
            //                                                else
            //                                                {
            //                                                    WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, true, false, false);
            //                                                }
            //                                                this.itemTime = selectedItem.UseTime;
            //                                            }
            //                                        }
            //                                        else
            //                                        {
            //                                            if (selectedItem.Pick > 0)
            //                                            {
            //                                                if (Main.tileDungeon[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type] || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 37 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 25 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 58)
            //                                                {
            //                                                    hitTile += selectedItem.Pick / 2;
            //                                                }
            //                                                else
            //                                                {
            //                                                    hitTile += selectedItem.Pick;
            //                                                }
            //                                                if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 25 && selectedItem.Pick < 65)
            //                                                {
            //                                                    hitTile = 0;
            //                                                }
            //                                                else
            //                                                {
            //                                                    if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 37 && selectedItem.Pick < 55)
            //                                                    {
            //                                                        hitTile = 0;
            //                                                    }
            //                                                    else
            //                                                    {
            //                                                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 56 && selectedItem.Pick < 65)
            //                                                        {
            //                                                            hitTile = 0;
            //                                                        }
            //                                                        else
            //                                                        {
            //                                                            if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 58 && selectedItem.Pick < 65)
            //                                                            {
            //                                                                hitTile = 0;
            //                                                            }
            //                                                            else
            //                                                            {
            //                                                                if (Main.tileDungeon[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type] && selectedItem.Pick < 65)
            //                                                                {
            //                                                                    if ((double)Player.tileTargetX < (double)Main.maxTilesX * 0.25 || (double)Player.tileTargetX > (double)Main.maxTilesX * 0.75)
            //                                                                    {
            //                                                                        hitTile = 0;
            //                                                                    }
            //                                                                }
            //                                                            }
            //                                                        }
            //                                                    }
            //                                                }
            //                                                if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 40 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 53 || Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 59)
            //                                                {
            //                                                    hitTile += selectedItem.Pick;
            //                                                }
            //                                                if (hitTile >= 100)
            //                                                {
            //                                                    hitTile = 0;
            //                                                    WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, false, false, false);
            //                                                }
            //                                                else
            //                                                {
            //                                                    WorldModify.KillTile(Player.tileTargetX, Player.tileTargetY, true, false, false);
            //                                                }
            //                                                this.itemTime = selectedItem.UseTime;
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                        }
            //                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Wall > 0)
            //                        {
            //                            if (this.itemTime == 0 && this.itemAnimation > 0 && this.controlUseItem)
            //                            {
            //                                if (selectedItem.Hammer > 0)
            //                                {
            //                                    bool flag3 = true;
            //                                    if (!Main.wallHouse[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Wall])
            //                                    {
            //                                        flag3 = false;
            //                                        for (int k = Player.tileTargetX - 1; k < Player.tileTargetX + 2; k++)
            //                                        {
            //                                            for (int l = Player.tileTargetY - 1; l < Player.tileTargetY + 2; l++)
            //                                            {
            //                                                if (Main.tile.At(k, l).Wall != Main.tile.At(Player.tileTargetX, Player.tileTargetY).Wall)
            //                                                {
            //                                                    flag3 = true;
            //                                                    break;
            //                                                }
            //                                            }
            //                                        }
            //                                    }
            //                                    if (flag3)
            //                                    {
            //                                        if (hitTileX != Player.tileTargetX || hitTileY != Player.tileTargetY)
            //                                        {
            //                                            hitTile = 0;
            //                                            hitTileX = Player.tileTargetX;
            //                                            hitTileY = Player.tileTargetY;
            //                                        }
            //                                        hitTile += selectedItem.Hammer;
            //                                        if (hitTile >= 100)
            //                                        {
            //                                            hitTile = 0;
            //                                            WorldModify.KillWall(Player.tileTargetX, Player.tileTargetY, false);
            //                                        }
            //                                        else
            //                                        {
            //                                            WorldModify.KillWall(Player.tileTargetX, Player.tileTargetY, true);
            //                                        }
            //                                        this.itemTime = selectedItem.UseTime;
            //                                    }
            //                                }
            //                            }
            //                        }
            //                    }
            //                }
                if (selectedItem.Type == 29 && this.itemAnimation > 0 && this.statLifeMax < 400 && this.itemTime == 0)
                {
                    if (this.itemTime == 0)
                    {
                        this.itemTime = selectedItem.UseTime;
                        this.statLifeMax += 20;
                        this.statLife += 20;
                        if (Main.myPlayer == this.whoAmi)
                        {
                            this.HealEffect(20);
                        }
                    }
                }
                if (selectedItem.Type == 109 && this.itemAnimation > 0 && this.statManaMax < 200 && this.itemTime == 0)
                {
                    if (this.itemTime == 0)
                    {
                        this.itemTime = selectedItem.UseTime;
                        this.statManaMax += 20;
                        this.statMana += 20;
                        if (Main.myPlayer == this.whoAmi)
                        {
                            this.ManaEffect(20);
                        }
                    }
                }
            // client code (tileTargetX/Y), not updated
            //                if (selectedItem.CreateTile >= 0)
            //                {
            //                    if (this.Position.X / 16f - (float)Player.tileRangeX - (float)selectedItem.TileBoost <= (float)Player.tileTargetX && (this.Position.X + (float)this.Width) / 16f + (float)Player.tileRangeX + (float)selectedItem.TileBoost - 1f >= (float)Player.tileTargetX && this.Position.Y / 16f - (float)Player.tileRangeY - (float)selectedItem.TileBoost <= (float)Player.tileTargetY && (this.Position.Y + (float)this.Height) / 16f + (float)Player.tileRangeY + (float)selectedItem.TileBoost - 2f >= (float)Player.tileTargetY)
            //                    {
            //                        this.showItemIcon = true;
            //                        if (!Main.tile.At(Player.tileTargetX, Player.tileTargetY).Active || selectedItem.CreateTile == 23 || selectedItem.CreateTile == 2 || selectedItem.CreateTile == 60 || selectedItem.CreateTile == 70)
            //                        {
            //                            if (this.itemTime == 0 && this.itemAnimation > 0 && this.controlUseItem)
            //                            {
            //                                bool flag4 = false;
            //                                if (selectedItem.CreateTile == 23 || selectedItem.CreateTile == 2)
            //                                {
            //                                    if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Active && Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 0)
            //                                    {
            //                                        flag4 = true;
            //                                    }
            //                                }
            //                                else
            //                                {
            //                                    if (selectedItem.CreateTile == 60 || selectedItem.CreateTile == 70)
            //                                    {
            //                                        if (Main.tile.At(Player.tileTargetX, Player.tileTargetY).Active && Main.tile.At(Player.tileTargetX, Player.tileTargetY).Type == 59)
            //                                        {
            //                                            flag4 = true;
            //                                        }
            //                                    }
            //                                    else
            //                                    {
            //                                        if (selectedItem.CreateTile == 4)
            //                                        {
            //                                            int num13 = (int)Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Type;
            //                                            int num14 = (int)Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Type;
            //                                            int num15 = (int)Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Type;
            //                                            int num16 = (int)Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY - 1).Type;
            //                                            int num17 = (int)Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY - 1).Type;
            //                                            int num18 = (int)Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY - 1).Type;
            //                                            int num19 = (int)Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY + 1).Type;
            //                                            if (!Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active)
            //                                            {
            //                                                num13 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Active)
            //                                            {
            //                                                num14 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Active)
            //                                            {
            //                                                num15 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY - 1).Active)
            //                                            {
            //                                                num16 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY - 1).Active)
            //                                            {
            //                                                num17 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY + 1).Active)
            //                                            {
            //                                                num18 = -1;
            //                                            }
            //                                            if (!Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY + 1).Active)
            //                                            {
            //                                                num19 = -1;
            //                                            }
            //                                            if (num13 >= 0 && Main.tileSolid[num13] && !Main.tileNoAttach[num13])
            //                                            {
            //                                                flag4 = true;
            //                                            }
            //                                            else
            //                                            {
            //                                                if ((num14 >= 0 && Main.tileSolid[num14] && !Main.tileNoAttach[num14]) || (num14 == 5 && num16 == 5 && num18 == 5))
            //                                                {
            //                                                    flag4 = true;
            //                                                }
            //                                                else
            //                                                {
            //                                                    if ((num15 >= 0 && Main.tileSolid[num15] && !Main.tileNoAttach[num15]) || (num15 == 5 && num17 == 5 && num19 == 5))
            //                                                    {
            //                                                        flag4 = true;
            //                                                    }
            //                                                }
            //                                            }
            //                                        }
            //                                        else
            //                                        {
            //                                            if (selectedItem.CreateTile == 78)
            //                                            {
            //                                                if (Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active && (Main.tileSolid[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Type] || Main.tileTable[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Type]))
            //                                                {
            //                                                    flag4 = true;
            //                                                }
            //                                            }
            //                                            else
            //                                            {
            //                                                if (selectedItem.CreateTile == 13 || selectedItem.CreateTile == 29 || selectedItem.CreateTile == 33 || selectedItem.CreateTile == 49)
            //                                                {
            //                                                    if (Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active && Main.tileTable[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Type])
            //                                                    {
            //                                                        flag4 = true;
            //                                                    }
            //                                                }
            //                                                else
            //                                                {
            //                                                    if (selectedItem.CreateTile == 51)
            //                                                    {
            //                                                        if (Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Active || Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Wall > 0 || Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Active || Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Wall > 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active || Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Wall > 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Active || Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Wall > 0)
            //                                                        {
            //                                                            flag4 = true;
            //                                                        }
            //                                                    }
            //                                                    else
            //                                                    {
            //                                                        if ((Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Active && Main.tileSolid[(int)Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Type]) || Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Wall > 0 || (Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Active && Main.tileSolid[(int)Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Type]) || Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Wall > 0 || (Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active && Main.tileSolid[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Type]) || Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Wall > 0 || (Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Active && Main.tileSolid[(int)Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Type]) || Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Wall > 0)
            //                                                        {
            //                                                            flag4 = true;
            //                                                        }
            //                                                    }
            //                                                }
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                                if (flag4)
            //                                {
            //                                    if (WorldModify.PlaceTile(Player.tileTargetX, Player.tileTargetY, selectedItem.CreateTile, false, false, this.whoAmi))
            //                                    {
            //                                        this.itemTime = selectedItem.UseTime;
            //                                        if (selectedItem.CreateTile == 15)
            //                                        {
            //                                            if (this.direction == 1)
            //                                            {
            //                                                TileRef expr_40C8 = Main.tile.At(Player.tileTargetX, Player.tileTargetY);
            //                                                expr_40C8.SetFrameX ((short) (expr_40C8.FrameX + 18));
            //                                                TileRef expr_40ED = Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1);
            //                                                expr_40ED.SetFrameX ((short) (expr_40ED.FrameX + 18));
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                        }
            //                    }
            //                }
            //                if (selectedItem.CreateWall >= 0)
            //                {
            //                    if (this.Position.X / 16f - (float)Player.tileRangeX - (float)selectedItem.TileBoost <= (float)Player.tileTargetX && (this.Position.X + (float)this.Width) / 16f + (float)Player.tileRangeX + (float)selectedItem.TileBoost - 1f >= (float)Player.tileTargetX && this.Position.Y / 16f - (float)Player.tileRangeY - (float)selectedItem.TileBoost <= (float)Player.tileTargetY && (this.Position.Y + (float)this.Height) / 16f + (float)Player.tileRangeY + (float)selectedItem.TileBoost - 2f >= (float)Player.tileTargetY)
            //                    {
            //                        this.showItemIcon = true;
            //                        if (this.itemTime == 0 && this.itemAnimation > 0 && this.controlUseItem)
            //                        {
            //                            if (Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Active || Main.tile.At(Player.tileTargetX + 1, Player.tileTargetY).Wall > 0 || Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Active || Main.tile.At(Player.tileTargetX - 1, Player.tileTargetY).Wall > 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Active || Main.tile.At(Player.tileTargetX, Player.tileTargetY + 1).Wall > 0 || Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Active || Main.tile.At(Player.tileTargetX, Player.tileTargetY - 1).Wall > 0)
            //                            {
            //                                if ((int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Wall != selectedItem.CreateWall)
            //                                {
            //                                    WorldModify.PlaceWall(Player.tileTargetX, Player.tileTargetY, selectedItem.CreateWall, false);
            //                                    if ((int)Main.tile.At(Player.tileTargetX, Player.tileTargetY).Wall == selectedItem.CreateWall)
            //                                    {
            //                                        this.itemTime = selectedItem.UseTime;
            //                                    }
            //                                }
            //                            }
            //                        }
            //                    }
            //                }
            }
            if (selectedItem.Damage >= 0 && selectedItem.Type > 0 && !selectedItem.NoMelee)
            {
                if (this.itemAnimation > 0)
                {
                    bool flag5 = false;
                    Rectangle rectangle = new Rectangle((int)this.itemLocation.X, (int)this.itemLocation.Y, 32, 32);
                    rectangle.Width = (int)((float)rectangle.Width * selectedItem.Scale);
                    rectangle.Height = (int)((float)rectangle.Height * selectedItem.Scale);
                    if (this.direction == -1)
                    {
                        rectangle.X -= rectangle.Width;
                    }
                    if (this.gravDir == 1f)
                    {
                        rectangle.Y -= rectangle.Height;
                    }
                    rectangle.Y -= rectangle.Height;
                    if (selectedItem.UseStyle == 1)
                    {
                        if ((double)this.itemAnimation < (double)this.itemAnimationMax * 0.333)
                        {
                            if (this.direction == -1)
                            {
                                rectangle.X -= (int)((double)rectangle.Width * 1.4 - (double)rectangle.Width);
                            }
                            rectangle.Width = (int)((double)rectangle.Width * 1.4);
                            rectangle.Y += (int)((double)rectangle.Height * 0.5 * (double)this.gravDir);
                            rectangle.Height = (int)((double)rectangle.Height * 1.1);
                        }
                        else
                        {
                            if ((double)this.itemAnimation >= (double)this.itemAnimationMax * 0.666)
                            {
                                if (this.direction == 1)
                                {
                                    rectangle.X -= (int)((double)rectangle.Width * 1.2);
                                }
                                rectangle.Width *= 2;
                                rectangle.Y -= (int)(((double)rectangle.Height * 1.4 - (double)rectangle.Height) * (double)this.gravDir);
                                rectangle.Height = (int)((double)rectangle.Height * 1.4);
                            }
                        }
                    }
                    else
                    {
                        if (selectedItem.UseStyle == 3)
                        {
                            if ((double)this.itemAnimation > (double)this.itemAnimationMax * 0.666)
                            {
                                flag5 = true;
                            }
                            else
                            {
                                if (this.direction == -1)
                                {
                                    rectangle.X -= (int)((double)rectangle.Width * 1.4 - (double)rectangle.Width);
                                }
                                rectangle.Width = (int)((double)rectangle.Width * 1.4);
                                rectangle.Y += (int)((double)rectangle.Height * 0.6);
                                rectangle.Height = (int)((double)rectangle.Height * 0.6);
                            }
                        }
                    }
                    if (!flag5)
                    {
            #if CLIENT_CODE
                        if (Main.myPlayer == i)
                        {
                            int dmgg = (int) (selectedItem.Damage * this.meleeDamage);
                            int num21 = rectangle.X / 16;
                            int num22 = (rectangle.X + rectangle.Width) / 16 + 1;
                            int num23 = rectangle.Y / 16;
                            int num24 = (rectangle.Y + rectangle.Height) / 16 + 1;
                            for (int k = num21; k < num22; k++)
                            {
                                for (int l = num23; l < num24; l++)
                                {
                                    if (Main.tile.At(k, l).Type == 3 || Main.tile.At(k, l).Type == 24 || Main.tile.At(k, l).Type == 28 || Main.tile.At(k, l).Type == 32 || Main.tile.At(k, l).Type == 51 || Main.tile.At(k, l).Type == 52 || Main.tile.At(k, l).Type == 61 || Main.tile.At(k, l).Type == 62 || Main.tile.At(k, l).Type == 69 || Main.tile.At(k, l).Type == 71 || Main.tile.At(k, l).Type == 73 || Main.tile.At(k, l).Type == 74)
                                    {
                                        WorldModify.KillTile(k, l, false, false, false);
                                    }
                                }
                            }
                            for (int j = 0; j < NPC.MAX_NPCS; j++)
                            {
                                var npc = Main.npcs[j];
                                if (npc.Active && npc.immune[i] == 0 && this.attackCD == 0 && !npc.friendly)
                                {
                                    Rectangle value = new Rectangle((int)npc.Position.X, (int)npc.Position.Y, npc.Width, npc.Height);
                                    if (rectangle.Intersects(value))
                                    {
                                        if (npc.noTileCollide || Collision.CanHit(this.Position, this.Width, this.Height, npc.Position, npc.Width, npc.Height))
                                        {
                                            bool crit = false;
                                            if (Main.rand.Next(1, 101) <= this.meleeCrit)
                                            {
                                                crit = true;
                                            }
                                            int dmggg = Main.DamageVar(dmgg);
                                            this.StatusNPC (selectedItem.Type, npc);
                                            npc.StrikeNPC (dmggg, selectedItem.KnockBack, this.direction, crit);
                                            npc.immune[i] = (ushort) this.itemAnimation;
                                            this.attackCD = (int)((double)this.itemAnimationMax * 0.33);
                                        }
                                    }
                                }
                            }
                            if (this.hostile)
                            {
                                for (int j = 0; j < 255; j++)
                                {
                                    var player = Main.players[j];
                                    if (j != i && player.Active && player.hostile && !player.immune && !player.dead)
                                    {
                                        if (this.team == 0 || this.team != player.team)
                                        {
                                            Rectangle value2 = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Width, player.Height);
                                            if (rectangle.Intersects(value2))
                                            {
                                                bool crit = false;
                                                if (Main.rand.Next(1, 101) <= 10)
                                                {
                                                    crit = true;
                                                }
                                                int dmggg = Main.DamageVar(dmgg);
                                                this.StatusPvP (selectedItem.Type, player);
                                                if (Collision.CanHit(this.Position, this.Width, this.Height, player.Position, player.Width, player.Height))
                                                {
                                                    player.Hurt (dmggg, this.direction, true, false, "", crit);
                                                    NetMessage.SendData(26, -1, -1, "", j, (float)this.direction, (float)selectedItem.Damage, 1f, crit ? 1 : 0);
                                                    this.attackCD = (int)((double)this.itemAnimationMax * 0.33);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
            #endif //CLIENT_CODE
                    }
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0)
            {
                if (selectedItem.HealLife > 0)
                {
                    this.statLife += selectedItem.HealLife;
                    this.itemTime = selectedItem.UseTime;
                    if (Main.myPlayer == this.whoAmi)
                    {
                        this.HealEffect(selectedItem.HealLife);
                    }
                }
                if (selectedItem.HealMana > 0)
                {
                    this.statMana += selectedItem.HealMana;
                    this.itemTime = selectedItem.UseTime;
                    if (Main.myPlayer == this.whoAmi)
                    {
                        this.ManaEffect(selectedItem.HealMana);
                    }
                }
                if (selectedItem.BuffType > 0)
                {
                    if (this.whoAmi == Main.myPlayer)
                    {
                        this.AddBuff (selectedItem.BuffType, selectedItem.BuffTime, true);
                    }
                    this.itemTime = selectedItem.UseTime;
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0 && selectedItem.Type == 361)
            {
                this.itemTime = selectedItem.UseTime;
                if (Main.invasionType == 0)
                {
                    var ctx = new HookContext
                    {
                        Connection = Connection,
                        Sender = this,
                        Player = this,
                    };

                    var args = new HookArgs.PlayerTriggeredEvent
                    {
                        Type = WorldEventType.INVASION,
                    };

                    HookPoints.PlayerTriggeredEvent.Invoke (ref ctx, ref args);

                    if (ctx.CheckForKick ())
                        return;
                    else if (ctx.Result != HookResult.IGNORE)
                    {
                        ProgramLog.Users.Log ("{0} @ {1}: Invasion triggered by {2}.", IPAddress, whoAmi, Name);
                        NetMessage.SendData (Packet.PLAYER_CHAT, -1, -1, string.Concat (Name, " has summoned an invasion!"), 255, 255, 128, 150);
                        Main.invasionDelay = 0;
                        Main.StartInvasion();
                    }
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0 && (selectedItem.Type == 43 || selectedItem.Type == 70))
            {
                this.itemTime = selectedItem.UseTime;
                bool flag6 = false;
                int num25 = 4;
                if (selectedItem.Type == 43)
                {
                    num25 = 4;
                }
                else
                {
                    if (selectedItem.Type == 70)
                    {
                        num25 = 13;
                    }
                }
                for (int j = 0; j < 1000; j++)
                {
                    if (Main.npcs[j].Active && Main.npcs[j].Type == num25)
                    {
                        flag6 = true;
                        break;
                    }
                }
                if (flag6)
                {
            #if CLIENT_CODE
                    if (Main.myPlayer == this.whoAmi)
                    {
                        this.Hurt(this.statLife * (this.statDefense + 1), -this.direction, false, false);
                    }
            #endif //CLIENT_CODE
                }
                else if ( (selectedItem.Type == 43 && !Main.dayTime) || (selectedItem.Type == 70 && zoneEvil) )
                {
                    var ctx = new HookContext
                    {
                        Connection = Connection,
                        Sender = this,
                        Player = this,
                    };

                    var args = new HookArgs.PlayerTriggeredEvent
                    {
                        X = (int) (Position.X/16),
                        Y = (int) (Position.Y/16),
                        Type = WorldEventType.BOSS,
                        Name = selectedItem.Type == 43 ? "Eye of Cthulhu" : "Eater of Worlds",
                    };

                    HookPoints.PlayerTriggeredEvent.Invoke (ref ctx, ref args);

                    if (ctx.CheckForKick ())
                        return;
                    else if (ctx.Result != HookResult.IGNORE)
                    {
                        if (selectedItem.Type == 43)
                        {
                            ProgramLog.Users.Log ("{0} @ {1}: Eye of Cthulhu summoned by {2}.", IPAddress, whoAmi, Name);
                            NetMessage.SendData (Packet.PLAYER_CHAT, -1, -1, string.Concat (Name, " has summoned the Eye of Cthulhu!"), 255, 255, 128, 150);
                            NPC.SpawnOnPlayer(Main.players[i], i, 4);
                        }
                        else if (selectedItem.Type == 70)
                        {
                            ProgramLog.Users.Log ("{0} @ {1}: Eater of Worlds summoned by {2}.", IPAddress, whoAmi, Name);
                            NetMessage.SendData (Packet.PLAYER_CHAT, -1, -1, string.Concat (Name, " has summoned the Eater of Worlds!"), 255, 255, 128, 150);
                            NPC.SpawnOnPlayer(Main.players[i], i, 13);
                        }
                    }
                }
            }

            if (selectedItem.Type == 50 && this.itemAnimation > 0)
            {
                if (this.itemTime == 0)
                {
                    this.itemTime = selectedItem.UseTime;
                }
                else
                {
                    if (this.itemTime == selectedItem.UseTime / 2)
                    {
                        this.grappling[0] = -1;
                        this.grapCount = 0;
                        for (int j = 0; j < 1000; j++)
                        {
                            if (Main.projectile[j].Active && Main.projectile[j].Owner == i)
                            {
                                if (Main.projectile[j].aiStyle == 7)
                                {
                                    Main.projectile[j].Kill();
                                }
                            }
                        }
                        this.Spawn();
                    }
                }
            }
            if (i == Main.myPlayer)
            {
                if (this.itemTime == selectedItem.UseTime && selectedItem.Consumable)
                {
                    bool flag11 = true;
                    if (selectedItem.Ranged && this.ammoCost80 && Main.rand.Next(5) == 0)
                    {
                        flag11 = false;
                    }
                    if (flag11)
                    {
                        selectedItem.Stack--;
                        if (selectedItem.Stack <= 0)
                        {
                            this.itemTime = this.itemAnimation;
                        }
                    }
                }
                if (selectedItem.Stack <= 0 && this.itemAnimation == 0)
                {
                    inventory[selectedItemIndex] = new Item();
                }
            }
        }
        public static void TileFrame(int i, int j, bool resetFrame = false, bool noBreak = false)
        {
            if (i > 5 && j > 5 && i < Main.maxTilesX - 5 && j < Main.maxTilesY - 5)
            {
                if (Main.tile.At(i, j).Liquid > 0 && !WorldModify.noLiquidCheck)
                {
                    Liquid.AddWater(i, j);
                }
                if (Main.tile.At(i, j).Active)
                {
                    if (noBreak && Main.tileFrameImportant[(int)Main.tile.At(i, j).Type])
                    {
                        return;
                    }
                    int num = -1;
                    int num2 = -1;
                    int num3 = -1;
                    int num4 = -1;
                    int num5 = -1;
                    int num6 = -1;
                    int num7 = -1;
                    int num8 = -1;
                    int num9 = (int)Main.tile.At(i, j).Type;
                    if (Main.tileStone[num9])
                    {
                        num9 = 1;
                    }
                    int frameX = (int)Main.tile.At(i, j).FrameX;
                    int frameY = (int)Main.tile.At(i, j).FrameY;
                    Rectangle rectangle;
                    rectangle.X = -1;
                    rectangle.Y = -1;
                    WorldModify.mergeUp = false;
                    WorldModify.mergeDown = false;
                    WorldModify.mergeLeft = false;
                    WorldModify.mergeRight = false;
                    if (Main.tile.At(i - 1, j).Active)
                    {
                        num4 = (int)Main.tile.At(i - 1, j).Type;
                    }
                    if (Main.tile.At(i + 1, j).Active)
                    {
                        num5 = (int)Main.tile.At(i + 1, j).Type;
                    }
                    if ( Main.tile.At(i, j - 1).Active)
                    {
                        num2 = (int)Main.tile.At(i, j - 1).Type;
                    }
                    if (Main.tile.At(i, j + 1).Active)
                    {
                        num7 = (int)Main.tile.At(i, j + 1).Type;
                    }
                    if (Main.tile.At(i - 1, j - 1).Active)
                    {
                        num = (int)Main.tile.At(i - 1, j - 1).Type;
                    }
                    if (Main.tile.At(i + 1, j - 1).Active)
                    {
                        num3 = (int)Main.tile.At(i + 1, j - 1).Type;
                    }
                    if (Main.tile.At(i - 1, j + 1).Active)
                    {
                        num6 = (int)Main.tile.At(i - 1, j + 1).Type;
                    }
                    if (Main.tile.At(i + 1, j + 1).Active)
                    {
                        num8 = (int)Main.tile.At(i + 1, j + 1).Type;
                    }
                    if (num4 >= 0 && Main.tileStone[num4])
                    {
                        num4 = 1;
                    }
                    if (num5 >= 0 && Main.tileStone[num5])
                    {
                        num5 = 1;
                    }
                    if (num2 >= 0 && Main.tileStone[num2])
                    {
                        num2 = 1;
                    }
                    if (num7 >= 0 && Main.tileStone[num7])
                    {
                        num7 = 1;
                    }
                    if (num >= 0 && Main.tileStone[num])
                    {
                        num = 1;
                    }
                    if (num3 >= 0 && Main.tileStone[num3])
                    {
                        num3 = 1;
                    }
                    if (num6 >= 0 && Main.tileStone[num6])
                    {
                        num6 = 1;
                    }
                    if (num8 >= 0 && Main.tileStone[num8])
                    {
                        num8 = 1;
                    }
                    if (num9 != 0 && num9 != 1)
                    {
                        if (num9 == 3 || num9 == 24 || num9 == 61 || num9 == 71 || num9 == 73 || num9 == 74)
                        {
                            WorldModify.PlantCheck(i, j);
                            return;
                        }
                        if (num9 == 4)
                        {
                            if (num7 >= 0 && Main.tileSolid[num7] && !Main.tileNoAttach[num7])
                            {
                                Main.tile.At(i, j).SetFrameX(0);
                                return;
                            }
                            if ((num4 >= 0 && Main.tileSolid[num4] && !Main.tileNoAttach[num4]) || (num4 == 5 && num == 5 && num6 == 5))
                            {
                                Main.tile.At(i, j).SetFrameX(22);
                                return;
                            }
                            if ((num5 >= 0 && Main.tileSolid[num5] && !Main.tileNoAttach[num5]) || (num5 == 5 && num3 == 5 && num8 == 5))
                            {
                                Main.tile.At(i, j).SetFrameX(44);
                                return;
                            }
                            WorldModify.KillTile(i, j, false, false, false);
                            return;
                        }
                        else
                        {
                            if (num9 == 80)
                            {
                                WorldModify.CactusFrame(i, j);
                                return;
                            }
                            if (num9 == 12 || num9 == 31)
                            {
                                if (!WorldModify.destroyObject)
                                {
                                    int num10 = i;
                                    int num11 = j;
                                    if (Main.tile.At(i, j).FrameX == 0)
                                    {
                                        num10 = i;
                                    }
                                    else
                                    {
                                        num10 = i - 1;
                                    }
                                    if (Main.tile.At(i, j).FrameY == 0)
                                    {
                                        num11 = j;
                                    }
                                    else
                                    {
                                        num11 = j - 1;
                                    }
                                    if ((!Main.tile.At(num10, num11).Active || (int)Main.tile.At(num10, num11).Type != num9 ||
                                        !Main.tile.At(num10 + 1, num11).Active || (int)Main.tile.At(num10 + 1, num11).Type != num9 ||
                                            !Main.tile.At(num10, num11 + 1).Active || (int)Main.tile.At(num10, num11 + 1).Type != num9 ||
                                                !Main.tile.At(num10 + 1, num11 + 1).Active ||
                                                    (int)Main.tile.At(num10 + 1, num11 + 1).Type != num9))
                                    {
                                        WorldModify.destroyObject = true;
                                        if ((int)Main.tile.At(num10, num11).Type == num9)
                                        {
                                            WorldModify.KillTile(num10, num11, false, false, false);
                                        }
                                        if ((int)Main.tile.At(num10 + 1, num11).Type == num9)
                                        {
                                            WorldModify.KillTile(num10 + 1, num11, false, false, false);
                                        }
                                        if ((int)Main.tile.At(num10, num11 + 1).Type == num9)
                                        {
                                            WorldModify.KillTile(num10, num11 + 1, false, false, false);
                                        }
                                        if ((int)Main.tile.At(num10 + 1, num11 + 1).Type == num9)
                                        {
                                            WorldModify.KillTile(num10 + 1, num11 + 1, false, false, false);
                                        }
                                        if (!WorldModify.noTileActions)
                                        {
                                            if (num9 == 12)
                                            {
                                                Item.NewItem(num10 * 16, num11 * 16, 32, 32, 29, 1, false);
                                            }
                                            else
                                            {
                                                if (num9 == 31)
                                                {
                                                    if (WorldModify.genRand.Next(2) == 0)
                                                    {
                                                        WorldModify.spawnMeteor = true;
                                                    }
                                                    int num12 = Main.rand.Next(5);
                                                    if (!WorldModify.shadowOrbSmashed)
                                                    {
                                                        num12 = 0;
                                                    }
                                                    if (num12 == 0)
                                                    {
                                                        Item.NewItem(num10 * 16, num11 * 16, 32, 32, 96, 1, false);
                                                        int stack = WorldModify.genRand.Next(25, 51);
                                                        Item.NewItem(num10 * 16, num11 * 16, 32, 32, 97, stack, false);
                                                    }
                                                    else
                                                    {
                                                        if (num12 == 1)
                                                        {
                                                            Item.NewItem(num10 * 16, num11 * 16, 32, 32, 64, 1, false);
                                                        }
                                                        else
                                                        {
                                                            if (num12 == 2)
                                                            {
                                                                Item.NewItem(num10 * 16, num11 * 16, 32, 32, 162, 1, false);
                                                            }
                                                            else
                                                            {
                                                                if (num12 == 3)
                                                                {
                                                                    Item.NewItem(num10 * 16, num11 * 16, 32, 32, 115, 1, false);
                                                                }
                                                                else
                                                                {
                                                                    if (num12 == 4)
                                                                    {
                                                                        Item.NewItem(num10 * 16, num11 * 16, 32, 32, 111, 1, false);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    WorldModify.shadowOrbSmashed = true;
                                                    WorldModify.shadowOrbCount++;
                                                    if (WorldModify.shadowOrbCount >= 3)
                                                    {
                                                        WorldModify.shadowOrbCount = 0;
                                                        float num13 = (float)(num10 * 16);
                                                        float num14 = (float)(num11 * 16);
                                                        float num15 = -1f;
                                                        int plr = 0;
                                                        for (int k = 0; k < 255; k++) //FIXME: propage player here
                                                        {
                                                            float num16 = Math.Abs(Main.players[k].Position.X - num13) + Math.Abs(Main.players[k].Position.Y - num14);
                                                            if (num16 < num15 || num15 == -1f)
                                                            {
                                                                plr = 0;
                                                                num15 = num16;
                                                            }
                                                        }

                                                        var player = Main.players[plr];

                                                        var ctx = new HookContext
                                                        {
                                                            Connection = player.Connection,
                                                            Sender = player,
                                                            Player = player,
                                                        };

                                                        var args = new HookArgs.PlayerTriggeredEvent
                                                        {
                                                            X = (int) (player.Position.X/16),
                                                            Y = (int) (player.Position.Y/16),
                                                            Type = WorldEventType.BOSS,
                                                            Name = "Eater of Worlds",
                                                        };

                                                        HookPoints.PlayerTriggeredEvent.Invoke (ref ctx, ref args);

                                                        if (!ctx.CheckForKick () && ctx.Result != HookResult.IGNORE)
                                                        {
                                                            ProgramLog.Users.Log ("{0} @ {1}: Eater of Worlds summoned by {2}.", player.IPAddress, plr, player.Name);
                                                            NetMessage.SendData (Packet.PLAYER_CHAT, -1, -1, string.Concat (player.Name, " has summoned the Eater of Worlds!"), 255, 255, 128, 150);
                                                            NPC.SpawnOnPlayer(player, plr, 13);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        string text = "A horrible chill goes down your spine...";
                                                        if (WorldModify.shadowOrbCount == 2)
                                                        {
                                                            text = "Screams echo around you...";
                                                        }
                                                        NetMessage.SendData(25, -1, -1, text, 255, 50f, 255f, 130f, 0);
                                                    }
                                                }
                                            }
                                        }
                                        WorldModify.destroyObject = false;
                                    }
                                }
                                return;
                            }
                            if (num9 == 19)
                            {
                                if (num4 == num9 && num5 == num9)
                                {
                                    if (Main.tile.At(i, j).FrameNumber == 0)
                                    {
                                        rectangle.X = 0;
                                        rectangle.Y = 0;
                                    }
                                    if (Main.tile.At(i, j).FrameNumber == 1)
                                    {
                                        rectangle.X = 0;
                                        rectangle.Y = 18;
                                    }
                                    if (Main.tile.At(i, j).FrameNumber == 2)
                                    {
                                        rectangle.X = 0;
                                        rectangle.Y = 36;
                                    }
                                }
                                else
                                {
                                    if (num4 == num9 && num5 == -1)
                                    {
                                        if (Main.tile.At(i, j).FrameNumber == 0)
                                        {
                                            rectangle.X = 18;
                                            rectangle.Y = 0;
                                        }
                                        if (Main.tile.At(i, j).FrameNumber == 1)
                                        {
                                            rectangle.X = 18;
                                            rectangle.Y = 18;
                                        }
                                        if (Main.tile.At(i, j).FrameNumber == 2)
                                        {
                                            rectangle.X = 18;
                                            rectangle.Y = 36;
                                        }
                                    }
                                    else
                                    {
                                        if (num4 == -1 && num5 == num9)
                                        {
                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 0;
                                            }
                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 18;
                                            }
                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 36;
                                            }
                                        }
                                        else
                                        {
                                            if (num4 != num9 && num5 == num9)
                                            {
                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                {
                                                    rectangle.X = 54;
                                                    rectangle.Y = 0;
                                                }
                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                {
                                                    rectangle.X = 54;
                                                    rectangle.Y = 18;
                                                }
                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                {
                                                    rectangle.X = 54;
                                                    rectangle.Y = 36;
                                                }
                                            }
                                            else
                                            {
                                                if (num4 == num9 && num5 != num9)
                                                {
                                                    if (Main.tile.At(i, j).FrameNumber == 0)
                                                    {
                                                        rectangle.X = 72;
                                                        rectangle.Y = 0;
                                                    }
                                                    if (Main.tile.At(i, j).FrameNumber == 1)
                                                    {
                                                        rectangle.X = 72;
                                                        rectangle.Y = 18;
                                                    }
                                                    if (Main.tile.At(i, j).FrameNumber == 2)
                                                    {
                                                        rectangle.X = 72;
                                                        rectangle.Y = 36;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num4 != num9 && num4 != -1 && num5 == -1)
                                                    {
                                                        if (Main.tile.At(i, j).FrameNumber == 0)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 0;
                                                        }
                                                        if (Main.tile.At(i, j).FrameNumber == 1)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 18;
                                                        }
                                                        if (Main.tile.At(i, j).FrameNumber == 2)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 36;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num4 == -1 && num5 != num9 && num5 != -1)
                                                        {
                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 0;
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 18;
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 36;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                            {
                                                                rectangle.X = 90;
                                                                rectangle.Y = 0;
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                            {
                                                                rectangle.X = 90;
                                                                rectangle.Y = 18;
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                            {
                                                                rectangle.X = 90;
                                                                rectangle.Y = 36;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (num9 == 10)
                                {
                                    if (!WorldModify.destroyObject)
                                    {
                                        int frameY2 = (int)Main.tile.At(i, j).FrameY;
                                        int num17 = j;
                                        bool flag = false;
                                        if (frameY2 == 0)
                                        {
                                            num17 = j;
                                        }
                                        if (frameY2 == 18)
                                        {
                                            num17 = j - 1;
                                        }
                                        if (frameY2 == 36)
                                        {
                                            num17 = j - 2;
                                        }
                                        if (!Main.tile.At(i, num17 - 1).Active || !Main.tileSolid[(int)Main.tile.At(i, num17 - 1).Type])
                                        {
                                            flag = true;
                                        }
                                        if (!Main.tile.At(i, num17 + 3).Active || !Main.tileSolid[(int)Main.tile.At(i, num17 + 3).Type])
                                        {
                                            flag = true;
                                        }
                                        if (!Main.tile.At(i, num17).Active || (int)Main.tile.At(i, num17).Type != num9)
                                        {
                                            flag = true;
                                        }
                                        if (!Main.tile.At(i, num17 + 1).Active || (int)Main.tile.At(i, num17 + 1).Type != num9)
                                        {
                                            flag = true;
                                        }
                                        if (!Main.tile.At(i, num17 + 2).Active || (int)Main.tile.At(i, num17 + 2).Type != num9)
                                        {
                                            flag = true;
                                        }
                                        if (flag)
                                        {
                                            WorldModify.destroyObject = true;
                                            WorldModify.KillTile(i, num17, false, false, false);
                                            WorldModify.KillTile(i, num17 + 1, false, false, false);
                                            WorldModify.KillTile(i, num17 + 2, false, false, false);
                                            Item.NewItem(i * 16, j * 16, 16, 16, 25, 1, false);
                                        }
                                        WorldModify.destroyObject = false;
                                    }
                                    return;
                                }
                                if (num9 == 11)
                                {
                                    if (!WorldModify.destroyObject)
                                    {
                                        int num18 = 0;
                                        int num19 = i;
                                        int num20 = j;
                                        int frameX2 = (int)Main.tile.At(i, j).FrameX;
                                        int frameY3 = (int)Main.tile.At(i, j).FrameY;
                                        bool flag2 = false;
                                        if (frameX2 == 0)
                                        {
                                            num19 = i;
                                            num18 = 1;
                                        }
                                        else
                                        {
                                            if (frameX2 == 18)
                                            {
                                                num19 = i - 1;
                                                num18 = 1;
                                            }
                                            else
                                            {
                                                if (frameX2 == 36)
                                                {
                                                    num19 = i + 1;
                                                    num18 = -1;
                                                }
                                                else
                                                {
                                                    if (frameX2 == 54)
                                                    {
                                                        num19 = i;
                                                        num18 = -1;
                                                    }
                                                }
                                            }
                                        }
                                        if (frameY3 == 0)
                                        {
                                            num20 = j;
                                        }
                                        else
                                        {
                                            if (frameY3 == 18)
                                            {
                                                num20 = j - 1;
                                            }
                                            else
                                            {
                                                if (frameY3 == 36)
                                                {
                                                    num20 = j - 2;
                                                }
                                            }
                                        }
                                        if (!Main.tile.At(num19, num20 - 1).Active ||
                                            !Main.tileSolid[(int)Main.tile.At(num19, num20 - 1).Type] ||
                                            !Main.tile.At(num19, num20 + 3).Active ||
                                            !Main.tileSolid[(int)Main.tile.At(num19, num20 + 3).Type])
                                        {
                                            flag2 = true;
                                            WorldModify.destroyObject = true;
                                            Item.NewItem(i * 16, j * 16, 16, 16, 25, 1, false);
                                        }
                                        int num21 = num19;
                                        if (num18 == -1)
                                        {
                                            num21 = num19 - 1;
                                        }
                                        for (int l = num21; l < num21 + 2; l++)
                                        {
                                            for (int m = num20; m < num20 + 3; m++)
                                            {
                                                if (!flag2 && (Main.tile.At(l, m).Type != 11 || !Main.tile.At(l, m).Active))
                                                {
                                                    WorldModify.destroyObject = true;
                                                    Item.NewItem(i * 16, j * 16, 16, 16, 25, 1, false);
                                                    flag2 = true;
                                                    l = num21;
                                                    m = num20;
                                                }
                                                if (flag2)
                                                {
                                                    WorldModify.KillTile(l, m, false, false, false);
                                                }
                                            }
                                        }
                                        WorldModify.destroyObject = false;
                                    }
                                    return;
                                }
                                if (num9 == 34 || num9 == 35 || num9 == 36 || num9 == 106)
                                {
                                    WorldModify.Check3x3(i, j, (int)((byte)num9));
                                    return;
                                }
                                if (num9 == 15 || num9 == 20)
                                {
                                    WorldModify.Check1x2(i, j, (byte)num9);
                                    return;
                                }
                                if (num9 == 14 || num9 == 17 || num9 == 26 || num9 == 77 || num9 == 86 || num9 == 87 || num9 == 88 || num9 == 89)
                                {
                                    WorldModify.Check3x2(i, j, (int)((byte)num9));
                                    return;
                                }
                                if (num9 == 16 || num9 == 18 || num9 == 29 || num9 == 103)
                                {
                                    WorldModify.Check2x1(i, j, (byte)num9);
                                    return;
                                }
                                if (num9 == 13 || num9 == 33 || num9 == 49 || num9 == 50 || num9 == 78)
                                {
                                    WorldModify.CheckOnTable1x1(i, j, (int)((byte)num9));
                                    return;
                                }
                                if (num9 == 21)
                                {
                                    WorldModify.CheckChest(i, j, (int)((byte)num9));
                                    return;
                                }
                                if (num9 == 27)
                                {
                                    WorldModify.CheckSunflower(i, j, 27);
                                    return;
                                }
                                if (num9 == 28)
                                {
                                    WorldModify.CheckPot(i, j, 28);
                                    return;
                                }
                                if (num9 == 91)
                                {
                                    WorldModify.CheckBanner(i, j, (byte)num9);
                                    return;
                                }
                                if (num9 == 92 || num9 == 93)
                                {
                                    WorldModify.Check1xX(i, j, (byte)num9);
                                    return;
                                }
                                if (num9 == 104 || num9 == 105)
                                {
                                    WorldModify.Check2xX(i, j, (byte)num9);
                                }
                                else
                                {
                                    if (num9 == 101 || num9 == 102)
                                    {
                                        WorldModify.Check3x4(i, j, (int)((byte)num9));
                                        return;
                                    }
                                    if (num9 == 42)
                                    {
                                        WorldModify.Check1x2Top(i, j, (byte)num9);
                                        return;
                                    }
                                    if (num9 == 55 || num9 == 85)
                                    {
                                        WorldModify.CheckSign(i, j, num9);
                                        return;
                                    }
                                    if (num9 == 79 || num9 == 90)
                                    {
                                        WorldModify.Check4x2(i, j, num9);
                                        return;
                                    }
                                    if (num9 == 85 || num9 == 94 || num9 == 95 || num9 == 96 || num9 == 97 || num9 == 98 || num9 == 99 || num9 == 100)
                                    {
                                        WorldModify.Check2x2(i, j, num9);
                                        return;
                                    }
                                    if (num9 == 81)
                                    {
                                        if (num4 != -1 || num2 != -1 || num5 != -1)
                                        {
                                            WorldModify.KillTile(i, j, false, false, false);
                                            return;
                                        }
                                        if (num7 < 0 || !Main.tileSolid[num7])
                                        {
                                            WorldModify.KillTile(i, j, false, false, false);
                                        }
                                        return;
                                    }
                                    else
                                    {
                                        if (Main.tileAlch[num9])
                                        {
                                            WorldModify.CheckAlch(i, j);
                                            return;
                                        }
                                        if (num9 == 72)
                                        {
                                            if (num7 != num9 && num7 != 70)
                                            {
                                                WorldModify.KillTile(i, j, false, false, false);
                                            }
                                            else
                                            {
                                                if (num2 != num9 && Main.tile.At(i, j).FrameX == 0)
                                                {
                                                    Main.tile.At(i, j).SetFrameNumber((byte)WorldModify.genRand.Next(3));
                                                    if (Main.tile.At(i, j).FrameNumber == 0)
                                                    {
                                                        Main.tile.At(i, j).SetFrameX(18);
                                                        Main.tile.At(i, j).SetFrameY(0);
                                                    }
                                                    if (Main.tile.At(i, j).FrameNumber == 1)
                                                    {
                                                        Main.tile.At(i, j).SetFrameX(18);
                                                        Main.tile.At(i, j).SetFrameY(18);
                                                    }
                                                    if (Main.tile.At(i, j).FrameNumber == 2)
                                                    {
                                                        Main.tile.At(i, j).SetFrameX(18);
                                                        Main.tile.At(i, j).SetFrameY(36);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (num9 == 5)
                                            {
                                                if (num7 == 23)
                                                {
                                                    num7 = 2;
                                                }
                                                if (num7 == 60)
                                                {
                                                    num7 = 2;
                                                }
                                                if (Main.tile.At(i, j).FrameX >= 22 && Main.tile.At(i, j).FrameX <= 44 && Main.tile.At(i, j).FrameY >= 132 && Main.tile.At(i, j).FrameY <= 176)
                                                {
                                                    if ((num4 != num9 && num5 != num9) || num7 != 2)
                                                    {
                                                        WorldModify.KillTile(i, j, false, false, false);
                                                    }
                                                }
                                                else
                                                {
                                                    if ((Main.tile.At(i, j).FrameX == 88 && Main.tile.At(i, j).FrameY >= 0 && Main.tile.At(i, j).FrameY <= 44) || (Main.tile.At(i, j).FrameX == 66 && Main.tile.At(i, j).FrameY >= 66 && Main.tile.At(i, j).FrameY <= 130) || (Main.tile.At(i, j).FrameX == 110 && Main.tile.At(i, j).FrameY >= 66 && Main.tile.At(i, j).FrameY <= 110) || (Main.tile.At(i, j).FrameX == 132 && Main.tile.At(i, j).FrameY >= 0 && Main.tile.At(i, j).FrameY <= 176))
                                                    {
                                                        if (num4 == num9 && num5 == num9)
                                                        {
                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(110);
                                                                Main.tile.At(i, j).SetFrameY(66);
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(110);
                                                                Main.tile.At(i, j).SetFrameY(88);
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(110);
                                                                Main.tile.At(i, j).SetFrameY(110);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num4 == num9)
                                                            {
                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(0);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(22);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(44);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num5 == num9)
                                                                {
                                                                    if (Main.tile.At(i, j).FrameNumber == 0)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(66);
                                                                        Main.tile.At(i, j).SetFrameY(66);
                                                                    }
                                                                    if (Main.tile.At(i, j).FrameNumber == 1)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(66);
                                                                        Main.tile.At(i, j).SetFrameY(88);
                                                                    }
                                                                    if (Main.tile.At(i, j).FrameNumber == 2)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(66);
                                                                        Main.tile.At(i, j).SetFrameY(110);
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (Main.tile.At(i, j).FrameNumber == 0)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(0);
                                                                        Main.tile.At(i, j).SetFrameY(0);
                                                                    }
                                                                    if (Main.tile.At(i, j).FrameNumber == 1)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(0);
                                                                        Main.tile.At(i, j).SetFrameY(22);
                                                                    }
                                                                    if (Main.tile.At(i, j).FrameNumber == 2)
                                                                    {
                                                                        Main.tile.At(i, j).SetFrameX(0);
                                                                        Main.tile.At(i, j).SetFrameY(44);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                if (Main.tile.At(i, j).FrameY >= 132 && Main.tile.At(i, j).FrameY <= 176 && (Main.tile.At(i, j).FrameX == 0 || Main.tile.At(i, j).FrameX == 66 || Main.tile.At(i, j).FrameX == 88))
                                                {
                                                    if (num7 != 2)
                                                    {
                                                        WorldModify.KillTile(i, j, false, false, false);
                                                    }
                                                    if (num4 != num9 && num5 != num9)
                                                    {
                                                        if (Main.tile.At(i, j).FrameNumber == 0)
                                                        {
                                                            Main.tile.At(i, j).SetFrameX(0);
                                                            Main.tile.At(i, j).SetFrameY(0);
                                                        }
                                                        if (Main.tile.At(i, j).FrameNumber == 1)
                                                        {
                                                            Main.tile.At(i, j).SetFrameX(0);
                                                            Main.tile.At(i, j).SetFrameY(22);
                                                        }
                                                        if (Main.tile.At(i, j).FrameNumber == 2)
                                                        {
                                                            Main.tile.At(i, j).SetFrameX(0);
                                                            Main.tile.At(i, j).SetFrameY(44);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num4 != num9)
                                                        {
                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(0);
                                                                Main.tile.At(i, j).SetFrameY(132);
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(0);
                                                                Main.tile.At(i, j).SetFrameY(154);
                                                            }
                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                            {
                                                                Main.tile.At(i, j).SetFrameX(0);
                                                                Main.tile.At(i, j).SetFrameY(176);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num5 != num9)
                                                            {
                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(66);
                                                                    Main.tile.At(i, j).SetFrameY(132);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(66);
                                                                    Main.tile.At(i, j).SetFrameY(154);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(66);
                                                                    Main.tile.At(i, j).SetFrameY(176);
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(132);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(154);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(88);
                                                                    Main.tile.At(i, j).SetFrameY(176);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                if ((Main.tile.At(i, j).FrameX == 66 && (Main.tile.At(i, j).FrameY == 0 || Main.tile.At(i, j).FrameY == 22 || Main.tile.At(i, j).FrameY == 44)) || (Main.tile.At(i, j).FrameX == 88 && (Main.tile.At(i, j).FrameY == 66 || Main.tile.At(i, j).FrameY == 88 || Main.tile.At(i, j).FrameY == 110)) || (Main.tile.At(i, j).FrameX == 44 && (Main.tile.At(i, j).FrameY == 198 || Main.tile.At(i, j).FrameY == 220 || Main.tile.At(i, j).FrameY == 242)) || (Main.tile.At(i, j).FrameX == 66 && (Main.tile.At(i, j).FrameY == 198 || Main.tile.At(i, j).FrameY == 220 || Main.tile.At(i, j).FrameY == 242)))
                                                {
                                                    if (num4 != num9 && num5 != num9)
                                                    {
                                                        WorldModify.KillTile(i, j, false, false, false);
                                                    }
                                                }
                                                else
                                                {
                                                    if (num7 == -1 || num7 == 23)
                                                    {
                                                        WorldModify.KillTile(i, j, false, false, false);
                                                    }
                                                    else
                                                    {
                                                        if (num2 != num9 && Main.tile.At(i, j).FrameY < 198 && ((Main.tile.At(i, j).FrameX != 22 && Main.tile.At(i, j).FrameX != 44) || Main.tile.At(i, j).FrameY < 132))
                                                        {
                                                            if (num4 == num9 || num5 == num9)
                                                            {
                                                                if (num7 == num9)
                                                                {
                                                                    if (num4 == num9 && num5 == num9)
                                                                    {
                                                                        if (Main.tile.At(i, j).FrameNumber == 0)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(132);
                                                                            Main.tile.At(i, j).SetFrameY(132);
                                                                        }
                                                                        if (Main.tile.At(i, j).FrameNumber == 1)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(132);
                                                                            Main.tile.At(i, j).SetFrameY(154);
                                                                        }
                                                                        if (Main.tile.At(i, j).FrameNumber == 2)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(132);
                                                                            Main.tile.At(i, j).SetFrameY(176);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num4 == num9)
                                                                        {
                                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(132);
                                                                                Main.tile.At(i, j).SetFrameY(0);
                                                                            }
                                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(132);
                                                                                Main.tile.At(i, j).SetFrameY(22);
                                                                            }
                                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(132);
                                                                                Main.tile.At(i, j).SetFrameY(44);
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num5 == num9)
                                                                            {
                                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(132);
                                                                                    Main.tile.At(i, j).SetFrameY(66);
                                                                                }
                                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(132);
                                                                                    Main.tile.At(i, j).SetFrameY(88);
                                                                                }
                                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(132);
                                                                                    Main.tile.At(i, j).SetFrameY(110);
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num4 == num9 && num5 == num9)
                                                                    {
                                                                        if (Main.tile.At(i, j).FrameNumber == 0)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(154);
                                                                            Main.tile.At(i, j).SetFrameY(132);
                                                                        }
                                                                        if (Main.tile.At(i, j).FrameNumber == 1)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(154);
                                                                            Main.tile.At(i, j).SetFrameY(154);
                                                                        }
                                                                        if (Main.tile.At(i, j).FrameNumber == 2)
                                                                        {
                                                                            Main.tile.At(i, j).SetFrameX(154);
                                                                            Main.tile.At(i, j).SetFrameY(176);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num4 == num9)
                                                                        {
                                                                            if (Main.tile.At(i, j).FrameNumber == 0)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(154);
                                                                                Main.tile.At(i, j).SetFrameY(0);
                                                                            }
                                                                            if (Main.tile.At(i, j).FrameNumber == 1)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(154);
                                                                                Main.tile.At(i, j).SetFrameY(22);
                                                                            }
                                                                            if (Main.tile.At(i, j).FrameNumber == 2)
                                                                            {
                                                                                Main.tile.At(i, j).SetFrameX(154);
                                                                                Main.tile.At(i, j).SetFrameY(44);
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num5 == num9)
                                                                            {
                                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(154);
                                                                                    Main.tile.At(i, j).SetFrameY(66);
                                                                                }
                                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(154);
                                                                                    Main.tile.At(i, j).SetFrameY(88);
                                                                                }
                                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                                {
                                                                                    Main.tile.At(i, j).SetFrameX(154);
                                                                                    Main.tile.At(i, j).SetFrameY(110);
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (Main.tile.At(i, j).FrameNumber == 0)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(110);
                                                                    Main.tile.At(i, j).SetFrameY(0);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 1)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(110);
                                                                    Main.tile.At(i, j).SetFrameY(22);
                                                                }
                                                                if (Main.tile.At(i, j).FrameNumber == 2)
                                                                {
                                                                    Main.tile.At(i, j).SetFrameX(110);
                                                                    Main.tile.At(i, j).SetFrameY(44);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                rectangle.X = (int)Main.tile.At(i, j).FrameX;
                                                rectangle.Y = (int)Main.tile.At(i, j).FrameY;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (Main.tileFrameImportant[(int)Main.tile.At(i, j).Type])
                    {
                        return;
                    }
                    int num22 = 0;
                    if (resetFrame)
                    {
                        num22 = WorldModify.genRand.Next(0, 3);
                        Main.tile.At(i, j).SetFrameNumber((byte)num22);
                    }
                    else
                    {
                        num22 = (int)Main.tile.At(i, j).FrameNumber;
                    }
                    if (num9 == 0)
                    {
                        if (num2 >= 0 && Main.tileMergeDirt[num2])
                        {
                            WorldModify.TileFrame(i, j - 1, false, false);
                            if (WorldModify.mergeDown)
                            {
                                num2 = num9;
                            }
                        }
                        if (num7 >= 0 && Main.tileMergeDirt[num7])
                        {
                            WorldModify.TileFrame(i, j + 1, false, false);
                            if (WorldModify.mergeUp)
                            {
                                num7 = num9;
                            }
                        }
                        if (num4 >= 0 && Main.tileMergeDirt[num4])
                        {
                            WorldModify.TileFrame(i - 1, j, false, false);
                            if (WorldModify.mergeRight)
                            {
                                num4 = num9;
                            }
                        }
                        if (num5 >= 0 && Main.tileMergeDirt[num5])
                        {
                            WorldModify.TileFrame(i + 1, j, false, false);
                            if (WorldModify.mergeLeft)
                            {
                                num5 = num9;
                            }
                        }
                        if (num >= 0 && Main.tileMergeDirt[num])
                        {
                            num = num9;
                        }
                        if (num3 >= 0 && Main.tileMergeDirt[num3])
                        {
                            num3 = num9;
                        }
                        if (num6 >= 0 && Main.tileMergeDirt[num6])
                        {
                            num6 = num9;
                        }
                        if (num8 >= 0 && Main.tileMergeDirt[num8])
                        {
                            num8 = num9;
                        }
                        if (num2 == 2)
                        {
                            num2 = num9;
                        }
                        if (num7 == 2)
                        {
                            num7 = num9;
                        }
                        if (num4 == 2)
                        {
                            num4 = num9;
                        }
                        if (num5 == 2)
                        {
                            num5 = num9;
                        }
                        if (num == 2)
                        {
                            num = num9;
                        }
                        if (num3 == 2)
                        {
                            num3 = num9;
                        }
                        if (num6 == 2)
                        {
                            num6 = num9;
                        }
                        if (num8 == 2)
                        {
                            num8 = num9;
                        }
                        if (num2 == 23)
                        {
                            num2 = num9;
                        }
                        if (num7 == 23)
                        {
                            num7 = num9;
                        }
                        if (num4 == 23)
                        {
                            num4 = num9;
                        }
                        if (num5 == 23)
                        {
                            num5 = num9;
                        }
                        if (num == 23)
                        {
                            num = num9;
                        }
                        if (num3 == 23)
                        {
                            num3 = num9;
                        }
                        if (num6 == 23)
                        {
                            num6 = num9;
                        }
                        if (num8 == 23)
                        {
                            num8 = num9;
                        }
                    }
                    else
                    {
                        if (num9 == 57)
                        {
                            if (num2 == 58)
                            {
                                WorldModify.TileFrame(i, j - 1, false, false);
                                if (WorldModify.mergeDown)
                                {
                                    num2 = num9;
                                }
                            }
                            if (num7 == 58)
                            {
                                WorldModify.TileFrame(i, j + 1, false, false);
                                if (WorldModify.mergeUp)
                                {
                                    num7 = num9;
                                }
                            }
                            if (num4 == 58)
                            {
                                WorldModify.TileFrame(i - 1, j, false, false);
                                if (WorldModify.mergeRight)
                                {
                                    num4 = num9;
                                }
                            }
                            if (num5 == 58)
                            {
                                WorldModify.TileFrame(i + 1, j, false, false);
                                if (WorldModify.mergeLeft)
                                {
                                    num5 = num9;
                                }
                            }
                            if (num == 58)
                            {
                                num = num9;
                            }
                            if (num3 == 58)
                            {
                                num3 = num9;
                            }
                            if (num6 == 58)
                            {
                                num6 = num9;
                            }
                            if (num8 == 58)
                            {
                                num8 = num9;
                            }
                        }
                        else
                        {
                            if (num9 == 59)
                            {
                                if (num2 == 60)
                                {
                                    num2 = num9;
                                }
                                if (num7 == 60)
                                {
                                    num7 = num9;
                                }
                                if (num4 == 60)
                                {
                                    num4 = num9;
                                }
                                if (num5 == 60)
                                {
                                    num5 = num9;
                                }
                                if (num == 60)
                                {
                                    num = num9;
                                }
                                if (num3 == 60)
                                {
                                    num3 = num9;
                                }
                                if (num6 == 60)
                                {
                                    num6 = num9;
                                }
                                if (num8 == 60)
                                {
                                    num8 = num9;
                                }
                                if (num2 == 70)
                                {
                                    num2 = num9;
                                }
                                if (num7 == 70)
                                {
                                    num7 = num9;
                                }
                                if (num4 == 70)
                                {
                                    num4 = num9;
                                }
                                if (num5 == 70)
                                {
                                    num5 = num9;
                                }
                                if (num == 70)
                                {
                                    num = num9;
                                }
                                if (num3 == 70)
                                {
                                    num3 = num9;
                                }
                                if (num6 == 70)
                                {
                                    num6 = num9;
                                }
                                if (num8 == 70)
                                {
                                    num8 = num9;
                                }
                            }
                        }
                    }
                    if (Main.tileMergeDirt[num9])
                    {
                        if (num2 == 0)
                        {
                            num2 = -2;
                        }
                        if (num7 == 0)
                        {
                            num7 = -2;
                        }
                        if (num4 == 0)
                        {
                            num4 = -2;
                        }
                        if (num5 == 0)
                        {
                            num5 = -2;
                        }
                        if (num == 0)
                        {
                            num = -2;
                        }
                        if (num3 == 0)
                        {
                            num3 = -2;
                        }
                        if (num6 == 0)
                        {
                            num6 = -2;
                        }
                        if (num8 == 0)
                        {
                            num8 = -2;
                        }
                    }
                    else
                    {
                        if (num9 == 58)
                        {
                            if (num2 == 57)
                            {
                                num2 = -2;
                            }
                            if (num7 == 57)
                            {
                                num7 = -2;
                            }
                            if (num4 == 57)
                            {
                                num4 = -2;
                            }
                            if (num5 == 57)
                            {
                                num5 = -2;
                            }
                            if (num == 57)
                            {
                                num = -2;
                            }
                            if (num3 == 57)
                            {
                                num3 = -2;
                            }
                            if (num6 == 57)
                            {
                                num6 = -2;
                            }
                            if (num8 == 57)
                            {
                                num8 = -2;
                            }
                        }
                        else
                        {
                            if (num9 == 59)
                            {
                                if (num2 == 1)
                                {
                                    num2 = -2;
                                }
                                if (num7 == 1)
                                {
                                    num7 = -2;
                                }
                                if (num4 == 1)
                                {
                                    num4 = -2;
                                }
                                if (num5 == 1)
                                {
                                    num5 = -2;
                                }
                                if (num == 1)
                                {
                                    num = -2;
                                }
                                if (num3 == 1)
                                {
                                    num3 = -2;
                                }
                                if (num6 == 1)
                                {
                                    num6 = -2;
                                }
                                if (num8 == 1)
                                {
                                    num8 = -2;
                                }
                            }
                        }
                    }
                    if (num9 == 32)
                    {
                        if (num7 == 23)
                        {
                            num7 = num9;
                        }
                    }
                    else
                    {
                        if (num9 == 69)
                        {
                            if (num7 == 60)
                            {
                                num7 = num9;
                            }
                        }
                        else
                        {
                            if (num9 == 51)
                            {
                                if (num2 > -1 && !Main.tileNoAttach[num2])
                                {
                                    num2 = num9;
                                }
                                if (num7 > -1 && !Main.tileNoAttach[num7])
                                {
                                    num7 = num9;
                                }
                                if (num4 > -1 && !Main.tileNoAttach[num4])
                                {
                                    num4 = num9;
                                }
                                if (num5 > -1 && !Main.tileNoAttach[num5])
                                {
                                    num5 = num9;
                                }
                                if (num > -1 && !Main.tileNoAttach[num])
                                {
                                    num = num9;
                                }
                                if (num3 > -1 && !Main.tileNoAttach[num3])
                                {
                                    num3 = num9;
                                }
                                if (num6 > -1 && !Main.tileNoAttach[num6])
                                {
                                    num6 = num9;
                                }
                                if (num8 > -1 && !Main.tileNoAttach[num8])
                                {
                                    num8 = num9;
                                }
                            }
                        }
                    }
                    if (num2 > -1 && !Main.tileSolid[num2] && num2 != num9)
                    {
                        num2 = -1;
                    }
                    if (num7 > -1 && !Main.tileSolid[num7] && num7 != num9)
                    {
                        num7 = -1;
                    }
                    if (num4 > -1 && !Main.tileSolid[num4] && num4 != num9)
                    {
                        num4 = -1;
                    }
                    if (num5 > -1 && !Main.tileSolid[num5] && num5 != num9)
                    {
                        num5 = -1;
                    }
                    if (num > -1 && !Main.tileSolid[num] && num != num9)
                    {
                        num = -1;
                    }
                    if (num3 > -1 && !Main.tileSolid[num3] && num3 != num9)
                    {
                        num3 = -1;
                    }
                    if (num6 > -1 && !Main.tileSolid[num6] && num6 != num9)
                    {
                        num6 = -1;
                    }
                    if (num8 > -1 && !Main.tileSolid[num8] && num8 != num9)
                    {
                        num8 = -1;
                    }
                    if (num9 == 2 || num9 == 23 || num9 == 60 || num9 == 70)
                    {
                        int num23 = 0;
                        if (num9 == 60 || num9 == 70)
                        {
                            num23 = 59;
                        }
                        else
                        {
                            if (num9 == 2)
                            {
                                if (num2 == 23)
                                {
                                    num2 = num23;
                                }
                                if (num7 == 23)
                                {
                                    num7 = num23;
                                }
                                if (num4 == 23)
                                {
                                    num4 = num23;
                                }
                                if (num5 == 23)
                                {
                                    num5 = num23;
                                }
                                if (num == 23)
                                {
                                    num = num23;
                                }
                                if (num3 == 23)
                                {
                                    num3 = num23;
                                }
                                if (num6 == 23)
                                {
                                    num6 = num23;
                                }
                                if (num8 == 23)
                                {
                                    num8 = num23;
                                }
                            }
                            else
                            {
                                if (num9 == 23)
                                {
                                    if (num2 == 2)
                                    {
                                        num2 = num23;
                                    }
                                    if (num7 == 2)
                                    {
                                        num7 = num23;
                                    }
                                    if (num4 == 2)
                                    {
                                        num4 = num23;
                                    }
                                    if (num5 == 2)
                                    {
                                        num5 = num23;
                                    }
                                    if (num == 2)
                                    {
                                        num = num23;
                                    }
                                    if (num3 == 2)
                                    {
                                        num3 = num23;
                                    }
                                    if (num6 == 2)
                                    {
                                        num6 = num23;
                                    }
                                    if (num8 == 2)
                                    {
                                        num8 = num23;
                                    }
                                }
                            }
                        }
                        if (num2 != num9 && num2 != num23 && (num7 == num9 || num7 == num23))
                        {
                            if (num4 == num23 && num5 == num9)
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 0;
                                    rectangle.Y = 198;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 18;
                                    rectangle.Y = 198;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 36;
                                    rectangle.Y = 198;
                                }
                            }
                            else
                            {
                                if (num4 == num9 && num5 == num23)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 54;
                                        rectangle.Y = 198;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 72;
                                        rectangle.Y = 198;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 90;
                                        rectangle.Y = 198;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (num7 != num9 && num7 != num23 && (num2 == num9 || num2 == num23))
                            {
                                if (num4 == num23 && num5 == num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 0;
                                        rectangle.Y = 216;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 18;
                                        rectangle.Y = 216;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 216;
                                    }
                                }
                                else
                                {
                                    if (num4 == num9 && num5 == num23)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 216;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 216;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 216;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (num4 != num9 && num4 != num23 && (num5 == num9 || num5 == num23))
                                {
                                    if (num2 == num23 && num7 == num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 144;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 162;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 180;
                                        }
                                    }
                                    else
                                    {
                                        if (num7 == num9 && num5 == num2)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 90;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 108;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 126;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (num5 != num9 && num5 != num23 && (num4 == num9 || num4 == num23))
                                    {
                                        if (num2 == num23 && num7 == num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 144;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 162;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 180;
                                            }
                                        }
                                        else
                                        {
                                            if (num7 == num9 && num5 == num2)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 90;
                                                    rectangle.Y = 90;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 90;
                                                    rectangle.Y = 108;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 90;
                                                    rectangle.Y = 126;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (num2 == num9 && num7 == num9 && num4 == num9 && num5 == num9)
                                        {
                                            if (num != num9 && num3 != num9 && num6 != num9 && num8 != num9)
                                            {
                                                if (num8 == num23)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 108;
                                                        rectangle.Y = 324;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 126;
                                                        rectangle.Y = 324;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 144;
                                                        rectangle.Y = 324;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num3 == num23)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 342;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 126;
                                                            rectangle.Y = 342;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 144;
                                                            rectangle.Y = 342;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num6 == num23)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 108;
                                                                rectangle.Y = 360;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 360;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 144;
                                                                rectangle.Y = 360;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num == num23)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 108;
                                                                    rectangle.Y = 378;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 126;
                                                                    rectangle.Y = 378;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 144;
                                                                    rectangle.Y = 378;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 144;
                                                                    rectangle.Y = 234;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 198;
                                                                    rectangle.Y = 234;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 252;
                                                                    rectangle.Y = 234;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (num != num9 && num8 != num9)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 36;
                                                        rectangle.Y = 306;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 306;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 72;
                                                        rectangle.Y = 306;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num3 != num9 && num6 != num9)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 90;
                                                            rectangle.Y = 306;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 306;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 126;
                                                            rectangle.Y = 306;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num != num9 && num3 == num9 && num6 == num9 && num8 == num9)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 108;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 144;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 180;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num == num9 && num3 != num9 && num6 == num9 && num8 == num9)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 36;
                                                                    rectangle.Y = 108;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 36;
                                                                    rectangle.Y = 144;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 36;
                                                                    rectangle.Y = 180;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num == num9 && num3 == num9 && num6 != num9 && num8 == num9)
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 90;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 126;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 162;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num == num9 && num3 == num9 && num6 == num9 && num8 != num9)
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 90;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 126;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 162;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (num2 == num9 && num7 == num23 && num4 == num9 && num5 == num9 && num == -1 && num3 == -1)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 108;
                                                    rectangle.Y = 18;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 126;
                                                    rectangle.Y = 18;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 144;
                                                    rectangle.Y = 18;
                                                }
                                            }
                                            else
                                            {
                                                if (num2 == num23 && num7 == num9 && num4 == num9 && num5 == num9 && num6 == -1 && num8 == -1)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 108;
                                                        rectangle.Y = 36;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 126;
                                                        rectangle.Y = 36;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 144;
                                                        rectangle.Y = 36;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num2 == num9 && num7 == num9 && num4 == num23 && num5 == num9 && num3 == -1 && num8 == -1)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 198;
                                                            rectangle.Y = 0;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 198;
                                                            rectangle.Y = 18;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 198;
                                                            rectangle.Y = 36;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num2 == num9 && num7 == num9 && num4 == num9 && num5 == num23 && num == -1 && num6 == -1)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 180;
                                                                rectangle.Y = 0;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 180;
                                                                rectangle.Y = 18;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 180;
                                                                rectangle.Y = 36;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num2 == num9 && num7 == num23 && num4 == num9 && num5 == num9)
                                                            {
                                                                if (num3 != -1)
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 108;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 144;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 180;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num != -1)
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 108;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 144;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 36;
                                                                            rectangle.Y = 180;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num2 == num23 && num7 == num9 && num4 == num9 && num5 == num9)
                                                                {
                                                                    if (num8 != -1)
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 54;
                                                                            rectangle.Y = 90;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 54;
                                                                            rectangle.Y = 126;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 54;
                                                                            rectangle.Y = 162;
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num6 != -1)
                                                                        {
                                                                            if (num22 == 0)
                                                                            {
                                                                                rectangle.X = 36;
                                                                                rectangle.Y = 90;
                                                                            }
                                                                            if (num22 == 1)
                                                                            {
                                                                                rectangle.X = 36;
                                                                                rectangle.Y = 126;
                                                                            }
                                                                            if (num22 == 2)
                                                                            {
                                                                                rectangle.X = 36;
                                                                                rectangle.Y = 162;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num2 == num9 && num7 == num9 && num4 == num9 && num5 == num23)
                                                                    {
                                                                        if (num != -1)
                                                                        {
                                                                            if (num22 == 0)
                                                                            {
                                                                                rectangle.X = 54;
                                                                                rectangle.Y = 90;
                                                                            }
                                                                            if (num22 == 1)
                                                                            {
                                                                                rectangle.X = 54;
                                                                                rectangle.Y = 126;
                                                                            }
                                                                            if (num22 == 2)
                                                                            {
                                                                                rectangle.X = 54;
                                                                                rectangle.Y = 162;
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num6 != -1)
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 54;
                                                                                    rectangle.Y = 108;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 54;
                                                                                    rectangle.Y = 144;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 54;
                                                                                    rectangle.Y = 180;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num2 == num9 && num7 == num9 && num4 == num23 && num5 == num9)
                                                                        {
                                                                            if (num3 != -1)
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 36;
                                                                                    rectangle.Y = 90;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 36;
                                                                                    rectangle.Y = 126;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 36;
                                                                                    rectangle.Y = 162;
                                                                                }
                                                                            }
                                                                            else
                                                                            {
                                                                                if (num8 != -1)
                                                                                {
                                                                                    if (num22 == 0)
                                                                                    {
                                                                                        rectangle.X = 36;
                                                                                        rectangle.Y = 108;
                                                                                    }
                                                                                    if (num22 == 1)
                                                                                    {
                                                                                        rectangle.X = 36;
                                                                                        rectangle.Y = 144;
                                                                                    }
                                                                                    if (num22 == 2)
                                                                                    {
                                                                                        rectangle.X = 36;
                                                                                        rectangle.Y = 180;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if ((num2 == num23 && num7 == num9 && num4 == num9 && num5 == num9) || (num2 == num9 && num7 == num23 && num4 == num9 && num5 == num9) || (num2 == num9 && num7 == num9 && num4 == num23 && num5 == num9) || (num2 == num9 && num7 == num9 && num4 == num9 && num5 == num23))
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 18;
                                                                                    rectangle.Y = 18;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 36;
                                                                                    rectangle.Y = 18;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 54;
                                                                                    rectangle.Y = 18;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ((num2 == num9 || num2 == num23) && (num7 == num9 || num7 == num23) && (num4 == num9 || num4 == num23) && (num5 == num9 || num5 == num23))
                        {
                            if (num != num9 && num != num23 && (num3 == num9 || num3 == num23) && (num6 == num9 || num6 == num23) && (num8 == num9 || num8 == num23))
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 54;
                                    rectangle.Y = 108;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 54;
                                    rectangle.Y = 144;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 54;
                                    rectangle.Y = 180;
                                }
                            }
                            else
                            {
                                if (num3 != num9 && num3 != num23 && (num == num9 || num == num23) && (num6 == num9 || num6 == num23) && (num8 == num9 || num8 == num23))
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 108;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 144;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 180;
                                    }
                                }
                                else
                                {
                                    if (num6 != num9 && num6 != num23 && (num == num9 || num == num23) && (num3 == num9 || num3 == num23) && (num8 == num9 || num8 == num23))
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 90;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 126;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 162;
                                        }
                                    }
                                    else
                                    {
                                        if (num8 != num9 && num8 != num23 && (num == num9 || num == num23) && (num6 == num9 || num6 == num23) && (num3 == num9 || num3 == num23))
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 90;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 126;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 162;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (num2 != num23 && num2 != num9 && num7 == num9 && num4 != num23 && num4 != num9 && num5 == num9 && num8 != num23 && num8 != num9)
                        {
                            if (num22 == 0)
                            {
                                rectangle.X = 90;
                                rectangle.Y = 270;
                            }
                            if (num22 == 1)
                            {
                                rectangle.X = 108;
                                rectangle.Y = 270;
                            }
                            if (num22 == 2)
                            {
                                rectangle.X = 126;
                                rectangle.Y = 270;
                            }
                        }
                        else
                        {
                            if (num2 != num23 && num2 != num9 && num7 == num9 && num4 == num9 && num5 != num23 && num5 != num9 && num6 != num23 && num6 != num9)
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 144;
                                    rectangle.Y = 270;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 162;
                                    rectangle.Y = 270;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 180;
                                    rectangle.Y = 270;
                                }
                            }
                            else
                            {
                                if (num7 != num23 && num7 != num9 && num2 == num9 && num4 != num23 && num4 != num9 && num5 == num9 && num3 != num23 && num3 != num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 90;
                                        rectangle.Y = 288;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 108;
                                        rectangle.Y = 288;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 126;
                                        rectangle.Y = 288;
                                    }
                                }
                                else
                                {
                                    if (num7 != num23 && num7 != num9 && num2 == num9 && num4 == num9 && num5 != num23 && num5 != num9 && num != num23 && num != num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 144;
                                            rectangle.Y = 288;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 162;
                                            rectangle.Y = 288;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 180;
                                            rectangle.Y = 288;
                                        }
                                    }
                                    else
                                    {
                                        if (num2 != num9 && num2 != num23 && num7 == num9 && num4 == num9 && num5 == num9 && num6 != num9 && num6 != num23 && num8 != num9 && num8 != num23)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 144;
                                                rectangle.Y = 216;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 198;
                                                rectangle.Y = 216;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 252;
                                                rectangle.Y = 216;
                                            }
                                        }
                                        else
                                        {
                                            if (num7 != num9 && num7 != num23 && num2 == num9 && num4 == num9 && num5 == num9 && num != num9 && num != num23 && num3 != num9 && num3 != num23)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 144;
                                                    rectangle.Y = 252;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 198;
                                                    rectangle.Y = 252;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 252;
                                                    rectangle.Y = 252;
                                                }
                                            }
                                            else
                                            {
                                                if (num4 != num9 && num4 != num23 && num7 == num9 && num2 == num9 && num5 == num9 && num3 != num9 && num3 != num23 && num8 != num9 && num8 != num23)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 126;
                                                        rectangle.Y = 234;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 180;
                                                        rectangle.Y = 234;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 234;
                                                        rectangle.Y = 234;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num5 != num9 && num5 != num23 && num7 == num9 && num2 == num9 && num4 == num9 && num != num9 && num != num23 && num6 != num9 && num6 != num23)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 162;
                                                            rectangle.Y = 234;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 216;
                                                            rectangle.Y = 234;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 270;
                                                            rectangle.Y = 234;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num2 != num23 && num2 != num9 && (num7 == num23 || num7 == num9) && num4 == num23 && num5 == num23)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 36;
                                                                rectangle.Y = 270;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 270;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 72;
                                                                rectangle.Y = 270;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num7 != num23 && num7 != num9 && (num2 == num23 || num2 == num9) && num4 == num23 && num5 == num23)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 36;
                                                                    rectangle.Y = 288;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 54;
                                                                    rectangle.Y = 288;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 72;
                                                                    rectangle.Y = 288;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num4 != num23 && num4 != num9 && (num5 == num23 || num5 == num9) && num2 == num23 && num7 == num23)
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 0;
                                                                        rectangle.Y = 270;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 0;
                                                                        rectangle.Y = 288;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 0;
                                                                        rectangle.Y = 306;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num5 != num23 && num5 != num9 && (num4 == num23 || num4 == num9) && num2 == num23 && num7 == num23)
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 18;
                                                                            rectangle.Y = 270;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 18;
                                                                            rectangle.Y = 288;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 18;
                                                                            rectangle.Y = 306;
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num2 == num9 && num7 == num23 && num4 == num23 && num5 == num23)
                                                                        {
                                                                            if (num22 == 0)
                                                                            {
                                                                                rectangle.X = 198;
                                                                                rectangle.Y = 288;
                                                                            }
                                                                            if (num22 == 1)
                                                                            {
                                                                                rectangle.X = 216;
                                                                                rectangle.Y = 288;
                                                                            }
                                                                            if (num22 == 2)
                                                                            {
                                                                                rectangle.X = 234;
                                                                                rectangle.Y = 288;
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num2 == num23 && num7 == num9 && num4 == num23 && num5 == num23)
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 198;
                                                                                    rectangle.Y = 270;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 216;
                                                                                    rectangle.Y = 270;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 234;
                                                                                    rectangle.Y = 270;
                                                                                }
                                                                            }
                                                                            else
                                                                            {
                                                                                if (num2 == num23 && num7 == num23 && num4 == num9 && num5 == num23)
                                                                                {
                                                                                    if (num22 == 0)
                                                                                    {
                                                                                        rectangle.X = 198;
                                                                                        rectangle.Y = 306;
                                                                                    }
                                                                                    if (num22 == 1)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 306;
                                                                                    }
                                                                                    if (num22 == 2)
                                                                                    {
                                                                                        rectangle.X = 234;
                                                                                        rectangle.Y = 306;
                                                                                    }
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (num2 == num23 && num7 == num23 && num4 == num23 && num5 == num9)
                                                                                    {
                                                                                        if (num22 == 0)
                                                                                        {
                                                                                            rectangle.X = 144;
                                                                                            rectangle.Y = 306;
                                                                                        }
                                                                                        if (num22 == 1)
                                                                                        {
                                                                                            rectangle.X = 162;
                                                                                            rectangle.Y = 306;
                                                                                        }
                                                                                        if (num22 == 2)
                                                                                        {
                                                                                            rectangle.X = 180;
                                                                                            rectangle.Y = 306;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (num2 != num9 && num2 != num23 && num7 == num9 && num4 == num9 && num5 == num9)
                        {
                            if ((num6 == num23 || num6 == num9) && num8 != num23 && num8 != num9)
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 0;
                                    rectangle.Y = 324;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 18;
                                    rectangle.Y = 324;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 36;
                                    rectangle.Y = 324;
                                }
                            }
                            else
                            {
                                if ((num8 == num23 || num8 == num9) && num6 != num23 && num6 != num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 54;
                                        rectangle.Y = 324;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 72;
                                        rectangle.Y = 324;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 90;
                                        rectangle.Y = 324;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (num7 != num9 && num7 != num23 && num2 == num9 && num4 == num9 && num5 == num9)
                            {
                                if ((num == num23 || num == num9) && num3 != num23 && num3 != num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 0;
                                        rectangle.Y = 342;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 18;
                                        rectangle.Y = 342;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 342;
                                    }
                                }
                                else
                                {
                                    if ((num3 == num23 || num3 == num9) && num != num23 && num != num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 342;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 342;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 342;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (num4 != num9 && num4 != num23 && num2 == num9 && num7 == num9 && num5 == num9)
                                {
                                    if ((num3 == num23 || num3 == num9) && num8 != num23 && num8 != num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 54;
                                            rectangle.Y = 360;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 360;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 360;
                                        }
                                    }
                                    else
                                    {
                                        if ((num8 == num23 || num8 == num9) && num3 != num23 && num3 != num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 0;
                                                rectangle.Y = 360;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 18;
                                                rectangle.Y = 360;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 360;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (num5 != num9 && num5 != num23 && num2 == num9 && num7 == num9 && num4 == num9)
                                    {
                                        if ((num == num23 || num == num9) && num6 != num23 && num6 != num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 0;
                                                rectangle.Y = 378;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 18;
                                                rectangle.Y = 378;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 378;
                                            }
                                        }
                                        else
                                        {
                                            if ((num6 == num23 || num6 == num9) && num != num23 && num != num9)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 54;
                                                    rectangle.Y = 378;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 72;
                                                    rectangle.Y = 378;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 90;
                                                    rectangle.Y = 378;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ((num2 == num9 || num2 == num23) && (num7 == num9 || num7 == num23) && (num4 == num9 || num4 == num23) && (num5 == num9 || num5 == num23) && num != -1 && num3 != -1 && num6 != -1 && num8 != -1)
                        {
                            if (num22 == 0)
                            {
                                rectangle.X = 18;
                                rectangle.Y = 18;
                            }
                            if (num22 == 1)
                            {
                                rectangle.X = 36;
                                rectangle.Y = 18;
                            }
                            if (num22 == 2)
                            {
                                rectangle.X = 54;
                                rectangle.Y = 18;
                            }
                        }
                        if (num2 == num23)
                        {
                            num2 = -2;
                        }
                        if (num7 == num23)
                        {
                            num7 = -2;
                        }
                        if (num4 == num23)
                        {
                            num4 = -2;
                        }
                        if (num5 == num23)
                        {
                            num5 = -2;
                        }
                        if (num == num23)
                        {
                            num = -2;
                        }
                        if (num3 == num23)
                        {
                            num3 = -2;
                        }
                        if (num6 == num23)
                        {
                            num6 = -2;
                        }
                        if (num8 == num23)
                        {
                            num8 = -2;
                        }
                    }
                    if ((num9 == 1 || num9 == 2 || num9 == 6 || num9 == 7 || num9 == 8 || num9 == 9 || num9 == 22 || num9 == 23 || num9 == 25 || num9 == 37 || num9 == 40 || num9 == 53 || num9 == 56 || num9 == 58 || num9 == 59 || num9 == 60 || num9 == 70) && rectangle.X == -1 && rectangle.Y == -1)
                    {
                        if (num2 >= 0 && num2 != num9)
                        {
                            num2 = -1;
                        }
                        if (num7 >= 0 && num7 != num9)
                        {
                            num7 = -1;
                        }
                        if (num4 >= 0 && num4 != num9)
                        {
                            num4 = -1;
                        }
                        if (num5 >= 0 && num5 != num9)
                        {
                            num5 = -1;
                        }
                        if (num2 != -1 && num7 != -1 && num4 != -1 && num5 != -1)
                        {
                            if (num2 == -2 && num7 == num9 && num4 == num9 && num5 == num9)
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 144;
                                    rectangle.Y = 108;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 162;
                                    rectangle.Y = 108;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 180;
                                    rectangle.Y = 108;
                                }
                                WorldModify.mergeUp = true;
                            }
                            else
                            {
                                if (num2 == num9 && num7 == -2 && num4 == num9 && num5 == num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 144;
                                        rectangle.Y = 90;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 162;
                                        rectangle.Y = 90;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 180;
                                        rectangle.Y = 90;
                                    }
                                    WorldModify.mergeDown = true;
                                }
                                else
                                {
                                    if (num2 == num9 && num7 == num9 && num4 == -2 && num5 == num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 162;
                                            rectangle.Y = 126;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 162;
                                            rectangle.Y = 144;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 162;
                                            rectangle.Y = 162;
                                        }
                                        WorldModify.mergeLeft = true;
                                    }
                                    else
                                    {
                                        if (num2 == num9 && num7 == num9 && num4 == num9 && num5 == -2)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 144;
                                                rectangle.Y = 126;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 144;
                                                rectangle.Y = 144;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 144;
                                                rectangle.Y = 162;
                                            }
                                            WorldModify.mergeRight = true;
                                        }
                                        else
                                        {
                                            if (num2 == -2 && num7 == num9 && num4 == -2 && num5 == num9)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 36;
                                                    rectangle.Y = 90;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 36;
                                                    rectangle.Y = 126;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 36;
                                                    rectangle.Y = 162;
                                                }
                                                WorldModify.mergeUp = true;
                                                WorldModify.mergeLeft = true;
                                            }
                                            else
                                            {
                                                if (num2 == -2 && num7 == num9 && num4 == num9 && num5 == -2)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 90;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 126;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 162;
                                                    }
                                                    WorldModify.mergeUp = true;
                                                    WorldModify.mergeRight = true;
                                                }
                                                else
                                                {
                                                    if (num2 == num9 && num7 == -2 && num4 == -2 && num5 == num9)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 36;
                                                            rectangle.Y = 108;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 36;
                                                            rectangle.Y = 144;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 36;
                                                            rectangle.Y = 180;
                                                        }
                                                        WorldModify.mergeDown = true;
                                                        WorldModify.mergeLeft = true;
                                                    }
                                                    else
                                                    {
                                                        if (num2 == num9 && num7 == -2 && num4 == num9 && num5 == -2)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 108;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 144;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 180;
                                                            }
                                                            WorldModify.mergeDown = true;
                                                            WorldModify.mergeRight = true;
                                                        }
                                                        else
                                                        {
                                                            if (num2 == num9 && num7 == num9 && num4 == -2 && num5 == -2)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 180;
                                                                    rectangle.Y = 126;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 180;
                                                                    rectangle.Y = 144;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 180;
                                                                    rectangle.Y = 162;
                                                                }
                                                                WorldModify.mergeLeft = true;
                                                                WorldModify.mergeRight = true;
                                                            }
                                                            else
                                                            {
                                                                if (num2 == -2 && num7 == -2 && num4 == num9 && num5 == num9)
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 144;
                                                                        rectangle.Y = 180;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 162;
                                                                        rectangle.Y = 180;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 180;
                                                                        rectangle.Y = 180;
                                                                    }
                                                                    WorldModify.mergeUp = true;
                                                                    WorldModify.mergeDown = true;
                                                                }
                                                                else
                                                                {
                                                                    if (num2 == -2 && num7 == num9 && num4 == -2 && num5 == -2)
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 198;
                                                                            rectangle.Y = 90;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 198;
                                                                            rectangle.Y = 108;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 198;
                                                                            rectangle.Y = 126;
                                                                        }
                                                                        WorldModify.mergeUp = true;
                                                                        WorldModify.mergeLeft = true;
                                                                        WorldModify.mergeRight = true;
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num2 == num9 && num7 == -2 && num4 == -2 && num5 == -2)
                                                                        {
                                                                            if (num22 == 0)
                                                                            {
                                                                                rectangle.X = 198;
                                                                                rectangle.Y = 144;
                                                                            }
                                                                            if (num22 == 1)
                                                                            {
                                                                                rectangle.X = 198;
                                                                                rectangle.Y = 162;
                                                                            }
                                                                            if (num22 == 2)
                                                                            {
                                                                                rectangle.X = 198;
                                                                                rectangle.Y = 180;
                                                                            }
                                                                            WorldModify.mergeDown = true;
                                                                            WorldModify.mergeLeft = true;
                                                                            WorldModify.mergeRight = true;
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num2 == -2 && num7 == -2 && num4 == num9 && num5 == -2)
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 216;
                                                                                    rectangle.Y = 144;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 216;
                                                                                    rectangle.Y = 162;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 216;
                                                                                    rectangle.Y = 180;
                                                                                }
                                                                                WorldModify.mergeUp = true;
                                                                                WorldModify.mergeDown = true;
                                                                                WorldModify.mergeRight = true;
                                                                            }
                                                                            else
                                                                            {
                                                                                if (num2 == -2 && num7 == -2 && num4 == -2 && num5 == num9)
                                                                                {
                                                                                    if (num22 == 0)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 90;
                                                                                    }
                                                                                    if (num22 == 1)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 108;
                                                                                    }
                                                                                    if (num22 == 2)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 126;
                                                                                    }
                                                                                    WorldModify.mergeUp = true;
                                                                                    WorldModify.mergeDown = true;
                                                                                    WorldModify.mergeLeft = true;
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (num2 == -2 && num7 == -2 && num4 == -2 && num5 == -2)
                                                                                    {
                                                                                        if (num22 == 0)
                                                                                        {
                                                                                            rectangle.X = 108;
                                                                                            rectangle.Y = 198;
                                                                                        }
                                                                                        if (num22 == 1)
                                                                                        {
                                                                                            rectangle.X = 126;
                                                                                            rectangle.Y = 198;
                                                                                        }
                                                                                        if (num22 == 2)
                                                                                        {
                                                                                            rectangle.X = 144;
                                                                                            rectangle.Y = 198;
                                                                                        }
                                                                                        WorldModify.mergeUp = true;
                                                                                        WorldModify.mergeDown = true;
                                                                                        WorldModify.mergeLeft = true;
                                                                                        WorldModify.mergeRight = true;
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        if (num2 == num9 && num7 == num9 && num4 == num9 && num5 == num9)
                                                                                        {
                                                                                            if (num == -2)
                                                                                            {
                                                                                                if (num22 == 0)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 108;
                                                                                                }
                                                                                                if (num22 == 1)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 144;
                                                                                                }
                                                                                                if (num22 == 2)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 180;
                                                                                                }
                                                                                            }
                                                                                            if (num3 == -2)
                                                                                            {
                                                                                                if (num22 == 0)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 108;
                                                                                                }
                                                                                                if (num22 == 1)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 144;
                                                                                                }
                                                                                                if (num22 == 2)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 180;
                                                                                                }
                                                                                            }
                                                                                            if (num6 == -2)
                                                                                            {
                                                                                                if (num22 == 0)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 90;
                                                                                                }
                                                                                                if (num22 == 1)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 126;
                                                                                                }
                                                                                                if (num22 == 2)
                                                                                                {
                                                                                                    rectangle.X = 18;
                                                                                                    rectangle.Y = 162;
                                                                                                }
                                                                                            }
                                                                                            if (num8 == -2)
                                                                                            {
                                                                                                if (num22 == 0)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 90;
                                                                                                }
                                                                                                if (num22 == 1)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 126;
                                                                                                }
                                                                                                if (num22 == 2)
                                                                                                {
                                                                                                    rectangle.X = 0;
                                                                                                    rectangle.Y = 162;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (num9 != 2 && num9 != 23 && num9 != 60 && num9 != 70)
                            {
                                if (num2 == -1 && num7 == -2 && num4 == num9 && num5 == num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 234;
                                        rectangle.Y = 0;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 252;
                                        rectangle.Y = 0;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 270;
                                        rectangle.Y = 0;
                                    }
                                    WorldModify.mergeDown = true;
                                }
                                else
                                {
                                    if (num2 == -2 && num7 == -1 && num4 == num9 && num5 == num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 234;
                                            rectangle.Y = 18;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 252;
                                            rectangle.Y = 18;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 270;
                                            rectangle.Y = 18;
                                        }
                                        WorldModify.mergeUp = true;
                                    }
                                    else
                                    {
                                        if (num2 == num9 && num7 == num9 && num4 == -1 && num5 == -2)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 234;
                                                rectangle.Y = 36;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 252;
                                                rectangle.Y = 36;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 270;
                                                rectangle.Y = 36;
                                            }
                                            WorldModify.mergeRight = true;
                                        }
                                        else
                                        {
                                            if (num2 == num9 && num7 == num9 && num4 == -2 && num5 == -1)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 234;
                                                    rectangle.Y = 54;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 252;
                                                    rectangle.Y = 54;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 270;
                                                    rectangle.Y = 54;
                                                }
                                                WorldModify.mergeLeft = true;
                                            }
                                        }
                                    }
                                }
                            }
                            if (num2 != -1 && num7 != -1 && num4 == -1 && num5 == num9)
                            {
                                if (num2 == -2 && num7 == num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 72;
                                        rectangle.Y = 144;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 72;
                                        rectangle.Y = 162;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 72;
                                        rectangle.Y = 180;
                                    }
                                    WorldModify.mergeUp = true;
                                }
                                else
                                {
                                    if (num7 == -2 && num2 == num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 90;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 108;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 72;
                                            rectangle.Y = 126;
                                        }
                                        WorldModify.mergeDown = true;
                                    }
                                }
                            }
                            else
                            {
                                if (num2 != -1 && num7 != -1 && num4 == num9 && num5 == -1)
                                {
                                    if (num2 == -2 && num7 == num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 144;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 162;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 90;
                                            rectangle.Y = 180;
                                        }
                                        WorldModify.mergeUp = true;
                                    }
                                    else
                                    {
                                        if (num7 == -2 && num2 == num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 90;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 108;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 90;
                                                rectangle.Y = 126;
                                            }
                                            WorldModify.mergeDown = true;
                                        }
                                    }
                                }
                                else
                                {
                                    if (num2 == -1 && num7 == num9 && num4 != -1 && num5 != -1)
                                    {
                                        if (num4 == -2 && num5 == num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 0;
                                                rectangle.Y = 198;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 18;
                                                rectangle.Y = 198;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 198;
                                            }
                                            WorldModify.mergeLeft = true;
                                        }
                                        else
                                        {
                                            if (num5 == -2 && num4 == num9)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 54;
                                                    rectangle.Y = 198;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 72;
                                                    rectangle.Y = 198;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 90;
                                                    rectangle.Y = 198;
                                                }
                                                WorldModify.mergeRight = true;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (num2 == num9 && num7 == -1 && num4 != -1 && num5 != -1)
                                        {
                                            if (num4 == -2 && num5 == num9)
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 0;
                                                    rectangle.Y = 216;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 18;
                                                    rectangle.Y = 216;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 36;
                                                    rectangle.Y = 216;
                                                }
                                                WorldModify.mergeLeft = true;
                                            }
                                            else
                                            {
                                                if (num5 == -2 && num4 == num9)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 216;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 72;
                                                        rectangle.Y = 216;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 90;
                                                        rectangle.Y = 216;
                                                    }
                                                    WorldModify.mergeRight = true;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (num2 != -1 && num7 != -1 && num4 == -1 && num5 == -1)
                                            {
                                                if (num2 == -2 && num7 == -2)
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 108;
                                                        rectangle.Y = 216;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 108;
                                                        rectangle.Y = 234;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 108;
                                                        rectangle.Y = 252;
                                                    }
                                                    WorldModify.mergeUp = true;
                                                    WorldModify.mergeDown = true;
                                                }
                                                else
                                                {
                                                    if (num2 == -2)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 126;
                                                            rectangle.Y = 144;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 126;
                                                            rectangle.Y = 162;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 126;
                                                            rectangle.Y = 180;
                                                        }
                                                        WorldModify.mergeUp = true;
                                                    }
                                                    else
                                                    {
                                                        if (num7 == -2)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 90;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 108;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 126;
                                                                rectangle.Y = 126;
                                                            }
                                                            WorldModify.mergeDown = true;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (num2 == -1 && num7 == -1 && num4 != -1 && num5 != -1)
                                                {
                                                    if (num4 == -2 && num5 == -2)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 162;
                                                            rectangle.Y = 198;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 180;
                                                            rectangle.Y = 198;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 198;
                                                            rectangle.Y = 198;
                                                        }
                                                        WorldModify.mergeLeft = true;
                                                        WorldModify.mergeRight = true;
                                                    }
                                                    else
                                                    {
                                                        if (num4 == -2)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 0;
                                                                rectangle.Y = 252;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 18;
                                                                rectangle.Y = 252;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 36;
                                                                rectangle.Y = 252;
                                                            }
                                                            WorldModify.mergeLeft = true;
                                                        }
                                                        else
                                                        {
                                                            if (num5 == -2)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 54;
                                                                    rectangle.Y = 252;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 72;
                                                                    rectangle.Y = 252;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 90;
                                                                    rectangle.Y = 252;
                                                                }
                                                                WorldModify.mergeRight = true;
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (num2 == -2 && num7 == -1 && num4 == -1 && num5 == -1)
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 144;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 162;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 108;
                                                            rectangle.Y = 180;
                                                        }
                                                        WorldModify.mergeUp = true;
                                                    }
                                                    else
                                                    {
                                                        if (num2 == -1 && num7 == -2 && num4 == -1 && num5 == -1)
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 108;
                                                                rectangle.Y = 90;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 108;
                                                                rectangle.Y = 108;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 108;
                                                                rectangle.Y = 126;
                                                            }
                                                            WorldModify.mergeDown = true;
                                                        }
                                                        else
                                                        {
                                                            if (num2 == -1 && num7 == -1 && num4 == -2 && num5 == -1)
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 0;
                                                                    rectangle.Y = 234;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 18;
                                                                    rectangle.Y = 234;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 36;
                                                                    rectangle.Y = 234;
                                                                }
                                                                WorldModify.mergeLeft = true;
                                                            }
                                                            else
                                                            {
                                                                if (num2 == -1 && num7 == -1 && num4 == -1 && num5 == -2)
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 54;
                                                                        rectangle.Y = 234;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 72;
                                                                        rectangle.Y = 234;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 90;
                                                                        rectangle.Y = 234;
                                                                    }
                                                                    WorldModify.mergeRight = true;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (rectangle.X < 0 || rectangle.Y < 0)
                    {
                        if (num9 == 2 || num9 == 23 || num9 == 60 || num9 == 70)
                        {
                            if (num2 == -2)
                            {
                                num2 = num9;
                            }
                            if (num7 == -2)
                            {
                                num7 = num9;
                            }
                            if (num4 == -2)
                            {
                                num4 = num9;
                            }
                            if (num5 == -2)
                            {
                                num5 = num9;
                            }
                            if (num == -2)
                            {
                                num = num9;
                            }
                            if (num3 == -2)
                            {
                                num3 = num9;
                            }
                            if (num6 == -2)
                            {
                                num6 = num9;
                            }
                            if (num8 == -2)
                            {
                                num8 = num9;
                            }
                        }
                        if (num2 == num9 && num7 == num9 && (num4 == num9 & num5 == num9))
                        {
                            if (num != num9 && num3 != num9)
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 108;
                                    rectangle.Y = 18;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 126;
                                    rectangle.Y = 18;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 144;
                                    rectangle.Y = 18;
                                }
                            }
                            else
                            {
                                if (num6 != num9 && num8 != num9)
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 108;
                                        rectangle.Y = 36;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 126;
                                        rectangle.Y = 36;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 144;
                                        rectangle.Y = 36;
                                    }
                                }
                                else
                                {
                                    if (num != num9 && num6 != num9)
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 180;
                                            rectangle.Y = 0;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 180;
                                            rectangle.Y = 18;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 180;
                                            rectangle.Y = 36;
                                        }
                                    }
                                    else
                                    {
                                        if (num3 != num9 && num8 != num9)
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 198;
                                                rectangle.Y = 0;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 198;
                                                rectangle.Y = 18;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 198;
                                                rectangle.Y = 36;
                                            }
                                        }
                                        else
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 18;
                                                rectangle.Y = 18;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 36;
                                                rectangle.Y = 18;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 54;
                                                rectangle.Y = 18;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (num2 != num9 && num7 == num9 && (num4 == num9 & num5 == num9))
                            {
                                if (num22 == 0)
                                {
                                    rectangle.X = 18;
                                    rectangle.Y = 0;
                                }
                                if (num22 == 1)
                                {
                                    rectangle.X = 36;
                                    rectangle.Y = 0;
                                }
                                if (num22 == 2)
                                {
                                    rectangle.X = 54;
                                    rectangle.Y = 0;
                                }
                            }
                            else
                            {
                                if (num2 == num9 && num7 != num9 && (num4 == num9 & num5 == num9))
                                {
                                    if (num22 == 0)
                                    {
                                        rectangle.X = 18;
                                        rectangle.Y = 36;
                                    }
                                    if (num22 == 1)
                                    {
                                        rectangle.X = 36;
                                        rectangle.Y = 36;
                                    }
                                    if (num22 == 2)
                                    {
                                        rectangle.X = 54;
                                        rectangle.Y = 36;
                                    }
                                }
                                else
                                {
                                    if (num2 == num9 && num7 == num9 && (num4 != num9 & num5 == num9))
                                    {
                                        if (num22 == 0)
                                        {
                                            rectangle.X = 0;
                                            rectangle.Y = 0;
                                        }
                                        if (num22 == 1)
                                        {
                                            rectangle.X = 0;
                                            rectangle.Y = 18;
                                        }
                                        if (num22 == 2)
                                        {
                                            rectangle.X = 0;
                                            rectangle.Y = 36;
                                        }
                                    }
                                    else
                                    {
                                        if (num2 == num9 && num7 == num9 && (num4 == num9 & num5 != num9))
                                        {
                                            if (num22 == 0)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 0;
                                            }
                                            if (num22 == 1)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 18;
                                            }
                                            if (num22 == 2)
                                            {
                                                rectangle.X = 72;
                                                rectangle.Y = 36;
                                            }
                                        }
                                        else
                                        {
                                            if (num2 != num9 && num7 == num9 && (num4 != num9 & num5 == num9))
                                            {
                                                if (num22 == 0)
                                                {
                                                    rectangle.X = 0;
                                                    rectangle.Y = 54;
                                                }
                                                if (num22 == 1)
                                                {
                                                    rectangle.X = 36;
                                                    rectangle.Y = 54;
                                                }
                                                if (num22 == 2)
                                                {
                                                    rectangle.X = 72;
                                                    rectangle.Y = 54;
                                                }
                                            }
                                            else
                                            {
                                                if (num2 != num9 && num7 == num9 && (num4 == num9 & num5 != num9))
                                                {
                                                    if (num22 == 0)
                                                    {
                                                        rectangle.X = 18;
                                                        rectangle.Y = 54;
                                                    }
                                                    if (num22 == 1)
                                                    {
                                                        rectangle.X = 54;
                                                        rectangle.Y = 54;
                                                    }
                                                    if (num22 == 2)
                                                    {
                                                        rectangle.X = 90;
                                                        rectangle.Y = 54;
                                                    }
                                                }
                                                else
                                                {
                                                    if (num2 == num9 && num7 != num9 && (num4 != num9 & num5 == num9))
                                                    {
                                                        if (num22 == 0)
                                                        {
                                                            rectangle.X = 0;
                                                            rectangle.Y = 72;
                                                        }
                                                        if (num22 == 1)
                                                        {
                                                            rectangle.X = 36;
                                                            rectangle.Y = 72;
                                                        }
                                                        if (num22 == 2)
                                                        {
                                                            rectangle.X = 72;
                                                            rectangle.Y = 72;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (num2 == num9 && num7 != num9 && (num4 == num9 & num5 != num9))
                                                        {
                                                            if (num22 == 0)
                                                            {
                                                                rectangle.X = 18;
                                                                rectangle.Y = 72;
                                                            }
                                                            if (num22 == 1)
                                                            {
                                                                rectangle.X = 54;
                                                                rectangle.Y = 72;
                                                            }
                                                            if (num22 == 2)
                                                            {
                                                                rectangle.X = 90;
                                                                rectangle.Y = 72;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (num2 == num9 && num7 == num9 && (num4 != num9 & num5 != num9))
                                                            {
                                                                if (num22 == 0)
                                                                {
                                                                    rectangle.X = 90;
                                                                    rectangle.Y = 0;
                                                                }
                                                                if (num22 == 1)
                                                                {
                                                                    rectangle.X = 90;
                                                                    rectangle.Y = 18;
                                                                }
                                                                if (num22 == 2)
                                                                {
                                                                    rectangle.X = 90;
                                                                    rectangle.Y = 36;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (num2 != num9 && num7 != num9 && (num4 == num9 & num5 == num9))
                                                                {
                                                                    if (num22 == 0)
                                                                    {
                                                                        rectangle.X = 108;
                                                                        rectangle.Y = 72;
                                                                    }
                                                                    if (num22 == 1)
                                                                    {
                                                                        rectangle.X = 126;
                                                                        rectangle.Y = 72;
                                                                    }
                                                                    if (num22 == 2)
                                                                    {
                                                                        rectangle.X = 144;
                                                                        rectangle.Y = 72;
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (num2 != num9 && num7 == num9 && (num4 != num9 & num5 != num9))
                                                                    {
                                                                        if (num22 == 0)
                                                                        {
                                                                            rectangle.X = 108;
                                                                            rectangle.Y = 0;
                                                                        }
                                                                        if (num22 == 1)
                                                                        {
                                                                            rectangle.X = 126;
                                                                            rectangle.Y = 0;
                                                                        }
                                                                        if (num22 == 2)
                                                                        {
                                                                            rectangle.X = 144;
                                                                            rectangle.Y = 0;
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (num2 == num9 && num7 != num9 && (num4 != num9 & num5 != num9))
                                                                        {
                                                                            if (num22 == 0)
                                                                            {
                                                                                rectangle.X = 108;
                                                                                rectangle.Y = 54;
                                                                            }
                                                                            if (num22 == 1)
                                                                            {
                                                                                rectangle.X = 126;
                                                                                rectangle.Y = 54;
                                                                            }
                                                                            if (num22 == 2)
                                                                            {
                                                                                rectangle.X = 144;
                                                                                rectangle.Y = 54;
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            if (num2 != num9 && num7 != num9 && (num4 != num9 & num5 == num9))
                                                                            {
                                                                                if (num22 == 0)
                                                                                {
                                                                                    rectangle.X = 162;
                                                                                    rectangle.Y = 0;
                                                                                }
                                                                                if (num22 == 1)
                                                                                {
                                                                                    rectangle.X = 162;
                                                                                    rectangle.Y = 18;
                                                                                }
                                                                                if (num22 == 2)
                                                                                {
                                                                                    rectangle.X = 162;
                                                                                    rectangle.Y = 36;
                                                                                }
                                                                            }
                                                                            else
                                                                            {
                                                                                if (num2 != num9 && num7 != num9 && (num4 == num9 & num5 != num9))
                                                                                {
                                                                                    if (num22 == 0)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 0;
                                                                                    }
                                                                                    if (num22 == 1)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 18;
                                                                                    }
                                                                                    if (num22 == 2)
                                                                                    {
                                                                                        rectangle.X = 216;
                                                                                        rectangle.Y = 36;
                                                                                    }
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (num2 != num9 && num7 != num9 && (num4 != num9 & num5 != num9))
                                                                                    {
                                                                                        if (num22 == 0)
                                                                                        {
                                                                                            rectangle.X = 162;
                                                                                            rectangle.Y = 54;
                                                                                        }
                                                                                        if (num22 == 1)
                                                                                        {
                                                                                            rectangle.X = 180;
                                                                                            rectangle.Y = 54;
                                                                                        }
                                                                                        if (num22 == 2)
                                                                                        {
                                                                                            rectangle.X = 198;
                                                                                            rectangle.Y = 54;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (rectangle.X <= -1 || rectangle.Y <= -1)
                    {
                        if (num22 <= 0)
                        {
                            rectangle.X = 18;
                            rectangle.Y = 18;
                        }
                        if (num22 == 1)
                        {
                            rectangle.X = 36;
                            rectangle.Y = 18;
                        }
                        if (num22 >= 2)
                        {
                            rectangle.X = 54;
                            rectangle.Y = 18;
                        }
                    }
                    Main.tile.At(i, j).SetFrameX((short)rectangle.X);
                    Main.tile.At(i, j).SetFrameY((short)rectangle.Y);
                    if (num9 == 52 || num9 == 62)
                    {
                        if (!Main.tile.At(i, j - 1).Active)
                        {
                            num2 = -1;
                        }
                        else
                        {
                            num2 = (int)Main.tile.At(i, j - 1).Type;
                        }
                        if (num2 != num9 && num2 != 2 && num2 != 60)
                        {
                            WorldModify.KillTile(i, j, false, false, false);
                        }
                    }
                    if (!WorldModify.noTileActions && num9 == 53)
                    {
                        if (!Main.tile.At(i, j + 1).Active)
                            {
                                bool flag4 = true;
                                if (Main.tile.At(i, j - 1).Active && Main.tile.At(i, j - 1).Type == 21)
                                {
                                    flag4 = false;
                                }
                                if (flag4)
                                {
                                    int type2 = 31;
                                    if (num9 == 59)
                                    {
                                        type2 = 39;
                                    }
                                    if (num9 == 57)
                                    {
                                        type2 = 40;
                                    }
                                    Main.tile.At(i, j).SetActive(false);
                                    int num25 = Projectile.NewProjectile((float)(i * 16 + 8), (float)(j * 16 + 8), 0f, 0.41f, type2, 10, 0f, Main.myPlayer);
                                    Main.projectile[num25].Velocity.Y = 0.5f;
                                    Projectile expr_6501_cp_0 = Main.projectile[num25];
                                    expr_6501_cp_0.Position.Y = expr_6501_cp_0.Position.Y + 2f;
                                    Main.projectile[num25].ai[0] = 1f;
                                    NetMessage.SendTileSquare(-1, i, j, 1);
                                    WorldModify.SquareTileFrame(i, j, true);
                                }
                            }
                    }
                    if (rectangle.X != frameX && rectangle.Y != frameY && frameX >= 0 && frameY >= 0)
                    {
                        bool flag5 = WorldModify.mergeUp;
                        bool flag6 = WorldModify.mergeDown;
                        bool flag7 = WorldModify.mergeLeft;
                        bool flag8 = WorldModify.mergeRight;
                        WorldModify.TileFrame(i - 1, j, false, false);
                        WorldModify.TileFrame(i + 1, j, false, false);
                        WorldModify.TileFrame(i, j - 1, false, false);
                        WorldModify.TileFrame(i, j + 1, false, false);
                        WorldModify.mergeUp = flag5;
                        WorldModify.mergeDown = flag6;
                        WorldModify.mergeLeft = flag7;
                        WorldModify.mergeRight = flag8;
                    }
                }
            }
        }
        /// <summary>
        /// Spawns specified NPC type on specified player. (Generally a BOSS)
        /// </summary>
        /// <param name="playerIndex">Index of player to spawn on</param>
        /// <param name="Type">Type of NPC to spawn</param>
        /// <param name="makespawn">Forces the NPC to spawn even if spawning is disallowed</param>
        public static void SpawnOnPlayer(int playerIndex, int Type, bool makespawn = false)
        {
            if (Main.stopSpawns && !makespawn)
                return;

            if (!makespawn && IsNPCSummoned(Type)) //Monitor this, Possible hack
            {
                return;
            }

            bool flag = false;
            int x = 0;
            int y = 0;
            int num3 = (int)(Main.players[playerIndex].Position.X / 16f) - spawnRangeX * 2;
            int num4 = (int)(Main.players[playerIndex].Position.X / 16f) + spawnRangeX * 2;
            int num5 = (int)(Main.players[playerIndex].Position.Y / 16f) - spawnRangeY * 2;
            int num6 = (int)(Main.players[playerIndex].Position.Y / 16f) + spawnRangeY * 2;
            int num7 = (int)(Main.players[playerIndex].Position.X / 16f) - safeRangeX;
            int num8 = (int)(Main.players[playerIndex].Position.X / 16f) + safeRangeX;
            int num9 = (int)(Main.players[playerIndex].Position.Y / 16f) - safeRangeY;
            int num10 = (int)(Main.players[playerIndex].Position.Y / 16f) + safeRangeY;

            if (num3 < 0)
                num3 = 0;
            if (num4 > Main.maxTilesX)
                num4 = Main.maxTilesX;
            if (num5 < 0)
                num5 = 0;
            if (num6 > Main.maxTilesY)
                num6 = Main.maxTilesY;

            for (int i = 0; i < 1000; i++)
            {
                int j = 0;
                while (j < 100)
                {
                    int num11 = Main.rand.Next(num3, num4);
                    int num12 = Main.rand.Next(num5, num6);
                    if (Main.tile.At(num11, num12).Active && Main.tileSolid[(int)Main.tile.At(num11, num12).Type])
                    {
                        if (!flag && !flag)
                        {
                            j++;
                            continue;
                        }
                        break;
                    }
                    if (!Main.wallHouse[(int)Main.tile.At(num11, num12).Wall] || i >= 999)
                    {
                        int k = num12;
                        while (k < Main.maxTilesY)
                        {
                            if (Main.tile.At(num11, k).Active && Main.tileSolid[(int)Main.tile.At(num11, k).Type])
                            {
                                if (num11 < num7 || num11 > num8 || k < num9 || k > num10 || i == 999)
                                {
                                    x = num11;
                                    y = k;
                                    flag = true;
                                }
                                break;
                            }
                            else
                            {
                                k++;
                            }
                        }
                        if (!flag || i >= 999)
                        {
                            if (!flag && !flag)
                            {
                                j++;
                                continue;
                            }
                            break;
                        }
                        int num13 = x - spawnSpaceX / 2;
                        int num14 = x + spawnSpaceX / 2;
                        int num15 = y - spawnSpaceY;
                        int num16 = y;
                        if (num13 < 0)
                        {
                            flag = false;
                        }
                        if (num14 > Main.maxTilesX)
                        {
                            flag = false;
                        }
                        if (num15 < 0)
                        {
                            flag = false;
                        }
                        if (num16 > Main.maxTilesY)
                        {
                            flag = false;
                        }
                        if (flag)
                        {
                            for (int l = num13; l < num14; l++)
                            {
                                for (int m = num15; m < num16; m++)
                                {
                                    if (Main.tile.At(l, m).Active && Main.tileSolid[(int)Main.tile.At(l, m).Type])
                                    {
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        }
                        j++;
                        continue;
                    }
                }
                if (flag && i < 999)
                {
                    Rectangle rectangle = new Rectangle(x * 16, y * 16, 16, 16);
                    for (int n = 0; n < Main.MAX_PLAYERS; n++)
                    {
                        if (Main.players[n].Active)
                        {
                            Rectangle rectangle2 = new Rectangle((int)(Main.players[n].Position.X + (float)(Main.players[n].Width / 2) - (float)(sWidth / 2) -
                                (float)safeRangeX), (int)(Main.players[n].Position.Y + (float)(Main.players[n].Height / 2) - (float)(sHeight / 2) -
                                (float)safeRangeY), sWidth + safeRangeX * 2, sHeight + safeRangeY * 2);
                            if (rectangle.Intersects(rectangle2))
                                flag = true;
                        }
                    }
                }
                if (flag)
                    break;
            }
            if (flag)
            {
                var player = Main.players[playerIndex];
                var ctx = new HookContext
                {
                    Connection = player.Connection,
                    Sender = player,
                    Player = player,
                };

                var args = new HookArgs.PlayerTriggeredEvent
                {
                    X = x,
                    Y = y,
                    Type = WorldEventType.BOSS,
                    Name = ((NPCType)Type).ToString()
                };

                HookPoints.PlayerTriggeredEvent.Invoke(ref ctx, ref args);

                if (ctx.CheckForKick())
                    return;
                else if (ctx.Result != HookResult.IGNORE)
                {
                    if (Type == (int)NPCType.N113_WALL_OF_FLESH && Main.hardMode)
                    {
                        Main.hardMode = false;
                        var msg = NetMessage.PrepareThreadInstance();
                        msg.WorldData(false);
                        msg.Broadcast();
                    }

                    int npcIndex = NewNPC(x * 16 + 8, y * 16, Type, 1, makespawn);
                    if (npcIndex == 200)
                        return;

                    Main.npcs[npcIndex].target = playerIndex;
                    Main.npcs[npcIndex].timeLeft *= 20;

                    string npcName = Main.npcs[npcIndex].Name;
                    if (!String.IsNullOrEmpty(Main.npcs[npcIndex].DisplayName))
                        npcName = Main.npcs[npcIndex].DisplayName;

                    if (npcIndex < 200)
                        NetMessage.SendData(23, -1, -1, String.Empty, npcIndex);

                    /*if (Type == (int)NPCType.N125_RETINAZER)
                    {
                        NetMessage.SendData(25, -1, -1, "The Twins have awoken!", 255, 175f, 75f, 255f);
                        return;
                    }
                    else if (Type != (int)NPCType.N82_WRAITH && Type != (int)NPCType.N126_SPAZMATISM && Type != (int)NPCType.N50_KING_SLIME)
                        NetMessage.SendData(25, -1, -1, npcName + " has awoken!", 255, 175f, 75f, 255f);*/

                    if (Type != (int)NPCType.N82_WRAITH && Type != (int)NPCType.N126_SPAZMATISM && Type != (int)NPCType.N50_KING_SLIME)
                    {
                        if (Type == (int)NPCType.N125_RETINAZER)
                        {
                            npcName = "The Twins";
                            ProgramLog.Users.Log("{0} @ {1}: {3} summoned by {2}.", player.IPAddress, player.whoAmi, player.Name, npcName);

                            var twinsMessage = String.Format("{0} have been awoken by {1}", npcName, player.Name);
                            NetMessage.SendData(Packet.PLAYER_CHAT, -1, -1, twinsMessage, 255, 255, 128, 150);
                        }

                        ProgramLog.Users.Log("{0} @ {1}: {3} summoned by {2}.", player.IPAddress, player.whoAmi, player.Name, npcName);

                        var bossMessage = String.Format("{0} has summoned the {1}!", player.Name, npcName);
                        NetMessage.SendData(Packet.PLAYER_CHAT, -1, -1, bossMessage, 255, 255, 128, 150);
                    }
                }
            }
        }
        /// <summary>
        /// Updates specified item's condition
        /// </summary>
        /// <param name="TileRefs">Reference to the ITile method, For usage between Sandbox and Realtime</param>
        /// <param name="i">Item index</param>
        public void UpdateItem(Func<Int32, Int32, ITile> TileRefs, int i)
        {
            if (TileRefs == null)
                TileRefs = TileCollection.ITileAt;

            if (this.Active)
            {
                float addVelocity = 0.1f;
                float maxVelocity = 7f;
                Vector2 value = this.Velocity * 0.5f;
                if (this.Wet)
                {
                    maxVelocity = 5f;
                    addVelocity = 0.08f;
                }
                if (this.OwnTime > 0)
                    this.OwnTime--;
                else
                    this.OwnIgnore = -1;
                if (this.KeepTime > 0)
                    this.KeepTime--;

                if (!this.BeingGrabbed)
                {
                    this.Velocity.Y = this.Velocity.Y + addVelocity;

                    if (this.Velocity.Y > maxVelocity)
                        this.Velocity.Y = maxVelocity;

                    this.Velocity.X = this.Velocity.X * 0.95f;

                    if ((double)this.Velocity.X < 0.1 && (double)this.Velocity.X > -0.1)
                        this.Velocity.X = 0f;

                    this.LavaWet = Collision.LavaCollision(this.Position, this.Width, this.Height);
                    if (Collision.WetCollision(this.Position, this.Width, this.Height))
                    {
                        if (!this.Wet)
                        {
                            if (this.WetCount == 0)
                            {
                                this.WetCount = 20;
                            }
                            this.Wet = true;
                        }
                    }
                    else if (this.Wet)
                    {
                        this.Wet = false;
                    }
                    if (!this.Wet)
                    {
                        this.LavaWet = false;
                    }
                    if (this.WetCount > 0)
                    {
                        this.WetCount -= 1;
                    }
                    if (this.Wet)
                    {
                        Vector2 vector = this.Velocity;
                        this.Velocity = Collision.TileCollision(this.Position, this.Velocity, this.Width, this.Height, false, false);
                        if (this.Velocity.X != vector.X)
                        {
                            value.X = this.Velocity.X;
                        }
                        if (this.Velocity.Y != vector.Y)
                        {
                            value.Y = this.Velocity.Y;
                        }
                    }
                    else
                    {
                        this.Velocity = Collision.TileCollision(this.Position, this.Velocity, this.Width, this.Height, false, false);
                    }

                    if (this.LavaWet)
                    {
                        if (this.Type == 267)
                        {
                            Active = false;
                            Type = 0;
                            Name = String.Empty;
                            Stack = 0;
                            for (int l = 0; l < NPC.MAX_NPCS; l++)
                            {
                                if (Main.npcs[l].Active && Main.npcs[l].type == NPCType.N22_GUIDE)
                                {
                                    if (NPC.SpawnWallOfFlesh(TileRefs, Position) == SpawnFlags.SUMMONED)
                                        if (Main.npcs[l].StrikeNPC(World.Sender, 9999, 10f, -Main.npcs[l].direction))
                                        {
                                            NetMessage.SendData(28, -1, -1, String.Empty, l, 9999f, 10f, (float)(-(float)Main.npcs[l].direction), 0);
                                            break;
                                        }
                                }
                            }
                            NetMessage.SendData(21);
                        }
                    }
                    else
                    {
                        if (this.Owner == Main.myPlayer && this.LavaWet && this.Type != 312 && this.Type != 318 && this.Type != 173 && this.Type != 174 && this.Type != 175 && this.Rare == 0)
                        {
                            if (this.Type == 267)
                            {
                                var player = Main.players[this.Owner];
                                var ctx = new HookContext
                                {
                                    Sender = player
                                };

                                var args = new HookArgs.PlayerTriggeredEvent
                                {
                                    Type = WorldEventType.BOSS
                                };

                                HookPoints.PlayerTriggeredEvent.Invoke(ref ctx, ref args);

                                if (ctx.CheckForKick())
                                    return;
                                else if (ctx.Result != HookResult.IGNORE)
                                {
                                    ProgramLog.Users.Log("{0} @ {1}: Wall Of Flesh triggered by {2}.", player.IPAddress, this.Owner, player.Name);

                                    for (int l = 0; l < NPC.MAX_NPCS; l++)
                                    {
                                        if (Main.npcs[l].Active && Main.npcs[l].type == NPCType.N22_GUIDE)
                                        {
                                            if (NPC.SpawnWallOfFlesh(TileRefs, Position) == SpawnFlags.SUMMONED)
                                            {
                                                if (Main.npcs[l].StrikeNPC(World.Sender, 9999, 10f, -Main.npcs[l].direction))
                                                {
                                                    NetMessage.SendData(28, -1, -1, String.Empty, l, 9999f, 10f, (float)(-(float)Main.npcs[l].direction), 0);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            this.Active = false;
                            this.Type = 0;
                            this.Name = String.Empty;
                            this.Stack = 0;
                            NetMessage.SendData(21, -1, -1, String.Empty, i);
                        }
                        if (this.Type == 75 && Main.dayTime)
                        {
                            this.Active = false;
                            this.Type = 0;
                            this.Stack = 0;
                            NetMessage.SendData(21, -1, -1, "", i);
                        }
                    }
                }
                else
                {
                    this.BeingGrabbed = false;
                }
                if (this.SpawnTime < 2147483646)
                {
                    this.SpawnTime++;
                }
                if (this.Owner != Main.myPlayer)
                {
                    this.Release++;
                    if (this.Release >= 300)
                    {
                        this.Release = 0;
                        NetMessage.SendData(39, this.Owner, -1, "", i);
                    }
                }
                if (this.Wet)
                {
                    this.Position += value;
                }
                else
                {
                    this.Position += this.Velocity;
                }
                if (this.NoGrabDelay > 0)
                {
                    this.NoGrabDelay--;
                }
            }
        }
        public void ItemCheck(Func<Int32, Int32, ITile> TileRefs, ISandbox sandbox, int i)
        {
            if (TileRefs == null)
                TileRefs = TileCollection.ITileAt;

            int dmg = this.inventory[this.selectedItemIndex].damage;
            if (dmg > 0)
            {
                if (this.inventory[this.selectedItemIndex].Melee)
                {
                    dmg = (int)((float)dmg * this.meleeDamage);
                }
                else if (this.inventory[this.selectedItemIndex].Ranged)
                {
                    dmg = (int)((float)dmg * this.rangedDamage);
                }
                else if (this.inventory[this.selectedItemIndex].Magic)
                {
                    dmg = (int)((float)dmg * this.magicDamage);
                }
            }

            Item selectedItem = inventory[selectedItemIndex];
            if (selectedItem.AutoReuse && !this.noItems)
            {
                releaseUseItem = true;
                if (itemAnimation == 1 && selectedItem.Stack > 0)
                {
                    itemAnimation = 0;
                }
            }
            if (this.itemAnimation == 0 && ReUseDelay > 0)
            {
                this.itemAnimation = ReUseDelay;
                this.itemTime = ReUseDelay;
                ReUseDelay = 0;
            }
            if (controlUseItem && itemAnimation == 0 && releaseUseItem && selectedItem.UseStyle > 0)
            {
                bool flag = true;
                if (this.noItems)
                {
                    flag = false;
                }
                if (selectedItem.Shoot == ProjectileType.N6_ENCHANTED_BOOMERANG ||
                    selectedItem.Shoot == ProjectileType.N19_FLAMARANG ||
                    selectedItem.Shoot == ProjectileType.N33_THORN_CHAKRUM)
                {
                    for (int j = 0; j < Main.maxProjectiles; j++)
                    {
                        if (Main.projectile[j].Active && Main.projectile[j].Owner == Main.myPlayer && Main.projectile[j].type == selectedItem.Shoot)
                        {
                            flag = false;
                        }
                    }
                }
                if (selectedItem.Shoot == ProjectileType.N13_HOOK || selectedItem.Shoot == ProjectileType.N32_IVY_WHIP)
                {
                    for (int k = 0; k < 1000; k++)
                    {
                        if (Main.projectile[k].Active && Main.projectile[k].Owner == Main.myPlayer && Main.projectile[k].type == selectedItem.Shoot && Main.projectile[k].ai[0] != 2f)
                        {
                            flag = false;
                        }
                    }
                }
                if (selectedItem.Potion && flag)
                {
                    if (this.potionDelay <= 0)
                    {
                        potionDelay = Item.POTION_DELAY;
                        this.AddBuff(21, this.potionDelay, true);
                    }
                    else
                    {
                        flag = false;
                    }
                }

                if (selectedItem.Mana > 0 && flag)
                {
                    if (selectedItem.Type != 127 || !spaceGun)
                    {
                        if (statMana >= (int)((float)selectedItem.Mana * manaCost))
                        {
                            statMana -= (int)((float)selectedItem.Mana * manaCost);
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }

                if (selectedItem.Type == 43 && Main.dayTime)
                {
                    flag = false;
                }
                if (selectedItem.Type == 70 && !this.zoneEvil)
                {
                    flag = false;
                }

                if (flag)
                {
                    if (selectedItem.UseAmmo > 0)
                    {
                        flag = false;
                        for (int l = 0; l < 44; l++)
                        {
                            if (l < 4 && this.ammo[l].Ammo == selectedItem.UseAmmo && this.ammo[l].Stack > 0)
                            {
                                flag = true;
                                break;
                            }
                            if (this.inventory[l].Ammo == selectedItem.UseAmmo && this.inventory[l].Stack > 0)
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                }

                if (flag)
                {
                    if (grappling[0] > -1)
                    {
                        if (controlRight)
                        {
                            direction = 1;
                        }
                        else if (controlLeft)
                        {
                            direction = -1;
                        }
                    }

                    channel = selectedItem.Channel;
                    attackCD = 0;

                    if (selectedItem.Melee)
                    {
                        this.itemAnimation = (int)((float)selectedItem.UseAnimation * this.meleeSpeed);
                        this.itemAnimationMax = (int)((float)selectedItem.UseAnimation * this.meleeSpeed);
                    }
                    else
                    {
                        this.itemAnimation = selectedItem.UseAnimation;
                        this.itemAnimationMax = selectedItem.UseAnimation;
                        ReUseDelay = selectedItem.ReUseDelay;
                    }
                }

                if (flag && selectedItem.Shoot == ProjectileType.N18_ORB_OF_LIGHT)
                {
                    for (int j = 0; j < 1000; j++)
                    {
                        if (Main.projectile[j].Active && Main.projectile[j].Owner == i && Main.projectile[j].type == selectedItem.Shoot)
                        {
                            Main.projectile[j].Kill(TileRefs, sandbox);
                        }
                    }
                }
            }

            if (!this.controlUseItem)
            {
                this.channel = false;
            }

            if (this.itemAnimation > 0)
            {
                if (selectedItem.Mana > 0)
                {
                    this.manaRegenDelay = (int)this.maxRegenDelay;
                }

                itemHeight = selectedItem.Height;
                itemWidth = selectedItem.Width;
                itemAnimation--;
            }
            else if (selectedItem.HoldStyle == 1)
            {
                this.itemLocation.X = this.Position.X + (float)this.Width * 0.5f + 20f * (float)this.direction;

                this.itemLocation.Y = this.Position.Y + 24f;
                this.itemRotation = 0f;
                if (this.gravDir == -1f)
                {
                    this.itemRotation = -this.itemRotation;
                    this.itemLocation.Y = this.Position.Y + (float)this.Height + (this.Position.Y - this.itemLocation.Y);
                }
            }
            else if (selectedItem.HoldStyle == 2)
            {
                this.itemLocation.X = this.Position.X + (float)this.Width * 0.5f + (float)(6 * this.direction);
                this.itemLocation.Y = this.Position.Y + 16f;
                this.itemRotation = 0.79f * (float)(-(float)this.direction);
                if (this.gravDir == -1f)
                {
                    this.itemRotation = -this.itemRotation;
                    this.itemLocation.Y = this.Position.Y + (float)this.Height + (this.Position.Y - this.itemLocation.Y);
                }
            }

            releaseUseItem = !controlUseItem;

            if (this.itemTime > 0)
            {
                this.itemTime--;
            }
            if (i == Main.myPlayer)
            {
                if (selectedItem.Shoot > 0 && itemAnimation > 0 && itemTime == 0)
                {
                    ProjectileType shoot = selectedItem.Shoot;
                    float shootSpeed = selectedItem.ShootSpeed;

                    if (selectedItem.Melee && shoot != ProjectileType.N25_BALL_O_HURT && shoot !=
                        ProjectileType.N26_BLUE_MOON && shoot != ProjectileType.N35_SUNFURY)
                    {
                        shootSpeed /= this.meleeSpeed;
                    }

                    bool flag2 = false;
                    int damage = dmg;
                    float knockBack = selectedItem.KnockBack;
                    if (shoot == ProjectileType.N13_HOOK || shoot == ProjectileType.N32_IVY_WHIP)
                    {
                        grappling[0] = -1;
                        grapCount = 0;
                        for (int j = 0; j < Main.maxProjectiles; j++)
                        {
                            if (Main.projectile[j].Active && Main.projectile[j].Owner == i)
                            {
                                if (Main.projectile[j].type == ProjectileType.N13_HOOK)
                                {
                                    Main.projectile[j].Kill(TileRefs, sandbox);
                                }
                            }
                        }
                    }

                    if (selectedItem.UseAmmo > 0)
                    {
                        Item item = null;
                        bool flag3 = false;
                        for (int num12 = 0; num12 < 4; num12++)
                        {
                            if (this.ammo[num12].Ammo == selectedItem.UseAmmo && this.ammo[num12].Stack > 0)
                            {
                                item = this.ammo[num12];
                                flag2 = true;
                                flag3 = true;
                                break;
                            }
                        }
                        if (!flag3)
                        {
                            for (int num13 = 0; num13 < 44; num13++)
                            {
                                if (this.inventory[num13].Ammo == selectedItem.UseAmmo && this.inventory[num13].Stack > 0)
                                {
                                    item = this.inventory[num13];
                                    flag2 = true;
                                    break;
                                }
                            }
                        }
                        if (flag2)
                        {
                            if (item != null && item.Shoot != 0)
                            {
                                shoot = item.Shoot;
                            }
                            shootSpeed += item.ShootSpeed;
                            if (item.Ranged)
                            {
                                damage += (int)((float)item.damage * this.rangedDamage);
                            }
                            else
                            {
                                damage += item.damage;
                            }

                            if (selectedItem.UseAmmo == ProjectileType.N1_WOODEN_ARROW && this.archery)
                            {
                                if (shootSpeed < 20f)
                                {
                                    shootSpeed *= 1.2f;
                                    if (shootSpeed > 20f)
                                    {
                                        shootSpeed = 20f;
                                    }
                                }
                                damage = (int)((double)((float)damage) * 1.2);
                            }
                            if (item != null) knockBack += item.KnockBack;
                            bool flag4 = false;
                            if (selectedItem.Type == 98 && Main.rand.Next(3) == 0)
                            {
                                flag4 = true;
                            }
                            if (this.ammoCost80 && Main.rand.Next(5) == 0)
                            {
                                flag4 = true;
                            }
                            if (!flag4 && item != null)
                            {
                                item.Stack--;
                                if (item.Stack <= 0)
                                {
                                    item.Active = false;
                                    item.Name = "";
                                    item.Type = 0;
                                }
                            }
                        }
                    }
                    else
                    {
                        flag2 = true;
                    }

                    if (flag2)
                    {
                        if (damage == 1 && selectedItem.Type == 120)
                        {
                            damage = 2;
                        }
                    }
                    else if (selectedItem.UseStyle == 5)
                    {
                        itemRotation = 0f;
                        //NetMessage.SendData(41, -1, -1, "", this.whoAmi);
                    }
                }

                if (selectedItem.Type == 29 && this.itemAnimation > 0 && this.statLifeMax < 400 && this.itemTime == 0)
                {
                    if (this.itemTime == 0)
                    {
                        this.itemTime = selectedItem.UseTime;
                        this.statLifeMax += 20;
                        this.statLife += 20;
                        if (Main.myPlayer == this.whoAmi)
                        {
                            this.HealEffect(20);
                        }
                    }
                }
                if (selectedItem.Type == 109 && this.itemAnimation > 0 && this.statManaMax < 200 && this.itemTime == 0)
                {
                    if (this.itemTime == 0)
                    {
                        this.itemTime = selectedItem.UseTime;
                        this.statManaMax += 20;
                        this.statMana += 20;
                        if (Main.myPlayer == this.whoAmi)
                        {
                            this.ManaEffect(20);
                        }
                    }
                }
            }
            if (selectedItem.damage >= 0 && selectedItem.Type > 0 && !selectedItem.NoMelee)
            {
                if (this.itemAnimation > 0)
                {
                    //bool flag5 = false;
                    Rectangle rectangle = new Rectangle((int)this.itemLocation.X, (int)this.itemLocation.Y, 32, 32);
                    rectangle.Width = (int)((float)rectangle.Width * selectedItem.scale);
                    rectangle.Height = (int)((float)rectangle.Height * selectedItem.scale);
                    if (this.direction == -1)
                    {
                        rectangle.X -= rectangle.Width;
                    }
                    if (this.gravDir == 1f)
                    {
                        rectangle.Y -= rectangle.Height;
                    }
                    rectangle.Y -= rectangle.Height;
                    if (selectedItem.UseStyle == 1)
                    {
                        if ((double)this.itemAnimation < (double)this.itemAnimationMax * 0.333)
                        {
                            if (this.direction == -1)
                            {
                                rectangle.X -= (int)((double)rectangle.Width * 1.4 - (double)rectangle.Width);
                            }
                            rectangle.Width = (int)((double)rectangle.Width * 1.4);
                            rectangle.Y += (int)((double)rectangle.Height * 0.5 * (double)this.gravDir);
                            rectangle.Height = (int)((double)rectangle.Height * 1.1);
                        }
                        else
                        {
                            if ((double)this.itemAnimation >= (double)this.itemAnimationMax * 0.666)
                            {
                                if (this.direction == 1)
                                {
                                    rectangle.X -= (int)((double)rectangle.Width * 1.2);
                                }
                                rectangle.Width *= 2;
                                rectangle.Y -= (int)(((double)rectangle.Height * 1.4 - (double)rectangle.Height) * (double)this.gravDir);
                                rectangle.Height = (int)((double)rectangle.Height * 1.4);
                            }
                        }
                    }
                    else if (selectedItem.UseStyle == 3)
                    {
                        if (!((double)this.itemAnimation > (double)this.itemAnimationMax * 0.666))
                        {
                            if (this.direction == -1)
                            {
                                rectangle.X -= (int)((double)rectangle.Width * 1.4 - (double)rectangle.Width);
                            }
                            rectangle.Width = (int)((double)rectangle.Width * 1.4);
                            rectangle.Y += (int)((double)rectangle.Height * 0.6);
                            rectangle.Height = (int)((double)rectangle.Height * 0.6);
                        }
                    }
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0)
            {
                if (selectedItem.HealLife > 0)
                {
                    this.statLife += selectedItem.HealLife;
                    this.itemTime = selectedItem.UseTime;
                    if (Main.myPlayer == this.whoAmi)
                    {
                        this.HealEffect(selectedItem.HealLife);
                    }
                }
                if (selectedItem.HealMana > 0)
                {
                    this.statMana += selectedItem.HealMana;
                    this.itemTime = selectedItem.UseTime;
                    if (Main.myPlayer == this.whoAmi)
                    {
                        this.ManaEffect(selectedItem.HealMana);
                    }
                }
                if (selectedItem.BuffType > 0)
                {
                    if (this.whoAmi == Main.myPlayer)
                    {
                        this.AddBuff(selectedItem.BuffType, selectedItem.BuffTime, true);
                    }
                    this.itemTime = selectedItem.UseTime;
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0 && selectedItem.Type == 361)
            {
                this.itemTime = selectedItem.UseTime;
                if (Main.invasionType == 0)
                {
                    var ctx = new HookContext
                    {
                        Connection = Connection,
                        Sender = this,
                        Player = this,
                    };

                    var args = new HookArgs.PlayerTriggeredEvent
                    {
                        Type = WorldEventType.INVASION,
                    };

                    HookPoints.PlayerTriggeredEvent.Invoke(ref ctx, ref args);

                    if (ctx.CheckForKick())
                        return;
                    else if (ctx.Result != HookResult.IGNORE)
                    {
                        ProgramLog.Users.Log("{0} @ {1}: Invasion triggered by {2}.", IPAddress, whoAmi, Name);
                        NetMessage.SendData(Packet.PLAYER_CHAT, -1, -1, string.Concat(Name, " has summoned an invasion!"), 255, 255, 128, 150);
                        Main.invasionDelay = 0;
                        Main.StartInvasion();
                    }
                }
            }
            if (this.itemTime == 0 && this.itemAnimation > 0 && (selectedItem.Type == 43 || selectedItem.Type == 70))
            {
                this.itemTime = selectedItem.UseTime;

                if ((selectedItem.Type == 43 && !Main.dayTime) || (selectedItem.Type == 70 && zoneEvil))
                {
                    /*var ctx = new HookContext
                    {
                        Connection = Connection,
                        Sender = this,
                        Player = this,
                    };

                    var args = new HookArgs.PlayerTriggeredEvent
                    {
                        X = (int)(Position.X / 16),
                        Y = (int)(Position.Y / 16),
                        Type = WorldEventType.BOSS,
                        Name = selectedItem.Type == 43 ? "Eye of Cthulhu" : "Eater of Worlds",
                    };

                    HookPoints.PlayerTriggeredEvent.Invoke(ref ctx, ref args);

                    if (ctx.CheckForKick())
                        return;
                    else if (ctx.Result != HookResult.IGNORE)
                    {
                        if (selectedItem.Type == 43)
                        {
                            ProgramLog.Users.Log("{0} @ {1}: Eye of Cthulhu summoned by {2}.", IPAddress, whoAmi, Name);
                            NetMessage.SendData(Packet.PLAYER_CHAT, -1, -1, string.Concat(Name, " has summoned the Eye of Cthulhu!"), 255, 255, 128, 150);
                            NPC.SpawnOnPlayer(i, 4);
                        }
                        else if (selectedItem.Type == 70)
                        {
                            ProgramLog.Users.Log("{0} @ {1}: Eater of Worlds summoned by {2}.", IPAddress, whoAmi, Name);
                            NetMessage.SendData(Packet.PLAYER_CHAT, -1, -1, string.Concat(Name, " has summoned the Eater of Worlds!"), 255, 255, 128, 150);
                            NPC.SpawnOnPlayer(i, 13);
                        }
                    }*/
                }
            }

            if (selectedItem.Type == 50 && this.itemAnimation > 0)
            {
                if (this.itemTime == 0)
                {
                    this.itemTime = selectedItem.UseTime;
                }
                else if (this.itemTime == selectedItem.UseTime / 2)
                {
                    this.grappling[0] = -1;
                    this.grapCount = 0;
                    for (int j = 0; j < 1000; j++)
                    {
                        if (Main.projectile[j].Active && Main.projectile[j].Owner == i)
                        {
                            if (Main.projectile[j].aiStyle == 7)
                            {
                                Main.projectile[j].Kill(TileRefs, sandbox);
                            }
                        }
                    }
                    this.Spawn(TileRefs, sandbox);
                }
            }
        }