Пример #1
0
        public static World LoadLVL(string filename)
        {
            //TODO make loading/saving better.
            //if (WorldLoad != null)
            //	WorldLoad(this);
            World w = new World() { chunkData = new Dictionary<Point, Chunk>(), name = filename };
            Logger.Log("Loading " + w.name + "...");

            /*using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(filename + "/" + filename + ".blocks", FileMode.Open))
                {
                    byte[] comp;
                    ms.SetLength(fs.Length);
                    fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
                    DecompressData(ms.GetBuffer(), out comp);
                    ms.Write(comp, 0, comp.Length);
                }
                byte[] bytes = ms.ToArray();
                long chunkcount = ms.Length / 32776;

                if(!Program.RunningInMono()) //mono doesn't have Parallel.For(long, long, Syatem.Action<long>) implemented
                {
                    Parallel.For(0, chunkcount, i =>
                    {
                        try
                        {
                            int block = (int)i * 32776;
                            int x = BitConverter.ToInt32(bytes, 0 + block);
                            int z = BitConverter.ToInt32(bytes, 4 + block);
                            Chunk c = new Chunk(x, z);
                            Array.Copy(bytes, 8 + block, c.blocks, 0, 32768);
                            c.RecalculateLight();
                            c.SpreadLight();
                            lock (w.chunkData)
                                if (!w.chunkData.ContainsKey(new Point(x, z)))
                                    w.chunkData.Add(new Point(x, z), c);
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex.ToString());
                        }
                    });
                }
                else{
                    for (int i = 0; i < chunkcount; i++)
                    {
                        try
                        {
                            int block = (int)i * 32776;
                            int x = BitConverter.ToInt32(bytes, 0 + block);
                            int z = BitConverter.ToInt32(bytes, 4 + block);
                            Chunk c = new Chunk(x, z);
                            Array.Copy(bytes, 8 + block, c.blocks, 0, 32768);
                            c.RecalculateLight();
                            c.SpreadLight();
                            lock (w.chunkData)
                                if (!w.chunkData.ContainsKey(new Point(x, z)))
                                    w.chunkData.Add(new Point(x, z), c);
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex.ToString());
                        }
                    }
                }
            }*/

            try
            {
                using (StreamReader sw = new StreamReader(filename + "/" + filename + ".ini"))
                {
                    w.seed = long.Parse(sw.ReadLine());
                    w.SpawnX = int.Parse(sw.ReadLine());
                    w.SpawnY = int.Parse(sw.ReadLine());
                    w.SpawnZ = int.Parse(sw.ReadLine());
                    w.ChunkLimit = int.Parse(sw.ReadLine());
                    w.time = long.Parse(sw.ReadLine());
                    w.dimension = sbyte.Parse(sw.ReadLine());
                    w.moonPhase = byte.Parse(sw.ReadLine());
                }
            }
            catch { /*Logger.Log("Error loading world configuration!");*/ }

            w.physics = new Physics(w);
            w.generator = new GenStandard(w, true);
            w.chunkManager = new WorldChunkManager(w);

            int cursorH = 22 + w.name.Length;
            float count = 0, total = (Server.ViewDistance * 2 + 1) * (Server.ViewDistance * 2 + 1);
            Console.SetCursorPosition(cursorH, Console.CursorTop - 1);
            Console.Write("0%");

            object derpLock = new object();
            try
            {
                Parallel.For(((int)w.SpawnX >> 4) - Server.ViewDistance, ((int)w.SpawnX >> 4) + Server.ViewDistance + 1, x =>
                {
                    Parallel.For(((int)w.SpawnZ >> 4) - Server.ViewDistance, ((int)w.SpawnZ >> 4) + Server.ViewDistance + 1, z =>
                    {
                        w.LoadChunk(x, z, false, false);
                        lock (derpLock)
                        {
                            Console.SetCursorPosition(cursorH, Console.CursorTop);
                            count++; Console.Write((int)((count / total) * 100) + "%");
                        }
                    });
                });
            }
            catch (NotImplementedException)
            {
                for (int x = ((int)w.SpawnX >> 4) - Server.ViewDistance; x < ((int)w.SpawnX >> 4) + Server.ViewDistance + 1; x++)
                {
                    for (int z = ((int)w.SpawnZ >> 4) - Server.ViewDistance; z < ((int)w.SpawnZ >> 4) + Server.ViewDistance + 1; z++)
                    {
                        w.LoadChunk(x, z, false, false);
                        Console.SetCursorPosition(cursorH, Console.CursorTop);
                        count++; Console.Write((int)((count / total) * 100) + "%");
                    }
                }
            }

            Console.WriteLine();

            World.worlds.Add(w);
            Logger.Log(filename + " Loaded.");
            Logger.Log("Look distance = " + Server.ViewDistance);

            w.Init();
            w.physics.Start();

            if (World.WorldLoad != null)
                World.WorldLoad(w);

            return w;
        }