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 },
145 if (_funcH ==
nullptr)
147 _funcH = std::bind(&
AStar::AStarGetH,
this, std::placeholders::_1, std::placeholders::_2);
166 std::list<AStarPoint>& list_path,
167 std::list<AStarPoint>* fail_path =
nullptr)
170 log_msg(
"{} -> {}", source, target);
176 AStarNode* node_start = _get_node(source);
177 node_start->
_open =
true;
179 node_start->
_cost = 0;
180 node_start->
_total = _funcH(source, target);
182 _queueOpen.Push(node_start);
186 while (!_queueOpen.IsEmpty())
189 _nodeBest = _queueOpen.Pop();
191 log_info(
"当前最佳节点为:{}", _nodeBest->_xy);
193 if (_nodeBest->_xy == target
194 || (target_near && _is_link_node(_nodeBest->_xy, target)))
200 list_path.push_front(_nodeBest->_xy);
201 _nodeBest = _nodeBest->_parent;
207 if (_bankCur + 32 >= _maxNode)
215 fail_path->push_front(_nodeBest->_xy);
216 _nodeBest = _nodeBest->_parent;
223 _funcLink(
this, _nodeBest->_xy);
225 _link_node(_nodeBest->_xy);
228 _nodeBest->_closed =
true;
236 fail_path->push_front(_nodeBest->_xy);
237 _nodeBest = _nodeBest->_parent;
251 if (_nodeBest->_parent ==
nullptr ||
252 _nodeBest->_parent->_xy != xy)
255 AStarCost g = _funcG(_nodeBest->_xy, xy);
263 if (actual_node->
_closed ==
false)
265 if (actual_node->
_open)
268 AStarCost new_cost = _nodeBest->_cost + g * k_cost;
270 AStarCost new_total = new_cost + _funcH(xy, _xyTarget);
273 if (new_total < actual_node->_total)
278 actual_node->
_parent = _nodeBest;
279 actual_node->
_cost = new_cost;
280 actual_node->
_total = new_total;
281 _queueOpen.UpdateNode(actual_node);
286 actual_node->
_parent = _nodeBest;
287 actual_node->
_cost = _nodeBest->_cost + g * k_cost;
288 actual_node->
_total = actual_node->
_cost + _funcH(xy, _xyTarget);
290 _queueOpen.Push(actual_node);
291 actual_node->
_open =
true;
305 assert(_funcLink ==
nullptr);
306 _bDiagonal = diagonal;
314 _funcLink = func_link;
320 delete[] _arrNodeBank;
326 auto& iter_y = _masterNode[xy.y];
327 AStarNode*& node = iter_y[xy.x];
336 if (_bankCur >= _maxNode)
342 node = &_arrNodeBank[_bankCur++];
345 node->_closed =
false;
359 AddNode({ p.x + 1, p.y }, ASTAR_K0);
360 AddNode({ p.x - 1, p.y }, ASTAR_K0);
361 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
362 AddNode({ p.x, p.y - 1 }, ASTAR_K0);
365 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
366 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
367 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
368 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
373 if (p.x + 1 < _size.w)
374 AddNode({ p.x + 1, p.y }, ASTAR_K0);
376 AddNode({ p.x - 1, p.y }, ASTAR_K0);
377 if (p.y + 1 < _size.h)
378 AddNode({ p.x, p.y + 1 }, ASTAR_K0);
380 AddNode({ p.x, p.y - 1}, ASTAR_K0);
384 if (p.x + 1 < _size.w && p.y + 1 < _size.h)
385 AddNode({ p.x + 1, p.y + 1 }, ASTAR_K1);
386 if (p.x + 1 < _size.w && p.y > 0)
387 AddNode({ p.x + 1, p.y - 1 }, ASTAR_K1);
388 if (p.x > 0 && p.y > 0)
389 AddNode({ p.x - 1, p.y - 1 }, ASTAR_K1);
390 if (p.x > 0 && p.y + 1 < _size.h)
391 AddNode({ p.x - 1, p.y + 1 }, ASTAR_K1);
403 return p.x <= 1 && p.y <= 1;
406 return p.x + p.y <= 1;
410 std::map<unsigned, std::map<unsigned, AStarNode*>> _masterNode;
bool Search(const AStarPoint &source, const AStarPoint &target, bool target_near, std::list< AStarPoint > &list_path, std::list< AStarPoint > *fail_path=nullptr)
搜索