typen.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003     Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; see the file COPYING. If not, write to the 
00017     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018     Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include <stdio.h>
00022 #include <cstring>
00023 #include <cmath>
00024 
00025 #include "global.h"
00026 #include "ascstring.h"
00027 #include "stringtokenizer.h"
00028 #include "misc.h"
00029 #include "typen.h"
00030 #include "graphicset.h"
00031 // #include "basegfx.h" 
00032 
00033 #include "vehicletype.h"
00034 #include "buildingtype.h"
00035 #include "textfileparser.h"
00036 #include "textfile_evaluation.h"
00037 
00039 const char*  choehenstufen[choehenstufennum] = {"deep submerged", "submerged", "floating", "ground level", "low-level flight", "flight", "high-level flight", "orbit"};
00040 
00041 
00042 
00043 const char*  cmovemalitypes[cmovemalitypenum] = { "default",
00044                                                  "light tracked vehicle", "medium tracked vehicle", "heavy tracked vehicle",
00045                                                  "light wheeled vehicle", "medium wheeled vehicle", "heavy wheeled vehicle",
00046                                                  "trooper",               "rail vehicle",           "medium aircraft",
00047                                                  "medium ship",           "building / turret / object", "light aircraft",
00048                                                  "heavy aircraft",        "light ship",             "heavy ship",  "helicopter",
00049                                                  "hoovercraft"  };
00050 
00051 const char* moveMaliTypeIcons[cmovemalitypenum] = { "pad_symbol_default.png",
00052                                  "pad_symbol_lighttracked.png",
00053                                  "pad_symbol_mediumtracked.png",
00054                                  "pad_symbol_heavytracked.png",
00055                                  "pad_symbol_lightwheeled.png",
00056                                  "pad_symbol_mediumwheeled.png",
00057                                  "pad_symbol_heavywheeled.png",
00058                                  "pad_symbol_trooper.png",
00059                                  "pad_symbol_rail.png",
00060                                  "pad_symbol_mediumair.png",
00061                                  "pad_symbol_mediumship.png",
00062                                  "pad_symbol_turret.png",
00063                                  "pad_symbol_lightair.png",
00064                                  "pad_symbol_heavyair.png",
00065                                  "pad_symbol_lightship.png",
00066                                  "pad_symbol_heavyship.png",
00067                                  "pad_symbol_helicopter.png",
00068                                  "pad_symbol_hoovercraft.png" };
00069 
00070                                                  
00071 const char* cnetcontrol[cnetcontrolnum] = { "store energy",           "store material",           "store fuel",
00072                                             "move out all energy",           "move out all material",           "move out all fuel",
00073                                             "stop storing energy", "stop storing material", "stop storing fuel",
00074                                             "stop energy extraction", "stop material extraction", "stop fuelextraction" };
00075 const char* cgeneralnetcontrol[4] = {       "store",  "move out", "stop storing", "stop using" };
00076                                           // Functionen in Gebuden ....
00077 
00078 const char*  cwettertypen[cwettertypennum] = {"dry (standard)","light rain", "heavy rain", "few snow", "lot of snow", "lot of snow + ice"};
00079 const char*  resourceNames[3]  = {"energy", "material", "fuel"};
00080 
00082 const int experienceDecreaseDamageBoundaries[experienceDecreaseDamageBoundaryNum] = { 80, 60, 40, 20 };
00083 
00084 const int directionangle [ sidenum ] = 
00085  { 0, -53, -127, -180, -180 -53 , -180 -127 };
00086 
00087 
00088 
00089 
00090 const int csolarkraftwerkleistung[cwettertypennum] = { 1024, 512, 256, 756, 384, 384 }; // 1024 ist Maximum
00091 
00092 
00093 
00094 
00095 int getheightdelta ( int height1, int height2 )
00096 {
00097    int ah = height1;
00098    int dh = height2;
00099    int hd = dh - ah;
00100 
00101    if ( ah >= 3 && dh <= 2 )
00102       hd++;
00103    if (dh >= 3 && ah <= 2 )
00104       hd--;
00105 
00106    return hd;
00107 }
00108 
00109 
00110 
00111 ResourceMatrix :: ResourceMatrix (  )
00112 {
00113    for ( int i = 0; i < resourceTypeNum; i++ )
00114       for ( int j = 0; j < resourceTypeNum; j++ )
00115          if ( i == j )
00116             e[i][j] = 1;
00117          else
00118             e[i][j] = 0;
00119 }
00120 
00121 ResourceMatrix :: ResourceMatrix ( const float* f )
00122 {
00123    int num = 0;
00124    for ( int i = 0; i < resourceTypeNum; i++ )
00125       for ( int j = 0; j < resourceTypeNum; j++ )
00126          e[i][j] = f[num++];
00127 }
00128 
00129 
00130 
00131 Resources ResourceMatrix :: operator* ( const Resources& r ) const
00132 {
00133    Resources res;
00134    for ( int i = 0; i < resourceTypeNum; i++ )
00135       for ( int j = 0; j < resourceTypeNum; j++ )
00136          res.resource(i) += int( e[i][j] * r.resource(j));
00137 
00138    return res;
00139 }
00140 
00141 void ResourceMatrix :: read ( tnstream& stream )
00142 {
00143    stream.readInt(); // version
00144    for ( int i = 0; i < resourceTypeNum; i++ )
00145       for ( int j = 0; j < resourceTypeNum; j++ )
00146          e[i][j] = stream.readFloat();
00147 }
00148 
00149 void ResourceMatrix :: write ( tnstream& stream ) const
00150 {
00151    stream.writeInt(1);
00152    for ( int i = 0; i < resourceTypeNum; i++ )
00153       for ( int j = 0; j < resourceTypeNum; j++ )
00154          stream.writeFloat( e[i][j] );
00155 }
00156 
00157 void ResourceMatrix :: runTextIO ( const ASCString& name, PropertyContainer& pc )
00158 {
00159    vector<double> values;
00160    if ( !pc.isReading()) 
00161       for ( int i = 0; i < resourceTypeNum; i++ )
00162          for ( int j = 0; j < resourceTypeNum; j++ )
00163             values.push_back( e[j][i] );
00164       
00165         pc.addDFloatArray(name, values);
00166         if ( pc.isReading()) {
00167            if ( values.size() != 9 )
00168               pc.error("invalid element number of " + name);
00169            
00170            int pos = 0;
00171       for ( int i = 0; i < resourceTypeNum; i++ )
00172          for ( int j = 0; j < resourceTypeNum; j++ )
00173             e[j][i] = values[pos++];
00174         }
00175 }
00176 
00177 void ResourceMatrix :: runTextIO ( const ASCString& name, PropertyContainer& pc, const ResourceMatrix& defaultValue )
00178 {
00179    if ( pc.isReading()) {
00180       if ( pc.find(name))
00181          runTextIO(name,pc);
00182       else
00183          for ( int i = 0; i < resourceTypeNum; i++ )
00184             for ( int j = 0; j < resourceTypeNum; j++ )
00185                e[i][j] = defaultValue.e[i][j];;
00186          
00187    } else
00188       runTextIO(name,pc);
00189 }
00190 
00191 
00192 
00193 const char* Resources::name( int r )
00194 {
00195    return resourceNames[r];
00196 }
00197 
00198 void Resources :: read ( tnstream& stream )
00199 {
00200    for ( int i = 0; i< resourceTypeNum; i++ )
00201       resource(i) = stream.readInt();
00202 }
00203 
00204 void Resources :: write ( tnstream& stream ) const
00205 {
00206    for ( int i = 0; i< resourceTypeNum; i++ )
00207       stream.writeInt( resource(i) );
00208 }
00209 
00210 
00211 Resources operator- ( const Resources& res1, const Resources& res2 )
00212 {
00213    Resources res = res1;
00214    res -= res2;
00215    return res;
00216 }
00217 
00218 Resources operator- ( const Resources& res1 )
00219 {
00220    return Resources ( -res1.energy, -res1.material, -res1.fuel );
00221 }
00222 
00223 
00224 Resources operator+ ( const Resources& res1, const Resources& res2 )
00225 {
00226    Resources res = res1;
00227    res += res2;
00228    return res;
00229 }
00230 
00231 Resources operator* ( const Resources& res1, float a )
00232 {
00233    Resources res = res1;
00234    for ( int r = 0; r < resourceTypeNum; r++ )
00235       res.resource(r) = int( res.resource(r) * a );
00236    return res;
00237 }
00238 
00239 Resources operator/ ( const Resources& res1, float a )
00240 {
00241    Resources res = res1;
00242    for ( int r = 0; r < resourceTypeNum; r++ )
00243       res.resource(r) = int( res.resource(r) / a );
00244    return res;
00245 }
00246 
00247 /*
00248 Resources Resources::operator* ( double d )
00249 {
00250    Resources rs;
00251    for ( int i = 0; i < resourceTypeNum; i++ )
00252       rs.resource(i) = int( resource(i)*d );
00253    return rs;
00254 }
00255 */
00256 
00257 void Resources::runTextIO ( PropertyContainer& pc )
00258 {
00259    pc.addInteger  ( "Energy", energy );
00260    pc.addInteger  ( "Material", material );
00261    pc.addInteger  ( "fuel", fuel );
00262 }
00263 
00264 void Resources::runTextIO ( PropertyContainer& pc, const Resources& defaultValue )
00265 {
00266    pc.addInteger  ( "Energy", energy, defaultValue.energy );
00267    pc.addInteger  ( "Material", material, defaultValue.material );
00268    pc.addInteger  ( "fuel", fuel, defaultValue.fuel );
00269 }
00270 
00271 ASCString Resources::toString() const
00272 {
00273    ASCString s;
00274    int cnt = 0;
00275    for ( int r = 0; r < 3; r++ )
00276       if ( resource(r) )
00277          cnt++;
00278 
00279    int ps = 0;
00280    for ( int r = 0; r < 3; r++ )
00281       if ( resource(r) ) {
00282          ps++;
00283          ASCString txt3;
00284          txt3.format( "%d %s", resource(r), resourceNames[r] );
00285          if ( ps>1 && ps < cnt )
00286             s += ", ";
00287          if ( ps>1 && ps == cnt )
00288             s += " and ";
00289          s += txt3;
00290       }
00291    return s;
00292 }
00293 
00294 
00295 MapCoordinate& MapCoordinate::operator+=( const MapCoodinateVector& delta )
00296 {
00297    x += delta.dx;
00298    y += delta.dy;
00299    return *this;
00300 }
00301 
00302 void MapCoordinate::move(int width, int height) {
00303    x +=width;
00304    y +=height;
00305 }
00306 
00307 ASCString MapCoordinate::toString(bool coordinates) const
00308 {
00309    ASCString s;
00310    if ( coordinates ) 
00311       s.format( "%d/%d", x, y);
00312    else
00313       s.format( "#coord(%d/%d)#", x, y);
00314    return s;
00315 }
00316 
00317 
00318 vector<IntRange> String2IntRangeVector( const ASCString& t )
00319 {
00320    vector<IntRange> irv;
00321 
00322    StringSplit st ( t, ";, " );
00323    ASCString s = st.getNextToken();
00324    while ( !s.empty() ) {
00325       // we have to take care about negative numbers , which have the - at index 0
00326       if ( s.find ( "-",1 ) != ASCString::npos ) {
00327          ASCString from = s.substr ( 0, s.find ( "-", 1 ) );
00328          ASCString to = s.substr ( s.find ( "-",1 )+1 );
00329          irv.push_back ( IntRange ( atoi ( from.c_str() ), atoi ( to.c_str() )));
00330       } else {
00331          irv.push_back ( IntRange ( atoi ( s.c_str() ), atoi ( s.c_str() )));
00332       }
00333       s = st.getNextToken();
00334    }
00335    return irv;
00336 }
00337 
00338 
00339 void IntRange::read ( tnstream& stream )
00340 {
00341    stream.readInt();
00342    from = stream.readInt();
00343    to = stream.readInt();
00344 }
00345 
00346 void IntRange::write ( tnstream& stream ) const
00347 {
00348    stream.writeInt(1);
00349    stream.writeInt( from );
00350    stream.writeInt( to );
00351 }
00352 
00354 
00355 
00356 ASCString Properties::getValue( const ASCString& key )
00357 {
00358    return data[key];
00359 }
00360 
00361 void Properties::setValue( const ASCString& key, const ASCString& value )
00362 {
00363    data[key] = value;
00364 }
00365       
00366 void Properties::write( tnstream& stream ) const
00367 {
00368    stream.writeInt(1);
00369    stream.writeInt( data.size() );
00370    for ( std::map<ASCString, ASCString>::const_iterator i = data.begin(); i != data.end(); ++i ) {
00371       stream.writeString( i->first, true );
00372       stream.writeString( i->second, true );
00373    }
00374 }
00375 
00376 void Properties::read( tnstream& stream )
00377 {
00378    stream.readInt();
00379    int count = stream.readInt();
00380    for ( int i = 0; i < count; ++i ) {
00381       ASCString key, value;
00382       stream.readTextString(key, true );
00383       stream.readTextString(value, true );
00384       data[key] = value;
00385    }
00386 }
00387 
00388 ASCString heightToString( int bitmappedHeight )
00389 {
00390    ASCString s;
00391    for ( int i = 0; i < 8; ++i )
00392       if ( bitmappedHeight & (1 << i )) {
00393          if ( !s.empty() )
00394             s += " ";
00395          s += choehenstufen[i];
00396       }
00397    return s;
00398 }
00399 

Generated on Mon May 21 01:26:38 2012 for Advanced Strategic Command by  doxygen 1.5.1