public static int GenerateDeltaCount(int size)
        {
            long fixSize = FixedMath.Create(size);
            int  ret     = FixedMath.Mul(FixedMath.Mul(fixSize, fixSize), FixedMath.Pi).CeilToInt();

            return(ret);
        }
        public static int GenerateDeltaCount(long radius)
        {
            radius /= GridManager.ScanResolution;
            int ret = FixedMath.Mul(FixedMath.Mul(radius, radius), FixedMath.Pi).CeilToInt();

            //if (ret < 5) ret = 5;
            return(ret);
        }
Ejemplo n.º 3
0
 protected override void OnSetup()
 {
     cachedBody             = Agent.Body;
     cachedBody.OnContact  += HandleCollision;
     cachedTurn             = Agent.Turner;
     collisionStopTreshold  = FixedMath.Mul(timescaledSpeed, CollisionStopTreshold);
     collisionStopTreshold *= collisionStopTreshold;
     timescaledAcceleration = Acceleration * 32 / LockstepManager.FrameRate;
     if (timescaledAcceleration > FixedMath.One)
     {
         timescaledAcceleration = FixedMath.One;
     }
     closingDistance = cachedBody.Radius;
     stuckTolerance  = ((Agent.Body.Radius * Speed) >> FixedMath.SHIFT_AMOUNT) / LockstepManager.FrameRate;
     stuckTolerance *= stuckTolerance;
 }
Ejemplo n.º 4
0
 protected override void OnSetup()
 {
     cachedBody            = Agent.Body;
     cachedBody.OnContact += HandleCollision;
     CachedTurn            = Agent.GetAbility <Turn>();
     CanTurn = _canTurn && CachedTurn != null;
     collisionStopTreshold  = FixedMath.Mul(timescaledSpeed, CollisionStopTreshold);
     collisionStopTreshold *= collisionStopTreshold;
     timescaledAcceleration = Acceleration * 32 / LockstepManager.FrameRate;
     if (timescaledAcceleration > FixedMath.One)
     {
         timescaledAcceleration = FixedMath.One;
     }
     closingDistance  = cachedBody.Radius;
     stuckTolerance   = ((Agent.Body.Radius * Speed) >> FixedMath.SHIFT_AMOUNT) / LockstepManager.FrameRate;
     stuckTolerance  *= stuckTolerance;
     CanPathfind      = _canPathfind;
     this.SlowArrival = true;
 }
Ejemplo n.º 5
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }

            if (IsMoving)
            {
                Agent.SetState(AnimState.Moving);

                if (CanPathfind)
                {
                    if (DoPathfind)
                    {
                        DoPathfind = false;
                        if (viableDestination)
                        {
                            if (Pathfinder.GetStartNode(cachedBody.Position, out currentNode))
                            {
                                if (currentNode.DoesEqual(this.destinationNode))
                                {
                                    if (this.RepathTries >= 1)
                                    {
                                        this.Arrive();
                                    }
                                }
                                else
                                {
                                    if (straightPath)
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                            straightPath = false;
                                        }
                                        else
                                        {
                                        }
                                    }
                                    else
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            straightPath = true;
                                        }
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath[pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                long stuckThreshold   = 0;

                if (distance > closingDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);

                    if (CanTurn)
                    {
                        CachedTurn.StartTurnDirection(movementDirection);
                    }

                    stuckThreshold = this.timescaledAcceleration / 4;
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, StopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    if (this.SlowArrival)
                    {
                        long closingSpeed = distance.Div(closingDistance);
                        desiredVelocity = movementDirection * (closingSpeed);
                        stuckThreshold  = closingSpeed / 2;
                    }
                    else
                    {
                        desiredVelocity = (movementDirection);
                        stuckThreshold  = this.timescaledAcceleration / 2;
                    }
                }
                //Temporary improvement to detecting stuck
                StuckTime++;
                stuckThreshold = stuckThreshold * 7 / 6;

                if (GetFullCanAutoStop() && (Agent.Body.Position - this.AveragePosition).FastMagnitude() < (stuckThreshold * stuckThreshold))
                {
                    if (StuckTime > StuckTimeThreshold)
                    {
                        if (movingToWaypoint)
                        {
                            this.pathIndex++;
                        }
                        else
                        {
                            if (RepathTries < StuckRepathTries)
                            {
                                DoPathfind = true;
                                RepathTries++;
                            }
                            else
                            {
                                RepathTries = 0;
                                this.Arrive();
                            }
                        }
                        StuckTime = 0;
                    }
                }
                else
                {
                    if (StuckTime > 0)
                    {
                        StuckTime -= 1;
                    }

                    RepathTries = 0;
                }

                if (movingToWaypoint)
                {
                    if (
                        (
                            this.pathIndex >= 0 &&
                            distance < closingDistance &&
                            (movementDirection).Dot(waypointDirection) < 0
                        ) ||
                        distance < FixedMath.Mul(closingDistance, FixedMath.Half))
                    {
                        this.pathIndex++;
                    }
                }

                desiredVelocity      *= Speed;
                cachedBody._velocity += GetAdjustVector(desiredVelocity);

                cachedBody.VelocityChanged = true;

                AutoStopPauser--;
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody.Velocity += GetAdjustVector(Vector2d.zero);
                }
                StoppedTime++;
            }
            AveragePosition = AveragePosition.Lerped(Agent.Body.Position, FixedMath.One / 4);
        }
Ejemplo n.º 6
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (CanPathfind)
                {
                    if (repathCount <= 0)
                    {
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody._position.x, cachedBody._position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                        repathCount  = RepathRate;
                                    }
                                    else
                                    {
                                        repathCount = StraightRepathRate;
                                    }
                                }
                                else
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        repathCount = RepathRate;
                                    }
                                    else
                                    {
                                        straightPath = true;
                                        repathCount  = StraightRepathRate;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                        if (hasPath)
                        {
                            //TODO: fix this shit
                            repathCount--;
                        }
                        else
                        {
                            repathCount--;
                        }
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath [pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                if (distance > closingDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);
                    if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y).AbsMoreThan(FixedMath.Half))
                    {
                        lastMovementDirection = movementDirection;
                        if (CanTurn)
                        {
                            CachedTurn.StartTurnDirection(movementDirection);
                        }
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    if (this.SlowArrival)
                    {
                        desiredVelocity = (movementDirection * (distance) / (closingDistance));
                    }
                    else
                    {
                        desiredVelocity = (movementDirection);
                    }
                }


                if (movingToWaypoint)
                {
                    if (distance < FixedMath.Mul(closingDistance, FixedMath.One))
                    {
                        this.pathIndex++;
                    }
                }
                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;

                cachedBody.VelocityChanged = true;
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                stopTime++;
            }
        }
Ejemplo n.º 7
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            //TODO: Organize/split this function
            if (IsMoving)
            {
                Agent.SetState(AnimState.Moving);

                if (CanPathfind)
                {
                    if (DoPathfind)
                    {
                        DoPathfind = false;
                        if (viableDestination)
                        {
                            if (Pathfinder.GetStartNode(cachedBody.Position, out currentNode))
                            {
                                if (currentNode.DoesEqual(this.destinationNode))
                                {
                                    if (this.RepathTries >= 1)
                                    {
                                        this.Arrive();
                                    }
                                }
                                else
                                {
                                    if (straightPath)
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                            straightPath = false;
                                        }
                                        else
                                        {
                                        }
                                    }
                                    else
                                    {
                                        if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                        {
                                            if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                    GridSize, GetNodeHash(destinationNode)))
                                            {
                                                hasPath   = true;
                                                pathIndex = 0;
                                            }
                                            else
                                            {
                                                if (IsFormationMoving)
                                                {
                                                    StartMove(MyMovementGroup.Destination);
                                                    IsFormationMoving = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            straightPath = true;
                                        }
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath[pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                long stuckThreshold   = timescaledAcceleration / LockstepManager.FrameRate;

                long slowDistance = cachedBody.VelocityMagnitude.Div(timescaledDecceleration);


                if (distance > slowDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);
                    if (CanTurn)
                    {
                        CachedTurn.StartTurnDirection(movementDirection);
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, StopMultiplier))
                    {
                        Arrive();
                        //TODO: Don't skip this frame of slowing down
                        return;
                    }
                    if (distance > closingDistance)
                    {
                        if (CanTurn)
                        {
                            CachedTurn.StartTurnDirection(movementDirection);
                        }
                    }
                    if (distance <= slowDistance)
                    {
                        long closingSpeed = distance.Div(slowDistance);
                        if (CanTurn)
                        {
                            CachedTurn.StartTurnDirection(movementDirection);
                        }
                        desiredVelocity = movementDirection * closingSpeed;
                        decellerating   = true;
                        //Reduce occurence of units preventing other units from reaching destination
                        stuckThreshold *= 4;
                    }
                }
                //If unit has not moved stuckThreshold in a frame, it's stuck
                StuckTime++;
                if (GetCanAutoStop())
                {
                    if (Agent.Body.Position.FastDistance(AveragePosition) <= (stuckThreshold * stuckThreshold))
                    {
                        if (StuckTime > StuckTimeThreshold)
                        {
                            if (movingToWaypoint)
                            {
                                this.pathIndex++;
                            }
                            else
                            {
                                if (RepathTries < StuckRepathTries)
                                {
                                    DoPathfind = true;
                                    RepathTries++;
                                }
                                else
                                {
                                    RepathTries = 0;
                                    this.Arrive();
                                }
                            }
                            StuckTime = 0;
                        }
                    }
                    else
                    {
                        if (StuckTime > 0)
                        {
                            StuckTime -= 1;
                        }

                        RepathTries = 0;
                    }
                }
                if (movingToWaypoint)
                {
                    if (
                        (
                            this.pathIndex >= 0 &&
                            distance < closingDistance &&
                            (movementDirection).Dot(waypointDirection) < 0
                        ) ||
                        distance < FixedMath.Mul(closingDistance, FixedMath.Half))
                    {
                        this.pathIndex++;
                    }
                }

                desiredVelocity      *= Speed;
                cachedBody._velocity += GetAdjustVector(desiredVelocity);

                cachedBody.VelocityChanged = true;
            }
            else
            {
                decellerating = true;

                //Slowin' down
                if (cachedBody.VelocityMagnitude > 0)
                {
                    cachedBody.Velocity += GetAdjustVector(Vector2d.zero);
                }
                StoppedTime++;
            }
            decellerating = false;

            AutoStopPauser--;
            CollisionStopPauser--;
            StopPauseLooker--;
            AveragePosition = AveragePosition.Lerped(Agent.Body.Position, FixedMath.One / 2);
        }
Ejemplo n.º 8
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (CanPathfind)
                {
                    if (DoPathfind)
                    {
                        DoPathfind = false;
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody._position.x, cachedBody._position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                    }
                                    else
                                    {
                                    }
                                }
                                else
                                {
                                    if (Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath,
                                                                GridSize))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        straightPath = true;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            targetPos = this.Destination;
                        }
                        else
                        {
                            targetPos = myPath [pathIndex];
                        }
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody._position;

                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }
                bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1);
                if (distance > closingDistance || movingToWaypoint)
                {
                    desiredVelocity = (movementDirection);

                    if (CanTurn)
                    {
                        CachedTurn.StartTurnDirection(movementDirection);
                    }


                    long threshold = this.timescaledSpeed / 4;

                    if (GetFullCanCollisionStop() && (Agent.Body.Position - this.LastPosition).FastMagnitude() < (threshold * threshold))
                    {
                        StuckTime++;
                        if (StuckTime > StuckTimeThreshold)
                        {
                            if (RepathTries < StuckRepathTries)
                            {
                                DoPathfind = true;
                                RepathTries++;
                            }
                            else
                            {
                                RepathTries = 0;
                                this.Arrive();
                            }
                            StuckTime = 0;
                        }
                    }
                    else
                    {
                        StuckTime   = 0;
                        RepathTries = 0;
                    }
                }
                else
                {
                    StuckTime = 0;
                    if (distance < FixedMath.Mul(closingDistance, StopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    if (this.SlowArrival)
                    {
                        desiredVelocity = (movementDirection * (distance) / (closingDistance));
                    }
                    else
                    {
                        desiredVelocity = (movementDirection);
                    }
                }


                if (movingToWaypoint)
                {
                    if (distance < FixedMath.Mul(closingDistance, FixedMath.Half))
                    {
                        this.pathIndex++;
                    }
                }
                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;

                cachedBody.VelocityChanged = true;

                TempCanCollisionStop = true;
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                StoppedTime++;
            }
            LastPosition = Agent.Body.Position;
        }
Ejemplo n.º 9
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (canPathfind)
                {
                    if (repathCount <= 0)
                    {
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody.Position.x, cachedBody.Position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                        repathCount  = RepathRate;
                                    }
                                    else
                                    {
                                        repathCount = StraightRepathRate;
                                    }
                                }
                                else
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        repathCount = RepathRate;
                                    }
                                    else
                                    {
                                        straightPath = true;
                                        repathCount  = StraightRepathRate;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                        if (hasPath)
                        {
                            repathCount--;
                        }
                        else
                        {
                            repathCount--;
                        }
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            pathIndex = myPath.Count - 1;
                        }
                        targetPos = myPath[pathIndex];
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody.Position;
                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }

                if (distance > closingDistance)
                {
                    desiredVelocity = (movementDirection);
                    if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y) != 0)
                    {
                        lastMovementDirection = movementDirection;
                        cachedTurn.StartTurnRaw(movementDirection);
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    desiredVelocity = (movementDirection * (distance) / (closingDistance));
                }

                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;
                if (distance <= closingDistance)
                {
                    pathIndex++;
                }
                cachedBody.VelocityChanged = true;
                if (collidedWithTrackedAgent)
                {
                    if (collidedCount >= CollisionStopCount)
                    {
                        collidedCount  = 0;
                        collidingAgent = null;
                        Arrive();
                    }
                    else
                    {
                        if (lastPosition.FastDistance(cachedBody.Position.x, cachedBody.Position.y)
                            < collisionStopTreshold)
                        {
                            collidedCount++;
                        }
                        else
                        {
                            lastPosition  = cachedBody.Position;
                            collidedCount = 0;
                        }
                    }
                    collidedWithTrackedAgent = false;
                }
                else
                {
                    collidingAgent = null;
                    collidedCount  = 0;
                }
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                stopTime++;
            }
        }