5 int nodeCount = neighborsIDList.size();
8 for(
int i = 0; i < nodeCount; i++) {
11 nodeList.push_back(node);
12 nodePtrs.push_back(&nodeList[i]);
16 for(
int i = 0; i < nodeCount; i++) {
17 for(
int nodeID : neighborsIDList[i]) {
18 if(nodeID == EMPTY_ID)
break;
19 neighborPtrs[i].push_back(nodePtrs[nodeID]);
24 for(
int i = 0; i < nodeCount; i++) {
25 nodePtrs[i]->setNeighbors(&neighborPtrs[i]);
26 nodePtrs[i]->setPosition(positionList[i].x, positionList[i].y);
30 blockAreaNodeList = &nodePtrs;
35 for(
auto itr = blockAreaNodeList->begin(); itr <= blockAreaNodeList->end(); itr++) {
36 (*itr)->setBeClosed(
false);
37 (*itr)->setHasBlock(
false);
39 (*itr)->setRealCost(0);
40 (*itr)->setParentNode(
nullptr);
46 auto itr = blockAreaNodeList->begin() + blockID;
47 (*itr)->setHasBlock(
true);
52 auto itr = blockAreaNodeList->begin() + id;
53 return (*itr)->hasBlock();
58 auto startItr = blockAreaNodeList->begin() + start;
59 auto endItr = blockAreaNodeList->begin() + end;
61 int estimatedCostX = std::abs((*endItr)->getPositionX() - (*startItr)->getPositionX());
62 int estimatedCostY = std::abs((*endItr)->getPositionY() - (*startItr)->getPositionY());
63 int score = estimatedCostX + estimatedCostY;
64 if((*startItr)->hasBlock()) score += 99;
66 (*startItr)->setScore(score);
67 (*startItr)->setRealCost(0);
68 (*startItr)->setParentNode(
nullptr);
70 std::vector<Node*> around;
73 std::vector<int> route = { endNode->
getNodeID() };
75 while(parent !=
nullptr) {
80 std::reverse(route.begin(), route.end());
86 std::int32_t realCost, std::int8_t end)
88 auto currentItr = blockAreaNodeList->begin() + parent->
getNodeID();
89 auto endItr = blockAreaNodeList->begin() + end;
93 for(
auto itr = (*currentItr)->getNeighbors()->begin();
94 itr != (*currentItr)->getNeighbors()->end(); itr++) {
95 if((*itr)->isClosed() || (*itr) == parent->
getParentNode())
continue;
97 int estimatedCostX = std::abs((*endItr)->getPositionX() - (*itr)->getPositionX());
98 int estimatedCostY = std::abs((*endItr)->getPositionY() - (*itr)->getPositionY());
99 int score = realCost + estimatedCostX + estimatedCostY;
100 if((*itr)->hasBlock() && (*itr)->getNodeID() != end) score += 99;
102 (*itr)->setScore(score);
103 (*itr)->setRealCost(realCost);
104 (*itr)->setParentNode(parent);
105 around->push_back((*itr));
108 std::int32_t min = 999;
109 std::int32_t minCost = 999;
110 Node* minNode =
nullptr;
112 for(
auto itr = around->begin(); itr != around->end(); itr++) {
113 if((*itr)->isClosed() || (*itr) == parent->
getParentNode())
continue;
116 if(score > min || (score == min && (*itr)->getRealCost() >= minCost))
continue;
119 minCost = (*itr)->getRealCost();
124 std::int8_t cost = 0;
125 Node* node = minNode;
138 return blockAreaNodeList;
void setNodeID(std::int8_t nodeNumber)
std::vector< Node * > * getBlockAreaNodeList()
bool hasBlock(std::int8_t id)
Node * calculateNeighborCost(Node *parent, std::vector< Node * > *around, std::int32_t realCost, std::int8_t end)
void setHasBlockIn(std::int8_t blockID)
void setBeClosed(bool closed_)
std::vector< int > searchRoute(std::int8_t start, std::int8_t end)