/// <summary> /// Creates a delta between a state and a record. If forceUpdate is set /// to false, this function will return null if there is no change between /// the current and basis. /// </summary> internal static RailStateDelta CreateDelta( EntityId entityId, RailState current, RailStateRecord basisRecord, bool includeControllerData, bool includeImmutableData, Tick commandAck, Tick removedTick) { bool shouldReturn = includeControllerData || includeImmutableData || removedTick.IsValid; uint flags = RailState.FLAGS_ALL; if ((basisRecord != null) && (basisRecord.State != null)) { flags = current.CompareMutableData(basisRecord.State); } if ((flags == 0) && (shouldReturn == false)) { return(null); } RailState deltaState = RailState.Create(current.factoryType); deltaState.Flags = flags; deltaState.ApplyMutableFrom(current, deltaState.Flags); deltaState.HasControllerData = includeControllerData; if (includeControllerData) { deltaState.ApplyControllerFrom(current); } deltaState.HasImmutableData = includeImmutableData; if (includeImmutableData) { deltaState.ApplyImmutableFrom(current); } deltaState.RemovedTick = removedTick; deltaState.CommandAck = commandAck; // We don't need to include a tick when sending -- it's in the packet RailStateDelta delta = RailResource.Instance.CreateDelta(); delta.Initialize(Tick.INVALID, entityId, deltaState, false); return(delta); }
internal static RailStateDelta DecodeDelta( RailBitBuffer buffer, Tick packetTick) { RailStateDelta delta = RailResource.Instance.CreateDelta(); RailState state = null; // Read: [EntityId] EntityId entityId = buffer.ReadEntityId(); // Read: [IsFrozen] bool isFrozen = buffer.ReadBool(); if (isFrozen == false) { // Read: [FactoryType] int factoryType = buffer.ReadInt(RailState.FactoryTypeCompressor); state = RailState.Create(factoryType); // Read: [IsRemoved] bool isRemoved = buffer.ReadBool(); if (isRemoved) { // Read: [DestroyedTick] state.RemovedTick = buffer.ReadTick(); // End Read } else { // Read: [HasControllerData] state.HasControllerData = buffer.ReadBool(); // Read: [HasImmutableData] state.HasImmutableData = buffer.ReadBool(); // Read: [Flags] state.Flags = buffer.Read(state.FlagBits); // Read: [Mutable Data] state.DecodeMutableData(buffer, state.Flags); if (state.HasControllerData) { // Read: [Controller Data] state.DecodeControllerData(buffer); // Read: [Command Ack] state.CommandAck = buffer.ReadTick(); } if (state.HasImmutableData) { // Read: [Immutable Data] state.DecodeImmutableData(buffer); } } } delta.Initialize(packetTick, entityId, state, isFrozen); return(delta); }