赞
踩
# 机器人控制函数
void control_robots() {
std::vector<std::pair<int, int>> robot_next; // 最新机器人坐标
for (int i = 0; i < 10; ++i) {
robot_next.push_back({robot[i].x, robot[i].y});
}
for (int robot_id : robot_list) {
//改成,先判断机器人是否存在异常
if(robot[robot_id].status == 0)
{
if (robot[robot_id].goods == 1)//机器人携带货物异常
{
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1;
robot[robot_id].berth = -1;
robot[robot_id].ops.clear();
search_berth(robot_id, best_robot_map[robot_id].second);
}
else //机器人没有携带货物发生异常,则可以搜索货物
{
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1;
robot[robot_id].ops.clear();
search_goods( robot_id, 100, 1);
}
}
//如果机器人有目标的话,就执行下面的操作。有目标但是不一定有货物。可能是泊位目标,也可能是货物目标
if (robot[robot_id].target[0] != -1) {
//有货物但是没有泊位,就搜匹配的泊位路径
if (robot[robot_id].goods == 1 && robot[robot_id].berth == -1) {
search_berth(robot_id, best_robot_map[robot_id].second); //search_berth()函数前面是机器人id,后面是泊位id
}
//前面不执行的话说明就是机器人是有目标,身上没货,其实就是有货物目标但是没有拿到,那么传的就是机器人目标
auto target_i = robot[robot_id].target; //不管如何,都要将机器人的目标传出去,前面如果执行目标位置就是泊位,如果不执行,目标位置就是原来的货物
if (!robot[robot_id].ops.empty()) {
auto DOA = robot[robot_id].ops.front();
auto temp_next = direction(robot[robot_id].x, robot[robot_id].y, DOA);
if (std::find(robot_next.begin(), robot_next.end(), temp_next) == robot_next.end()) {//如果当前机器人的下一个位置,在10个机器人机器人坐标里面都没有找到,则说明没有发生碰撞
if (robot[robot_id].status == 1) //如果机器人status正常,处于正常状态,那么就移动机器人
{
robot_next.push_back(temp_next);
robot[robot_id].ops.erase(robot[robot_id].ops.begin()); // 移除已执行的操作
printf("move %d %d\n", robot_id, DOA);
}
}
}
//机器人到达指定位置
if (robot[robot_id].x==target_i[0] && robot[robot_id].y == target_i[1] && robot[robot_id].goods == 0) {
printf("get %d\n", robot_id);
gds[target_i[0]][target_i[1]].reset();
}
//机器人到达了指定码头
if (robot[robot_id].goods == 1 && robot[robot_id].x==berth[best_robot_map[robot_id].second].x && robot[robot_id].y == berth[best_robot_map[robot_id].second].y) {
printf("pull %d\n", robot_id);
berth[best_robot_map[robot_id].second].goods += 1;
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1; // 重置目标
robot[robot_id].berth = -1;
search_goods( robot_id, 100, 1); //一放下货物就开始搜
}
}
else
//如果机器人没有目标的话,就搜索货物,因为没有目标的情况只有一种,就是在码头放完货的时候
search_goods( robot_id, 100, 1);
}
}
#轮船控制函数
void control_robots() {
std::vector<std::pair<int, int>> robot_next; // 最新机器人坐标
for (int i = 0; i < 10; ++i) {
robot_next.push_back({robot[i].x, robot[i].y});
}
for (int robot_id : robot_list) {
//改成,先判断机器人是否存在异常
if(robot[robot_id].status == 0)
{
if (robot[robot_id].goods == 1)//机器人携带货物异常
{
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1;
robot[robot_id].berth = -1;
robot[robot_id].ops.clear();
search_berth(robot_id, best_robot_map[robot_id].second);
}
else //机器人没有携带货物发生异常,则可以搜索货物
{
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1;
robot[robot_id].ops.clear();
search_goods( robot_id, 100, 1);
}
}
//如果机器人有目标的话,就执行下面的操作。有目标但是不一定有货物。可能是泊位目标,也可能是货物目标
if (robot[robot_id].target[0] != -1) {
//有货物但是没有泊位,就搜匹配的泊位路径
if (robot[robot_id].goods == 1 && robot[robot_id].berth == -1) {
search_berth(robot_id, best_robot_map[robot_id].second); //search_berth()函数前面是机器人id,后面是泊位id
}
//前面不执行的话说明就是机器人是有目标,身上没货,其实就是有货物目标但是没有拿到,那么传的就是机器人目标
auto target_i = robot[robot_id].target; //不管如何,都要将机器人的目标传出去,前面如果执行目标位置就是泊位,如果不执行,目标位置就是原来的货物
if (!robot[robot_id].ops.empty()) {
auto DOA = robot[robot_id].ops.front();
auto temp_next = direction(robot[robot_id].x, robot[robot_id].y, DOA);
if (std::find(robot_next.begin(), robot_next.end(), temp_next) == robot_next.end()) {//如果当前机器人的下一个位置,在10个机器人机器人坐标里面都没有找到,则说明没有发生碰撞
if (robot[robot_id].status == 1) //如果机器人status正常,处于正常状态,那么就移动机器人
{
robot_next.push_back(temp_next);
robot[robot_id].ops.erase(robot[robot_id].ops.begin()); // 移除已执行的操作
printf("move %d %d\n", robot_id, DOA);
}
}
}
//机器人到达指定位置
if (robot[robot_id].x==target_i[0] && robot[robot_id].y == target_i[1] && robot[robot_id].goods == 0) {
printf("get %d\n", robot_id);
gds[target_i[0]][target_i[1]].reset();
}
//机器人到达了指定码头
if (robot[robot_id].goods == 1 && robot[robot_id].x==berth[best_robot_map[robot_id].second].x && robot[robot_id].y == berth[best_robot_map[robot_id].second].y) {
printf("pull %d\n", robot_id);
berth[best_robot_map[robot_id].second].goods += 1;
robot[robot_id].target[0] = -1;
robot[robot_id].target[1] = -1; // 重置目标
robot[robot_id].berth = -1;
search_goods( robot_id, 100, 1); //一放下货物就开始搜
}
}
else
//如果机器人没有目标的话,就搜索货物,因为没有目标的情况只有一种,就是在码头放完货的时候
search_goods( robot_id, 100, 1);
}
}
写在最后:由于代码较长,版幅有限所以就把资源放到个人哪里,明年再战。
2024年3月25日于深圳大学
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。