Phoenix2D-Library  0.10
Config.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 <fstream>
22 #include <boost/regex.hpp>
23 #include <cstdlib>
24 #include <iostream>
25 #include "Config.h"
26 #include "Self.h"
27 namespace Phoenix
28 {
29 boost::regex config_regex("\\(config\\s(\\w+)\\s(\\w+)\\)");
30 boost::regex position_regex("\\(position\\s(\\d+)\\s([\\d\\.\\-]+)\\s([\\d\\.\\-]+)\\)");
31 boost::regex logging_regex("\\(logging\\s(\\d+)\\)");
32 boost::regex verbose_regex("\\(verbose\\s(\\d+)\\)");
33 boost::regex player_history_regex("\\(player_history\\s(\\d+)\\)");
34 
35 //Globals
36 unsigned int Config::BUFFER_MAX_HISTORY = 4;
37 unsigned int Config::PLAYER_MAX_HISTORY = 16;
38 unsigned int Config::BALL_MAX_HISTORY = 16;
39 unsigned int Config::COMMANDS_MAX_HISTORY = 4;
40 unsigned int Config::COMMAND_PRECISION = 4;
41 std::string Config::LOG_NAME = "";
42 //individuals
43 Position Config::POSITION;
44 bool Config::PLAYER_HISTORY = false;
45 bool Config::LOGGING = false;
46 bool Config::TRAINER_LOGGING = false;
47 bool Config::VERBOSE = false;
48 
50  std::ifstream file("conf.phx", std::ifstream::in);
51  if (file) {
52  std::string line;
53  while (std::getline(file, line)) {
54  if (line.size() > 0 && line[0] == '#') continue;
55  boost::cmatch match;
56  if (boost::regex_match(line.c_str(), match, config_regex)) {
57  std::string config = std::string() + match[1];
58  if (config.compare("buffer_max_history") == 0) {
59  Config::BUFFER_MAX_HISTORY = (unsigned int)atoi((std::string() + match[2]).c_str());
60  } else if (config.compare("player_max_history") == 0) {
61  Config::PLAYER_MAX_HISTORY = (unsigned int)atoi((std::string() + match[2]).c_str());
62  } else if (config.compare("ball_max_history") == 0) {
63  Config::BALL_MAX_HISTORY = (unsigned int)atoi((std::string() + match[2]).c_str());
64  } else if (config.compare("commands_max_history") == 0) {
65  Config::COMMANDS_MAX_HISTORY = (unsigned int)atoi((std::string() + match[2]).c_str());
66  } else if (config.compare("commands_precision") == 0) {
67  Config::COMMAND_PRECISION = (unsigned int)atoi((std::string() + match[2]).c_str());
68  } else if (config.compare("log_name") == 0) {
69  Config::LOG_NAME = std::string() + match[2];
70  }
71  } else if (boost::regex_match(line.c_str(), match, logging_regex)) {
72  int number = atoi((std::string() + match[1]).c_str());
73  if (number == 0) {
75  }
76  }
77  }
78  file.close();
79  } else {
80  std::cerr << "Config::Config() -> error opening conf.phx file" << std::cerr;
81  }
82 }
83 
85 
86 }
87 
88 void Config::load() {
89  switch (Self::UNIFORM_NUMBER) {
90  case 1:
91  Config::POSITION = Position(-50.0, 0.0);;
92  break;
93  case 2:
94  Config::POSITION = Position(-10.0, 0.0);
95  break;
96  case 3:
97  Config::POSITION = Position(-1.0, -10.0);
98  break;
99  case 4:
100  Config::POSITION = Position(-1.0, 10.0);
101  break;
102  case 5:
103  Config::POSITION = Position(-12.0, 0.0);
104  break;
105  case 6:
106  Config::POSITION = Position(-2.0, -11.0);
107  break;
108  case 7:
109  Config::POSITION = Position(-2.0, 11.0);
110  break;
111  case 8:
112  Config::POSITION = Position(-14.0, -5.0);
113  break;
114  case 9:
115  Config::POSITION = Position(-14.0, 5.0);
116  break;
117  case 10:
118  Config::POSITION = Position(-14.0, -10.0);
119  break;
120  case 11:
121  Config::POSITION = Position(-14.0, 10.0);
122  break;
123  }
124  std::string filename = Self::TEAM_NAME + ".phx";
125  std::ifstream file(filename.c_str(), std::ifstream::in);
126  if (file) {
127  std::string line;
128  while (std::getline(file, line)) {
129  if (line.size() > 0 && line[0] == '#') continue;
130  boost::cmatch match;
131  if (boost::regex_match(line.c_str(), match, position_regex)) {
132  int number = atoi((std::string() + match[1]).c_str());
133  double x = atof((std::string() + match[2]).c_str());
134  double y = atof((std::string() + match[3]).c_str());
135  if (Self::UNIFORM_NUMBER == number) {
136  Config::POSITION = Position(x, y);
137  }
138  } else if (boost::regex_match(line.c_str(), match, logging_regex)) {
139  int number = atoi((std::string() + match[1]).c_str());
140  if (Self::UNIFORM_NUMBER == number) {
141  Config::LOGGING = true;
142  }
143  } else if (boost::regex_match(line.c_str(), match, verbose_regex)) {
144  int number = atoi((std::string() + match[1]).c_str());
145  if (Self::UNIFORM_NUMBER == number) {
146  Config::VERBOSE = true;
147  }
148  } else if (boost::regex_match(line.c_str(), match, player_history_regex)) {
149  int number = atoi((std::string() + match[1]).c_str());
150  if (Self::UNIFORM_NUMBER == number) {
151  Config::PLAYER_HISTORY = true;
152  }
153  }
154  }
155  file.close();
156  } else {
157  std::cerr << "Config::load() -> error opening " << filename << " file" << std::endl;
158  }
159 }
160 }
static int UNIFORM_NUMBER
Definition: Self.h:49
static unsigned int BUFFER_MAX_HISTORY
Definition: Config.h:39
Position The Position lorem Ipsum
Definition: Position.h:50
static std::string LOG_NAME
Definition: Config.h:44
boost::regex logging_regex("\\(logging\\s(\\d+)\\)")
boost::regex verbose_regex("\\(verbose\\s(\\d+)\\)")
static double x
Definition: Self.cpp:89
static std::string TEAM_NAME
Definition: Self.h:48
static unsigned int COMMANDS_MAX_HISTORY
Definition: Config.h:42
static unsigned int PLAYER_MAX_HISTORY
Definition: Config.h:40
static bool VERBOSE
Definition: Config.h:38
boost::regex position_regex("\\(position\\s(\\d+)\\s([\\d\\.\\-]+)\\s([\\d\\.\\-]+)\\)")
boost::regex player_history_regex("\\(player_history\\s(\\d+)\\)")
static double y
Definition: Self.cpp:90
void load()
Definition: Config.cpp:88
static unsigned int COMMAND_PRECISION
Definition: Config.h:43
static bool LOGGING
Definition: Config.h:36
boost::regex config_regex("\\(config\\s(\\w+)\\s(\\w+)\\)")
static bool PLAYER_HISTORY
Definition: Config.h:45
static unsigned int BALL_MAX_HISTORY
Definition: Config.h:41
static Position POSITION
Definition: Config.h:35
static bool TRAINER_LOGGING
Definition: Config.h:37