diff --git a/src/karts/controller/skidding_ai.cpp b/src/karts/controller/skidding_ai.cpp index f79c8d8d8..5badcded2 100644 --- a/src/karts/controller/skidding_ai.cpp +++ b/src/karts/controller/skidding_ai.cpp @@ -1902,9 +1902,18 @@ void SkiddingAI::computeNearestKarts() unsigned int kart_id = m_world->getPlayerKart(i)->getWorldKartId(); overall_distance.push_back(m_world->getOverallDistance(kart_id)); } - + // Sort the list in descending order std::sort(overall_distance.begin(), overall_distance.end(), std::greater()); + + // Get the AI's position (the position update may not be done, leading to crashes) + int curr_position = 1; + for(unsigned int i=0; igetNumKarts(); i++) + { + if( m_world->getOverallDistance(i) > own_overall_distance + && !m_world->getKart(i)->isEliminated()) + curr_position++; + } for(unsigned int i=0; igetNumKarts() - race_manager->getNumPlayers(); - int position_among_ai = m_kart->getPosition() - m_num_players_ahead; + int position_among_ai = curr_position - m_num_players_ahead; // Converts a position among AI to a position among players // The 1st player get an index of 0, the 2nd an index of 2, etc.