2008-09-07 09:25:58 -04:00
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2006 SuperTuxKart-Team
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "race/race_manager.hpp"
|
2008-09-13 03:09:36 -04:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
#include <iostream>
|
2010-08-07 21:02:26 -04:00
|
|
|
#include <algorithm>
|
2008-09-07 09:25:58 -04:00
|
|
|
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "challenges/unlock_manager.hpp"
|
2009-06-11 06:00:43 -04:00
|
|
|
#include "config/user_config.hpp"
|
|
|
|
#include "config/stk_config.hpp"
|
2009-07-07 20:46:47 -04:00
|
|
|
#include "input/device_manager.hpp"
|
|
|
|
#include "input/input_manager.hpp"
|
2008-11-06 23:34:01 -05:00
|
|
|
#include "karts/kart_properties_manager.hpp"
|
2008-09-20 19:45:22 -04:00
|
|
|
#include "modes/follow_the_leader.hpp"
|
2009-08-20 20:32:24 -04:00
|
|
|
#include "modes/profile_world.hpp"
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "modes/standard_race.hpp"
|
|
|
|
#include "modes/world.hpp"
|
2008-10-14 16:20:54 -04:00
|
|
|
#include "modes/three_strikes_battle.hpp"
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "network/network_manager.hpp"
|
2010-05-30 11:45:44 -04:00
|
|
|
#include "states_screens/grand_prix_lose.hpp"
|
|
|
|
#include "states_screens/grand_prix_win.hpp"
|
2010-02-20 21:22:05 -05:00
|
|
|
#include "states_screens/kart_selection.hpp"
|
|
|
|
#include "states_screens/main_menu_screen.hpp"
|
|
|
|
#include "states_screens/state_manager.hpp"
|
2009-01-22 07:02:40 -05:00
|
|
|
#include "tracks/track_manager.hpp"
|
2011-02-02 17:26:01 -05:00
|
|
|
#include "utils/ptr_vector.hpp"
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
RaceManager* race_manager= NULL;
|
|
|
|
|
2008-09-09 09:43:09 -04:00
|
|
|
/** Constructs the race manager.
|
|
|
|
*/
|
2008-09-07 09:25:58 -04:00
|
|
|
RaceManager::RaceManager()
|
|
|
|
{
|
2009-06-20 21:10:43 -04:00
|
|
|
m_num_karts = UserConfigParams::m_num_karts;
|
2008-09-07 09:25:58 -04:00
|
|
|
m_difficulty = RD_HARD;
|
2008-09-21 20:55:27 -04:00
|
|
|
m_major_mode = MAJOR_MODE_SINGLE;
|
2009-12-22 16:57:15 -05:00
|
|
|
m_minor_mode = MINOR_MODE_NORMAL_RACE;
|
2008-09-07 09:25:58 -04:00
|
|
|
m_track_number = 0;
|
|
|
|
m_coin_target = 0;
|
2008-11-19 17:30:51 -05:00
|
|
|
setTrack("jungle");
|
2009-07-18 11:21:16 -04:00
|
|
|
setNumLocalPlayers(0);
|
|
|
|
//setLocalKartInfo(0, "tux");
|
2008-09-07 09:25:58 -04:00
|
|
|
} // RaceManager
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
RaceManager::~RaceManager()
|
|
|
|
{
|
|
|
|
} // ~RaceManager
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::reset()
|
|
|
|
{
|
|
|
|
m_num_finished_karts = 0;
|
|
|
|
m_num_finished_players = 0;
|
|
|
|
} // reset
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 10:34:04 -04:00
|
|
|
void RaceManager::setPlayerKart(unsigned int player_id, const RemoteKartInfo& ki)
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
2008-09-07 10:34:04 -04:00
|
|
|
m_player_karts[player_id] = ki;
|
|
|
|
} // setPlayerKart
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 10:34:04 -04:00
|
|
|
void RaceManager::setLocalKartInfo(unsigned int player_id, const std::string& kart)
|
|
|
|
{
|
2011-06-12 13:35:13 -04:00
|
|
|
assert(kart.size() > 0);
|
2010-03-07 13:42:04 -05:00
|
|
|
assert(0<=player_id && player_id <m_local_player_karts.size());
|
2010-12-05 11:12:35 -05:00
|
|
|
assert(kart_properties_manager->getKart(kart) != NULL);
|
2008-09-07 10:34:04 -04:00
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
m_local_player_karts[player_id] = RemoteKartInfo(player_id, kart,
|
2010-02-18 16:28:32 -05:00
|
|
|
StateManager::get()->getActivePlayerProfile(player_id)->getName(),
|
2009-07-18 11:21:16 -04:00
|
|
|
network_manager->getMyHostId());
|
2008-09-07 10:34:04 -04:00
|
|
|
} // setLocalKartInfo
|
2008-09-07 09:33:12 -04:00
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2010-07-29 19:03:20 -04:00
|
|
|
const Kart *RaceManager::getKartWithGPRank(unsigned int n)
|
|
|
|
{
|
|
|
|
for(unsigned int i=0; i<m_kart_status.size(); i++)
|
2010-08-29 20:21:04 -04:00
|
|
|
if(m_kart_status[i].m_gp_rank == (int)n)
|
2010-07-29 19:03:20 -04:00
|
|
|
return World::getWorld()->getKart(i);
|
|
|
|
return NULL;
|
|
|
|
} // getKLartWithGPRank
|
2010-03-07 13:42:04 -05:00
|
|
|
|
2010-07-29 19:03:20 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
int RaceManager::getLocalPlayerGPRank(const int playerID) const
|
|
|
|
{
|
|
|
|
const int amount = m_kart_status.size();
|
|
|
|
for (int n=0; n<amount; n++)
|
|
|
|
{
|
|
|
|
if (m_kart_status[n].m_local_player_id == playerID)
|
|
|
|
{
|
|
|
|
return m_kart_status[n].m_gp_rank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2010-07-22 18:51:14 -04:00
|
|
|
} // getLocalPlayerGPRank
|
2010-03-07 13:42:04 -05:00
|
|
|
|
2008-09-07 09:33:12 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 10:34:04 -04:00
|
|
|
void RaceManager::setNumLocalPlayers(unsigned int n)
|
2008-09-07 09:33:12 -04:00
|
|
|
{
|
2010-03-07 13:42:04 -05:00
|
|
|
m_local_player_karts.resize(n);
|
2008-09-07 10:34:04 -04:00
|
|
|
} // setNumLocalPlayers
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 09:43:09 -04:00
|
|
|
/** Sets the number of players.
|
|
|
|
* \param num Number of players.
|
|
|
|
*/
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::setNumPlayers(int num)
|
|
|
|
{
|
|
|
|
m_player_karts.resize(num);
|
|
|
|
} // setNumPlayers
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::setDifficulty(Difficulty diff)
|
|
|
|
{
|
2008-11-24 03:22:34 -05:00
|
|
|
m_difficulty = diff;
|
2008-09-07 09:25:58 -04:00
|
|
|
} // setDifficulty
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::setTrack(const std::string& track)
|
|
|
|
{
|
|
|
|
m_tracks.clear();
|
|
|
|
m_tracks.push_back(track);
|
2010-12-30 21:24:44 -05:00
|
|
|
|
|
|
|
m_coin_target = 0;
|
2008-09-07 09:25:58 -04:00
|
|
|
} // setTrack
|
|
|
|
|
2008-09-07 10:24:40 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
/** \brief Computes the list of random karts to be used for the AI. */
|
2008-09-07 10:24:40 -04:00
|
|
|
void RaceManager::computeRandomKartList()
|
|
|
|
{
|
2008-09-09 09:43:09 -04:00
|
|
|
int n = m_num_karts - m_player_karts.size();
|
2010-03-24 19:52:41 -04:00
|
|
|
if(UserConfigParams::m_verbosity>=5)
|
|
|
|
std::cout << "AI karts count = " << n << " for m_num_karts="
|
|
|
|
<< m_num_karts << " and m_player_karts.size()="
|
|
|
|
<< m_player_karts.size() << std::endl;
|
|
|
|
|
2008-09-09 09:43:09 -04:00
|
|
|
// If less kart selected than there are player karts, adjust the number of
|
|
|
|
// karts to the minimum
|
|
|
|
if(n<0)
|
|
|
|
{
|
|
|
|
m_num_karts -= n;
|
|
|
|
n = 0;
|
|
|
|
}
|
2010-09-21 03:37:42 -04:00
|
|
|
m_ai_kart_list=kart_properties_manager->getRandomKartList(n, m_player_karts);
|
2008-09-07 10:24:40 -04:00
|
|
|
|
|
|
|
} // computeRandomKartList
|
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::startNew()
|
|
|
|
{
|
2008-09-21 20:55:27 -04:00
|
|
|
if(m_major_mode==MAJOR_MODE_GRAND_PRIX) // GP: get tracks and laps from grand prix
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
|
|
|
m_tracks = m_grand_prix.getTracks();
|
|
|
|
m_num_laps = m_grand_prix.getLaps();
|
|
|
|
}
|
|
|
|
assert(m_player_karts.size() > 0);
|
|
|
|
|
|
|
|
// command line parameters: negative numbers=all karts
|
|
|
|
if(m_num_karts < 0 ) m_num_karts = stk_config->m_max_karts;
|
|
|
|
if((size_t)m_num_karts < m_player_karts.size())
|
|
|
|
m_num_karts = (int)m_player_karts.size();
|
|
|
|
|
|
|
|
// Create the kart status data structure to keep track of scores, times, ...
|
|
|
|
// ==========================================================================
|
|
|
|
m_kart_status.clear();
|
2008-09-07 10:24:40 -04:00
|
|
|
|
2010-09-21 03:37:42 -04:00
|
|
|
// Before thi
|
2010-09-21 06:28:30 -04:00
|
|
|
assert((unsigned int)m_num_karts == m_ai_kart_list.size()+m_player_karts.size());
|
2008-09-07 10:24:40 -04:00
|
|
|
// First add the AI karts (randomly chosen)
|
|
|
|
// ----------------------------------------
|
2010-12-19 20:16:03 -05:00
|
|
|
|
|
|
|
// GP ranks start with -1 for the leader.
|
|
|
|
int init_gp_rank =
|
|
|
|
race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER
|
|
|
|
? -1
|
|
|
|
: 0;
|
2010-09-21 03:37:42 -04:00
|
|
|
const unsigned int ai_kart_count = m_ai_kart_list.size();
|
2010-09-20 13:09:25 -04:00
|
|
|
for(unsigned int i=0; i<ai_kart_count; i++)
|
2010-07-29 19:03:20 -04:00
|
|
|
{
|
2010-09-21 03:37:42 -04:00
|
|
|
m_kart_status.push_back(KartStatus(m_ai_kart_list[i], i, -1, -1,
|
2010-07-29 19:03:20 -04:00
|
|
|
init_gp_rank, KT_AI));
|
|
|
|
init_gp_rank ++;
|
2011-03-21 02:15:39 -04:00
|
|
|
if(UserConfigParams::m_ftl_debug)
|
|
|
|
{
|
|
|
|
printf("[ftl] rank %d ai-kart %s\n", init_gp_rank,
|
|
|
|
m_ai_kart_list[i].c_str());
|
|
|
|
}
|
2010-07-29 19:03:20 -04:00
|
|
|
}
|
2008-09-07 10:24:40 -04:00
|
|
|
|
|
|
|
// Then the players, which start behind the AI karts
|
|
|
|
// -------------------------------------------------
|
|
|
|
for(int i=m_player_karts.size()-1; i>=0; i--)
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
2008-09-07 10:24:40 -04:00
|
|
|
KartType kt=(m_player_karts[i].getHostId()==network_manager->getMyHostId())
|
|
|
|
? KT_PLAYER : KT_NETWORK_PLAYER;
|
|
|
|
m_kart_status.push_back(KartStatus(m_player_karts[i].getKartName(), i,
|
|
|
|
m_player_karts[i].getLocalPlayerId(),
|
|
|
|
m_player_karts[i].getGlobalPlayerId(),
|
2010-07-29 19:03:20 -04:00
|
|
|
init_gp_rank, kt
|
2008-09-07 09:25:58 -04:00
|
|
|
) );
|
2011-03-21 02:15:39 -04:00
|
|
|
if(UserConfigParams::m_ftl_debug)
|
|
|
|
{
|
|
|
|
printf("[ftl] rank %d kart %s\n", init_gp_rank,
|
|
|
|
m_player_karts[i].getKartName().c_str());
|
|
|
|
}
|
2010-07-29 19:03:20 -04:00
|
|
|
init_gp_rank ++;
|
2008-09-07 10:24:40 -04:00
|
|
|
}
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
// Then start the race with the first track
|
|
|
|
// ========================================
|
|
|
|
m_track_number = 0;
|
|
|
|
startNextRace();
|
|
|
|
} // startNew
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
/** \brief Starts the next (or first) race.
|
|
|
|
* It sorts the kart status data structure
|
2008-09-09 09:43:09 -04:00
|
|
|
* according to the number of points, and then creates the world().
|
|
|
|
*/
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::startNextRace()
|
|
|
|
{
|
2010-12-08 17:18:55 -05:00
|
|
|
// Uncomment to debug audio leaks
|
|
|
|
// sfx_manager->dump();
|
|
|
|
|
2010-09-15 22:31:20 -04:00
|
|
|
stk_config->getAllScores(&m_score_for_position, m_num_karts);
|
2010-02-22 20:00:21 -05:00
|
|
|
IrrlichtDevice* device = irr_driver->getDevice();
|
|
|
|
GUIEngine::renderLoading();
|
|
|
|
device->getVideoDriver()->endScene();
|
|
|
|
device->getVideoDriver()->beginScene(true, true, video::SColor(255,100,101,140));
|
|
|
|
|
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
m_num_finished_karts = 0;
|
|
|
|
m_num_finished_players = 0;
|
|
|
|
|
|
|
|
// if subsequent race, sort kart status structure
|
|
|
|
// ==============================================
|
|
|
|
if (m_track_number > 0)
|
|
|
|
{
|
|
|
|
// In follow the leader mode do not change the first kart,
|
|
|
|
// since it's always the leader.
|
2008-09-21 20:55:27 -04:00
|
|
|
int offset = (m_minor_mode==MINOR_MODE_FOLLOW_LEADER) ? 1 : 0;
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
std::sort(m_kart_status.begin()+offset, m_kart_status.end());
|
|
|
|
//reverse kart order if flagged in stk_config
|
2010-03-15 19:00:34 -04:00
|
|
|
if (stk_config->m_gp_order)
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
|
|
|
std::reverse(m_kart_status.begin()+offset, m_kart_status.end());
|
|
|
|
}
|
|
|
|
} // not first race
|
|
|
|
|
|
|
|
// the constructor assigns this object to the global
|
|
|
|
// variable world. Admittedly a bit ugly, but simplifies
|
|
|
|
// handling of objects which get created in the constructor
|
|
|
|
// and need world to be defined.
|
2009-08-23 11:42:58 -04:00
|
|
|
if (ProfileWorld::isProfileMode())
|
2010-02-11 02:11:52 -05:00
|
|
|
World::setWorld(new ProfileWorld());
|
2009-08-20 20:32:24 -04:00
|
|
|
else if(m_minor_mode==MINOR_MODE_FOLLOW_LEADER)
|
2010-02-11 02:11:52 -05:00
|
|
|
World::setWorld(new FollowTheLeaderRace());
|
2009-12-22 16:57:15 -05:00
|
|
|
else if(m_minor_mode==MINOR_MODE_NORMAL_RACE ||
|
2009-08-20 20:32:24 -04:00
|
|
|
m_minor_mode==MINOR_MODE_TIME_TRIAL)
|
2010-02-11 02:11:52 -05:00
|
|
|
World::setWorld(new StandardRace());
|
2009-08-20 20:32:24 -04:00
|
|
|
else if(m_minor_mode==MINOR_MODE_3_STRIKES)
|
2010-02-11 02:11:52 -05:00
|
|
|
World::setWorld(new ThreeStrikesBattle());
|
2009-08-20 20:32:24 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Could not create given race mode\n");
|
|
|
|
assert(0);
|
|
|
|
}
|
2010-02-11 02:11:52 -05:00
|
|
|
World::getWorld()->init();
|
2009-08-11 22:55:53 -04:00
|
|
|
// Save the current score and set last time to zero. This is necessary
|
|
|
|
// if someone presses esc after finishing a gp, and selects restart:
|
|
|
|
// The race is rerun, and the points and scores get reset ... but if
|
|
|
|
// a kart hasn't finished the race at this stage, last_score and time
|
|
|
|
// would be undefined.
|
|
|
|
for(int i=0; i<m_num_karts; i++)
|
|
|
|
{
|
|
|
|
m_kart_status[i].m_last_score = m_kart_status[i].m_score;
|
|
|
|
m_kart_status[i].m_last_time = 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
} // startNextRace
|
2010-02-11 02:11:52 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::next()
|
2010-12-30 21:24:44 -05:00
|
|
|
{
|
2010-03-01 15:17:31 -05:00
|
|
|
World::deleteWorld();
|
2008-09-07 09:25:58 -04:00
|
|
|
m_num_finished_karts = 0;
|
|
|
|
m_num_finished_players = 0;
|
|
|
|
m_track_number++;
|
|
|
|
if(m_track_number<(int)m_tracks.size())
|
|
|
|
{
|
2008-10-01 20:27:39 -04:00
|
|
|
if(network_manager->getMode()==NetworkManager::NW_SERVER)
|
|
|
|
network_manager->beginReadySetGoBarrier();
|
|
|
|
else
|
|
|
|
network_manager->setState(NetworkManager::NS_WAIT_FOR_RACE_DATA);
|
2008-09-07 09:25:58 -04:00
|
|
|
startNextRace();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-01 20:27:39 -04:00
|
|
|
// Back to main menu. Change the state of the state of the
|
|
|
|
// network manager.
|
|
|
|
if(network_manager->getMode()==NetworkManager::NW_SERVER)
|
|
|
|
network_manager->setState(NetworkManager::NS_MAIN_MENU);
|
|
|
|
else
|
|
|
|
network_manager->setState(NetworkManager::NS_WAIT_FOR_AVAILABLE_CHARACTERS);
|
2009-03-26 19:31:00 -04:00
|
|
|
exitRace();
|
2008-09-07 09:25:58 -04:00
|
|
|
}
|
|
|
|
} // next
|
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2011-02-02 17:26:01 -05:00
|
|
|
/** This class is only used in computeGPRanks, but the C++ standard
|
|
|
|
* forbids the usage of local data type in templates, so we have to
|
|
|
|
* declare it outside of the function. This class is used to store
|
|
|
|
* and compare data for determining the GP rank.
|
|
|
|
*/
|
|
|
|
namespace computeGPRanksData
|
|
|
|
{
|
|
|
|
class SortData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int m_score;
|
|
|
|
int m_position;
|
|
|
|
float m_race_time;
|
|
|
|
bool operator<(const SortData &a)
|
|
|
|
{
|
|
|
|
return ( (m_score > a.m_score) ||
|
2011-03-07 20:20:53 -05:00
|
|
|
(m_score == a.m_score && m_race_time < a.m_race_time) );
|
2011-02-02 17:26:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // SortData
|
|
|
|
} // namespace
|
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
void RaceManager::computeGPRanks()
|
|
|
|
{
|
|
|
|
// calculate the rank of each kart
|
|
|
|
const unsigned int NUM_KARTS = getNumberOfKarts();
|
2011-02-03 17:20:53 -05:00
|
|
|
PtrVector<computeGPRanksData::SortData> sort_data;
|
2011-02-02 17:04:13 -05:00
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
// Ignore the first kart if it's a follow-the-leader race.
|
2010-07-29 19:03:20 -04:00
|
|
|
int start=(race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER);
|
2011-02-02 17:04:13 -05:00
|
|
|
if (start)
|
2010-03-07 13:42:04 -05:00
|
|
|
{
|
|
|
|
// fill values for leader
|
2011-02-02 17:26:01 -05:00
|
|
|
computeGPRanksData::SortData *sd = new computeGPRanksData::SortData();
|
2011-02-02 17:04:13 -05:00
|
|
|
|
|
|
|
sd->m_position = -1;
|
|
|
|
sd->m_score = -1;
|
|
|
|
sd->m_race_time = -1;
|
|
|
|
sort_data.push_back(sd);
|
2010-03-07 13:42:04 -05:00
|
|
|
m_kart_status[0].m_gp_rank = -1;
|
2011-03-21 02:15:39 -04:00
|
|
|
if(UserConfigParams::m_ftl_debug)
|
|
|
|
{
|
|
|
|
printf("[ftl] kart '%s' has position %d.\n",
|
|
|
|
World::getWorld()->getKart(0)->getIdent().c_str(),
|
|
|
|
sd->m_position);
|
|
|
|
}
|
2010-03-07 13:42:04 -05:00
|
|
|
}
|
2011-02-02 17:04:13 -05:00
|
|
|
for (unsigned int kart_id = start; kart_id < NUM_KARTS; ++kart_id)
|
2010-03-07 13:42:04 -05:00
|
|
|
{
|
2011-02-02 17:26:01 -05:00
|
|
|
computeGPRanksData::SortData *sd = new computeGPRanksData::SortData();
|
2011-02-02 17:04:13 -05:00
|
|
|
sd->m_position = kart_id;
|
2011-03-21 02:15:39 -04:00
|
|
|
sd->m_score = getKartScore(kart_id);
|
|
|
|
sd->m_race_time = getOverallTime(kart_id);
|
2011-02-02 17:04:13 -05:00
|
|
|
sort_data.push_back(sd);
|
2011-03-21 02:15:39 -04:00
|
|
|
if(UserConfigParams::m_ftl_debug)
|
|
|
|
{
|
|
|
|
printf("[ftl] kart '%s' has position %d score %d.\n",
|
|
|
|
World::getWorld()->getKart(kart_id)->getIdent().c_str(),
|
|
|
|
sd->m_position, sd->m_score);
|
|
|
|
}
|
2011-02-02 17:04:13 -05:00
|
|
|
}
|
2010-03-07 13:42:04 -05:00
|
|
|
|
2011-02-02 17:04:13 -05:00
|
|
|
sort_data.insertionSort(start);
|
2010-03-07 13:42:04 -05:00
|
|
|
for (unsigned int i=start; i < NUM_KARTS; ++i)
|
|
|
|
{
|
2010-12-19 20:16:03 -05:00
|
|
|
//printf("setting kart %s to rank %i\n",
|
|
|
|
// m_kart_status[position[i]].m_ident.c_str(), i-start);
|
2011-03-21 02:15:39 -04:00
|
|
|
if(UserConfigParams::m_ftl_debug)
|
|
|
|
{
|
|
|
|
const Kart *kart =
|
|
|
|
World::getWorld()->getKart(sort_data[i].m_position);
|
|
|
|
printf("[ftl] kart '%s' has now position %d.\n",
|
|
|
|
kart->getIdent().c_str(),
|
|
|
|
i-start);
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:04:13 -05:00
|
|
|
m_kart_status[sort_data[i].m_position].m_gp_rank = i - start;
|
2010-03-07 13:42:04 -05:00
|
|
|
}
|
|
|
|
// printf("kart %s has rank %i\n", 0, m_kart_status[0].m_gp_rank);
|
2011-02-02 17:04:13 -05:00
|
|
|
} // computeGPRanks
|
2010-03-07 13:42:04 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2009-03-26 19:31:00 -04:00
|
|
|
void RaceManager::exitRace()
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
|
|
|
// Only display the grand prix result screen if all tracks
|
|
|
|
// were finished, and not when a race is aborted.
|
2009-12-06 11:06:39 -05:00
|
|
|
if (m_major_mode==MAJOR_MODE_GRAND_PRIX && m_track_number==(int)m_tracks.size())
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
|
|
|
unlock_manager->grandPrixFinished();
|
2010-02-20 21:22:05 -05:00
|
|
|
|
|
|
|
StateManager::get()->resetAndGoToScreen( MainMenuScreen::getInstance() );
|
2010-05-30 11:45:44 -04:00
|
|
|
|
2010-05-30 19:20:32 -04:00
|
|
|
|
|
|
|
bool someHumanPlayerWon = false;
|
|
|
|
const unsigned int kartStatusCount = m_kart_status.size();
|
2010-08-07 21:02:26 -04:00
|
|
|
|
2010-10-28 21:39:27 -04:00
|
|
|
const int loserThreshold = 3;
|
2010-08-07 21:02:26 -04:00
|
|
|
|
2010-02-21 18:10:24 -05:00
|
|
|
std::string winners[3];
|
2010-05-30 19:20:32 -04:00
|
|
|
std::vector<std::string> humanLosers; // because we don't care about AIs that lost
|
|
|
|
for (unsigned int i=0; i < kartStatusCount; ++i)
|
2010-02-21 18:10:24 -05:00
|
|
|
{
|
2010-08-07 21:02:26 -04:00
|
|
|
if(UserConfigParams::m_verbosity >= 5)
|
2010-05-30 11:45:44 -04:00
|
|
|
{
|
2010-03-24 19:52:41 -04:00
|
|
|
std::cout << m_kart_status[i].m_ident << " has GP final rank "
|
|
|
|
<< m_kart_status[i].m_gp_rank << std::endl;
|
2010-05-30 11:45:44 -04:00
|
|
|
}
|
2010-03-24 19:52:41 -04:00
|
|
|
|
2010-03-07 13:42:04 -05:00
|
|
|
const int rank = m_kart_status[i].m_gp_rank;
|
2010-08-07 21:02:26 -04:00
|
|
|
if (rank >= 0 && rank < loserThreshold)
|
2010-02-21 18:10:24 -05:00
|
|
|
{
|
|
|
|
winners[rank] = m_kart_status[i].m_ident;
|
2010-05-30 19:20:32 -04:00
|
|
|
if (m_kart_status[i].m_kart_type == KT_PLAYER ||
|
|
|
|
m_kart_status[i].m_kart_type == KT_NETWORK_PLAYER)
|
|
|
|
{
|
|
|
|
someHumanPlayerWon = true;
|
|
|
|
}
|
|
|
|
}
|
2010-08-07 21:02:26 -04:00
|
|
|
else if (rank >= loserThreshold)
|
2010-05-30 19:20:32 -04:00
|
|
|
{
|
|
|
|
if (m_kart_status[i].m_kart_type == KT_PLAYER ||
|
|
|
|
m_kart_status[i].m_kart_type == KT_NETWORK_PLAYER)
|
|
|
|
{
|
|
|
|
humanLosers.push_back(m_kart_status[i].m_ident);
|
|
|
|
}
|
2010-02-21 18:10:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-30 19:20:32 -04:00
|
|
|
if (someHumanPlayerWon)
|
|
|
|
{
|
|
|
|
StateManager::get()->pushScreen( GrandPrixWin::getInstance() );
|
|
|
|
GrandPrixWin::getInstance()->setKarts(winners);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StateManager::get()->pushScreen( GrandPrixLose::getInstance() );
|
|
|
|
|
2010-07-17 14:17:15 -04:00
|
|
|
if (humanLosers.size() >= 1)
|
2010-05-30 19:20:32 -04:00
|
|
|
{
|
2010-07-17 14:17:15 -04:00
|
|
|
GrandPrixLose::getInstance()->setKarts( humanLosers );
|
2010-05-30 19:20:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "RaceManager::exitRace() : what's going on?? no winners and no losers??\n";
|
2010-07-17 14:17:15 -04:00
|
|
|
std::vector<std::string> karts;
|
|
|
|
karts.push_back(UserConfigParams::m_default_kart);
|
|
|
|
GrandPrixLose::getInstance()->setKarts( karts );
|
2010-05-30 19:20:32 -04:00
|
|
|
}
|
|
|
|
}
|
2008-09-07 09:25:58 -04:00
|
|
|
}
|
2009-12-01 14:49:11 -05:00
|
|
|
|
2010-03-01 15:17:31 -05:00
|
|
|
World::deleteWorld();
|
2008-09-07 09:25:58 -04:00
|
|
|
m_track_number = 0;
|
2009-03-26 19:31:00 -04:00
|
|
|
} // exitRace
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 09:43:09 -04:00
|
|
|
/** A kart has finished the race at the specified time (which can be
|
2010-02-17 06:59:51 -05:00
|
|
|
* different from World::getWorld()->getClock() in case of setting
|
|
|
|
* extrapolated arrival times). This function is only called from
|
|
|
|
* kart::finishedRace()
|
2008-09-09 09:43:09 -04:00
|
|
|
* \param kart The kart that finished the race.
|
|
|
|
* \param time Time at which the kart finished the race.
|
|
|
|
*/
|
2010-02-17 06:59:51 -05:00
|
|
|
void RaceManager::kartFinishedRace(const Kart *kart, float time)
|
2008-09-07 09:25:58 -04:00
|
|
|
{
|
2008-12-19 19:37:45 -05:00
|
|
|
unsigned int id = kart->getWorldKartId();
|
2008-09-07 09:25:58 -04:00
|
|
|
int pos = kart->getPosition();
|
|
|
|
|
2008-10-22 12:05:08 -04:00
|
|
|
assert(pos-1 >= 0);
|
2008-10-24 01:03:12 -04:00
|
|
|
assert(pos-1 < (int)m_kart_status.size());
|
2008-10-22 12:05:08 -04:00
|
|
|
|
2008-12-25 13:16:49 -05:00
|
|
|
m_kart_status[id].m_last_score = m_kart_status[id].m_score;
|
2008-12-19 19:37:45 -05:00
|
|
|
m_kart_status[id].m_score += m_score_for_position[pos-1];
|
|
|
|
m_kart_status[id].m_overall_time += time;
|
|
|
|
m_kart_status[id].m_last_time = time;
|
2008-09-07 09:25:58 -04:00
|
|
|
m_num_finished_karts ++;
|
2010-02-14 19:54:28 -05:00
|
|
|
if(kart->getController()->isPlayerController()) m_num_finished_players++;
|
2010-02-17 06:59:51 -05:00
|
|
|
} // kartFinishedRace
|
2008-09-07 09:25:58 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-01-03 15:17:44 -05:00
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
void RaceManager::rerunRace()
|
|
|
|
{
|
|
|
|
// Subtract last score from all karts:
|
|
|
|
for(int i=0; i<m_num_karts; i++)
|
|
|
|
{
|
2008-12-25 13:16:49 -05:00
|
|
|
m_kart_status[i].m_score = m_kart_status[i].m_last_score;
|
2008-09-07 09:25:58 -04:00
|
|
|
m_kart_status[i].m_overall_time -= m_kart_status[i].m_last_time;
|
|
|
|
}
|
2010-02-11 02:11:52 -05:00
|
|
|
World::getWorld()->restartRace();
|
2008-09-07 09:25:58 -04:00
|
|
|
} // rerunRace
|
|
|
|
|
2011-01-03 14:25:56 -05:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void RaceManager::startGP(const GrandPrixData* gp)
|
|
|
|
{
|
|
|
|
assert(gp != NULL);
|
|
|
|
|
|
|
|
StateManager::get()->enterGameState();
|
|
|
|
setGrandPrix(*gp);
|
|
|
|
setCoinTarget( 0 ); // Might still be set from a previous challenge
|
|
|
|
network_manager->setupPlayerKartInfo();
|
|
|
|
|
|
|
|
setMajorMode(RaceManager::MAJOR_MODE_GRAND_PRIX);
|
|
|
|
startNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void RaceManager::startSingleRace(const std::string trackIdent, const int num_laps)
|
|
|
|
{
|
|
|
|
StateManager::get()->enterGameState();
|
|
|
|
setTrack(trackIdent.c_str());
|
|
|
|
|
|
|
|
if (num_laps != -1) setNumLaps( num_laps );
|
|
|
|
|
|
|
|
setMajorMode(RaceManager::MAJOR_MODE_SINGLE);
|
|
|
|
|
|
|
|
setCoinTarget( 0 ); // Might still be set from a previous challenge
|
|
|
|
network_manager->setupPlayerKartInfo();
|
|
|
|
|
|
|
|
startNew();
|
|
|
|
}
|
|
|
|
|
2008-09-07 09:25:58 -04:00
|
|
|
/* EOF */
|