Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }