Removed one of the kart sorts that occurred after a grand prix ended.

If karts tie on points at the end of a grand prix, they are now 
evaluated on overall race time too. 


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1451 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
scifly 2008-02-09 20:23:14 +00:00
parent c23ac39b05
commit 15adc2f2d4
4 changed files with 44 additions and 29 deletions

View File

@ -58,39 +58,19 @@ GrandPrixEnd::GrandPrixEnd()
m_context = new ssgContext;
oldContext->makeCurrent();
int highest = 0;
//FIXME: We go from the back to the front because the players are in the
//back and in case of a tie they will win against the AI, *but* if it's
//player vs. player, the player who goes first would win.
for(int i = race_manager->getNumKarts() - 1; i > 0; --i)
if(race_manager->getKartScore(i) > race_manager->getKartScore(highest))
highest = i;
const std::string KART_NAME = race_manager->getKartName(highest);
const KartProperties* WINNING_KART = kart_properties_manager->getKart(KART_NAME);
static char output[MAX_MESSAGE_LENGTH];
snprintf(output, sizeof(output),
_("The winner is %s!"),WINNING_KART->getName().c_str());
widget_manager->addWgt( WTOK_TITLE, 60, 10);
widget_manager->showWgtRect(WTOK_TITLE);
widget_manager->showWgtText(WTOK_TITLE);
widget_manager->setWgtText(WTOK_TITLE, output);
widget_manager->setWgtTextSize(WTOK_TITLE, WGT_FNT_LRG);
widget_manager->breakLine();
const unsigned int MAX_STR_LEN = 60;
const unsigned int NUM_KARTS = world->getNumKarts();
Kart *kart;
int *scores = new int[NUM_KARTS];
int *position = new int[NUM_KARTS];
float *race_time = new float[NUM_KARTS];
for( unsigned int i = 0; i < NUM_KARTS; ++i )
{
kart = world->getKart(i);
position[i] = i;
scores[i] = race_manager->getKartScore(i);
race_time[i] = race_manager->getKartOverallTime(i);
}
//Bubblesort
@ -100,23 +80,40 @@ GrandPrixEnd::GrandPrixEnd()
sorted = true;
for( unsigned int i = 0; i < NUM_KARTS - 1; ++i )
{
if( scores[i] < scores[i+1] )
if( scores[i] < scores[i+1] || (scores[i] == scores[i+1]
&& race_time[i] > race_time[i+1]))
{
int tmp_score[2];
float tmp_time;
tmp_score[0] = position[i];
tmp_score[1] = scores[i];
tmp_time = race_time[i];
position[i] = position[i+1];
scores[i] = scores[i+1];
race_time[i] = race_time[i+1];
position[i+1] = tmp_score[0];
scores[i+1] = tmp_score[1];
race_time[i+1] = tmp_time;
sorted = false;
}
}
} while(!sorted);
kart = world->getKart(position[0]);
static char output[MAX_MESSAGE_LENGTH];
snprintf(output, sizeof(output),
_("The winner is %s!"),kart->getName().c_str());
widget_manager->addWgt( WTOK_TITLE, 60, 10);
widget_manager->showWgtRect(WTOK_TITLE);
widget_manager->showWgtText(WTOK_TITLE);
widget_manager->setWgtText(WTOK_TITLE, output);
widget_manager->setWgtTextSize(WTOK_TITLE, WGT_FNT_LRG);
widget_manager->breakLine();
m_score = new char[MAX_STR_LEN*NUM_KARTS];
@ -134,8 +131,11 @@ GrandPrixEnd::GrandPrixEnd()
widget_manager->setWgtTextSize(WTOK_FIRSTKART + i, WGT_FNT_SML);
widget_manager->breakLine();
}
const std::string KART_NAME = race_manager->getKartName(position[0]);
const KartProperties* WINNING_KART = kart_properties_manager->getKart(KART_NAME);
delete []scores;
delete []position;
delete []race_time;
widget_manager->addWgt(WTOK_QUIT, 50, 7);
widget_manager->activateWgt(WTOK_QUIT);

View File

@ -83,10 +83,10 @@ GrandPrixMode::GrandPrixMode(const std::vector<std::string>& players_,
const int NUM_AI_KARTS = m_num_karts - NUM_PLAYERS;
//Add the AI karts
for(int i = 0; i < NUM_AI_KARTS; ++i)
m_karts.push_back(KartStatus(kart_names[i], 0, i, NUM_PLAYERS));
m_karts.push_back(KartStatus(kart_names[i], 0, 0.0f, i, NUM_PLAYERS));
//Add the player karts
for(int i = 0; i < NUM_PLAYERS; ++i)
m_karts.push_back(KartStatus(kart_names[i+NUM_AI_KARTS], 0, i+NUM_AI_KARTS, i));
m_karts.push_back(KartStatus(kart_names[i+NUM_AI_KARTS], 0, 0.0f, i+NUM_AI_KARTS, i));
}
//-----------------------------------------------------------------------------
@ -178,6 +178,7 @@ GrandPrixMode::exit_race()
}
}
//=============================================================================
QuickRaceMode::QuickRaceMode(const std::string& track_,
const std::vector<std::string>& players_,

View File

@ -39,6 +39,8 @@ public:
virtual int getKartScore (int kart) const {return 0;}
virtual std::string getKartName(int kart) const { return "";}
virtual int getPositionScore(int pos ) const {return 0;}
virtual void addKartOverallTime (int kart, float race_time) {}
virtual float getKartOverallTime (int kart) const {return 0.0f;}
};
class GrandPrixMode : public RaceMode
@ -53,14 +55,16 @@ private:
{
std::string ident;//The .tkkf filename without the .tkkf
int score;
float overall_time;
int prev_finish_pos;
int player;//Which player controls the kart, for the AI this is
//the number of players.
KartStatus(const std::string& ident_, const int& score_,
const int& prev_finish_pos_, const int& player_) :
ident(ident_), score(score_), prev_finish_pos(prev_finish_pos_),
player(player_) {}
const float& overall_time_, const int& prev_finish_pos_,
const int& player_) :
ident(ident_), score(score_), overall_time(overall_time_),
prev_finish_pos(prev_finish_pos_), player(player_){}
};
@ -88,6 +92,11 @@ public:
m_karts[kart].score +=
getPositionScore(pos);
}
float getKartOverallTime (int kart) const {return m_karts[kart].overall_time;}
void addKartOverallTime (int kart, float race_time)
{
m_karts[kart].overall_time += race_time;
}
friend bool operator< (const KartStatus& left, const KartStatus& right)
{
if (left.score < right.score) return true;
@ -172,6 +181,8 @@ public:
int getPositionScore(int pos) const { return m_mode->getPositionScore(pos);}
std::string getKartName(int kart) const { return m_mode->getKartName(kart); }
void addKartScore(int kart, int pos) { m_mode->addKartScore(kart, pos); }
void addKartOverallTime(int kart, float race_time) {m_mode->addKartOverallTime(kart, race_time); }
float getKartOverallTime(int kart) {return m_mode->getKartOverallTime(kart); }
void addFinishedKarts(int num) { m_num_finished_karts += num; }
void PlayerFinishes() { m_num_finished_players++; }
int allPlayerFinished() {return m_num_finished_players==m_players.size();}

View File

@ -464,6 +464,7 @@ void World::checkRaceStatus()
m_kart[i]->setFinishingState(m_clock);
race_manager->addKartScore((int)i, m_kart[i]->getPosition());
race_manager->addKartOverallTime((int) i, m_clock);
++new_finished_karts;
if(m_kart[i]->isPlayerKart())
@ -509,8 +510,10 @@ void World::checkRaceStatus()
{
if(!m_kart[i]->raceIsFinished())
{
m_kart[i]->setFinishingState(m_kart[i]->estimateFinishTime());
const float est_finish_time = m_kart[i]->estimateFinishTime();
m_kart[i]->setFinishingState(est_finish_time);
race_manager->addKartScore((int)i, m_kart[i]->getPosition());
race_manager->addKartOverallTime((int) i, est_finish_time);
} // if !raceIsFinished
} // for i
}