Advanced Strategic Command
typen.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include <stdio.h>
22 #include <cstring>
23 #include <cmath>
24 
25 #include "global.h"
26 #include "ascstring.h"
27 #include "stringtokenizer.h"
28 #include "misc.h"
29 #include "typen.h"
30 #include "graphicset.h"
31 // #include "basegfx.h"
32 
33 #include "vehicletype.h"
34 #include "buildingtype.h"
35 #include "textfileparser.h"
36 #include "textfile_evaluation.h"
37 
39 const char* choehenstufen[choehenstufennum] = {"deep submerged", "submerged", "floating", "ground level", "low-level flight", "flight", "high-level flight", "orbit"};
40 
41 
42 
43 const char* cmovemalitypes[cmovemalitypenum] = { "default",
44  "light tracked vehicle", "medium tracked vehicle", "heavy tracked vehicle",
45  "light wheeled vehicle", "medium wheeled vehicle", "heavy wheeled vehicle",
46  "trooper", "rail vehicle", "medium aircraft",
47  "medium ship", "building / turret / object", "light aircraft",
48  "heavy aircraft", "light ship", "heavy ship", "helicopter",
49  "hoovercraft" };
50 
51 const char* moveMaliTypeIcons[cmovemalitypenum] = { "pad_symbol_default.png",
52  "pad_symbol_lighttracked.png",
53  "pad_symbol_mediumtracked.png",
54  "pad_symbol_heavytracked.png",
55  "pad_symbol_lightwheeled.png",
56  "pad_symbol_mediumwheeled.png",
57  "pad_symbol_heavywheeled.png",
58  "pad_symbol_trooper.png",
59  "pad_symbol_rail.png",
60  "pad_symbol_mediumair.png",
61  "pad_symbol_mediumship.png",
62  "pad_symbol_turret.png",
63  "pad_symbol_lightair.png",
64  "pad_symbol_heavyair.png",
65  "pad_symbol_lightship.png",
66  "pad_symbol_heavyship.png",
67  "pad_symbol_helicopter.png",
68  "pad_symbol_hoovercraft.png" };
69 
70 
71 const char* cnetcontrol[cnetcontrolnum] = { "store energy", "store material", "store fuel",
72  "move out all energy", "move out all material", "move out all fuel",
73  "stop storing energy", "stop storing material", "stop storing fuel",
74  "stop energy extraction", "stop material extraction", "stop fuelextraction" };
75 const char* cgeneralnetcontrol[4] = { "store", "move out", "stop storing", "stop using" };
76  // Functionen in Gebuden ....
77 
78 const char* cwettertypen[cwettertypennum] = {"dry (standard)","light rain", "heavy rain", "few snow", "lot of snow", "lot of snow + ice"};
79 const char* resourceNames[3] = {"energy", "material", "fuel"};
80 
81 const int directionangle [ sidenum ] =
82  { 0, -53, -127, -180, -180 -53 , -180 -127 };
83 
84 
85 
86 
87 const int csolarkraftwerkleistung[cwettertypennum] = { 1024, 512, 256, 756, 384, 384 }; // 1024 ist Maximum
88 
89 
90 
91 
92 int getheightdelta ( int height1, int height2 )
93 {
94  int ah = height1;
95  int dh = height2;
96  int hd = dh - ah;
97 
98  if ( ah >= 3 && dh <= 2 )
99  hd++;
100  if (dh >= 3 && ah <= 2 )
101  hd--;
102 
103  return hd;
104 }
105 
106 
107 
109 {
110  for ( int i = 0; i < resourceTypeNum; i++ )
111  for ( int j = 0; j < resourceTypeNum; j++ )
112  if ( i == j )
113  e[i][j] = 1;
114  else
115  e[i][j] = 0;
116 }
117 
119 {
120  int num = 0;
121  for ( int i = 0; i < resourceTypeNum; i++ )
122  for ( int j = 0; j < resourceTypeNum; j++ )
123  e[i][j] = f[num++];
124 }
125 
126 
127 
129 {
130  Resources res;
131  for ( int i = 0; i < resourceTypeNum; i++ )
132  for ( int j = 0; j < resourceTypeNum; j++ )
133  res.resource(i) += int( e[i][j] * r.resource(j));
134 
135  return res;
136 }
137 
139 {
140  stream.readInt(); // version
141  for ( int i = 0; i < resourceTypeNum; i++ )
142  for ( int j = 0; j < resourceTypeNum; j++ )
143  e[i][j] = stream.readFloat();
144 }
145 
146 void ResourceMatrix :: write ( tnstream& stream ) const
147 {
148  stream.writeInt(1);
149  for ( int i = 0; i < resourceTypeNum; i++ )
150  for ( int j = 0; j < resourceTypeNum; j++ )
151  stream.writeFloat( e[i][j] );
152 }
153 
155 {
156  vector<double> values;
157  if ( !pc.isReading())
158  for ( int i = 0; i < resourceTypeNum; i++ )
159  for ( int j = 0; j < resourceTypeNum; j++ )
160  values.push_back( e[j][i] );
161 
162  pc.addDFloatArray(name, values);
163  if ( pc.isReading()) {
164  if ( values.size() != 9 )
165  pc.error("invalid element number of " + name);
166 
167  int pos = 0;
168  for ( int i = 0; i < resourceTypeNum; i++ )
169  for ( int j = 0; j < resourceTypeNum; j++ )
170  e[j][i] = values[pos++];
171  }
172 }
173 
174 void ResourceMatrix :: runTextIO ( const ASCString& name, PropertyContainer& pc, const ResourceMatrix& defaultValue )
175 {
176  if ( pc.isReading()) {
177  if ( pc.find(name))
178  runTextIO(name,pc);
179  else
180  for ( int i = 0; i < resourceTypeNum; i++ )
181  for ( int j = 0; j < resourceTypeNum; j++ )
182  e[i][j] = defaultValue.e[i][j];;
183 
184  } else
185  runTextIO(name,pc);
186 }
187 
188 
189 
190 const char* Resources::name( int r )
191 {
192  return resourceNames[r];
193 }
194 
195 void Resources :: read ( tnstream& stream )
196 {
197  for ( int i = 0; i< resourceTypeNum; i++ )
198  resource(i) = stream.readInt();
199 }
200 
201 void Resources :: write ( tnstream& stream ) const
202 {
203  for ( int i = 0; i< resourceTypeNum; i++ )
204  stream.writeInt( resource(i) );
205 }
206 
207 
208 Resources operator- ( const Resources& res1, const Resources& res2 )
209 {
210  Resources res = res1;
211  res -= res2;
212  return res;
213 }
214 
216 {
217  return Resources ( -res1.energy, -res1.material, -res1.fuel );
218 }
219 
220 
221 Resources operator+ ( const Resources& res1, const Resources& res2 )
222 {
223  Resources res = res1;
224  res += res2;
225  return res;
226 }
227 
228 Resources operator* ( const Resources& res1, float a )
229 {
230  Resources res = res1;
231  for ( int r = 0; r < resourceTypeNum; r++ )
232  res.resource(r) = int( res.resource(r) * a );
233  return res;
234 }
235 
236 Resources operator/ ( const Resources& res1, float a )
237 {
238  Resources res = res1;
239  for ( int r = 0; r < resourceTypeNum; r++ )
240  res.resource(r) = int( res.resource(r) / a );
241  return res;
242 }
243 
244 /*
245 Resources Resources::operator* ( double d )
246 {
247  Resources rs;
248  for ( int i = 0; i < resourceTypeNum; i++ )
249  rs.resource(i) = int( resource(i)*d );
250  return rs;
251 }
252 */
253 
255 {
256  pc.addInteger ( "Energy", energy );
257  pc.addInteger ( "Material", material );
258  pc.addInteger ( "fuel", fuel );
259 }
260 
261 void Resources::runTextIO ( PropertyContainer& pc, const Resources& defaultValue )
262 {
263  pc.addInteger ( "Energy", energy, defaultValue.energy );
264  pc.addInteger ( "Material", material, defaultValue.material );
265  pc.addInteger ( "fuel", fuel, defaultValue.fuel );
266 }
267 
269 {
270  ASCString s;
271  int cnt = 0;
272  for ( int r = 0; r < 3; r++ )
273  if ( resource(r) )
274  cnt++;
275 
276  int ps = 0;
277  for ( int r = 0; r < 3; r++ )
278  if ( resource(r) ) {
279  ps++;
280  ASCString txt3;
281  txt3.format( "%d %s", resource(r), resourceNames[r] );
282  if ( ps>1 && ps < cnt )
283  s += ", ";
284  if ( ps>1 && ps == cnt )
285  s += " and ";
286  s += txt3;
287  }
288  return s;
289 }
290 
291 
293 {
294  x += delta.dx;
295  y += delta.dy;
296  return *this;
297 }
298 
299 void MapCoordinate::move(int width, int height) {
300  x +=width;
301  y +=height;
302 }
303 
304 ASCString MapCoordinate::toString(bool coordinates) const
305 {
306  ASCString s;
307  if ( coordinates )
308  s.format( "%d/%d", x, y);
309  else
310  s.format( "#coord(%d/%d)#", x, y);
311  return s;
312 }
313 
314 ASCString MapCoordinate3D::toString(bool coordinates) const
315 {
316  ASCString s;
317  if ( coordinates )
318  s.format( "%d/%d/%d", x, y, getNumericalHeight());
319  else
320  s.format( "#coord(%d/%d)#", x, y);
321  return s;
322 }
323 
324 
325 
326 vector<IntRange> String2IntRangeVector( const ASCString& t )
327 {
328  vector<IntRange> irv;
329 
330  StringSplit st ( t, ";, " );
331  ASCString s = st.getNextToken();
332  while ( !s.empty() ) {
333  // we have to take care about negative numbers , which have the - at index 0
334  if ( s.find ( "-",1 ) != ASCString::npos ) {
335  ASCString from = s.substr ( 0, s.find ( "-", 1 ) );
336  ASCString to = s.substr ( s.find ( "-",1 )+1 );
337  irv.push_back ( IntRange ( atoi ( from.c_str() ), atoi ( to.c_str() )));
338  } else {
339  irv.push_back ( IntRange ( atoi ( s.c_str() ), atoi ( s.c_str() )));
340  }
341  s = st.getNextToken();
342  }
343  return irv;
344 }
345 
346 
347 void IntRange::read ( tnstream& stream )
348 {
349  stream.readInt();
350  from = stream.readInt();
351  to = stream.readInt();
352 }
353 
354 void IntRange::write ( tnstream& stream ) const
355 {
356  stream.writeInt(1);
357  stream.writeInt( from );
358  stream.writeInt( to );
359 }
360 
362 
363 
365 {
366  return data[key];
367 }
368 
369 void Properties::setValue( const ASCString& key, const ASCString& value )
370 {
371  data[key] = value;
372 }
373 
374 void Properties::write( tnstream& stream ) const
375 {
376  stream.writeInt(1);
377  stream.writeInt( data.size() );
378  for ( std::map<ASCString, ASCString>::const_iterator i = data.begin(); i != data.end(); ++i ) {
379  stream.writeString( i->first, true );
380  stream.writeString( i->second, true );
381  }
382 }
383 
384 void Properties::read( tnstream& stream )
385 {
386  stream.readInt();
387  int count = stream.readInt();
388  for ( int i = 0; i < count; ++i ) {
389  ASCString key, value;
390  stream.readTextString(key, true );
391  stream.readTextString(value, true );
392  data[key] = value;
393  }
394 }
395 
396 ASCString heightToString( int bitmappedHeight )
397 {
398  ASCString s;
399  for ( int i = 0; i < 8; ++i )
400  if ( bitmappedHeight & (1 << i )) {
401  if ( !s.empty() )
402  s += " ";
403  s += choehenstufen[i];
404  }
405  return s;
406 }
407 
int fuel
Definition: typen.h:101
A mathematical matrix for transforming Resources instance (which is mathematically a vector) into a d...
Definition: typen.h:158
const int cnetcontrolnum
The number of netcontrol actions ; see cnetcontrol.
Definition: typen.h:551
void write(tnstream &stream) const
Definition: typen.cpp:146
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
void read(tnstream &stream)
Definition: typen.cpp:384
miscellaneous functions which are used by ASC and all its tools
int getheightdelta(int height1, int height2)
calculate the height difference between two levels of height.
Definition: typen.cpp:92
const int choehenstufennum
The number of levels of height.
Definition: typen.h:67
int energy
Definition: typen.h:99
Resources operator/(const Resources &res1, float a)
Definition: typen.cpp:236
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:314
void runTextIO(const ASCString &name, PropertyContainer &pc)
Definition: typen.cpp:154
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
ASCString heightToString(int bitmappedHeight)
Definition: typen.cpp:396
const int csolarkraftwerkleistung[cwettertypennum]
Definition: typen.cpp:87
const char * cwettertypen[cwettertypennum]
the names of the different weather
Definition: typen.cpp:78
void write(tnstream &stream) const
Definition: typen.cpp:374
void read(tnstream &stream)
Definition: typen.cpp:347
ASCString toString() const
Definition: typen.cpp:268
vector< IntRange > String2IntRangeVector(const ASCString &t)
parses a string repesenting a number of int ranges Example input: "10-20; 25 ; 30-125" ...
Definition: typen.cpp:326
Functions to evaluate the parsed *.asctxt files.
const char * cmovemalitypes[cmovemalitypenum]
the names of the different MoveMalusTypes
Definition: typen.cpp:43
Resources operator*(const Resources &res1, float a)
Definition: typen.cpp:228
void runTextIO(PropertyContainer &pc)
Definition: typen.cpp:254
int getNumericalHeight() const
Definition: typen.h:242
A system that provides a set of images for vehicles, buildings, etc.
void read(tnstream &stream)
Definition: typen.cpp:195
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
Global platform dependant definitions. This file just branches to the platform specific files in thei...
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
static const char * name(int r)
Definition: typen.cpp:190
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int from
the lower boundary.
Definition: typen.h:337
virtual void writeString(const string &pc, bool binary=true)
writes the C++ String pc to the stream.
Definition: basestrm.cpp:545
const int sidenum
the number of sides that a field has; is now fixed at 6;
Definition: typen.h:438
virtual float readFloat(void)
Reads a flaot variable.
Definition: basestrm.cpp:328
void read(tnstream &stream)
Definition: typen.cpp:138
void error(const ASCString &errmsg)
const int resourceTypeNum
The number of different resources that ASC uses.
Definition: typen.h:77
void write(tnstream &stream) const
Definition: typen.cpp:201
const int directionangle[sidenum]
Definition: typen.cpp:81
A simple string tokenizer.
Coordinate on the twodimensional map.
Definition: typen.h:202
ASCString getValue(const ASCString &key)
Definition: typen.cpp:364
Resources operator*(const Resources &r) const
Definition: typen.cpp:128
const int cmovemalitypenum
The number of vehicle categories; Each category has its own move malus.
Definition: typen.h:384
The interface for the buildingtype class.
represents a change of a MapCoordinate
Definition: typen.h:189
int material
Definition: typen.h:100
void setValue(const ASCString &key, const ASCString &value)
Definition: typen.cpp:369
const char * moveMaliTypeIcons[cmovemalitypenum]
filenames of icons representing the varios movemalus types
Definition: typen.cpp:51
void addDFloatArray(const ASCString &name, vector< double > &property)
Represents a range of numbers, with a lower and upper boundary.
Definition: typen.h:334
MapCoordinate & operator+=(const MapCoodinateVector &delta)
Definition: typen.cpp:292
void addInteger(const ASCString &name, int &property)
const char * choehenstufen[choehenstufennum]
The different levels of height.
Definition: typen.cpp:39
int atoi(const std::string &s)
Definition: misc.cpp:152
int & resource(int type)
Definition: typen.h:105
int to
the upper boundary
Definition: typen.h:340
virtual void writeFloat(float f)
Write a floating point variable.
Definition: basestrm.cpp:385
const char * resourceNames[3]
the names of the different resources. Redundent, as we already have Resources::name ...
Definition: typen.cpp:79
const char * cgeneralnetcontrol[4]
Definition: typen.cpp:75
Resources operator-(const Resources &res1, const Resources &res2)
Definition: typen.cpp:208
Resources are basically the currency of ASC.
Definition: typen.h:97
virtual bool readTextString(ASCString &s, bool includeCR=false)
Reads a string.
Definition: basestrm.cpp:511
Resources operator+(const Resources &res1, const Resources &res2)
Definition: typen.cpp:221
bool find(const ASCString &name)
void write(tnstream &stream) const
Definition: typen.cpp:354
void move(int width, int height)
Definition: typen.cpp:299
const char * cnetcontrol[cnetcontrolnum]
The actions that can be used to control a ResourceNet.
Definition: typen.cpp:71
const int cwettertypennum
The number of different weather.
Definition: typen.h:61
Functions to parse the *.asctxt files.
ASCString getNextToken()