Changeset 3525 in orxonox.OLD for orxonox/branches/levelloader/src/track_manager.cc
- Timestamp:
- Mar 13, 2005, 6:59:20 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/levelloader/src/track_manager.cc
r3523 r3525 90 90 } 91 91 92 /** 93 \brief Sets the name of this TrackElement 94 \param trackName The new name for this element 95 */ 96 void 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 */ 111 TrackElement* 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 } 92 126 93 127 … … 106 140 107 141 PRINTF(3)("Initializing the TrackManager\n"); 108 this->namer = new TrackNamer();109 142 this->firstTrackElem = new TrackElement(); 110 143 this->firstTrackElem->ID = 1; … … 152 185 \param childCount The Count of children to make space for. 153 186 */ 154 void TrackManager::initChildren(unsigned int childCount )187 void TrackManager::initChildren(unsigned int childCount, SubString* names) 155 188 { 156 189 this->currentTrackElem->childCount = childCount; … … 159 192 for (int i=0; i<childCount; i++) 160 193 { 194 if( names != NULL) 195 { 196 if( this->findTrackElementByName( names->getString(i))) 197 { 198 PRINTF(ERR)("Track name '%s' already taken 199 } 200 } 201 161 202 this->currentTrackElem->children[i] = new TrackElement(); 162 203 this->currentTrackElem->children[i]->ID = ++trackElemCount; … … 176 217 } 177 218 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 */ 224 TrackElement* TrackManager::findTrackElementByName(const char* trackName) const 225 { 226 return firstTrackElem->findByName(trackName); 227 } 178 228 // INITIALIZE // 179 229 … … 317 367 \param count The Count of childrens the current HotPoint will have. 318 368 \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 319 370 320 371 \see void TrackManager::fork(int count, ...) … … 322 373 \todo initialisation is wrong!! also in setSavePoint. 323 374 */ 324 void TrackManager::forkV(unsigned int count, int* trackIDs )375 void TrackManager::forkV(unsigned int count, int* trackIDs, SubString* names) 325 376 { 326 377 printf("Forking with %d children\n", count); … … 331 382 for(int i = 0; i < count; i++) 332 383 trackIDs[i]=this->trackElemCount+1+i; 333 this->initChildren(count );384 this->initChildren(count, names); 334 385 } 335 386 … … 636 687 \param root the element containing all track data 637 688 */ 638 void TrackManager::loadTrack( TiX MLElement* root)689 void TrackManager::loadTrack( TiXmlElement* root) 639 690 { 640 691 assert( root != NULL); 641 692 642 TiX MLElement* element;643 TiX MLNode* container;693 TiXmlElement* element; 694 TiXmlNode* container; 644 695 double x, y, z, d; 645 696 … … 720 771 void TrackManager::forkS( char* string) 721 772 { 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 */ 793 void TrackManager::joinS( char* string) 794 { 722 795 SubString* parts = new SubString( string); 723 796 int *IDs; … … 728 801 IDs = new int[n]; 729 802 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 strings744 \param string the names of the track to be joined, separated by commas745 */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 757 803 int d, t; 758 804 d = 0; … … 763 809 for( int i = 0; i < n; i++) 764 810 { 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) 767 813 { 768 814 PRINTF(ERR)("Track name '%s' unknown, could not join\n", parts->getString( i)); … … 771 817 else 772 818 { 773 PRINTF(DEBUG)(" '%s'", parts->getString( i)); 819 PRINTF(DEBUG)(" '%s'(%d)", parts->getString( i), element->ID); 820 IDs[d] = element->ID; 774 821 d++; 775 822 } … … 790 837 void TrackManager::workOnS( char* string) 791 838 { 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.