Example #1
0
        public override void Close()
        {
            if (m_dangerZoneId != -1)
            {
                MyDangerZones.Instance.Unregister(this);
                m_dangerZoneId = -1;
            }

            MyEntities.OnEntityRemove -= MyEntities_OnEntityRemove;
            MySession.Static.LinkEntities -= OnLinkEntities;

            if (Followers.Count > 0)
            {
                MySmallShipBot newLeader = Followers[0];
                Debug.Assert(newLeader != this);

                newLeader.Leader = null;
                Debug.Assert(!newLeader.Closed);

                if (m_aiTemplate.IsPatroling() && newLeader.WaypointPath != null)
                {
                    SetWaypointPath(newLeader.WaypointPath.Name);
                    newLeader.Patrol();
                }
                else
                {
                    Idle();
                }

                var followers = Followers.ToArray();
                for (int i = 1; i < followers.Length; i++)
                {
                    followers[i].Follow(newLeader);
                }
                Followers.Remove(newLeader);
                Debug.Assert(Followers.Count == 0);
            }
            
            StopFollow();

            if (m_currentBehavior != null)
            {
                m_currentBehavior.Close(this);
                m_currentBehavior = null;
            }
            
            if (m_biochemEffect != null)
            {
                m_biochemEffect.Stop();
                m_biochemEffect = null;
            }
            WaypointPath = null;

            if (!IsSleeping)
                MySmallShipBot.TotalAliveBots--;
            
            base.Close();
        }
Example #2
0
        private void UpdateBehavior()
        {
            // Remove topmost ignored desires
            while (m_desires.Count > 0 && m_aiTemplate.IsIgnored(m_desires[m_desires.Count - 1].DesireType))
            {
                m_desires.RemoveAt(m_desires.Count - 1);
            }

            foreach (MyBotDesire desire in m_desires)
            {
                desire.Update();
            }

            // Remove invalid behavior
            if (m_currentBehavior != null && (
                m_currentBehavior.IsInvalid() || (m_currentBehavior.SourceDesire != null && m_currentBehavior.SourceDesire.IsInvalid(this))))   // invalid desire or behavior
            {
                m_desires.Remove(m_currentBehavior.SourceDesire);
                m_currentBehavior.Close(this);
                m_currentBehavior = null;
            }


            if (m_desires.Count == 0)
            {
                if (m_currentBehavior == null ||
                    m_aiTemplate.GetBehaviorType(BotDesireType.IDLE) != m_currentBehavior.GetBehaviorType() ||    // if AItemplate is changed
                    m_currentBehavior.SourceDesire != null)                                     // current behavior is not idle behavior
                {
                    m_currentBehavior = m_aiTemplate.GetBehavior(BotDesireType.IDLE);
                    m_currentBehavior.Init(this);
                }
            }
            else
            {
                // Select first desire (sorted)
                MyBotDesire desire = m_desires[m_desires.Count - 1];

                // Don't update current behavior if it's the same
                if (m_currentBehavior != null && m_currentBehavior.SourceDesire != null &&
                    m_currentBehavior.GetBehaviorType() == m_aiTemplate.GetBehaviorType(m_currentBehavior.SourceDesire.DesireType) && 
                    m_currentBehavior.SourceDesire == desire)
                {
                    return;
                }

                if (m_currentBehavior != null)
                {
                    m_currentBehavior.Close(this);
                }
                
                m_currentBehavior = m_aiTemplate.GetBehavior(desire.DesireType);
                if (m_currentBehavior != null)
                {
                    m_currentBehavior.SourceDesire = desire;
                    m_currentBehavior.Init(this);
                }
            }
        }