public void BuildChangedValues()
        {
            if (PositionChanged || RotationChanged)
            {
                BuildPoints();
                BuildBounds();
            }
            if (PositionChanged || this.HeightPosChanged)
            {
                LastPosition           = _position;
                PositionChangedBuffer  = true;
                PositionChanged        = false;
                this.SetVisualPosition = true;
                this.HeightPosChanged  = false;
            }
            else
            {
                PositionChangedBuffer  = false;
                this.SetVisualPosition = false;
            }

            if (RotationChanged)
            {
                _rotation.Normalize();
                RotationChangedBuffer  = true;
                RotationChanged        = false;
                this.SetVisualRotation = true;
            }
            else
            {
                RotationChangedBuffer  = false;
                this.SetVisualRotation = false;
            }
        }
Exemple #2
0
        void BuildChangedValues()
        {
            if (PositionChanged || RotationChanged)
            {
                BuildPoints();
                BuildBounds();
                //Reset this value so we're not permanently considered colliding against wall
                this.ImmovableCollisionDirection = Vector2d.zero;
            }

            if (PositionChanged || this.HeightPosChanged)
            {
                PositionChangedBuffer       = true;
                PositionChanged             = false;
                this._settingVisualsCounter = SETTING_VISUALS_COUNT;
                this.HeightPosChanged       = false;
            }
            else
            {
                PositionChangedBuffer = false;
            }

            if (RotationChanged)
            {
                _rotation.Normalize();
                RotationChangedBuffer       = true;
                RotationChanged             = false;
                this._settingVisualsCounter = SETTING_VISUALS_COUNT;
            }
            else
            {
                RotationChangedBuffer = false;
            }
        }
Exemple #3
0
        private void ApplyCone(Vector3d center3d, Vector2d forward, long radius, long angle)
        {
            Vector2d center    = center3d.ToVector2d();
            long     fastRange = radius * radius;

            Scan(center, radius);
            for (int i = 0; i < ScanOutput.Count; i++)
            {
                LSAgent  agent      = ScanOutput[i];
                Vector2d agentPos   = agent.Body._position;
                Vector2d difference = agentPos - center;

                if (difference.FastMagnitude() > fastRange)
                {
                    continue;
                }
                if (forward.Dot(difference) < 0)
                {
                    continue;
                }
                difference.Normalize();

                long cross = forward.Cross(difference).Abs();
                if (cross > angle)
                {
                    continue;
                }
                HitAgent(agent);
            }
        }
Exemple #4
0
        public void DistributeCircle_Box(LSBody box, LSBody circle)
        {
            xMore = circle._position.x > box._position.x;
            yMore = circle._position.y > box._position.y;

            if (xMore)
            {
                PenetrationX = (circle.XMin - box.XMax);
            }
            else
            {
                PenetrationX = (circle.XMax - box.XMin);
            }
            if (yMore)
            {
                PenetrationY = (circle.YMin - box.YMax);
            }
            else
            {
                PenetrationY = (circle.YMax - box.YMin);
            }

            //PenetrationX = PenetrationX + circle.Velocity.x;
            //PenetrationY = PenetrationY + circle.Velocity.y;
            xAbs = PenetrationX < 0 ? -PenetrationX : PenetrationX;
            yAbs = PenetrationY < 0 ? -PenetrationY : PenetrationY;

            if ((xAbs <= circle.Radius && yAbs <= circle.Radius))
            {
                Vector2d corner;
                corner.x = xMore ? box.Position.x + box.HalfWidth : box.Position.x - box.HalfWidth;
                corner.y = yMore ? box.Position.y + box.HalfHeight : box.Position.y - box.HalfHeight;
                Vector2d dir = circle.Position - corner;
                dir.Normalize();

                circle.Position = corner + dir * circle.Radius;
            }
            else
            {
                if (xAbs > yAbs)
                {
                    PenetrationX = 0;
                    //if (yAbs < circle.Radius) PenetrationY = PenetrationY * yAbs / circle.Radius;
                }
                else
                {
                    PenetrationY = 0;
                    //if (xAbs < circle.Radius) PenetrationX = PenetrationX * xAbs / circle.Radius;
                }
                //Resolving
                circle._position.x -= PenetrationX;
                circle._position.y -= PenetrationY;
            }



            circle.PositionChanged = true;
            circle.BuildBounds();
        }
 public static void PrepareAxisCheck(Vector2d p1, Vector2d p2)
 {
     cacheP1   = p1;
     cacheP2   = p2;
     cacheAxis = p2 - p1;
     cacheAxis.Normalize();
     axisMin       = p1.Dot(cacheAxis.x, cacheAxis.y);
     axisMax       = p2.Dot(cacheAxis.x, cacheAxis.y);
     cacheProjPerp = cacheP1.Cross(cacheAxis.x, cacheAxis.y);
 }
        private void CalculateRotationValues()
        {
            Vector2d newRot = Forward.ToVector2d().ToRotation();
            long     newRotMag;

            newRot.Normalize(out newRotMag);
            this.Forward2d            = newRot.ToDirection();
            this.Agent.Body.Rotation  = newRot;
            this.TargetVisualRotation = Quaternion.LookRotation(Forward.ToVector3());
        }
Exemple #7
0
 private void HandleContact(LSBody other)
 {
     if (targetReached == true && Agent.IsCasting == false && !(Agent.Body.Immovable || Agent.Body.IsTrigger))
     {
         Vector2d delta = this.Agent.Body._position - this.Agent.Body.LastPosition;
         if (delta.FastMagnitude() > collisionTurnThreshold)
         {
             delta.Normalize();
             this.StartTurnDirection(delta);
         }
     }
 }
        public static void PrepareAxisCheck(Vector2d p1, Vector2d p2, bool calculateIntersectionPoints = true)
        {
            cacheP1   = p1;
            cacheP2   = p2;
            cacheAxis = p2 - p1;
            cacheAxis.Normalize();
            cacheAxisNormal = cacheAxis.rotatedLeft;

            axisMin       = p1.Dot(cacheAxis.x, cacheAxis.y);
            axisMax       = p2.Dot(cacheAxis.x, cacheAxis.y);
            cacheProj     = cacheP1.Dot(cacheAxis.x, cacheAxis.y);
            cacheProjPerp = cacheP1.Dot(cacheAxisNormal.x, cacheAxisNormal.y);
            perpVector    = cacheAxisNormal * cacheProjPerp;

            calculateIntersections = calculateIntersectionPoints;
        }
Exemple #9
0
        private void CalculateDistancesAndAxes()
        {
            if (Points.Length == 1)
            {
                return;
            }
            else if (Points.Length <= 0)
            {
                return;
            }
            int connectionCount = Points.Length + (Looped ? 0 : -1);

            EnsureSize(ref _pointDistances, connectionCount);
            EnsureSize(ref _axes, connectionCount);
            EnsureSize(ref _cachedPointMags, connectionCount);
            long     totalLength = 0;
            Vector2d lastPoint   = Points [0];

            for (int i = 1; i < Points.Length; i++)
            {
                Vector2d curPoint = Points [i];
                Vector2d delta    = curPoint - lastPoint;
                long     dist;
                delta.Normalize(out dist);
                PointDistances [i - 1]  = dist;
                _axes [i - 1]           = delta;
                _cachedPointMags[i - 1] = totalLength;

                totalLength += dist;

                lastPoint = curPoint;
            }
            if (Looped)
            {
                Vector2d delta = _points [0] - lastPoint;
                long     dist;
                delta.Normalize(out dist);
                PointDistances [_points.Length - 1]  = dist;
                _axes [_points.Length - 1]           = delta;
                _cachedPointMags[_points.Length - 1] = totalLength;

                totalLength += dist;
            }

            TotalLength = totalLength;
        }
 void CheckAutoturn()
 {
     if (isColliding)
     {
         isColliding = false;
         //autoturn direction will be culmination of positional changes
         if (targetReached == true && Agent.IsCasting == false && !(Agent.Body.Immovable || Agent.Body.IsTrigger))
         {
             Vector2d delta = this.Agent.Body._position - this.Agent.Body.LastPosition;
             if (delta.FastMagnitude() > collisionTurnThreshold)
             {
                 delta.Normalize();
                 this.StartTurnDirection(delta);
             }
         }
     }
 }
        public void BuildChangedValues()
        {
            if (PositionChanged || RotationChanged)
            {
                BuildPoints();
                BuildBounds();
            }
            if (PositionChanged)
            {
                PositionChangedBuffer  = true;
                PositionChanged        = false;
                this.SetVisualPosition = true;
            }
            else
            {
                PositionChangedBuffer  = false;
                this.SetVisualPosition = false;
            }

            if (RotationChanged)
            {
                if (HasParent)
                {
                    LocalRotation.Normalize();
                }
                else
                {
                    Rotation.Normalize();
                }
                RotationChangedBuffer  = true;
                RotationChanged        = false;
                this.SetVisualRotation = true;
            }
            else
            {
                RotationChangedBuffer  = false;
                this.SetVisualRotation = false;
            }
        }
Exemple #12
0
        public void BuildPoints()
        {
            if (Shape != ColliderType.Polygon)
            {
                return;
            }
            int VertLength = Vertices.Length;

            if (RotationChanged)
            {
                for (int i = 0; i < VertLength; i++)
                {
                    RotatedPoints[i] = Vertices[i];
                    RotatedPoints[i].Rotate(_rotation.x, _rotation.y);
                }
                for (int i = VertLength - 1; i >= 0; i--)
                {
                    int      nextIndex = i + 1 < VertLength ? i + 1 : 0;
                    Vector2d point     = RotatedPoints[nextIndex];
                    point.Subtract(ref RotatedPoints[i]);
                    point.Normalize();
                    Edges[i] = point;
                    point.RotateRight();
                    EdgeNorms[i] = point;
                }
                if (!OutMoreThanSet)
                {
                    OutMoreThanSet = true;
                    long dot = Edges[0].Cross(Edges[1]);
                    this.OutMoreThan = dot < 0;
                }
            }
            for (int i = 0; i < VertLength; i++)
            {
                RealPoints[i].x = RotatedPoints[i].x + _position.x;
                RealPoints[i].y = RotatedPoints[i].y + _position.y;
            }
        }
Exemple #13
0
        void BehaveWithTarget()
        {
            if (Target.IsActive == false || Target.SpawnVersion != targetVersion ||
                (this.TargetAllegiance & Agent.GetAllegiance(Target)) == 0)
            {
                StopEngage();
                BehaveWithNoTarget();
                return;
            }
            if (IsWindingUp)
            {
                windupCount--;
                if (windupCount < 0)
                {
                    if (this.AgentConditional(Target))
                    {
                        Fire();
                        while (this.attackCount < 0)
                        {
                            this.attackCount += (this.AttackInterval);
                        }
                        this.attackCount -= Windup;
                        IsWindingUp       = false;
                    }
                    else
                    {
                        StopEngage();
                        this.ScanAndEngage();
                    }
                }
            }
            else
            {
                Vector2d targetDirection = Target.Body._position - cachedBody._position;
                long     fastMag         = targetDirection.FastMagnitude();

                if (fastMag <= fastRangeToTarget)
                {
                    if (!inRange)
                    {
                        if (CanMove)
                        {
                            cachedMove.StopMove();
                        }
                    }
                    Agent.SetState(EngagingAnimState);

                    long mag;
                    targetDirection.Normalize(out mag);
                    bool withinTurn = TrackAttackAngle == false ||
                                      (fastMag != 0 &&
                                       cachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                       cachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= AttackAngle);
                    bool needTurn = mag != 0 && !withinTurn;
                    if (needTurn)
                    {
                        if (CanTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        if (attackCount <= 0)
                        {
                            StartWindup();
                        }
                    }

                    if (inRange == false)
                    {
                        inRange = true;
                    }
                }
                else
                {
                    if (CanMove)
                    {
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(Target.Body._position);
                            cachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (Target.Body.PositionChangedBuffer || inRange)
                            {
                                cachedMove.Destination = Target.Body._position;
                            }
                        }
                    }

                    if (isAttackMoving || isFocused == false)
                    {
                        searchCount -= 1;
                        if (searchCount <= 0)
                        {
                            searchCount = SearchRate;
                            if (ScanAndEngage())
                            {
                            }
                            else
                            {
                            }
                        }
                    }
                    if (inRange == true)
                    {
                        inRange = false;
                    }
                }
            }
        }
Exemple #14
0
        void BehaveWithTarget()
        {
            if (Target.IsActive == false || Target.SpawnVersion != targetVersion ||
                (this.TargetAllegiance & Agent.GetAllegiance(Target)) == 0)
            {
                //Target no longer exists
                StopEngage();
                BehaveWithNoTarget();
                return;
            }

            if (!IsWindingUp)
            {
                Vector2d targetDirection = Target.Body._position - cachedBody._position;
                long     fastMag         = targetDirection.FastMagnitude();

                if (CheckRange())
                {
                    if (!inRange)
                    {
                        if (CanMove)
                        {
                            cachedMove.StopMove();
                        }
                        inRange = true;
                    }
                    Agent.SetState(EngagingAnimState);

                    long mag;
                    targetDirection.Normalize(out mag);
                    bool withinTurn = TrackAttackAngle == false ||
                                      (fastMag != 0 &&
                                       cachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                       cachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= AttackAngle);
                    bool needTurn = mag != 0 && !withinTurn;
                    if (needTurn)
                    {
                        if (CanTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        if (attackCount <= 0)
                        {
                            StartWindup();
                        }
                    }
                }
                else
                {
                    if (CanMove)
                    {
                        cachedMove.PauseAutoStop();
                        cachedMove.PauseCollisionStop();
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(Target.Body._position);
                            cachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (inRange)
                            {
                                cachedMove.Destination = Target.Body.Position;
                            }
                            else
                            {
                                if (repathTimer.AdvanceFrame())
                                {
                                    if (Target.Body.PositionChangedBuffer &&
                                        Target.Body.Position.FastDistance(cachedMove.Destination.x, cachedMove.Destination.y) >= (repathDistance * repathDistance))
                                    {
                                        cachedMove.StartMove(Target.Body._position);
                                        //So units don't sync up and path on the same frame
                                        repathTimer.AdvanceFrames(repathRandom);
                                    }
                                }
                            }
                        }
                    }

                    if (IsAttackMoving || isFocused == false)
                    {
                        searchCount -= 1;
                        if (searchCount <= 0)
                        {
                            searchCount = SearchRate;
                            if (ScanAndEngage())
                            {
                            }
                            else
                            {
                            }
                        }
                    }
                    if (inRange == true)
                    {
                        inRange = false;
                    }
                }
            }

            if (IsWindingUp)
            {
                windupCount -= LockstepManager.DeltaTime;
                if (windupCount < 0)
                {
                    if (this.AgentConditional(Target))
                    {
                        Fire();
                        int counter = 0;
                        while (this.attackCount <= 0)
                        {
                            this.attackCount += (this.AttackInterval);
                            counter++;
                            if (counter > 1)
                            {
                                Debug.Log("asdf" + this.attackCount.ToDouble());
                            }
                        }
                        this.attackCount -= Windup;
                    }
                    else
                    {
                        StopEngage();
                        this.ScanAndEngage();
                    }
                    IsWindingUp = false;
                }
            }

            if (inRange)
            {
                cachedMove.PauseAutoStop();
                cachedMove.PauseCollisionStop();
            }
        }
Exemple #15
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);
        }
Exemple #16
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++;
            }
        }
Exemple #17
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;
        }
Exemple #18
0
        void BehaveWithTarget()
        {
            if (Target.IsActive == false || Target.SpawnVersion != targetVersion ||
                (this.TargetAllegiance & Agent.GetAllegiance(Target)) == 0)
            {
                //Target's lifecycle has ended
                StopEngage();
                BehaveWithNoTarget();
                return;
            }

            if (!IsWindingUp)
            {
                Vector2d targetDirection = Target.Body._position - cachedBody._position;
                long     fastMag         = targetDirection.FastMagnitude();

                //TODO: Optimize this instead of recalculating magnitude multiple times
                if (CheckRange())
                {
                    if (!inRange)
                    {
                        if (CanMove)
                        {
                            cachedMove.StopMove();
                        }
                        inRange = true;
                    }
                    Agent.SetState(EngagingAnimState);

                    long mag;
                    targetDirection.Normalize(out mag);
                    bool withinTurn = TrackAttackAngle == false ||
                                      (fastMag != 0 &&
                                       cachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                       cachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= AttackAngle);
                    bool needTurn = mag != 0 && !withinTurn;
                    if (needTurn)
                    {
                        if (CanTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                    }
                    else
                    {
                        if (attackCount >= AttackInterval)
                        {
                            StartWindup();
                        }
                    }
                }
                else
                {
                    if (CanMove)
                    {
                        cachedMove.PauseAutoStop();
                        cachedMove.PauseCollisionStop();
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(Target.Body._position);
                            cachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (inRange)
                            {
                                cachedMove.Destination = Target.Body.Position;
                            }
                            else
                            {
                                if (repathTimer.AdvanceFrame())
                                {
                                    if (Target.Body.PositionChangedBuffer &&
                                        Target.Body.Position.FastDistance(cachedMove.Destination.x, cachedMove.Destination.y) >= (repathDistance * repathDistance))
                                    {
                                        cachedMove.StartMove(Target.Body._position);
                                        //So units don't sync up and path on the same frame
                                        repathTimer.AdvanceFrames(repathRandom);
                                    }
                                }
                            }
                        }
                    }

                    if (IsAttackMoving || isFocused == false)
                    {
                        searchCount -= 1;
                        if (searchCount <= 0)
                        {
                            searchCount = SearchRate;
                            if (ScanAndEngage())
                            {
                            }
                            else
                            {
                            }
                        }
                    }
                    if (inRange == true)
                    {
                        inRange = false;
                    }
                }
            }
            if (IsWindingUp)
            {
                //TODO: Do we need AgentConditional checks here?
                windupCount += LockstepManager.DeltaTime;
                if (CanTurn)
                {
                    Vector2d targetVector = Target.Body._position - cachedBody._position;
                    cachedTurn.StartTurnVector(targetVector);
                }
                if (windupCount >= Windup)
                {
                    windupCount = 0;
                    Fire();
                    while (this.attackCount >= AttackInterval)
                    {
                        //resetting back down after attack is fired
                        this.attackCount -= (this.AttackInterval);
                    }
                    this.attackCount += Windup;
                    IsWindingUp       = false;
                }
            }
            else
            {
                windupCount = 0;
            }
            if (inRange)
            {
                cachedMove.PauseAutoStop();
                cachedMove.PauseCollisionStop();
            }
        }
        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);
        }
Exemple #20
0
 public void TurnDirection(Vector2d targetDirection)
 {
     targetDirection.Normalize();
     StartTurn(targetDirection);
 }
        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++;
            }
        }
Exemple #22
0
 public void StartTurnVector(Vector2d targetVector)
 {
     targetVector.Normalize();
     StartTurnDirection(targetVector);
 }