{ // 如果是负载的状态 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { // 如果到达终点 agvs[i].setLoad(false); // 设置为空载状态 agvs[i].setState(true); std :: cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl; int completed_task_index = -1; for (int j = 0; j < tasks.size(); j++) { if (tasks[j].id == agvs[i].get_task_id()) { completed_task_index = j; break; } } if (completed_task_index != -1) { tasks[completed_task_index].completed = 2; } task_to_agv(); // 更新任务分配 update(); // 更新AGV状态 } else { // 否则行驶到终点 Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path_to_end = astar.getPath(start_node, end_node1); path_to_end.erase(path_to_end.begin()); std::vector<Node*> path; path.insert(path.end(), path_to_end.begin(), path_to_end.end()); paths[i] = path; } },添加條件,且任務的completed不爲2
时间: 2023-12-11 16:02:30 浏览: 132
你的意思是在 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) 的条件下,添加一个判断任务的 completed 是否等于 2 的条件吗?如果不等于 2,才执行后续的代码块,是吗?如果是这样,可以这样修改:
```
if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) {
// 获取已完成的任务索引
int completed_task_index = -1;
for (int j = 0; j < tasks.size(); j++) {
if (tasks[j].id == agvs[i].get_task_id()) {
completed_task_index = j;
break;
}
}
// 如果任务未完成,则更新 completed 状态
if (completed_task_index != -1 && tasks[completed_task_index].completed != 2) {
tasks[completed_task_index].completed = 2;
}
// 执行后续操作
if (agvs[i].getLoad() && completed_task_index != -1 && tasks[completed_task_index].completed == 2) {
agvs[i].setLoad(false);
agvs[i].setState(true);
std::cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl;
task_to_agv();
update();
} else {
Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY());
Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY());
std::vector<Node*> path_to_end = astar.getPath(start_node, end_node1);
path_to_end.erase(path_to_end.begin());
std::vector<Node*> path;
path.insert(path.end(), path_to_end.begin(), path_to_end.end());
paths[i] = path;
}
}
```
这样修改后,如果任务未完成,则不会执行后续的代码块,直到任务状态变为 completed == 2 才会执行。
阅读全文