protected override void UpdateInternal(AIStateInfo aIState)
 {
     if (distances != null)
     {
         int   length   = directions.Length;
         float distance = aIState.VectorToTarget.Magnitude - (aIState.Target.BoundingRadius + source.BoundingRadius);
         float angle    = aIState.VectorToTarget.Angle;
         this.aimAngle = MathHelper.PI + angle;
         float aimAngle;
         for (int pos = 0; pos < length; ++pos)
         {
             if (distance < distances[pos] * 2)
             {
                 aimAngle = angle - directions[pos];
                 if (MathHelper.Abs(MathHelper.GetAngleDifference(angle, aimAngle)) <
                     MathHelper.Abs(MathHelper.GetAngleDifference(angle, this.aimAngle)))
                 {
                     this.shouldAim = true;
                     this.aimAngle  = aimAngle;
                 }
                 if (distance < distances[pos] &&
                     Logic.InArc(source.DirectionAngle - directions[pos],
                                 angle,
                                 distance,
                                 aIState.Target.BoundingRadius,
                                 .1f))
                 {
                     this.shouldActivate = true;
                     return;
                 }
             }
         }
     }
 }
        protected override void UpdateInternal(AIStateInfo aIState)
        {
            float distance = aIState.VectorToTarget.Magnitude;
            float aimAngle = 0;
            float angle    = aIState.VectorToTarget.Angle;
            int   length   = action.Weapons.Length;

            for (int pos = 0; pos < length; ++pos)
            {
                float range = action.Weapons[pos].LifeTime.TimeLeft *
                              (action.Weapons[pos].MovementInfo.MaxLinearVelocity + aIState.VectorToTarget.Normalized * source.Current.Velocity.Linear) +
                              (source.BoundingRadius + aIState.Target.BoundingRadius + action.Weapons[pos].BoundingRadius);
                if (range > distance)
                {
                    if (action.Weapons[pos].IsFireAndForget)
                    {
                        this.aimAngle = angle - action.VelocityAngles[pos];
                        float diff = MathHelper.GetAngleDifference(this.aimAngle,
                                                                   source.DirectionAngle);
                        this.shouldActivate = MathHelper.Abs(diff) < MathHelper.HALF_PI;
                        this.shouldAim      = !this.shouldActivate;
                        return;
                    }
                    else
                    {
                        if (Logic.TrySolveInterceptAngle(
                                source.Current.Position.Linear,
                                aIState.Target.Current.Position.Linear,
                                aIState.Target.Current.Velocity.Linear - source.Current.Velocity.Linear,
                                action.Weapons[pos].MovementInfo.MaxLinearVelocity,
                                out aimAngle))
                        {
                            aimAngle += action.VelocityAngles[pos];
                        }


                        if (MathHelper.Abs(MathHelper.GetAngleDifference(angle, aimAngle)) <
                            MathHelper.Abs(MathHelper.GetAngleDifference(angle, this.aimAngle)))
                        {
                            this.shouldAim = true;
                            this.aimAngle  = aimAngle;
                        }
                        if (Logic.InArc(source.DirectionAngle + action.VelocityAngles[pos],
                                        aimAngle,
                                        distance,
                                        aIState.Target.BoundingRadius,
                                        GunActionAIInfo.AimTolerance + GetRandomVelocityAngle(pos)))
                        {
                            this.shouldActivate = true;
                            return;
                        }
                    }
                }
            }
        }
        public void Update(AIStateInfo aIState)
        {
            this.shouldActivate = false;
            this.shouldAim      = false;
            bool test = this.cost == null || this.source.ShipState.MeetsCost(-this.cost);

            if (test)
            {
                UpdateInternal(aIState);
            }
        }
 protected override void UpdateInternal(AIStateInfo aIState)
 {
     for (int pos = 0; pos < aIState.Threats.Length; ++pos)
     {
         if (aIState.Threats[pos])
         {
             this.shouldActivate = true;
             return;
         }
     }
 }
 protected override void UpdateInternal(AIStateInfo aIState)
 {
     if (cost != null)
     {
         if (cost.Health > 0)
         {
             this.shouldActivate = source.ShipState.Energy.HasRoomFor(-cost.Energy * 1.2f);
         }
         else
         {
             this.shouldActivate = true;
         }
     }
 }
 protected override void UpdateInternal(AIStateInfo aIState)
 {
     if (action.Radius > aIState.VectorToTarget.Magnitude)
     {
         this.shouldActivate = true;
         return;
     }
     for (int pos = 0; pos < aIState.Threats.Length; ++pos)
     {
         if (aIState.Threats[pos])
         {
             this.shouldActivate = true;
             break;
         }
     }
 }
        protected override void UpdateInternal(AIStateInfo aIState)
        {
            if (action.CurrentWeapon == null)
            {
                float distance = aIState.VectorToTarget.Magnitude;

                float range = action.Weapon.LifeTime.TimeLeft *
                              (action.Weapon.MovementInfo.MaxLinearVelocity + aIState.VectorToTarget.Normalized * source.Current.Velocity.Linear) +
                              (source.BoundingRadius + aIState.Target.BoundingRadius + action.Weapon.BoundingRadius);
                if (range > distance)
                {
                    if (action.Weapon.IsFireAndForget)
                    {
                        this.aimAngle = aIState.VectorToTarget.Angle + action.VelocityAngle;
                        float diff = MathHelper.GetAngleDifference(this.aimAngle,
                                                                   source.DirectionAngle);
                        this.shouldActivate = MathHelper.Abs(diff) < MathHelper.HALF_PI;
                        this.shouldAim      = !this.shouldActivate;
                    }
                    else if (Logic.TrySolveInterceptAngle(
                                 source.Current.Position.Linear,
                                 aIState.Target.Current.Position.Linear,
                                 aIState.Target.Current.Velocity.Linear - source.Current.Velocity.Linear,
                                 action.Weapon.MovementInfo.MaxLinearVelocity,
                                 out this.aimAngle))
                    {
                        this.aimAngle += action.VelocityAngle;
                        this.shouldAim = true;
                        float angle = aIState.VectorToTarget.Angle;
                        this.shouldActivate = Logic.InArc(source.DirectionAngle + action.VelocityAngle,
                                                          aimAngle,
                                                          distance,
                                                          aIState.Target.BoundingRadius,
                                                          GunActionAIInfo.AimTolerance);
                    }
                }
            }
            else
            {
                Vector2D fromcw = aIState.Target.Current.Position.Linear - action.CurrentWeapon.Current.Position.Linear;
                if (fromcw * action.CurrentWeapon.Current.Velocity.Linear > 0)
                {
                    this.shouldActivate = true;
                }
            }
        }
 protected override void UpdateInternal(AIStateInfo aIState)
 {
     this.shouldActivate = !source.IsInvisible;
 }
 protected abstract void UpdateInternal(AIStateInfo aIState);
        protected override void UpdateInternal(AIStateInfo aIState)
        {
            float distance = aIState.VectorToTarget.Magnitude;

            this.shouldActivate = distance > minRange && distance < maxRange;
        }
Ejemplo n.º 11
0
        public override ControlInput GetControlInput(float dt, ControlInput original)
        {
            if (rand.Next(0, 99) == 1)
            {
                base.target = null;
                base.CheckTarget();
            }
            overideDesired = false;


            IControlable[] obstacles = this.obstacles.ToArray();
            bool[]         threats   = new bool[obstacles.Length];
            for (int pos = 0; pos < obstacles.Length; ++pos)
            {
                threats[pos] = obstacles[pos].IsThreatTo(this.host);
            }
            AIStateInfo stateInfo    = null;
            int         actionsCount = Math.Min(3, shipHost.Actions.Count);

            if (target != null)
            {
                stateInfo = new AIStateInfo(
                    target,
                    target.Current.Position.Linear - host.Current.Position.Linear,
                    obstacles,
                    threats
                    );
                for (int pos = 0; pos < actionsCount; ++pos)
                {
                    IActionAIInfo actionAIInfo = shipHost.Actions[pos].AIInfo;
                    if (actionAIInfo != null)
                    {
                        actionAIInfo.Update(stateInfo);
                        if (!overideDesired && actionAIInfo.ShouldAim)
                        {
                            desiredAngle   = actionAIInfo.AimAngle;
                            overideDesired = true;
                        }
                        if (actionAIInfo.ShouldActivate)
                        {
                            original[InputAction.Action] = true;
                            original.ActiveActions[pos]  = true;
                        }
                    }
                }
            }
            if (!overideDesired)
            {
                for (int pos = 0; pos < obstacles.Length; ++pos)
                {
                    if (threats[pos])
                    {
                        IControlable obstacle = obstacles[pos];
                        Vector2D     dir2     = obstacle.Current.Position.Linear - host.Current.Position.Linear;
                        if ((obstacle.ControlableType & ControlableType.Ship) == ControlableType.Ship)
                        {
                            desiredAngle   = (-host.DirectionVector).Angle;
                            overideDesired = true;
                        }
                        else
                        {
                            desiredAngle   = (dir2 ^ (host.DirectionVector ^ dir2)).Angle;
                            overideDesired = true;
                        }
                        break;
                    }
                }
            }
            if (target != null)
            {
                IShipAIInfo shipAIInfo = shipHost.AIInfo;
                if (shipAIInfo != null)
                {
                    shipAIInfo.Update(stateInfo);
                    if (shipAIInfo.ShouldSetAngle)
                    {
                        overideDesired = true;
                        desiredAngle   = shipAIInfo.DesiredAngle;
                    }
                }
            }
            else if (!overideDesired)
            {
                if (host.Current.Velocity.Linear.Magnitude > host.MovementInfo.MaxLinearVelocity.Value * .05f)
                {
                    desiredAngle   = (-host.Current.Velocity.Linear).Angle;
                    overideDesired = true;
                    original       = base.GetControlInput(dt, original);
                    if (MathHelper.Abs(MathHelper.GetAngleDifference(desiredAngle, host.DirectionAngle)) < .1f)
                    {
                        original.ThrustPercent = 1;
                    }
                    else
                    {
                        original.ThrustPercent = 0;
                    }
                    return(original);
                }
            }
            return(base.GetControlInput(dt, original));
        }