Phoenix2D-Library  0.10
Player.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <sstream>
22 #include <cstdlib>
23 #include <cmath>
24 #include "Player.h"
25 #include "Self.h"
26 #include "Controller.h"
27 namespace Phoenix
28 {
30  distance = 100.0;
31  direction = 0.0;
32  distChange = 0.0;
33  dirChange = 0.0;
34  bodyDirection = 0.0;
35  x = 0.0;
36  y = 0.0;
37  vx = 0.0;
38  vy = 0.0;
39  theta = 0.0;
40  headDirection = 0.0;
41  pointDir = 0.0;
42  team = "undefined";
43  uniform_number = 0;
44  body_b = false;
45  head_b = false;
46  pointing = false;
47  kicking = false;
48  tackling = false;
49  goalie = false;
50  simulation_time = -1;
51  player_id = -1;
52  is_in_sight_range = false;
53  is_localized = false;
54  is_bounded = false;
55 }
56 
57 Player::Player(std::string name, std::string position, int simulation_time) {
58  distance = 100.0;
59  direction = 0.0;
60  distChange = 0.0;
61  dirChange = 0.0;
62  bodyDirection = 0.0;
63  x = 0.0;
64  y = 0.0;
65  vx = 0.0;
66  vy = 0.0;
67  theta = 0.0;
68  headDirection = 0.0;
69  pointDir = 0.0;
70  team = "undefined";
71  uniform_number = 0;
72  body_b = true;
73  head_b = true;
74  pointing = false;
75  kicking = false;
76  tackling = false;
77  goalie = false;
78  std::vector<std::string> tokens;
79  std::stringstream ss_name(name); // = std::stringstream(name);
80  std::string token;
81  while (std::getline(ss_name, token, ' ')) {
82  tokens.push_back(token);
83  }
84  std::string team_t = tokens[1].substr(1, tokens[1].length() - 2);
85  if (Controller::AGENT_TYPE == 't') {
86  team = team_t;
87  } else if (team_t.compare(Self::TEAM_NAME) == 0) {
88  team = "our";
89  } else {
90  team = "opp";
91  }
92  if (tokens.size() > 3) {
93  goalie = true;
94  }
95  uniform_number = atoi(tokens[2].c_str());
96  this->simulation_time = simulation_time;
97  tokens.clear();
98  std::stringstream ss_position(position); // = std::stringstream(position);
99  while (std::getline(ss_position, token, ' ')) {
100  if (token.compare("k") == 0) {
101  kicking = true;
102  } else if (token.compare("t") == 0) {
103  tackling = true;
104  } else {
105  tokens.push_back(token);
106  }
107  }
108  switch (tokens.size()) {
109  case 6:
110  x = atof(tokens[0].c_str());
111  y = atof(tokens[1].c_str());
112  vx = atof(tokens[2].c_str());
113  vy = atof(tokens[3].c_str());
114  theta = atof(tokens[4].c_str());
115  headDirection = atof(tokens[5].c_str());
116  break;
117  case 7:
118  x = atof(tokens[0].c_str());
119  y = atof(tokens[1].c_str());
120  vx = atof(tokens[2].c_str());
121  vy = atof(tokens[3].c_str());
122  theta = atof(tokens[4].c_str());
123  headDirection = atof(tokens[5].c_str());
124  pointDir = atof(tokens[6].c_str());
125  pointing = true;
126  break;
127  default:
128  break;
129  }
131  this->position = Position(x, y, theta, headDirection);
132  player_id = -1;
133  is_in_sight_range = true;
134  is_localized = false;
135  is_bounded = false;
136 }
137 
138 Player::Player(std::string name, std::string position, int simulation_time, Position player_position, Vector2D player_velocity) {
139  distance = 100.0;
140  direction = 0.0;
141  distChange = 0.0;
142  dirChange = 0.0;
143  bodyDirection = 0.0;
144  headDirection = 0.0;
145  pointDir = 0.0;
146  x = 0.0;
147  y = 0.0;
148  vx = 0.0;
149  vy = 0.0;
150  theta = 0.0;
151  team = "undefined";
152  uniform_number = 0;
153  body_b = false;
154  head_b = false;
155  pointing = false;
156  kicking = false;
157  tackling = false;
158  goalie = false;
159  bool vel = false;
160  std::vector<std::string> tokens;
161  std::stringstream ss_name(name); // = std::stringstream(name);
162  std::string token;
163  while (std::getline(ss_name, token, ' ')) {
164  tokens.push_back(token);
165  }
166  std::string team_t;
167  switch (tokens.size()) {
168  case 1:
169  break;
170  case 2:
171  team_t = tokens[1].substr(1, tokens[1].length() - 2);
172  if (team_t.compare(Self::TEAM_NAME) == 0) {
173  team = "our";
174  } else {
175  team = "opp";
176  }
177  break;
178  case 3:
179  team_t = tokens[1].substr(1, tokens[1].length() - 2);
180  if (team_t.compare(Self::TEAM_NAME) == 0) {
181  team = "our";
182  } else {
183  team = "opp";
184  }
185  uniform_number = atoi(tokens[2].c_str());
186  break;
187  case 4: //goalie
188  team_t = tokens[1].substr(1, tokens[1].length() - 2);
189  if (team_t.compare(Self::TEAM_NAME) == 0) {
190  team = "our";
191  } else {
192  team = "opp";
193  }
194  uniform_number = atoi(tokens[2].c_str());
195  goalie = true;
196  break;
197  default:
198  break;
199  }
200  tokens.clear();
201  std::stringstream ss_position(position); // = std::stringstream(position);
202  while (std::getline(ss_position, token, ' ')) {
203  if (token.compare("k") == 0) {
204  kicking = true;
205  } else if (token.compare("t") == 0) {
206  tackling = true;
207  } else {
208  tokens.push_back(token);
209  }
210  }
211  switch (tokens.size()) {
212  case 1:
213  direction = atof(tokens[0].c_str());
214  break;
215  case 2:
216  distance = atof(tokens[0].c_str());
217  direction = atof(tokens[1].c_str());
218  break;
219  case 3:
220  distance = atof(tokens[0].c_str());
221  direction = atof(tokens[1].c_str());
222  pointDir = atof(tokens[2].c_str());
223  pointing = true;
224  break;
225  case 4:
226  distance = atof(tokens[0].c_str());
227  direction = atof(tokens[1].c_str());
228  distChange = atof(tokens[2].c_str());
229  dirChange = atof(tokens[3].c_str());
230  vel = true;
231  break;
232  case 5:
233  distance = atof(tokens[0].c_str());
234  direction = atof(tokens[1].c_str());
235  distChange = atof(tokens[2].c_str());
236  dirChange = atof(tokens[3].c_str());
237  vel = true;
238  pointDir = atof(tokens[4].c_str());
239  pointing = true;
240  break;
241  case 6:
242  distance = atof(tokens[0].c_str());
243  direction = atof(tokens[1].c_str());
244  distChange = atof(tokens[2].c_str());
245  dirChange = atof(tokens[3].c_str());
246  vel = true;
247  bodyDirection = atof(tokens[4].c_str());
248  headDirection = atof(tokens[5].c_str());
249  body_b = true;
250  head_b = true;
251  break;
252  case 7:
253  distance = atof(tokens[0].c_str());
254  direction = atof(tokens[1].c_str());
255  distChange = atof(tokens[2].c_str());
256  dirChange = atof(tokens[3].c_str());
257  vel = true;
258  bodyDirection = atof(tokens[4].c_str());
259  headDirection = atof(tokens[5].c_str());
260  body_b = true;
261  head_b = true;
262  pointDir = atof(tokens[6].c_str());
263  pointing = true;
264  break;
265  default:
266  break;
267  }
268  theta = bodyDirection + player_position.getBodyDirection() + player_position.getHeadDirection();
269  if (theta > 180.0) {
270  theta -= 360.0;
271  } else if (theta <= -180.0) {
272  theta += 360.0;
273  }
274  double source_direction = player_position.getBodyDirection() + player_position.getHeadDirection() + direction;
275  if (source_direction > 180.0) {
276  source_direction -= 360.0;
277  } else if (source_direction <= 180.0) {
278  source_direction += 360.0;
279  }
280  double erx = cos(Self::PI * source_direction / 180.0);
281  double ery = sin(Self::PI * source_direction / 180.0);
282  x = player_position.getX() + erx * distance;
283  y = player_position.getY() + ery * distance;
284  double erxm = (180.0 * erx) / (Self::PI * distance);
285  double erym = (180.0 * ery) / (Self::PI * distance);
286  double vry = (distChange * erym + dirChange * erx) / (ery * erym + erx * erxm);
287  double vrx = (distChange - ery * vry) / erx;
288  vx = player_velocity.getXComponent() + vrx;
289  vy = player_velocity.getYComponent() + vry;
290  if (body_b && head_b) {
291  this->position = Position(x, y, theta, headDirection);
292  } else {
293  this->position = Position(x, y);
294  }
295  if (vel) {
297  } else {
299  }
300  this->simulation_time = simulation_time;
301  player_id = -1;
302  is_in_sight_range = true;
303  is_localized = false;
304  is_bounded = false;
305 }
306 
308 
309 }
310 
312  /*if (body_b && head_b) {
313  return Position(x, y, theta, headDirection);
314  } else {
315  return Position(x, y);
316  }*/
317  return position;
318 }
319 
320 std::string Player::getTeam() {
321  return team;
322 }
323 
325  return uniform_number;
326 }
327 
329  //return Vector2D::getVector2DWithXAndY(vx, vy);
330  return velocity;
331 }
332 
334  return goalie;
335 }
336 
338  return pointing;
339 }
340 
342  return pointDir;
343 }
344 
346  return kicking;
347 }
348 
350  return tackling;
351 }
352 
353 void Player::boundTo(Player* player) {
354  //bound = player;
355  //double b_distance = player->distance;
356  is_bounded = true;
357 }
358 
359 void Player::setPlayerId(int player_id) {
360  this->player_id = player_id;
361 }
362 
364  return player_id;
365 }
366 
369 }
370 
372  return is_in_sight_range;
373 }
374 
376  pretenders.push_back(player);
377 }
378 
380  return pretenders.size();
381 }
382 
384  return pretenders.front();
385 }
386 
388  return is_localized;
389 }
390 
392  return is_bounded;
393 }
394 
395 std::string Player::print() {
396  return "";
397 }
398 }
Position position
Definition: Player.h:67
double theta
Definition: Player.h:51
double distChange
Definition: Player.h:42
Player The Player lorem Ipsum
Definition: Player.h:39
double headDirection
Definition: Player.h:45
bool isKicking()
Definition: Player.cpp:345
bool is_in_sight_range
Definition: Player.h:63
bool isInSightRange()
Definition: Player.cpp:371
Position The Position lorem Ipsum
Definition: Position.h:50
bool head_b
Definition: Player.h:53
void boundTo(Player *player)
Definition: Player.cpp:353
static double PI
Definition: Self.h:47
double y
Definition: Player.h:48
double x
Definition: Player.h:47
bool is_localized
Definition: Player.h:64
bool isTackling()
Definition: Player.cpp:349
Vector2D velocity
Definition: Player.h:68
double bodyDirection
Definition: Player.h:44
void pretendToBound(Player *player)
Definition: Player.cpp:375
double direction
Definition: Player.h:41
int getUniformNumber()
Definition: Player.cpp:324
double dirChange
Definition: Player.h:43
static Vector2D getVector2DWithXAndY(double dx, double dy)
Definition: Vector2D.cpp:59
double getYComponent()
Definition: Vector2D.cpp:67
bool goalie
Definition: Player.h:60
std::string getTeam()
Definition: Player.cpp:320
std::string team
Definition: Player.h:57
double getBodyDirection()
Definition: Position.cpp:73
bool localized()
Definition: Player.cpp:387
double getPointingDirection()
Definition: Player.cpp:341
double pointDir
Definition: Player.h:46
bool tackling
Definition: Player.h:56
double vx
Definition: Player.h:49
double distance
Definition: Player.h:40
static std::string TEAM_NAME
Definition: Self.h:48
bool pointing
Definition: Player.h:54
Player * getPretenderFront()
Definition: Player.cpp:383
static Vector2D getEmptyVector()
Definition: Vector2D.cpp:49
int simulation_time
Definition: Player.h:59
void toggleSightRange()
Definition: Player.cpp:367
bool isGoalie()
Definition: Player.cpp:333
int getPretendersCount()
Definition: Player.cpp:379
void setPlayerId(int player_id)
Definition: Player.cpp:359
std::vector< Player * > pretenders
Definition: Player.h:66
int getPlayerId()
Definition: Player.cpp:363
bool is_bounded
Definition: Player.h:65
bool kicking
Definition: Player.h:55
double getHeadDirection()
Definition: Position.cpp:77
bool body_b
Definition: Player.h:52
double getXComponent()
Definition: Vector2D.cpp:63
Position getPosition()
Definition: Player.cpp:311
bool bounded()
Definition: Player.cpp:391
std::string print()
Definition: Player.cpp:395
double vy
Definition: Player.h:50
int uniform_number
Definition: Player.h:58
int player_id
Definition: Player.h:62
static char AGENT_TYPE
p = Player, t = Trainer
Definition: Controller.h:78
Vector2D getVelocity()
Definition: Player.cpp:328
bool isPointing()
Definition: Player.cpp:337
Vector2D The Vector2D lorem Ipsum
Definition: Vector2D.h:45