Phoenix2D-Library  0.10
Commands.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 <iostream>
22 #include <sstream>
23 #include <iomanip>
24 #include <list>
25 #include "Commands.h"
26 #include "Command.h"
27 #include "Connect.h"
28 #include "Self.h"
29 #include "Server.h"
30 #include "Config.h"
31 #include "Game.h"
32 namespace Phoenix
33 {
35 std::list<Command> commands_history;
36 std::list<Command*> commands_to_send;
37 
39  commands_connect_ptr = connect;
40 }
41 
43  if (Config::VERBOSE) std::cout << "Commands out" << std::endl;
44 }
45 
47  while (commands_history.size() > 0 && Game::SIMULATION_TIME - commands_history.front().createdAt() > Config::COMMANDS_MAX_HISTORY) {
48  commands_history.pop_front();
49  }
50 }
51 
52 void Commands::move(double x, double y) {
53  std::stringstream ss;
54  ss << "(move " << std::setprecision(4) << x << " " << y << ")" << std::endl;
55  std::string command;
56  std::getline(ss, command);
57  commands_history.push_back(Command(command, 1, MOVE));
58  commands_history.back().setArgs((void *)&x, (void *)&y);
59  commands_to_send.push_back(&commands_history.back());
60 }
61 
62 void Commands::turn(double moment) {
63  if (moment < Server::MINMOMENT) {
64  moment = Server::MINMOMENT;
65  } else if (moment > Server::MAXMOMENT) {
66  moment = Server::MAXMOMENT;
67  }
68  std::stringstream ss;
69  ss << "(turn " << std::setprecision(4) << moment << ")" << std::endl;
70  std::string command;
71  std::getline(ss, command);
72  commands_history.push_back(Command(command, 1, TURN));
73  commands_history.back().setArgs((void *)&moment);
74  commands_to_send.push_back(&commands_history.back());
75 }
76 
77 void Commands::turnNeck(double moment) {
78  if (moment < Server::MINMOMENT) {
79  moment = Server::MINMOMENT;
80  } else if (moment > Server::MAXMOMENT) {
81  moment = Server::MAXMOMENT;
82  }
83  std::stringstream ss;
84  ss << "(turn_neck " << std::setprecision(4) << moment << ")" << std::endl;
85  std::string command;
86  std::getline(ss, command);
87  commands_history.push_back(Command(command, 1, TURN_NECK));
88  commands_history.back().setArgs((void *)&moment);
89  commands_to_send.push_back(&commands_history.back());
90 }
91 
92 void Commands::dash(double power, double direction) {
93  if (power > Server::MAXPOWER) {
94  power = Server::MAXPOWER;
95  } else if (power < Server::MINPOWER) {
96  power = Server::MINPOWER;
97  }
98  if (direction > Server::MAXMOMENT) {
99  direction = Server::MAXMOMENT;
100  } else if (direction < Server::MINMOMENT) {
101  direction = Server::MINMOMENT;
102  }
103  std::stringstream ss;
104  ss << "(dash " << std::setprecision(4) << power << " " << direction << ")" << std::endl;
105  std::string command;
106  std::getline(ss, command);
107  commands_history.push_back(Command(command, 1, DASH));
108  commands_history.back().setArgs((void *)&power, (void *)&direction);
109  commands_to_send.push_back(&commands_history.back());
110 }
111 
112 void Commands::say(std::string message) {
113  std::string command = "(say " + message + ")";
114  commands_history.push_back(Command(command, 1, SAY));
115  commands_history.back().setArgs((void *)&message);
116  commands_to_send.push_back(&commands_history.back());
117 }
118 
119 void Commands::catchBall(double direction) {
120  std::stringstream ss;
121  ss << "(catch " << std::setprecision(4) << direction << ")" << std::endl;
122  std::string command;
123  std::getline(ss, command);
124  commands_history.push_back(Command(command, 1, CATCH));
125  commands_history.back().setArgs((void *)&direction);
126  commands_to_send.push_back(&commands_history.back());
127 }
128 
129 void Commands::kick(double power, double direction) {
130  std::stringstream ss;
131  ss << "(kick " << std::setprecision(4) << power << " " << direction << ")" << std::endl;
132  std::string command;
133  std::getline(ss, command);
134  commands_history.push_back(Command(command, 1, KICK));
135  commands_history.back().setArgs((void *)&power, (void *)&direction);
136  commands_to_send.push_back(&commands_history.back());
137 }
138 
139 void Commands::tackle(double power, bool willToFoul) {
140  std::stringstream ss;
141  ss << "(tackle " << std::setprecision(4) << power << (willToFoul ? " true" : " false") << std::endl;
142  std::string command;
143  std::getline(ss, command);
144  commands_history.push_back(Command(command, 1, TACKLE));
145  commands_history.back().setArgs((void *)&power, (void *)&willToFoul);
146  commands_to_send.push_back(&commands_history.back());
147 }
148 
149 void Commands::pointTo(double distance, double direction) {
150  std::stringstream ss;
151  ss << "(pointto " << std::setprecision(4) << distance << " " << direction << ")" << std::endl;
152  std::string command;
153  std::getline(ss, command);
154  commands_history.push_back(Command(command, 1, POINT));
155  commands_history.back().setArgs((void *)&distance, (void *)&direction);
156  commands_to_send.push_back(&commands_history.back());
157 }
158 
159 void Commands::changeView(std::string width) {
160  std::string command = "(change_view " + width + ")";
161  commands_history.push_back(Command(command, 1, CHANGE_VIEW));
162  commands_history.back().setArgs((void *)&width);
163  commands_to_send.push_back(&commands_history.back());
164 }
165 
166 void Commands::moveObject(std::string object, double x, double y) {
167  std::stringstream ss;
168  ss << "(move " << object << " " << std::setprecision(4) << x << " " << y << ")" << std::endl;
169  std::string command;
170  std::getline(ss, command);
171  commands_history.push_back(Command(command, 1, MOVE_OBJECT));
172  commands_to_send.push_back(&commands_history.back());
173 }
174 
175 void Commands::changeMode(std::string mode) {
176  std::string command = "(change_mode " + mode + ")";
177  commands_history.push_back(Command(command, 1, CHANGE_MODE));
178  commands_history.back().setArgs((void *)&mode);
179  commands_to_send.push_back(&commands_history.back());
180 }
181 
183  std::string command = "(start)";
184  commands_history.push_back(Command(command, 1, START));
185  commands_to_send.push_back(&commands_history.back());
186 }
187 
189  std::string command = "(recover)";
190  commands_history.push_back(Command(command, 1, RECOVER));
191  commands_to_send.push_back(&commands_history.back());
192 }
193 
195  int commands_sent_counter = 0;
196  std::list<Command*> commands_sent;
197  if (commands_to_send.size() > 0) {
198  std::string message = "";
199  int weight = 0;
200  do {
201  Command* command_to_send = commands_to_send.front();
202  weight += command_to_send->getWeight();
203  if (weight < 2) {
204  command_to_send->changeStatusTo(SENT);
205  message += command_to_send->getCommand();
206  commands_sent.push_back(command_to_send);
207  commands_to_send.pop_front();
208  commands_sent_counter++;
209  }
210  } while (weight < 2 && commands_to_send.size() > 0);
212  }
213  commands_to_send.clear(); //we clear the commands to send queue
214  Self::setLastCommandsSet(commands_sent);
215  return commands_sent_counter;
216 }
217 }
void pointTo(double distance, double direction)
Definition: Commands.cpp:149
Used by Player and Goalie.
Definition: Command.h:56
void dash(double power, double direction)
Definition: Commands.cpp:92
std::list< Command > commands_history
Definition: Commands.cpp:35
Connect * commands_connect_ptr
Definition: Commands.cpp:34
void changeView(std::string width)
Definition: Commands.cpp:159
Used by Trainer.
Definition: Command.h:61
Used by Player and Goalie.
Definition: Command.h:53
Used by Player and Goalie.
Definition: Command.h:50
void changeMode(std::string mode)
Definition: Commands.cpp:175
Used by Player and Goalie.
Definition: Command.h:52
Commands(Connect *connect)
Definition: Commands.cpp:38
static double MINMOMENT
Definition: Server.h:137
Used by Player and Goalie.
Definition: Command.h:57
void move(double x, double y)
Definition: Commands.cpp:52
std::list< Command * > commands_to_send
Definition: Commands.cpp:36
Used by Trainer.
Definition: Command.h:60
bool sendMessage(std::string msg)
Definition: Connect.cpp:86
Used by Player and Goalie.
Definition: Command.h:49
void turn(double moment)
Definition: Commands.cpp:62
void moveObject(std::string object, double x, double y)
Definition: Commands.cpp:166
void catchBall(double direction)
Definition: Commands.cpp:119
Used by Trainer.
Definition: Command.h:62
static double x
Definition: Self.cpp:89
Used by Player and Goalie.
Definition: Command.h:51
void kick(double power, double direction)
Definition: Commands.cpp:129
Command The Command lorem Ipsum
Definition: Command.h:70
void tackle(double power, bool willToFoul)
Definition: Commands.cpp:139
static unsigned int COMMANDS_MAX_HISTORY
Definition: Config.h:42
static bool VERBOSE
Definition: Config.h:38
void changeStatusTo(COMMAND_STATUS status)
Definition: Command.cpp:157
static double y
Definition: Self.cpp:90
Used by Player and Goalie.
Definition: Command.h:55
Used by Player and Goalie.
Definition: Command.h:58
Used by Player and Goalie.
Definition: Command.h:54
void say(std::string message)
Definition: Commands.cpp:112
std::string getCommand()
Definition: Command.cpp:85
static unsigned int SIMULATION_TIME
Definition: Game.h:38
Used by Trainer.
Definition: Command.h:59
void turnNeck(double moment)
Definition: Commands.cpp:77
static void setLastCommandsSet(std::list< Command * > last_commands_sent)
Definition: Self.cpp:722
static double MAXPOWER
Definition: Server.h:134
static double MINPOWER
Definition: Server.h:140
static double MAXMOMENT
Definition: Server.h:131