Example #1
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;
        }
Example #2
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;
                    }
                    desiredVelocity = (movementDirection * (distance) / (closingDistance));
                }


                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++;
            }
        }
Example #3
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++;
            }
        }
Example #4
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   = 1;

                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 / LockstepManager.FrameRate;
                    }
                    else
                    {
                        desiredVelocity = (movementDirection);
                        stuckThreshold  = this.timescaledAcceleration;
                    }
                }
                //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
            {
                //Slowin' down
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody.Velocity += GetAdjustVector(Vector2d.zero);
                }
                StoppedTime++;
            }
            AutoStopPauser--;
            CollisionStopPauser--;
            StopPauseLooker--;
            AveragePosition = AveragePosition.Lerped(Agent.Body.Position, FixedMath.One / 2);
        }