Ejemplo n.º 1
0
    private void _Tick()
    {
        bool gatheringTurns  = true;
        bool waitingForTurns = false;

        while (gatheringTurns)
        {
            if (!_ParseEventQueue())
            {
                return;
            }

            if (!waitingForTurns)
            {
                _SendCurrentLocalActions(_currentPlayer, _simTickCount);
                ++_simTickCount;
            }

            //_TickNetwork();

            if (_simTickCount >= LATENCY_TICKS)
            {
                int           executeTurn = _simTickCount - LATENCY_TICKS;
                CActionTurn[] playerTurns = new CActionTurn[CWorld.MAX_PLAYERS];

                // Time to execute turn, so query Game Session for all the player turns for turn X.

                // Get replay turns from journal
                _replay.ReplayGetTurns(executeTurn, _actionTurns);

                // TODO: Since we are moving journal 'up a level' don't search entire actionTurns,
                // only search until all players have a valid turn (combine get and check steps).

                // Get all player turns
                for (int i = 0; i < _actionTurns.Count; ++i)
                {
                    if (_actionTurns[i].mTurn == executeTurn)
                    {
                        playerTurns[_actionTurns[i].mPlayerID] = _actionTurns[i];
                    }
                }

                bool turnsMissing = false;

                // If there are turns missing then halt the simulation
                for (int i = 0; i < CWorld.MAX_PLAYERS; ++i)
                {
                    if (_world.mPlayers[i].mHumanInput && playerTurns[i] == null)
                    {
                        turnsMissing = true;
                        break;
                    }
                }

                if (!waitingForTurns && turnsMissing)
                {
                    Debug.LogError("Simulation Wait " + _simTickCount);
                }

                if (!turnsMissing)
                {
                    // At this point we have all required turns for this simulation tick

                    gatheringTurns = false;

                    if (waitingForTurns)
                    {
                        Debug.LogError("Simulation Resumed " + _simTickCount);
                    }

                    int compareHash = -1;

                    // Execute all the turns & remove from recvd turns
                    for (int i = 0; i < CWorld.MAX_PLAYERS; ++i)
                    {
                        if (_world.mPlayers[i].mHumanInput)
                        {
                            if (compareHash == -1)
                            {
                                compareHash = playerTurns[i].mHash;
                            }
                            else
                            {
                                if (compareHash != playerTurns[i].mHash)
                                {
                                    // TODO: Inform player & unity thread we are out of sync!
                                    //CGame.UIManager.DisplayLargeMessage("Out of Sync!");
                                    Debug.LogError("Out Of Sync");
                                    _running = false;
                                    return;
                                }
                            }

                            _actionTurns.Remove(playerTurns[i]);
                            _world.ExecuteTurnActions(playerTurns[i]);
                        }
                    }

                    _replay.ReplayWriteTurns(executeTurn, playerTurns);
                }
            }
        }

        // If we get this far then we can do a game tick.
        //Debug.Log("Sim Tick " + _simTickCount + " " + (_simTickCount - LATENCY_TICKS) + " " + (_GetTimeUS() / 1000) + "ms");
        //Debug.Log("ST: " + ((double)System.Diagnostics.Stopwatch.GetTimestamp() / (double)System.Diagnostics.Stopwatch.Frequency - 198000.0));
        _world.SimTick();
    }