/// <summary> /// 对比测试用的非启发算法,不要使用 /// </summary> /// <param name="startX"></param> /// <param name="startY"></param> /// <param name="endX"></param> /// <param name="endY"></param> /// <param name="eightDirection"></param> /// <param name="useBinaryHeap"></param> /// <returns></returns> public List <MapNode> findPathByXYNoInspire(int startX, int startY, int endX, int endY, bool eightDirection = false) { //Debug.Log(startX + " " + startY + " " + endX + " " + endY); List <MapNode> result = new List <MapNode>(); if (startX == endX && startY == endY) { result.Add(_mapdata[startY, startX]); return(result); } MapNode start = _mapdata[startY, startX]; MapNode end = _mapdata[endY, endX]; _open = new ListOpenList <MapNode>(); _open.push(start); start.parentNode = null; start.g = start.h = start.f = 0; end.parentNode = null; _closed = new List <MapNode>(); while (_open.getCount() > 0 && (_closed.IndexOf(end) == -1)) { //不去是用启发算法去找一个可能最优的,而是随便找一个来计算 MapNode mn = (_open as ListOpenList <MapNode>).pop(); //检查周围 if (!eightDirection) { for (int i = 0; i < 4; i++) { int _x = mn.x + _xd[i]; int _y = mn.y + _yd[i]; if (_x < 0 || _y < 0 || (_x >= _width) || (_y >= _height)) { continue; } MapNode testN = _mapdata[_y, _x]; if (_closed.IndexOf(testN) != -1) { continue; } if (!testN.isPassAble()) { _closed.Add(testN); testN.parentNode = mn; continue; } if (_open.indexOf(testN) != -1) { if (testN.g > mn.g + testN.weight) { testN.g = mn.g + testN.weight; testN.parentNode = mn; } continue; } else { _open.push(testN); testN.parentNode = mn; testN.g = mn.g + testN.weight; } } } else { for (int i = 0; i < 8; i++) { int _x = mn.x + _xde[i]; int _y = mn.y + _yde[i]; if (_x < 0 || _y < 0 || (_x >= _width) || (_y >= _height)) { continue; } MapNode testN = _mapdata[_y, _x]; if (_closed.IndexOf(testN) != -1) { continue; } if (!testN.isPassAble()) { _closed.Add(testN); testN.parentNode = mn; continue; } if (_open.indexOf(testN) != -1) { if (testN.g > mn.g + testN.weight) { testN.g = mn.g + testN.weight; testN.parentNode = mn; } continue; } else { _open.push(testN); testN.parentNode = mn; testN.g = mn.g + testN.weight; } } } //加入closed _closed.Add(mn); } MapNode c = end; while (c != null) { result.Insert(0, c); c = c.parentNode; } if (result.Count == 1) { result.Clear(); result.Add(start); } if (result.Count < 2) { //找不到路 } return(result); }
private void checkAround(MapNode node, MapNode goalNode, bool eightDirection) { if (eightDirection) { for (int e = 7; e >= 0; e--) { int _xe = node.x + _xde[e]; int _ye = node.y + _yde[e]; if ((_xe < 0) || (_ye < 0) || (_xe >= _width) || (_ye >= _height)) { continue; } MapNode testNodee = _mapdata[_ye, _xe]; if (_closed.IndexOf(testNodee) != -1) { //已经在关闭列表 continue; } if (!testNodee.isPassAble()) { //如果不可通过 _closed.Add(testNodee); testNodee.parentNode = node; continue; } if (_open.indexOf(testNodee) != -1) { //已经在开放列表,判断f的值是不是更好 if ((node.g + Mathf.Sqrt(_xde[e] * _xde[e] + _yde[e] * _yde[e])) < testNodee.g) { testNodee.g = node.g + /* testNodee.weight;*/ Mathf.Sqrt(_xde[e] * _xde[e] + _yde[e] * _yde[e]) * testNodee.weight; testNodee.parentNode = node; testNodee.f = testNodee.g + testNodee.h; ////此处是否应该在二项堆里重新排序. _open.rePostion(testNodee); } continue; } else { testNodee.g = node.g + /*testNodee.weight;*/ Mathf.Sqrt(_xde[e] * _xde[e] + _yde[e] * _yde[e]) * testNodee.weight; testNodee.h = Mathf.Abs(goalNode.x - testNodee.x) + Mathf.Abs(goalNode.y - testNodee.y); testNodee.f = testNodee.g + testNodee.h; testNodee.parentNode = node; _open.push(testNodee); continue; } } } else { for (int i = 3; i >= 0; i--) { int _x = node.x + _xd[i]; int _y = node.y + _yd[i]; if ((_x < 0) || (_y < 0) || (_x >= _width) || (_y >= _height)) { continue; } MapNode testNode = _mapdata[_y, _x]; if (_closed.IndexOf(testNode) != -1) { //已在关闭列表,不处理 continue; } if (!testNode.isPassAble()) { //不可通过,加入关闭列表 _closed.Add(testNode); testNode.parentNode = node; continue; } if (_open.indexOf(testNode) != -1) { //已经在开放列表,判断新的g值是不是更好 if ((node.g + testNode.weight) < testNode.g) { testNode.g = node.g + testNode.weight; testNode.f = testNode.g + testNode.h; ////此处是否应该在二项堆里重新排序. _open.rePostion(testNode); testNode.parentNode = node; } continue; } else { //加入开放列表 testNode.g = node.g + testNode.weight; testNode.h = Mathf.Abs(goalNode.x - testNode.x) + Mathf.Abs(goalNode.y - testNode.y); testNode.f = testNode.g + testNode.h; testNode.parentNode = node; _open.push(testNode); continue; } } } }