-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
174 lines (149 loc) · 4.51 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
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
#include "rapidjson/filereadstream.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "src/parser.hpp"
#include "src/dungeon.hpp"
#include "src/player.hpp"
#include <iostream>
#ifdef __APPLE__
#include <editline/readline.h>
#else
#include <readline/readline.h>
#include <readline/history.h>
#endif
using namespace rapidjson;
Document parseFile(std::string filename) {
FILE* fp = fopen(filename.c_str(), "r");
if (!fp) {
std::cerr << "config.json not found, exiting" << std::endl;
exit(1);
}
//Create a stream buffer, process file as character stream
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);
fclose(fp);
//Return errors if present with character offset into file
if (d.HasParseError()) {
std::cerr << "Error (offset " << d.GetErrorOffset() << ") : "
<< GetParseError_En(d.GetParseError()) << std::endl;
exit(1);
}
return d;
}
void printArray(std::string msg, Value & val) {
std::cout << msg;
for (SizeType i = 0; i < val.Size()-1; ++i) {
std::cout << val[i].GetString() << ", ";
}
std::cout << val[val.Size()-1].GetString() << std::endl;
}
void printHelp(Document & config) {
Value& words = config["words"];
printArray("Inspect environment with: ", words["introspect"]);
printArray("Use items with: ", words["action"]);
printArray("Take items with: ", words["add_inventory"]);
printArray("Drop items with: ", words["del_inventory"]);
printArray("View inventory with: ", words["view_inventory"]);
printArray("Move between rooms with: ", words["movement"]);
printArray("Quit the game with: ", words["quit"]);
std::cout << "Many of these phrases can be used with prepositions, such as 'pick up cat'." << std::endl;
std::cout << "All words in a group have equivalent behavior, i.e. take and pick up do the same thing" << std::endl;
}
void fail() {
std::cout << "I don't understand." << std::endl;
}
int main(int argc, char const* argv[]) {
Document config = parseFile("config.json");
Game::Parser parser;
Game::Dungeon dungeon;
Game::Player player;
std::vector<Game::Token> words;
parser.LoadConfig(config);
if (!dungeon.LoadDungeon(config, parser)) {
std::cerr << "Failed to load dungeon from config!" << std::endl;
return 1;
}
if (config.HasMember("title")) {
std::cout << config["title"].GetString() << std::endl;
}
//place player in dungeon
player.Move(dungeon.GetStart());
// Game loop
Game::Item* item;
while (player.IsAlive()) {
std::string input = readline("> ");
if (input == "") {
continue;
}
add_history(input.c_str());
parser.ParseLine(input, words);
std::string match = parser.MatchRule(words);
if (match == "view_inventory") {
player.PrintInventory();
}
else if (match == "add_inventory") {
item = player.GetRoom()->TakeItem(words.back().word);
if (item) {
player.AddInventory(words.back().word, item);
} else {
fail();
}
}
else if (match == "del_inventory") {
player.DelInventory(words.back().word);
}
else if (match == "introspect") {
if (words.back().grammar == "room") {
std::cout << player.GetRoom()->Inspect() << std::endl;
std::cout << "Door directions:" << std::endl;
for (auto door : player.GetRoom()->ListDoors()) {
std::cout << door << std::endl;
}
} else if (words.back().grammar == "item") {
item = player.GetItem(words.back().word);
if (item) {
std::cout << item->GetDescription() << std::endl;
} else {
item = player.GetRoom()->GetItem(words.back().word);
if (item) {
std::cout << item->GetDescription() << std::endl;
} else {
fail();
}
}
}
}
else if (match == "action") {
std::string result = player.UseItem(words.back().word);
if (result == "") {
result = player.GetRoom()->UseItem(words.back().word);
if (result == "") {
fail();
}
}
std::cout << result << std::endl;
}
else if (match == "movement") {
Game::Room* room = player.GetRoom()->GetNeighbor(words.back().word);
if (room) {
player.Move(room);
} else {
fail();
}
}
else if (match == "help_text") {
printHelp(config);
}
else if (match == "quit") {
break;
}
else {
fail();
}
words.clear();
}
std::cout << "Game Over!" << std::endl;
return 0;
}