public static void Initialize()
        {
            if (Program.properties != null)
            {
                stopSpawns     = Program.properties.StopNPCSpawning;
                SpawnsOverride = Program.properties.NPCSpawnsOverride;
            }

            NPC.ClearNames();
            NPC.SetNames();

            //foreach (var buff in new int[] { 20, 24, 31, 39 })
            //    pvpBuff[buff] = true;

            foreach (var buff in new int[] { 20, 21, 22, 23, 24, 25, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 })
            {
                debuff[buff] = true;
            }

            foreach (var i in new int[] { 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 24, 26, 27, 28, 29,
                                          31, 33, 34, 35, 36, 42, 50, 55, 61, 71, 72, 73, 74, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89,
                                          90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 110, 113, 114, 125, 126,
                                          128, 129, 132, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 149 })
            {
                tileFrameImportant[i] = true;
            }

            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 130, 131 })
            {
                tileStone[i] = true;
            }

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 22, 23, 25, 30, 32, 37, 38, 39, 40, 41, 43, 44, 45,
                                          46, 47, 48, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112,
                                          115, 116, 117, 118, 119, 120, 121, 122, 123, 130, 131, 137, 138, 140, 145, 146, 147, 148 })
            {
                tileBlockLight[i] = true;
            }

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 19, 22, 23, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46,
                                          47, 48, 53, 54, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112, 116, 117,
                                          118, 119, 120, 121, 122, 123, 127, 130, 137, 138, 140, 145, 146, 147, 148 })
            {
                tileSolid[i] = true;
            }

            foreach (var i in new int[] { 1, 6, 7, 8, 9, 22, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 53, 56, 107,
                                          108, 111, 112, 116, 117, 118, 119, 120, 121, 122, 123, 140, 145, 146, 147, 148 })
            {
                tileMergeDirt[i] = true;
            }

            foreach (var i in new int[] { 3, 24, 28, 32, 51, 52, 61, 62, 69, 71, 73, 74, 82, 83, 84, 110, 113, 115 })
            {
                tileCut[i] = true;
            }

            foreach (var i in new int[] { 3, 4, 24, 32, 50, 61, 69, 73, 74, 82, 83, 84, 110, 113 })
            {
                tileNoFail[i] = true;
            }

            foreach (var i in new int[] { 1, 4, 5, 6, 10, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31 })
            {
                wallHouse[i] = true;
            }

            foreach (var i in new int[] { 82, 83, 84 })
            {
                tileAlch[i] = true;
            }

            foreach (var i in new int[] { 3, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 27, 28, 29, 32, 33, 34, 35, 36,
                                          42, 49, 50, 52, 55, 61, 62, 69, 71, 72, 73, 74, 79, 80, 81, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98,
                                          99, 100, 101, 102, 103, 104, 106, 110, 113, 115, 125, 126 })
            {
                tileLavaDeath[i] = true;
            }

            foreach (var i in new int[] { 3, 4, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 27, 50, 86, 87, 88, 89, 90, 91, 92,
                                          93, 94, 95, 96, 97, 98, 99, 101, 102, 110, 114 })
            {
                tileNoAttach[i] = true;
            }

            foreach (var i in new int[] { 41, 43, 44 })
            {
                tileDungeon[i] = true;
            }

            foreach (var i in new int[] { 14, 18, 19, 87, 88, 101, 114 })
            {
                tileTable[i] = true;
            }

            foreach (var i in new int[] { 4, 51, 93, 98 })
            {
                tileWaterDeath[i] = true;
            }


            foreach (var i in new int[] { 8, 12 })
            {
                tileShine[i] = 1000;
            }
            foreach (var i in new int[] { 9 })
            {
                tileShine[i] = 1050;
            }
            foreach (var i in new int[] { 7 })
            {
                tileShine[i] = 1100;
            }
            foreach (var i in new int[] { 6, 22 })
            {
                tileShine[i] = 1150;
            }
            foreach (var i in new int[] { 21 })
            {
                tileShine[i] = 1200;
            }
            foreach (var i in new int[] { 122 })
            {
                tileShine[i] = 1800;
            }
            foreach (var i in new int[] { 121 })
            {
                tileShine[i] = 1850;
            }
            foreach (var i in new int[] { 45 })
            {
                tileShine[i] = 1900;
            }
            foreach (var i in new int[] { 46 })
            {
                tileShine[i] = 2000;
            }
            foreach (var i in new int[] { 47 })
            {
                tileShine[i] = 2100;
            }
            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 108 })
            {
                tileShine[i] = 900;
            }
            foreach (var i in new int[] { 111 })
            {
                tileShine[i] = 850;
            }
            foreach (var i in new int[] { 107 })
            {
                tileShine[i] = 950;
            }
            foreach (var i in new int[] { 129 })
            {
                tileShine[i] = 300;
            }
            foreach (var i in new int[] { 109, 110, 117, 116 })
            {
                tileShine[i] = 9000;
            }
            foreach (var i in new int[] { 118 })
            {
                tileShine[i] = 8000;
            }
            foreach (var i in new int[] { 125 })
            {
                tileShine[i] = 600;
            }


            foreach (var i in new int[] { 14, 16, 18, 19, 87, 88, 101, 114 })
            {
                tileSolidTop[i] = true;
            }


            for (int l = 0; l < Item.MAX_ITEMS + 1; l++)
            {
                item[l] = new Item();
            }
            for (int m = 0; m < NPC.MAX_NPCS + 1; m++)
            {
                npcs[m]        = new NPC();
                npcs[m].whoAmI = m;
            }
            for (int i = 0; i < MAX_PLAYERS + 1; i++)
            {
                players[i] = new Player();
            }
            for (int num2 = 0; num2 < 1001; num2++)
            {
                projectile[num2] = new Projectile();
            }
            for (int num10 = 0; num10 < Liquid.resLiquid; num10++)
            {
                liquid[num10] = new Liquid();
            }
            for (int num11 = 0; num11 < 10000; num11++)
            {
                liquidBuffer[num11] = new LiquidBuffer();
            }

            teamColor[0] = new Color(255, 255, 255);
            teamColor[1] = new Color(230, 40, 20);
            teamColor[2] = new Color(20, 200, 30);
            teamColor[3] = new Color(75, 90, 255);
            teamColor[4] = new Color(200, 180, 0);

            NetPlay.Init();
        }
        public static void DelWater(int liquidIndex)
        {
            int x = Main.liquid[liquidIndex].x;
            int y = Main.liquid[liquidIndex].y;

            if (Main.tile.At(x, y).Liquid < 2)
            {
                Main.tile.At(x, y).SetLiquid(0);
            }
            else if (Main.tile.At(x, y).Liquid < 20)
            {
                if ((Main.tile.At(x - 1, y).Liquid < Main.tile.At(x, y).Liquid&& (!Main.tile.At(x - 1, y).Active || !Main.tileSolid[(int)Main.tile.At(x - 1, y).Type] || Main.tileSolidTop[(int)Main.tile.At(x - 1, y).Type])) || (Main.tile.At(x + 1, y).Liquid < Main.tile.At(x, y).Liquid&& (!Main.tile.At(x + 1, y).Active || !Main.tileSolid[(int)Main.tile.At(x + 1, y).Type] || Main.tileSolidTop[(int)Main.tile.At(x + 1, y).Type])) || (Main.tile.At(x, y + 1).Liquid < 255 && (!Main.tile.At(x, y + 1).Active || !Main.tileSolid[(int)Main.tile.At(x, y + 1).Type] || Main.tileSolidTop[(int)Main.tile.At(x, y + 1).Type])))
                {
                    Main.tile.At(x, y).SetLiquid(0);
                }
            }
            else if (Main.tile.At(x, y + 1).Liquid < 255 && (!Main.tile.At(x, y + 1).Active || !Main.tileSolid[(int)Main.tile.At(x, y + 1).Type] || Main.tileSolidTop[(int)Main.tile.At(x, y + 1).Type]) && !Liquid.stuck)
            {
                Main.liquid[liquidIndex].kill = 0;
                return;
            }
            if (Main.tile.At(x, y).Liquid < 250 && Main.tile.At(x, y - 1).Liquid > 0)
            {
                Liquid.AddWater(x, y - 1);
            }
            if (Main.tile.At(x, y).Liquid == 0)
            {
                Main.tile.At(x, y).SetLava(false);
            }
            else
            {
                if (Main.tile.At(x + 1, y).Liquid > 0 && Main.tile.At(x + 1, y + 1).Liquid < 250 && !Main.tile.At(x + 1, y + 1).Active)
                {
                    Liquid.AddWater(x + 1, y);
                }
                if (Main.tile.At(x - 1, y).Liquid > 0 && Main.tile.At(x - 1, y + 1).Liquid < 250 && !Main.tile.At(x - 1, y + 1).Active)
                {
                    Liquid.AddWater(x - 1, y);
                }
                if (Main.tile.At(x, y).Lava)
                {
                    Liquid.LavaCheck(x, y);
                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= y + 1; j++)
                        {
                            if (Main.tile.At(i, j).Active)
                            {
                                if (Main.tile.At(i, j).Type == 2 || Main.tile.At(i, j).Type == 23)
                                {
                                    Main.tile.At(i, j).SetType(0);
                                    WorldModify.SquareTileFrame(i, j, true);

                                    NetMessage.SendTileSquare(-1, x, y, 3);
                                }
                                else if (Main.tile.At(i, j).Type == 60 || Main.tile.At(i, j).Type == 70)
                                {
                                    Main.tile.At(i, j).SetType(59);
                                    WorldModify.SquareTileFrame(i, j, true);

                                    NetMessage.SendTileSquare(-1, x, y, 3);
                                }
                            }
                        }
                    }
                }
            }


            NetMessage.SendWater(x, y);

            Liquid.numLiquid--;
            Main.tile.At(Main.liquid[liquidIndex].x, Main.liquid[liquidIndex].y).SetCheckingLiquid(false);
            Main.liquid[liquidIndex].x    = Main.liquid[Liquid.numLiquid].x;
            Main.liquid[liquidIndex].y    = Main.liquid[Liquid.numLiquid].y;
            Main.liquid[liquidIndex].kill = Main.liquid[Liquid.numLiquid].kill;
            if (Main.tileAlch[(int)Main.tile.At(x, y).Type])
            {
                WorldModify.CheckAlch(x, y);
            }
        }
        public static void Initialize()
        {
            if (Program.properties != null)
            {
                stopSpawns = Program.properties.StopNPCSpawning;
                SpawnsOverride = Program.properties.NPCSpawnsOverride;
            }

            NPC.ClearNames();
            NPC.SetNames();

            //foreach (var buff in new int[] { 20, 24, 31, 39 })
            //    pvpBuff[buff] = true;

            foreach (var buff in new int[] { 20, 21, 22, 23, 24, 25, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 })
                debuff[buff] = true;

            foreach (var i in new int[] { 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 24, 26, 27, 28, 29,
                31, 33, 34, 35, 36, 42, 50, 55, 61, 71, 72, 73, 74, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89,
                90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 110, 113, 114, 125, 126,
                128, 129, 132, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 149 })
                tileFrameImportant[i] = true;

            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 130, 131 })
                tileStone[i] = true;

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 22, 23, 25, 30, 32, 37, 38, 39, 40, 41, 43, 44, 45,
                46, 47, 48, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112,
                115, 116, 117, 118, 119, 120, 121, 122, 123, 130, 131, 137, 138, 140, 145, 146, 147, 148 })
                tileBlockLight[i] = true;

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 19, 22, 23, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46,
                47, 48, 53, 54, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112, 116, 117,
                118, 119, 120, 121, 122, 123, 127, 130, 137, 138, 140, 145, 146, 147, 148 })
                tileSolid[i] = true;

            foreach (var i in new int[] { 1, 6, 7, 8, 9, 22, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 53, 56, 107,
                108, 111, 112, 116, 117, 118, 119, 120, 121, 122, 123, 140, 145, 146, 147, 148 })
                tileMergeDirt[i] = true;

            foreach (var i in new int[] { 3, 24, 28, 32, 51, 52, 61, 62, 69, 71, 73, 74, 82, 83, 84, 110, 113, 115 })
                tileCut[i] = true;

            foreach (var i in new int[] { 3, 4, 24, 32, 50, 61, 69, 73, 74, 82, 83, 84, 110, 113 })
                tileNoFail[i] = true;

            foreach (var i in new int[] { 1, 4, 5, 6, 10, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31 })
                wallHouse[i] = true;

            foreach (var i in new int[] { 82, 83, 84 })
                tileAlch[i] = true;

            foreach (var i in new int[] { 3, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 27, 28, 29, 32, 33, 34, 35, 36,
                42, 49, 50, 52, 55, 61, 62, 69, 71, 72, 73, 74, 79, 80, 81, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98,
                99, 100, 101, 102, 103, 104, 106, 110, 113, 115, 125, 126 })
                tileLavaDeath[i] = true;

            foreach (var i in new int[] { 3, 4, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 27, 50, 86, 87, 88, 89, 90, 91, 92,
                93, 94, 95, 96, 97, 98, 99, 101, 102, 110, 114 })
                tileNoAttach[i] = true;

            foreach (var i in new int[] { 41, 43, 44 })
                tileDungeon[i] = true;

            foreach (var i in new int[] { 14, 18, 19, 87, 88, 101, 114 })
                tileTable[i] = true;

            foreach (var i in new int[] { 4, 51, 93, 98 })
                tileWaterDeath[i] = true;

            foreach (var i in new int[] { 8, 12 })
                tileShine[i] = 1000;
            foreach (var i in new int[] { 9 })
                tileShine[i] = 1050;
            foreach (var i in new int[] { 7 })
                tileShine[i] = 1100;
            foreach (var i in new int[] { 6, 22 })
                tileShine[i] = 1150;
            foreach (var i in new int[] { 21 })
                tileShine[i] = 1200;
            foreach (var i in new int[] { 122 })
                tileShine[i] = 1800;
            foreach (var i in new int[] { 121 })
                tileShine[i] = 1850;
            foreach (var i in new int[] { 45 })
                tileShine[i] = 1900;
            foreach (var i in new int[] { 46 })
                tileShine[i] = 2000;
            foreach (var i in new int[] { 47 })
                tileShine[i] = 2100;
            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 108 })
                tileShine[i] = 900;
            foreach (var i in new int[] { 111 })
                tileShine[i] = 850;
            foreach (var i in new int[] { 107 })
                tileShine[i] = 950;
            foreach (var i in new int[] { 129 })
                tileShine[i] = 300;
            foreach (var i in new int[] { 109, 110, 117, 116 })
                tileShine[i] = 9000;
            foreach (var i in new int[] { 118 })
                tileShine[i] = 8000;
            foreach (var i in new int[] { 125 })
                tileShine[i] = 600;

            foreach (var i in new int[] { 14, 16, 18, 19, 87, 88, 101, 114 })
                tileSolidTop[i] = true;

            for (int l = 0; l < Item.MAX_ITEMS + 1; l++)
                item[l] = new Item();
            for (int m = 0; m < NPC.MAX_NPCS + 1; m++)
            {
                npcs[m] = new NPC();
                npcs[m].whoAmI = m;
            }
            for (int i = 0; i < MAX_PLAYERS + 1; i++)
                players[i] = new Player();
            for (int num2 = 0; num2 < 1001; num2++)
                projectile[num2] = new Projectile();
            for (int num10 = 0; num10 < Liquid.resLiquid; num10++)
                liquid[num10] = new Liquid();
            for (int num11 = 0; num11 < 10000; num11++)
                liquidBuffer[num11] = new LiquidBuffer();

            teamColor[0] = new Color(255, 255, 255);
            teamColor[1] = new Color(230, 40, 20);
            teamColor[2] = new Color(20, 200, 30);
            teamColor[3] = new Color(75, 90, 255);
            teamColor[4] = new Color(200, 180, 0);

            NetPlay.Init();
        }
        public static double QuickWater(int verbose = 0, int minY = -1, int maxY = -1, ProgressLogger prog = null)
        {
            int num = 0;

            if (minY == -1)
            {
                minY = 3;
            }

            if (maxY == -1)
            {
                maxY = Main.maxTilesY - 3;
            }

            for (int i = maxY; i >= minY; i--)
            {
                if (prog != null)
                {
                    if (verbose > 0)
                    {
                        float num2 = (float)(maxY - i) / (float)(maxY - minY + 1);
                        num2 /= (float)verbose;
                        //Main.statusText = "Settling liquids: " + (int)(num2 * 100f + 1f) + "%";
                        prog.Value += (int)(num2 * 100f + 1f);
                    }
                    else if (verbose < 0)
                    {
                        float num3 = (float)(maxY - i) / (float)(maxY - minY + 1);
                        num3 /= (float)(-(float)verbose);
                        //Main.statusText = "Creating underworld: " + (int)(num3 * 100f + 1f) + "%";
                        prog.Value += (int)(num3 * 100f + 1f);
                    }
                }

                for (int j = 0; j < 2; j++)
                {
                    int num4 = 2;
                    int num5 = Main.maxTilesX - 2;
                    int num6 = 1;
                    if (j == 1)
                    {
                        num4 = Main.maxTilesX - 2;
                        num5 = 2;
                        num6 = -1;
                    }
                    for (int num7 = num4; num7 != num5; num7 += num6)
                    {
                        if (Main.tile.At(num7, i).Liquid > 0)
                        {
                            int  num8  = -num6;
                            bool flag  = false;
                            int  num9  = num7;
                            int  num10 = i;
                            bool flag2 = Main.tile.At(num7, i).Lava;
                            byte b     = Main.tile.At(num7, i).Liquid;
                            Main.tile.At(num7, i).SetLiquid(0);
                            bool flag3 = true;
                            int  num11 = 0;
                            while (flag3 && num9 > 3 && num9 < Main.maxTilesX - 3 && num10 < Main.maxTilesY - 3)
                            {
                                flag3 = false;
                                while (Main.tile.At(num9, num10 + 1).Liquid == 0 && num10 < Main.maxTilesY - 5 && (!Main.tile.At(num9, num10 + 1).Active || !Main.tileSolid[(int)Main.tile.At(num9, num10 + 1).Type] || Main.tileSolidTop[(int)Main.tile.At(num9, num10 + 1).Type]))
                                {
                                    flag  = true;
                                    num8  = num6;
                                    num11 = 0;
                                    flag3 = true;
                                    num10++;
                                    if (num10 > WorldModify.waterLine)
                                    {
                                        flag2 = true;
                                    }
                                }
                                if (Main.tile.At(num9, num10 + 1).Liquid > 0 && Main.tile.At(num9, num10 + 1).Liquid < 255 && Main.tile.At(num9, num10 + 1).Lava == flag2)
                                {
                                    int num12 = (int)(255 - Main.tile.At(num9, num10 + 1).Liquid);
                                    if (num12 > (int)b)
                                    {
                                        num12 = (int)b;
                                    }
                                    TileRef expr_25A = Main.tile.At(num9, num10 + 1);
                                    expr_25A.AddLiquid((byte)num12);
                                    b -= (byte)num12;
                                    if (b <= 0)
                                    {
                                        num++;
                                        break;
                                    }
                                }
                                if (num11 == 0)
                                {
                                    if (Main.tile.At(num9 + num8, num10).Liquid == 0 && (!Main.tile.At(num9 + num8, num10).Active || !Main.tileSolid[(int)Main.tile.At(num9 + num8, num10).Type] || Main.tileSolidTop[(int)Main.tile.At(num9 + num8, num10).Type]))
                                    {
                                        num11 = num8;
                                    }
                                    else
                                    {
                                        if (Main.tile.At(num9 - num8, num10).Liquid == 0 && (!Main.tile.At(num9 - num8, num10).Active || !Main.tileSolid[(int)Main.tile.At(num9 - num8, num10).Type] || Main.tileSolidTop[(int)Main.tile.At(num9 - num8, num10).Type]))
                                        {
                                            num11 = -num8;
                                        }
                                    }
                                }
                                if (num11 != 0 && Main.tile.At(num9 + num11, num10).Liquid == 0 && (!Main.tile.At(num9 + num11, num10).Active || !Main.tileSolid[(int)Main.tile.At(num9 + num11, num10).Type] || Main.tileSolidTop[(int)Main.tile.At(num9 + num11, num10).Type]))
                                {
                                    flag3 = true;
                                    num9 += num11;
                                }
                                if (flag && !flag3)
                                {
                                    flag  = false;
                                    flag3 = true;
                                    num8  = -num6;
                                    num11 = 0;
                                }
                            }
                            if (num7 != num9 && i != num10)
                            {
                                num++;
                            }
                            Main.tile.At(num9, num10).SetLiquid(b);
                            Main.tile.At(num9, num10).SetLava(flag2);
                            if (Main.tile.At(num9 - 1, num10).Liquid > 0 && Main.tile.At(num9 - 1, num10).Lava != flag2)
                            {
                                if (flag2)
                                {
                                    Liquid.LavaCheck(num9, num10);
                                }
                                else
                                {
                                    Liquid.LavaCheck(num9 - 1, num10);
                                }
                            }
                            else
                            {
                                if (Main.tile.At(num9 + 1, num10).Liquid > 0 && Main.tile.At(num9 + 1, num10).Lava != flag2)
                                {
                                    if (flag2)
                                    {
                                        Liquid.LavaCheck(num9, num10);
                                    }
                                    else
                                    {
                                        Liquid.LavaCheck(num9 + 1, num10);
                                    }
                                }
                                else
                                {
                                    if (Main.tile.At(num9, num10 - 1).Liquid > 0 && Main.tile.At(num9, num10 - 1).Lava != flag2)
                                    {
                                        if (flag2)
                                        {
                                            Liquid.LavaCheck(num9, num10);
                                        }
                                        else
                                        {
                                            Liquid.LavaCheck(num9, num10 - 1);
                                        }
                                    }
                                    else
                                    {
                                        if (Main.tile.At(num9, num10 + 1).Liquid > 0 && Main.tile.At(num9, num10 + 1).Lava != flag2)
                                        {
                                            if (flag2)
                                            {
                                                Liquid.LavaCheck(num9, num10);
                                            }
                                            else
                                            {
                                                Liquid.LavaCheck(num9, num10 + 1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return((double)num);
        }
        public static void UpdateLiquid()
        {
            Liquid.cycles    = 25;
            Liquid.maxLiquid = 5000;

            if (!WorldModify.gen)
            {
                if (!Liquid.panicMode)
                {
                    if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > 4000)
                    {
                        Liquid.panicCounter++;
                        if (Liquid.panicCounter > 1800 || Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > 13500)
                        {
                            Liquid.StartPanic();
                        }
                    }
                    else
                    {
                        Liquid.panicCounter = 0;
                    }
                }

                if (Liquid.panicMode)
                {
                    try
                    {
                        Terraria_Server.Networking.LiquidUpdateBuffer.ClearQueue();
                        NetMessage.DisableLiquidUpdates = true;

                        int num = 0;
                        while (Liquid.panicY >= 3 && num < 5)
                        {
                            num++;
                            Liquid.QuickWater(0, Liquid.panicY, Liquid.panicY);
                            Liquid.panicY--;
                            if (Liquid.panicY < 3)
                            {
                                ProgramLog.Log("Water has been settled.");
                                Liquid.panicCounter = 0;
                                Liquid.panicMode    = false;
                                using (var prog = new ProgressLogger(Main.maxTilesX - 2, "Performing water check"))
                                    WorldModify.WaterCheck(prog);
                                for (int i = 0; i < 255; i++)
                                {
                                    for (int j = 0; j < Main.maxSectionsX; j++)
                                    {
                                        for (int k = 0; k < Main.maxSectionsY; k++)
                                        {
                                            NetPlay.slots[i].tileSection[j, k] = false;
                                        }
                                    }
                                }
                            }
                        }
                        return;
                    }
                    finally
                    {
                        NetMessage.DisableLiquidUpdates = false;
                    }
                }
            }

            if (Liquid.quickSettle || Liquid.numLiquid > 2000)
            {
                Liquid.quickFall = true;
            }
            else
            {
                Liquid.quickFall = false;
            }

            Liquid.wetCounter++;

            int num2 = Liquid.maxLiquid / Liquid.cycles;

            int num3 = num2 * (Liquid.wetCounter - 1);

            int num4 = num2 * Liquid.wetCounter;

            if (Liquid.wetCounter == Liquid.cycles)
            {
                num4 = Liquid.numLiquid;
            }

            if (num4 > Liquid.numLiquid)
            {
                num4 = Liquid.numLiquid;
                Liquid.wetCounter = Liquid.cycles;
            }

            if (Liquid.quickFall)
            {
                for (int l = num3; l < num4; l++)
                {
                    Main.liquid[l].delay = 10;
                    Main.liquid[l].Update();
                    Main.tile.At(Main.liquid[l].x, Main.liquid[l].y).SetSkipLiquid(false);
                }
            }
            else
            {
                for (int m = num3; m < num4; m++)
                {
                    if (!Main.tile.At(Main.liquid[m].x, Main.liquid[m].y).SkipLiquid)
                    {
                        Main.liquid[m].Update();
                    }
                    else
                    {
                        Main.tile.At(Main.liquid[m].x, Main.liquid[m].y).SetSkipLiquid(false);
                    }
                }
            }

            if (Liquid.wetCounter >= Liquid.cycles)
            {
                Liquid.wetCounter = 0;
                for (int n = Liquid.numLiquid - 1; n >= 0; n--)
                {
                    if (Main.liquid[n].kill > 3)
                    {
                        Liquid.DelWater(n);
                    }
                }

                int num5 = Liquid.maxLiquid - (Liquid.maxLiquid - Liquid.numLiquid);

                if (num5 > LiquidBuffer.numLiquidBuffer)
                {
                    num5 = LiquidBuffer.numLiquidBuffer;
                }

                for (int num6 = 0; num6 < num5; num6++)
                {
                    Main.tile.At(Main.liquidBuffer[0].x, Main.liquidBuffer[0].y).SetCheckingLiquid(false);
                    Liquid.AddWater(Main.liquidBuffer[0].x, Main.liquidBuffer[0].y);
                    LiquidBuffer.DelBuffer(0);
                }

                if (Liquid.numLiquid > 0 && Liquid.numLiquid > Liquid.stuckAmount - 50 && Liquid.numLiquid < Liquid.stuckAmount + 50)
                {
                    Liquid.stuckCount++;
                    if (Liquid.stuckCount >= 10000)
                    {
                        Liquid.stuck = true;
                        for (int num7 = Liquid.numLiquid - 1; num7 >= 0; num7--)
                        {
                            Liquid.DelWater(num7);
                        }
                        Liquid.stuck      = false;
                        Liquid.stuckCount = 0;
                        return;
                    }
                }
                else
                {
                    Liquid.stuckCount  = 0;
                    Liquid.stuckAmount = Liquid.numLiquid;
                }
            }
        }
        public void Update()
        {
            if (Main.tile.At(this.x, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x, this.y).Type])
            {
                if (Main.tile.At(this.x, this.y).Type != 10)
                {
                    Main.tile.At(this.x, this.y).SetLiquid(0);
                }
                this.kill = 9;
                return;
            }
            byte  liquid = Main.tile.At(this.x, this.y).Liquid;
            float num    = 0f;

            if (this.y > Main.maxTilesY - 200 && !Main.tile.At(this.x, this.y).Lava&& Main.tile.At(this.x, this.y).Liquid > 0)
            {
                byte b = 2;
                if (Main.tile.At(this.x, this.y).Liquid < b)
                {
                    b = Main.tile.At(this.x, this.y).Liquid;
                }
                TileRef expr_16F = Main.tile.At(this.x, this.y);
                expr_16F.AddLiquid(-b);
            }

            if (Main.tile.At(this.x, this.y).Liquid == 0)
            {
                this.kill = 9;
                return;
            }

            if (Main.tile.At(this.x, this.y).Lava)
            {
                Liquid.LavaCheck(this.x, this.y);
                if (!Liquid.quickFall)
                {
                    if (this.delay < 5)
                    {
                        this.delay++;
                        return;
                    }
                    this.delay = 0;
                }
            }
            else
            {
                if (Main.tile.At(this.x - 1, this.y).Lava)
                {
                    Liquid.AddWater(this.x - 1, this.y);
                }
                if (Main.tile.At(this.x + 1, this.y).Lava)
                {
                    Liquid.AddWater(this.x + 1, this.y);
                }
                if (Main.tile.At(this.x, this.y - 1).Lava)
                {
                    Liquid.AddWater(this.x, this.y - 1);
                }
                if (Main.tile.At(this.x, this.y + 1).Lava)
                {
                    Liquid.AddWater(this.x, this.y + 1);
                }
            }

            if ((!Main.tile.At(this.x, this.y + 1).Active || !Main.tileSolid[(int)Main.tile.At(this.x, this.y + 1).Type] || Main.tileSolidTop[(int)Main.tile.At(this.x, this.y + 1).Type]) && (Main.tile.At(this.x, this.y + 1).Liquid <= 0 || Main.tile.At(this.x, this.y + 1).Lava == Main.tile.At(this.x, this.y).Lava) && Main.tile.At(this.x, this.y + 1).Liquid < 255)
            {
                num = (float)(255 - Main.tile.At(this.x, this.y + 1).Liquid);
                if (num > (float)Main.tile.At(this.x, this.y).Liquid)
                {
                    num = (float)Main.tile.At(this.x, this.y).Liquid;
                }
                TileRef expr_42E = Main.tile.At(this.x, this.y);
                expr_42E.AddLiquid(-(byte)num);
                TileRef expr_455 = Main.tile.At(this.x, this.y + 1);
                expr_455.AddLiquid(+(byte)num);
                Main.tile.At(this.x, this.y + 1).SetLava(Main.tile.At(this.x, this.y).Lava);
                Liquid.AddWater(this.x, this.y + 1);
                Main.tile.At(this.x, this.y + 1).SetSkipLiquid(true);
                Main.tile.At(this.x, this.y).SetSkipLiquid(true);
                if (Main.tile.At(this.x, this.y).Liquid > 250)
                {
                    Main.tile.At(this.x, this.y).SetLiquid(255);
                }
                else
                {
                    Liquid.AddWater(this.x - 1, this.y);
                    Liquid.AddWater(this.x + 1, this.y);
                }
            }

            if (Main.tile.At(this.x, this.y).Liquid > 0)
            {
                bool flag  = true;
                bool flag2 = true;
                bool flag3 = true;
                bool flag4 = true;
                if (Main.tile.At(this.x - 1, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x - 1, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x - 1, this.y).Type])
                {
                    flag = false;
                }
                else if (Main.tile.At(this.x - 1, this.y).Liquid > 0 && Main.tile.At(this.x - 1, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                {
                    flag = false;
                }
                else if (Main.tile.At(this.x - 2, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x - 2, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x - 2, this.y).Type])
                {
                    flag3 = false;
                }
                else if (Main.tile.At(this.x - 2, this.y).Liquid == 0)
                {
                    flag3 = false;
                }
                else if (Main.tile.At(this.x - 2, this.y).Liquid > 0 && Main.tile.At(this.x - 2, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                {
                    flag3 = false;
                }

                if (Main.tile.At(this.x + 1, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x + 1, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x + 1, this.y).Type])
                {
                    flag2 = false;
                }
                else
                {
                    if (Main.tile.At(this.x + 1, this.y).Liquid > 0 && Main.tile.At(this.x + 1, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                    {
                        flag2 = false;
                    }
                    else
                    {
                        if (Main.tile.At(this.x + 2, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x + 2, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x + 2, this.y).Type])
                        {
                            flag4 = false;
                        }
                        else
                        {
                            if (Main.tile.At(this.x + 2, this.y).Liquid == 0)
                            {
                                flag4 = false;
                            }
                            else
                            {
                                if (Main.tile.At(this.x + 2, this.y).Liquid > 0 && Main.tile.At(this.x + 2, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                                {
                                    flag4 = false;
                                }
                            }
                        }
                    }
                }
                int num2 = 0;
                if (Main.tile.At(this.x, this.y).Liquid < 3)
                {
                    num2 = -1;
                }
                if (flag && flag2)
                {
                    if (flag3 && flag4)
                    {
                        bool flag5 = true;
                        bool flag6 = true;
                        if (Main.tile.At(this.x - 3, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x - 3, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x - 3, this.y).Type])
                        {
                            flag5 = false;
                        }
                        else
                        {
                            if (Main.tile.At(this.x - 3, this.y).Liquid == 0)
                            {
                                flag5 = false;
                            }
                            else
                            {
                                if (Main.tile.At(this.x - 3, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                                {
                                    flag5 = false;
                                }
                            }
                        }
                        if (Main.tile.At(this.x + 3, this.y).Active&& Main.tileSolid[(int)Main.tile.At(this.x + 3, this.y).Type] && !Main.tileSolidTop[(int)Main.tile.At(this.x + 3, this.y).Type])
                        {
                            flag6 = false;
                        }
                        else
                        {
                            if (Main.tile.At(this.x + 3, this.y).Liquid == 0)
                            {
                                flag6 = false;
                            }
                            else
                            {
                                if (Main.tile.At(this.x + 3, this.y).Lava != Main.tile.At(this.x, this.y).Lava)
                                {
                                    flag6 = false;
                                }
                            }
                        }
                        if (flag5 && flag6)
                        {
                            num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x - 2, this.y).Liquid + Main.tile.At(this.x + 2, this.y).Liquid + Main.tile.At(this.x - 3, this.y).Liquid + Main.tile.At(this.x + 3, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 7f));
                            int num3 = 0;
                            Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 1, this.y);
                                Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                                Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            Main.tile.At(this.x - 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 2, this.y);
                                Main.tile.At(this.x - 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            Main.tile.At(this.x + 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 2, this.y);
                                Main.tile.At(this.x + 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            Main.tile.At(this.x - 3, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 3, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 3, this.y);
                                Main.tile.At(this.x - 3, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            Main.tile.At(this.x + 3, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 3, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 3, this.y);
                                Main.tile.At(this.x + 3, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }
                            if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 1, this.y);
                            }
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                            }
                            if (Main.tile.At(this.x - 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 2, this.y);
                            }
                            if (Main.tile.At(this.x + 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 2, this.y);
                            }
                            if (Main.tile.At(this.x - 3, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 3, this.y);
                            }
                            if (Main.tile.At(this.x + 3, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 3, this.y);
                            }
                            if (num3 != 6 || Main.tile.At(this.x, this.y - 1).Liquid <= 0)
                            {
                                Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                        else
                        {
                            int num4 = 0;
                            num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x - 2, this.y).Liquid + Main.tile.At(this.x + 2, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 5f));
                            Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 1, this.y);
                                Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }
                            Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                                Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }
                            Main.tile.At(this.x - 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 2, this.y);
                                Main.tile.At(this.x - 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }
                            Main.tile.At(this.x + 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 2, this.y);
                                Main.tile.At(this.x + 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }
                            if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 1, this.y);
                            }
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                            }
                            if (Main.tile.At(this.x - 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 2, this.y);
                            }
                            if (Main.tile.At(this.x + 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 2, this.y);
                            }
                            if (num4 != 4 || Main.tile.At(this.x, this.y - 1).Liquid <= 0)
                            {
                                Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                    }
                    else
                    {
                        if (flag3)
                        {
                            num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x - 2, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 4f) + 0.001);
                            Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x - 1, this.y);
                                Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                            }
                            Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                                Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            Main.tile.At(this.x - 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x - 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                            {
                                Main.tile.At(this.x - 2, this.y).SetLiquid((byte)num);
                                Liquid.AddWater(this.x - 2, this.y);
                            }
                            Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                        }
                        else
                        {
                            if (flag4)
                            {
                                num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x + 2, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                                num = (float)Math.Round((double)(num / 4f) + 0.001);
                                Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                                if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(this.x - 1, this.y);
                                    Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                                }
                                Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                                if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(this.x + 1, this.y);
                                    Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                                }
                                Main.tile.At(this.x + 2, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                                if (Main.tile.At(this.x + 2, this.y).Liquid != (byte)num || Main.tile.At(this.x, this.y).Liquid != (byte)num)
                                {
                                    Main.tile.At(this.x + 2, this.y).SetLiquid((byte)num);
                                    Liquid.AddWater(this.x + 2, this.y);
                                }
                                Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                                num = (float)Math.Round((double)(num / 3f) + 0.001);
                                Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                                if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                                {
                                    Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                                }
                                if (Main.tile.At(this.x, this.y).Liquid != (byte)num || Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(this.x - 1, this.y);
                                }
                                Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                                if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                                {
                                    Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                                }
                                if (Main.tile.At(this.x, this.y).Liquid != (byte)num || Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(this.x + 1, this.y);
                                }
                                Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                    }
                }
                else
                {
                    if (flag)
                    {
                        num = (float)((int)(Main.tile.At(this.x - 1, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                        num = (float)Math.Round((double)(num / 2f) + 0.001);
                        if (Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                        {
                            Main.tile.At(this.x - 1, this.y).SetLiquid((byte)num);
                        }
                        Main.tile.At(this.x - 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                        if (Main.tile.At(this.x, this.y).Liquid != (byte)num || Main.tile.At(this.x - 1, this.y).Liquid != (byte)num)
                        {
                            Liquid.AddWater(this.x - 1, this.y);
                        }
                        Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                    }
                    else
                    {
                        if (flag2)
                        {
                            num = (float)((int)(Main.tile.At(this.x + 1, this.y).Liquid + Main.tile.At(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 2f) + 0.001);
                            if (Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Main.tile.At(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            Main.tile.At(this.x + 1, this.y).SetLava(Main.tile.At(this.x, this.y).Lava);
                            if (Main.tile.At(this.x, this.y).Liquid != (byte)num || Main.tile.At(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(this.x + 1, this.y);
                            }
                            Main.tile.At(this.x, this.y).SetLiquid((byte)num);
                        }
                    }
                }
            }
            if (Main.tile.At(this.x, this.y).Liquid == liquid)
            {
                this.kill++;
                return;
            }
            if (Main.tile.At(this.x, this.y).Liquid == 254 && liquid == 255)
            {
                Main.tile.At(this.x, this.y).SetLiquid(255);
                this.kill++;
                return;
            }
            Liquid.AddWater(this.x, this.y - 1);
            this.kill = 0;
        }
        public static void DelWater(Func <Int32, Int32, ITile> TileRefs, ISandbox sandbox, int id)
        {
            if (TileRefs == null)
            {
                TileRefs = TileCollection.ITileAt;
            }

            int x = Main.liquid[id].x;
            int y = Main.liquid[id].y;

            if (TileRefs(x, y).Liquid < 2)
            {
                TileRefs(x, y).SetLiquid(0);

                if (TileRefs(x - 1, y).Liquid < 2)
                {
                    TileRefs(x - 1, y).SetLiquid(0);
                }

                if (TileRefs(x + 1, y).Liquid < 2)
                {
                    TileRefs(x + 1, y).SetLiquid(0);
                }
            }
            else if (TileRefs(x, y).Liquid < 20)
            {
                if ((TileRefs(x - 1, y).Liquid < TileRefs(x, y).Liquid&& (!TileRefs(x - 1, y).Active || !Main.tileSolid[(int)TileRefs(x - 1, y).Type] || Main.tileSolidTop[(int)TileRefs(x - 1, y).Type])) || (TileRefs(x + 1, y).Liquid < TileRefs(x, y).Liquid&& (!TileRefs(x + 1, y).Active || !Main.tileSolid[(int)TileRefs(x + 1, y).Type] || Main.tileSolidTop[(int)TileRefs(x + 1, y).Type])) || (TileRefs(x, y + 1).Liquid < 255 && (!TileRefs(x, y + 1).Active || !Main.tileSolid[(int)TileRefs(x, y + 1).Type] || Main.tileSolidTop[(int)TileRefs(x, y + 1).Type])))
                {
                    TileRefs(x, y).SetLiquid(0);
                }
            }
            else if (TileRefs(x, y + 1).Liquid < 255 && (!TileRefs(x, y + 1).Active || !Main.tileSolid[(int)TileRefs(x, y + 1).Type] || Main.tileSolidTop[(int)TileRefs(x, y + 1).Type]) && !Liquid.stuck)
            {
                Main.liquid[id].kill = 0;
                return;
            }

            if (TileRefs(x, y).Liquid < 250 && TileRefs(x, y - 1).Liquid > 0)
            {
                Liquid.AddWater(TileRefs, sandbox, x, y - 1);
            }

            if (TileRefs(x, y).Liquid == 0)
            {
                TileRefs(x, y).SetLava(false);
            }
            else
            {
                if ((TileRefs(x + 1, y).Liquid > 0 && TileRefs(x + 1, y + 1).Liquid < 250 && !TileRefs(x + 1, y + 1).Active) || (TileRefs(x - 1, y).Liquid > 0 && TileRefs(x - 1, y + 1).Liquid < 250 && !TileRefs(x - 1, y + 1).Active))
                {
                    Liquid.AddWater(TileRefs, sandbox, x - 1, y);
                    Liquid.AddWater(TileRefs, sandbox, x + 1, y);
                }
                if (TileRefs(x, y).Lava)
                {
                    Liquid.LavaCheck(TileRefs, sandbox, x, y);
                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= y + 1; j++)
                        {
                            if (TileRefs(i, j).Active)
                            {
                                if (TileRefs(i, j).Type == 2 || TileRefs(i, j).Type == 23 || TileRefs(i, j).Type == 109)
                                {
                                    TileRefs(i, j).SetType(0);
                                    WorldModify.SquareTileFrame(TileRefs, sandbox, i, j, true);
                                    NetMessage.SendTileSquare(-1, x, y, 3);
                                }
                                else if (TileRefs(i, j).Type == 60 || TileRefs(i, j).Type == 70)
                                {
                                    TileRefs(i, j).SetType(59);
                                    WorldModify.SquareTileFrame(TileRefs, sandbox, i, j, true);
                                    NetMessage.SendTileSquare(-1, x, y, 3);
                                }
                            }
                        }
                    }
                }
            }

            NetMessage.SendWater(x, y);

            Liquid.numLiquid--;
            TileRefs(Main.liquid[id].x, Main.liquid[id].y).SetCheckingLiquid(false);
            Main.liquid[id].x    = Main.liquid[Liquid.numLiquid].x;
            Main.liquid[id].y    = Main.liquid[Liquid.numLiquid].y;
            Main.liquid[id].kill = Main.liquid[Liquid.numLiquid].kill;

            if (Main.tileAlch[(int)TileRefs(x, y).Type])
            {
                WorldModify.CheckAlch(TileRefs, sandbox, x, y);
            }
        }
        public static void UpdateLiquid(Func <Int32, Int32, ITile> TileRefs, ISandbox sandbox)
        {
            if (TileRefs == null)
            {
                TileRefs = TileCollection.ITileAt;
            }

            //{
            //Liquid.cycles = 30;
            //    Liquid.maxLiquid = 6000;
            //}

            if (!WorldModify.gen)
            {
                if (!Liquid.panicMode)
                {
                    if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > 4000)
                    {
                        Liquid.panicCounter++;
                        if (Liquid.panicCounter > 1800 || Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > 13500)
                        {
                            Liquid.StartPanic();
                        }
                    }
                    else
                    {
                        Liquid.panicCounter = 0;
                    }
                }

                if (Liquid.panicMode)
                {
                    int num = 0;
                    while (Liquid.panicY >= 3 && num < 5)
                    {
                        num++;
                        Liquid.QuickWater(TileRefs, sandbox, 0, Liquid.panicY, Liquid.panicY);
                        Liquid.panicY--;
                        if (Liquid.panicY < 3)
                        {
                            ProgramLog.Log(Languages.Water_WaterHasBeenSettled);
                            Liquid.panicCounter = 0;
                            Liquid.panicMode    = false;

                            using (var prog = new ProgressLogger(Main.maxTilesX - 2, Languages.Water_PerformingWaterCheck))
                                WorldModify.WaterCheck(TileRefs, sandbox, prog);

                            for (int i = 0; i < 255; i++)
                            {
                                for (int j = 0; j < Main.maxSectionsX; j++)
                                {
                                    for (int k = 0; k < Main.maxSectionsY; k++)
                                    {
                                        NetPlay.slots[i].tileSection[j, k] = false;
                                    }
                                }
                            }
                        }
                    }
                    return;
                }
            }

            Liquid.quickFall = Liquid.quickSettle || Liquid.numLiquid > 2000;
            Liquid.wetCounter++;

            int num2 = Liquid.maxLiquid / Liquid.cycles;
            int num3 = num2 * (Liquid.wetCounter - 1);
            int num4 = num2 * Liquid.wetCounter;

            if (Liquid.wetCounter == Liquid.cycles)
            {
                num4 = Liquid.numLiquid;
            }

            if (num4 > Liquid.numLiquid)
            {
                num4 = Liquid.numLiquid;
                Liquid.wetCounter = Liquid.cycles;
            }

            if (Liquid.quickFall)
            {
                for (int l = num3; l < num4; l++)
                {
                    Main.liquid[l].delay = 10;
                    Main.liquid[l].Update(TileRefs, sandbox);
                    TileRefs(Main.liquid[l].x, Main.liquid[l].y).SetSkipLiquid(false);
                }
            }
            else
            {
                for (int m = num3; m < num4; m++)
                {
                    if (!TileRefs(Main.liquid[m].x, Main.liquid[m].y).SkipLiquid)
                    {
                        Main.liquid[m].Update(TileRefs, sandbox);
                    }
                    else
                    {
                        TileRefs(Main.liquid[m].x, Main.liquid[m].y).SetSkipLiquid(false);
                    }
                }
            }

            if (Liquid.wetCounter >= Liquid.cycles)
            {
                Liquid.wetCounter = 0;

                for (int n = Liquid.numLiquid - 1; n >= 0; n--)
                {
                    if (Main.liquid[n].kill > 3)
                    {
                        Liquid.DelWater(TileRefs, sandbox, n);
                    }
                }

                int num5 = Liquid.maxLiquid - (Liquid.maxLiquid - Liquid.numLiquid);
                if (num5 > LiquidBuffer.numLiquidBuffer)
                {
                    num5 = LiquidBuffer.numLiquidBuffer;
                }

                for (int num6 = 0; num6 < num5; num6++)
                {
                    TileRefs(Main.liquidBuffer[0].x, Main.liquidBuffer[0].y).SetCheckingLiquid(false);
                    Liquid.AddWater(TileRefs, sandbox, Main.liquidBuffer[0].x, Main.liquidBuffer[0].y);
                    LiquidBuffer.DelBuffer(0);
                }

                if (Liquid.numLiquid > 0 && Liquid.numLiquid > Liquid.stuckAmount - 50 && Liquid.numLiquid < Liquid.stuckAmount + 50)
                {
                    Liquid.stuckCount++;
                    if (Liquid.stuckCount >= 10000)
                    {
                        Liquid.stuck = true;

                        for (int num7 = Liquid.numLiquid - 1; num7 >= 0; num7--)
                        {
                            Liquid.DelWater(TileRefs, sandbox, num7);
                        }

                        Liquid.stuck      = false;
                        Liquid.stuckCount = 0;
                        return;
                    }
                }
                else
                {
                    Liquid.stuckCount  = 0;
                    Liquid.stuckAmount = Liquid.numLiquid;
                }
            }
        }
        public static double QuickWater(Func <Int32, Int32, ITile> TileRefs, ISandbox sandbox, int verbose = 0, int minY = -1, int maxY = -1, ProgressLogger prog = null)
        {
            if (TileRefs == null)
            {
                TileRefs = TileCollection.ITileAt;
            }

            int num = 0;

            if (minY == -1)
            {
                minY = 3;
            }
            if (maxY == -1)
            {
                maxY = Main.maxTilesY - 3;
            }
            for (int i = maxY; i >= minY; i--)
            {
                if (verbose > 0)
                {
                    float num2 = (float)(maxY - i) / (float)(maxY - minY + 1);
                    num2 /= (float)verbose;

                    if (prog != null)
                    {
                        prog.Value += (int)(num2 * 100f + 1f);
                    }
                }
                else if (verbose < 0)
                {
                    float num3 = (float)(maxY - i) / (float)(maxY - minY + 1);
                    num3 /= (float)(-(float)verbose);

                    if (prog != null)
                    {
                        prog.Value += (int)(num3 * 100f + 1f);
                    }
                }

                for (int j = 0; j < 2; j++)
                {
                    int num4 = 2;
                    int num5 = Main.maxTilesX - 2;
                    int num6 = 1;
                    if (j == 1)
                    {
                        num4 = Main.maxTilesX - 2;
                        num5 = 2;
                        num6 = -1;
                    }
                    for (int num7 = num4; num7 != num5; num7 += num6)
                    {
                        if (TileRefs(num7, i).Liquid > 0)
                        {
                            int  num8  = -num6;
                            bool flag  = false;
                            int  num9  = num7;
                            int  num10 = i;

                            bool flag2 = TileRefs(num7, i).Lava;
                            byte b     = TileRefs(num7, i).Liquid;
                            TileRefs(num7, i).SetLiquid(0);

                            bool flag3 = true;
                            int  num11 = 0;
                            while (flag3 && num9 > 3 && num9 < Main.maxTilesX - 3 && num10 < Main.maxTilesY - 3)
                            {
                                flag3 = false;
                                while (TileRefs(num9, num10 + 1).Liquid == 0 && num10 < Main.maxTilesY - 5 && (!TileRefs(num9, num10 + 1).Active || !Main.tileSolid[(int)TileRefs(num9, num10 + 1).Type] || Main.tileSolidTop[(int)TileRefs(num9, num10 + 1).Type]))
                                {
                                    flag  = true;
                                    num8  = num6;
                                    num11 = 0;
                                    flag3 = true;
                                    num10++;
                                    if (num10 > WorldModify.waterLine)
                                    {
                                        flag2 = true;
                                    }
                                }
                                if (TileRefs(num9, num10 + 1).Liquid > 0 && TileRefs(num9, num10 + 1).Liquid < 255 && TileRefs(num9, num10 + 1).Lava == flag2)
                                {
                                    int num12 = (int)(255 - TileRefs(num9, num10 + 1).Liquid);
                                    if (num12 > (int)b)
                                    {
                                        num12 = (int)b;
                                    }

                                    TileRefs(num9, num10 + 1).AddLiquid((byte)num12);

                                    b -= (byte)num12;
                                    if (b <= 0)
                                    {
                                        num++;
                                        break;
                                    }
                                }
                                if (num11 == 0)
                                {
                                    if (TileRefs(num9 + num8, num10).Liquid == 0 && (!TileRefs(num9 + num8, num10).Active || !Main.tileSolid[(int)TileRefs(num9 + num8, num10).Type] || Main.tileSolidTop[(int)TileRefs(num9 + num8, num10).Type]))
                                    {
                                        num11 = num8;
                                    }
                                    else if (TileRefs(num9 - num8, num10).Liquid == 0 && (!TileRefs(num9 - num8, num10).Active || !Main.tileSolid[(int)TileRefs(num9 - num8, num10).Type] || Main.tileSolidTop[(int)TileRefs(num9 - num8, num10).Type]))
                                    {
                                        num11 = -num8;
                                    }
                                }
                                if (num11 != 0 && TileRefs(num9 + num11, num10).Liquid == 0 && (!TileRefs(num9 + num11, num10).Active || !Main.tileSolid[(int)TileRefs(num9 + num11, num10).Type] || Main.tileSolidTop[(int)TileRefs(num9 + num11, num10).Type]))
                                {
                                    flag3 = true;
                                    num9 += num11;
                                }
                                if (flag && !flag3)
                                {
                                    flag  = false;
                                    flag3 = true;
                                    num8  = -num6;
                                    num11 = 0;
                                }
                            }
                            if (num7 != num9 && i != num10)
                            {
                                num++;
                            }

                            TileRefs(num9, num10).SetLiquid(b);
                            TileRefs(num9, num10).SetLava(flag2);

                            if (TileRefs(num9 - 1, num10).Liquid > 0 && TileRefs(num9 - 1, num10).Lava != flag2)
                            {
                                if (flag2)
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10);
                                }
                                else
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9 - 1, num10);
                                }
                            }
                            else if (TileRefs(num9 + 1, num10).Liquid > 0 && TileRefs(num9 + 1, num10).Lava != flag2)
                            {
                                if (flag2)
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10);
                                }
                                else
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9 + 1, num10);
                                }
                            }
                            else if (TileRefs(num9, num10 - 1).Liquid > 0 && TileRefs(num9, num10 - 1).Lava != flag2)
                            {
                                if (flag2)
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10);
                                }
                                else
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10 - 1);
                                }
                            }
                            else if (TileRefs(num9, num10 + 1).Liquid > 0 && TileRefs(num9, num10 + 1).Lava != flag2)
                            {
                                if (flag2)
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10);
                                }
                                else
                                {
                                    Liquid.LavaCheck(TileRefs, sandbox, num9, num10 + 1);
                                }
                            }
                        }
                    }
                }
            }

            return((double)num);
        }
        public void Update(Func <Int32, Int32, ITile> TileRefs, ISandbox sandbox)
        {
            if (TileRefs == null)
            {
                TileRefs = TileCollection.ITileAt;
            }

            if (TileRefs(this.x, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x, this.y).Type])
            {
                this.kill = 9;
                return;
            }

            byte  liquid = TileRefs(this.x, this.y).Liquid;
            float num    = 0f;

            if (this.y > Main.maxTilesY - 200 && !TileRefs(this.x, this.y).Lava&& TileRefs(this.x, this.y).Liquid > 0)
            {
                byte b = 2;
                if (TileRefs(this.x, this.y).Liquid < b)
                {
                    b = TileRefs(this.x, this.y).Liquid;
                }

                TileRefs(this.x, this.y).AddLiquid(-b);
            }

            if (TileRefs(this.x, this.y).Liquid == 0)
            {
                this.kill = 9;
                return;
            }

            if (TileRefs(this.x, this.y).Lava)
            {
                Liquid.LavaCheck(TileRefs, sandbox, this.x, this.y);
                if (!Liquid.quickFall)
                {
                    if (this.delay < 5)
                    {
                        this.delay++;
                        return;
                    }
                    this.delay = 0;
                }
            }
            else
            {
                if (TileRefs(this.x - 1, this.y).Lava)
                {
                    Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                }

                if (TileRefs(this.x + 1, this.y).Lava)
                {
                    Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                }

                if (TileRefs(this.x, this.y - 1).Lava)
                {
                    Liquid.AddWater(TileRefs, sandbox, this.x, this.y - 1);
                }

                if (TileRefs(this.x, this.y + 1).Lava)
                {
                    Liquid.AddWater(TileRefs, sandbox, this.x, this.y + 1);
                }
            }
            if ((!TileRefs(this.x, this.y + 1).Active || !Main.tileSolid[(int)TileRefs(this.x, this.y + 1).Type] || Main.tileSolidTop[(int)TileRefs(this.x, this.y + 1).Type]) && (TileRefs(this.x, this.y + 1).Liquid <= 0 || TileRefs(this.x, this.y + 1).Lava == TileRefs(this.x, this.y).Lava) && TileRefs(this.x, this.y + 1).Liquid < 255)
            {
                num = (float)(255 - TileRefs(this.x, this.y + 1).Liquid);
                if (num > (float)TileRefs(this.x, this.y).Liquid)
                {
                    num = (float)TileRefs(this.x, this.y).Liquid;
                }

                TileRefs(this.x, this.y).AddLiquid(-(byte)num);
                TileRefs(this.x, this.y + 1).AddLiquid((byte)num);

                TileRefs(this.x, this.y + 1).SetLava(TileRefs(this.x, this.y).Lava);

                Liquid.AddWater(TileRefs, sandbox, this.x, this.y + 1);

                TileRefs(this.x, this.y + 1).SetSkipLiquid(true);
                TileRefs(this.x, this.y).SetSkipLiquid(true);

                if (TileRefs(this.x, this.y).Liquid > 250)
                {
                    TileRefs(this.x, this.y).SetLiquid(255);
                }
                else
                {
                    Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                    Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                }
            }
            if (TileRefs(this.x, this.y).Liquid > 0)
            {
                bool flag  = true;
                bool flag2 = true;
                bool flag3 = true;
                bool flag4 = true;
                if (TileRefs(this.x - 1, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x - 1, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x - 1, this.y).Type])
                {
                    flag = false;
                }
                else if (TileRefs(this.x - 1, this.y).Liquid > 0 && TileRefs(this.x - 1, this.y).Lava != TileRefs(this.x, this.y).Lava)
                {
                    flag = false;
                }
                else if (TileRefs(this.x - 2, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x - 2, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x - 2, this.y).Type])
                {
                    flag3 = false;
                }
                else if (TileRefs(this.x - 2, this.y).Liquid == 0)
                {
                    flag3 = false;
                }
                else if (TileRefs(this.x - 2, this.y).Liquid > 0 && TileRefs(this.x - 2, this.y).Lava != TileRefs(this.x, this.y).Lava)
                {
                    flag3 = false;
                }

                if (TileRefs(this.x + 1, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x + 1, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x + 1, this.y).Type])
                {
                    flag2 = false;
                }
                else if (TileRefs(this.x + 1, this.y).Liquid > 0 && TileRefs(this.x + 1, this.y).Lava != TileRefs(this.x, this.y).Lava)
                {
                    flag2 = false;
                }
                else if (TileRefs(this.x + 2, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x + 2, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x + 2, this.y).Type])
                {
                    flag4 = false;
                }
                else if (TileRefs(this.x + 2, this.y).Liquid == 0)
                {
                    flag4 = false;
                }
                else if (TileRefs(this.x + 2, this.y).Liquid > 0 && TileRefs(this.x + 2, this.y).Lava != TileRefs(this.x, this.y).Lava)
                {
                    flag4 = false;
                }

                int num2 = 0;
                if (TileRefs(this.x, this.y).Liquid < 3)
                {
                    num2 = -1;
                }
                if (flag && flag2)
                {
                    if (flag3 && flag4)
                    {
                        bool flag5 = true;
                        bool flag6 = true;
                        if (TileRefs(this.x - 3, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x - 3, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x - 3, this.y).Type])
                        {
                            flag5 = false;
                        }
                        else if (TileRefs(this.x - 3, this.y).Liquid == 0)
                        {
                            flag5 = false;
                        }
                        else if (TileRefs(this.x - 3, this.y).Lava != TileRefs(this.x, this.y).Lava)
                        {
                            flag5 = false;
                        }

                        if (TileRefs(this.x + 3, this.y).Active&& Main.tileSolid[(int)TileRefs(this.x + 3, this.y).Type] && !Main.tileSolidTop[(int)TileRefs(this.x + 3, this.y).Type])
                        {
                            flag6 = false;
                        }
                        else if (TileRefs(this.x + 3, this.y).Liquid == 0)
                        {
                            flag6 = false;
                        }
                        else if (TileRefs(this.x + 3, this.y).Lava != TileRefs(this.x, this.y).Lava)
                        {
                            flag6 = false;
                        }

                        if (flag5 && flag6)
                        {
                            num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x - 2, this.y).Liquid + TileRefs(this.x + 2, this.y).Liquid + TileRefs(this.x - 3, this.y).Liquid + TileRefs(this.x + 3, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 7f));
                            int num3 = 0;

                            TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                                TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                                TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            TileRefs(this.x - 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 2, this.y);
                                TileRefs(this.x - 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            TileRefs(this.x + 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 2, this.y);
                                TileRefs(this.x + 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            TileRefs(this.x - 3, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 3, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 3, this.y);
                                TileRefs(this.x - 3, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            TileRefs(this.x + 3, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 3, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 3, this.y);
                                TileRefs(this.x + 3, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num3++;
                            }

                            if (TileRefs(this.x - 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                            }
                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                            }
                            if (TileRefs(this.x - 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 2, this.y);
                            }
                            if (TileRefs(this.x + 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 2, this.y);
                            }
                            if (TileRefs(this.x - 3, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 3, this.y);
                            }
                            if (TileRefs(this.x + 3, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 3, this.y);
                            }
                            if (num3 != 6 || TileRefs(this.x, this.y - 1).Liquid <= 0)
                            {
                                TileRefs(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                        else
                        {
                            int num4 = 0;
                            num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x - 2, this.y).Liquid + TileRefs(this.x + 2, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 5f));

                            TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                                TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }

                            TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                                TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }

                            TileRefs(this.x - 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 2, this.y);
                                TileRefs(this.x - 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }

                            TileRefs(this.x + 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 2, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 2, this.y);
                                TileRefs(this.x + 2, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num4++;
                            }

                            if (TileRefs(this.x - 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                            }
                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                            }
                            if (TileRefs(this.x - 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 2, this.y);
                            }
                            if (TileRefs(this.x + 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 2, this.y);
                            }
                            if (num4 != 4 || TileRefs(this.x, this.y - 1).Liquid <= 0)
                            {
                                TileRefs(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                    }
                    else
                    {
                        if (flag3)
                        {
                            num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x - 2, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 4f) + 0.001);

                            TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                                TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                            }

                            TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                                TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                            }

                            TileRefs(this.x - 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x - 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                            {
                                TileRefs(this.x - 2, this.y).SetLiquid((byte)num);
                                Liquid.AddWater(TileRefs, sandbox, this.x - 2, this.y);
                            }

                            TileRefs(this.x, this.y).SetLiquid((byte)num);
                        }
                        else
                        {
                            if (flag4)
                            {
                                num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x + 2, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                                num = (float)Math.Round((double)(num / 4f) + 0.001);

                                TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                                if (TileRefs(this.x - 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                                    TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                                }

                                TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                                if (TileRefs(this.x + 1, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                                    TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                                }

                                TileRefs(this.x + 2, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                                if (TileRefs(this.x + 2, this.y).Liquid != (byte)num || TileRefs(this.x, this.y).Liquid != (byte)num)
                                {
                                    TileRefs(this.x + 2, this.y).SetLiquid((byte)num);
                                    Liquid.AddWater(TileRefs, sandbox, this.x + 2, this.y);
                                }

                                TileRefs(this.x, this.y).SetLiquid((byte)num);
                            }
                            else
                            {
                                num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                                num = (float)Math.Round((double)(num / 3f) + 0.001);

                                TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                                if (TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                                {
                                    TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                                }
                                if (TileRefs(this.x, this.y).Liquid != (byte)num || TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                                }

                                TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                                if (TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                                {
                                    TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                                }
                                if (TileRefs(this.x, this.y).Liquid != (byte)num || TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                                {
                                    Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                                }

                                TileRefs(this.x, this.y).SetLiquid((byte)num);
                            }
                        }
                    }
                }
                else
                {
                    if (flag)
                    {
                        num = (float)((int)(TileRefs(this.x - 1, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                        num = (float)Math.Round((double)(num / 2f) + 0.001);
                        if (TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                        {
                            TileRefs(this.x - 1, this.y).SetLiquid((byte)num);
                        }

                        TileRefs(this.x - 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                        if (TileRefs(this.x, this.y).Liquid != (byte)num || TileRefs(this.x - 1, this.y).Liquid != (byte)num)
                        {
                            Liquid.AddWater(TileRefs, sandbox, this.x - 1, this.y);
                        }

                        TileRefs(this.x, this.y).SetLiquid((byte)num);
                    }
                    else
                    {
                        if (flag2)
                        {
                            num = (float)((int)(TileRefs(this.x + 1, this.y).Liquid + TileRefs(this.x, this.y).Liquid) + num2);
                            num = (float)Math.Round((double)(num / 2f) + 0.001);
                            if (TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                TileRefs(this.x + 1, this.y).SetLiquid((byte)num);
                            }

                            TileRefs(this.x + 1, this.y).SetLava(TileRefs(this.x, this.y).Lava);

                            if (TileRefs(this.x, this.y).Liquid != (byte)num || TileRefs(this.x + 1, this.y).Liquid != (byte)num)
                            {
                                Liquid.AddWater(TileRefs, sandbox, this.x + 1, this.y);
                            }

                            TileRefs(this.x, this.y).SetLiquid((byte)num);
                        }
                    }
                }
            }
            if (TileRefs(this.x, this.y).Liquid == liquid)
            {
                this.kill++;
                return;
            }
            if (TileRefs(this.x, this.y).Liquid == 254 && liquid == 255)
            {
                TileRefs(this.x, this.y).SetLiquid(255);
                this.kill++;
                return;
            }
            Liquid.AddWater(TileRefs, sandbox, this.x, this.y - 1);
            this.kill = 0;
        }