private static void EncodeVertex(HeightMapVertex vertex, List <byte> output, short defaultX, short defaultZ) { CommandByte command = new CommandByte(); command.UseDefaultX = (defaultX == vertex.X); command.UseDefaultZ = (defaultZ == vertex.Z); List <byte> bytes = new List <byte>(); if (!command.UseDefaultX) { //Determine x flags if (((ushort)(vertex.X << 8) >> 8) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.X & 0xff)); } else if ((vertex.X & 0x3fc0) == vertex.X && ((ushort)(vertex.X << 2) >> 2) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.X >> 6) & 0xff)); } else if ((vertex.X & 0x1fe0) == vertex.X && ((ushort)(vertex.X << 3) >> 3) == vertex.X) { command.XFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.X >> 5) & 0xff)); } else { command.XFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.X >> 8) & 0xff)); bytes.Add((byte)(vertex.X & 0xff)); } } //Determine y flags if (((ushort)(vertex.Y << 8) >> 8) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.Y & 0xff)); } else if ((vertex.Y & 0x03fc) == vertex.Y && ((ushort)(vertex.Y << 6) >> 6) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.Y >> 2) & 0xff)); } else if ((vertex.Y & 0x01fe) == vertex.Y && ((ushort)(vertex.Y << 7) >> 7) == vertex.Y) { command.YFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.Y >> 1) & 0xff)); } else { command.YFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.Y >> 8) & 0xff)); bytes.Add((byte)(vertex.Y & 0xff)); } if (!command.UseDefaultZ) { //Determine z flags if (((ushort)(vertex.Z << 8) >> 8) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteNoShift; bytes.Add((byte)(vertex.Z & 0xff)); } else if ((vertex.Z & 0x3fc0) == vertex.Z && ((ushort)(vertex.Z << 2) >> 2) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteShift2; bytes.Add((byte)((vertex.Z >> 6) & 0xff)); } else if ((vertex.Z & 0x1fe0) == vertex.Z && ((ushort)(vertex.Z << 3) >> 3) == vertex.Z) { command.ZFlags = CommandByte.ByteReadingType.Read1ByteShift1; bytes.Add((byte)((vertex.Z >> 5) & 0xff)); } else { command.ZFlags = CommandByte.ByteReadingType.Read2Bytes; bytes.Add((byte)((vertex.Z >> 8) & 0xff)); bytes.Add((byte)(vertex.Z & 0xff)); } } bytes.Insert(0, command.GetAsByte()); output.AddRange(bytes); return; }