Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

unitset.cpp

Go to the documentation of this file.
00001 
00008 /*
00009     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00010     Copyright (C) 1994-2005  Martin Bickel  and  Marc Schellenberger
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; see the file COPYING. If not, write to the
00024     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00025     Boston, MA  02111-1307  USA
00026 */
00027 
00028 
00029 
00030 #include "unitset.h"
00031 #include "vehicletype.h"
00032 #include "buildingtype.h"
00033 
00034 
00035 
00036 bool SingleUnitSet :: isMember ( int id, Type type )
00037 {
00038    if ( type == unit ) {
00039       for ( int i = 0; i < unitIds.size(); i++ )
00040         if ( id >= unitIds[i].from && id <= unitIds[i].to )
00041            return true;
00042    }
00043    if ( type == building ) {
00044       for ( int i = 0; i < buildingIds.size(); i++ )
00045         if ( id >= buildingIds[i].from && id <= buildingIds[i].to )
00046            return true;
00047    }
00048    return false;
00049 }
00050 
00051 
00052 std::vector<IntRange> SingleUnitSet::parseIDs ( const char* s )
00053 {
00054    char buf[10000];
00055 
00056    std::vector<IntRange> res;
00057 
00058    if ( s && s[0] ) {
00059 
00060       strcpy ( buf, s);
00061 
00062       char* piclist = strtok ( buf, ";\r\n" );
00063 
00064       char* pic = strtok ( piclist, "," );
00065       while ( pic ) {
00066          int from, to;
00067          if ( strchr ( pic, '-' )) {
00068             char* a = strchr ( pic, '-' );
00069             *a = 0;
00070             from = atoi ( pic );
00071             to = atoi ( ++a );
00072          } else
00073             from = to = atoi ( pic );
00074 
00075          IntRange ir;
00076          ir.from = from;
00077          ir.to = to;
00078          res.push_back ( ir );
00079 
00080          pic = strtok ( NULL, "," );
00081       }
00082    }
00083    return res;
00084 }
00085 
00086 void SingleUnitSet::TranslationTable::parseString ( const char* s )
00087 {
00088    if ( s && s[0] && strchr ( s, ';' )) {
00089       char buf[10000];
00090       if ( s[0] == '#' )
00091          strcpy ( buf, s+1 );
00092       else
00093          strcpy ( buf, s );
00094 
00095       char* tname = strtok ( buf, ";\n\r");
00096       if ( tname )
00097          name = tname;
00098 
00099       char* xl = strtok ( NULL, ";\n\r" );
00100       while ( xl ) {
00101          if ( strchr ( xl, ',' )) {
00102             char* a = strchr ( xl, ',' );
00103             *a = 0;
00104             IntRange ir;
00105             ir.from = atoi ( xl );
00106             ir.to = atoi ( ++a );
00107 
00108             translation.push_back ( ir );
00109 
00110          }
00111          xl = strtok ( NULL, ";\n\r" );
00112       }
00113 
00114    }
00115 }
00116 
00117 
00118 void SingleUnitSet::read ( pnstream stream )
00119 {
00120    if ( !stream )
00121       return;
00122    const char separator = '=';
00123    ASCString s;
00124    int data = stream->readTextString ( s );
00125    if ( s == "#V2#" ) {
00126       while ( data ) {
00127          ASCString s2;
00128          data = stream->readTextString ( s2 );
00129 
00130          size_t seppos = s2.find_first_of ( separator );
00131          if ( seppos >= 0 ) {
00132             ASCString b = s2.substr(0, seppos);
00133             ASCString e = s2.substr( seppos+1 );
00134             if ( b == "NAME" )
00135                name = e;
00136 
00137             if ( b == "ACTIVE" )
00138                active = atoi ( e.c_str() );
00139 
00140 
00141             if ( b == "TRANSFORMATION" ) {
00142                TranslationTable* tt = new TranslationTable;
00143                tt->parseString ( e.c_str() );
00144                transtab.push_back ( tt );
00145             }
00146 
00147             if ( b == "MAINTAINER" )
00148                maintainer = e;
00149 
00150             if ( b == "INFORMATION" )
00151                information = e;
00152 
00153             if ( b == "FILTERBUILDINGS" )
00154                filterBuildings = atoi ( e.c_str() );
00155 
00156             if ( b == "IDENTIFICATION" )
00157                ID = atoi ( e.c_str() );
00158 
00159 
00160             if ( b == "ID" )
00161                unitIds = parseIDs ( e.c_str() );
00162 
00163             if ( b == "BUILDINGID" )
00164                buildingIds = parseIDs ( e.c_str() );
00165 
00166          }
00167       }
00168    } else {
00169       size_t seppos = s.find_first_of ( ';' );
00170       if ( seppos >= 0 ) {
00171          ASCString b = s.substr(0, seppos);
00172          ASCString e = s.substr( seppos+1 );
00173          name = b;
00174          parseIDs ( e.c_str() );
00175 
00176          while ( data ) {
00177             ASCString s2;
00178             data = stream->readTextString ( s2 );
00179             if ( s2.length() ) {
00180                TranslationTable* tt = new TranslationTable;
00181                tt->parseString ( s2.c_str() );
00182                transtab.push_back ( tt );
00183             }
00184          }
00185       }
00186    }
00187 }
00188 
00189 
00190 void loadUnitSets ( void )
00191 {
00192    displayLogMessage ( 4, "loading unit set definition files\n" );
00193    tfindfile ff ( "*.set" );
00194    string n = ff.getnextname();
00195    while ( !n.empty() ) {
00196       displayLogMessage ( 5, " loading unit set definition file %s ... ",n.c_str() );
00197       tnfilestream stream ( n.c_str(), tnstream::reading );
00198 
00199       SingleUnitSet* set = new SingleUnitSet;
00200       set->read ( &stream );
00201       unitSets.push_back ( set );
00202 
00203 //      ItemFiltrationSystem::ItemFilter* itf = new ItemFiltrationSystem::ItemFilter ( set->name, set->ids, !set->active );
00204 //      ItemFiltrationSystem::itemFilters.push_back ( itf );
00205 
00206       n = ff.getnextname();
00207       displayLogMessage ( 5, "done\n" );
00208    } /* endwhile */
00209 }
00210 
00211 UnitSets unitSets;
00212 
00213 
00214 
00215 
00216 int getUnitSetID( const Vehicletype* veh )
00217 {
00218    for ( UnitSets::iterator i = unitSets.begin(); i != unitSets.end(); ++i)
00219       if ( (*i)->isMember( veh->id, SingleUnitSet::unit) )
00220          return (*i)->ID;
00221 
00222    return -1;
00223 }
00224 
00225 int getUnitSetID( const BuildingType* bld )
00226 {
00227    for ( UnitSets::iterator i = unitSets.begin(); i != unitSets.end(); ++i)
00228       if ( (*i)->isMember( bld->id, SingleUnitSet::building) )
00229          return (*i)->ID;
00230 
00231    return -1;
00232 }
00233 
00234 bool vehicleComp( const Vehicletype* v1, const Vehicletype* v2 )
00235 {
00236    int id1 = getUnitSetID(v1);
00237    int id2 = getUnitSetID(v2);
00238    return (id1 <  id2) ||
00239           (id1 == id2 && v1->movemalustyp  < v2->movemalustyp ) ||
00240           (id1 == id2 && v1->movemalustyp == v2->movemalustyp && v1->name < v2->name);
00241 }
00242 

Generated on Tue Jun 24 01:27:54 2008 for Advanced Strategic Command by  doxygen 1.4.2