예제 #1
0
 public LastSeenTarget(LastSeen seen, IMyCubeBlock block = null)
 {
     m_lastSeen           = seen;
     m_block              = block;
     m_lastPostion        = m_lastSeen.LastKnownPosition;
     m_lastPositionUpdate = m_lastSeen.LastSeenAt;
     m_targetType         = TargetingOptions.GetTargetType(seen.Entity);
 }
예제 #2
0
        /// <summary>
        /// Targets a LastSeen chosen from the given storage, will overrride current target.
        /// </summary>
        /// <param name="storage">NetworkStorage to get LastSeen from.</param>
        public void GetLastSeenTarget(RelayStorage storage, double range)
        {
            if (Globals.UpdateCount < m_nextLastSeenSearch)
            {
                return;
            }
            m_nextLastSeenSearch = Globals.UpdateCount + 100ul;

            if (storage == null)
            {
                //Log.DebugLog("no storage", "GetLastSeenTarget()", Logger.severity.INFO);
                return;
            }

            if (storage.LastSeenCount == 0)
            {
                //Log.DebugLog("no last seen in storage", "GetLastSeenTarget()", Logger.severity.DEBUG);
                return;
            }

            LastSeen     processing;
            IMyCubeBlock targetBlock;

            if (CurrentTarget.Entity != null && storage.TryGetLastSeen(CurrentTarget.Entity.EntityId, out processing) && processing.isRecent())
            {
                LastSeenTarget lst = myTarget as LastSeenTarget;
                if (lst != null && lst.Block != null && !lst.Block.Closed)
                {
                    Log.TraceLog("Updating current last seen target");
                    lst.Update(processing);
                    CurrentTarget = myTarget;
                    return;
                }

                if (ChooseBlock(processing, out targetBlock))
                {
                    Log.TraceLog("Updating current last seen, chose a new block");
                    myTarget      = new LastSeenTarget(processing, targetBlock);
                    CurrentTarget = myTarget;
                    return;
                }
            }

            if (Options.TargetEntityId > 0L)
            {
                if (storage.TryGetLastSeen(Options.TargetEntityId, out processing))
                {
                    Log.TraceLog("Got last seen for entity id");
                    ChooseBlock(processing, out targetBlock);
                    myTarget      = new LastSeenTarget(processing, targetBlock);
                    CurrentTarget = myTarget;
                }
                //else
                //	Log.DebugLog("failed to get last seen from entity id", "GetLastSeenTarget()");
                return;
            }

            processing  = null;
            targetBlock = null;

            if (SEAD)
            {
                throw new NotImplementedException();
                //float highestPowerLevel = 0f;

                //storage.ForEachLastSeen((LastSeen seen) => {
                //	if (seen.isRecent() && Options.CanTargetType(seen.Entity) && CanConsiderHostile(seen.Entity))
                //	{
                //		IMyCubeBlock block;
                //		float powerLevel;
                //		if (RadarEquipment_old.GetRadarEquipment(seen, out block, out powerLevel) && powerLevel > highestPowerLevel)
                //		{
                //			highestPowerLevel = powerLevel;
                //			processing = seen;
                //			targetBlock = block;
                //		}
                //	}
                //});
            }
            else
            {
                Vector3D   myPos       = ProjectilePosition();
                TargetType bestType    = TargetType.LowestPriority;
                double     maxRange    = range * range;
                double     closestDist = maxRange;

                storage.ForEachLastSeen(seen => {
                    TargetType typeOfSeen = TargetingOptions.GetTargetType(seen.Entity);
                    if (typeOfSeen <= bestType && Options.CanTargetType(typeOfSeen) && seen.isRecent() && CanConsiderHostile(seen.Entity))
                    {
                        IMyCubeBlock block;
                        if (!ChooseBlock(seen, out block) || !CheckWeaponsTargeting(typeOfSeen, seen.Entity))
                        {
                            return;
                        }

                        if (typeOfSeen == bestType && targetBlock != null && block == null)
                        {
                            return;
                        }

                        double dist = Vector3D.DistanceSquared(myPos, seen.LastKnownPosition);
                        if ((typeOfSeen < bestType && dist < maxRange) || dist < closestDist)
                        {
                            closestDist = dist;
                            bestType    = typeOfSeen;
                            processing  = seen;
                            targetBlock = block;
                        }
                    }
                });

                Log.DebugLog(() => "chose last seen with entity: " + processing.Entity.nameWithId() + ", block: " + targetBlock.getBestName() + ", type: " + bestType + ", distance squared: " + closestDist + ", position: " + processing.Entity.GetPosition(), condition: processing != null);
                Log.DebugLog("no last seen target found", condition: processing == null);
            }

            if (processing == null)
            {
                if (this is Guided.GuidedMissile)
                {
                    Log.TraceLog("GuidedMissile failed to get LastSeen target, keeping previous");
                    return;
                }

                //Log.DebugLog("failed to get a target from last seen", "GetLastSeenTarget()");
                myTarget      = NoTarget.Instance;
                CurrentTarget = myTarget;
            }
            else
            {
                myTarget      = new LastSeenTarget(processing, targetBlock);
                CurrentTarget = myTarget;
            }
        }