aboutsummaryrefslogtreecommitdiff
path: root/frontend/main.cpp
blob: f8e25268c38dc87e2acee225e81147a693536e6b (plain)
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
#include <cstdlib>
#include <cstdio>
#include <memory>

#include "backend/print.h"
#include "backend/Dungeon.h"

#include "GameData.h"
#include "GameController.h"
#include "Exception.h"
#include "load_dungeon.h"
#include "generate_dungeon.h"
#include "rl.h"
#include "strings.h"

using namespace std;

static unique_ptr<Dungeon> make_dungeon() noexcept {
	while (1) {
		str_print(strings::INTRO);
		string filename = rl();
		try {
			if (filename.size() == 0) {
				return generate_dungeon();
			} else {
				return load_dungeon(filename);
			}
		} catch (Exception & e) {
			lprtf("FOUT: %s\n", e.what());
		}
	}
}

FollowupAction game_main() {
	unique_ptr<Dungeon> dungeon = make_dungeon();
	GameController game { *dungeon };

	while (1) {
		string line = rl();
		if (line.length() == 0) continue;
		FollowupAction action = game.cmd(line);

		switch (action) {
			case NONE: break;
			case UPDATE: {
				dungeon->update();
				break;
			}
			default: return action;
		}
	}
}

int main() {
	try {
		GameData::get_instance(); // pre-load DB
	} catch (Exception & e) {
		lprtf("FOUT: %s\n", e.what());
		return EXIT_FAILURE;
	}

	FollowupAction action;
	do {
		action = game_main();
		if (action == EXIT) break;
	} while (action == RESTART);

	return EXIT_SUCCESS;
}