예제 #1
0
LList <char *> *ListSubDirectoryNames(const char *_dir)
{
    LList<char *> *result = new LList<char *>();

#ifdef WIN32
    _finddata_t thisfile;
    long fileindex;
	char *dir = ConcatPaths( _dir, "*.*", NULL );
	
	fileindex = _findfirst( dir, &thisfile );

    int exitmeplease = 0;

    while( fileindex != -1 && !exitmeplease ) 
    {
        if( strcmp( thisfile.name, "." ) != 0 &&
            strcmp( thisfile.name, ".." ) != 0 &&
            (thisfile.attrib & _A_SUBDIR) )
        {
            char *newname = strdup( thisfile.name );   
            result->PutData( newname );
        }

        exitmeplease = _findnext( fileindex, &thisfile );
    }
	
	delete[] dir;
#else

    DIR *dir = opendir(_dir);
    if (dir == NULL)
        return result;
    for (struct dirent *entry; (entry = readdir(dir)) != NULL; ) {
        if (entry->d_name[0] == '.')
            continue;
            
        char fullname[strlen(_dir) + strlen(entry->d_name) + 2];
        sprintf(fullname, "%s%s%s", 
            _dir, 
            _dir[0] ? "/" : "",
            entry->d_name);
            
        if (IsDirectory(fullname))
            result->PutData( strdup(entry->d_name) );
    }
    closedir(dir);
    
#endif
    return result;
}
void EclDirtyRectangle ( int x, int y, int w, int h )
{
    DirtyRect *dr = new DirtyRect(x, y, w, h);
    dirtyrects.PutData( dr );

    for ( int i = 0; i < windows.Size(); ++i )
    {
        EclWindow *window = windows.GetData(i);
        if ( EclRectangleOverlap( x, y, w, h, window->m_x, window->m_y, window->m_w, window->m_h ) )
            EclDirtyWindow ( window );
    }
}
예제 #3
0
void Authentication_RequestStatus( char *_key, int _keyId, char *_ip )
{
    //
    // Does the key already exist in our list?

    s_authResultsMutex.Lock();

    AuthenticationResult *result = NULL;

    for( int i = 0; i < s_authResults.Size(); ++i )
    {
        AuthenticationResult *authResult = s_authResults[i];
        if( strcmp(authResult->m_authKey, _key) == 0 )
        {
            result = authResult;
            break;
        }
    }

    //
    // Add the key to our list if required

    if( !result )
    {
        result = new AuthenticationResult();
        strcpy( result->m_authKey, _key );
        result->m_keyId = _keyId;
        if( _ip ) strcpy( result->m_ip, _ip );
        else      strcpy( result->m_ip, "unknown" );
        
        s_authResults.PutData( result );
    }
    
    s_authResultsMutex.Unlock();    


    //
    // Start the authorisation thread if required

    if( !s_authThreadRunning )
    {
        s_authThreadRunning = true;
        NetStartThread( AuthenticationThread );
    }
}
예제 #4
0
void Authentication_SetStatus( char *_key, int _keyId, int _status )
{
    bool authChanged = false;

    s_authResultsMutex.Lock();

    AuthenticationResult *result = NULL;

    for( int i = 0; i < s_authResults.Size(); ++i )
    {
        AuthenticationResult *authResult = s_authResults[i];
        if( strcmp(authResult->m_authKey, _key) == 0 )
        {
            result = authResult;
            break;
        }
    }

    if( !result )
    {
        result = new AuthenticationResult();
        strcpy( result->m_authKey, _key );
        s_authResults.PutData( result );
    }

    if( result->m_authResult != _status )
    {
        char *authString = Authentication_GetStatusString(_status);
        AppDebugOut( "Received Authentication : %s : (keyID %d) : %s\n", _key, _keyId, authString );
    }

    result->m_authResult = _status;
    result->m_keyId = _keyId;

    s_authResultsMutex.Unlock();
}
예제 #5
0
void AlliancesWindow::Update()
{   
    //
    // Build a list of all teams;

    LList<Team *> teams;
    for( int i = 0; i < g_app->GetWorld()->m_teams.Size(); ++i )
    {
        Team *team = g_app->GetWorld()->m_teams[i];
        teams.PutData( team );
    }


    //
    // Now put the other teams in, in alliance order

    int currentIndex = 0;

    while( teams.Size() > 0 )
    {
        Team *baseTeam = teams[0];
        m_teamOrder[currentIndex] = baseTeam->m_teamId;
        ++currentIndex;
        teams.RemoveData(0);

        for( int i = 0; i < teams.Size(); ++i )
        {
            Team *possibleAlly = teams[i];
            if( possibleAlly->m_allianceId == baseTeam->m_allianceId )
            {
                m_teamOrder[currentIndex] = possibleAlly->m_teamId;
                ++currentIndex;
                teams.RemoveData(i);
                --i;
            }
        }
    }

    //
    // Are there any votes we can see?
    
    for( int i = 0; i < MAX_TEAMS; ++i )
    {
        m_votes[i] = -1;
    }    

    currentIndex = 0;
    
    for( int i = 0; i < g_app->GetWorld()->m_votingSystem.m_votes.Size(); ++i )
    {
        if( g_app->GetWorld()->m_votingSystem.m_votes.ValidIndex(i) )
        {
            Vote *vote = g_app->GetWorld()->m_votingSystem.m_votes[i];
            if( vote->m_result == Vote::VoteUnknown &&
                vote->CanSeeVote( g_app->GetWorld()->m_myTeamId ) )
            {
                m_votes[currentIndex] = i;
                ++currentIndex;
            }
        }
    }


    //
    // Make sure we are the right size
   
    m_h = 120 + g_app->GetWorld()->m_teams.Size() * 42;
    m_h += currentIndex * 45;    

    if( currentIndex > 0 ) m_h += 10;
    
    m_h = max(m_h, 300 );

    GetButton("Close")->m_y = m_h - 25;
}
예제 #6
0
// Finds all the filenames in the specified directory that match the specified
// filter. Directory should be like "data/textures" or "data/textures/".
// Filter can be NULL or "" or "*.bmp" or "map_*" or "map_*.txt"
// Set FullFilename to true if you want results like "data/textures/blah.bmp" 
// or false for "blah.bmp"
LList <char *> *ListDirectory( const char *_dir, const char *_filter, bool _fullFilename )
{
    if(_filter == NULL || _filter[0] == '\0')
    {
        _filter = "*";
    }

    if(_dir == NULL || _dir[0] == '\0')
    {
        _dir = "";
    }

    // Create a DArray for our results
    LList <char *> *result = new LList<char *>();

    // Now add on all files found locally
#ifdef WIN32
    char searchstring [256];
    AppAssert(strlen(_dir) + strlen(_filter) < sizeof(searchstring) - 1);
    sprintf( searchstring, "%s%s", _dir, _filter );

    _finddata_t thisfile;
    long fileindex = _findfirst( searchstring, &thisfile );

    int exitmeplease = 0;

    while( fileindex != -1 && !exitmeplease ) 
    {
        if( strcmp( thisfile.name, "." ) != 0 &&
            strcmp( thisfile.name, ".." ) != 0 &&
            !(thisfile.attrib & _A_SUBDIR) )
        {
            char *newname = NULL;
            if( _fullFilename ) 
            {
                int len = strlen(_dir) + strlen(thisfile.name);
                newname = new char [len + 1];
                sprintf( newname, "%s%s", _dir, thisfile.name );      
            }
            else
            {
                int len = strlen(thisfile.name);
                newname = new char [len + 1];
                sprintf( newname, "%s", thisfile.name );
            }

            result->PutData( newname );
        }

        exitmeplease = _findnext( fileindex, &thisfile );
    }
#else
    DIR *dir = opendir(_dir[0] ? _dir : ".");
    if (dir == NULL)
        return result;
    for (struct dirent *entry; (entry = readdir(dir)) != NULL; ) {
        if (FilterMatch(entry->d_name, _filter)) {
            char fullname[strlen(_dir) + strlen(entry->d_name) + 2];
            sprintf(fullname, "%s%s%s", 
                _dir, 
                _dir[0] ? "/" : "",
                entry->d_name);
            if (!IsDirectory(fullname)) {
                result->PutData( 
                    newStr(_fullFilename ? fullname : entry->d_name));
            }
        }
    }
    closedir(dir);
#endif

    return result;
}
예제 #7
0
파일: nuke.cpp 프로젝트: cahocachi/DEFCON
void Nuke::FindTarget( int team, int targetTeam, int launchedBy, Fixed range, Fixed *longitude, Fixed *latitude, int *objectId )
{
    START_PROFILE("Nuke::FindTarget");
    
    WorldObject *launcher = g_app->GetWorld()->GetWorldObject(launchedBy);
    if( !launcher ) 
    {
        END_PROFILE("Nuke::FindTarget");
        return;
    }

    LList<int> validTargets;
        
    for( int i = 0; i < g_app->GetWorld()->m_objects.Size(); ++i )
    {
        if( g_app->GetWorld()->m_objects.ValidIndex(i) )
        {
            WorldObject *obj = g_app->GetWorld()->m_objects[i];
            if( obj->m_teamId == targetTeam &&
                obj->m_seen[team] &&
                !obj->IsMovingObject() )
            {
                Fixed distanceSqd = g_app->GetWorld()->GetDistanceSqd( launcher->m_longitude, launcher->m_latitude, obj->m_longitude, obj->m_latitude);
                if( distanceSqd <= range * range )
                {                    
                    int numTargetedNukes = CountTargetedNukes( team, obj->m_longitude, obj->m_latitude );
                    
                    if( (obj->m_type == WorldObject::TypeRadarStation && numTargetedNukes < 2 ) ||
                        (obj->m_type != WorldObject::TypeRadarStation && numTargetedNukes < 4 ) )
                    {
                        validTargets.PutData(obj->m_objectId);
                    }
                }
            }
        }
    }

    if( validTargets.Size() > 0 )
    {
        int targetId = syncrand() % validTargets.Size();
        int objIndex = validTargets[ targetId ];
        WorldObject *obj = g_app->GetWorld()->GetWorldObject(objIndex);
        if( obj )
        {
            *longitude = obj->m_longitude;
            *latitude = obj->m_latitude;
            *objectId = obj->m_objectId;
            END_PROFILE("Nuke::FindTarget");
            return;
        }
    }

    Team *friendlyTeam = g_app->GetWorld()->GetTeam( team );

    int maxPop = 500000;        // Don't bother hitting cities with less than 0.5M survivors

    for( int i = 0; i < g_app->GetWorld()->m_cities.Size(); ++i )
    {
        if( g_app->GetWorld()->m_cities.ValidIndex(i) )
        {
            City *city = g_app->GetWorld()->m_cities[i];

            if( !g_app->GetWorld()->IsFriend( city->m_teamId, team) && 
				g_app->GetWorld()->GetDistanceSqd( city->m_longitude, city->m_latitude, launcher->m_longitude, launcher->m_latitude) <= range * range)               
            {
                int numTargetedNukes = CountTargetedNukes(team, city->m_longitude, city->m_latitude);
                int estimatedPop = City::GetEstimatedPopulation( team, i, numTargetedNukes );
                if( estimatedPop > maxPop )
                {
                    maxPop = estimatedPop;
                    *longitude = city->m_longitude;
                    *latitude = city->m_latitude;
                    *objectId = -1;
                }
            }
        }
    }
    
    END_PROFILE("Nuke::FindTarget");
}
예제 #8
0
void SoundSystem::Advance()
{
    if( !m_channels )
	{
		return;
	}

    float timeNow = GetHighResTime();
    if( timeNow >= m_timeSync )
    {
        m_timeSync = timeNow + SOUNDSYSTEM_UPDATEPERIOD;
        
        START_PROFILE("SoundSystem");        
		
#ifndef TARGET_MSVC
		((SoundLibrary2dSDL *)g_soundLibrary2d)->m_callbackLock.Lock();
#endif
		
        //
        // Resync with blueprints (changed by editor)

        START_PROFILE("Propagate Blueprints" );
        if( m_propagateBlueprints )
        {
            PropagateBlueprints();
        }
        END_PROFILE("Propagate Blueprints" );


        //
		// First pass : Recalculate all Perceived Sound Volumes
		// Throw away sounds that have had their chance
        // Build a list of instanceIDs for sorting

        START_PROFILE("Allocate Sorted Array" );
        static int sortedArraySize = 128;
        static SoundInstanceId *sortedIds = NULL;
        if( m_sounds.NumUsed() > sortedArraySize )
        {
            delete [] sortedIds;
            sortedIds = NULL;
            while( sortedArraySize < m_sounds.NumUsed() )
                sortedArraySize *= 2;
        }
        if( !sortedIds )
        {            
            sortedIds = new SoundInstanceId[ sortedArraySize ];
        }

        int numSortedIds = 0;
        END_PROFILE("Allocate Sorted Array" );

        START_PROFILE("Perceived Volumes" );
        for( int i = 0; i < m_sounds.Size(); ++i )
        {
            if( m_sounds.ValidIndex(i) )
            {
                SoundInstance *instance = m_sounds[i];
                if( !instance->IsPlaying() && !instance->m_loopType ) instance->m_restartAttempts--;
                if( instance->m_restartAttempts < 0 )
                {
                    ShutdownSound( instance );
                }
                else if( instance->m_positionType == SoundInstance::Type3DAttachedToObject &&
                         !instance->GetAttachedObject().IsValid() )                
                {
                    ShutdownSound( instance );
                }
                else
                {
                    instance->CalculatePerceivedVolume();
                    sortedIds[numSortedIds] = instance->m_id;
                    numSortedIds++;
                }
            }
        }
        END_PROFILE("Perceived Volumes" );


        //        
		// Sort sounds into perceived volume order
        // NOTE : There are exactly numSortedId elements in sortedIds.  
        // NOTE : It isn't safe to assume numSortedIds == m_sounds.NumUsed()
        
        START_PROFILE("Sort Samples" );
        qsort( sortedIds, numSortedIds, sizeof(SoundInstanceId), SoundInstanceCompare );
        END_PROFILE("Sort Samples" );


        //
		// Second pass : Recalculate all Sound Priorities starting with the nearest sounds
        // Reduce priorities as more of the same sounds are played

        BTree<float> existingInstances;
        
                
        //                
        // Also look out for the highest priority new sound to swap in

        START_PROFILE("Recalculate Priorities" );

        LList<SoundInstance *> newInstances;
        SoundInstance *newInstance = NULL;
        float highestInstancePriority = 0.0f;

        for( int i = 0; i < numSortedIds; ++i )
        {
            SoundInstanceId id = sortedIds[i];
            SoundInstance *instance = GetSoundInstance( id );
            AppDebugAssert( instance );

            instance->m_calculatedPriority = instance->m_perceivedVolume;

            BTree<float> *existingInstance = existingInstances.LookupTree( instance->m_eventName );
            if( existingInstance )
            {
                instance->m_calculatedPriority *= existingInstance->data;
                existingInstance->data *= 0.75f;
            }
            else
            {
                existingInstances.PutData( instance->m_eventName, 0.75f );
            }

            if( !instance->IsPlaying() )
            {
                if( instance->m_positionType == SoundInstance::TypeMusic )
                {
                    newInstances.PutData( instance );
                }
                else if( instance->m_calculatedPriority > highestInstancePriority )
                {
                    newInstance = instance;
                    highestInstancePriority = instance->m_calculatedPriority;
                }
            }
        }

        if( newInstance )
        {
            newInstances.PutData( newInstance );
        }

        END_PROFILE("Recalculate Priorities" );
        
     
        for( int i = 0; i < newInstances.Size(); ++i )
        {            
            SoundInstance *newInstance = newInstances[i];
            bool isMusic = (newInstance->m_positionType == SoundInstance::TypeMusic);

            // Find worst old sound to get rid of  
            START_PROFILE("Find best Channel" );
            int bestAvailableChannel = FindBestAvailableChannel( isMusic );
            END_PROFILE("Find best Channel" );

			// FindBestAvailableChannel can return -1, so let's not access an invalid index later on.
			if ( bestAvailableChannel < 0 )
				continue;

            START_PROFILE("Stop Old Sound" );
			// Stop the old sound
            SoundInstance *existingInstance = GetSoundInstance( m_channels[bestAvailableChannel] );
            if( existingInstance && !existingInstance->m_loopType )
            {
                ShutdownSound( existingInstance );
            }
            else if( existingInstance )
            {
                existingInstance->StopPlaying();
            }            
            END_PROFILE("Stop Old Sound" );


            START_PROFILE( "Start New Sound" );
			// Start the new sound
            bool success = newInstance->StartPlaying( bestAvailableChannel );
            if( success )
            {
                m_channels[bestAvailableChannel] = newInstance->m_id;
            }
            else
            {
                // This is fairly bad, the sound failed to play
                // Which means it failed to load, or to go into a channel
                ShutdownSound( newInstance );
            }
            END_PROFILE("Start New Sound" );
            
            START_PROFILE("Reset Channel" );
            g_soundLibrary3d->ResetChannel( bestAvailableChannel);
            END_PROFILE("Reset Channel" );
        }


        //
        // Advance all sound channels

        START_PROFILE("Advance All Channels" );
        for( int i = 0; i < m_numChannels; ++i )
        {            
            SoundInstanceId soundId = m_channels[i];            
            SoundInstance *currentSound = GetSoundInstance( soundId );                         
            if( currentSound )
            {
                bool amIDone = currentSound->Advance();
                if( amIDone )
                {                    
			        START_PROFILE("Shutdown Sound" );
                    ShutdownSound( currentSound );
			        END_PROFILE("Shutdown Sound" );
                }    
            }
        }
        END_PROFILE("Advance All Channels" );


		//
        // Update our listener position

        START_PROFILE("UpdateListener" );    

        Vector3<float> camPos, camVel, camUp, camFront;
        bool cameraDefined = m_interface->GetCameraPosition( camPos, camFront, camUp, camVel );

        if( cameraDefined )
        {
            if( g_preferences->GetInt("SoundSwapStereo",0) == 0 )
            {
                camUp *= -1.0f;
            }

            g_soundLibrary3d->SetListenerPosition( camPos, camFront, camUp, camVel );
        }
        
        END_PROFILE("UpdateListener" );    


        //
        // Advance our sound library

        START_PROFILE("SoundLibrary3d Advance" );
        g_soundLibrary3d->Advance();
        END_PROFILE("SoundLibrary3d Advance" );
      
#ifdef SOUNDSYSTEM_VERIFY
        RuntimeVerify();
#endif

#ifndef TARGET_MSVC
		((SoundLibrary2dSDL *)g_soundLibrary2d)->m_callbackLock.Unlock();
#endif

        END_PROFILE("SoundSystem");                    
    }
}
예제 #9
0
void SWInterface::ToggleVersionMenu ( char *program, int x, int y )
{

	if ( program && 
		 !IsVisibleVersionMenu ( program ) ) {

		// Create it
		
		LList <char *> software;				// Bit of a hack - think about this
		LList <float> versions;

		DataBank *db = &(game->GetWorld ()->GetPlayer ()->gateway.databank);

		for ( int di = 0; di < db->GetDataSize (); ++di ) {

			if ( db->GetDataFile (di) &&
				 db->GetDataFile (di)->TYPE == DATATYPE_PROGRAM &&
				 strcmp ( db->GetDataFile (di)->title, program ) == 0 ) {

				software.PutData ( db->GetDataFile (di)->title );
				versions.PutData ( db->GetDataFile (di)->version );

			}

		}

		// Ensures total time = 500ms

		int timesplit = 500;
		if ( software.Size () > 0 )
			timesplit /= software.Size ();

		for ( int si = 0; si < software.Size (); ++si ) {
	
			char caption [128], tooltip [128], name [128];
			UplinkSnprintf ( caption, sizeof ( caption ), "%s v%1.1f", software.GetData (si), versions.GetData (si) );
			UplinkStrncpy ( tooltip, "Runs this software application", sizeof ( tooltip ) );
			UplinkSnprintf ( name, sizeof ( name ), "hud_version %d", si );
			EclRegisterButton ( x, y, 120, 15, caption, tooltip, name );
			EclRegisterButtonCallbacks ( name, SoftwareDraw, SoftwareClick, button_click, button_highlight );
			EclRegisterMovement ( name, x, y - si * 17, si * timesplit );

		}
		
        SgPlaySound ( RsArchiveFileOpen ("sounds/softwaremenu.wav"), "sounds/softwaremenu.wav", false );

	}
	else {

		// Remove it
		// We don't know how many entries there could be, so keep deleting until they run out

		int i = 0;
		char name [128];
		UplinkSnprintf ( name, sizeof ( name ), "hud_version %d", i );

		while ( EclGetButton ( name ) != NULL ) {

			EclRemoveButton ( name );
			++i;
			UplinkSnprintf ( name, sizeof ( name ), "hud_version %d", i );

		}	

		// Force redraw of the button that fathered this menu

		char bname [32];
		UplinkSnprintf ( bname, sizeof ( bname ), "hud_software %d", currentprogrambutton );
		EclDirtyButton ( bname );

		currentprogrambutton = -1;

	}

}
예제 #10
0
void SWInterface::ToggleSubMenu ( int softwareTYPE, int x, int y )
{

	if ( !IsVisibleSubMenu ( softwareTYPE ) ) {

		// Create a list of all software in memory

		LList <char *> software;				// Bit of a hack - think about this
		LList <float> versions;

		DataBank *db = &(game->GetWorld ()->GetPlayer ()->gateway.databank);

		for ( int di = 0; di < db->GetDataSize (); ++di ) {

			if ( db->GetDataFile (di) &&
				 db->GetDataFile (di)->TYPE == DATATYPE_PROGRAM &&
				 db->GetDataFile (di)->softwareTYPE == softwareTYPE ) {

				// This sofware is the correct type.  Add it into the list if it isn't already there,
				// or if its version is higher than any existing program of the same name

				// First try to find a software item of the same name
				int existingindex = -1;
				
				for ( int si = 0; si < software.Size (); ++si ) {
					if ( strcmp ( software.GetData (si), db->GetDataFile (di)->title ) == 0 ) {
						existingindex = si;
						break;
					}
				}

				// If it already exists, add this item in only if it has a higher version number

				if ( existingindex == -1 ) {

					software.PutData ( db->GetDataFile (di)->title );
					versions.PutData ( db->GetDataFile (di)->version );

				}
				if ( existingindex != -1 &&	
					 db->GetDataFile (di)->version > versions.GetData (existingindex) ) {
				
					software.RemoveData ( existingindex );
					versions.RemoveData ( existingindex );

					software.PutDataAtIndex ( db->GetDataFile (di)->title, existingindex );
					versions.PutDataAtIndex ( db->GetDataFile (di)->version, existingindex );

				}

			}

		}

		int timesplit = 500;
		if ( software.Size () > 0 )
			timesplit /= software.Size ();				// Ensures total time = 500ms

		for ( int si = 0; si < software.Size (); ++si ) {
	
			char caption [128], tooltip [128], name [128];
			UplinkSnprintf ( caption, sizeof ( caption ), "%s v%1.1f", software.GetData (si), versions.GetData (si) );
			UplinkStrncpy ( tooltip, "Runs this software application", sizeof ( tooltip ) );
			UplinkSnprintf ( name, sizeof ( name ), "hud_software %d", si );
			EclRegisterButton ( x, y, 140, 15, caption, tooltip, name );
			EclRegisterButtonCallbacks ( name, SoftwareDraw, SoftwareClick, button_click, SoftwareHighlight );
			EclRegisterMovement ( name, x, y - si * 17, si * timesplit );

		}

		currentsubmenu = softwareTYPE;

        SgPlaySound ( RsArchiveFileOpen ( "sounds/softwaremenu.wav" ), "sounds/softwaremenu.wav", false );

	}
	else {

		// Remove the software menu
		// We don't know how many entries there could be, so keep deleting until they run out

		int i = 0;
		char name [32];
		UplinkSnprintf ( name, sizeof ( name ), "hud_software %d", i );

		while ( EclGetButton ( name ) != NULL ) {

			EclRemoveButton ( name );
			++i;
			UplinkSnprintf ( name, sizeof ( name ), "hud_software %d", i );

		}

		// Redraw the button that was selected

		char bname [32];
		UplinkSnprintf ( bname, sizeof ( bname ), "hud_swmenu %d", currentsubmenu );
		EclDirtyButton ( bname );

		currentsubmenu = SOFTWARETYPE_NONE;

	}

}