void Directory::GetDataDArray ( const char *dataName, DArray<int> *darray ) const
{
    AppAssert( dataName );
    AppAssert( darray );

    //
    // First try to find a directory representing this DArray

    Directory *dir = GetDirectory( dataName );
    if ( !dir )
    {
        AppReleaseAssert(false, "Failed to find DArray named %s", dataName );
        return;
    }

    //
    // Get info about the DArray

    char *tID = dir->GetDataString("tID");
    int size = dir->GetDataInt("Size");
    if ( strcmp(tID, "DArray<int>") != 0 || size == DIRECTORY_SAFEINT)
    {
        AppDebugOut("Found object %s but it doesn't appear to be a DArray\n" );
    }

    //
    // Iterate through it, loading data
    
    darray->SetSize( size );
    for ( int i = 0; i < size; ++i )
    {
        char indexName[16];
        sprintf ( indexName, "[i %d]", i );
        Directory *thisIndex = dir->GetDirectory( indexName );
        if ( !thisIndex )
        {
            AppDebugOut("Error loading DArray %s : index %d is missing\n", dataName, i );
        }
        else
        {
            bool used = thisIndex->GetDataBool("[Used]");
            
            if ( !used )
            {
                if ( darray->ValidIndex(i) )
                    darray->RemoveData(i);
            }
            else
            {
                int theData = thisIndex->GetDataInt("[Data]");
                darray->PutData( theData, i );
            }
        }
    }
}
void Directory::GetDataLList ( const char *dataName, LList<Directory *> *llist ) const
{
    AppAssert( dataName );
    AppAssert( llist );

    //
    // First try to find a directory representing this LList

    Directory *dir = GetDirectory( dataName );
    if ( !dir )
    {
        AppDebugOut("Failed to find LList named %s\n", dataName);
        return;
    }

    //
    // Get info about the LList

    char *tID = dir->GetDataString("tID");
    int size = dir->GetDataInt("Size");
    if ( strcmp(tID, "LList<Directory *>") != 0 || size == DIRECTORY_SAFEINT)
    {
        AppDebugOut("Found object %s but it doesn't appear to be a LList\n");
    }

    //
    // Iterate through it, loading data
    
    for ( int i = 0; i < size; ++i )
    {
        char indexName[16];
        sprintf ( indexName, "[i %d]", i );
        Directory *thisIndex = dir->GetDirectory( indexName );
        if ( !thisIndex )
        {
            AppDebugOut("Error loading LList %s : index %d is missing\n", dataName, i);
        }
        else
        {
            if( thisIndex->m_subDirectories.ValidIndex(0) )
            {
                Directory *theDir = thisIndex->m_subDirectories[0];
                llist->PutData( theDir );
            }
            else
            {
                AppDebugOut("Error loading LList %s : could not find directory in index %d\n", dataName, i);
            }
        }
    }
}
void Directory::GetDataLList( const char *dataName, LList<int> *llist ) const
{
    AppAssert( dataName );
    AppAssert( llist );

    //
    // First try to find a directory representing this LList

    Directory *dir = GetDirectory( dataName );
    if ( !dir )
    {
        AppDebugOut("Failed to find LList %s\n", dataName);
        return;
    }

    //
    // Get info about the LList

    char *tID = dir->GetDataString("tID");
    int size = dir->GetDataInt("Size");
    if ( strcmp(tID, "LList<int>") != 0 || size == DIRECTORY_SAFEINT)
    {
        AppDebugOut("Found object %s but it doesn't appear to be a LList\n");
    }

    //
    // Iterate through it, loading data
    
    for ( int i = 0; i < size; ++i )
    {
        char indexName[16];
        sprintf ( indexName, "[i %d]", i );
        int theData = dir->GetDataInt("indexName");
        llist->PutDataAtEnd( theData );
    }
}
예제 #4
0
파일: defcon.cpp 프로젝트: cahocachi/DEFCON
void DefconMain()
{
    g_app = new App();
    g_app->MinimalInit();

	g_app->FinishInit();

    double nextServerAdvanceTime = GetHighResTime();
    double serverAdvanceStartTime = -1;
    double lastRenderTime = GetHighResTime();

    bool serverRunning = false;

#ifdef TESTBED
    FILE *file = fopen("testbed-results.txt", "w" );
    if( file ) fclose( file );
#endif

    //
    // Main loops

	while(true)
    {
        //
        // Get the time
        double timeNow = GetHighResTime();

        //
        // Advance the server
        if( g_app->GetServer() && g_app->GetServer()->IsRunning() )
        {
            if( !serverRunning )
            {
                nextServerAdvanceTime = GetHighResTime();
                serverAdvanceStartTime = -1;
                serverRunning = true;
            }


#ifdef TESTBED
            if( g_app->m_gameRunning )
            {
                if( g_app->GetServer()->TestBedReadyToContinue() ||
                    timeNow > nextServerAdvanceTime )
                {
                    g_app->GetServer()->Advance();
                    nextServerAdvanceTime = GetHighResTime() + 0.3f;
                }
            }
            else
            {
                if( timeNow > nextServerAdvanceTime )            
                {
                    g_app->GetServer()->Advance();
                    nextServerAdvanceTime = GetHighResTime() + 0.5f;
                }
            }            
#else
            if( timeNow > nextServerAdvanceTime )            
            {
                g_app->GetServer()->Advance();
                float timeToAdd = SERVER_ADVANCE_PERIOD.DoubleValue();
                if( !g_app->m_gameRunning ) timeToAdd *= 5.0f;
                nextServerAdvanceTime += timeToAdd;
                if (timeNow > nextServerAdvanceTime)
                {
                    nextServerAdvanceTime = timeNow + timeToAdd;
                }
            }        
#endif
        }        

        if( g_app->GetClientToServer() )        
        {            
            START_PROFILE("Client Main Loop");

			g_app->GetClientToServer()->Advance();

#ifdef TESTBED
            if( g_app->m_gameRunning &&
                g_app->GetGame()->m_winner != -1 )
            {
                RestartTestBed(1, "Game Over");
            }

            if( g_app->GetClientToServer()->m_outOfSyncClients.Size() )
            {                
                char notes[256];
                sprintf( notes, "SYNC ERROR at %s", g_app->GetWorld()->m_theDate.GetTheDate() );
                RestartTestBed(-1, notes );
            }

            if( g_app->m_gameRunning &&
                g_app->GetWorld()->m_theDate.m_theDate > 10 )
            {
                float estimatedLatency = g_app->GetClientToServer()->GetEstimatedLatency();
                if( estimatedLatency > 60.0f )
                {
                    RestartTestBed(-1, "Connection lost" );
                }

                if( g_app->GetServer() &&
                    g_app->GetServer()->m_clients.NumUsed() < g_app->GetGame()->GetOptionValue("MaxTeams") )
                {
                    RestartTestBed(-1, "Client lost connection" );
                }
            }
#endif


            //
            // Read latest update from Server
            Directory *letter = g_app->GetClientToServer()->GetNextLetter();

            if( letter )
            {
                if( !strcmp(letter->m_name, NET_DEFCON_MESSAGE) == 0 ||
                    !letter->HasData(NET_DEFCON_SEQID, DIRECTORY_TYPE_INT) )
                {
                    AppDebugOut( "Client received bogus message, discarded (5)\n" );
                    delete letter;
                }
                else
                {
                    int seqId = letter->GetDataInt( NET_DEFCON_SEQID );                
                    if( seqId == -1 )
                    {
                        // This letter is just for us
                        ProcessServerLetters( letter );
                        delete letter;
                    }
                    else
                    {
                        AppReleaseAssert( seqId == g_lastProcessedSequenceId + 1, "Networking broken!" );

                        bool handled = ProcessServerLetters(letter);
                        if(!handled)
                        {
                            g_app->GetClientToServer()->ProcessServerUpdates( letter );
                        }
                
                        if( g_app->m_gameRunning )
                        {
                            g_app->GetWorld()->Update();
                        }
                        else
                        {
                            HandleGameStart();
                        }

                        g_lastServerAdvance = (float)seqId * SERVER_ADVANCE_PERIOD.DoubleValue() + g_startTime;
                        g_lastProcessedSequenceId = seqId;       

                        delete letter;            
                               
                        if( seqId == 0 && g_app->GetClientToServer()->m_resynchronising > 0.0f )
                        {
                            AppDebugOut( "Client successfully began Resynchronising\n" );
                            g_app->GetClientToServer()->m_resynchronising = -1.0f;
                        }

                        unsigned char sync = GenerateSyncValue();
                        SyncRandLog( "Sync %d = %d", g_lastProcessedSequenceId, sync );
                        g_app->GetClientToServer()->SendSyncronisation( g_lastProcessedSequenceId, sync );
                    }
                }
            }

            END_PROFILE("Client Main Loop");
        }

        //
        // Render

        UpdateAdvanceTime();

        if( TimeToRender(lastRenderTime) )
        {
            lastRenderTime = GetHighResTime();
            g_app->Update();
            g_app->Render();
            g_soundSystem->Advance();
            if( g_profiler ) g_profiler->Advance();
        }
    }
	delete g_app;
	g_app = NULL;
}