Fix #4110 and some clean-up

This commit is contained in:
Alayan 2019-10-25 22:02:41 +02:00
parent ff2ccdbde3
commit fc769c48a7

View File

@ -1893,8 +1893,7 @@ void SkiddingAI::computeNearestKarts()
float own_overall_distance = m_world->getOverallDistance(m_kart->getWorldKartId());
m_num_players_ahead = 0;
unsigned int n = ProfileWorld::isProfileMode()
? 0 : race_manager->getNumPlayers();
unsigned int n = ProfileWorld::isProfileMode() ? 0 : race_manager->getNumPlayers();
std::vector<float> overall_distance;
// Get the players distances
@ -1904,8 +1903,8 @@ void SkiddingAI::computeNearestKarts()
overall_distance.push_back(m_world->getOverallDistance(kart_id));
}
// Sort the list
std::sort(overall_distance.begin(), overall_distance.end());
// Sort the list in descending order
std::sort(overall_distance.begin(), overall_distance.end(), std::greater<float>());
for(unsigned int i=0; i<n; i++)
{
@ -1918,10 +1917,12 @@ void SkiddingAI::computeNearestKarts()
|| ( race_manager->isFollowMode() && m_kart->getWorldKartId() == 0))
target_overall_distance = 999999.9f;
// In higher difficulties, rubber band towards the first player,
// if at all (SuperTux has currently no rubber banding at all)
else if (race_manager->getDifficulty() == RaceManager::DIFFICULTY_HARD ||
race_manager->getDifficulty() == RaceManager::DIFFICULTY_BEST)
// In higher difficulties and in follow the leader, rubber band towards the first player,
// if at all (SuperTux has no rubber banding at all). Boosted AIs also target the 1st player.
else if ( race_manager->getDifficulty() == RaceManager::DIFFICULTY_HARD
|| race_manager->getDifficulty() == RaceManager::DIFFICULTY_BEST
|| race_manager->isFollowMode()
|| m_kart->getBoostAI())
{
target_overall_distance = overall_distance[n-1]; // Highest player distance
}
@ -1930,19 +1931,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 = 1.0f;
// The 1st player get an index of 0, the 2nd an index of 2, etc.
int target_index = 0;
// Avoid a division by 0.
// If there is only one AI, it will target the first player
// 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;
{
target_index = (position_among_ai-1) * (race_manager->getNumPlayers()-1);
target_index += (num_ai/2) - 1;
target_index = target_index / (num_ai - 1);
}
// 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 <= (int)n-1);
target_overall_distance = overall_distance[target_index];
}