Skip to content

Commit

Permalink
Fix #5096
Browse files Browse the repository at this point in the history
- Move the GP higshcore update from race_result_gui.cpp to world.cpp ; GUI code should not be doing this sort of operation
- Update the GP hishcore for any number of eligible local human players.
- Various simplifications of the highscore logic in world.cpp
- Drop support for lap-trial GP highscore - the code for it was extremely hacky and a fix would require a complete rewrite of how lap totals are tracked in lap-trial GPs
  • Loading branch information
Alayan-stk-2 committed Dec 23, 2024
1 parent c73a797 commit 2c30427
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 65 deletions.
4 changes: 0 additions & 4 deletions src/config/saved_grand_prix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ class SavedGrandPrix
/** Returns time target used in Lap Trial mode */
float getTimeTarget() const { return m_time_target; }

// ------------------------------------------------------------------------
/** Returns total laps in GP */
int getPlayerTotalLaps() const { return m_player_total_laps; }

// ------------------------------------------------------------------------
/** Sets the index of the last track finished. */
void setNextTrack(int next_track) { m_next_track = next_track; }
Expand Down
70 changes: 39 additions & 31 deletions src/modes/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,14 +1256,13 @@ Highscores* World::getGPHighscores() const
}

// ----------------------------------------------------------------------------
/** Called at the end of a race. Checks if the current times are worth a new
* score, if so it notifies the HighscoreManager so the new score is added
* and saved.
/** Called at the end of a race.
* Submits the valid new times through the addHighscore functions, which then handles
* checking if the new times are worth of a highscore and saving them.
* The best highscore rank is transmitted to highlight the best new highscore in the GUI.
*/
void World::updateHighscores(int* best_highscore_rank)
{
*best_highscore_rank = -1;

if(!m_use_highscores) return;

// Add times to highscore list. First compute the order of karts,
Expand Down Expand Up @@ -1291,7 +1290,6 @@ void World::updateHighscores(int* best_highscore_rank)
{
// no kart claimed to be in this position, most likely means
// the kart location data is wrong

#ifdef DEBUG
Log::error("[World]", "Incorrect kart positions:");
for (unsigned int i=0; i<m_karts.size(); i++ )
Expand All @@ -1314,39 +1312,49 @@ void World::updateHighscores(int* best_highscore_rank)
Kart *k = (Kart*)m_karts[index[pos]].get();

Highscores* highscores = getHighscores();

int highscore_rank = 0;
float highscore_value = RaceManager::get()->isLapTrialMode() ? static_cast<float>(getFinishedLapsOfKart(index[pos]))
: k->getFinishTime();
// The player is a local player, so there is a name:
if (RaceManager::get()->isLapTrialMode())
{
highscore_rank = highscores->addData(k->getIdent(),
k->getController()->getName(),
static_cast<float>(getFinishedLapsOfKart(index[pos])));
}
else
int highscore_rank = highscores->addData(k->getIdent(), k->getController()->getName(), highscore_value);

if (highscore_rank > 0 && (*best_highscore_rank == -1 ||
highscore_rank < *best_highscore_rank))
{
highscore_rank = highscores->addData(k->getIdent(),
k->getController()->getName(),
k->getFinishTime() );
*best_highscore_rank = highscore_rank;
}

} // next position

// Handle GP highscores
if (RaceManager::get()->getMajorMode() == RaceManager::MAJOR_MODE_GRAND_PRIX &&
RaceManager::get()->getNumOfTracks() == RaceManager::get()->getTrackNumber() + 1 &&
!RaceManager::get()->getGrandPrix().isRandomGP() &&
RaceManager::get()->getSkippedTracksInGP() == 0)
{
std::string gp_name = RaceManager::get()->getGrandPrix().getId();

if (highscore_rank > 0)
for (unsigned int kart_id = 0; kart_id < RaceManager::get()->getNumberOfKarts(); kart_id++)
{
if (*best_highscore_rank == -1 ||
highscore_rank < *best_highscore_rank)
{
*best_highscore_rank = highscore_rank;
}
// Only record times for local player karts and only if
// they finished the race
if (!m_karts[kart_id]->getController()->isLocalPlayerController() ||
!m_karts[kart_id]->hasFinishedRace() ||
m_karts[kart_id]->isEliminated())
continue;

Highscores::setSortOrder(Highscores::SO_DEFAULT);
highscore_manager->sortHighscores(false);
// The original lap trial GP code was a mess. I'm not fixing it...
if (RaceManager::get()->isLapTrialMode())
continue;

highscore_manager->saveHighscores();
}
} // next position
delete []index;
Kart *k = (Kart*)m_karts[kart_id].get();
float full_time = RaceManager::get()->getOverallTime(kart_id);

Highscores* highscores = World::getWorld()->getGPHighscores();
// The player is a local player, so there is a name:
highscores->addGPData(k->getIdent(), k->getController()->getName(), gp_name, full_time);
} // for kart_id
} // Handle GP highscores

delete []index;
} // updateHighscores

//-----------------------------------------------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions src/race/race_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ RaceManager::RaceManager()
m_flag_deactivated_ticks = stk_config->time2Ticks(3.0f);
m_skipped_tracks_in_gp = 0;
m_gp_time_target = 0.0f;
m_gp_total_laps = 0;
setMaxGoal(0);
setTimeTarget(0.0f);
setReverseTrack(false);
Expand Down Expand Up @@ -432,10 +431,7 @@ void RaceManager::startNew(bool from_overworld)
m_skipped_tracks_in_gp = m_saved_gp->getSkippedTracks();
Log::info("RaceManager","%d",isLapTrialMode());
if (isLapTrialMode())
{
m_gp_time_target = m_saved_gp->getTimeTarget();
m_gp_total_laps = m_saved_gp->getPlayerTotalLaps();
}
} // if m_saved_gp==NULL
} // if m_continue_saved_gp
} // if !network_world
Expand Down Expand Up @@ -795,7 +791,7 @@ void RaceManager::saveGP()
m_grand_prix.getReverseType(),
m_skipped_tracks_in_gp,
isLapTrialMode() ? m_gp_time_target : 0.0f,
isLapTrialMode() ? m_gp_total_laps : 0,
0,
m_kart_status);

// If a new GP is saved, delete any other saved data for this
Expand Down
4 changes: 0 additions & 4 deletions src/race/race_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,6 @@ class RaceManager
// ----------------------------------------------------------------------------------------
void setGPTimeTarget(float time_target) { m_gp_time_target = time_target; }
// ----------------------------------------------------------------------------------------
int getGPTotalLaps() const { return m_gp_total_laps; }
// ----------------------------------------------------------------------------------------
void addGPTotalLaps(int laps) { m_gp_total_laps += laps; }
// ----------------------------------------------------------------------------------------
/** Whether the current game mode allow live joining even the current game
*. started in network*/
bool supportsLiveJoining() const
Expand Down
21 changes: 0 additions & 21 deletions src/states_screens/race_result_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,6 @@ void RaceResultGUI::init()
MessageQueue::add(MessageQueue::MT_GENERIC, tips_string);
}
#endif

if (RaceManager::get()->getMajorMode() == RaceManager::MAJOR_MODE_GRAND_PRIX &&
!NetworkConfig::get()->isNetworking() &&
(RaceManager::get()->getMinorMode() == RaceManager::MINOR_MODE_NORMAL_RACE || RaceManager::get()->getMinorMode() == RaceManager::MINOR_MODE_TIME_TRIAL ||
RaceManager::get()->isLapTrialMode()))
{
const AbstractKart* k = RaceManager::get()->getKartWithGPRank(RaceManager::get()->getLocalPlayerGPRank(PLAYER_ID_GAME_MASTER));
RaceManager::get()->addGPTotalLaps(World::getWorld()->getFinishedLapsOfKart(k->getWorldKartId()));
if (RaceManager::get()->getNumOfTracks() == RaceManager::get()->getTrackNumber() + 1
&& !RaceManager::get()->getGrandPrix().isRandomGP() && RaceManager::get()->getSkippedTracksInGP() == 0)
{
Highscores* highscores = World::getWorld()->getGPHighscores();
float full_time;
if (RaceManager::get()->isLapTrialMode())
full_time = static_cast<float>(RaceManager::get()->getGPTotalLaps());
else
full_time = RaceManager::get()->getOverallTime(RaceManager::get()->getLocalPlayerGPRank(PLAYER_ID_GAME_MASTER));
std::string gp_name = RaceManager::get()->getGrandPrix().getId();
highscores->addGPData(k->getIdent(), k->getController()->getName(), gp_name, full_time);
}
}
} // init

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 2c30427

Please sign in to comment.