73 _heap.push_back(node);
78 for (
auto iter = _heap.begin(); iter != _heap.end(); ++iter)
80 if ((*iter)->_xy == node->
_xy)
96 std::vector<AStarNode*> _heap;
102 a.x < b.x ? (b.x - a.x) : (a.x - b.x),
103 a.y < b.y ? (b.y - a.y) : (a.y - b.y)
118 return ASTAR_K0 * (d.x + d.y) - (std::min)(d.x, d.y) * (2 * ASTAR_K0 - ASTAR_K1);
135 size_t max_node = 10000) :
138 _funcLink{ func_link },
139 _maxNode{ max_node },
144 _masterNode.reserve(_maxNode);
146 if (_funcH ==
nullptr)
148 _funcH = std::bind(&
AStar::AStarGetH,
this, std::placeholders::_1, std::placeholders::_2);
167 std::list<AStarPoint>& list_path,
168 std::list<AStarPoint>* fail_path =
nullptr)
171 log_msg(
"{} -> {}", source, target);
177 AStarNode* node_start = _get_node(source);
178 node_start->
_open =
true;
180 node_start->
_cost = 0;
181 node_start->
_total = _funcH(source, target);
183 _queueOpen.Push(node_start);
187 while (!_queueOpen.IsEmpty())
190 _nodeBest = _queueOpen.Pop();
192 log_info(
"当前最佳节点为:{}", _nodeBest->_xy);
194 if (_nodeBest->_xy == target
195 || (target_near && _is_link_node(_nodeBest->_xy, target)))
201 list_path.push_front(_nodeBest->_xy);
202 _nodeBest = _nodeBest->_parent;
208 if (_bankCur + 32 >= _maxNode)
216 fail_path->push_front(_nodeBest->_xy);
217 _nodeBest = _nodeBest->_parent;
224 _funcLink(
this, _nodeBest->_xy);
226 _link_node(_nodeBest->_xy);
229 _nodeBest->_closed =
true;
237 fail_path->push_front(_nodeBest->_xy);
238 _nodeBest = _nodeBest->_parent;
252 if (_nodeBest->_parent ==
nullptr ||
253 _nodeBest->_parent->_xy != xy)
256 AStarCost g = _funcG(_nodeBest->_xy, xy);
264 if (actual_node->
_closed ==
false)
266 if (actual_node->
_open)
269 AStarCost new_cost = _nodeBest->_cost + g * k_cost;
271 AStarCost new_total = new_cost + _funcH(xy, _xyTarget);
274 if (new_total < actual_node->_total)
279 actual_node->
_parent = _nodeBest;
280 actual_node->
_cost = new_cost;
281 actual_node->
_total = new_total;
282 _queueOpen.UpdateNode(actual_node);
287 actual_node->
_parent = _nodeBest;
288 actual_node->
_cost = _nodeBest->_cost + g * k_cost;
289 actual_node->
_total = actual_node->
_cost + _funcH(xy, _xyTarget);
291 _queueOpen.Push(actual_node);
292 actual_node->
_open =
true;
306 assert(_funcLink ==
nullptr);
307 _bDiagonal = diagonal;
315 _funcLink = func_link;
321 delete[] _arrNodeBank;
327 uint64_t index = ((uint64_t)xy.y << 32) | (uint64_t)xy.x;
329 AStarNode*& node = _masterNode[index];
338 if (_bankCur >= _maxNode)
344 node = &_arrNodeBank[_bankCur++];
347 node->_closed =
false;
361 AddNode({ p.x + 1, p.y }, ASTAR_K0);
362 AddNode({ p.x - 1, p.y }, ASTAR_K0);
363 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
364 AddNode({ p.x, p.y - 1 }, ASTAR_K0);
367 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
368 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
369 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
370 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
375 if (p.x + 1 < _size.w)
376 AddNode({ p.x + 1, p.y }, ASTAR_K0);
378 AddNode({ p.x - 1, p.y }, ASTAR_K0);
379 if (p.y + 1 < _size.h)
380 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
382 AddNode({ p.x, p.y - 1}, ASTAR_K0);
386 if (p.x + 1 < _size.w && p.y + 1 < _size.h)
387 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
388 if (p.x + 1 < _size.w && p.y > 0)
389 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
390 if (p.x > 0 && p.y > 0)
391 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
392 if (p.x > 0 && p.y + 1 < _size.h)
393 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
405 return p.x <= 1 && p.y <= 1;
408 return p.x + p.y <= 1;
412 std::unordered_map<uint64_t, AStarNode*>_masterNode;
bool Search(const AStarPoint &source, const AStarPoint &target, bool target_near, std::list< AStarPoint > &list_path, std::list< AStarPoint > *fail_path=nullptr)
搜索