Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 13, 2005, 6:59:20 PM (19 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: removed excess class usage adn messed with makefile definitions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/track_manager.cc

    r3523 r3525  
    9090}
    9191
     92/**
     93   \brief Sets the name of this TrackElement
     94   \param trackName The new name for this element
     95*/
     96void TrackElement::setName(const char* trackName)
     97{
     98        if( this->name != NULL) delete this->name;
     99       
     100        this->name = new char[strlen(trackName) + 1];
     101        strcpy( this->name, trackName);
     102}
     103
     104/**
     105   \brief Searches through all the TrackElements for trackName.
     106   \param trackName The name to search for.
     107   \returns The TrackElement if Found, NULL otherwise.
     108   
     109   \todo make this more modular, to search for different things
     110*/
     111TrackElement* TrackElement::findByName(const char* trackName)
     112{
     113  // return if Found.
     114  if ( this->name != NULL && !strcmp( this->name, trackname))
     115    return this;
     116  // search on.
     117  if (this->childCount > 0)
     118    for (int i=0; i < this->childCount; i++)
     119      {
     120        TrackElement* tmpElem;
     121        if ((tmpElem = this->children[i]->findByName(trackName)))
     122          return tmpElem;
     123      }
     124  else return NULL;
     125}
    92126
    93127
     
    106140
    107141  PRINTF(3)("Initializing the TrackManager\n");
    108   this->namer = new TrackNamer();
    109142  this->firstTrackElem = new TrackElement();
    110143  this->firstTrackElem->ID = 1;
     
    152185   \param childCount The Count of children to make space for.
    153186*/
    154 void TrackManager::initChildren(unsigned int childCount)
     187void TrackManager::initChildren(unsigned int childCount, SubString* names)
    155188{
    156189  this->currentTrackElem->childCount = childCount;
     
    159192  for (int i=0; i<childCount; i++)
    160193    {
     194        if( names != NULL)
     195        {
     196                if( this->findTrackElementByName( names->getString(i)))
     197                {
     198                        PRINTF(ERR)("Track name '%s' already taken
     199                }
     200            }
     201               
    161202      this->currentTrackElem->children[i] = new TrackElement();
    162203      this->currentTrackElem->children[i]->ID = ++trackElemCount;
     
    176217}
    177218
     219/**
     220   \brief Searches for a given trackname.
     221   \param trackName the trackName to search for.
     222   \returns The TrackElement if found, NULL otherwise.
     223*/
     224TrackElement* TrackManager::findTrackElementByName(const char* trackName) const
     225{
     226  return firstTrackElem->findByName(trackName);
     227}
    178228// INITIALIZE //
    179229
     
    317367   \param count The Count of childrens the current HotPoint will have.
    318368   \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
     369   \param names A SubString containing the trackNames for the new tracks, if NULL, no names will be assigned
    319370
    320371   \see void TrackManager::fork(int count, ...)
     
    322373   \todo initialisation is wrong!! also in setSavePoint.
    323374*/
    324 void TrackManager::forkV(unsigned int count, int* trackIDs)
     375void TrackManager::forkV(unsigned int count, int* trackIDs, SubString* names)
    325376{
    326377  printf("Forking with %d children\n", count);
     
    331382  for(int i = 0; i < count; i++)
    332383    trackIDs[i]=this->trackElemCount+1+i;
    333   this->initChildren(count);
     384  this->initChildren(count, names);
    334385}
    335386
     
    636687   \param root the element containing all track data
    637688*/
    638 void TrackManager::loadTrack( TiXMLElement* root)
     689void TrackManager::loadTrack( TiXmlElement* root)
    639690{
    640691        assert( root != NULL);
    641692       
    642         TiXMLElement* element;
    643         TiXMLNode* container;
     693        TiXmlElement* element;
     694        TiXmlNode* container;
    644695        double x, y, z, d;
    645696       
     
    720771void TrackManager::forkS( char* string)
    721772{
     773        // get the names for the tracks and therefore the amount of new tracks
     774        SubString* names = new SubString( string);
     775        int *IDs;
     776        int n = parts->getN();
     777       
     778        assert( n != 0);
     779       
     780        IDs = new int[n];
     781       
     782        forkV( n, IDs, names);
     783       
     784        // IDs are irrelevant when assigning string names to tracks
     785        delete IDs;
     786        delete names;
     787}
     788
     789/**
     790   \brief joins the named strings
     791   \param string the names of the track to be joined, separated by commas
     792*/
     793void TrackManager::joinS( char* string)
     794{
    722795        SubString* parts = new SubString( string);
    723796        int *IDs;
     
    728801        IDs = new int[n];
    729802       
    730         forkV( n, IDs);
    731        
    732         for( int i = 0; i < n; i++)
    733         {
    734                 PRINTF(DEBUG)("Track fork '%s'(%d) generated\n", parts->getString( i), IDs[i]);
    735                 namer->add( parts->getString( i), IDs[i]);
    736         }
    737                
    738         delete IDs;
    739         delete parts;
    740 }
    741 
    742 /**
    743    \brief joins the named strings
    744    \param string the names of the track to be joined, separated by commas
    745 */
    746 void TrackManager::joinS( char* string)
    747 {
    748         SubString* parts = new SubString( string);
    749         int *IDs;
    750         int n = parts->getN();
    751        
    752        
    753         assert( n != 0);
    754        
    755         IDs = new int[n];
    756        
    757803        int d, t;
    758804        d = 0;
     
    763809        for( int i = 0; i < n; i++)
    764810        {
    765                 IDs[d] = namer->getIDof( parts->getString( i));
    766                 if( IDs[d] == -1)
     811                TrackElement* element = this->findTrackElementByName(parts->getString( i));
     812                if( element == NULL)
    767813                {
    768814                        PRINTF(ERR)("Track name '%s' unknown, could not join\n", parts->getString( i));
     
    771817                else
    772818                {
    773                         PRINTF(DEBUG)(" '%s'", parts->getString( i));
     819                        PRINTF(DEBUG)(" '%s'(%d)", parts->getString( i), element->ID);
     820                        IDs[d] = element->ID;
    774821                        d++;
    775822                }
     
    790837void TrackManager::workOnS( char* string)
    791838{
    792         int i;
    793         i = namer->getIdof( string);
    794         if( i != -1)
    795         {
    796                 WorkOn( i);
    797         }
    798         else PRINTF(ERR)("'%s' is not a valid TrackIdentifier\n", string);
    799 }
    800 
    801 
    802 //      ------------------------------------------
    803 //              TrackNamer
    804 //      -------------------------------------------
    805 
    806 TrackNamer::~TrackNamer()
    807 {
    808         TrackIdentifier* t;
    809        
    810         while( first != NULL)
    811         {
    812                 t = first;
    813                 first = first->next;
    814                 free( t->name);
    815                 free( t);
    816         }
    817 }
    818                
    819 int TrackNamer::getIDof( const char* name)
    820 {
    821         TrackIdentifier *t;
    822         t = first;
    823        
    824         assert( name != NULL);
    825        
    826         while( t != NULL)
    827         {
    828                 if( !strcmp( name, t->name)) return t->ID;
    829         }
    830        
    831         return -1;
    832 }
    833 
    834 const char* TrackNamer::getNameof( int ID)
    835 {
    836         TrackIdentifier *t;
    837         t = first;
    838        
    839         assert( ID != -1);
    840        
    841         while( t != NULL)
    842         {
    843                 if( ID == t->ID) return t->name;
    844         }
    845        
    846         return NULL;
    847 }
    848 
    849 void TrackNamer::add( const char* name, int ID)
    850 {
    851         TrackIdentifier *t, *d;
    852        
    853         assert( name != NULL);
    854         assert( ID != -1);
    855        
    856         t = malloc( sizeof( TrackIdentifier));
    857         t->next = NULL;
    858         t->ID = ID;
    859         t->name = malloc( strlen( name) + 1);
    860         strcpy( t->name, name);
    861        
    862         if( first == NULL)
    863         {
    864                 first = t;
    865         }
    866         else
    867         {
    868                 d = first;
    869                 while( d->next != NULL)
    870                 {
    871                         if( ID == d->ID)
    872                         {
    873                                 PRINTF(ERR)("Track ID %d already named %s (tried to name it %s)\n", ID, d->name, name);
    874                                 free( t->name);
    875                                 free( t);
    876                                 return;
    877                         }
    878                         if( !strcmp( name, d->name))
    879                         {
    880                                 PRINTF(ERR)("Track name %s already used by track ID %d (tried to assign it to track ID %d)<n", name, d->ID, ID);
    881                                 free( t->name);
    882                                 free( t);
    883                                 return;
    884                         }
    885                         d = d->next;
    886                 }
    887                 d->next = t;
    888         }       
    889 }
     839  TrackElement* tmpElem = findTrackElementByName( string);
     840  if (tmpElem)
     841    this->currentTrackElem = tmpElem;
     842  else
     843    PRINTF(ERR)("TrackElement not Found, leaving unchanged\n");
     844  PRINTF(DEBUG)("now Working on %d\n", this->currentTrackElem->ID);
     845}
Note: See TracChangeset for help on using the changeset viewer.