예제 #1
0
        private void GetAction(GoapNode parentNode, List <GoapNode> goapNodeList, List <GoapAction> goapActionList, GoapStatus goapStatus)
        {
            for (int i = 0; i < goapActionList.Count; ++i)
            {
                GoapAction goapAction = goapActionList[i];

                GoapStatus preconditions = goapAction.GetPreconditions(); // 前置条件
                GoapStatus effectsStatus = goapAction.GetEffect();        // 执行效果

                if (!preconditions.IsContainIn(parentNode.CurrentStatus)) // 不满足前置条件
                {
                    continue;
                }

                GoapStatus newStatus = parentNode.CurrentStatus.Clone();
                newStatus.AddFromStatus(effectsStatus);

                GoapNode goapNode = new GoapNode(parentNode, goapAction, newStatus, parentNode.Cost + goapAction.Cost);

                if (goapStatus.IsAnyContainIn(newStatus))
                {
                    goapNodeList.Add(goapNode);
                    continue;
                }

                List <GoapAction> actionList = Remove(goapActionList, i);
                GetAction(goapNode, goapNodeList, actionList, goapStatus);
            }
        }
예제 #2
0
        public Queue <GoapAction> Plan(List <GoapAction> goapActionList, GoapStatus worldStatus, GoapStatus goalStatus)
        {
            Queue <GoapAction> goapActionQueue = new Queue <GoapAction>();

            List <GoapAction> validActionList = new List <GoapAction>();

            for (int i = 0; i < goapActionList.Count; ++i)
            {
                GoapAction goapAction = goapActionList[i];
                if (goapAction.CheckProceduralPrecondition())
                {
                    validActionList.Add(goapAction);
                }
            }

            List <GoapNode> goapNodeList = new List <GoapNode>();
            GoapNode        goapNode     = new GoapNode(null, null, worldStatus, 0);

            GetAction(goapNode, goapNodeList, validActionList, goalStatus);

            if (goapNodeList.Count <= 0)
            {
                return(goapActionQueue);
            }

            GoapNode heighestNode = GetHighestNode(goapNodeList);

            goapActionQueue = GetActionQueue(heighestNode);

            return(goapActionQueue);
        }
예제 #3
0
 /// <summary>
 /// 将 a 的值 加到自身
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public void AddFromStatus(GoapStatus a)
 {
     foreach (var pair in a.Status())
     {
         AddState(pair.Key, pair.Value);
     }
 }
예제 #4
0
        // Use this for initialization
        protected virtual void Start()
        {
            worldStats = new GoapStatus();
            goalStatus = new GoapStatus();

            SetActions();
        }
예제 #5
0
        public GoapStatus Clone()
        {
            GoapStatus goapStatus = new GoapStatus();

            foreach (var pair in statusDic)
            {
                goapStatus.AddState(pair.Key, pair.Value);
            }

            return(goapStatus);
        }
예제 #6
0
 /// <summary>
 /// 将 a 和 自己 key 相同且值也相同的元素移除
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public void RemoveFromStatus(GoapStatus a)
 {
     foreach (var pair in a.Status())
     {
         object value = null;
         if (statusDic.TryGetValue(pair.Key, out value))
         {
             if (Equals(pair.Value, value))
             {
                 RemoveState(pair.Key);
             }
         }
     }
 }
예제 #7
0
        /// <summary>
        /// 有任何一个值包含在 a 中
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public bool IsAnyContainIn(GoapStatus a)
        {
            Dictionary <GoapCondition, object> aStatusDic = a.Status();

            foreach (var pair in aStatusDic)
            {
                object aValue = null;
                if (aStatusDic.TryGetValue(pair.Key, out aValue))
                {
                    if (Equals(pair.Value, aValue))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
예제 #8
0
        /// <summary>
        /// 包含于 a
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public bool IsContainIn(GoapStatus a)
        {
            Dictionary <GoapCondition, object> aStatusDic = a.Status();

            bool isContentIn = true;

            foreach (var pair in statusDic)
            {
                object aValue = null;
                if (aStatusDic.TryGetValue(pair.Key, out aValue))
                {
                    if (Equals(pair.Value, aValue))
                    {
                        continue;
                    }
                }

                isContentIn = false;
                break;
            }

            return(isContentIn);
        }
예제 #9
0
 public virtual void InitStatus()
 {
     preconditionsStatus = new GoapStatus();
     effectsStatus       = new GoapStatus();
 }