forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
125 lines (120 loc) · 3.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "stdafx.h"
#include "view.h"
#include "model.h"
#include "quest.h"
#include "tribe.h"
#include "message_buffer.h"
#include "statistics.h"
#include "options.h"
using namespace std;
int main(int argc, char* argv[]) {
View* view;
ifstream input;
ofstream output;
string lognamePref = "log";
Debug::init();
int seed = time(0);
int forceMode = -1;
bool genExit = false;
if (argc == 3) {
genExit = true;
if (argv[2][0] == 'a')
forceMode = 1;
else
forceMode = 0;
}
if (argc == 2 && argv[1][0] == 'd')
forceMode = 1;
else
if (argc == 2 && argv[1][0] != 'l') {
seed = convertFromString<int>(argv[1]);
argc = 1;
}
if (argc == 1 || forceMode > -1) {
Random.init(seed);
string fname(lognamePref);
fname += convertToString(seed);
output.open(fname);
CHECK(output.is_open());
Debug() << "Writing to " << fname;
view = View::createLoggingView(output);
} else {
string fname = argv[1];
Debug() << "Reading from " << fname;
seed = convertFromString<int>(fname.substr(lognamePref.size()));
Random.init(seed);
input.open(fname);
CHECK(input.is_open());
view = View::createReplayView(input);
}
int lastIndex = 0;
while (1) {
Tribe::init();
Item::identifyEverything();
EventListener::initialize();
Statistics::init();
Options::init("options.txt");
NameGenerator::init("first_names.txt", "aztec_names.txt", "creatures.txt",
"artifacts.txt", "world.txt", "town_names.txt", "dwarfs.txt", "gods.txt", "demons.txt", "dogs.txt");
ItemFactory::init();
bool modelReady = false;
messageBuffer.initialize(view);
view->initialize();
auto choice = forceMode > -1 ? Optional<int>(forceMode) : view->chooseFromList("", {
View::ListElem("Choose your profession:", View::TITLE), "Keeper", "Adventurer",
View::ListElem("Or simply:", View::TITLE), "Change options", "View high scores", "Quit"}, lastIndex);
if (!choice)
continue;
lastIndex = *choice;
if (choice == 2) {
Options::handle(view, false);
continue;
}
if (choice == 4)
exit(0);
if (choice == 3) {
Model* m = new Model(view);
m->showHighscore();
continue;
}
unique_ptr<Model> model;
string ex;
thread t = (thread([&] {
for (int i : Range(5)) {
try {
model.reset(choice == 1 ? Model::heroModel(view) : Model::collectiveModel(view));
break;
} catch (string s) {
ex = s;
}
}
modelReady = true;
}));
view->displaySplash(modelReady);
t.join();
if (genExit)
break;
if (!model)
view->presentText("Sorry!", "World generation permanently failed with the following error:\n \n" + ex +
"\n \nIf you would be so kind, please send this line to [email protected] Thanks!");
int var = 0;
view->setTimeMilli(0);
try {
while (1) {
if (model->isTurnBased())
model->update(var++);
else
model->update(double(view->getTimeMilli()) / 300);
}
} catch (GameOverException ex) {
}
#ifdef RELEASE
catch (string ex) {
view->presentText("Sorry!", "The game has crashed with the following error:\n \n" + ex +
"\n \nIf you would be so kind, please send this line and a description of the circumstances to "
" [email protected] Thanks!");
}
#endif
}
return 0;
}