Back to index

extremetuxracer  0.5beta
game_mgr.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (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; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #include "game_mgr.h"
00021 #include "game_config.h"
00022 
00023 #include "ppgltk/alg/defs.h"
00024 
00025 #include "SDL.h"
00026 
00027 GameMgr* gameMgr = NULL;
00028 
00029 GameMgr::GameMgr()
00030 {
00031        m_lastTicks = SDL_GetTicks()/1000.0;
00032     m_raceHasToBeRetried = false;
00033 
00034 }
00035 
00036 
00037 void
00038 GameMgr::setupEventAndCup( std::list<EventData>::iterator event,
00039                                           std::list<CupData>::iterator cup)
00040 {
00041        currentEvent=event;
00042        currentCup=cup;                                                
00043 }
00044 
00045 void
00046 GameMgr::setCurrentRace(std::list<CourseData>::iterator race)
00047 {
00048        mi_currentRace = race;
00049 }
00050 
00051 void
00052 GameMgr::reset(gametype_t type)
00053 {
00054        gametype=type;
00055 }
00056 
00057 unsigned int
00058 GameMgr::calculateScore(double time, int herring, int health)
00059 {
00060        int timescore = MAX( 0, int(100*((*mi_currentRace).time_req[DIFFICULTY_LEVEL_EASY]-time)));
00061        int herringscore = 200*herring;
00062        
00063        //for future use
00064        int healthscore = 0*health;
00065        
00066        return timescore+herringscore+healthscore;
00067 }
00068 
00069 
00070 void
00071 GameMgr::updatePlayersScores()
00072 {
00073        for (int i=0; i<numPlayers; i++){
00074               players[i].score=calculateScore(time,
00075                                                                       players[i].herring,
00076                                                                       players[i].health);
00077        }
00078 }
00079 
00080 bool
00081 GameMgr::updateCurrentRaceData()
00082 {
00083        m_eventWon=false;
00084        m_cupWon=false;
00085        
00086        if(    time <= (*mi_currentRace).time_req[difficulty] &&
00087               players[0].herring >= (*mi_currentRace).herring_req[difficulty] &&
00088               players[0].score >= (*mi_currentRace).score_req[difficulty] &&
00089               !wasRaceAborted() ) 
00090     {
00091               m_raceWon=true;             
00092        } else {
00093               m_raceWon=false;
00094     }  
00095        
00096        bool bestScore = players[0].updateCupCourseData(
00097                                    (*currentEvent).name,
00098                                    (*currentCup).name,
00099                                    (*mi_currentRace).name,
00100                                    time,
00101                                    players[0].herring,
00102                                    players[0].score,
00103                                    m_raceWon);
00104        
00105        if(m_raceWon){       
00106               std::list<CourseData>::iterator lastRace=(*currentCup).raceList.end();
00107               lastRace--;
00108               
00109               if(mi_currentRace == lastRace){
00110                      m_cupWon = true;
00111                      players[0].setCupComplete(  (*currentEvent).name,
00112                                                                       (*currentCup).name);
00113                      std::list<CupData>::iterator lastCup=(*currentEvent).cupList.end();
00114                      lastCup--;
00115                      if(currentCup == lastCup){
00116                             m_eventWon = true;
00117                             players[0].setEventComplete((*currentEvent).name);
00118                      }
00119                      if(!getparam_always_save_event_race_data()){
00120                             players[0].saveData();
00121                      }
00122               }
00123        }
00124        if(getparam_always_save_event_race_data()){
00125               players[0].saveData();
00126        }
00127        return bestScore;
00128 }
00129 
00130 bool
00131 GameMgr::wasRaceWon()
00132 {
00133        return m_raceWon;    
00134 }
00135 
00136 bool
00137 GameMgr::wasCupWon()
00138 {
00139        return m_cupWon;     
00140 }
00141 
00142 bool
00143 GameMgr::wasEventWon()
00144 {
00145        return m_eventWon;   
00146 }
00147 
00148 bool GameMgr::doesRaceHaveToBeRetried() 
00149 {
00150     return m_raceHasToBeRetried;
00151 }
00152 
00153 void GameMgr::setRaceHasToBeRetried(bool b) {
00154     m_raceHasToBeRetried = b;
00155 }
00156 
00157 
00158 void
00159 GameMgr::resetTimeStep()
00160 {
00161        m_lastTicks = SDL_GetTicks()/1000.0;
00162 }
00163 
00164 void
00165 GameMgr::updateTimeStep()
00166 {
00167        double ticks = SDL_GetTicks()/1000.0;
00168        timeStep = ticks - m_lastTicks;
00169        timeStep = MAX(timeStep,EPS);
00170        m_lastTicks = ticks;
00171 }