Advanced Strategic Command
unitset.cpp
Go to the documentation of this file.
1 
8 /*
9  This file is part of Advanced Strategic Command; http://www.asc-hq.de
10  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; see the file COPYING. If not, write to the
24  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25  Boston, MA 02111-1307 USA
26 */
27 
28 
29 
30 #include "unitset.h"
31 #include "vehicletype.h"
32 #include "buildingtype.h"
33 
34 
35 
36 bool SingleUnitSet :: isMember ( int id, Type type )
37 {
38  if ( type == unit ) {
39  for ( int i = 0; i < unitIds.size(); i++ )
40  if ( id >= unitIds[i].from && id <= unitIds[i].to )
41  return true;
42  }
43  if ( type == building ) {
44  for ( int i = 0; i < buildingIds.size(); i++ )
45  if ( id >= buildingIds[i].from && id <= buildingIds[i].to )
46  return true;
47  }
48  return false;
49 }
50 
51 
52 std::vector<IntRange> SingleUnitSet::parseIDs ( const char* s )
53 {
54  char buf[10000];
55 
56  std::vector<IntRange> res;
57 
58  if ( s && s[0] ) {
59 
60  strcpy ( buf, s);
61 
62  char* piclist = strtok ( buf, ";\r\n" );
63 
64  char* pic = strtok ( piclist, "," );
65  while ( pic ) {
66  int from, to;
67  if ( strchr ( pic, '-' )) {
68  char* a = strchr ( pic, '-' );
69  *a = 0;
70  from = atoi ( pic );
71  to = atoi ( ++a );
72  } else
73  from = to = atoi ( pic );
74 
75  IntRange ir;
76  ir.from = from;
77  ir.to = to;
78  res.push_back ( ir );
79 
80  pic = strtok ( NULL, "," );
81  }
82  }
83  return res;
84 }
85 
87 {
88  if ( s && s[0] && strchr ( s, ';' )) {
89  char buf[10000];
90  if ( s[0] == '#' )
91  strcpy ( buf, s+1 );
92  else
93  strcpy ( buf, s );
94 
95  char* tname = strtok ( buf, ";\n\r");
96  if ( tname )
97  name = tname;
98 
99  char* xl = strtok ( NULL, ";\n\r" );
100  while ( xl ) {
101  if ( strchr ( xl, ',' )) {
102  char* a = strchr ( xl, ',' );
103  *a = 0;
104  IntRange ir;
105  ir.from = atoi ( xl );
106  ir.to = atoi ( ++a );
107 
108  translation.push_back ( ir );
109 
110  }
111  xl = strtok ( NULL, ";\n\r" );
112  }
113 
114  }
115 }
116 
117 
119 {
120  if ( !stream )
121  return;
122  const char separator = '=';
123  ASCString s;
124  int data = stream->readTextString ( s );
125  if ( s == "#V2#" ) {
126  while ( data ) {
127  ASCString s2;
128  data = stream->readTextString ( s2 );
129 
130  size_t seppos = s2.find_first_of ( separator );
131  if ( seppos != ASCString::npos ) {
132  ASCString b = s2.substr(0, seppos);
133  ASCString e = s2.substr( seppos+1 );
134  if ( b == "NAME" )
135  name = e;
136 
137  if ( b == "ACTIVE" )
138  active = atoi ( e.c_str() );
139 
140 
141  if ( b == "TRANSFORMATION" ) {
143  tt->parseString ( e.c_str() );
144  transtab.push_back ( tt );
145  }
146 
147  if ( b == "MAINTAINER" )
148  maintainer = e;
149 
150  if ( b == "INFORMATION" )
151  information = e;
152 
153  if ( b == "FILTERBUILDINGS" )
154  filterBuildings = atoi ( e.c_str() );
155 
156  if ( b == "IDENTIFICATION" )
157  ID = atoi ( e.c_str() );
158 
159 
160  if ( b == "ID" )
161  unitIds = parseIDs ( e.c_str() );
162 
163  if ( b == "BUILDINGID" )
164  buildingIds = parseIDs ( e.c_str() );
165 
166  }
167  }
168  } else {
169  size_t seppos = s.find_first_of ( ';' );
170  if ( seppos != ASCString::npos ) {
171  ASCString b = s.substr(0, seppos);
172  ASCString e = s.substr( seppos+1 );
173  name = b;
174  parseIDs ( e.c_str() );
175 
176  while ( data ) {
177  ASCString s2;
178  data = stream->readTextString ( s2 );
179  if ( s2.length() ) {
181  tt->parseString ( s2.c_str() );
182  transtab.push_back ( tt );
183  }
184  }
185  }
186  }
187 }
188 
189 
190 void loadUnitSets ( void )
191 {
192  displayLogMessage ( 4, "loading unit set definition files\n" );
193  tfindfile ff ( "*.set" );
194  string n = ff.getnextname();
195  while ( !n.empty() ) {
196  displayLogMessage ( 5, " loading unit set definition file %s ... ",n.c_str() );
197  tnfilestream stream ( n.c_str(), tnstream::reading );
198 
199  SingleUnitSet* set = new SingleUnitSet;
200  set->read ( &stream );
201  unitSets.push_back ( set );
202 
203 // ItemFiltrationSystem::ItemFilter* itf = new ItemFiltrationSystem::ItemFilter ( set->name, set->ids, !set->active );
204 // ItemFiltrationSystem::itemFilters.push_back ( itf );
205 
206  n = ff.getnextname();
207  displayLogMessage ( 5, "done\n" );
208  } /* endwhile */
209 }
210 
212 
213 
214 
215 
216 int getUnitSetID( const VehicleType* veh )
217 {
218  for ( UnitSets::iterator i = unitSets.begin(); i != unitSets.end(); ++i)
219  if ( (*i)->isMember( veh->id, SingleUnitSet::unit) )
220  return (*i)->ID;
221 
222  return -1;
223 }
224 
225 int getUnitSetID( const BuildingType* bld )
226 {
227  for ( UnitSets::iterator i = unitSets.begin(); i != unitSets.end(); ++i)
228  if ( (*i)->isMember( bld->id, SingleUnitSet::building) )
229  return (*i)->ID;
230 
231  return -1;
232 }
233 
234 bool vehicleComp( const VehicleType* v1, const VehicleType* v2 )
235 {
236  int id1 = getUnitSetID(v1);
237  int id2 = getUnitSetID(v2);
238  return (id1 < id2) ||
239  (id1 == id2 && v1->movemalustyp < v2->movemalustyp ) ||
240  (id1 == id2 && v1->movemalustyp == v2->movemalustyp && v1->name < v2->name);
241 }
242 
void read(tnstream *stream)
Definition: unitset.cpp:118
void parseString(const char *s)
Definition: unitset.cpp:86
ASCString information
Definition: unitset.h:47
ASCString getnextname(int *loc=NULL, bool *inContainer=NULL, ASCString *location=NULL)
Returns the next entry of the internal file list.
Definition: basestrm.cpp:2211
bool isMember(int id, Type type)
Definition: unitset.cpp:36
bool vehicleComp(const VehicleType *v1, const VehicleType *v2)
Definition: unitset.cpp:234
SingleUnitSet(void)
Definition: unitset.h:54
The interface for all kinds of IO stream.
std::vector< IntRange > unitIds
Definition: unitset.h:49
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int from
the lower boundary.
Definition: typen.h:337
std::vector< IntRange > parseIDs(const char *s)
Definition: unitset.cpp:52
ASCString name
Definition: unitset.h:45
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
The class describing properties that are common to all buildings of a certain kind.
Definition: buildingtype.h:35
void displayLogMessage(int msgVerbosity, const char *message,...)
int movemalustyp
The category of the unit. Original used only to distinguish only between different movement costs for...
Definition: vehicletype.h:206
Searching for files.
Definition: basestrm.h:484
The interface for the buildingtype class.
std::vector< IntRange > translation
Definition: unitset.h:37
ASCString maintainer
Definition: unitset.h:46
bool filterBuildings
Definition: unitset.h:52
int getUnitSetID(const VehicleType *veh)
Definition: unitset.cpp:216
Represents a range of numbers, with a lower and upper boundary.
Definition: typen.h:334
int active
Definition: unitset.h:43
int atoi(const std::string &s)
Definition: misc.cpp:152
std::vector< TranslationTable * > transtab
Definition: unitset.h:51
void loadUnitSets(void)
Definition: unitset.cpp:190
std::vector< IntRange > buildingIds
Definition: unitset.h:50
int to
the upper boundary
Definition: typen.h:340
a container that stores pointers and deletes the pointed-to objects on destruction ...
Definition: typen.h:314
UnitSets unitSets
Definition: unitset.cpp:211
virtual bool readTextString(ASCString &s, bool includeCR=false)
Reads a string.
Definition: basestrm.cpp:511
ASCString name
a short name, for example B-52