Back to index

supertuxkart  0.5+dfsg1
history.cpp
Go to the documentation of this file.
00001 //  $Id: history.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "history.hpp"
00021 #include "world.hpp"
00022 #include "kart.hpp"
00023 #include "track.hpp"
00024 #include "race_manager.hpp"
00025 
00026 History* history = 0;
00027 
00028 void History::SetSize(int n)
00029 {
00030     m_size      = n;
00031     m_all_deltas  = new float[m_size];
00032     m_current    = -1;
00033     m_wrapped    = false;
00034 }   // History
00035 
00036 //-----------------------------------------------------------------------------
00037 void History::StoreDelta(float delta)
00038 {
00039     this->m_current++;
00040     if(m_current>=m_size)
00041     {
00042         m_wrapped = true;
00043         m_current = 0;
00044     }
00045     m_all_deltas[m_current] = delta;
00046 }   // StoreDT
00047 
00048 //-----------------------------------------------------------------------------
00049 float History::GetNextDelta()
00050 {
00051     m_current++;
00052     if(m_current>=m_size)
00053     {
00054         fprintf(stderr,"History: finished.\n");
00055         exit(-3);
00056     }
00057     return m_all_deltas[m_current];
00058 }   // GetNextDT
00059 
00060 //-----------------------------------------------------------------------------
00061 void History::Save()
00062 {
00063     FILE *fd = fopen("history.dat","w");
00064     int  nKarts = race_manager->getNumKarts();
00065 #ifdef VERSION
00066     fprintf(fd, "Version:  %s\n",   VERSION);
00067 #endif
00068     fprintf(fd, "numkarts: %d\n",   nKarts);
00069     fprintf(fd, "numplayers: %d\n", race_manager->getNumPlayers());
00070     fprintf(fd, "difficulty: %d\n", race_manager->getDifficulty());
00071     fprintf(fd, "track: %s\n",      world->getTrack()->getIdent().c_str());
00072 
00073     int k;
00074     for(k=0; k<nKarts; k++)
00075     {
00076         fprintf(fd, "model %d: %s\n",k, world->getKart(k)->getName().c_str());
00077     }
00078     fprintf(fd, "size:     %d\n", GetCount());
00079 
00080     int j = m_wrapped ? m_current : 0;
00081     for(int i=0; i<GetCount(); i++)
00082     {
00083         fprintf(fd, "delta: %f\n",m_all_deltas[i]);
00084         j=(j+1)%m_size;
00085     }
00086 
00087     for(int k=0; k<nKarts; k++)
00088     {
00089         Kart* kart= world->getKart(k);
00090         char s[1024];
00091         j = m_wrapped ? m_current : 0;
00092         for(int i=0; i<GetCount(); i++)
00093         {
00094             kart->WriteHistory(s, k, j);
00095             fprintf(fd, "%s\n",s);
00096             j=(j+1)%m_size;
00097         }   // for i
00098     }   // for k
00099     fprintf(fd, "History file end.\n");
00100     fclose(fd);
00101 }   // Save
00102 
00103 //-----------------------------------------------------------------------------
00104 void History::Load()
00105 {
00106     char s[1024], s1[1024];
00107     int  n, numKarts;
00108     m_fd = fopen("history.dat","r");
00109 
00110     fgets(s, 1023, m_fd);
00111     if(sscanf(s,"Version: %s",s1)!=1)
00112     {
00113         fprintf(stderr, "WARNING: no Version information found in history file.\n");
00114         exit(-2);
00115     }
00116 #ifdef VERSION
00117     if(strcmp(s1,VERSION))
00118     {
00119         fprintf(stderr, "WARNING: history is version '%s'\n",s1);
00120         fprintf(stderr, "         tuxracer version is '%s'\n",VERSION);
00121     }
00122 #endif
00123     fgets(s, 1023, m_fd);
00124     if(sscanf(s, "numkarts: %d",&numKarts)!=1)
00125     {
00126         fprintf(stderr,"WARNING: No number of karts found in history file.\n");
00127         exit(-2);
00128     }
00129     race_manager->setNumKarts(numKarts);
00130 
00131     fgets(s, 1023, m_fd);
00132     if(sscanf(s, "numplayers: %d",&n)!=1)
00133     {
00134         fprintf(stderr,"WARNING: No number of players found in history file.\n");
00135         exit(-2);
00136     }
00137     race_manager->setNumPlayers(n);
00138 
00139     fgets(s, 1023, m_fd);
00140     if(sscanf(s, "difficulty: %d",&n)!=1)
00141     {
00142         fprintf(stderr,"WARNING: No difficulty found in history file.\n");
00143         exit(-2);
00144     }
00145     race_manager->setDifficulty((RaceManager::Difficulty)n);
00146 
00147     fgets(s, 1023, m_fd);
00148     if(sscanf(s, "track: %s",s1)!=1)
00149     {
00150         fprintf(stderr,"WARNING: Track not found in history file.\n");
00151     }
00152     race_manager->setTrack(s1);
00153     // This value doesn't really matter, but should be defined, otherwise
00154     // the racing phase can switch to 'ending'
00155     race_manager->setNumLaps(10);
00156 
00157     for(int i=0; i<numKarts; i++)
00158     {
00159         fgets(s, 1023, m_fd);
00160         if(sscanf(s, "model %d: %s",&n, s1)!=2)
00161         {
00162             fprintf(stderr,"WARNING: No model information for kart %d found.\n",
00163                     i);
00164             exit(-2);
00165         }
00166     }   // for i<nKarts
00167     // JH: The model information is currently ignored
00168     fgets(s, 1023, m_fd);
00169     if(sscanf(s,"size: %d",&n)!=1)
00170     {
00171         fprintf(stderr,"WARNING: Number of records not found in history file.\n");
00172         exit(-2);
00173     }
00174     SetSize(n);
00175     for(int i=0; i<m_size; i++)
00176     {
00177         fgets(s, 1023, m_fd);
00178         sscanf(s, "delta: %f\n",m_all_deltas+i);
00179     }
00180     m_current = -1;
00181 }   // Load
00182 
00183 //-----------------------------------------------------------------------------
00184 void History::LoadKartData(Kart* k, int kartNumber)
00185 {
00186     char s[1024];
00187     for(int i=0; i<m_size; i++)
00188     {
00189         fgets(s, 1023, m_fd);
00190         k->ReadHistory(s, kartNumber, i);
00191     }   // for i<m_current
00192 }   // LoadKartData
00193