public byte[] GetGameState() { return(GenerateGameState.GenerateState(_lowTiles, _midTiles, _highTiles)); }
// Generates a Version 0.1 gamestate public static byte[] GenerateState(Tile[,] low, Tile[,] mid, Tile[,] high) { int w = low.GetLength(0); int h = low.GetLength(1); int players = PlayerManager.PM.PlayerCount; string fragstr = ""; string woodstr = ""; string stonestr = ""; string gemstr = ""; int currTurn = PlayerManager.PM.CurrTurn; string datatype = "binary_compressed"; //create the build fragment strings for (int i = 0; i < PlayerManager.PM.PlayerCount; i++) { woodstr += " " + FragmentManager.FM.CountWood(i); stonestr += " " + FragmentManager.FM.CountStone(i); gemstr += " " + FragmentManager.FM.CountGem(i); } fragstr = GenerateGameState.GetFragList(); //Generate the header string header = "VERSION 0.1\n" + "WIDTH " + w + "\n" + "HEIGHT " + h + "\n" + "PLAYERS " + players + "\n" + "FRAGS " + fragstr + "\n" + "WOOD" + woodstr + "\n" + "STONE" + stonestr + "\n" + "GEMS" + gemstr + "\n" + "NETWORKGAMEID " + "" + "\n" + "CURRENTTURN " + currTurn + "\n" + "DATA " + datatype + "\n"; //create the byte array byte[] b = new byte[w * h * 6 * 3]; for (int y = 0; y < h * 3; y++) { for (int x = 0; x < w; x++) { int index = (y * w + x) * 6; int layer = y / h; Tile[,] t = low; if (layer == 1) { t = mid; } else if (layer == 2) { t = high; } int newY = y % h; int newX = x; b[index + 0] = 0; b[index + 1] = 0; b[index + 2] = 0; b[index + 3] = 0; b[index + 4] = 0; b[index + 5] = 0; if (t[newX, newY] != null) { //BYTE 1 b[index + 0] = (byte)(((byte)(t[newX, newY].ORIG_HEIGHT + 10 + 1)) & HEIGHT_MASK); // original Height //BYTE 2 b[index + 1] = (byte)((byte)(((byte)(t[newX, newY].getHeight() + 10 + 1)) & HEIGHT_MASK) | // current height and richness (byte)(((byte)(t[newX, newY].getRichness() + 1) << 5) & RICHNESS_MASK)); //BYTE 3 & 4 UnitToBytes(out b[index + 2], out b[index + 3], t[newX, newY].Resident); //BYTE 5 SurfaceFragmentToBytes(out b[index + 4], t[newX, newY]); //BYTE 6 b[index + 5] = (byte)t[newX, newY].PeekFragment(); } } } //compress the byte data if (datatype == "binary_compressed") { b = CompressionHelper0_1.Compress(b); } //join the byte arrays together byte[] final = new byte[b.Length + header.Length]; System.Buffer.BlockCopy(b, 0, final, header.Length, b.Length); b = System.Text.Encoding.ASCII.GetBytes(header); System.Buffer.BlockCopy(b, 0, final, 0, b.Length); //print( Application.dataPath ); //File.WriteAllBytes( Application.dataPath + "/newtestmapfromunity.txt" , final ); return(final); }