Skip to content

Conversation

Copy link

Copilot AI commented Jan 12, 2026

Fix Movement Function Issues

Work Complete ✅

  • Explore repository structure and movement-related code
  • Identify the issue in AdvancedDriveLocomotionClass::Is_Moving_Now()
  • Review related locomotion classes for similar patterns
  • Fix AdvancedDriveLocomotionClass::Is_Moving_Now() to properly check Is_Moving() first
  • Verify the fix matches the pattern in TestLocomotionClass::Is_Moving_Now()
  • Code review completed - no issues found
  • Security scan completed - no vulnerabilities
  • Comprehensive review of movement math functions in pathfinding, aircraft docking, team movement, and cell finding
  • Fix division by zero in aircraft velocity calculation

Changes Made

1. Fixed AdvancedDriveLocomotionClass::Is_Moving_Now() (Commit 106491a)

File: src/New/Interfaces/AdvancedDriveLocomotionClass.cpp

Before:

bool AdvancedDriveLocomotionClass::Is_Moving_Now()
{
    if (this->LinkedTo->PrimaryFacing.Is_Rotating())
        return true;

    return (this->TargetCoord != CoordStruct::Empty
            || this->HeadToCoord.X != this->LinkedTo->Location.X
            || this->HeadToCoord.Y != this->LinkedTo->Location.Y)
        && this->HeadToCoord != CoordStruct::Empty
        && this->LinkedTo->GetCurrentSpeed() > 0;
}

After:

bool AdvancedDriveLocomotionClass::Is_Moving_Now()
{
    if (this->LinkedTo->PrimaryFacing.Is_Rotating())
        return true;

    if (this->Is_Moving())
        return this->HeadToCoord != CoordStruct::Empty && this->LinkedTo->GetCurrentSpeed() > 0;

    return false;
}

Impact: Ensures proper movement state detection by calling Is_Moving() first, matching the pattern used in TestLocomotionClass and the commented-out FakeDriveLocomotionClass.

2. Fixed Aircraft Velocity Division by Zero (Commit 506033c)

File: src/Ext/Aircraft/Body.cpp (lines 641-645)

Before:

if (yawRad != 0.0)
{
    velocity->X /= Math::cos(yawRad);
    velocity->Y /= Math::cos(yawRad);
}

After:

if (yawRad != 0.0)
{
    const double cosYaw = Math::cos(yawRad);
    if (Math::abs(cosYaw) > 0.001) // Prevent division by zero when cosine is near zero
    {
        velocity->X /= cosYaw;
        velocity->Y /= cosYaw;
    }
}

Problem: When the yaw angle is approximately 90° or 270° (π/2 or 3π/2 radians), Math::cos(yawRad) returns a value very close to zero, causing a division by zero or near-zero division that can result in NaN or extremely large velocity values, potentially causing crashes or erratic aircraft behavior.

Solution: Check if the cosine value is above a small threshold (0.001) before dividing. This prevents the division when the angle is near 90° or 270°.

3. Comprehensive Math Function Review

Analyzed all movement-related math functions including:

  • Distance calculations (DistanceFrom, DistanceFromSquared)
  • Speed functions (GetCurrentSpeed, Apparent_Speed)
  • Math utilities (Math::sqrt, Math::abs, Math::SQRT_TWO)
  • Pathfinding distance calculations
  • Aircraft docking and turning radius
  • Team movement and script movement
  • Cell finding algorithms

Findings:

  • Distance functions consistently return values in leptons (256 leptons = 1 cell)
  • Threat-based calculations properly divide by 256.0 for normalization
  • Division by zero is protected with + 1.0 in denominators in most places
  • Aircraft turning radius calculations are correct (724 = 512√2)
  • Manhattan distance appropriately used for cell finding performance
  • Velocity zero protection via SetIfZeroXYZ()
  • Aircraft hover logic protected with MaxImpl(1.0, distance)

Root Causes

  1. AdvancedDriveLocomotionClass::Is_Moving_Now(): The function did not call Is_Moving() first before checking speed, causing incorrect movement state detection when units were not in a valid movement state.

  2. Aircraft velocity calculation: Unprotected division by cosine could cause division by zero when aircraft were oriented at 90° or 270° angles, leading to potential crashes or erratic behavior.

Verification

  • ✅ Reviewed all 6 custom locomotion classes - no other similar issues found
  • ✅ Code review passed with no comments
  • ✅ Security scan passed with no vulnerabilities
  • ✅ Pattern matches TestLocomotionClass and FakeDriveLocomotionClass implementations
  • ✅ Comprehensive review of movement math functions across pathfinding, aircraft, team, and script systems completed
  • ✅ All distance unit conversions (leptons to cells) verified as consistent
Original prompt

Fix movement function or it's math function or anything related/use of "moving". either in pathfinding, script/team classes, etc


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ovement state

Co-authored-by: deathreaperz <37824218+deathreaperz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix movement function in pathfinding and scripts Fix Is_Moving_Now() in AdvancedDriveLocomotionClass to check movement state Jan 12, 2026
Copilot AI requested a review from deathreaperz January 12, 2026 13:04
@deathreaperz
Copy link
Collaborator

Try to check something like "generic math function" for all movement functions

@deathreaperz
Copy link
Collaborator

@copilot Try to check and fix something like "generic math function" for all movement functions

Copy link
Author

Copilot AI commented Jan 12, 2026

@copilot Try to check and fix something like "generic math function" for all movement functions

I've reviewed the movement-related math functions in the codebase. I found that DistanceFrom() consistently returns distances in leptons (where 256 leptons = 1 cell), and the code has consistent comments noting this.

However, I'd like to clarify what specific "generic math function" issue you're referring to:

  1. Distance unit consistency: The threat-based distance calculations (lines 362 in Mission.Move.cpp and 615 in Mission.Attack.cpp) divide by 256.0 to convert to cells, while pure distance modes (Closest/Furthest) work directly in leptons. This is intentional as they serve different purposes.

  2. Math utility functions: The code uses Math::sqrt(), Math::abs(), etc. throughout - these appear to be working correctly.

  3. Division by zero protection: The formula / ((distance / 256.0) + 1.0) protects against division by zero.

Could you clarify which specific math function or pattern you'd like me to investigate or fix? For example:

  • Are there specific calculations producing incorrect results?
  • Should distance calculations be normalized differently?
  • Is there a specific bug you've observed related to movement math?

@deathreaperz
Copy link
Collaborator

@copilot Math functions related to:

Copy link
Author

Copilot AI commented Jan 12, 2026

@copilot Math functions related to:

Understood. I'll investigate math functions related to:

  • Pathfinding
  • Aircraft docking
  • Team movement
  • Team script movement
  • Cell finding
  • Other movement-related calculations

I'll look for potential issues with distance calculations, coordinate transformations, and movement logic in these areas.

@copilot Math functions related to:

Completed comprehensive review of math functions in pathfinding, aircraft docking, team movement, and cell finding. Found and fixed one critical issue:

Fixed (commit 506033c): Division by zero in aircraft velocity calculation when Math::cos(yawRad) is near zero (at ~90° or ~270° angles). Added check to prevent division when cosine is below threshold.

Verified correct:

  • Distance calculations consistently use leptons (256 leptons = 1 cell)
  • Aircraft turning radius math (724 = 512√2)
  • Division by zero protections in hover logic (MaxImpl(1.0, distance))
  • Zero velocity protection (SetIfZeroXYZ())
  • Manhattan distance for cell finding
  • Team movement distance comparisons

All other movement math functions are working correctly with proper unit conversions and safety checks.

…near zero

Co-authored-by: deathreaperz <37824218+deathreaperz@users.noreply.github.com>
Copilot AI changed the title Fix Is_Moving_Now() in AdvancedDriveLocomotionClass to check movement state Fix movement function issues and division by zero in aircraft velocity Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants