forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_loop.cpp
398 lines (365 loc) · 12.8 KB
/
main_loop.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "stdafx.h"
#include "main_loop.h"
#include "view.h"
#include "highscores.h"
#include "music.h"
#include "options.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "dirent.h"
#include "progress_meter.h"
#include "file_sharing.h"
#include "square.h"
#include "clock.h"
#include "model_builder.h"
#include "parse_game.h"
MainLoop::MainLoop(View* v, Highscores* h, FileSharing* fSharing, const string& freePath,
const string& uPath, Options* o, Jukebox* j, std::atomic<bool>& fin)
: view(v), dataFreePath(freePath), userPath(uPath), options(o), jukebox(j),
highscores(h), fileSharing(fSharing), finished(fin) {
}
vector<MainLoop::SaveFileInfo> MainLoop::getSaveFiles(const string& path, const string& suffix) {
vector<SaveFileInfo> ret;
DIR* dir = opendir(path.c_str());
CHECK(dir) << "Couldn't open " + path;
while (dirent* ent = readdir(dir)) {
string name(path + "/" + ent->d_name);
if (endsWith(name, suffix)) {
struct stat buf;
stat(name.c_str(), &buf);
ret.push_back({ent->d_name, buf.st_mtime, false});
}
}
closedir(dir);
sort(ret.begin(), ret.end(), [](const SaveFileInfo& a, const SaveFileInfo& b) {
return a.date > b.date;
});
return ret;
}
static string getDateString(time_t t) {
char buf[100];
strftime(buf, sizeof(buf), "%c", std::localtime(&t));
return buf;
}
static const int saveVersion = 100;
static bool isCompatible(int loadedVersion) {
return loadedVersion > 2 && loadedVersion <= saveVersion && loadedVersion / 100 == saveVersion / 100;
}
static string getSaveSuffix(Model::GameType t) {
switch (t) {
case Model::GameType::KEEPER: return ".kep";
case Model::GameType::ADVENTURER: return ".adv";
case Model::GameType::RETIRED_KEEPER: return ".ret";
case Model::GameType::AUTOSAVE: return ".aut";
}
}
template <typename InputType>
static unique_ptr<Model> loadGameUsing(const string& filename, bool eraseFile) {
unique_ptr<Model> model;
try {
InputType input(filename.c_str());
string discard;
int version;
input.getArchive() >>BOOST_SERIALIZATION_NVP(version) >> BOOST_SERIALIZATION_NVP(discard);
Serialization::registerTypes(input.getArchive(), version);
input.getArchive() >> BOOST_SERIALIZATION_NVP(model);
} catch (boost::archive::archive_exception& ex) {
return nullptr;
}
if (eraseFile)
remove(filename.c_str());
return model;
}
static unique_ptr<Model> loadGame(const string& filename, bool eraseFile) {
if (auto model = loadGameUsing<CompressedInput>(filename, eraseFile))
return model;
return loadGameUsing<CompressedInput2>(filename, eraseFile);
}
bool isNonAscii(char c) {
return !(c>=0 && c <128);
}
string stripNonAscii(string s) {
s.erase(remove_if(s.begin(),s.end(), isNonAscii), s.end());
return s;
}
static void saveGame(PModel& model, const string& path) {
try {
CompressedOutput out(path);
Serialization::registerTypes(out.getArchive(), saveVersion);
string game = model->getGameDisplayName();
out.getArchive() << BOOST_SERIALIZATION_NVP(saveVersion) << BOOST_SERIALIZATION_NVP(game)
<< BOOST_SERIALIZATION_NVP(model);
} catch (boost::archive::archive_exception& ex) {
FAIL << ex.what();
}
}
View::ListElem MainLoop::getGameName(const SaveFileInfo& save) {
auto info = getNameAndVersion(userPath + "/" + save.filename);
return View::ListElem(info->first, getDateString(save.date));
}
int MainLoop::getSaveVersion(const SaveFileInfo& save) {
if (auto info = getNameAndVersion(userPath + "/" + save.filename))
return info->second;
else
return -1;
}
void MainLoop::uploadFile(const string& path) {
ProgressMeter meter(1);
atomic<bool> cancelled(false);
view->displaySplash(meter, View::UPLOADING, [&] { cancelled = true; fileSharing->getCancelFun()();});
optional<string> error = fileSharing->uploadRetired(path, meter);
view->clearSplash();
if (error && !cancelled)
view->presentText("Error uploading file", *error);
}
string MainLoop::getSavePath(Model* model, Model::GameType gameType) {
return userPath + "/" + stripNonAscii(model->getGameIdentifier()) + getSaveSuffix(gameType);
}
void MainLoop::saveUI(PModel& model, Model::GameType type, View::SplashType splashType) {
ProgressMeter meter(1.0 / 62500);
Square::progressMeter = &meter;
view->displaySplash(meter, splashType);
string path = getSavePath(model.get(), type);
MEASURE(saveGame(model, path), "saving time");
view->clearSplash();
Square::progressMeter = nullptr;
if (type == Model::GameType::RETIRED_KEEPER && options->getBoolValue(OptionId::ONLINE))
uploadFile(path);
}
void MainLoop::eraseAutosave(Model* model) {
remove(getSavePath(model, Model::GameType::AUTOSAVE).c_str());
}
void MainLoop::getSaveOptions(const vector<pair<Model::GameType, string>>& games,
vector<View::ListElem>& options, vector<SaveFileInfo>& allFiles) {
for (auto elem : games) {
vector<SaveFileInfo> files = getSaveFiles(userPath, getSaveSuffix(elem.first));
files = ::filter(files, [this] (const SaveFileInfo& info) { return isCompatible(getSaveVersion(info));});
append(allFiles, files);
if (!files.empty()) {
options.emplace_back(elem.second, View::TITLE);
append(options, transform2<View::ListElem>(files,
[this] (const SaveFileInfo& info) { return getGameName(info);}));
}
}
}
void MainLoop::getDownloadOptions(vector<View::ListElem>& options, vector<SaveFileInfo>& allFiles,
const string& title) {
vector<FileSharing::GameInfo> games = fileSharing->listGames();
sort(games.begin(), games.end(), [] (const FileSharing::GameInfo& a, const FileSharing::GameInfo& b) {
return a.time > b.time; });
options.emplace_back(title, View::TITLE);
for (FileSharing::GameInfo info : games)
if (isCompatible(info.version)) {
bool dup = false;
for (auto& elem : allFiles)
if (elem.filename == info.filename) {
dup = true;
break;
}
if (dup)
continue;
options.emplace_back(info.displayName, getDateString(info.time));
allFiles.push_back({info.filename, info.time, true});
}
}
optional<MainLoop::SaveFileInfo> MainLoop::chooseSaveFile(const vector<View::ListElem>& options,
const vector<SaveFileInfo>& allFiles, string noSaveMsg, View* view) {
if (options.empty()) {
view->presentText("", noSaveMsg);
return none;
}
auto ind = view->chooseFromList("Choose game", options, 0);
if (ind)
return allFiles[*ind];
else
return none;
}
void MainLoop::playModel(PModel model, bool withMusic, bool noAutoSave) {
view->reset();
const int stepTimeMilli = 3;
Intervalometer meter(stepTimeMilli);
double totTime = model->getTime();
double lastMusicUpdate = -1000;
double lastAutoSave = model->getTime();
while (1) {
double gameTimeStep = view->getGameSpeed() / stepTimeMilli;
if (auto exitInfo = model->update(totTime)) {
if (!exitInfo->isAbandoned())
saveUI(model, exitInfo->getGameType(), View::SAVING);
eraseAutosave(model.get());
return;
}
if (lastMusicUpdate < totTime - 1 && withMusic) {
jukebox->setType(model->getCurrentMusic(), model->changeMusicNow());
lastMusicUpdate = totTime;
}
if (model->isTurnBased())
++totTime;
else
totTime += min(1.0, double(meter.getCount(view->getTimeMilli())) * gameTimeStep);
if (lastAutoSave < model->getTime() - getAutosaveFreq() && !noAutoSave) {
if (options->getBoolValue(OptionId::AUTOSAVE))
saveUI(model, Model::GameType::AUTOSAVE, View::AUTOSAVING);
lastAutoSave = model->getTime();
}
}
}
int MainLoop::getAutosaveFreq() {
return 1500;
}
void MainLoop::playGameChoice() {
while (1) {
auto choice = view->chooseGameType();
PModel model;
switch (choice) {
case View::KEEPER_CHOICE:
options->setDefaultString(OptionId::KEEPER_NAME, NameGenerator::get(NameGeneratorId::FIRST)->getNext());
if (options->handleOrExit(view, OptionSet::KEEPER, -1)) {
model = keeperGame();
}
break;
case View::ADVENTURER_CHOICE:
options->setDefaultString(OptionId::ADVENTURER_NAME,
NameGenerator::get(NameGeneratorId::FIRST)->getNext());
if (options->handleOrExit(view, OptionSet::ADVENTURER, -1)) {
model = adventurerGame();
}
break;
case View::LOAD_CHOICE:
model = loadPrevious(eraseSave(options));
break;
case View::BACK_CHOICE:
return;
}
if (model) {
model->setOptions(options);
model->setHighscores(highscores);
jukebox->setType(MusicType::PEACEFUL, true);
playModel(std::move(model));
}
view->reset();
}
}
void MainLoop::splashScreen() {
ProgressMeter meter(1);
jukebox->setType(MusicType::INTRO, true);
NameGenerator::init(dataFreePath + "/names");
playModel(ModelBuilder::splashModel(meter, view, dataFreePath + "/splash.txt"), false, true);
}
void MainLoop::showCredits(const string& path, View* view) {
ifstream in(path);
CHECK(!!in);
vector<View::ListElem> lines;
while (1) {
char buf[100];
in.getline(buf, 100);
if (!in)
break;
string s(buf);
if (s.back() == ':')
lines.emplace_back(s, View::TITLE);
else
lines.emplace_back(s, View::NORMAL);
}
view->presentList("Credits", lines, false);
}
void MainLoop::start(bool tilesPresent) {
if (options->getBoolValue(OptionId::MUSIC))
jukebox->toggle(true);
splashScreen();
view->reset();
if (!tilesPresent)
view->presentText("", "You are playing a version of KeeperRL without graphical tiles. "
"Besides lack of graphics and music, this "
"is the same exact game as the full version. If you'd like to buy the full version, "
"please visit keeperrl.com.\n \nYou can also get it by donating to any wildlife charity. "
"More information on the website.");
int lastIndex = 0;
while (1) {
jukebox->setType(MusicType::MAIN, true);
auto choice = view->chooseFromList("", {
"Play game", "Change settings", "View high scores", "View credits", "Quit"}, lastIndex, View::MAIN_MENU);
if (!choice)
continue;
lastIndex = *choice;
switch (*choice) {
case 0: playGameChoice(); break;
case 1: options->handle(view, OptionSet::GENERAL); break;
case 2: highscores->present(view); break;
case 3: showCredits(dataFreePath + "/credits.txt", view); break;
case 4: finished = true; break;
}
if (finished)
break;
}
}
PModel MainLoop::keeperGame() {
PModel model;
string ex;
ProgressMeter meter(1.0 / 166000);
view->displaySplash(meter, View::CREATING);
NameGenerator::init(dataFreePath + "/names");
model = ModelBuilder::collectiveModel(meter, options, view,
NameGenerator::get(NameGeneratorId::WORLD)->getNext());
view->clearSplash();
return model;
}
PModel MainLoop::loadModel(string file, bool erase) {
ProgressMeter meter(1.0 / 62500);
Square::progressMeter = &meter;
view->displaySplash(meter, View::LOADING);
Debug() << "Loading from " << file;
PModel ret = loadGame(userPath + "/" + file, erase);
view->clearSplash();
if (ret) {
ret->setView(view);
} else
view->presentText("Sorry", "This save file is corrupted :(");
return ret;
}
bool MainLoop::downloadGame(const string& filename) {
ProgressMeter meter(1);
atomic<bool> cancelled(false);
view->displaySplash(meter, View::DOWNLOADING, [&] { cancelled = true; fileSharing->getCancelFun()();});
optional<string> error = fileSharing->download(filename, userPath, meter);
view->clearSplash();
if (error && !cancelled)
view->presentText("Error downloading file", *error);
return !error;
}
PModel MainLoop::adventurerGame() {
vector<View::ListElem> elems;
vector<SaveFileInfo> files;
getSaveOptions({
{Model::GameType::RETIRED_KEEPER, "Retired local games:"}}, elems, files);
if (options->getBoolValue(OptionId::ONLINE))
getDownloadOptions(elems, files, "Retired online games:");
optional<SaveFileInfo> savedGame = chooseSaveFile(elems, files, "No retired games found.", view);
if (savedGame) {
if (savedGame->download)
if (!downloadGame(savedGame->filename))
return nullptr;
return loadModel(savedGame->filename, false);
}
else
return nullptr;
}
PModel MainLoop::loadPrevious(bool erase) {
vector<View::ListElem> options;
vector<SaveFileInfo> files;
getSaveOptions({
{Model::GameType::AUTOSAVE, "Recovered games:"},
{Model::GameType::KEEPER, "Keeper games:"},
{Model::GameType::ADVENTURER, "Adventurer games:"}}, options, files);
optional<SaveFileInfo> savedGame = chooseSaveFile(options, files, "No save files found.", view);
if (savedGame)
return loadModel(savedGame->filename, erase);
else
return nullptr;
}
bool MainLoop::eraseSave(Options* options) {
#ifdef RELEASE
return !options->getBoolValue(OptionId::KEEP_SAVEFILES);
#endif
return false;
}