Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.