Back to index

supertuxkart  0.5+dfsg1
challenge.cpp
Go to the documentation of this file.
00001 //  $Id: challenge.cpp 1259 2007-09-24 12:28:19Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2008 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 "translation.hpp"
00021 #include "challenges/challenge.hpp"
00022 #include "world.hpp"
00023 #include "race_manager.hpp"
00024 #include "track_manager.hpp"
00025 #if defined(WIN32) && !defined(__CYGWIN__)
00026 #  define snprintf _snprintf
00027 #endif
00028 
00029 
00030 Challenge::Challenge(std::string id, std::string name) : 
00031     m_state(CH_INACTIVE), m_Id(id), m_Name(name)
00032 {
00033 }   // Challenge
00034 
00035 //-----------------------------------------------------------------------------
00039 void Challenge::load(const lisp::Lisp* config)
00040 {
00041     const lisp::Lisp* subnode= config->getLisp(getId());
00042     if(!subnode) return;
00043     
00044     // See if the challenge is solved (it's activated later from the
00045     // unlock_manager).
00046     bool finished=false;
00047     subnode->get("solved", finished);
00048     m_state = finished ? CH_SOLVED : CH_INACTIVE;
00049     if(!finished) loadState(subnode);
00050 }   // load
00051 
00052 //-----------------------------------------------------------------------------
00053 void Challenge::save(lisp::Writer* writer)
00054 {
00055     writer->beginList(getId());
00056     writer->write("solved", isSolved());
00057     if(!isSolved()) saveState(writer);
00058     writer->endList(getId());
00059 }   // save
00060 
00061 //-----------------------------------------------------------------------------
00062 void Challenge::addUnlockTrackReward(std::string track_name)
00063 {
00064     UnlockableFeature feature;
00065     feature.name = track_name;
00066     feature.type = UNLOCK_TRACK;
00067     m_feature.push_back(feature);
00068 }
00069 //-----------------------------------------------------------------------------
00070 void Challenge::addUnlockModeReward(std::string internal_mode_name, std::string user_mode_name)
00071 {
00072     UnlockableFeature feature;
00073     feature.name = internal_mode_name;
00074     feature.type = UNLOCK_MODE;
00075     feature.user_name = user_mode_name;
00076     m_feature.push_back(feature);
00077 }
00078 //-----------------------------------------------------------------------------
00079 void Challenge::addUnlockGPReward(std::string gp_name)
00080 {
00081     UnlockableFeature feature;
00082     feature.name = _(gp_name.c_str());
00083     feature.type = UNLOCK_GP;
00084     m_feature.push_back(feature);
00085 }
00086 //-----------------------------------------------------------------------------
00087 void Challenge::addUnlockDifficultyReward(std::string internal_name, std::string user_name)
00088 {
00089     UnlockableFeature feature;
00090     feature.name = internal_name;
00091     feature.type = UNLOCK_DIFFICULTY;
00092     feature.user_name = user_name;
00093     m_feature.push_back(feature);
00094 }
00095 //-----------------------------------------------------------------------------
00096 const std::string Challenge::getUnlockedMessage() const
00097 {
00098     std::string unlocked_message;
00099     
00100     const unsigned int amount = (unsigned int)m_feature.size();
00101     for(unsigned int n=0; n<amount; n++)
00102     {
00103         // add line break if we are showing multiple messages
00104         if(n>0) unlocked_message+='\n';
00105         
00106         char message[128];
00107         
00108         // write message depending on feature type
00109         switch(m_feature[n].type)
00110         {
00111             case UNLOCK_TRACK:
00112                 {
00113                     Track* track = track_manager->getTrack( m_feature[n].name );
00114                     snprintf(message, 127, _("New track '%s'\nnow available"), gettext(track->getName()) );
00115                     break;
00116                 }
00117             case UNLOCK_MODE:
00118                 snprintf(message, 127, _("New game mode\n'%s'\nnow available"), m_feature[n].user_name.c_str() );
00119                 break;
00120             case UNLOCK_GP:
00121                 snprintf(message, 127, _("New Grand Prix '%s'\nnow available"), m_feature[n].name.c_str() );
00122                 break;
00123             case UNLOCK_DIFFICULTY:
00124                 snprintf(message, 127, _("New difficulty\n'%s'\nnow available"), m_feature[n].user_name.c_str() );
00125                 break;
00126         }
00127         unlocked_message += message;
00128     }
00129     
00130     return unlocked_message;
00131 }