-
Notifications
You must be signed in to change notification settings - Fork 2
/
robot.cpp
344 lines (329 loc) · 10.6 KB
/
robot.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "robot.h"
#include <QDebug>
Robot::Robot() :
RobotBase(),
m_hasGuardianLine(false),
m_guardianLine(QPointF(0,0), QPointF(0,0)),
m_toP1Distance(0.0),
m_movingSpeed(1.0)
{
setPos(QPointF(0, 0));
}
Robot::Robot(QPointF pos) :
Robot()
{
setPos(pos);
}
Robot::Robot(qreal x, qreal y) :
Robot()
{
setPos(x, y);
}
void Robot::addToDetectList(Robot *robot)
{
//Check the robot whether has already be in the list.
if(!m_detectedRobotList.contains(robot))
{
m_detectedRobotList.append(robot);
}
}
void Robot::removeFromDetectList(Robot *robot)
{
//Remove all the robot from the list.
m_detectedRobotList.removeOne(robot);
}
void Robot::moveOneStep()
{
//Get the next step and set the position.
setPos(nextStep());
//If this robot has a guardian line, change the toP1Distance.
if(m_hasGuardianLine)
{
m_toP1Distance+=m_movingSpeed;
}
}
QPointF Robot::nextStep() const
{
//If the robot has the guardian line, calculate the next step on the line.
if(m_hasGuardianLine)
{
//Get the next position.
qreal robotPosition= (m_toP1Distance + m_movingSpeed) /
m_guardianLine.length();
return m_guardianLine.pointAt(robotPosition);
}
//If not have a guardian line, then move to the direction.
//Generate the direction line.
QLineF directionLine(m_pos, m_pos+QPointF(m_detectRadius, 0));
directionLine.setAngle(m_angle);
directionLine.setLength(1.0);
//The p2 is the next step position.
return directionLine.p2();
}
void Robot::updateDirection()
{
//----Magic! Don't touch!---
//If the detected list is empty, then keep the direction.
if(m_detectedRobotList.isEmpty())
{
//If the robot has a guardian line,
if(m_hasGuardianLine)
{
//Check if the robot reach one side of the line.
if(m_toP1Distance<=0.9)
{
//Move to the guardian line angle.
m_movingSpeed=1.0;
m_angle=m_guardianLine.angle();
return;
}
if(m_toP1Distance>=m_guardianLine.length())
{
//Move to the opposite angle of the guardian line.
m_movingSpeed=-1.0;
m_angle=m_oppositeGuardianLine.angle();
return;
}
}
//Or else keep the direction.
return;
}
//Now the detected robot list cannot be empty.
//If the robot has a line to guard.
if(m_hasGuardianLine)
{
//Check if the robot reach one side of the line.
if(m_toP1Distance<=0.9 || m_toP1Distance>=m_guardianLine.length()-0.9)
{
//Move to the different direction.
if(m_movingSpeed>0)
{
m_movingSpeed=-1.0;
m_angle=m_oppositeGuardianLine.angle();
}
else
{
m_movingSpeed=1.0;
m_angle=m_guardianLine.angle();
}
return;
}
//Or else, we should have move the robot to opposite direction of the
//nearest robot.
//The nearest robot have three kind of types:
// 1. It doesn't have a guardian line.
// 2. It has a guardian line, but it's not the same as mine.
// 3. It has the same guardian line.
//For the first type, ignore it.
//For the second and third type, there's one rule: the robot should
//move to the direction which should leave that robot away.
QList<RobotStatus> statusList;
for(Robot *robot : m_detectedRobotList)
{
RobotStatus currentStatus;
currentStatus.robot=robot;
currentStatus.distance=QLineF(m_pos, robot->pos()).angle();
statusList.append(currentStatus);
}
qSort(statusList);
//Get the nearest robot, .
RobotStatus nearestStatus=statusList.takeFirst();
while(!nearestStatus.robot->hasGuardianLine() && !statusList.isEmpty())
{
nearestStatus=statusList.takeFirst();
}
//Check the nearest status.
if(!nearestStatus.robot->hasGuardianLine())
{
//All the robot in the detect range don't has a guardian line.
//They will move away from this point.
return;
}
//So now, we get the nearest point which contains a guardian line.
Robot *nearestRobot=nearestStatus.robot;
//If these two robot has the same guardian line, and they are getting
//closer(have the different speed), move to the other direction.
//If these two robots have the different speed.
//For this kinds of type,
//p1 this
//| |
//+---*><*----------------------
// |
// nearest
//
//Or for this kinds of type,
//
// this p2
// | |
//-----------------*><*----+
// |
// nearest
//
//We have to change the direction.
//
//So now, there is an ugly thing we have to met.(What the fuck!)
//The nearest point is not at the same line, but according to the
//context, this line must be the neighbouring line. Like the following:
//
// --------+
// |
// |
//
//We have change the direction when both of these robots are moving to
//the same point, and that point is pretty interesting. It's the p1 for
//the second line and the p2 for the first line. So:
//
// this p2(for this)
// | |
// -----*>-+-p1(for nearest)
// |
// ^
// *
// |
//
//At this time, the moving speed of this and nearest will be different
//(this is 1.0 and nearest is -1.0).
//For another case, it will be like this:
//
// p1(for this) p1
// | |
// p2(for nearest)-+-<*-------
// |
// ^
// *
// |
//
//At this time, the moving speed of this and nearest will be different
//as well(this is -1.0 and nearest is 1.0).
//We have to change the direction in these two cases.
if(nearestRobot->movingSpeed()!=m_movingSpeed)
{
if(nearestRobot->guardianLine()==m_guardianLine)
{
if((m_movingSpeed>0 &&
m_toP1Distance<nearestRobot->toP1Distance()) ||
(m_movingSpeed<0 &&
m_toP1Distance>nearestRobot->toP1Distance()))
{
//Move to the different direction.
moveToOppositeDirection();
//Ask the robot move to the differect direction as well.
nearestRobot->moveToOppositeDirection();
}
}
else
{
if((m_movingSpeed>0 &&
m_toP1Distance>nearestRobot->toP1Distance()) ||
(m_movingSpeed<0 &&
m_toP1Distance<nearestRobot->toP1Distance()))
{
//Move to the different direction.
moveToOppositeDirection();
//Ask the robot move to the differect direction as well.
nearestRobot->moveToOppositeDirection();
}
}
}
//Or else, keep moving.
return;
}
//The prefer direction is to link all the detected robots, calculate the
//average angle of the robot lists.
qreal angleSum=0.0;
for(Robot *robot : m_detectedRobotList)
{
angleSum+=QLineF(m_pos, robot->pos()).angle();
}
angleSum/=m_detectedRobotList.size();
//Set the angle to the opposite angle.
setAngle(angleSum+180.0);
}
bool Robot::hasGuardianLine() const
{
return m_hasGuardianLine;
}
void Robot::setGuardianLine(const QLineF &line,
const QPointF &footPoint)
{
//Clear the previous data.
resetGuardianLine();
//Check the length of the line.
if(line.length()==0.0)
{
return;
}
//Set has guardian line flag.
m_hasGuardianLine=true;
//Save the guardian line.
m_guardianLine=line;
//Get the opposite guardian line.
m_oppositeGuardianLine=QLineF(m_guardianLine.p2(), m_guardianLine.p1());
QLineF directionAngle=QLineF(QPointF(0.0, 0.0), QPointF(10.0, 0));
directionAngle.setAngle(m_angle);
//If the current angle is nearly to the angle, then the moving speed will be
//1.0(follow the direction of the line), or -1.0(reverse direction of the
//line)
if(m_oppositeGuardianLine.angleTo(directionAngle) <
m_guardianLine.angleTo(directionAngle))
{
m_movingSpeed=1.0;
m_angle=m_guardianLine.angle();
}
else
{
m_movingSpeed=-1.0;
m_angle=m_oppositeGuardianLine.angle();
}
//Move the robot to the foot point.
setPos(footPoint);
//Save the initial distance to p1.
m_toP1Distance=pointDistance(m_guardianLine.p1(), footPoint);
}
void Robot::resetGuardianLine()
{
m_hasGuardianLine=false;
m_guardianLine=QLineF();
m_oppositeGuardianLine=QLineF();
m_toP1Distance=0.0;
m_movingSpeed=1.0;
}
void Robot::resetDetectList()
{
m_detectedRobotList.clear();
}
void Robot::moveToOppositeDirection()
{
if(!m_hasGuardianLine)
{
return;
}
//If the robot is moving along the line, change the direction back.
if(m_movingSpeed>0)
{
m_movingSpeed=-1.0;
m_angle=m_oppositeGuardianLine.angle();
}
else
{
m_movingSpeed=1.0;
m_angle=m_guardianLine.angle();
}
}