Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1933] Validate impulse_request values #1934

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/ai/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void ShipAI::run(float delta)
{
owner->target_rotation = owner->getRotation();
owner->warp_request = 0.0;
owner->impulse_request = 0.0f;
owner->setImpulseRequest(0.0f);

updateWeaponState(delta);
if (update_target_delay > 0.0f)
Expand Down Expand Up @@ -598,14 +598,19 @@ 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->impulse_request = 1.0f;
else
owner->impulse_request = (distance - keep_distance) / owner->impulse_max_speed * 5.0f;
if (rotation_diff > 90)
owner->impulse_request = -owner->impulse_request;
else if (rotation_diff < 45)
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)
{
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.0f)
owner->setImpulseRequest(-owner->impulse_request);
else if (rotation_diff < 45.0f)
owner->setImpulseRequest(owner->impulse_request * (1.0f - ((rotation_diff - 45.0f) / 45.0f)));
}
}
}

Expand Down Expand Up @@ -633,19 +638,19 @@ void ShipAI::flyFormation(P<SpaceObject> target, glm::vec2 offset)
{
float angle_diff = angleDifference(owner->target_rotation, owner->getRotation());
if (angle_diff > 10.0f)
owner->impulse_request = 0.0f;
owner->setImpulseRequest(0.0f);
else if (angle_diff > 5.0f)
owner->impulse_request = (10.0f - angle_diff) / 5.0f;
owner->setImpulseRequest((10.0f - angle_diff) / 5.0f);
else
owner->impulse_request = 1.0f;
owner->setImpulseRequest(1.0f);
}else{
if (distance > r / 2.0f)
{
owner->target_rotation += angleDifference(owner->target_rotation, target->getRotation()) * (1.0f - distance / r);
owner->impulse_request = distance / r;
owner->setImpulseRequest(distance / r);
}else{
owner->target_rotation = target->getRotation();
owner->impulse_request = 0.0f;
owner->setImpulseRequest(0.0f);
}
}
}else{
Expand Down
2 changes: 1 addition & 1 deletion src/ai/fighterAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void FighterAI::runAttack(P<SpaceObject> target)
else
{
owner->target_rotation = evade_direction;
owner->impulse_request = 1.0;
owner->setImpulseRequest(1.0f);
}
break;
case recharge:
Expand Down
19 changes: 9 additions & 10 deletions src/spaceObjects/spaceship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ SpaceShip::SpaceShip(string multiplayerClassName, float multiplayer_significant_
setCollisionPhysics(true, false);

target_rotation = getRotation();
impulse_request = 0;
impulse_request = 0.0f;
current_impulse = 0;
has_warp_drive = true;
warp_request = 0;
Expand Down Expand Up @@ -960,9 +960,9 @@ void SpaceShip::update(float delta)
else
target_rotation = vec2ToAngle(getPosition() - docking_target->getPosition());
if (fabs(angleDifference(target_rotation, getRotation())) < 10.0f)
impulse_request = -1.f;
setImpulseRequest(-1.f);
else
impulse_request = 0.f;
setImpulseRequest(0.f);
}
if (docking_state == DS_Docked)
{
Expand All @@ -985,7 +985,7 @@ void SpaceShip::update(float delta)
}
}
}
impulse_request = 0.f;
setImpulseRequest(0.f);
}
if ((docking_state == DS_Docked) || (docking_state == DS_Docking))
warp_request = 0;
Expand Down Expand Up @@ -1102,10 +1102,9 @@ void SpaceShip::update(float delta)
}
}
current_warp = 0.f;
if (impulse_request > 1.0f)
impulse_request = 1.0f;
if (impulse_request < -1.0f)
impulse_request = -1.0f;
// Validate impulse request; this might not be necessary.
setImpulseRequest(impulse_request);

if (current_impulse < impulse_request)
{
if (cap_speed > 0)
Expand Down Expand Up @@ -1312,7 +1311,7 @@ void SpaceShip::requestUndock()
{
docked_style = DockStyle::None;
docking_state = DS_NotDocking;
impulse_request = 0.5;
setImpulseRequest(0.5f);
}
}

Expand All @@ -1321,7 +1320,7 @@ void SpaceShip::abortDock()
if (docking_state == DS_Docking)
{
docking_state = DS_NotDocking;
impulse_request = 0.f;
setImpulseRequest(0.f);
warp_request = 0;
target_rotation = getRotation();
}
Expand Down
1 change: 1 addition & 0 deletions src/spaceObjects/spaceship.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ class SpaceShip : public ShipTemplateBasedObject
impulse_max_speed = forward_speed;
impulse_max_reverse_speed = reverse_speed.value_or(forward_speed);
}
void setImpulseRequest(float request) { impulse_request = std::clamp(request, -1.0f, 1.0f); }
float getSystemCoolantRate(ESystem system) const { if (system >= SYS_COUNT) return 0.f; if (system <= SYS_None) return 0.f; return systems[system].coolant_rate_per_second; }
void setSystemCoolantRate(ESystem system, float rate) { if (system >= SYS_COUNT) return; if (system <= SYS_None) return; systems[system].coolant_rate_per_second = rate; }
float getRotationMaxSpeed() { return turn_speed; }
Expand Down
Loading