void BroadcastMovementChange(Position newPos, Position delta) { Position = newPos; bool posChanged = (delta.X != 0) || (delta.Y != 0) || (delta.Z != 0); bool rotChanged = (delta.R != 0) || (delta.L != 0); Packet packet; // create the movement packet if (delta.FitsIntoMoveRotatePacket && positionSyncCounter < PositionSyncInterval) { if (posChanged && rotChanged) { // incremental position + rotation update packet = Packet.MakeMoveRotate(Id, new Position { X = delta.X, Y = delta.Y, Z = delta.Z, R = newPos.R, L = newPos.L }); } else if (posChanged) { // incremental position update packet = Packet.MakeMove(Id, delta); } else if (rotChanged) { // absolute rotation update packet = Packet.MakeRotate(Id, newPos); } else { return; } } else { // full (absolute position + rotation) update packet = Packet.MakeTeleport(Id, newPos); } positionSyncCounter++; if (positionSyncCounter >= PositionSyncInterval) { positionSyncCounter = 0; } Server.Players.Send(this, packet); }
void SendMap() { // write MapBegin //Logger.Log( "Send: MapBegin()" ); writer.Write(OpCode.MapBegin); // grab a compressed copy of the map byte[] blockData; Map map = Server.Map; using (MemoryStream mapStream = new MemoryStream()) { using (GZipStream compressor = new GZipStream(mapStream, CompressionMode.Compress)) { int convertedBlockCount = IPAddress.HostToNetworkOrder(map.Volume); compressor.Write(BitConverter.GetBytes(convertedBlockCount), 0, 4); byte[] rawData = (UsesCustomBlocks ? map.Blocks : map.GetFallbackMap()); compressor.Write(rawData, 0, rawData.Length); } blockData = mapStream.ToArray(); } // Transfer the map copy byte[] buffer = new byte[1024]; int mapBytesSent = 0; while (mapBytesSent < blockData.Length) { int chunkSize = blockData.Length - mapBytesSent; if (chunkSize > 1024) { chunkSize = 1024; } else { // CRC fix for ManicDigger for (int i = 0; i < buffer.Length; i++) { buffer[i] = 0; } } Buffer.BlockCopy(blockData, mapBytesSent, buffer, 0, chunkSize); byte progress = (byte)(100 * mapBytesSent / blockData.Length); // write in chunks of 1024 bytes or less //Logger.Log( "Send: MapChunk({0},{1})", chunkSize, progress ); writer.Write(OpCode.MapChunk); writer.Write((short)chunkSize); writer.Write(buffer, 0, 1024); writer.Write(progress); mapBytesSent += chunkSize; } // write MapEnd writer.Write(OpCode.MapEnd); writer.Write((short)map.Width); writer.Write((short)map.Height); writer.Write((short)map.Length); // write spawn point writer.Write(Packet.MakeAddEntity(255, Name, map.Spawn).Bytes); writer.Write(Packet.MakeTeleport(255, map.Spawn).Bytes); lastValidPosition = map.Spawn; }