Beispiel #1
0
        public void settleLiquid(GenerationProgress progress)
        {
            progress.Message = Lang.gen[27].Value;
            Liquid.QuickWater(3, -1, -1);
            WorldGen.WaterCheck();
            int k = 0;

            Liquid.quickSettle = true;
            while (k < 10)
            {
                int num = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                k++;
                float num2 = 0f;
                while (Liquid.numLiquid > 0)
                {
                    float num3 = (float)(num - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num;
                    if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num)
                    {
                        num = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                    }
                    if (num3 > num2)
                    {
                        num2 = num3;
                    }
                    else
                    {
                        num3 = num2;
                    }
                    if (k == 1)
                    {
                        progress.Set(num3 / 3f + 0.33f);
                    }
                    int num4 = 10;
                    if (k > num4)
                    {
                    }
                    Liquid.UpdateLiquid();
                }
                WorldGen.WaterCheck();
                progress.Set((float)k * 0.1f / 3f + 0.66f);
            }
            Liquid.quickSettle  = false;
            Main.tileSolid[190] = true;
        }
Beispiel #2
0
        public static void LoadWorld(string LoadPath)
        {
            using (FileStream fileStream = new FileStream(LoadPath, FileMode.Open))
            {
                using (BinaryReader binaryReader = new BinaryReader(fileStream))
                {
                    try
                    {
                        WorldModify.loadFailed  = false;
                        WorldModify.loadSuccess = false;
                        int Terraria_Release = binaryReader.ReadInt32();
                        if (Terraria_Release > Statics.CURRENT_TERRARIA_RELEASE)
                        {
                            WorldModify.loadFailed  = true;
                            WorldModify.loadSuccess = false;
                            try
                            {
                                binaryReader.Close();
                                fileStream.Close();
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            Main.worldName    = binaryReader.ReadString();
                            Main.worldID      = binaryReader.ReadInt32();
                            Main.leftWorld    = (float)binaryReader.ReadInt32();
                            Main.rightWorld   = (float)binaryReader.ReadInt32();
                            Main.topWorld     = (float)binaryReader.ReadInt32();
                            Main.bottomWorld  = (float)binaryReader.ReadInt32();
                            Main.maxTilesY    = binaryReader.ReadInt32();
                            Main.maxTilesX    = binaryReader.ReadInt32();
                            Main.maxSectionsX = Main.maxTilesX / 200;
                            Main.maxSectionsY = Main.maxTilesY / 150;
                            clearWorld();
                            Main.spawnTileX   = binaryReader.ReadInt32();
                            Main.spawnTileY   = binaryReader.ReadInt32();
                            Main.worldSurface = binaryReader.ReadDouble();
                            Main.rockLayer    = binaryReader.ReadDouble();
                            tempTime          = binaryReader.ReadDouble();
                            tempDayTime       = binaryReader.ReadBoolean();
                            tempMoonPhase     = binaryReader.ReadInt32();
                            tempBloodMoon     = binaryReader.ReadBoolean();
                            Main.dungeonX     = binaryReader.ReadInt32();
                            Main.dungeonY     = binaryReader.ReadInt32();
                            NPC.downedBoss1   = binaryReader.ReadBoolean();
                            NPC.downedBoss2   = binaryReader.ReadBoolean();
                            NPC.downedBoss3   = binaryReader.ReadBoolean();
                            if (Terraria_Release >= 29)
                            {
                                NPC.savedGoblin = binaryReader.ReadBoolean();
                                NPC.savedWizard = binaryReader.ReadBoolean();
                                if (Terraria_Release >= 34)
                                {
                                    NPC.savedMech = binaryReader.ReadBoolean();
                                }
                                NPC.downedGoblins = binaryReader.ReadBoolean();
                            }
                            if (Terraria_Release >= 32)
                            {
                                NPC.downedClown = binaryReader.ReadBoolean();
                            }
                            WorldModify.shadowOrbSmashed = binaryReader.ReadBoolean();
                            WorldModify.spawnMeteor      = binaryReader.ReadBoolean();
                            WorldModify.shadowOrbCount   = (int)binaryReader.ReadByte();
                            if (Terraria_Release >= 23)
                            {
                                WorldModify.altarCount = binaryReader.ReadInt32();
                                Main.hardMode          = binaryReader.ReadBoolean();
                            }
                            Main.invasionDelay = binaryReader.ReadInt32();
                            Main.invasionSize  = binaryReader.ReadInt32();
                            Main.invasionType  = binaryReader.ReadInt32();
                            Main.invasionX     = binaryReader.ReadDouble();

                            using (var prog = new ProgressLogger(Main.maxTilesX - 1, "Loading world tiles"))
                            {
                                for (int j = 0; j < Main.maxTilesX; j++)
                                {
                                    prog.Value = j;

                                    for (int k = 0; k < Main.maxTilesY; k++)
                                    {
                                        Main.tile.At(j, k).SetActive(binaryReader.ReadBoolean());
                                        if (Main.tile.At(j, k).Active)
                                        {
                                            Main.tile.At(j, k).SetType(binaryReader.ReadByte());
                                            if (Main.tileFrameImportant[(int)Main.tile.At(j, k).Type])
                                            {
                                                Main.tile.At(j, k).SetFrameX(binaryReader.ReadInt16());
                                                Main.tile.At(j, k).SetFrameY(binaryReader.ReadInt16());
                                            }
                                            else
                                            {
                                                Main.tile.At(j, k).SetFrameX(-1);
                                                Main.tile.At(j, k).SetFrameY(-1);
                                            }
                                        }
                                        if (Terraria_Release <= 25)
                                        {
                                            Main.tile.At(j, k).SetLighted(binaryReader.ReadBoolean());
                                        }
                                        if (binaryReader.ReadBoolean())
                                        {
                                            Main.tile.At(j, k).SetWall(binaryReader.ReadByte());
//											if (Main.tile.At(j, k).Wall == 7)
//												Main.tile.At(j, k).SetWall (17);
                                        }
                                        if (binaryReader.ReadBoolean())
                                        {
                                            Main.tile.At(j, k).SetLiquid(binaryReader.ReadByte());
                                            Main.tile.At(j, k).SetLava(binaryReader.ReadBoolean());
                                        }
                                        if (Terraria_Release >= 33)
                                        {
                                            Main.tile.At(j, k).SetWire(binaryReader.ReadBoolean());
                                        }
                                        if (Terraria_Release >= 25)
                                        {
                                            int num3 = (int)binaryReader.ReadInt16();
                                            if (num3 > 0)
                                            {
                                                for (int l = k + 1; l < k + num3 + 1; l++)
                                                {
                                                    Main.tile.At(j, l).SetActive(Main.tile.At(j, k).Active);
                                                    Main.tile.At(j, l).SetType(Main.tile.At(j, k).Type);
                                                    Main.tile.At(j, l).SetWall(Main.tile.At(j, k).Wall);
                                                    Main.tile.At(j, l).SetFrameX(Main.tile.At(j, k).FrameX);
                                                    Main.tile.At(j, l).SetFrameY(Main.tile.At(j, k).FrameY);
                                                    Main.tile.At(j, l).SetLiquid(Main.tile.At(j, k).Liquid);
                                                    Main.tile.At(j, l).SetLava(Main.tile.At(j, k).Lava);
                                                    Main.tile.At(j, l).SetWire(Main.tile.At(j, k).Wire);
                                                }
                                                k += num3;
                                            }
                                        }
                                    }
                                }
                            }

                            for (int l = 0; l < 1000; l++)
                            {
                                if (binaryReader.ReadBoolean())
                                {
                                    Main.chest[l]   = new Chest();
                                    Main.chest[l].x = binaryReader.ReadInt32();
                                    Main.chest[l].y = binaryReader.ReadInt32();
                                    for (int m = 0; m < Chest.MAX_ITEMS; m++)
                                    {
                                        Main.chest[l].contents[m] = new Item();
                                        int stack = binaryReader.ReadByte();
                                        if (stack > 0)
                                        {
                                            string defaults = Item.VersionName(binaryReader.ReadString(), Terraria_Release);
                                            Main.chest[l].contents[m]       = Registries.Item.Create(defaults, stack);
                                            Main.chest[m].contents[m].Stack = (int)stack;
                                            if (Terraria_Release >= 36)
                                            {
                                                Main.chest[m].contents[m].SetPrefix((int)binaryReader.ReadByte());
                                            }
                                        }
                                    }
                                }
                            }
                            for (int n = 0; n < 1000; n++)
                            {
                                if (binaryReader.ReadBoolean())
                                {
                                    string text = binaryReader.ReadString();
                                    int    num3 = binaryReader.ReadInt32();
                                    int    num4 = binaryReader.ReadInt32();
                                    if (Main.tile.At(num3, num4).Active&& (Main.tile.At(num3, num4).Type == 55 || Main.tile.At(num3, num4).Type == 85))
                                    {
                                        Main.sign[n]      = new Sign();
                                        Main.sign[n].x    = num3;
                                        Main.sign[n].y    = num4;
                                        Main.sign[n].text = text;
                                    }
                                }
                            }
                            bool flag = binaryReader.ReadBoolean();
                            int  num5 = 0;
                            while (flag)
                            {
                                string NPCName = binaryReader.ReadString();
                                Main.npcs[num5]            = Registries.NPC.Create(NPCName);
                                Main.npcs[num5].Position.X = binaryReader.ReadSingle();
                                Main.npcs[num5].Position.Y = binaryReader.ReadSingle();
                                Main.npcs[num5].homeless   = binaryReader.ReadBoolean();
                                Main.npcs[num5].homeTileX  = binaryReader.ReadInt32();
                                Main.npcs[num5].homeTileY  = binaryReader.ReadInt32();
                                flag = binaryReader.ReadBoolean();
                                num5++;
                            }
                            if (Terraria_Release >= 31)
                            {
                                Main.chrName[17]  = binaryReader.ReadString();
                                Main.chrName[18]  = binaryReader.ReadString();
                                Main.chrName[19]  = binaryReader.ReadString();
                                Main.chrName[20]  = binaryReader.ReadString();
                                Main.chrName[22]  = binaryReader.ReadString();
                                Main.chrName[54]  = binaryReader.ReadString();
                                Main.chrName[38]  = binaryReader.ReadString();
                                Main.chrName[107] = binaryReader.ReadString();
                                Main.chrName[108] = binaryReader.ReadString();
                                if (Terraria_Release >= 35)
                                {
                                    Main.chrName[124] = binaryReader.ReadString();
                                }
                            }
                            if (Terraria_Release >= 7)
                            {
                                bool   flag2 = binaryReader.ReadBoolean();
                                string a     = binaryReader.ReadString();
                                int    num6  = binaryReader.ReadInt32();
                                if (!flag2 || !(a == Main.worldName) || num6 != Main.worldID)
                                {
                                    WorldModify.loadSuccess = false;
                                    WorldModify.loadFailed  = true;
                                    binaryReader.Close();
                                    fileStream.Close();
                                    return;
                                }
                                WorldModify.loadSuccess = true;
                            }
                            else
                            {
                                WorldModify.loadSuccess = true;
                            }
                            binaryReader.Close();
                            fileStream.Close();

                            if (!WorldModify.loadFailed && WorldModify.loadSuccess)
                            {
                                WorldModify.gen = true;
                                for (int num9 = 0; num9 < Main.maxTilesX; num9++)
                                {
                                    float num10 = (float)num9 / (float)Main.maxTilesX;
                                    WorldModify.statusText = "Checking tile alignment: " + (int)(num10 * 100f + 1f) + "%";
                                    WorldModify.CountTiles(num9);
                                }
                                WorldModify.waterLine = Main.maxTilesY;
                                NPC.SetNames();
                                Liquid.QuickWater(2, -1, -1);
                                WorldModify.WaterCheck();
                                int num11 = 0;
                                Liquid.quickSettle = true;
                                int   num12 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                                float num13 = 0f;

                                using (var prog = new ProgressLogger(100, "Settling liquids"))
                                    while (Liquid.numLiquid > 0 && num11 < 100000)
                                    {
                                        num11++;
                                        float num14 = (float)(num12 - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num12;
                                        if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num12)
                                        {
                                            num12 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                                        }
                                        if (num14 > num13)
                                        {
                                            num13 = num14;
                                        }
                                        else
                                        {
                                            num14 = num13;
                                        }

                                        prog.Value = (int)(num14 * 50f + 50f);

                                        Liquid.UpdateLiquid();
                                    }
                                Liquid.quickSettle = false;

                                ProgramLog.Log("Performing Water Check");
                                WorldModify.WaterCheck();
                                WorldModify.gen = false;
                            }
                        }
                    }
                    catch
                    {
                        WorldModify.loadFailed  = true;
                        WorldModify.loadSuccess = false;
                        try
                        {
                            binaryReader.Close();
                            fileStream.Close();
                        }
                        catch
                        {
                        }
                        return;
                    }
                }
            }

            if (Main.worldName == null || Main.worldName == "")
            {
                Main.worldName = System.IO.Path.GetFileNameWithoutExtension(LoadPath);
            }

            Statics.WorldLoaded = true;

            PluginManager.NotifyWorldLoaded();

            var ctx = new HookContext
            {
            };

            var args = new HookArgs.WorldLoaded
            {
                Height = Main.maxTilesY,
                Width  = Main.maxTilesX,
            };

            HookPoints.WorldLoaded.Invoke(ref ctx, ref args);
        }
Beispiel #3
0
        public void VanillaHell(GenerationProgress progress)
        {
            progress.Message = Language.GetTextValue("LegacyGen.18");
            progress.Set(0f);
            int num = Main.maxTilesY - WorldGen.genRand.Next(150, 190);

            for (int k = 0; k < Main.maxTilesX; k++)
            {
                num += WorldGen.genRand.Next(-3, 4);
                if (num < Main.maxTilesY - 190)
                {
                    num = Main.maxTilesY - 190;
                }
                if (num > Main.maxTilesY - 160)
                {
                    num = Main.maxTilesY - 160;
                }
                for (int l = num - 20 - WorldGen.genRand.Next(3); l < Main.maxTilesY; l++)
                {
                    if (l >= num)
                    {
                        Main.tile[k, l].active(false);
                        Main.tile[k, l].lava(false);
                        Main.tile[k, l].liquid = 0;
                    }
                    else
                    {
                        Main.tile[k, l].type = 57;
                    }
                }
            }
            int num2 = Main.maxTilesY - WorldGen.genRand.Next(40, 70);

            for (int m = 10; m < Main.maxTilesX - 10; m++)
            {
                num2 += WorldGen.genRand.Next(-10, 11);
                if (num2 > Main.maxTilesY - 60)
                {
                    num2 = Main.maxTilesY - 60;
                }
                if (num2 < Main.maxTilesY - 100)
                {
                    num2 = Main.maxTilesY - 120;
                }
                for (int n = num2; n < Main.maxTilesY - 10; n++)
                {
                    if (!Main.tile[m, n].active())
                    {
                        Main.tile[m, n].lava(true);
                        Main.tile[m, n].liquid = 255;
                    }
                }
            }
            for (int num3 = 0; num3 < Main.maxTilesX; num3++)
            {
                if (WorldGen.genRand.Next(50) == 0)
                {
                    int num4 = Main.maxTilesY - 65;
                    while (!Main.tile[num3, num4].active() && num4 > Main.maxTilesY - 135)
                    {
                        num4--;
                    }
                    WorldGen.TileRunner(WorldGen.genRand.Next(0, Main.maxTilesX), num4 + WorldGen.genRand.Next(20, 50), (double)WorldGen.genRand.Next(15, 20), 1000, 57, true, 0f, (float)WorldGen.genRand.Next(1, 3), true, true);
                }
            }
            Liquid.QuickWater(-2, -1, -1);
            for (int num5 = 0; num5 < Main.maxTilesX; num5++)
            {
                float num6 = (float)num5 / (float)(Main.maxTilesX - 1);
                progress.Set(num6 / 2f + 0.5f);
                if (WorldGen.genRand.Next(13) == 0)
                {
                    int num7 = Main.maxTilesY - 65;
                    while ((Main.tile[num5, num7].liquid > 0 || Main.tile[num5, num7].active()) && num7 > Main.maxTilesY - 140)
                    {
                        num7--;
                    }
                    WorldGen.TileRunner(num5, num7 - WorldGen.genRand.Next(2, 5), (double)WorldGen.genRand.Next(5, 30), 1000, 57, true, 0f, (float)WorldGen.genRand.Next(1, 3), true, true);
                    float num8 = (float)WorldGen.genRand.Next(1, 3);
                    if (WorldGen.genRand.Next(3) == 0)
                    {
                        num8 *= 0.5f;
                    }
                    if (WorldGen.genRand.Next(2) == 0)
                    {
                        WorldGen.TileRunner(num5, num7 - WorldGen.genRand.Next(2, 5), (double)((int)((float)WorldGen.genRand.Next(5, 15) * num8)), (int)((float)WorldGen.genRand.Next(10, 15) * num8), 57, true, 1f, 0.3f, false, true);
                    }
                    if (WorldGen.genRand.Next(2) == 0)
                    {
                        num8 = (float)WorldGen.genRand.Next(1, 3);
                        WorldGen.TileRunner(num5, num7 - WorldGen.genRand.Next(2, 5), (double)((int)((float)WorldGen.genRand.Next(5, 15) * num8)), (int)((float)WorldGen.genRand.Next(10, 15) * num8), 57, true, -1f, 0.3f, false, true);
                    }
                    WorldGen.TileRunner(num5 + WorldGen.genRand.Next(-10, 10), num7 + WorldGen.genRand.Next(-10, 10), (double)WorldGen.genRand.Next(5, 15), WorldGen.genRand.Next(5, 10), -2, false, (float)WorldGen.genRand.Next(-1, 3), (float)WorldGen.genRand.Next(-1, 3), false, true);
                    if (WorldGen.genRand.Next(3) == 0)
                    {
                        WorldGen.TileRunner(num5 + WorldGen.genRand.Next(-10, 10), num7 + WorldGen.genRand.Next(-10, 10), (double)WorldGen.genRand.Next(10, 30), WorldGen.genRand.Next(10, 20), -2, false, (float)WorldGen.genRand.Next(-1, 3), (float)WorldGen.genRand.Next(-1, 3), false, true);
                    }
                    if (WorldGen.genRand.Next(5) == 0)
                    {
                        WorldGen.TileRunner(num5 + WorldGen.genRand.Next(-15, 15), num7 + WorldGen.genRand.Next(-15, 10), (double)WorldGen.genRand.Next(15, 30), WorldGen.genRand.Next(5, 20), -2, false, (float)WorldGen.genRand.Next(-1, 3), (float)WorldGen.genRand.Next(-1, 3), false, true);
                    }
                }
            }
            for (int num9 = 0; num9 < Main.maxTilesX; num9++)
            {
                WorldGen.TileRunner(WorldGen.genRand.Next(20, Main.maxTilesX - 20), WorldGen.genRand.Next(Main.maxTilesY - 180, Main.maxTilesY - 10), (double)WorldGen.genRand.Next(2, 7), WorldGen.genRand.Next(2, 7), -2, false, 0f, 0f, false, true);
            }
            for (int num10 = 0; num10 < Main.maxTilesX; num10++)
            {
                if (!Main.tile[num10, Main.maxTilesY - 145].active())
                {
                    Main.tile[num10, Main.maxTilesY - 145].liquid = 255;
                    Main.tile[num10, Main.maxTilesY - 145].lava(true);
                }
                if (!Main.tile[num10, Main.maxTilesY - 144].active())
                {
                    Main.tile[num10, Main.maxTilesY - 144].liquid = 255;
                    Main.tile[num10, Main.maxTilesY - 144].lava(true);
                }
            }
            for (int num11 = 0; num11 < (int)((double)(Main.maxTilesX * Main.maxTilesY) * 0.0008); num11++)
            {
                WorldGen.TileRunner(WorldGen.genRand.Next(0, Main.maxTilesX), WorldGen.genRand.Next(Main.maxTilesY - 140, Main.maxTilesY), (double)WorldGen.genRand.Next(2, 7), WorldGen.genRand.Next(3, 7), 58, false, 0f, 0f, false, true);
            }
            WorldGen.AddHellHouses();
        }
        public static void loadWorld(bool loadFromCloud)
        {
            MainCallback.WorldLoadBegin();
            WorldFile.IsWorldOnCloud = loadFromCloud;
            Main.checkXMas();
            Main.checkHalloween();
            bool flag = loadFromCloud && SocialAPI.Cloud != null;

            if (!FileUtilities.Exists(Main.worldPathName, flag) && Main.autoGen)
            {
                if (!flag)
                {
                    for (int i = Main.worldPathName.Length - 1; i >= 0; i--)
                    {
                        if (Main.worldPathName.Substring(i, 1) == string.Concat(Path.DirectorySeparatorChar))
                        {
                            string path = Main.worldPathName.Substring(0, i);
                            Directory.CreateDirectory(path);
                            break;
                        }
                    }
                }
                WorldGen.clearWorld();
                WorldGen.generateWorld(-1, Main.AutogenProgress);
                WorldFile.saveWorld();
            }
            if (WorldGen.genRand == null)
            {
                WorldGen.genRand = new Random((int)DateTime.Now.Ticks);
            }
            byte[] buffer = FileUtilities.ReadAllBytes(Main.worldPathName, flag);
            using (MemoryStream memoryStream = new MemoryStream(buffer))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    try
                    {
                        WorldGen.loadFailed  = false;
                        WorldGen.loadSuccess = false;
                        int num = binaryReader.ReadInt32();
                        WorldFile.versionNumber = num;
                        int num2;
                        if (num <= 87)
                        {
                            num2 = WorldFile.LoadWorld_Version1(binaryReader);
                        }
                        else
                        {
                            num2 = WorldFile.LoadWorld_Version2(binaryReader);
                        }
                        if (num < 141)
                        {
                            if (!loadFromCloud)
                            {
                                Main.ActiveWorldFileData.CreationTime = File.GetCreationTime(Main.worldPathName);
                            }
                            else
                            {
                                Main.ActiveWorldFileData.CreationTime = DateTime.Now;
                            }
                        }
                        binaryReader.Close();
                        memoryStream.Close();
                        if (num2 != 0)
                        {
                            WorldGen.loadFailed = true;
                        }
                        else
                        {
                            WorldGen.loadSuccess = true;
                        }
                        if (WorldGen.loadFailed || !WorldGen.loadSuccess)
                        {
                            return;
                        }
                        WorldGen.gen       = true;
                        WorldGen.waterLine = Main.maxTilesY;
                        Liquid.QuickWater(2, -1, -1);
                        WorldGen.WaterCheck();
                        int num3 = 0;
                        Liquid.quickSettle = true;
                        int   num4 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                        float num5 = 0;
                        while (Liquid.numLiquid > 0 && num3 < 100000)
                        {
                            num3++;
                            float num6 = (float)(num4 - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num4;
                            if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num4)
                            {
                                num4 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                            }
                            if (num6 > num5)
                            {
                                num5 = num6;
                            }
                            else
                            {
                                num6 = num5;
                            }
                            Main.statusText = string.Concat(new object[]
                            {
                                Lang.gen[27],
                                " ",
                                (int)(num6 * 100 / 2 + 50),
                                "%"
                            });
                            Liquid.UpdateLiquid();
                        }
                        Liquid.quickSettle  = false;
                        Main.weatherCounter = WorldGen.genRand.Next(3600, 18000);
                        Cloud.resetClouds();
                        WorldGen.WaterCheck();
                        WorldGen.gen = false;
                        NPC.setFireFlyChance();
                        MainCallback.WorldLoadEnd();
                        Main.InitLifeBytes();
                        if (Main.slimeRainTime > 0)
                        {
                            Main.StartSlimeRain(false);
                        }
                        NPC.setWorldMonsters();
                    }
                    catch (Exception e)
                    {
                        WorldGen.loadFailed  = true;
                        WorldGen.loadSuccess = false;
                        try
                        {
                            binaryReader.Close();
                            memoryStream.Close();
                        }
                        catch
                        {
                        }
                        Console.WriteLine(e);
                        return;
                    }
                }
            }
//            if (WorldFile.OnWorldLoad != null)
//            {
//                WorldFile.OnWorldLoad.Invoke();
//            }
        }
        public static void MuddifyShrines()
        {
            Liquid.QuickWater(3);
            WorldGen.WaterCheck();
            int num78 = 0;

            Liquid.quickSettle = true;
            while (num78 < 10)
            {
                int num79 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                num78++;
                float num80 = 0f;
                while (Liquid.numLiquid > 0)
                {
                    float num81 = (float)(num79 - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num79;
                    if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num79)
                    {
                        num79 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                    }
                    if (num81 > num80)
                    {
                        num80 = num81;
                    }
                    else
                    {
                        num81 = num80;
                    }
                    if (num78 == 1)
                    {
                        //progress.Set(num81 / 3f + 0.33f);
                    }
                    int num82 = 10;
                    if (num78 > num82)
                    {
                        num82 = num78;
                    }
                    Liquid.UpdateLiquid();
                }
                WorldGen.WaterCheck();
            }
            Liquid.quickSettle = false;



            foreach (List <Point> waterPoints in eachShrineWater)
            {
                int indexWaterTile = 0;
                foreach (Point floodedTilePoint in waterPoints.Distinct().Where(testby => Main.tile[testby.X, testby.Y].liquid > 250).OrderBy(testby => 100000 - testby.Y + WorldGen.genRand.Next(-4, 4)))
                {
                    if (indexWaterTile < waterPoints.Count / 2)
                    {
                        Tile tileAbove = Framing.GetTileSafely(floodedTilePoint.X, floodedTilePoint.Y - WorldGen.genRand.Next(2, 5));
                        if (tileAbove.liquid > 250)
                        {
                            Tile tile = Framing.GetTileSafely(floodedTilePoint.X, floodedTilePoint.Y);
                            if (!tile.active() || Main.tileCut[tile.type])
                            {
                                WorldGen.PlaceTile(floodedTilePoint.X, floodedTilePoint.Y, ModContent.TileType <MoistSand>(), false, true);
                            }
                        }
                    }
                    //indexWaterTile += 1;
                }
            }
        }
        private static void LoadWorldDirect(bool loadFromCloud)
        {
            WorldFile.IsWorldOnCloud = loadFromCloud;
            Main.checkXMas();
            Main.checkHalloween();
            bool flag = loadFromCloud && SocialAPI.Cloud != null;

            if (!FileUtilities.Exists(Main.worldPathName, flag) && Main.autoGen)
            {
                if (!flag)
                {
                    for (int i = Main.worldPathName.Length - 1; i >= 0; i--)
                    {
                        if (Main.worldPathName.Substring(i, 1) == (Path.DirectorySeparatorChar.ToString() ?? ""))
                        {
                            Directory.CreateDirectory(Main.worldPathName.Substring(0, i));
                            break;
                        }
                    }
                }
                WorldGen.clearWorld();
                Main.ActiveWorldFileData = WorldFile.CreateMetadata((Main.worldName == "") ? "World" : Main.worldName, flag, Main.expertMode);
                string text = (Main.AutogenSeedName ?? "").Trim();
                if (text.Length == 0)
                {
                    Main.ActiveWorldFileData.SetSeedToRandom();
                }
                else
                {
                    Main.ActiveWorldFileData.SetSeed(text);
                }
                WorldGen.generateWorld(Main.ActiveWorldFileData.Seed, Main.AutogenProgress);
                WorldFile.saveWorld();
            }
            using (MemoryStream memoryStream = new MemoryStream(FileUtilities.ReadAllBytes(Main.worldPathName, flag)))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    try
                    {
                        WorldGen.loadFailed  = false;
                        WorldGen.loadSuccess = false;
                        int num = WorldFile.versionNumber = binaryReader.ReadInt32();
                        int num2;
                        if (num <= 87)
                        {
                            // Not supported
                            num2 = WorldFile.LoadWorld_Version1(binaryReader);
                        }
                        else
                        {
                            num2 = LoadWorld_Version2(binaryReader);
                        }
                        if (num < 141)
                        {
                            if (!loadFromCloud)
                            {
                                Main.ActiveWorldFileData.CreationTime = File.GetCreationTime(Main.worldPathName);
                            }
                            else
                            {
                                Main.ActiveWorldFileData.CreationTime = DateTime.Now;
                            }
                        }
                        binaryReader.Close();
                        memoryStream.Close();
                        if (num2 != 0)
                        {
                            WorldGen.loadFailed = true;
                        }
                        else
                        {
                            WorldGen.loadSuccess = true;
                        }
                        if (WorldGen.loadFailed || !WorldGen.loadSuccess)
                        {
                            return;
                        }
                        WorldGen.gen       = true;
                        WorldGen.waterLine = Main.maxTilesY;
                        Liquid.QuickWater(2, -1, -1);
                        WorldGen.WaterCheck();
                        int num3 = 0;
                        Liquid.quickSettle = true;
                        int   num4 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                        float num5 = 0f;
                        while (Liquid.numLiquid > 0 && num3 < 100000)
                        {
                            num3++;
                            float num6 = (float)(num4 - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num4;
                            if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num4)
                            {
                                num4 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                            }
                            if (num6 > num5)
                            {
                                num5 = num6;
                            }
                            else
                            {
                                num6 = num5;
                            }

                            SetStatusText(string.Concat(new object[]
                            {
                                Lang.gen[27].Value,
                                " ",
                                (int)(num6 * 100f / 2f + 50f),
                                "%"
                            }));
                            Liquid.UpdateLiquid();
                        }
                        Liquid.quickSettle  = false;
                        Main.weatherCounter = WorldGen.genRand.Next(3600, 18000);
                        Cloud.resetClouds();
                        WorldGen.WaterCheck();
                        WorldGen.gen = false;
                        NPC.setFireFlyChance();
                        Main.InitLifeBytes();
                        if (Main.slimeRainTime > 0.0)
                        {
                            Main.StartSlimeRain(false);
                        }
                        NPC.setWorldMonsters();
                    }
                    catch (Exception value)
                    {
                        WorldGen.loadFailed  = true;
                        WorldGen.loadSuccess = false;
                        System.Console.WriteLine(value);
                        try
                        {
                            binaryReader.Close();
                            memoryStream.Close();
                        }
                        catch
                        {
                        }
                        return;
                    }
                }
            }

            EventInfo eventOnWorldLoad = typeof(WorldFile).GetEvent("OnWorldLoad", BindingFlags.Public | BindingFlags.Static);

            eventOnWorldLoad.GetRaiseMethod()?.Invoke(null, new object[] { });
            //if (WorldFile.OnWorldLoad != null)
            //WorldFile.OnWorldLoad();
        }
        public static void LoadWorld(Func <Int32, Int32, ITile> TileRefs, ISandbox sandbox, string LoadPath)
        {
            if (TileRefs == null)
            {
                TileRefs = TileCollection.ITileAt;
            }

            Main.checkXmas();

            using (FileStream fileStream = new FileStream(LoadPath, FileMode.Open))
            {
                using (BinaryReader binaryReader = new BinaryReader(fileStream))
                {
                    try
                    {
                        WorldModify.loadFailed  = false;
                        WorldModify.loadSuccess = false;
                        int Terraria_Release = binaryReader.ReadInt32();
                        if (Terraria_Release > Statics.CURRENT_TERRARIA_RELEASE)
                        {
                            WorldModify.loadFailed  = true;
                            WorldModify.loadSuccess = false;
                            try
                            {
                                binaryReader.Close();
                                fileStream.Close();
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            Main.worldName    = binaryReader.ReadString();
                            Main.worldID      = binaryReader.ReadInt32();
                            Main.leftWorld    = (float)binaryReader.ReadInt32();
                            Main.rightWorld   = (float)binaryReader.ReadInt32();
                            Main.topWorld     = (float)binaryReader.ReadInt32();
                            Main.bottomWorld  = (float)binaryReader.ReadInt32();
                            Main.maxTilesY    = binaryReader.ReadInt32();
                            Main.maxTilesX    = binaryReader.ReadInt32();
                            Main.maxSectionsX = Main.maxTilesX / 200;
                            Main.maxSectionsY = Main.maxTilesY / 150;
                            ClearWorld();
                            Main.spawnTileX   = binaryReader.ReadInt32();
                            Main.spawnTileY   = binaryReader.ReadInt32();
                            Main.worldSurface = binaryReader.ReadDouble();
                            Main.rockLayer    = binaryReader.ReadDouble();
                            tempTime          = binaryReader.ReadDouble();
                            tempDayTime       = binaryReader.ReadBoolean();
                            tempMoonPhase     = binaryReader.ReadInt32();
                            tempBloodMoon     = binaryReader.ReadBoolean();
                            Main.dungeonX     = binaryReader.ReadInt32();
                            Main.dungeonY     = binaryReader.ReadInt32();
                            NPC.downedBoss1   = binaryReader.ReadBoolean();
                            NPC.downedBoss2   = binaryReader.ReadBoolean();
                            NPC.downedBoss3   = binaryReader.ReadBoolean();
                            if (Terraria_Release >= 29)
                            {
                                NPC.savedGoblin = binaryReader.ReadBoolean();
                                NPC.savedWizard = binaryReader.ReadBoolean();
                                if (Terraria_Release >= 34)
                                {
                                    NPC.savedMech = binaryReader.ReadBoolean();
                                }
                                NPC.downedGoblins = binaryReader.ReadBoolean();
                            }
                            if (Terraria_Release >= 32)
                            {
                                NPC.downedClown = binaryReader.ReadBoolean();
                            }
                            if (Terraria_Release >= 37)
                            {
                                NPC.downedFrost = binaryReader.ReadBoolean();
                            }
                            WorldModify.shadowOrbSmashed = binaryReader.ReadBoolean();
                            WorldModify.spawnMeteor      = binaryReader.ReadBoolean();
                            WorldModify.shadowOrbCount   = (int)binaryReader.ReadByte();
                            if (Terraria_Release >= 23)
                            {
                                WorldModify.altarCount = binaryReader.ReadInt32();
                                Main.hardMode          = binaryReader.ReadBoolean();
                            }
                            Main.invasionDelay = binaryReader.ReadInt32();
                            Main.invasionSize  = binaryReader.ReadInt32();
                            Main.invasionType  = (InvasionType)binaryReader.ReadInt32();
                            Main.invasionX     = binaryReader.ReadDouble();

                            using (var prog = new ProgressLogger(Main.maxTilesX - 1, Languages.LoadingWorldTiles))
                            {
                                for (int j = 0; j < Main.maxTilesX; j++)
                                {
                                    prog.Value = j;

                                    for (int k = 0; k < Main.maxTilesY; k++)
                                    {
                                        Main.tile.At(j, k).SetActive(binaryReader.ReadBoolean());
                                        if (Main.tile.At(j, k).Active)
                                        {
                                            Main.tile.At(j, k).SetType(binaryReader.ReadByte());

                                            if (Main.tile.At(j, k).Type == 127)
                                            {
                                                Main.tile.At(j, k).SetActive(false);
                                            }

                                            if (Main.tileFrameImportant[(int)Main.tile.At(j, k).Type])
                                            {
                                                if (Terraria_Release < 28 && Main.tile.At(j, k).Type == 4)
                                                {
                                                    Main.tile.At(j, k).SetFrameX(0);
                                                    Main.tile.At(j, k).SetFrameY(0);
                                                }
                                                else
                                                {
                                                    Main.tile.At(j, k).SetFrameX(binaryReader.ReadInt16());
                                                    Main.tile.At(j, k).SetFrameY(binaryReader.ReadInt16());
                                                    if (Main.tile.At(j, k).Type == 144)
                                                    {
                                                        Main.tile.At(j, k).SetFrameY(0);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                Main.tile.At(j, k).SetFrameX(-1);
                                                Main.tile.At(j, k).SetFrameY(-1);
                                            }
                                        }
                                        if (Terraria_Release <= 25)
                                        {
                                            /*Main.tile.At(j, k).SetLighted(*/
                                            binaryReader.ReadBoolean();
                                            /*);*/
                                        }
                                        if (binaryReader.ReadBoolean())
                                        {
                                            Main.tile.At(j, k).SetWall(binaryReader.ReadByte());
                                            //											if (Main.tile.At(j, k).Wall == 7)
                                            //												Main.tile.At(j, k).SetWall (17);
                                        }
                                        if (binaryReader.ReadBoolean())
                                        {
                                            Main.tile.At(j, k).SetLiquid(binaryReader.ReadByte());
                                            Main.tile.At(j, k).SetLava(binaryReader.ReadBoolean());
                                        }
                                        if (Terraria_Release >= 33)
                                        {
                                            Main.tile.At(j, k).SetWire(binaryReader.ReadBoolean());
                                        }
                                        if (Terraria_Release >= 25)
                                        {
                                            int num3 = (int)binaryReader.ReadInt16();
                                            if (num3 > 0)
                                            {
                                                for (int l = k + 1; l < k + num3 + 1; l++)
                                                {
                                                    Main.tile.At(j, l).SetActive(Main.tile.At(j, k).Active);
                                                    Main.tile.At(j, l).SetType(Main.tile.At(j, k).Type);
                                                    Main.tile.At(j, l).SetWall(Main.tile.At(j, k).Wall);
                                                    Main.tile.At(j, l).SetFrameX(Main.tile.At(j, k).FrameX);
                                                    Main.tile.At(j, l).SetFrameY(Main.tile.At(j, k).FrameY);
                                                    Main.tile.At(j, l).SetLiquid(Main.tile.At(j, k).Liquid);
                                                    Main.tile.At(j, l).SetLava(Main.tile.At(j, k).Lava);
                                                    Main.tile.At(j, l).SetWire(Main.tile.At(j, k).Wire);
                                                }
                                                k += num3;
                                            }
                                        }
                                    }
                                }
                            }

                            for (int l = 0; l < Main.MAX_CHESTS; l++)
                            {
                                if (binaryReader.ReadBoolean())
                                {
                                    var x = binaryReader.ReadInt32();
                                    var y = binaryReader.ReadInt32();
                                    Main.chest[l] = Chest.InitializeChest(x, y);
                                    for (int m = 0; m < Chest.MAX_ITEMS; m++)
                                    {
                                        //Main.chest[l].contents[m] = new Item();
                                        int stack = binaryReader.ReadByte();
                                        if (stack > 0)
                                        {
                                            if (Terraria_Release >= 38)
                                            {
                                                Main.chest[l].contents[m]       = Item.netDefaults(binaryReader.ReadInt32());
                                                Main.chest[l].contents[m].Stack = stack;
                                            }
                                            else
                                            {
                                                string defaults = Item.VersionName(binaryReader.ReadString(), Terraria_Release);
                                                Main.chest[l].contents[m] = Registries.Item.Create(defaults, stack);
                                            }

                                            if (Terraria_Release >= 36)
                                            {
                                                Main.chest[l].contents[m].SetPrefix((int)binaryReader.ReadByte());
                                            }
                                        }
                                    }
                                }
                            }
                            for (int n = 0; n < Main.MAX_SIGNS; n++)
                            {
                                if (binaryReader.ReadBoolean())
                                {
                                    string text = binaryReader.ReadString();
                                    int    x    = binaryReader.ReadInt32();
                                    int    y    = binaryReader.ReadInt32();
                                    if (Main.tile.At(x, y).Active&& (Main.tile.At(x, y).Type == 55 || Main.tile.At(x, y).Type == 85))
                                    {
                                        Main.sign[n]      = new Sign();
                                        Main.sign[n].x    = x;
                                        Main.sign[n].y    = y;
                                        Main.sign[n].text = text;
                                    }
                                }
                            }
                            bool flag = binaryReader.ReadBoolean();
                            int  num5 = 0;
                            while (flag)
                            {
                                string NPCName = binaryReader.ReadString();
                                Main.npcs[num5]            = Registries.NPC.Create(NPCName);
                                Main.npcs[num5].Position.X = binaryReader.ReadSingle();
                                Main.npcs[num5].Position.Y = binaryReader.ReadSingle();
                                Main.npcs[num5].homeless   = binaryReader.ReadBoolean();
                                Main.npcs[num5].homeTileX  = binaryReader.ReadInt32();
                                Main.npcs[num5].homeTileY  = binaryReader.ReadInt32();
                                flag = binaryReader.ReadBoolean();
                                num5++;
                            }
                            if (Terraria_Release >= 31)
                            {
                                Main.chrName[17]  = binaryReader.ReadString();
                                Main.chrName[18]  = binaryReader.ReadString();
                                Main.chrName[19]  = binaryReader.ReadString();
                                Main.chrName[20]  = binaryReader.ReadString();
                                Main.chrName[22]  = binaryReader.ReadString();
                                Main.chrName[54]  = binaryReader.ReadString();
                                Main.chrName[38]  = binaryReader.ReadString();
                                Main.chrName[107] = binaryReader.ReadString();
                                Main.chrName[108] = binaryReader.ReadString();
                                if (Terraria_Release >= 35)
                                {
                                    Main.chrName[124] = binaryReader.ReadString();
                                }
                            }
                            if (Terraria_Release >= 7)
                            {
                                bool   flag2     = binaryReader.ReadBoolean();
                                string worldName = binaryReader.ReadString();
                                int    num6      = binaryReader.ReadInt32();
                                if (!flag2 || !(worldName == Main.worldName) || num6 != Main.worldID)
                                {
                                    WorldModify.loadSuccess = false;
                                    WorldModify.loadFailed  = true;
                                    binaryReader.Close();
                                    fileStream.Close();
                                    return;
                                }
                                WorldModify.loadSuccess = true;
                            }
                            else
                            {
                                WorldModify.loadSuccess = true;
                            }
                            binaryReader.Close();
                            fileStream.Close();

                            if (!WorldModify.loadFailed && WorldModify.loadSuccess)
                            {
                                WorldModify.gen = true;
                                using (var prog = new ProgressLogger(Main.maxTilesX, Languages.CheckingTileAlignment))
                                {
                                    for (int num9 = 0; num9 < Main.maxTilesX; num9++)
                                    {
                                        float num10 = (float)num9 / (float)Main.maxTilesX;
                                        //WorldModify.statusText = "Checking tile alignment: " + (int)(num10 * 100f + 1f) + "%";
                                        WorldModify.CountTiles(TileRefs, num9);
                                        prog.Value++;
                                    }
                                }

                                //ProgramLog.Log("Loading NPC Names...");
                                //NPC.SetNames();

                                ProgramLog.Log(Languages.Water_PreparingLiquids);
                                WorldModify.waterLine = Main.maxTilesY;
                                Liquid.QuickWater(TileRefs, sandbox, 2, -1, -1);
                                WorldModify.WaterCheck(TileRefs, sandbox);

                                int num11 = 0;
                                Liquid.quickSettle = true;
                                int   num12 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                                float num13 = 0f;

                                var pres = Liquid.numLiquid;
                                using (var prog = new ProgressLogger(Liquid.maxLiquid, Languages.Generation_SettlingLiquids))
                                {
                                    while (Liquid.numLiquid > 0 && num11 < 100000)
                                    {
                                        num11++;
                                        float num14 = (float)(num12 - (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer)) / (float)num12;
                                        if (Liquid.numLiquid + LiquidBuffer.numLiquidBuffer > num12)
                                        {
                                            num12 = Liquid.numLiquid + LiquidBuffer.numLiquidBuffer;
                                        }
                                        if (num14 > num13)
                                        {
                                            num13 = num14;
                                        }
                                        else
                                        {
                                            num14 = num13;
                                        }

                                        prog.Value = Liquid.numLiquid;

                                        Liquid.UpdateLiquid(TileRefs, sandbox);
                                    }
                                }

                                Liquid.quickSettle = false;

                                ProgramLog.Log(Languages.Water_PerformingWaterCheck);
                                WorldModify.WaterCheck(TileRefs, sandbox);
                                WorldModify.gen = false;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        WorldModify.loadFailed  = true;
                        WorldModify.loadSuccess = false;
                        try
                        {
                            binaryReader.Close();
                            fileStream.Close();
                        }
                        catch { }

                        ProgramLog.Error.Log(
                            String.Format("Error Loading world:\n{0}\nStack Trace: {1}", e, e.StackTrace)
                            );

                        return;
                    }
                }
            }

            if (String.IsNullOrEmpty(Main.worldName))
            {
                Main.worldName = System.IO.Path.GetFileNameWithoutExtension(LoadPath);
            }

            Statics.WorldLoaded = true;

            PluginManager.NotifyWorldLoaded();

            var ctx = new HookContext
            {
            };

            var args = new HookArgs.WorldLoaded
            {
                Height = Main.maxTilesY,
                Width  = Main.maxTilesX,
            };

            HookPoints.WorldLoaded.Invoke(ref ctx, ref args);
        }