예제 #1
0
 public void SetInitialWeaponReadiness()
 {
     foreach (var weapon in Weapons)
     {
         weapon.SetCurrentTargetAndAllowedAngle(_autoPilot.GetCurrentWaypoint(), this.WeaponMaxAngleFromTarget, this.MaxStaticWeaponRange, _validWaypoint, _autoPilot.Targeting.Target.GetEntity());
     }
 }
예제 #2
0
        public void ProcessStrafing()
        {
            if (!this.AllowStrafing)
            {
                return;
            }

            if (this.Strafing == false)
            {
                TimeSpan duration = MyAPIGateway.Session.GameDateTime - this.LastStrafeEndTime;
                if (duration.TotalMilliseconds >= this.ThisStrafeCooldown)
                {
                    //Logger.MsgDebug("Begin Strafe", DebugTypeEnum.AutoPilot);
                    this.LastStrafeStartTime = MyAPIGateway.Session.GameDateTime;
                    this.ThisStrafeDuration  = Rnd.Next(StrafeMinDurationMs, StrafeMaxDurationMs);
                    this.Strafing            = true;

                    MyAPIGateway.Parallel.Start(() => {
                        _autoPilot.Collision.RunSecondaryCollisionChecks();
                        this.CurrentAllowedStrafeDirections = Vector3I.Zero;
                        this.CurrentStrafeDirections        = new Vector3I(Rnd.Next(-1, 2), Rnd.Next(-1, 2), Rnd.Next(-1, 2));

                        if (this.CurrentStrafeDirections.X != 0)
                        {
                            this.CurrentAllowedStrafeDirections.X = 1;
                            var rAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Right, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            var lAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Left, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            bool rTargetIntersect = (rAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);
                            bool lTargetIntersect = (lAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);

                            if (this.CurrentStrafeDirections.X == 1)
                            {
                                if (rTargetIntersect || (_collision.RightResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.LeftResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: X Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.X *= -1;
                                }
                                else if (lTargetIntersect || (_collision.RightResult.HasTarget(StrafeMinimumTargetDistance) && _collision.LeftResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: X Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.X = 0;
                                }
                            }
                            else
                            {
                                if (lTargetIntersect || (_collision.LeftResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.RightResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: X Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.X *= -1;
                                }
                                else if (rTargetIntersect || (_collision.LeftResult.HasTarget(StrafeMinimumTargetDistance) && _collision.RightResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: X Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.X = 0;
                                }
                            }
                        }

                        if (this.CurrentStrafeDirections.Y != 0)
                        {
                            this.CurrentAllowedStrafeDirections.Y = 1;
                            var uAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Up, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            var dAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Down, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            bool uTargetIntersect = (uAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);
                            bool dTargetIntersect = (dAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);

                            if (this.CurrentStrafeDirections.Y == 1)
                            {
                                if (uTargetIntersect || (_collision.UpResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.DownResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Y Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Y *= -1;
                                }
                                else if (dTargetIntersect || (_collision.UpResult.HasTarget(StrafeMinimumTargetDistance) && _collision.DownResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Y Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Y = 0;
                                }
                            }
                            else
                            {
                                if (dTargetIntersect || (_collision.DownResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.UpResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Y Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Y *= -1;
                                }
                                else if (uTargetIntersect || (_collision.DownResult.HasTarget(StrafeMinimumTargetDistance) && _collision.UpResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Y Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Y = 0;
                                }
                            }
                        }

                        if (this.CurrentStrafeDirections.Z != 0)
                        {
                            this.CurrentAllowedStrafeDirections.Z = 1;
                            var fAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Forward, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            var bAngle            = VectorHelper.GetAngleBetweenDirections(_collision.Matrix.Backward, Vector3D.Normalize(_autoPilot.GetCurrentWaypoint() - _collision.Matrix.Translation));
                            bool fTargetIntersect = (fAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);
                            bool bTargetIntersect = (bAngle < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance);

                            if (this.CurrentStrafeDirections.Z == 1)
                            {
                                if (fTargetIntersect || (_collision.ForwardResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.BackwardResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Z Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Z *= -1;
                                }
                                else if (bTargetIntersect || (_collision.ForwardResult.HasTarget(StrafeMinimumTargetDistance) && _collision.BackwardResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Z Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Z = 0;
                                }
                            }
                            else
                            {
                                if (bTargetIntersect || (_collision.BackwardResult.HasTarget(StrafeMinimumTargetDistance) && !_collision.ForwardResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Z Reverse", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Z *= -1;
                                }
                                else if (fTargetIntersect || (_collision.BackwardResult.HasTarget(StrafeMinimumTargetDistance) && _collision.ForwardResult.HasTarget(StrafeMinimumTargetDistance)))
                                {
                                    Logger.MsgDebug("Strafe: Z Negate", DebugTypeEnum.AutoPilot);
                                    this.CurrentStrafeDirections.Z = 0;
                                }
                            }
                        }

                        if (_autoPilot.UpDirectionFromPlanet != Vector3D.Zero && _autoPilot.MyAltitude < _autoPilot.MinimumPlanetAltitude)
                        {
                            var thrustDir = VectorHelper.GetThrustDirectionsAwayFromDirection(_collision.Matrix, -_autoPilot.UpDirectionFromPlanet);

                            if (thrustDir.X != 0)
                            {
                                this.CurrentAllowedStrafeDirections.X = 1;
                                this.CurrentStrafeDirections.X        = thrustDir.X;
                            }

                            if (thrustDir.Y != 0)
                            {
                                this.CurrentAllowedStrafeDirections.Y = 1;
                                this.CurrentStrafeDirections.Y        = thrustDir.Y;
                            }

                            if (thrustDir.Z != 0)
                            {
                                this.CurrentAllowedStrafeDirections.Z = 1;
                                this.CurrentStrafeDirections.Z        = thrustDir.Z;
                            }
                        }
                    }, () => {
                        SetThrust(this.CurrentAllowedStrafeDirections, this.CurrentStrafeDirections);
                    });
                }
            }
            else
            {
                TimeSpan duration = MyAPIGateway.Session.GameDateTime - this.LastStrafeStartTime;

                if (duration.TotalMilliseconds >= this.ThisStrafeDuration)
                {
                    //Logger.MsgDebug("End Strafe", DebugTypeEnum.General);
                    this.InvertStrafingActivated = false;
                    this.LastStrafeEndTime       = MyAPIGateway.Session.GameDateTime;
                    this.ThisStrafeCooldown      = Rnd.Next(StrafeMinCooldownMs, StrafeMaxCooldownMs);
                    this.Strafing                   = false;
                    _collisionStrafeAdjusted        = false;
                    _minAngleDistanceStrafeAdjusted = false;
                    _collisionStrafeDirection       = Vector3D.Zero;
                    SetThrust(new Vector3I(0, 0, 0), new Vector3I(0, 0, 0));
                    //Logger.AddMsg("Cooldown: " + this.ThisStrafeCooldown.ToString(), true);
                }
                else
                {
                    //Logger.MsgDebug("Strafe Collision: " + _collision.VelocityResult.CollisionImminent.ToString() + " - " + _collision.VelocityResult.Time.ToString(), DebugTypeEnum.Collision);

                    if (!_collisionStrafeAdjusted && _collision.VelocityResult.CollisionImminent())
                    {
                        Logger.MsgDebug("Strafe Velocity Collision Detect: " + _collision.VelocityResult.Type.ToString() + ", " + _collision.VelocityResult.GetCollisionDistance(), DebugTypeEnum.Collision);
                        _collisionStrafeAdjusted = true;
                        StopStrafeDirectionNearestPosition(_collision.VelocityResult.GetCollisionCoords());
                        _collisionStrafeDirection = Vector3D.Normalize(_collision.VelocityResult.GetCollisionCoords() - RemoteControl.WorldMatrix.Translation);
                    }
                    else if (_collisionStrafeAdjusted && VectorHelper.GetAngleBetweenDirections(_collisionStrafeDirection, Vector3D.Normalize(_collision.Velocity - RemoteControl.WorldMatrix.Translation)) > 15)
                    {
                        Logger.MsgDebug("Strafe Collision Detect", DebugTypeEnum.General);
                        StopStrafeDirectionNearestPosition(_collision.VelocityResult.GetCollisionCoords());
                        _collisionStrafeDirection = Vector3D.Normalize(_collision.VelocityResult.GetCollisionCoords() - RemoteControl.WorldMatrix.Translation);
                    }

                    if (_minAngleDistanceStrafeAdjusted && _autoPilot.AngleToCurrentWaypoint < this.StrafeMinimumSafeAngleFromTarget && _autoPilot.DistanceToCurrentWaypoint < this.StrafeMinimumTargetDistance)
                    {
                        Logger.MsgDebug("Strafe Min Dist/Angle Detect", DebugTypeEnum.General);
                        _minAngleDistanceStrafeAdjusted = false;
                        StopStrafeDirectionNearestPosition(_collision.VelocityResult.GetCollisionCoords());
                        _collisionStrafeDirection = Vector3D.Normalize(_collision.VelocityResult.GetCollisionCoords() - RemoteControl.WorldMatrix.Translation);
                    }
                }
            }
        }