-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.cpp
More file actions
202 lines (181 loc) · 6.07 KB
/
Copy pathRobot.cpp
File metadata and controls
202 lines (181 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "Robot.h"
using namespace std;
Robot::Robot() {}
Robot::Robot(int startX, int startY)
{
this->x = startX, this->y = startY;
}
bool Robot::is_available(int Blocks[WIDTH][WIDTH], Point dest)
{
Point start(this->x, this->y);
Search search;
std::vector<Point> path = search.Astar(Blocks, start, dest);
if (path.size() == 0)
return false;
else
return true;
}
int Robot::generate_path(int Blocks[WIDTH][WIDTH], Point dest, Robot *RobotList)
{
Point start(this->x, this->y);
Search search;
std::vector<Point> path = search.Astar_robot_without_collision(Blocks, start, dest, RobotList, this->id);
if (path.size() == 0)
{
info("robot generate path error!");
return -1;
}
this->path = path;
this->path_index = 0;
return 0;
}
int Robot::move_to_cargo(int Blocks[WIDTH][WIDTH], std::set<Cargo *> &CargoSet, Robot *RobotList)
{
// 没有路径,尝试选择货物 alloc分配时会计算路径,保证可达
if (this->target_cargo == nullptr || isValid(this->target_cargo->x, this->target_cargo->y, Blocks) == false)
{
Allocator allocator;
this->target_cargo = allocator.alloc_robot_cargo(this, CargoSet, Blocks, RobotList);
}
if (this->target_cargo != nullptr && this->path.size() == 0)
{
}
// 如果成功选上 or 已有货物
if (this->target_cargo != nullptr)
{
if (this->path.size() <= 1 || this->path_index == this->path.size() - 1)
return -1;
Point cur = this->path[path_index + 1], last = this->path[path_index];
if (cur.x == last.x && cur.y == last.y + 1)
return 0;
else if (cur.x == last.x && cur.y == last.y - 1)
return 1;
else if (cur.x == last.x - 1 && cur.y == last.y)
return 2;
else if (cur.x == last.x + 1 && cur.y == last.y)
return 3;
else
{
info("generated path incontinuous!");
return -1;
}
}
else
{
// 如果没有找到可达的货物,则返回一个特殊值表示未找到
info("no target cargo\n");
return -1;
}
}
int Robot::move_to_berth(int Blocks[WIDTH][WIDTH], std::vector<Berth *> &BerthList, Robot *RobotList)
{
// 没有目标,则分配目标泊位,同时寻路
if (this->target_berth == nullptr)
{
Allocator allocator;
std::pair<Berth *, Point> t = allocator.alloc_robot_berth(this, BerthList, Blocks, RobotList);
this->target_berth = t.first;
this->berth_pos = Point(t.second.x, t.second.y);
}
if (this->target_berth != nullptr)
{
// 分配有目标, allocator保证必然是可达的,也就必然有路线
if (this->path.size() <= 1 || this->path_index == this->path.size() - 1)
return -1;
// 有路线,按路线走
Point cur = this->path[this->path_index + 1], last = this->path[this->path_index];
if (cur.x == last.x && cur.y == last.y + 1)
return 0;
else if (cur.x == last.x && cur.y == last.y - 1)
return 1;
else if (cur.x == last.x - 1 && cur.y == last.y)
return 2;
else if (cur.x == last.x + 1 && cur.y == last.y)
return 3;
else
{
info("generated path incontinuous!");
return -1;
}
}
else
{ // 没有分配到目标,下次继续分配
info("no target berth\n");
return -1;
}
}
void Robot::act(int Blocks[WIDTH][WIDTH], std::set<Cargo *> &CargoSet, std::vector<Berth *> &BerthList, Robot *RobotList)
{
// 只操作正在运行的机器人
if (this->is_running && this->dead == false)
{
// 移动前动作
if (this->path.size() && this->path_index == this->path.size() - 1)
{
this->path.clear();
this->path_index = -1;
if (this->is_carring_cargo == false)
{
std::cout << "get " << this->id << std::endl;
this->is_carring_cargo = true;
}
else
{
this->target_berth->take_in_cargo(this->target_cargo->val);
this->target_cargo = nullptr;
this->target_berth = nullptr;
this->berth_pos = Point(-1, -1);
std::cout << "pull " << this->id << std::endl;
this->is_carring_cargo = false;
}
}
// 移动动作
if (this->is_carring_cargo == false)
{
int dir = this->move_to_cargo(Blocks, CargoSet, RobotList);
if (dir != -1)
cout << "move " << this->id << " " << dir << endl;
}
else
{
int dir = this->move_to_berth(Blocks, BerthList, RobotList);
if (dir != -1)
cout << "move " << this->id << " " << dir << endl;
}
// 移动后动作
if (this->path.size() && this->path_index == this->path.size() - 1)
{
this->path.clear();
this->path_index = -1;
if (this->is_carring_cargo == false)
{
std::cout << "get " << this->id << std::endl;
}
else
{
this->target_berth->take_in_cargo(this->target_cargo->val);
this->target_cargo = nullptr;
this->target_berth = nullptr;
this->berth_pos = Point(-1, -1);
std::cout << "pull " << this->id << std::endl;
}
}
}
/*
// 将没在运行的机器人的路径置空,方面后续重新生成路径,从碰撞中恢复
else {
if(this->path_index != -1) {
if(this->is_carring_cargo == false) {
this->target_cargo = nullptr;
this->path.clear();
this->path_index = -1;
}
else {
this->target_berth = nullptr;
this->path.clear();
this->path_index = -1;
}
}
}
*/
}