Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3523 in orxonox.OLD


Ignore:
Timestamp:
Mar 13, 2005, 5:16:24 PM (19 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: Implemented support for loading tracks in the WorldDataFiles

Location:
orxonox/branches/levelloader/src
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/defs/stdincl.h

    r3499 r3523  
    3131#include "vector.h"
    3232
     33#include "tinyxml.h"
     34
    3335#include "list.h"
    3436#include "list_template.h"
  • orxonox/branches/levelloader/src/story_entities/world.cc

    r3503 r3523  
    133133ErrorMessage World::load()
    134134{
    135         if( 0)  // temporary until we really load from files
    136         {
    137                         GameLoader* loader = GameLoader::getInstance();
    138                        
    139                   if( getPath() == NULL)
    140                   {
    141                                 PRINTF(ERR)("World has no path specified for loading");
    142                                 return (ErrorMessage){213,"Path not specified","World::load()"};
    143                   }
    144                  
    145                         TiXMLDocument* XMLDoc = new TiXMLDocument( name);
    146                         // load the campaign document
    147                         if( !XMLDoc.LoadFile())
    148                         {
    149                                 // report an error
    150                                 PRINTF(ERR)("Error loading XML File: %s @ %d:%d\n", XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol());
    151                                 delete XMLDoc;
    152                                 return (ErrorMessage){213,"XML File parsing error","World::load()"};
    153                         }
    154                        
    155                         // check basic validity
    156                         TiXMLElement* element = XMLDoc.RootElement();
    157                         assert( element != NULL);
    158                        
    159                         element = element->FirstChildElement( "WorldDataFile");
    160                        
    161                         if( element == NULL )
    162                         {
    163                                 // report an error
    164                                 PRINTF(ERR)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
    165                                 delete XMLDoc;
    166                                 return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"};
    167                         }
    168                        
    169                         // load the parameters
    170                                 // name
    171                         char* temp;
    172                         char* string = grabParameter( root, "name");
    173                         if( string == NULL)
    174                         {
    175                                 PRINTF(ERR)("World is missing a proper 'name'\n");
    176                                 setStoryId( -1);
    177                         }
    178                         else
    179                         {
    180                                 temp = new char[strlen(string + 2)];
    181                                 worldName = temp;
    182                         }
    183                        
    184                        
    185                         // find WorldEntities
    186                   element = root->FirstChildElement( "WorldEntities");
    187                  
    188                   // load Players/Objects/Whatever
    189                         while( element != NULL)
    190                         {
    191                                 WorldEntity* created = (WorldEntity*) loader->fabricate( element);
    192                                 if( created != NULL) spawn( created);
    193                                 element = element->nextSiblingElement();
    194                         }
    195                        
    196                         // find Track
    197                                 // TODO: load the track somehow
    198                        
    199                         // free the XML data
    200                         delete XMLDoc;
    201         }
     135        GameLoader* loader = GameLoader::getInstance();
     136       
     137  if( getPath() == NULL)
     138  {
     139                PRINTF(ERR)("World has no path specified for loading");
     140                return (ErrorMessage){213,"Path not specified","World::load()"};
     141  }
     142 
     143        TiXMLDocument* XMLDoc = new TiXMLDocument( name);
     144        // load the campaign document
     145        if( !XMLDoc.LoadFile())
     146        {
     147                // report an error
     148                PRINTF(ERR)("Error loading XML File: %s @ %d:%d\n", XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol());
     149                delete XMLDoc;
     150                return (ErrorMessage){213,"XML File parsing error","World::load()"};
     151        }
     152       
     153        // check basic validity
     154        TiXMLElement* root = XMLDoc.RootElement();
     155        assert( root != NULL);
     156       
     157        element = root->FirstChildElement( "WorldDataFile");
     158       
     159        if( root == NULL )
     160        {
     161                // report an error
     162                PRINTF(ERR)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
     163                delete XMLDoc;
     164                return (ErrorMessage){213,"Path not a WorldDataFile","World::load()"};
     165        }
     166       
     167        // load the parameters
     168                // name
     169        TiXMLElement* element;
     170        char* temp;
     171        char* string = grabParameter( root, "name");
     172        if( string == NULL)
     173        {
     174                PRINTF(ERR)("World is missing a proper 'name'\n");
     175                setStoryId( -1);
     176        }
     177        else
     178        {
     179                temp = new char[strlen(string + 2)];
     180                worldName = temp;
     181        }
     182       
     183       
     184        // find WorldEntities
     185  element = root->FirstChildElement( "WorldEntities");
     186 
     187  if( element == NULL)
     188  {
     189                PRINTF(ERR)("World is missing 'WorldEntities'\n");
     190  }
     191  else
     192  {
     193        element = element->FirstChildElement();
     194          // load Players/Objects/Whatever
     195                while( element != NULL)
     196                {
     197                        WorldEntity* created = (WorldEntity*) loader->fabricate( element);
     198                        if( created != NULL) spawn( created);
     199                        element = element->nextSiblingElement();
     200                }
     201        }
     202       
     203        // find Track
     204  element = root->FirstChildElement( "Track");
     205  if( element == NULL)
     206  {
     207                PRINTF(ERR)("World is missing a 'Track'\n");
     208  }
     209  else
     210  {     
     211        //load track
     212        trackManager = TrackManager::getInstance();
     213        trackManager->loadTrack( element);
     214        }
     215 
     216       
     217        // free the XML data
     218        delete XMLDoc;
    202219       
    203220  //  BezierCurve* tmpCurve = new BezierCurve();
  • orxonox/branches/levelloader/src/track_manager.cc

    r3499 r3523  
    1111   ### File Specific:
    1212   main-programmer: Benjamin Grauer
    13    co-programmer: ...
     13   co-programmer: Christian Meyer
    1414*/
    1515
     
    1818
    1919#include "p_node.h"
     20#include "substring.h"
    2021
    2122#include <stdarg.h>
     
    105106
    106107  PRINTF(3)("Initializing the TrackManager\n");
     108  this->namer = new TrackNamer();
    107109  this->firstTrackElem = new TrackElement();
    108110  this->firstTrackElem->ID = 1;
     
    125127  PRINTF(3)("Deleting all the TrackElements\n");
    126128  delete this->firstTrackElem;
     129  delete this->namer;
    127130
    128131  // we do not have a TrackManager anymore
     
    628631    }
    629632}
     633
     634/**
     635   \brief loads track data from a XML Element
     636   \param root the element containing all track data
     637*/
     638void TrackManager::loadTrack( TiXMLElement* root)
     639{
     640        assert( root != NULL);
     641       
     642        TiXMLElement* element;
     643        TiXMLNode* container;
     644        double x, y, z, d;
     645       
     646        element = root->FirstChildElement();
     647       
     648        while( element != NULL)
     649        {
     650                if( !strcmp( element->Value(), "Point")
     651                {
     652                        container = element->FirstChild();
     653                        if( container != NULL && container->Type() == TEXT)
     654                        {
     655                                assert( container->Value() != NULL);
     656                                if( sscanf( container->Value(), "%f,%f,%f", &x, &y, &z) == 3)
     657                                        addPoint( Vector( x, y, z));
     658                                else
     659                                {
     660                                        PRINTF(ERR)("Invalid Point in Track (skipped)\n");
     661                                }                       
     662                        }
     663                }
     664                else if( !strcmp( element->Value(), "Duration")
     665                {
     666                        container = element->FirstChild();
     667                        if( container != NULL && container->Type() == TEXT)
     668                        {
     669                                assert( container->Value() != NULL);
     670                                if( sscanf( container->Value(), "%f", &d) == 1)
     671                                        setDuration( d);
     672                                else
     673                                {
     674                                        PRINTF(ERR)("Invalid Duration in Track (skipped)\n");
     675                                }                       
     676                        }
     677                }
     678                else if( !strcmp( element->Value(), "SavePoint")
     679                {
     680                        setSavePoint();
     681                }
     682                else if( !strcmp( element->Value(), "Fork")
     683                {
     684                        container = element->FirstChild();
     685                        if( container != NULL && container->Type() == TEXT)
     686                        {
     687                                assert( container->Value() != NULL);
     688                                forkS( container->Value());
     689                        }
     690                }
     691                else if( !strcmp( element->Value(), "Join")
     692                {
     693                        container = element->FirstChild();
     694                        if( container != NULL && container->Type() == TEXT)
     695                        {
     696                                assert( container->Value() != NULL);
     697                                joinS( container->Value());
     698                        }
     699                }
     700                else if( !strcmp( element->Value(), "WorkOn")
     701                {
     702                        container = element->FirstChild();
     703                        if( container != NULL && container->Type() == TEXT)
     704                        {
     705                                assert( container->Value() != NULL);
     706                                workOnS( container->Value());
     707                        }
     708                }
     709               
     710                element = element->NextSiblingElement();
     711        }
     712}
     713
     714/**
     715   \brief forks the current track and names the resulting tracks
     716   \param string the names of the new tracks, separated by commas
     717   
     718   The names used have to be unique within a particular track system.
     719*/
     720void TrackManager::forkS( char* string)
     721{
     722        SubString* parts = new SubString( string);
     723        int *IDs;
     724        int n = parts->getN();
     725       
     726        assert( n != 0);
     727       
     728        IDs = new int[n];
     729       
     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*/
     746void 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        int d, t;
     758        d = 0;
     759        t = n;
     760       
     761        PRINTF(DEBUG)("Joining tracks:");
     762       
     763        for( int i = 0; i < n; i++)
     764        {
     765                IDs[d] = namer->getIDof( parts->getString( i));
     766                if( IDs[d] == -1)
     767                {
     768                        PRINTF(ERR)("Track name '%s' unknown, could not join\n", parts->getString( i));
     769                        t--;
     770                }
     771                else
     772                {
     773                        PRINTF(DEBUG)(" '%s'", parts->getString( i));
     774                        d++;
     775                }
     776        }
     777       
     778        PRINTF(DEBUG)("\n");
     779
     780        joinV( t, IDs);
     781               
     782        delete IDs;
     783        delete parts;
     784}
     785
     786/**
     787   \brief set the piece of track to work on
     788   \param string the name of the track to work on (must have been previosly set by forkS)
     789*/
     790void TrackManager::workOnS( char* string)
     791{
     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
     806TrackNamer::~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               
     819int 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
     834const 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
     849void 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}
  • orxonox/branches/levelloader/src/track_manager.h

    r3499 r3523  
    1616
    1717class PNode;
     18class TrackNamer;
    1819
    1920//! condition for choosing a certain Path. \todo implement a useful way.
     
    106107  int trackElemCount;                 //!< The count of TrackElements that exist.
    107108  PNode* bindSlave;
     109        TrackNamer* namer;
    108110 
    109111  void initChildren(unsigned int childCount);
     
    130132  void joinV(unsigned int count, int* trackIDs);
    131133  void finalize(void);
     134        void forkS( char* string);
     135        void joinS( char* string);
     136        void workOnS( char* string);
     137
     138        // Method to load track data from file
     139        void loadTrack( TiXMLElement* root);
    132140
    133141  // Methods to calculate the position on the Path (runtime)
     
    145153};
    146154
     155typedef struct
     156{
     157        int ID;
     158        char* name;
     159        struct TrackIdentifier *next;
     160} TrackIdentifier;
     161
     162class TrackNamer
     163{
     164        public:
     165                TrackNamer() { first = NULL};
     166                ~TrackNamer();
     167               
     168                int getIDof( const char* name);
     169                const char* getNameof( int ID);
     170                void add( const char* name, int ID);
     171       
     172        private:
     173                TrackIdentifier* first;
     174};
     175
    147176#endif /* _TRACK_MANAGER_H */
Note: See TracChangeset for help on using the changeset viewer.