Skip to content

Commit

Permalink
[AI] Avoid division by 0 by validating impulse_max_speed
Browse files Browse the repository at this point in the history
  • Loading branch information
oznogon committed Apr 3, 2024
1 parent 5a4d862 commit 6227999
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,14 +598,21 @@ void ShipAI::flyTowards(glm::vec2 target, float keep_distance)
if (pathPlanner.route.size() > 1)
keep_distance = 0.0;

if (distance > keep_distance + owner->impulse_max_speed * 5.0f)
owner->setImpulseRequest(1.0f);
else
owner->setImpulseRequest((distance - keep_distance) / owner->impulse_max_speed * 5.0f);
if (rotation_diff > 90)
owner->setImpulseRequest(-owner->impulse_request);
else if (rotation_diff < 45)
owner->setImpulseRequest(owner->impulse_request * (1.0f - ((rotation_diff - 45.0f) / 45.0f)));
// setImpulseRequest only if impulse_max_speed is greater than 0.0
if (owner->impulse_max_speed > 0.0f)
{
owner->setImpulseRequest(0.0f);
} else {
if (distance > keep_distance + owner->impulse_max_speed * 5.0f)
owner->setImpulseRequest(1.0f);
else
owner->setImpulseRequest((distance - keep_distance) / owner->impulse_max_speed * 5.0f);

if (rotation_diff > 90)
owner->setImpulseRequest(-owner->impulse_request);
else if (rotation_diff < 45)
owner->setImpulseRequest(owner->impulse_request * (1.0f - ((rotation_diff - 45.0f) / 45.0f)));
}
}
}

Expand Down

0 comments on commit 6227999

Please sign in to comment.