src/tron/gAICharacter.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "gAICharacter.h"
00029 #include "tDirectories.h"
00030 #include "tConfiguration.h"
00031 #include "tRecorder.h"
00032 #include <fstream>
00033 #include <sstream>
00034 
00035 tString aiPlayersConfig("aiplayers.cfg");
00036 static tSettingItem<tString> aifile("AI_CHARACTER_FILE", aiPlayersConfig);
00037 
00038 tArray<gAICharacter> gAICharacter::s_Characters(0);
00039 
00040 static REAL weight[AI_PROPERTIES]=
00041     {
00042         10,
00043         10,
00044         10,
00045         10,
00046         10,
00047         10,
00048         10,
00049         10,
00050         10,
00051         10,
00052         0,
00053         0,
00054         0
00055     };
00056 
00057 // load this description from a stream. Return value: success or not?
00058 bool gAICharacter::Load(std::istream& file)
00059 {
00060     file >> name;
00061 
00062     iq  = 1;
00063     REAL iqt = 1;
00064 
00065     for (int i=0; i< AI_PROPERTIES; i++)
00066     {
00067         if (file.eof())
00068             return false;
00069 
00070         file >> properties[i];
00071         iq  += properties[i] * weight[i];
00072         iqt += weight[i];
00073     }
00074     iq *= 10/iqt;
00075 
00076     description.ReadLine(file);
00077     return true;
00078 }
00079 
00080 // loads and adds a single AI from a string to the array
00081 static bool LoadSingleAI( tString const & line )
00082 {
00083     // build stream from
00084     std::stringstream str(static_cast< char const * >( line ) );
00085 
00086     // gets new character and lets it load the stream
00087     int i = gAICharacter::s_Characters.Len();
00088     gAICharacter& c = gAICharacter::s_Characters[i];
00089     bool ret = c.Load(str);
00090 
00091     // remove character on failure
00092     if ( !ret )
00093         gAICharacter::s_Characters.SetLen(i);
00094 
00095     return ret;
00096 }
00097 
00098 void gAICharacter::LoadAll(const tString& filename)
00099 {
00100     std::ifstream f;
00101 
00102     tTextFileRecorder stream( tDirectories::Config(), filename );
00103     while( !stream.EndOfFile() )
00104     {
00105         tString line( stream.GetLine().c_str() );
00106         if ( line[0] != '#' )
00107             LoadSingleAI( line );
00108     }
00109 }
00110 

Generated on Sat Mar 15 22:56:05 2008 for Armagetron Advanced by  doxygen 1.5.4