Phoenix2D-Library  0.10
Game.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 <pthread.h>
23 #include <boost/regex.hpp>
24 #include "Self.h"
25 #include "Game.h"
26 #include "Config.h"
27 namespace Phoenix
28 {
29 std::string play_modes = "before_kick_off corner_kick_l corner_kick_r free_kick_l free_kick_r goal_kick_l goal_kick_r kick_in_l kick_in_r kick_off_l kick_off_r play_on";
30 std::string events = "drop_ball goal_l goal_r offside_l offside_r";
31 boost::regex goal_regex("goal_(l|r)_(\\d+)");
32 static bool cycle_flag = false;
33 static bool on_game = true;
34 static pthread_cond_t cycle_cond = PTHREAD_COND_INITIALIZER;
35 static pthread_mutex_t cycle_mutex = PTHREAD_MUTEX_INITIALIZER;
36 
37 unsigned int Game::GAME_TIME = 0;
38 unsigned int Game::SIMULATION_TIME = 0;
39 unsigned int Game::GOALS = 0;
40 unsigned int Game::GOALS_AGAINST = 0;
41 std::string Game::PLAY_MODE = "before_kick_off";
42 
44 
45 }
46 
48  if (Config::VERBOSE) std::cout << "Game out" << std::endl;
49 }
50 
51 void Game::updateTime(int game_time) {
52  int success = pthread_mutex_lock(&cycle_mutex);
53  if (success) {
54  std::cerr << "Game::updateTimes(int) -> can not lock mutex" << std::endl;
55  return;
56  }
57  Game::GAME_TIME = game_time;
59  cycle_flag = true;
60  success = pthread_mutex_unlock(&cycle_mutex);
61  if (success) {
62  std::cerr << "Game::updateTimes(int) -> can not unlock mutex" << std::endl;
63  }
64  success = pthread_cond_signal(&cycle_cond);
65  if (success) {
66  std::cerr << "Game::updateTime(int) -> can not signal to blocked threads" << std::endl;
67  }
68 }
69 
70 void Game::updatePlayMode(std::string play_mode) {
71  boost::cmatch match;
72  if (boost::regex_match(play_mode.c_str(), match, goal_regex)) {
73  int goals = atoi((std::string() + match[2]).c_str());
74  if (Self::SIDE.compare(match[1])) {
75  Game::GOALS = goals;
76  } else {
77  Game::GOALS_AGAINST = goals;
78  }
79  play_mode = std::string() + "goal_" + match[1];
80  }
81  if (play_modes.find(play_mode) != std::string::npos) {
82  Game::PLAY_MODE = play_mode;
83  } else if (play_mode.compare("time_over") == 0) {
84  on_game = false;
85  } else if (events.find(play_mode) != std::string::npos) {
86  std::cout << Game::SIMULATION_TIME << ": " << play_mode << std::endl;
87  } else {
88  std::cerr << "Not recognized: " << play_mode;
89  }
90 }
91 
93  int success = pthread_mutex_lock(&cycle_mutex);
94  if (success) {
95  std::cerr << "Game::nextCycle() -> can not lock mutex" << std::endl;
96  return false;
97  }
98  while (!cycle_flag) {
99  success = pthread_cond_wait(&cycle_cond, &cycle_mutex);
100  if (success) {
101  std::cerr << "Game::nextCycle() -> can not wait for condition" << std::endl;
102  return false;
103  }
104  }
105  cycle_flag = false;
106  success = pthread_mutex_unlock(&cycle_mutex);
107  if (success) {
108  std::cerr << "Game::nextCycle() -> can not unlock mutex" << std::endl;
109  }
110  return on_game;
111 }
112 }
void updatePlayMode(std::string play_mode)
Definition: Game.cpp:70
static bool nextCycle()
Definition: Game.cpp:92
static pthread_cond_t cycle_cond
Definition: Game.cpp:34
boost::regex goal_regex("goal_(l|r)_(\\d+)")
static unsigned int GOALS_AGAINST
Definition: Game.h:41
static std::string PLAY_MODE
Definition: Game.h:42
static std::string SIDE
Definition: Self.h:50
static unsigned int GOALS
Definition: Game.h:40
static bool VERBOSE
Definition: Config.h:38
static bool cycle_flag
Definition: Game.cpp:32
std::string play_modes
Definition: Game.cpp:29
std::string events
Definition: Game.cpp:30
static unsigned int SIMULATION_TIME
Definition: Game.h:38
static unsigned int GAME_TIME
Definition: Game.h:39
static pthread_mutex_t cycle_mutex
Definition: Game.cpp:35
static bool on_game
Definition: Game.cpp:33
void updateTime(int game_time)
Definition: Game.cpp:51