Phoenix2D-Library  0.10
World.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 <cmath>
22 #include <iostream>
23 #include "World.h"
24 #include "Game.h"
25 #include "Config.h"
26 #include "Self.h"
27 #include "Controller.h"
28 namespace Phoenix
29 {
30 std::list<Player> players;
32 
34 
35 }
36 
38  if (Config::VERBOSE) std::cout << "World out" << std::endl;
39 }
40 
42  double vision_angle = 180.0 + 10.0;
43  if (Self::VIEW_MODE_WIDTH.compare("narrow") == 0) {
44  vision_angle = 60.0 + 10.0;
45  } else if (Self::VIEW_MODE_WIDTH.compare("normal") == 0) {
46  vision_angle = 120.0 + 10.0;
47  }
48  for (std::list<Player>::iterator it = players.begin(); it != players.end(); ++it) {
49  if (std::abs(Self::getPosition().getDirectionTo(it->getPosition())) > vision_angle / 2.0) {
50  it->toggleSightRange();
51  } else if (!(it->isInSightRange())) {
52  it->toggleSightRange();
53  }
54  }
55 }
56 
57 void World::updateWorld(std::list<Player> new_players, Ball new_ball) {
59  double vision_angle = 180.0 + 10.0;
60  if (Self::VIEW_MODE_WIDTH.compare("narrow") == 0) {
61  vision_angle = 60.0 + 10.0;
62  } else if (Self::VIEW_MODE_WIDTH.compare("normal") == 0) {
63  vision_angle = 120.0 + 10.0;
64  }
65  for (std::list<Player>::iterator it = players.begin(); it != players.end(); ++it) {
66  if (std::abs(Self::getPosition().getDirectionTo(it->getPosition())) > vision_angle / 2.0) { //out the sight range
67  new_players.push_back(*it);
68  } else { //in the sight range
69  double min_distance = 120.0;
70  Player* nearest_player = 0;
71  for (std::list<Player>::iterator it_new = new_players.begin(); it_new != new_players.end(); ++it_new) {
72  double distance = it->getPosition().getDistanceTo(it_new->getPosition());
73  if (distance < min_distance) {
74  nearest_player = &(*it);
75  min_distance = distance;
76  }
77  }
78  if (nearest_player) {
79  nearest_player->pretendToBound(&(*it));
80  }
81  }
82  }
83  for (std::list<Player>::iterator it = players.begin(); it != players.end(); ++it) {
84  switch (it->getPretendersCount()) {
85  case 0:
86  //Nobody wants me :'(
87  break;
88  default: // > 0
89  Player* pretender = it->getPretenderFront();
90  pretender->boundTo(&(*it));
91  break;
92  }
93  }
94  }
95  players.swap(new_players);
96  ball = new_ball;
97 }
98 
99 void World::updateObserverWorld(std::list<Player> new_players, Ball new_ball) {
100  players = new_players;
101  ball = new_ball;
102 }
103 
105  return WorldModel(players, ball);
106 }
107 }
Player The Player lorem Ipsum
Definition: Player.h:39
Ball The Ball lorem Ipsum
Definition: Ball.h:38
void boundTo(Player *player)
Definition: Player.cpp:353
Ball ball
Definition: World.cpp:31
static Position getPosition()
Definition: Self.cpp:714
void pretendToBound(Player *player)
Definition: Player.cpp:375
WorldModel The WorldModel lorem Ipsum
Definition: WorldModel.h:38
void updateObserverWorld(std::list< Player > players, Ball ball)
Definition: World.cpp:99
Player * getPretenderFront()
Definition: Player.cpp:383
static bool VERBOSE
Definition: Config.h:38
WorldModel getWorldModel()
Definition: World.cpp:104
static std::string VIEW_MODE_WIDTH
Definition: Self.h:95
void updateWorld()
Definition: World.cpp:41
Position getPosition()
Definition: Player.cpp:311
static bool PLAYER_HISTORY
Definition: Config.h:45
std::list< Player > players
Definition: World.cpp:30
double getDistanceTo(Position position)
Definition: Position.cpp:81