Back to index

supertuxkart  0.5+dfsg1
race_options.cpp
Go to the documentation of this file.
00001 //  SuperTuxKart - a fun racing game with go-kart
00002 //  Copyright (C) 2006 SuperTuxKart-Team
00003 //
00004 //  This program is free software; you can redistribute it and/or
00005 //  modify it under the terms of the GNU General Public License
00006 //  as published by the Free Software Foundation; either version 2
00007 //  of the License, or (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 
00018 #include "race_manager.hpp"
00019 #include "race_options.hpp"
00020 #include "widget_manager.hpp"
00021 #include "user_config.hpp"
00022 #include "menu_manager.hpp"
00023 #include "material_manager.hpp"
00024 #include "unlock_manager.hpp"
00025 #include "translation.hpp"
00026 #if defined(WIN32) && !defined(__CYGWIN__)
00027 #  define snprintf _snprintf
00028 #endif
00029 
00030 enum WidgetTokens
00031 {
00032     WTOK_TITLE,
00033 
00034     WTOK_DIFFICULTY_TITLE,
00035     WTOK_DIFFICULTY_UP,
00036     WTOK_DIFFICULTY,
00037     WTOK_DIFFICULTY_DOWN,
00038 
00039     WTOK_KARTS_TITLE,
00040     WTOK_KARTS_UP,
00041     WTOK_KARTS,
00042     WTOK_KARTS_DOWN,
00043 
00044     WTOK_LAPS_TITLE,
00045     WTOK_LAPS_UP,
00046     WTOK_LAPS,
00047     WTOK_LAPS_DOWN,
00048 
00049     WTOK_START,
00050 
00051     WTOK_QUIT
00052 };
00053 
00054 RaceOptions::RaceOptions() 
00055 {
00056     m_difficulty=race_manager->getDifficulty();
00057     // FIXME: no medium AI atm
00058     if(m_difficulty==RaceManager::RD_MEDIUM) m_difficulty=RaceManager::RD_HARD;
00059     m_num_karts=race_manager->getNumKarts();
00060     m_num_laps=race_manager->getNumLaps();
00061 
00062     const int DESC_WIDTH=48;
00063     const int ITEM_WIDTH=35;
00064 
00065     // Difficulty
00066     // ==========
00067     widget_manager->addTextWgt( WTOK_DIFFICULTY_TITLE, DESC_WIDTH, 7, _("Difficulty") );
00068     widget_manager->hideWgtRect(WTOK_DIFFICULTY_TITLE);
00069     widget_manager->setWgtTextSize(WTOK_DIFFICULTY_TITLE, WGT_FNT_LRG);
00070     widget_manager->addTextButtonWgt( WTOK_DIFFICULTY_DOWN, 3, 7, " < " );
00071 
00072     widget_manager->addTextWgt( WTOK_DIFFICULTY, ITEM_WIDTH, 7, getDifficultyString(m_difficulty));
00073     widget_manager->setWgtBorderPercentage( WTOK_DIFFICULTY, 10 );
00074     widget_manager->showWgtBorder( WTOK_DIFFICULTY );
00075     widget_manager->hideWgtRect( WTOK_DIFFICULTY );
00076 
00077     widget_manager->addTextButtonWgt( WTOK_DIFFICULTY_UP, 3, 7, " > " );
00078 
00079     widget_manager->breakLine();
00080 
00081     // Number of karts
00082     // ===============
00083     widget_manager->addTextWgt( WTOK_KARTS_TITLE, DESC_WIDTH, 7, _("Number of karts") );
00084     widget_manager->hideWgtRect(WTOK_KARTS_TITLE);
00085     widget_manager->setWgtTextSize(WTOK_KARTS_TITLE, WGT_FNT_LRG);
00086     widget_manager->addTextButtonWgt( WTOK_KARTS_DOWN, 3, 7, " < " );
00087 
00088     char string_num_karts[MAX_MESSAGE_LENGTH];
00089     snprintf(string_num_karts, MAX_MESSAGE_LENGTH, "%d", m_num_karts);
00090     widget_manager->addTextWgt( WTOK_KARTS, ITEM_WIDTH, 7, string_num_karts );
00091     widget_manager->setWgtBorderPercentage( WTOK_KARTS, 10 );
00092     widget_manager->showWgtBorder( WTOK_KARTS );
00093     widget_manager->hideWgtRect( WTOK_KARTS );
00094 
00095     widget_manager->addTextButtonWgt( WTOK_KARTS_UP, 3, 7, " > " );
00096 
00097     widget_manager->breakLine();
00098 
00099     // Number of laps
00100     // ==============
00101     if( race_manager->getRaceMode() != RaceManager::RM_GRAND_PRIX   &&
00102         race_manager->getRaceMode() != RaceManager::RM_FOLLOW_LEADER   )
00103     {
00104         widget_manager->addTextWgt( WTOK_LAPS_TITLE, DESC_WIDTH, 7, _("Number of laps") );
00105         widget_manager->hideWgtRect(WTOK_LAPS_TITLE);
00106         widget_manager->setWgtTextSize(WTOK_LAPS_TITLE, WGT_FNT_LRG);
00107         widget_manager->addTextButtonWgt( WTOK_LAPS_DOWN, 3, 7, " < " );
00108 
00109         char string_num_laps[MAX_MESSAGE_LENGTH];
00110         snprintf(string_num_laps, MAX_MESSAGE_LENGTH, "%d", m_num_laps);
00111         widget_manager->addTextWgt( WTOK_LAPS, ITEM_WIDTH, 7, string_num_laps );
00112         widget_manager->setWgtBorderPercentage( WTOK_LAPS, 10 );
00113         widget_manager->showWgtBorder( WTOK_LAPS );
00114         widget_manager->hideWgtRect( WTOK_LAPS );
00115 
00116         widget_manager->addTextButtonWgt( WTOK_LAPS_UP, 3, 7, " > " );
00117     }
00118     widget_manager->breakLine();
00119     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 10, 10 );
00120     widget_manager->breakLine();
00121 
00122     //Lower buttons
00123     //=============
00124     widget_manager->addTextButtonWgt( WTOK_START, 60, 7, _("Start race") );
00125     widget_manager->setWgtBorderPercentage( WTOK_START, 20 );
00126     widget_manager->setWgtBorderColor( WTOK_START, WGT_TRANS_BLUE );
00127     widget_manager->showWgtBorder( WTOK_START );
00128     widget_manager->breakLine();
00129     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 1, 10);
00130 
00131     widget_manager->addTextButtonWgt( WTOK_QUIT, 60, 7, _("Press <ESC> to go back") );
00132 
00133     widget_manager->layout(WGT_AREA_ALL);
00134 }   // RaceOptions
00135 
00136 //-----------------------------------------------------------------------------
00137 RaceOptions::~RaceOptions()
00138 {
00139     widget_manager->reset();
00140 }   // ~RaceOptions
00141 
00142 //-----------------------------------------------------------------------------
00143 void RaceOptions::select()
00144 {
00145     switch ( widget_manager->getSelectedWgt() )
00146     {
00147         case WTOK_DIFFICULTY_UP:
00148             if( m_difficulty == RaceManager::RD_HARD && !unlock_manager->isLocked("skidding"))
00149             {
00150                 m_difficulty = RaceManager::RD_SKIDDING;
00151             }
00152             else if( m_difficulty == RaceManager::RD_MEDIUM )
00153             {
00154                 m_difficulty = RaceManager::RD_HARD;
00155             }
00156             else if( m_difficulty == RaceManager::RD_EASY )
00157             {
00158 //TEMP: done just for the release after 0.4 because of AI problems
00159 #undef ENABLE_MEDIUM_AI
00160 #if ENABLE_MEDIUM_AI
00161                 m_difficulty = RaceManager::RD_MEDIUM;
00162 #else
00163                 m_difficulty = RaceManager::RD_HARD;
00164 #endif
00165             }
00166             widget_manager->setWgtText( WTOK_DIFFICULTY, getDifficultyString(m_difficulty) );
00167             break;
00168 
00169         case WTOK_DIFFICULTY_DOWN:
00170             if( m_difficulty == RaceManager::RD_SKIDDING )
00171             {
00172                 m_difficulty = RaceManager::RD_HARD;
00173             }
00174 
00175             else if( m_difficulty == RaceManager::RD_HARD )
00176             {
00177 //TEMP: done just for the release after 0.4 because of AI problems
00178 #if ENABLE_MEDIUM_AI
00179                 m_difficulty = RaceManager::RD_MEDIUM;
00180 #else
00181                 m_difficulty = RaceManager::RD_EASY;
00182 #endif
00183             }
00184             else if( m_difficulty == RaceManager::RD_MEDIUM )
00185             {
00186                 m_difficulty = RaceManager::RD_EASY;
00187             }
00188             widget_manager->setWgtText( WTOK_DIFFICULTY, getDifficultyString(m_difficulty) );
00189 
00190             break;
00191 
00192         case WTOK_KARTS_UP:
00193             {
00194                 m_num_karts = std::min( stk_config->m_max_karts, m_num_karts + 1 );
00195 
00196                 char label[ MAX_MESSAGE_LENGTH ];
00197                 snprintf( label, MAX_MESSAGE_LENGTH, "%d", m_num_karts );
00198 
00199                 widget_manager->setWgtText( WTOK_KARTS, label );
00200             }
00201             break;
00202 
00203         case WTOK_KARTS_DOWN:
00204             {
00205                 // Follow the leader needs at least three karts
00206                 if(race_manager->getRaceMode() == RaceManager::RM_FOLLOW_LEADER)
00207                 {
00208                     m_num_karts = std::max( 3, m_num_karts - 1 );
00209                 }
00210                 else
00211                 {
00212                     m_num_karts = std::max( (int)race_manager->getNumPlayers(),
00213                         m_num_karts - 1 );
00214                 }
00215 
00216                 char label[ MAX_MESSAGE_LENGTH ];
00217                 snprintf( label, MAX_MESSAGE_LENGTH, "%d", m_num_karts );
00218 
00219                 widget_manager->setWgtText( WTOK_KARTS, label );
00220             }
00221             break;
00222 
00223         case WTOK_LAPS_UP:
00224             {
00225                 m_num_laps = std::min( 10, m_num_laps + 1 );
00226 
00227                 char label[ MAX_MESSAGE_LENGTH ];
00228                 snprintf( label, MAX_MESSAGE_LENGTH, "%d", m_num_laps );
00229 
00230                 widget_manager->setWgtText( WTOK_LAPS, label);
00231             }
00232             break;
00233 
00234         case WTOK_LAPS_DOWN:
00235             {
00236                 m_num_laps = std::max( 1, m_num_laps - 1 );
00237 
00238                 char label[ MAX_MESSAGE_LENGTH ];
00239                 snprintf( label, MAX_MESSAGE_LENGTH, "%d", m_num_laps );
00240 
00241                 widget_manager->setWgtText( WTOK_LAPS, label);
00242             }
00243             break;
00244 
00245     case WTOK_START:
00246         if( m_difficulty >= RaceManager::RD_EASY &&
00247             m_difficulty <= RaceManager::RD_SKIDDING)
00248         {
00249             race_manager->setDifficulty((RaceManager::Difficulty)m_difficulty);
00250         }
00251         else // invalid difficulty
00252         {
00253             race_manager->setDifficulty( RaceManager::RD_EASY );
00254         }
00255 
00256         race_manager->setNumKarts(m_num_karts);
00257 
00258         if( race_manager->getRaceMode() != RaceManager::RM_GRAND_PRIX    &&
00259             race_manager->getRaceMode() != RaceManager::RM_FOLLOW_LEADER    )
00260         {
00261             race_manager->setNumLaps( m_num_laps );
00262         }
00263 
00264         menu_manager->pushMenu(MENUID_START_RACE_FEEDBACK);
00265         break;
00266     case WTOK_QUIT:
00267         menu_manager->popMenu();
00268         break;
00269     default: break;
00270     }   // switch
00271 }   // select
00272 
00273 // ----------------------------------------------------------------------------
00274 const char *RaceOptions::getDifficultyString(int difficulty) const
00275 {
00276     switch(difficulty)
00277     {
00278     case RaceManager::RD_EASY:     return _("Novice");
00279     case RaceManager::RD_MEDIUM:   return _("Driver");
00280     case RaceManager::RD_HARD:     return _("Racer" );
00281     case RaceManager::RD_SKIDDING: return _("Skidding Preview");
00282     default:                       return _("Novice");
00283 
00284     }   // switch
00285 }   // getDifficultyString