1#include <Config/Globals.hpp>
2#include <Config/INI.hpp>
3#include <Misc/Utils.hpp>
4#include <Flow/InputManager.hpp>
5#include <Game/BoardParser.hpp>
6#include <Game/ScoreFile.hpp>
22std::string Globals::Config::directory =
"";
23std::string Globals::Config::file =
"";
24std::string Globals::Config::scoresFile =
"";
26bool Globals::Screen::center_horizontally =
true;
27bool Globals::Screen::center_vertically =
true;
29bool Globals::Screen::show_borders =
true;
30bool Globals::Screen::fancy_borders =
true;
31bool Globals::Screen::outer_border =
true;
33unsigned int Globals::Game::starting_speed = 1;
34int Globals::Game::fruits_at_once = 1;
35bool Globals::Game::random_walls =
false;
36bool Globals::Game::teleport =
false;
37std::string Globals::Game::current_level =
"";
39Globals::Game::BoardSize Globals::Game::board_size = LARGE;
41Globals::Game::BoardSize Globals::Game::intToBoardSize(
int val)
44 return Globals::Game::SMALL;
47 return Globals::Game::MEDIUM;
49 return Globals::Game::LARGE;
51int Globals::Game::boardSizeToInt(Globals::Game::BoardSize size)
53 if (size == Globals::Game::SMALL)
56 if (size == Globals::Game::MEDIUM)
62int Globals::Game::board_scroll_delay = 1000;
64bool Globals::Game::board_scroll_up =
false;
65bool Globals::Game::board_scroll_down =
false;
66bool Globals::Game::board_scroll_left =
false;
67bool Globals::Game::board_scroll_right =
false;
69ColorPair Globals::Theme::text;
70ColorPair Globals::Theme::hilite_text;
71ColorPair Globals::Theme::textbox;
73bool Globals::Error::has_config_file =
true;
74bool Globals::Error::has_score_file =
true;
75bool Globals::Error::old_version_score_file =
false;
76bool Globals::Error::strange_score_file =
false;
85 Globals::Theme::text = 0;
86 Globals::Theme::hilite_text = Colors::pair(COLOR_CYAN, COLOR_DEFAULT);
87 Globals::Theme::textbox = (Globals::Theme::hilite_text | A_REVERSE);
96 if (Utils::String::front(Globals::Config::directory) !=
'/')
100 Globals::Config::directory = (
"/tmp/" +
101 Globals::Config::directory);
104 Globals::Config::file = (Globals::Config::directory +
107 Globals::Config::scoresFile = (Globals::Config::directory +
108 "arcade.nsnakescores");
117 Globals::Config::directory =
"/dev/";
118 Globals::Config::file =
"/dev/null";
123 InputManager::bind(
"left", KEY_LEFT);
124 InputManager::bind(
"right", KEY_RIGHT);
125 InputManager::bind(
"up", KEY_UP);
126 InputManager::bind(
"down", KEY_DOWN);
127 InputManager::bind(
"pause",
'p');
128 InputManager::bind(
"help",
'h');
129 InputManager::bind(
"quit",
'q');
145 if (! Globals::Error::has_config_file)
147 std::cout <<
"Warning: We could not create the configuration file.\n"
148 <<
" Please check permissions to the path:\n"
149 <<
" " + Globals::Config::file
152 if (! Globals::Error::has_score_file)
154 std::cout <<
"Warning: We could not create the score file.\n"
155 <<
" Please check permissions to the path:\n"
156 <<
" " + Globals::Config::scoresFile
159 if (Globals::Error::old_version_score_file)
161 std::cout <<
"Warning: Your high score file is from an old nsnake version."
164 if (Globals::Error::strange_score_file)
169 std::cout <<
"Error: Corrupted high score file!\n"
170 <<
" We're sorry, but we had to erase it"
185 catch(std::runtime_error& e)
194 std::string buffer =
"";
204#define INI_GET(var, out, in) \
206 buffer = (* ini)(out)[in]; \
207 if (! buffer.empty()) \
209 Utils::String::convert(buffer, var); \
213 INI_GET(Globals::Screen::center_horizontally,
"screen",
"center_horizontal");
214 INI_GET(Globals::Screen::center_vertically,
"screen",
"center_vertical");
216 INI_GET(Globals::Screen::show_borders,
"screen",
"borders");
217 INI_GET(Globals::Screen::fancy_borders,
"screen",
"fancy_borders");
218 INI_GET(Globals::Screen::outer_border,
"screen",
"outer_border");
220 INI_GET(Globals::Game::random_walls,
"game",
"random_walls");
221 INI_GET(Globals::Game::fruits_at_once,
"game",
"fruits_at_once");
222 INI_GET(Globals::Game::teleport,
"game",
"teleport");
223 INI_GET(Globals::Game::board_scroll_delay,
"game",
"board_scroll_delay");
225 INI_GET(Globals::Game::board_scroll_up,
"game",
"board_scroll_up");
226 INI_GET(Globals::Game::board_scroll_down,
"game",
"board_scroll_down");
227 INI_GET(Globals::Game::board_scroll_left,
"game",
"board_scroll_left");
228 INI_GET(Globals::Game::board_scroll_right,
"game",
"board_scroll_right");
232 buffer = (* ini)(
"game")[
"starting_speed"];
233 if (! buffer.empty())
235 int starting_speed = Globals::Game::starting_speed;
236 Utils::String::convert(buffer, starting_speed);
237 Globals::Game::starting_speed = starting_speed;
245 INI_GET(tmp,
"input",
"left");
246 InputManager::bind(
"left", InputManager::stringToKey(tmp));
248 INI_GET(tmp,
"input",
"right");
249 InputManager::bind(
"right", InputManager::stringToKey(tmp));
251 INI_GET(tmp,
"input",
"up");
252 InputManager::bind(
"up", InputManager::stringToKey(tmp));
254 INI_GET(tmp,
"input",
"down");
255 InputManager::bind(
"down", InputManager::stringToKey(tmp));
257 INI_GET(tmp,
"input",
"pause");
258 InputManager::bind(
"pause", InputManager::stringToKey(tmp));
260 INI_GET(tmp,
"input",
"help");
261 InputManager::bind(
"help", InputManager::stringToKey(tmp));
263 INI_GET(tmp,
"input",
"quit");
264 InputManager::bind(
"quit", InputManager::stringToKey(tmp));
268 INI_GET(board_size,
"game",
"board_size");
269 Globals::Game::board_size = Globals::Game::intToBoardSize(board_size);
282 catch(std::runtime_error& e)
292#define INI_SET(out, in, var) \
294 buffer = Utils::String::toString(var); \
295 ini->top().addGroup(out); \
296 (* ini)(out).addKey(in, buffer); \
299 INI_SET(
"screen",
"center_horizontal", Globals::Screen::center_horizontally);
300 INI_SET(
"screen",
"center_vertical", Globals::Screen::center_vertically);
302 INI_SET(
"screen",
"borders", Globals::Screen::show_borders);
303 INI_SET(
"screen",
"fancy_borders", Globals::Screen::fancy_borders);
304 INI_SET(
"screen",
"outer_border", Globals::Screen::outer_border);
306 INI_SET(
"game",
"random_walls", Globals::Game::random_walls);
307 INI_SET(
"game",
"fruits_at_once", Globals::Game::fruits_at_once);
308 INI_SET(
"game",
"teleport", Globals::Game::teleport);
310 INI_SET(
"game",
"board_scroll_delay", Globals::Game::board_scroll_delay);
312 INI_SET(
"game",
"board_scroll_up", Globals::Game::board_scroll_up);
313 INI_SET(
"game",
"board_scroll_down", Globals::Game::board_scroll_down);
314 INI_SET(
"game",
"board_scroll_left", Globals::Game::board_scroll_left);
315 INI_SET(
"game",
"board_scroll_right", Globals::Game::board_scroll_right);
319 int starting_speed = Globals::Game::starting_speed;
320 buffer = Utils::String::toString(starting_speed);
322 (* ini)(
"game").addKey(
"starting_speed", buffer);
329 key = InputManager::keyToString(InputManager::getBind(
"left"));
330 INI_SET(
"input",
"left", key);
332 key = InputManager::keyToString(InputManager::getBind(
"right"));
333 INI_SET(
"input",
"right", key);
335 key = InputManager::keyToString(InputManager::getBind(
"up"));
336 INI_SET(
"input",
"up", key);
338 key = InputManager::keyToString(InputManager::getBind(
"down"));
339 INI_SET(
"input",
"down", key);
341 key = InputManager::keyToString(InputManager::getBind(
"pause"));
342 INI_SET(
"input",
"pause", key);
344 key = InputManager::keyToString(InputManager::getBind(
"help"));
345 INI_SET(
"input",
"help", key);
347 key = InputManager::keyToString(InputManager::getBind(
"quit"));
348 INI_SET(
"input",
"quit", key);
351 int board_size = Globals::Game::boardSizeToInt(Globals::Game::board_size);
352 INI_SET(
"game",
"board_size", board_size);
356 ini->
saveAs(Globals::Config::file);
358 catch(std::runtime_error& e)
static std::string directory
Default directory where the level files are.
Loads, reads and parses the contents of an INI file (or string).
void saveAs(std::string filename)
Save all the internal INI contents on a file with #filename.
Level & top()
Returns the top level of this INI file.
void create()
Creates a blank INI registry.
static std::string directory
Default directory where we store the game score files.
void init()
Allocates necessary variables.
char version[3]
Game version (format MMP - Major Minor Patch).
void loadFile()
Loads configuration from the default file name.
void saveFile()
Saves current configurations to the default file name.
void exit()
Warns the user about any errors and warnings found during the program's execution.
std::string getHome()
Gets the full path of the home directory for the user running this program.
bool create(std::string path)
Creates empty file #path.
bool exists(std::string path)
Tells if #path exists.
bool isDirectory(std::string path)
Tells if #path is a directory.
void mkdir_p(std::string path)
Creates #path directory hierarchy recursively, just like UNIX command mkdir -p.
void addGroup(std::string name)
Creates a new child group with #name.