Beispiel #1
0
        /* 递归可行,但效率太低
         * public void ReachableTilesInRange(T t, int range, ref List<T> ret)
         * {
         *  ret.Clear();
         *  if (range <= 0)
         *  {
         *      return;
         *  }
         *  HashSet<T> _exists = new HashSet<T>();
         *  _exists.Add(t);
         *  DSF(t, 1, range, ref ret, ref _exists);
         * }
         *
         * void DSF(T t, int depth, int maxDepth, ref List<T> ret, ref HashSet<T> exists)
         * {
         *  List<T> neighbors = new List<T>();
         *  Neighbors(t, ref neighbors, true);
         *  foreach (var item in neighbors)
         *  {
         *      if (depth < maxDepth)
         *      {
         *          // 递归调用
         *          DSF(item, depth+1, maxDepth, ref ret, ref exists);
         *      }
         *
         *      if (exists.Contains(item))
         *      {
         *          continue;
         *      }
         *
         *      ret.Add(item);
         *      exists.Add(item);
         *  }
         * }*/

        public bool FindPath(T src, T dst, ref List <T> path)
        {
            AStarResultCode ret = _astar.FindPath(src, dst, ref path, 0);

            Debug.Log(string.Format("寻路结果:{0}", ret));
            return(ret == AStarResultCode.SUCCESS);
        }
Beispiel #2
0
        // 可以A*搜索生成plan,也可以手动配置plan
        void BuildPlan(GOAPGoal goal)
        {
            int             maxOverLoad = 100;
            AStarResultCode ret         = _astar.FindPath(goal, CurWorldState, ref _path, maxOverLoad);

            switch (ret)
            {
            case AStarResultCode.SUCCESS:
                foreach (var action in _path)
                {
                    steps.Enqueue(action);
                }
                break;

            case AStarResultCode.FAIL_NO_WAY:
                Debug.Log(string.Format("寻路失败!无法获得goalType = {0}的路径。", goal.GetType()));
                break;

            case AStarResultCode.FAIL_OVERLOAD:
                Debug.Log(string.Format("寻路失败!在寻找goalType = {0}的路径时open列表元素个数超过上限{1}。",
                                        goal.GetType(), maxOverLoad));
                break;

            default:
                break;
            }
        }
Beispiel #3
0
        public AStarResultCode FindPath(GOAPGoal goal, WorldState curWorldState,
                                        ref List <GOAPAction> path, int maxOverload = 0)
        {
            path.Clear();
            _goal = goal;
            _nodes.Shuffle();
            curWorldState.CopyTo(ref _start.nodeWS);

            AStarResultCode ret = FindPath(_start, null, ref _nodePath, maxOverload);

            if (AStarResultCode.SUCCESS == ret)
            {
                foreach (var node in _nodePath)
                {
                    path.Add(node.goapAction);
                }
            }

            return(ret);
        }