public RailState CreateState(int factoryType) { RailState state = statePools[factoryType].Allocate(); state.FactoryType = factoryType; return(state); }
public void Initialize( Tick tick, EntityId entityId, RailState state, bool isFrozen) { this.tick = tick; this.entityId = entityId; this.state = state; this.IsFrozen = isFrozen; }
public static void EncodeDelta( IRailStateConstruction stateCreator, RailBitBuffer buffer, RailStateDelta delta) { // Write: [EntityId] buffer.WriteEntityId(delta.EntityId); // Write: [IsFrozen] buffer.WriteBool(delta.IsFrozen); if (delta.IsFrozen == false) { // Write: [FactoryType] RailState state = delta.State; buffer.WriteInt(stateCreator.EntityTypeCompressor, state.FactoryType); // Write: [IsRemoved] buffer.WriteBool(delta.RemovedTick.IsValid); if (delta.RemovedTick.IsValid) // Write: [RemovedTick] { buffer.WriteTick(delta.RemovedTick); } // Write: [HasControllerData] buffer.WriteBool(state.HasControllerData); // Write: [HasImmutableData] buffer.WriteBool(state.HasImmutableData); if (state.HasImmutableData) // Write: [Immutable Data] { state.DataSerializer.EncodeImmutableData(buffer); } // Write: [Flags] buffer.Write(state.DataSerializer.FlagBits, state.Flags); // Write: [Mutable Data] state.DataSerializer.EncodeMutableData(buffer, state.Flags); if (state.HasControllerData) { // Write: [Controller Data] state.DataSerializer.EncodeControllerData(buffer); // Write: [Command Ack] buffer.WriteTick(delta.CommandAck); } } }
public void Overwrite(IRailStateConstruction stateCreator, Tick tick, RailState state) { RailDebug.Assert(tick.IsValid); this.tick = tick; if (this.state == null) { this.state = state.Clone(stateCreator); } else { this.state.OverwriteFrom(state); } }
public void Initialize( Tick tick, EntityId entityId, RailState state, Tick removedTick, Tick commandAck, bool isFrozen) { Tick = tick; EntityId = entityId; this.state = state; RemovedTick = removedTick; CommandAck = commandAck; IsFrozen = isFrozen; }
public void Overwrite( Tick tick, RailState state) { RailDebug.Assert(tick.IsValid); this.tick = tick; if (this.state == null) { this.state = state.Clone(); } else { this.state.OverwriteFrom(state); } }
/// <summary> /// Creates a record of the current state, taking the latest record (if /// any) into account. If a latest state is given, this function will /// return null if there is no change between the current and latest. /// </summary> public static RailStateRecord Create( IRailStateConstruction stateCreator, Tick tick, RailState current, RailStateRecord latestRecord = null) { if (latestRecord != null) { RailState latest = latestRecord.State; bool shouldReturn = current.DataSerializer.CompareMutableData(latest.DataSerializer) > 0 || current.DataSerializer.IsControllerDataEqual(latest.DataSerializer) == false; if (shouldReturn == false) { return(null); } } RailStateRecord record = stateCreator.CreateRecord(); record.Overwrite(stateCreator, tick, current); return(record); }
public RailStateRecord() { state = null; tick = Tick.INVALID; }
private static Vector2 GetCoordinates(RailState state) { EntityState demoState = (EntityState)state; return(new Vector2(demoState.X, demoState.Y)); }
public static RailStateDelta DecodeDelta( IRailStateConstruction stateCreator, RailBitBuffer buffer, Tick packetTick) { RailStateDelta delta = stateCreator.CreateDelta(); RailState state = null; Tick commandAck = Tick.INVALID; Tick removedTick = Tick.INVALID; // Read: [EntityId] EntityId entityId = buffer.ReadEntityId(); // Read: [IsFrozen] bool isFrozen = buffer.ReadBool(); if (isFrozen == false) { // Read: [FactoryType] int factoryType = buffer.ReadInt(stateCreator.EntityTypeCompressor); state = stateCreator.CreateState(factoryType); // Read: [IsRemoved] bool isRemoved = buffer.ReadBool(); if (isRemoved) // Read: [RemovedTick] { removedTick = buffer.ReadTick(); } // Read: [HasControllerData] state.HasControllerData = buffer.ReadBool(); // Read: [HasImmutableData] state.HasImmutableData = buffer.ReadBool(); if (state.HasImmutableData) // Read: [Immutable Data] { state.DataSerializer.DecodeImmutableData(buffer); } // Read: [Flags] state.Flags = buffer.Read(state.DataSerializer.FlagBits); // Read: [Mutable Data] state.DataSerializer.DecodeMutableData(buffer, state.Flags); if (state.HasControllerData) { // Read: [Controller Data] state.DataSerializer.DecodeControllerData(buffer); // Read: [Command Ack] commandAck = buffer.ReadTick(); } } delta.Initialize(packetTick, entityId, state, removedTick, commandAck, isFrozen); return(delta); }
public static RailStateDelta Create( IRailStateConstruction stateCreator, EntityId entityId, RailState current, IEnumerable <RailStateRecord> basisStates, bool includeControllerData, bool includeImmutableData, Tick commandAck, Tick removedTick, bool forceAllMutable) { bool shouldReturn = forceAllMutable || includeControllerData && current.HasControllerData || includeImmutableData || removedTick.IsValid; // We don't know what the client has and hasn't received from us since // the acked state. As a result, we'll build diff flags across all // states sent *between* the latest and current. This accounts for // situations where a value changes and then quickly changes back, // while appearing as no change on just the current-latest diff. uint flags = 0; if (forceAllMutable == false && basisStates != null) { foreach (RailStateRecord record in basisStates) { flags |= current.DataSerializer.CompareMutableData(record.State.DataSerializer); } } else { flags = FLAGS_ALL; } if (flags == FLAGS_NONE && !shouldReturn) { return(null); } RailState deltaState = stateCreator.CreateState(current.FactoryType); deltaState.HasImmutableData = includeImmutableData; if (includeImmutableData) { deltaState.DataSerializer.ApplyImmutableFrom(current.DataSerializer); } deltaState.Flags = flags; deltaState.DataSerializer.ApplyMutableFrom(current.DataSerializer, deltaState.Flags); deltaState.HasControllerData = includeControllerData; if (includeControllerData) { deltaState.DataSerializer.ApplyControllerFrom(current.DataSerializer); } // We don't need to include a tick when sending -- it's in the packet RailStateDelta delta = stateCreator.CreateDelta(); delta.Initialize(Tick.INVALID, entityId, deltaState, removedTick, commandAck, false); return(delta); }
public RailStateRecord() { this.state = null; this.tick = Tick.INVALID; }