Ejemplo n.º 1
0
        public static void Load(ref World gameWorld)
        {
            //string fileString = "";

            //using (StreamReader fs = new StreamReader(fn))
            //{
            //    fileString = fs.ReadToEnd();
            //}

            //fileString = Decompress(fileString);

            //string[] fileSplit = fileString.Split('\n');

            //int cl = 0;

            //string line;
            //string[] split;

            //line = fileSplit[0];
            //split = line.Split(',');

            //cl+=2;
            OpenFileDialog sfd = new OpenFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".vxl";
            sfd.Filter = "Voxel Landscape|*.vxl";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK) return;

            byte[] buffer;

            using (FileStream gstr = new FileStream(sfd.FileName, FileMode.Open))
            {
                byte[] lb = new byte[4];
                gstr.Position = gstr.Length - 4;
                gstr.Read(lb, 0,4);
                int msgLength = BitConverter.ToInt32(lb, 0);

                buffer = new byte[msgLength];

                gstr.Position = 0;

                using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress))
                {

                    str.Read(buffer, 0, msgLength);
                }
            }

            int pos = 2;

            var sw = new StreamReader(new MemoryStream(buffer));
            string codename = sw.ReadLine();
            string dispname = sw.ReadLine();
            sw.Close();

            int foundcount = 0;
            while (foundcount < 2)
            {
                pos++;
                if (buffer[pos - 2] == 13 && buffer[pos - 1] == 10) foundcount++;
            }

            int xs = buffer[pos + 2];
            int ys = buffer[pos + 3];
            int zs = buffer[pos + 4];
            gameWorld = new World(50, 50, 1, false);
            gameWorld.X_CHUNKS = xs;
            gameWorld.Y_CHUNKS = ys;
            gameWorld.Z_CHUNKS = 1;

            gameWorld.Type = (MapType)buffer[pos];
            gameWorld.Theme = (Theme)buffer[pos + 1];

            int numSpawns = buffer[pos + 5];

            pos += 6;

            for (int i = 0; i < numSpawns; i++)
            {
                Spawn s = new Spawn();
                s.Position = new Vector3(BitConverter.ToInt32(buffer, pos), BitConverter.ToInt32(buffer, pos + 4), BitConverter.ToInt32(buffer, pos + 8));
                s.Type = (SpawnType)buffer[pos + 12];
                s.Rotation = buffer[pos + 13];
                gameWorld.Spawns.Add(s);
                pos += 14;
            }

            for (int z = 0; z < gameWorld.Z_CHUNKS; z++)
            {
                for (int y = 0; y < gameWorld.Y_CHUNKS; y++)
                {
                    for (int x = 0; x < gameWorld.X_CHUNKS; x++)
                    {
                        Chunk c = gameWorld.Chunks[x, y, z];

                        while (pos < buffer.Length)
                        {
                            if (Convert.ToChar(buffer[pos]) != 'c')
                            {
                                //str.Seek(-1, SeekOrigin.Current);
                                //str.Read(ba, 0, 10);
                                int vx = buffer[pos];
                                int vy = buffer[pos + 1];
                                int vz = buffer[pos + 2];
                                VoxelType type = (VoxelType)buffer[pos + 3];
                                short destruct = buffer[pos + 4];
                                Color top = new Color(buffer[pos + 5], buffer[pos + 6], buffer[pos + 7]);
                                Color side = new Color(buffer[pos + 8], buffer[pos + 9], buffer[pos + 10]);

                                c.SetVoxel(vx, vy, vz, true, destruct, type, top, side);
                                pos += 11;
                            }
                            else
                            {
                                pos++;
                                break;
                            }

                        }

                        //while (cl<fileSplit.Length)
                        //{
                        //    line = fileSplit[cl];
                        //    if (line == "C") break;
                        //    if (line == "") break;

                        //    split = line.Split(',');

                        //    c.SetVoxel(Convert.ToInt16(split[0]), Convert.ToInt16(split[1]), Convert.ToInt16(split[2]), true, (VoxelType)(Convert.ToInt16(split[3])), GetTopColor((VoxelType)(Convert.ToInt16(split[3]))), GetSideColor((VoxelType)(Convert.ToInt16(split[3]))));

                        //    cl++;
                        //}

                        //cl++;
                    }
                }

            }

            //fileSplit = null;
            //fileString = null;

            gameWorld.UpdateWorldMeshes();

            GC.Collect();
        }
Ejemplo n.º 2
0
        public static void Load(ref World gameWorld)
        {
            //string fileString = "";

            //using (StreamReader fs = new StreamReader(fn))
            //{
            //    fileString = fs.ReadToEnd();
            //}

            //fileString = Decompress(fileString);

            //string[] fileSplit = fileString.Split('\n');

            //int cl = 0;

            //string line;
            //string[] split;

            //line = fileSplit[0];
            //split = line.Split(',');

            //cl+=2;
            OpenFileDialog sfd = new OpenFileDialog();

            sfd.AddExtension = true;
            sfd.DefaultExt   = ".vxl";
            sfd.Filter       = "Voxel Landscape|*.vxl";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK)
            {
                return;
            }


            byte[] buffer;

            using (FileStream gstr = new FileStream(sfd.FileName, FileMode.Open))
            {
                byte[] lb = new byte[4];
                gstr.Position = gstr.Length - 4;
                gstr.Read(lb, 0, 4);
                int msgLength = BitConverter.ToInt32(lb, 0);

                buffer = new byte[msgLength];

                gstr.Position = 0;

                using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress))
                {
                    str.Read(buffer, 0, msgLength);
                }
            }

            int pos = 2;

            var    sw       = new StreamReader(new MemoryStream(buffer));
            string codename = sw.ReadLine();
            string dispname = sw.ReadLine();

            sw.Close();

            int foundcount = 0;

            while (foundcount < 2)
            {
                pos++;
                if (buffer[pos - 2] == 13 && buffer[pos - 1] == 10)
                {
                    foundcount++;
                }
            }

            int xs = buffer[pos + 2];
            int ys = buffer[pos + 3];
            int zs = buffer[pos + 4];

            gameWorld          = new World(50, 50, 1, false);
            gameWorld.X_CHUNKS = xs;
            gameWorld.Y_CHUNKS = ys;
            gameWorld.Z_CHUNKS = 1;

            gameWorld.Type  = (MapType)buffer[pos];
            gameWorld.Theme = (Theme)buffer[pos + 1];

            int numSpawns = buffer[pos + 5];

            pos += 6;

            for (int i = 0; i < numSpawns; i++)
            {
                Spawn s = new Spawn();
                s.Position = new Vector3(BitConverter.ToInt32(buffer, pos), BitConverter.ToInt32(buffer, pos + 4), BitConverter.ToInt32(buffer, pos + 8));
                s.Type     = (SpawnType)buffer[pos + 12];
                s.Rotation = buffer[pos + 13];
                gameWorld.Spawns.Add(s);
                pos += 14;
            }

            for (int z = 0; z < gameWorld.Z_CHUNKS; z++)
            {
                for (int y = 0; y < gameWorld.Y_CHUNKS; y++)
                {
                    for (int x = 0; x < gameWorld.X_CHUNKS; x++)
                    {
                        Chunk c = gameWorld.Chunks[x, y, z];

                        while (pos < buffer.Length)
                        {
                            if (Convert.ToChar(buffer[pos]) != 'c')
                            {
                                //str.Seek(-1, SeekOrigin.Current);
                                //str.Read(ba, 0, 10);
                                int       vx       = buffer[pos];
                                int       vy       = buffer[pos + 1];
                                int       vz       = buffer[pos + 2];
                                VoxelType type     = (VoxelType)buffer[pos + 3];
                                short     destruct = buffer[pos + 4];
                                Color     top      = new Color(buffer[pos + 5], buffer[pos + 6], buffer[pos + 7]);
                                Color     side     = new Color(buffer[pos + 8], buffer[pos + 9], buffer[pos + 10]);

                                c.SetVoxel(vx, vy, vz, true, destruct, type, top, side);
                                pos += 11;
                            }
                            else
                            {
                                pos++;
                                break;
                            }
                        }

                        //while (cl<fileSplit.Length)
                        //{
                        //    line = fileSplit[cl];
                        //    if (line == "C") break;
                        //    if (line == "") break;

                        //    split = line.Split(',');

                        //    c.SetVoxel(Convert.ToInt16(split[0]), Convert.ToInt16(split[1]), Convert.ToInt16(split[2]), true, (VoxelType)(Convert.ToInt16(split[3])), GetTopColor((VoxelType)(Convert.ToInt16(split[3]))), GetSideColor((VoxelType)(Convert.ToInt16(split[3]))));

                        //    cl++;
                        //}

                        //cl++;
                    }
                }
            }

            //fileSplit = null;
            //fileString = null;

            gameWorld.UpdateWorldMeshes();

            GC.Collect();
        }