Example #1
0
        void IModule <TState> .OnConstruct()
        {
            this.states = new CircularQueue <TState>(this.GetTicksPerState(), this.GetQueueCapacity());
            this.events = PoolDictionary <ulong, SortedList <long, HistoryEvent> > .Spawn(1000);

            PoolSortedList <int, HistoryEvent> .Prewarm(1000, 2);

            this.world.SetStatesHistoryModule(this);
        }
Example #2
0
        public void AddEvent(HistoryEvent historyEvent)
        {
            if (this.HasEvent(historyEvent) == true)
            {
                using (NoStackTrace.All) {
                    UnityEngine.Debug.LogWarning($"Duplicate event: {historyEvent}. Skip.");
                }
                return;
            }

            ++this.statEventsAdded;

            this.ValidatePrewarm();

            if (historyEvent.tick <= Tick.Zero)
            {
                // Tick fix if it is zero
                historyEvent.tick = Tick.One;
            }

            var key = MathUtils.GetKey(historyEvent.order, historyEvent.localOrder);

            if (this.events.TryGetValue(historyEvent.tick, out var list) == true)
            {
                list.Add(key, historyEvent);
            }
            else
            {
                list = PoolSortedList <long, HistoryEvent> .Spawn(StatesHistoryModule <TState> .POOL_HISTORY_CAPACITY);

                list.Add(key, historyEvent);
                this.events.Add(historyEvent.tick, list);
            }

            this.oldestTick = (this.oldestTick == Tick.Invalid || historyEvent.tick < this.oldestTick ? (Tick)historyEvent.tick : this.oldestTick);

            ++this.beginAddEventsCount;

            /*
             * if (this.currentTick >= historyEvent.tick) {
             *
             *  if (this.beginAddEvents == false) {
             *
             *      this.Simulate(historyEvent.tick, this.currentTick);
             *
             *  } else {
             *
             *      if (this.beginAddEventsTick > historyEvent.tick) {
             *
             *          this.beginAddEventsTick = historyEvent.tick;
             *
             *      }
             *
             *  }
             *
             * }*/
        }
Example #3
0
        void IModuleBase.OnConstruct()
        {
            this.statesHistory = new ME.ECS.Network.StatesHistory <TState>(this.world, this.GetQueueCapacity());
            //this.states = new StatesCircularQueue<TState>(this.GetTicksPerState(), this.GetQueueCapacity());
            this.events = PoolDictionary <Tick, ME.ECS.Collections.SortedList <long, HistoryEvent> > .Spawn(StatesHistoryModule <TState> .POOL_EVENTS_CAPACITY);

            this.syncHash = PoolDictionary <Tick, int> .Spawn(StatesHistoryModule <TState> .POOL_SYNCHASH_CAPACITY);

            PoolSortedList <int, HistoryEvent> .Prewarm(StatesHistoryModule <TState> .POOL_HISTORY_SIZE, StatesHistoryModule <TState> .POOL_HISTORY_CAPACITY);

            this.world.SetStatesHistoryModule(this);
        }
        public void Reset()
        {
            this.oldestTick         = Tick.Invalid;
            this.lastSavedStateTick = Tick.Invalid;

            this.syncHash.Clear();
            this.statesHistory.DiscardAll();
            this.statesHistory.Clear();

            foreach (var item in this.events)
            {
                PoolSortedList <long, HistoryEvent> .Recycle(item.Value);
            }
            this.events.Clear();
        }
Example #5
0
        public void AddEvent(HistoryEvent historyEvent)
        {
            ++this.statEventsAdded;

            this.ValidatePrewarm();

            if (historyEvent.tick <= Tick.Zero)
            {
                // Tick fix if it is zero
                historyEvent.tick = Tick.One;
            }

            SortedList <long, HistoryEvent> list;

            if (this.events.TryGetValue(historyEvent.tick, out list) == true)
            {
                list.Add(MathUtils.GetKey(historyEvent.order, historyEvent.localOrder), historyEvent);
            }
            else
            {
                list = PoolSortedList <long, HistoryEvent> .Spawn(StatesHistoryModule <TState> .POOL_HISTORY_CAPACITY);

                list.Add(MathUtils.GetKey(historyEvent.order, historyEvent.localOrder), historyEvent);
                this.events.Add(historyEvent.tick, list);
            }

            this.oldestTick = (this.oldestTick == Tick.Invalid || historyEvent.tick < this.oldestTick ? (Tick)historyEvent.tick : this.oldestTick);

            /*if (this.currentTick >= historyEvent.tick) {
             *
             *  if (this.beginAddEvents == false) {
             *
             *      this.Simulate(historyEvent.tick, this.currentTick);
             *
             *  } else {
             *
             ++this.beginAddEventsCount;
             *      if (this.beginAddEventsTick > historyEvent.tick) {
             *
             *          this.beginAddEventsTick = historyEvent.tick;
             *
             *      }
             *
             *  }
             *
             * }*/
        }
Example #6
0
        void IModuleBase.OnDeconstruct()
        {
            //this.maxTick = Tick.Zero;
            this.prewarmed = false;
            //this.beginAddEventsTick = Tick.Zero;
            this.beginAddEventsCount      = 0;
            this.beginAddEvents           = false;
            this.statEventsAdded          = 0;
            this.statPlayedEvents         = 0;
            this.oldestTick               = Tick.Invalid;
            this.lastSavedStateTick       = Tick.Invalid;
            this.pauseStoreStateSinceTick = Tick.Invalid;

            this.statesHistory.DiscardAll();

            this.world.SetStatesHistoryModule(null);

            foreach (var item in this.events)
            {
                var values = item.Value.Values;
                for (int i = 0, cnt = values.Count; i < cnt; ++i)
                {
                    var val = values[i];
                    if (val.parameters != null)
                    {
                        PoolArray <object> .Recycle(ref val.parameters);
                    }
                }
                item.Value.Clear();
                PoolSortedList <long, HistoryEvent> .Recycle(item.Value);
            }
            PoolDictionary <Tick, ME.ECS.Collections.SortedList <long, HistoryEvent> > .Recycle(ref this.events);

            foreach (var kv in this.syncHashTable)
            {
                PoolDictionary <int, int> .Recycle(kv.Value);
            }
            PoolDictionary <Tick, Dictionary <int, int> > .Recycle(ref this.syncHashTable);

            //this.states.Recycle();
            //this.states = null;
        }
Example #7
0
        public void AddEvent(HistoryEvent historyEvent)
        {
            ++this.statEventsAdded;

            this.ValidatePrewarm();

            if (historyEvent.tick <= 0UL)
            {
                // Tick fix if it is zero
                historyEvent.tick = 1UL;
            }

            SortedList <long, HistoryEvent> list;

            if (this.events.TryGetValue(historyEvent.tick, out list) == true)
            {
                list.Add(MathUtils.GetKey(historyEvent.order, historyEvent.localOrder), historyEvent);
            }
            else
            {
                list = PoolSortedList <long, HistoryEvent> .Spawn(2);

                list.Add(MathUtils.GetKey(historyEvent.order, historyEvent.localOrder), historyEvent);
                this.events.Add(historyEvent.tick, list);
            }

            if (this.currentTick >= historyEvent.tick)
            {
                var tick         = this.currentTick;
                var searchTick   = historyEvent.tick - 1;
                var historyState = this.GetStateBeforeTick(searchTick);
                //var cloneState = this.world.CreateState();
                //cloneState.Initialize(this.world, freeze: true, restore: false);
                if (historyState != null)
                {
                    // State found - simulate from this state to current tick
                    this.world.GetState().CopyFrom(historyState);
                    this.world.GetState().tick = tick;
                    //cloneState.CopyFrom(state);
                    //this.world.SetState(cloneState);
                    if (this.beginAddEvents == false)
                    {
                        this.world.SetPreviousTick(historyState.tick);
                        this.world.Simulate(tick);
                    }
                }
                else
                {
                    // Previous state was not found - need to rewind from initial state
                    var resetState = this.world.GetResetState();
                    this.world.GetState().CopyFrom(resetState);
                    this.world.GetState().tick = tick;
                    //cloneState.CopyFrom(resetState);
                    //this.world.SetState(cloneState);
                    if (this.beginAddEvents == false)
                    {
                        this.world.SetPreviousTick(resetState.tick);
                        this.world.Simulate(tick);
                    }

                    //throw new StateNotFoundException("Tick: " + searchTick);
                }
            }
        }