Phoenix2D-Library  0.10
Trainer.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 <vector>
22 #include <stack>
23 #include <string>
24 #include <fstream>
25 #include <iostream>
26 #include <cstdlib>
27 #include "Trainer.h"
28 #include "Commands.h"
29 #include "Config.h"
30 #include "Game.h"
31 namespace Phoenix
32 {
33 std::vector<std::string> code;
34 
35 enum COMMANDS {
36  START,
37  DO,
39  RECOVER,
41  SAY,
43 };
44 
46  int address;
48  int accum;
49  std::string arg;
50  int jump_to;
51 };
52 
53 std::vector<execution_line> program;
54 
55 bool loadCode() {
56  std::ifstream file("trainer.phx", std::ifstream::in);
57  if (file) {
58  std::string line;
59  while (std::getline(file, line)) {
60  code.push_back(line);
61  }
62  file.close();
63  return true;
64  } else {
65  std::cerr << "loadCode() -> file trainer.phx does not exist" << std::endl;
66  file.close();
67  return false;
68  }
69 }
70 
71 bool generateCode() {
72  int address = 0;
73  std::stack<int> jumps;
74  for (std::vector<std::string>::iterator it = code.begin(); it != code.end(); ++it) {
75  size_t found = it->find_first_of(" ");
76  if (found != std::string::npos) {
77  std::string type = it->substr(0, found);
78  std::string args = it->substr(found + 1);
79  if (type.compare("do") == 0) {
80  execution_line line = {address, DO, atoi(args.c_str()), "", address + 1};
81  jumps.push(address + 1);
82  program.push_back(line);
83  address++;
84  } else if (type.compare("wait") == 0) {
85  int wait = atoi(args.c_str());
86  if (wait > 0) {
87  execution_line first_line = {address, DO, wait - 1, "", address + 1};
88  program.push_back(first_line);
89  address++;
90  execution_line second_line = {address, END_DO, 1, "", address};
91  program.push_back(second_line);
92  address++;
93  }
94  } else if (type.compare("change_to") == 0) {
95  execution_line line = {address, CHANGE_TO, 1, args, address + 1};
96  program.push_back(line);
97  address++;
98  } else if (type.compare("say") == 0) {
99  execution_line line = {address, SAY, 1, args, address + 1};
100  program.push_back(line);
101  address++;
102  }
103  } else {
104  if ((*it).compare("recover") == 0) {
105  execution_line line = {address, RECOVER, 1, "", address + 1};
106  program.push_back(line);
107  address++;
108  } else if ((*it).compare("end_do") == 0) {
109  execution_line line = {address, END_DO, 1, "", jumps.top()};
110  jumps.pop();
111  program.push_back(line);
112  address++;
113  }
114  }
115  }
116  if (jumps.size() > 0) {
117  std::cerr << "generateCode() -> " << jumps.size() << " loops unclosed" << std::endl;
118  return false;
119  }
120  execution_line line = {address, END, 1, "time_over", address + 1};
121  program.push_back(line);
122  return true;
123 }
124 
125 int current_line = 0;
126 std::stack<int> accums;
127 
129  this->commands = commands;
130  if (loadCode()) {
131  newExecution = true;
132  if (!generateCode()) {
133  newExecution = false;
134  }
135  } else {
136  newExecution = false;
137  }
138 }
139 
141 
142 }
143 
145  if (Config::TRAINER_LOGGING && Game::PLAY_MODE.compare("play_on") == 0) {
146  std::list<Player*> players = world.getPlayers();
147  std::clog << Game::GAME_TIME;
148  for (std::list<Player*>::iterator it = players.begin(); it != players.end(); ++it) {
149  std::clog << " - " << (*it)->getTeam() << "(" << (*it)->getUniformNumber() << "): [" << ((*it)->getPosition()).toString() << "," << ((*it)->getVelocity()).toString() << "]";
150  }
151  std::clog << std::endl;
152  std::clog.flush();
153  }
154  execution_line line = program.at(current_line);
155  switch (line.command) {
156  case START:
157  break;
158  case DO:
159  accums.push(line.accum);
160  current_line = line.jump_to;
161  break;
162  case CHANGE_TO:
163  commands->changeMode(line.arg);
164  Game::PLAY_MODE = line.arg;
165  current_line = line.jump_to;
166  break;
167  case RECOVER:
168  commands->recover();
169  current_line = line.jump_to;
170  break;
171  case END_DO:
172  accums.top()--;
173  if (accums.top() > 0) {
174  current_line = line.jump_to;
175  } else {
176  accums.pop();
177  current_line++;
178  }
179  break;
180  case SAY:
181  commands->say(line.arg);
182  current_line = line.jump_to;
183  break;
184  case END:
185  commands->changeMode(line.arg);
186  newExecution = false;
187  break;
188  }
190 }
191 
193  return newExecution;
194 }
195 }
bool newExecution
Definition: Trainer.h:41
std::list< Player * > getPlayers()
Definition: WorldModel.cpp:42
Used by Trainer.
Definition: Command.h:61
void changeMode(std::string mode)
Definition: Commands.cpp:175
Used by Player and Goalie.
Definition: Command.h:52
void execute(WorldModel world)
Definition: Trainer.cpp:144
Commands The Commans lorem Ipsum
Definition: Commands.h:42
WorldModel The WorldModel lorem Ipsum
Definition: WorldModel.h:38
static std::string PLAY_MODE
Definition: Game.h:42
Commands * commands
Definition: Trainer.h:40
bool continueExecution()
Definition: Trainer.cpp:192
Used by Trainer.
Definition: Command.h:62
bool generateCode()
Definition: Trainer.cpp:71
bool loadCode()
Definition: Trainer.cpp:55
void say(std::string message)
Definition: Commands.cpp:112
static unsigned int GAME_TIME
Definition: Game.h:39
int current_line
Definition: Trainer.cpp:125
std::vector< execution_line > program
Definition: Trainer.cpp:53
std::stack< int > accums
Definition: Trainer.cpp:126
std::list< Player > players
Definition: World.cpp:30
Trainer(Commands *commands)
Definition: Trainer.cpp:128
std::vector< std::string > code
Definition: Trainer.cpp:33
static bool TRAINER_LOGGING
Definition: Config.h:37