Skip to content
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
5 changes: 3 additions & 2 deletions rts/Sim/MoveTypes/GroundMoveType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3322,8 +3322,9 @@ bool CGroundMoveType::UpdateDirectControl()
ChangeSpeed(0.0f, false, true);
}

if (unitCon.left ) { ChangeHeading(owner->heading + turnRate); turnSign = 1.0f; }
if (unitCon.right) { ChangeHeading(owner->heading - turnRate); turnSign = -1.0f; }
const short unitTurnRate = FloatToHeading(turnRate);
if (unitCon.left ) { ChangeHeading(owner->heading + unitTurnRate); turnSign = 1.0f; }
if (unitCon.right) { ChangeHeading(owner->heading - unitTurnRate); turnSign = -1.0f; }

// local client is controlling us
if (selfCon.GetControllee() == owner)
Expand Down
3 changes: 2 additions & 1 deletion rts/Sim/Path/IPathController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ short GMTDefaultPathController::GetDeltaHeading(
// add lookahead term to avoid overshooting target heading
// note that turnBrakeDist is always positive
const short brakeDistFactor = (absTurnSpeed >= maxTurnAccel);
const short stopTurnHeading = oldHeading + (turnBrakeDist * Sign(curTurnSpeed) * brakeDistFactor);
const short brakeDist = FloatToHeading(turnBrakeDist * Sign(curTurnSpeed));
const short stopTurnHeading = oldHeading + (brakeDist * brakeDistFactor);
const short curDeltaHeading = newHeading - stopTurnHeading;

if (brakeDistFactor == 0) {
Expand Down
1 change: 1 addition & 0 deletions rts/System/SpringMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ short int GetHeadingFromFacing(const int facing) _pure _warn_unused_result;
int GetFacingFromHeading(const short int heading) _pure _warn_unused_result;
float GetHeadingFromVectorF(const float dx, const float dz) _pure _warn_unused_result;
short int GetHeadingFromVector(const float dx, const float dz) _pure _warn_unused_result;
short int FloatToHeading(const float angle) _pure _warn_unused_result;
shortint2 GetHAndPFromVector(const float3 vec) _pure _warn_unused_result; // vec should be normalized
float2 GetHAndPFromVectorF(const float3 vec) _pure _warn_unused_result; // vec should be normalized
float3 GetVectorFromHeading(const short int heading) _pure _warn_unused_result;
Expand Down
11 changes: 11 additions & 0 deletions rts/System/SpringMath.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ inline short int GetHeadingFromFacing(const int facing)
}
}

// headings are circular 16-bit integer angles, so sums of them are meant to wrap.
// float->short is UB once out of range, so the float side has to be in range
// already; the assert catches a caller that is not in debug builds.
inline short int FloatToHeading(const float angle)
{
assert(angle >= static_cast<float>(std::numeric_limits<short int>::min())
&& angle <= static_cast<float>(std::numeric_limits<short int>::max()));

return static_cast<short int>(angle);
}

inline int GetFacingFromHeading(const short int heading)
{
if (heading >= 0) {
Expand Down