From 0b5d68d1d1181960098f7a9559f4a298d8a7e62e Mon Sep 17 00:00:00 2001 From: Alayan-stk-2 Date: Thu, 7 Jun 2018 20:43:09 +0200 Subject: [PATCH] Fix #3280 (#3284) --- src/karts/controller/skidding_ai.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/karts/controller/skidding_ai.cpp b/src/karts/controller/skidding_ai.cpp index b10cf880f..3a968a073 100644 --- a/src/karts/controller/skidding_ai.cpp +++ b/src/karts/controller/skidding_ai.cpp @@ -2013,13 +2013,19 @@ void SkiddingAI::computeNearestKarts() int num_ai = m_world->getNumKarts() - race_manager->getNumPlayers(); int position_among_ai = m_kart->getPosition() - m_num_players_ahead; // Converts a position among AI to a position among players - float ideal_target = ((position_among_ai-1) / ((float) num_ai-1) + float ideal_target = 1.0f; + + // Avoid a division by 0. + // If there is only one AI, it will target the first player + if (num_ai > 1) + ideal_target = ((position_among_ai-1) / ((float) num_ai-1) * ((float) race_manager->getNumPlayers()-1)) + 1.0f; + // Substract 1 as index start from 0 and add 0.5 to get rounding // The cast truncate the decimals, so it won't go over n-1 // as the highest possible ideal_target is n int target_index = (int) (ideal_target - 0.5f); - assert (target_index >= 0 && target_index <= n-1); + assert(target_index >= 0 && target_index <= n-1); target_overall_distance = overall_distance[target_index]; } // Now convert 'maximum overall distance' to distance to player.