forked from LARG/utaustinvilla3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplesoccer.cc
217 lines (190 loc) · 6.78 KB
/
simplesoccer.cc
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
#include "simplesoccer.h"
SimpleSoccerBehavior::SimpleSoccerBehavior( const std::string teamName,
int uNum,
const map<string, string>& namedParams_,
const string& rsg_)
: NaoBehavior( teamName,
uNum,
namedParams_,
rsg_) {
}
void SimpleSoccerBehavior::beam( double& beamX, double& beamY, double& beamAngle ) {
VecPosition space = getSpaceForRole(static_cast<Role>(worldModel->getUNum()-1));
beamX = space.getX();
beamY = space.getY();
beamAngle = 0;
}
SkillType SimpleSoccerBehavior::selectSkill() {
std::map<int, Role> agentRoles = getAgentRoles();
/*
// Draw role assignments
worldModel->getRVSender()->clear(); // erases drawings from previous cycle
VecPosition space = getSpaceForRole(agentRoles[worldModel->getUNum()]);
worldModel->getRVSender()->drawLine("me_to_space", me.getX(), me.getY(), space.getX(), space.getY());
worldModel->getRVSender()->drawPoint("role_space", space.getX(), space.getY(), 10.0f);
*/
if (agentRoles[worldModel->getUNum()] == ON_BALL) {
if (me.getDistanceTo(ball) > 1) {
// Walk to ball
return goToTarget(ball);
} else {
// Kick ball toward opponent's goal
return kickBall(KICK_FORWARD, VecPosition(HALF_FIELD_X, 0, 0));
}
}
return goToSpace(getSpaceForRole(agentRoles[worldModel->getUNum()]));
}
int SimpleSoccerBehavior::getPlayerClosestToBall() {
// Find closest player to ball
int playerClosestToBall = -1;
double closestDistanceToBall = 10000;
for(int i = WO_TEAMMATE1; i < WO_TEAMMATE1+NUM_AGENTS; ++i) {
VecPosition temp;
int playerNum = i - WO_TEAMMATE1 + 1;
if (worldModel->getUNum() == playerNum) {
// This is us
temp = worldModel->getMyPosition();
} else {
WorldObject* teammate = worldModel->getWorldObject( i );
if (teammate->validPosition) {
temp = teammate->pos;
} else {
continue;
}
}
temp.setZ(0);
double distanceToBall = temp.getDistanceTo(ball);
if (distanceToBall < closestDistanceToBall) {
playerClosestToBall = playerNum;
closestDistanceToBall = distanceToBall;
}
}
return playerClosestToBall;
}
map<int, Role> SimpleSoccerBehavior::getAgentRoles() {
map<int, Role> agentRoles;
set<Role> assignedRoles;
agentRoles[getPlayerClosestToBall()] = ON_BALL;
assignedRoles.insert(ON_BALL);
if (!agentRoles.count(1)) {
// Assign goalie the goalie role if goalie is not already assigned a role
agentRoles[1] = GOALIE;
assignedRoles.insert(GOALIE);
}
// Simple greedy role assignment of remaining unassigned roles
typedef std::list<std::pair<double, std::pair<int, Role>>> dist_to_roles_list;
dist_to_roles_list agentsDistancesToRoles;
for (int r = 0; r < NUM_ROLES; r++) {
Role role = static_cast<Role>(r);
if (assignedRoles.count(role)) {
continue;
}
for (int i = 1; i <= NUM_AGENTS; i++) {
if (agentRoles.count(i)) {
continue;
}
agentsDistancesToRoles.push_back(make_pair(getAgentDistanceToRole(i, role), make_pair(i, role)));
}
}
agentsDistancesToRoles.sort();
for (dist_to_roles_list::iterator it = agentsDistancesToRoles.begin(); it != agentsDistancesToRoles.end(); ++it) {
pair<int, Role> assignment = it->second;
int agent = assignment.first;
Role role = assignment.second;
if (agentRoles.count(agent) || assignedRoles.count(role)) {
continue;
}
agentRoles[agent] = role;
assignedRoles.insert(role);
if (agentRoles.size() == NUM_AGENTS) {
break;
}
}
return agentRoles;
}
VecPosition SimpleSoccerBehavior::getSpaceForRole(Role role) {
VecPosition ball = worldModel->getBall();
if (beamablePlayMode()) {
ball = VecPosition(0, 0, 0);
}
ball.setZ(0);
// Keep ball position on the field
ball.setX(max(min(ball.getX(), HALF_FIELD_X), -HALF_FIELD_X));
ball.setY(max(min(ball.getY(), HALF_FIELD_Y), -HALF_FIELD_Y));
VecPosition space = VecPosition(0,0,0);
switch(role) {
case ON_BALL:
space = ball;
break;
case GOALIE:
space = VecPosition(-HALF_FIELD_X+0.5, 0, 0);
break;
case SUPPORTER:
space = ball + VecPosition(-2,0,0);
break;
case BACK_LEFT:
space = ball + VecPosition(-10,3,0);
break;
case BACK_RIGHT:
space = ball + VecPosition(-10,-3,0);
break;
case MID_LEFT:
space = ball + VecPosition(-1,2,0);
break;
case MID_RIGHT:
space = ball + VecPosition(-1,-2,0);
break;
case WING_LEFT:
space = ball + VecPosition(0,7,0);
break;
case WING_RIGHT:
space = ball + VecPosition(0,-7,0);
break;
case FORWARD_LEFT:
space = ball + VecPosition(5,3,0);
break;
case FORWARD_RIGHT:
space = ball + VecPosition(5,-3,0);
break;
default:
cerr << "Unknown role: " << role << "\n";
}
space.setX(max(-HALF_FIELD_X, min(HALF_FIELD_X, space.getX())));
space.setY(max(-HALF_FIELD_Y, min(HALF_FIELD_Y, space.getY())));
if (beamablePlayMode()) {
// Stay within your own half on kickoffs
space.setX(min(-0.5, space.getX()));
}
return space;
}
double SimpleSoccerBehavior::getAgentDistanceToRole(int uNum, Role role) {
VecPosition temp;
if (worldModel->getUNum() == uNum) {
// This is us
temp = worldModel->getMyPosition();
} else {
WorldObject* teammate = worldModel->getWorldObject( WO_TEAMMATE1 + uNum - 1 );
temp = teammate->pos;
}
temp.setZ(0);
double distanceToRole = temp.getDistanceTo(getSpaceForRole(role));
return distanceToRole;
}
SkillType SimpleSoccerBehavior::goToSpace(VecPosition space) {
const double SPACE_THRESH = 0.5;
if (me.getDistanceTo(space) < SPACE_THRESH ) {
return watchPosition(ball);
}
// Adjust target to not be too close to teammates or the ball
VecPosition target = collisionAvoidance(true /*teammate*/, false/*opponent*/, true/*ball*/, 1/*proximity thresh*/, .5/*collision thresh*/, space, true/*keepDistance*/);
return goToTarget(target);
}
SkillType SimpleSoccerBehavior::watchPosition(VecPosition pos) {
const double POSITION_CENTER_THRESH = 10;
VecPosition localPos = worldModel->g2l(pos);
SIM::AngDeg localPosAngle = atan2Deg(localPos.getY(), localPos.getX());
if (abs(localPosAngle) > POSITION_CENTER_THRESH) {
return goToTargetRelative(VecPosition(), localPosAngle);
}
return SKILL_STAND;
}