public Chunk(World world, int wx, int wy, int wz, bool createGround) { parentWorld = world; worldX = wx; worldY = wy; worldZ = wz; boundingSphere = new BoundingSphere(new Vector3(worldX * (X_SIZE * Voxel.SIZE), worldY * (Y_SIZE * Voxel.SIZE), worldZ * (Z_SIZE * Voxel.SIZE)) + (new Vector3(X_SIZE * Voxel.SIZE, Y_SIZE * Voxel.SIZE, Z_SIZE * Voxel.SIZE) / 2f), (X_SIZE * Voxel.SIZE)); if (createGround) { for (int y = 0; y < Y_SIZE; y++) for (int x = 0; x < X_SIZE; x++) { for (int z = Chunk.Z_SIZE - 1; z >= Chunk.Z_SIZE - 5; z--) { SetVoxel(x, y, z, true, 0, VoxelType.Ground, new Color(0f, 0.5f + ((float)Helper.Random.NextDouble() * 0.1f), 0f), new Color(0f, 0.3f, 0f)); } } } }
public SaveForm(World world) { World = world; InitializeComponent(); }
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(); }
public static void Save(World gameWorld) { //StringBuilder sb = new StringBuilder(); //using (StringWriter str = new StringWriter(sb)) //{ SaveFileDialog sfd = new SaveFileDialog(); 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[] memBytes; //using (FileStream str = new FileStream(sfd.FileName, FileMode.Create)) using (MemoryStream str = new MemoryStream()) { var sw = new StreamWriter(str); sw.WriteLine(gameWorld.CodeName); sw.WriteLine(gameWorld.DisplayName); sw.Flush(); str.WriteByte((byte)gameWorld.Type); str.WriteByte((byte)gameWorld.Theme); //str.Write(gameWorld.X_CHUNKS + "," + gameWorld.Y_CHUNKS + "," + gameWorld.Z_CHUNKS + "\n"); //using (GZipStream gzstr = new GZipStream(str, CompressionMode.Compress)) //{ str.WriteByte(Convert.ToByte(gameWorld.X_CHUNKS)); str.WriteByte(Convert.ToByte(gameWorld.Y_CHUNKS)); str.WriteByte(Convert.ToByte(gameWorld.Z_CHUNKS)); str.WriteByte(Convert.ToByte(gameWorld.Spawns.Count)); for (int i = 0; i < gameWorld.Spawns.Count; i++) { str.Write(BitConverter.GetBytes((int)gameWorld.Spawns[i].Position.X),0,4); str.Write(BitConverter.GetBytes((int)gameWorld.Spawns[i].Position.Y), 0, 4); str.Write(BitConverter.GetBytes((int)gameWorld.Spawns[i].Position.Z), 0, 4); str.WriteByte(Convert.ToByte(gameWorld.Spawns[i].Type)); str.WriteByte(Convert.ToByte(gameWorld.Spawns[i].Rotation)); } 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++) { //str.Write("C\n"); Chunk c = gameWorld.Chunks[x, y, z]; for (byte vx = 0; vx < Chunk.X_SIZE; vx++) for (byte vy = 0; vy < Chunk.Y_SIZE; vy++) for (byte vz = 0; vz < Chunk.Z_SIZE; vz++) { if (!c.Voxels[vx, vy, vz].Active) continue; //string vox = vx + "," + vy + "," + vz + ","; //vox += ((int)c.Voxels[vx, vy, vz].Type); //str.Write(vox + "\n"); str.WriteByte(vx); str.WriteByte(vy); str.WriteByte(vz); str.WriteByte((byte)c.Voxels[vx, vy, vz].Type); str.WriteByte(Convert.ToByte(c.Voxels[vx, vy, vz].Destructable)); str.WriteByte(c.Voxels[vx, vy, vz].TR); str.WriteByte(c.Voxels[vx, vy, vz].TG); str.WriteByte(c.Voxels[vx, vy, vz].TB); str.WriteByte(c.Voxels[vx, vy, vz].SR); str.WriteByte(c.Voxels[vx, vy, vz].SG); str.WriteByte(c.Voxels[vx, vy, vz].SB); } str.WriteByte(Convert.ToByte('c')); } } } //} str.Flush(); memBytes = str.ToArray(); } using (FileStream str = new FileStream(sfd.FileName, FileMode.Create)) { using (GZipStream gzstr = new GZipStream(str, CompressionMode.Compress)) { gzstr.Write(memBytes, 0, memBytes.Length); gzstr.Flush(); } } //} //using (StreamWriter fs = new StreamWriter(fn)) //{ // fs.Write(Compress(sb.ToString())); // fs.Flush(); //} //sb.Clear(); //sb = null; GC.Collect(); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load<SpriteFont>("hudfont"); gameWorld = new World(50,50,1,true); gameCamera = new Camera(GraphicsDevice, GraphicsDevice.Viewport); cursor = new Cursor(); this.EnsurePrefabs(); List<string> pfs = new List<String>(Directory.EnumerateFiles(Path.Combine(Content.RootDirectory, "prefabs/"))); LoadSave.LoadAnim(ref spawnSprites, Path.Combine(Content.RootDirectory, "spawns.vxs")); foreach (string fn in pfs) { Prefabs.Add(LoadSave.LoadPrefab(fn)); } drawEffect = new BasicEffect(GraphicsDevice) { World = gameCamera.worldMatrix, View = gameCamera.viewMatrix, Projection = gameCamera.projectionMatrix, VertexColorEnabled = true, //LightingEnabled = true // turn on the lighting subsystem. }; cursorEffect = new BasicEffect(GraphicsDevice) { World = gameCamera.worldMatrix, View = gameCamera.viewMatrix, Projection = gameCamera.projectionMatrix, VertexColorEnabled = true }; saveForm = new SaveForm(gameWorld); GC.Collect(); }
public void PerformAction(Vector3 position, World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { // Need to make it so voxels have a type and then we can check if tree so we don't put trees on top of trees yo Vector2 center = new Vector2(position.X, position.Y); switch (Mode) { case CursorMode.LandScape: for (int x = 0; x < 32; x++) for (int y = 0; y < 32; y++) for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if(Voxels[x,y,0].Active) gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - Height, 0, VoxelType.Ground, gameWorld.ThemeTopColor(Theme), gameWorld.ThemeSideColor(Theme)); } break; case CursorMode.Trees: int numTrees = 1 +(Size / 2); for (int i = 0; i < numTrees; i++) { Vector2 pos = Helper.RandomPointInCircle(new Vector2(position.X,position.Y), 0f, Size); //int z = 0; //for (int zz = 0; zz < Chunk.Z_SIZE; zz++) if (gameWorld.GetVoxel((int)Position.X, (int)Position.Y, zz).Active) { z = zz - 1; break; } gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)position.Z); } //for (float a = 0f; a < MathHelper.TwoPi; a += 0.05f) //{ // for (int r = 0; r < Size; r++) // { // Vector3 pos = new Vector3(Helper.PointOnCircle(ref center, r, a), 0f); // gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)Position.Z); // } //} break; case CursorMode.Water: for (int x = 0; x < 32; x++) for (int y = 0; y < 32; y++) for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if(Voxels[x,y,0].Active) gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - (Height - 1), 0, VoxelType.Water, new Color(0.1f, 0.1f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f, new Color(0.3f, 0.3f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f); } break; case CursorMode.Prefab: int startX = (int)position.X - (prefab.X_SIZE/2); int startY = (int)position.Y - (prefab.Y_SIZE/2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { if (prefab.Voxels[x, y, z].Active) { gameWorld.SetVoxel(startX + x, startY + y, startZ + z, true, destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); } } } } break; case CursorMode.Spawn: bool found = false; for (int i = gameWorld.Spawns.Count - 1; i >= 0; i--) { if (gameWorld.Spawns[i].Position == position) { found = true; gameWorld.Spawns.RemoveAt(i); } } if (!found) { gameWorld.Spawns.Add(new Spawn() { Position = position, Rotation = (byte)spawnRot, Type = (SpawnType)selectedSpawn, }); } break; } }
public void PerformAction(World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { PerformAction(Position, gameWorld, prefab, selectedSpawn, spawnRot); if (MirrorMode) PerformAction(new Vector3(((gameWorld.X_SIZE)-Position.X),Position.Y,Position.Z), gameWorld, prefab, selectedSpawn, spawnRot); }
internal void CutPrefabSapce(World gameWorld, PrefabChunk prefab) { int startX = (int)Position.X - (prefab.X_SIZE / 2); int startY = (int)Position.Y - (prefab.Y_SIZE / 2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { gameWorld.SetVoxelActive(startX + x, startY + y, startZ + z, false); // destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); if (MirrorMode) gameWorld.SetVoxelActive(((gameWorld.X_SIZE) - startX-prefab.X_SIZE)+x, startY+y, startZ + z, false); } } } }