]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/game.cpp
Some structure Changes
[dragonblocks-bedrock.git] / src / game.cpp
1 #include <string>
2 #include <iostream>
3 #include <cstdio>
4 #include <sys/types.h>
5 #include <dirent.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include "mods.h"
9 #include "game.h"
10 #include "player.h"
11 using namespace std;
12
13 Map *Game::map;
14 string Game::worlddir;
15 string Game::logfile_path;
16 int Game::seed;
17 char **Game::argv;
18 int *Game::argc;
19 FILE *Game::logfile;
20 string Game::userdir;
21 Player *Game::player;
22
23 void Game::log(string text, int level){
24         string prefix;
25         int color;
26         switch(level){
27                 case WARNING: 
28                         color = CODE_ORANGE;
29                         prefix = "WARNING"; 
30                         break;
31                 case ERROR:
32                         color = CODE_RED;
33                         prefix = "ERROR"; 
34                         break;
35                 case INFO:
36                         color = CODE_LIGHTBLUE;
37                         prefix = "INFO"; 
38                         break;
39                 case LOG:
40                         color = CODE_BLUE;
41                         prefix = "LOG"; 
42                         break;
43                 case EASTEREGG:
44                         color = CODE_VIOLET;
45                         prefix = "EASTEREGG";
46                 default: break;
47         }
48         cout << "\e[3" << color << "m" << "[" << prefix << "] \e[0m" << text << endl;
49         if(logfile)
50                 fprintf(logfile, "[%s] %s\n", prefix.c_str(), text.c_str());
51 }
52 void Game::log(string text){
53         log(text, LOG);
54 }
55 void Game::help(){
56         cout << "Usage: " << argv[0] << "[OPTIONS]" << endl;
57         cout << "Options:" << endl;
58         cout << "\t" << "-h" << "\t" << "--help" << "\t\t\t" << "Display this help and exit." << endl;
59         cout << "\t" << "-v" << "\t" << "--version" << "\t\t" << "Display version info." << endl;
60         cout << "\t" << "-p" << "\t" << "--worldpath [PATH]" << "\t" << "Set world path." << endl;
61         cout << "\t" << "-w" << "\t" << "--worldname [PATH]" << "\t" << "Select world by name (Worlds are placed in ~/.dragonblocks/worlds/)." << endl;
62         cout << "\t" << "-r" << "\t" << "--worldlist" << "\t\t" << "Show a list of your worlds." << endl;
63         cout << "\t" << "-s" << "\t" << "--seed [NUMBER]" << "\t\t" << "Set seed." << endl;
64         cout << "\t" << "-l" << "\t" << "--logfile [PATH]" << "\t" << "Set logfile." << endl;
65 }
66 void Game::version(){
67         cout << "DRAGONBLOCKS BEDROCK EDITION" << endl;
68         cout << "Written in C++" << endl;
69         cout << "An Open Source Project by Elias Fleckenstein" << endl;
70         cout << "Dragonblocks " << VERSION << endl;
71 }
72 void Game::worldlist(){
73         log("Your worlds:");
74         DIR *folder = opendir((userdir + "/worlds/").c_str());;
75     if(!folder){
76                 Game::log("Cant Open World Directory", ERROR);
77                 exit(EXIT_FAILURE);
78         }
79         struct dirent *entry;
80         int files = 0;
81     while(entry = readdir(folder))
82     {
83         files++;
84                 if(entry->d_name[0] != '.')
85                         cout << "\t" << entry->d_name << endl;
86     }
87     files -= 2;
88         cout << "You have ";
89         if(files == 0) 
90                 cout << "no";
91         else
92                 cout << files;
93         cout << " World";
94         if(files != 1)
95                 cout << "s";
96         cout << "." << endl;
97         closedir(folder);
98     return;
99 }
100 void Game::save(){
101         map -> save();
102 }