Exemple #1
0
        public void SearchForTarget(string targetId, string classId, bool doSensorSweep)
        {
            if (IsMarkedForDeletion)
            {
                return;
            }
            //first, see if targetdetectedunit is still known
            var targetDet = OwnerPlayer.GetDetectedUnitById(targetId);
            if (targetDet != null)
            {
                InterceptTarget(targetDet);
                return;
            }
            GameManager.Instance.Log.LogDebug(
                string.Format("MissileUnit->SearchForTarget Missile {0} looks for detid={1} or classid={2}",
                this.ToShortString(), targetId, classId));
            if (doSensorSweep && Sensors.Any())
            {
                if (UnitClass.UnitType == GameConstants.UnitType.Missile)
                {
                    SetSensorsActivePassive(GameConstants.SensorType.Radar, true);
                }
                else if (UnitClass.UnitType == GameConstants.UnitType.Torpedo)
                {
                    SetSensorsActivePassive(GameConstants.SensorType.Sonar, true);
                }
                this.SensorSweep();
            }

            //otherwise, see if target with same unitclass can be found. UnitClass in 
            var unitClassTarget = GameManager.Instance.GetUnitClassById(classId);

            // Get ordered list by distance of detected units within our fuel distance
            IEnumerable<DetectedUnit> detectedUnitsOrdered;

            // Filter out targets outside the max search sector range
            if (MaxSectorRangeSearchDeg < 360)
            {
                var bearing = ActualBearingDeg.HasValue ? ActualBearingDeg.Value : 0.0;
                var maxDistanceM = FuelDistanceRemainingM;

                if (TargetPosition != null)
                {
                    bearing = MapHelper.CalculateBearingDegrees(Position.Coordinate, TargetPosition.Coordinate);
                    maxDistanceM = MapHelper.CalculateDistanceRoughM(Position.Coordinate, TargetPosition.Coordinate);
                }

                detectedUnitsOrdered = from d in OwnerPlayer.DetectedUnits
                                       where !d.IsMarkedForDeletion && d.Position != null && d.CanBeTargeted
                                       && MapHelper.IsWithinSector(Position.Coordinate, bearing,
                                                      MaxSectorRangeSearchDeg, d.Position.Coordinate)
                                       let distanceToTargetM = MapHelper.CalculateDistance3DM(d.Position, this.Position)
                                       where distanceToTargetM <= maxDistanceM
                                       orderby distanceToTargetM ascending
                                       select d;
            }
            else
            {
                detectedUnitsOrdered = from d in OwnerPlayer.DetectedUnits
                                       where !d.IsMarkedForDeletion && d.Position != null && d.CanBeTargeted
                                       let distanceToTargetM = MapHelper.CalculateDistance3DM(d.Position, this.Position)
                                       where distanceToTargetM <= FuelDistanceRemainingM
                                       orderby distanceToTargetM ascending
                                       select d;
            }

            //var weaponClass = GameManager.Instance.GetWeaponClassById(this.WeaponClassId);
            foreach (var det in detectedUnitsOrdered)
            {
                if (unitClassTarget != null)
                {
                    if (det.IsIdentified && det.RefersToUnit != null)
                    {
                        if (det.RefersToUnit.UnitClass.Id == unitClassTarget.Id)
                        {
                            InterceptTarget(det);
                            return;
                        }
                    }
                    else
                    {
                        if (LaunchWeapon != null
                            && det.RefersToUnit != null
                            && LaunchWeapon.CanTargetDetectedUnit(det.RefersToUnit, true))
                        {
                            InterceptTarget(det);
                            return;
                        }
                    }
                }
                else if (this.LaunchWeapon != null && this.LaunchWeapon.CanTargetDetectedUnit(det, true))
                {
                    InterceptTarget(det);
                    return;
                }
            }
            if (TargetDetectedUnit == null || TargetDetectedUnit.IsMarkedForDeletion)
            {
                if (MovementOrder != null && MovementOrder.CountWaypoints == 1)
                {
                    var wp = MovementOrder.PeekNextWaypoint();
                    if (MapHelper.CalculateDistance3DM(Position, wp.Position) < GameConstants.DISTANCE_TO_TARGET_IS_HIT_M)
                    {
                        GameManager.Instance.Log.LogDebug(string.Format("SearchForTarget: Missile {0} waypoint was close: deleted.", this));
                        MovementOrder = null;
                        //this.ActiveWaypoint = null;
                    }
                }
            }
            if ((TargetDetectedUnit == null || TargetDetectedUnit.IsMarkedForDeletion)
                && (MovementOrder == null || MovementOrder.CountWaypoints == 0 || GetActiveWaypoint() == null)) //can't find anything. self-destruct
            {
                GameManager.Instance.Log.LogDebug(
                    string.Format("MissileUnit->SearchForTarget Missile {0} found no target and is being deleted.",
                    this.ToShortString()));

                this.IsMarkedForDeletion = true;
            }
        }