void Write(BitStream stream) { // TODO: Make sure sleeping, server controlled entities are not moving locally (or they can be but eventually their position should be corrected) stream.WriteBool(MySession.ControlledEntity != null); if (MySession.ControlledEntity == null) { Vector3D pos = MySpectatorCameraController.Static.Position; stream.WriteDouble(pos.X); stream.WriteDouble(pos.Y); stream.WriteDouble(pos.Z); } else { var controlledEntity = MySession.ControlledEntity.Entity.GetTopMostParent(); // Send controlled entity every other frame to server if (MyMultiplayer.Static.FrameCounter % 2 == 0) { // TODO: Write additional client data, context if (controlledEntity != null && controlledEntity.SyncFlag && ((MySyncEntity)controlledEntity.SyncObject).ResponsibleForUpdate(Sync.Clients.LocalClient)) { stream.WriteInt64(controlledEntity.EntityId); switch (MyGuiScreenTerminal.GetCurrentScreen()) { case MyTerminalPageEnum.Inventory: stream.WriteInt32((int)MyContextKind.Inventory, 2); break; case MyTerminalPageEnum.ControlPanel: stream.WriteInt32((int)MyContextKind.Terminal, 2); break; case MyTerminalPageEnum.Production: stream.WriteInt32((int)MyContextKind.Production, 2); break; default: stream.WriteInt32((int)MyContextKind.None, 2); break; } if (MyGuiScreenTerminal.InteractedEntity != null) { stream.WriteInt64(MyGuiScreenTerminal.InteractedEntity.EntityId); } else { stream.WriteInt64(0); } ((MySyncEntity)controlledEntity.SyncObject).SerializePhysics(stream, null); } } } }
protected override void WriteInternal(BitStream stream, MyEntity controlledEntity) { MyContextKind context = GetContextByPage(MyGuiScreenTerminal.GetCurrentScreen()); stream.WriteInt32((int)context, 2); if (context != MyContextKind.None) { var entityId = MyGuiScreenTerminal.InteractedEntity != null ? MyGuiScreenTerminal.InteractedEntity.EntityId : 0; stream.WriteInt64(entityId); } }
private void WritePlanetSectors(BitStream stream) { stream.WriteInt32(0x42424242); var planets = MyPlanets.GetPlanets(); stream.WriteInt32(planets.Count); foreach (var planet in planets) { stream.WriteInt64(planet.EntityId); foreach (var sector in planet.EnvironmentSectors.Values) { if (sector.HasPhysics || sector.ServerOwned) { stream.WriteInt64(sector.SectorId.Pack64()); } } // don't know how many in advance so I will use -1 termination instead of count. stream.WriteInt64(-1); } }
public bool Serialize(BitStream stream, bool validate) { if (stream.Reading) { SenderUserId = stream.ReadInt64(); NumElements = stream.ReadInt32(); stream.ReadBytes(CompressedVoiceBuffer, 0,NumElements); } else { stream.WriteInt64(SenderUserId); stream.WriteInt32(NumElements); stream.WriteBytes(CompressedVoiceBuffer, 0, NumElements); } return true; }
/// <summary> /// Shared area for SE and ME. So far it writes whether you have a controlled entity or not. In the latter case you get the spectator position /// </summary> /// <param name="stream"></param> /// <param name="validControlledEntity"></param> private void WriteShared(BitStream stream, MyEntity controlledEntity) { stream.WriteBool(controlledEntity != null); if (controlledEntity == null) { Vector3D pos = MySpectatorCameraController.Static.Position; stream.Serialize(ref pos); } else { stream.WriteInt64(controlledEntity.EntityId); } }
protected void WritePlanetSectors(BitStream stream) { stream.WriteInt32(PlanetMagic); var planets = MyPlanets.GetPlanets(); // Planets are not enabled if session component is not loaded. if (planets == null) { stream.WriteInt32(0); return; } stream.WriteInt32(planets.Count); foreach (var planet in planets) { stream.WriteInt64(planet.EntityId); MyPlanetEnvironmentComponent env = planet.Components.Get<MyPlanetEnvironmentComponent>(); var syncLod = env.EnvironmentDefinition.SyncLod; foreach (var provider in env.Providers) { foreach (var sector in provider.LogicalSectors) if (sector.MinLod <= syncLod) { stream.WriteInt64(sector.Id); } } // don't know how many in advance so I will use ~0 termination instead of count. stream.WriteInt64(~0); } }
private InventoryDeltaInformation PrepareSendData(ref InventoryDeltaInformation packetInfo, BitStream stream, int maxBitPosition,out bool needsSplit) { needsSplit = false; int startStreamPosition = stream.BitPosition; InventoryDeltaInformation sentData = new InventoryDeltaInformation(); sentData.HasChanges = false; stream.WriteBool(false); stream.WriteUInt32(packetInfo.MessageId); stream.WriteBool(packetInfo.ChangedItems != null); if (packetInfo.ChangedItems != null) { stream.WriteInt32(packetInfo.ChangedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.ChangedItems = new Dictionary<uint, MyFixedPoint>(); foreach (var item in packetInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); if (stream.BitPosition <= maxBitPosition) { sentData.ChangedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.RemovedItems != null); if (packetInfo.RemovedItems != null) { stream.WriteInt32(packetInfo.RemovedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.RemovedItems = new List<uint>(); foreach (var item in packetInfo.RemovedItems) { stream.WriteUInt32(item); if (stream.BitPosition <= maxBitPosition) { sentData.RemovedItems.Add(item); sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.NewItems != null); if (packetInfo.NewItems != null) { stream.WriteInt32(packetInfo.NewItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>(); foreach (var item in packetInfo.NewItems) { MyPhysicalInventoryItem inventoryItem = item.Value; VRage.Serialization.MySerializer.Write(stream, ref inventoryItem, MyObjectBuilderSerializer.Dynamic); if (stream.BitPosition <= maxBitPosition) { sentData.NewItems[item.Key] = inventoryItem; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.WriteBool(packetInfo.SwappedItems != null); if (packetInfo.SwappedItems != null) { stream.WriteInt32(packetInfo.SwappedItems.Count); if (stream.BitPosition > maxBitPosition) { needsSplit = true; } else { sentData.SwappedItems = new Dictionary<int, int>(); foreach (var item in packetInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); if (stream.BitPosition <= maxBitPosition) { sentData.SwappedItems[item.Key] = item.Value; sentData.HasChanges = true; } else { needsSplit = true; } } } } stream.SetBitPositionWrite(startStreamPosition); return sentData; }
private InventoryDeltaInformation WriteInventory(ref InventoryDeltaInformation packetInfo, BitStream stream, byte packetId, int maxBitPosition, out bool needsSplit) { InventoryDeltaInformation sendPacketInfo = PrepareSendData(ref packetInfo, stream, maxBitPosition, out needsSplit); if (sendPacketInfo.HasChanges == false) { stream.WriteBool(false); return sendPacketInfo; } sendPacketInfo.MessageId = packetInfo.MessageId; stream.WriteBool(true); stream.WriteUInt32(sendPacketInfo.MessageId); stream.WriteBool(sendPacketInfo.ChangedItems != null); if (sendPacketInfo.ChangedItems != null) { stream.WriteInt32(sendPacketInfo.ChangedItems.Count); foreach (var item in sendPacketInfo.ChangedItems) { stream.WriteUInt32(item.Key); stream.WriteInt64(item.Value.RawValue); } } stream.WriteBool(sendPacketInfo.RemovedItems != null); if (sendPacketInfo.RemovedItems != null) { stream.WriteInt32(sendPacketInfo.RemovedItems.Count); foreach (var item in sendPacketInfo.RemovedItems) { stream.WriteUInt32(item); } } stream.WriteBool(sendPacketInfo.NewItems != null); if (sendPacketInfo.NewItems != null) { stream.WriteInt32(sendPacketInfo.NewItems.Count); foreach (var item in sendPacketInfo.NewItems) { stream.WriteInt32(item.Key); MyPhysicalInventoryItem itemTosend = item.Value; VRage.Serialization.MySerializer.Write(stream, ref itemTosend, MyObjectBuilderSerializer.Dynamic); } } stream.WriteBool(sendPacketInfo.SwappedItems != null); if (sendPacketInfo.SwappedItems != null) { stream.WriteInt32(sendPacketInfo.SwappedItems.Count); foreach (var item in sendPacketInfo.SwappedItems) { stream.WriteInt32(item.Key); stream.WriteInt32(item.Value); } } return sendPacketInfo; }
/// <summary> /// Serializes physics and takes into account support (what's entity standing on) /// </summary> private void SerializePhysicsWithSupport(BitStream stream, EndpointId forClient,uint timestamp, byte packetId, int maxBitPosition) { if (stream.Writing) { // TODO: only prototype implementation SetSupport(FindSupportDelegate()); stream.WriteBool(m_supportPhysics != null); if (m_supportPhysics != null) { stream.WriteInt64(m_supportPhysics.Entity.EntityId); var localToParent = Entity.WorldMatrix * MatrixD.Invert(m_supportPhysics.Entity.PositionComp.WorldMatrix); stream.Write(localToParent.Translation); stream.Write(Quaternion.CreateFromForwardUp(localToParent.Forward, localToParent.Up).ToVector4()); bool moving = IsMoving(Entity); stream.WriteBool(moving); SerializeVelocities(stream, Entity, EffectiveSimulationRatio, !IsControlledLocally, moving); } else { base.Serialize(stream, forClient,timestamp, packetId, maxBitPosition); } } else { if (stream.ReadBool()) { MyEntity support; bool apply = MyEntities.TryGetEntityById(stream.ReadInt64(), out support) && !IsControlledLocally; var pos = stream.ReadVector3D(); var orient = Quaternion.FromVector4(stream.ReadVector4()); if (apply) { var old = Entity.PositionComp.WorldMatrix; MatrixD localToParent = MatrixD.CreateFromQuaternion(orient); localToParent.Translation = pos; MatrixD matrix = localToParent * support.WorldMatrix; Entity.PositionComp.SetWorldMatrix(matrix, null, true); SetSupport(MySupportHelper.FindPhysics(support)); var handler = MoveHandler; if (handler != null) { handler(ref old, ref matrix); } } else { SetSupport(null); } bool moving = stream.ReadBool(); SerializeVelocities(stream, Entity, EffectiveSimulationRatio, apply, moving); } else { SetSupport(null); base.Serialize(stream, forClient, timestamp, packetId, maxBitPosition); } } }
private void WritePhysics(BitStream stream, MyEntity controlledEntity) { IMyReplicable controlledReplicable = MyExternalReplicable.FindByObject(controlledEntity); stream.WriteBool(controlledReplicable != null); if (controlledReplicable == null) { return; } IMyStateGroup stateGroup = null; bool useCharacterOnServer = controlledEntity is MyCharacter && MyFakes.ENABLE_CHARACTER_CONTROL_ON_SERVER; bool useGridOnServer = controlledEntity is MyCubeGrid && MyFakes.ENABLE_SHIP_CONTROL_ON_SERVER; MyShipController controller = MySession.Static.ControlledEntity as MyShipController; bool hasWheels = controller != null && controller.HasWheels; long? supportId = null; if (useCharacterOnServer || (useGridOnServer && hasWheels == false)) { MyEntityPositionVerificationStateGroup group = controlledReplicable.FindStateGroup<MyEntityPositionVerificationStateGroup>(); stateGroup = group; supportId = group.GetSupportID(); } else { stateGroup = controlledReplicable.FindStateGroup<MyEntityPhysicsStateGroup>(); } stream.WriteBool(useCharacterOnServer || (useGridOnServer && hasWheels == false)); stream.WriteBool(stateGroup != null ); if (stateGroup == null) { return; } stream.WriteBool(supportId.HasValue); if (supportId.HasValue) { stream.WriteInt64(supportId.Value); } bool isResponsible = MyEntityPhysicsStateGroup.ResponsibleForUpdate(controlledEntity,new EndpointId(Sync.MyId)); stream.WriteBool(isResponsible); if (isResponsible) { stateGroup.Serialize(stream, EndpointId, ClientTimeStamp, 0, 1024*1024); } }