-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREPL.hpp
401 lines (345 loc) · 11.1 KB
/
REPL.hpp
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
398
399
400
401
// Copyright 2016 Anton Erholt <aerholt@kth.se>
#ifndef LAB3_REPL_HPP_
#define LAB3_REPL_HPP_
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include "GameObject.hpp"
#include "Player.hpp"
#include "TickCount.hpp"
namespace lab3 {
typedef std::vector<std::string> Tokens;
struct Repl: public GameObject {
private:
void split(std::string str, char delim, std::vector<std::string> &elems) {
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
}
Player *player;
SmallGameMap *smallGameMap;
Tokens prev{""};
public:
Repl(Player *player, SmallGameMap *smallGameMap = nullptr)
: GameObject(), player(player), smallGameMap(smallGameMap) { }
virtual void action() {
// Read -> Eval -> Print -> Loop => REPL
std::cout << this->player->get_name() << " - "
<< this->player->getCurrent_health()
<< "/" << this->player->getMax_health() << ">";
std::string command_input;
std::getline(std::cin, command_input);
if (std::cin.eof()) {
throw std::out_of_range("Ctrl-D"); // Quit game
}
Tokens tokens;
this->split(command_input, ' ', tokens);
try {
this->parse(tokens);
} catch (std::invalid_argument &ex) {
std::cout << "Arglebargle, glop-glyf!?!" << std::endl;
}
}
void parse(Tokens &tokens) {
if (tokens.empty()) return;
this->previous(tokens);
}
inline void previous(Tokens &tokens) {
if (*tokens.begin() == "p") {
this->look(this->prev);
} else {
this->prev = tokens;
this->look(tokens);
}
}
inline void playerLook() {
std::clog << "player look" << std::endl;
std::cout << std::endl << player->getPosition()->name << std::endl;
for (size_t i = 0; i < player->getPosition()->name.size(); i++) {
std::cout << "=";
}
std::cout << std::endl << player->getPosition()->short_description << std::endl << std::endl;
for (auto &actor : findActorsByPosition(player->getPosition())) {
if (actor->id != player_id)
std::cout << "A " << *actor->getRace() << " is here." << std::endl;
}
if (!INVENTORIES[player->getPositionId()].empty()) {
std::cout << std::endl << std::endl << "Items:" << std::endl;
for (auto &item : INVENTORIES[player->getPositionId()]) {
std::cout << " " << item->getName() << std::endl;
}
}
this->printExits();
}
void printExits() const {
std::cout << "Exits: ";
std::vector<Direction> exits = player->getGame_map()->exits(player->getPosition());
if (exits.size() > 1) {
for (size_t i = 0; i < exits.size() - 1; ++i) {
std::cout << exits[i] << ", ";
}
}
std::cout << exits[exits.size() - 1];
std::cout << std::endl;
}
inline void look(Tokens &tokens) {
if (*tokens.begin() == "look" ||
*tokens.begin() == "l") {
this->playerLook();
return;
}
this->inventory(tokens);
}
inline void inventory(Tokens &tokens) {
if (*tokens.begin() == "inv" ||
*tokens.begin() == "inventory") {
std::cout << "Inventory:" << std::endl << std::endl;
auto inv = getInventory(player_id);
int tot_weight = 0;
for (auto ip : *inv) {
std::cout << ip->getName() << std::endl;
tot_weight += ip->getWeight();
}
std::cout << "Capacity: " << tot_weight << " / " << player->getMaxCarryCapacity()
<< std::endl << std::endl;
return;
}
this->help(tokens);
}
inline void help(Tokens &tokens) {
if (*tokens.begin() == "help" ||
*tokens.begin() == "man") {
std::cout << "There are a few commands:" << std::endl
<< " help - prints this help." << std::endl
<< " look - ('l' works too) look around you." << std::endl
<< " wait - wait a turn or two. Useful near glowing rocks." << std::endl
<< " p - repeats the previous command entered." << std::endl
<< " go *dir* - ('g' works too) walk in direction: " << std::endl
<< " *dir* can be any from " << std::endl
<< std::endl
<< " \"north\", \"south\"" << std::endl
<< " \"west\", \"east\"" << std::endl
<< " \"up\", \"down\"" << std::endl
<< std::endl
<< " quit - will quit the game." << std::endl
<< std::endl
<< std::endl
<< "Interaction commands:"
<< std::endl
<< " talk *target* - talks to *target*." <<
std::endl
<< " fight *target* - ('k' & 'kill' works too) fights with *target*." <<
std::endl
<< " take *item* - takes *item*." << std::endl
<< " inv - lists ITEMS in inventory." << std::endl
<< std::endl
<< std::endl
<< "Objects in the game are shown like this"
<< std::endl
<< "Example:"
<< std::endl
<< " Bird"
<< std::endl
<< std::endl
<< "In order to interact with the objects, simply type their name (lowercase should work) after the interaction."
<< std::endl
<< "Example:"
<< std::endl
<< " '>fight bird' -- fights with the bird"
<< std::endl
<< std::endl
<< "Note: Items may decay after a while. Don't get worried if you no longer carry the items you picked up."
<< std::endl
<< std::endl
<< "New commands may become available after you have gained some experience."
<< std::endl
<< std::endl;
if (player->kills > 3) {
std::cout << std::endl << "Bonus command:" << std::endl;
std::cout << " howl - gives you bonus damage for the next fight." << std::endl << std::endl;
}
return;
}
this->go(tokens);
}
inline void go(Tokens &tokens) {
if (*tokens.begin() == "go" ||
*tokens.begin() == "g") {
auto it = ++tokens.begin();
std::clog << "player go" << std::endl;
bool moved;
if (tokens.size() == 1) {
std::cout << "Go in what direction?" << std::endl;
return;
}
try {
auto dir = parseDirection(*it);
moved = this->player->go(dir);
} catch (std::exception &ex) {
moved = false;
}
if (moved) {
this->playerLook();
++tickCount;
} else {
std::cout << "Unable to move in that direction." << std::endl;
}
return;
}
this->fight(tokens);
}
inline void fight(Tokens &tokens) {
if (*tokens.begin() == "fight" ||
*tokens.begin() == "kill" ||
*tokens.begin() == "k") {
if (tokens.size() < 2) {
std::cout << "Fight who?" << std::endl;
return;
}
auto it = ++tokens.begin();
std::clog << "player fight " << *it << std::endl;
auto targets = findActorsByNameAtPosition(*it, player->getPosition());
if (targets.size() == 0) {
std::cout << "No such target found." << std::endl;
return;
}
uint64_t target_id = targets[0]->id;
if (target_id == player_id) {
std::cout << "Are you trying to kill yourself?" << std::endl;
return;
}
GameObject *target;
try {
target = findGameObjectById(target_id);
} catch (const std::invalid_argument &ex) {
std::cout << "No such target found." << std::endl;
return;
}
Actor *targetActor = dynamic_cast<Actor *>(target);
if (targetActor == nullptr) {
std::cout << "No such target found." << std::endl;
return;
}
if (targetActor->getPosition() == this->player->getPosition()) {
this->player->fight(targetActor);
++tickCount;
return;
}
std::cout << "No such target found." << std::endl;
return;
}
this->talk(tokens);
}
inline void talk(Tokens &tokens) {
if (*tokens.begin() == "talk") {
auto it = ++tokens.begin();
if (tokens.size() >= 2) {
std::clog << "player talk " << *it << std::endl;
}
return;
}
this->take(tokens);
}
inline void take(Tokens &tokens) {
if (*tokens.begin() == "take") {
auto it = ++tokens.begin();
if (tokens.size() >= 2) {
std::clog << "player take " << *it << std::endl;
Item * item = findItemByNameInInventory(*it, player->getPositionId());
if (item == nullptr) {
std::cout << "No such item found." << std::endl;
return;
}
uint64_t item_id = item->id;
player->take(item_id);
return;
}
}
this->drop(tokens);
}
inline void drop(Tokens &tokens) {
if (*tokens.begin() == "drop") {
auto it = ++tokens.begin();
if (tokens.size() >= 2) {
std::clog << "player drop " << *it << std::endl;
Item * item = findItemByNameInInventory(*it, player_id);
if (item == nullptr) {
std::cout << "No such item found." << std::endl;
return;
}
uint64_t item_id = item->id;
player->drop(item_id);
return;
}
}
this->wait(tokens);
}
inline void wait(Tokens &tokens) {
if (*tokens.begin() == "wait") {
std::clog << "player wait" << std::endl;
uint32_t player_hp_diff = this->player->getMax_health() - this->player->getCurrent_health();
if (this->player->getPosition()->id == this->player->start->id &&
player_hp_diff > 0) {
std::uint32_t health_amount = std::min((uint32_t) 20, player_hp_diff);
this->player->heal(health_amount);
std::cout << "The rock heals you with " << health_amount << " points." << std::endl;
}
++tickCount;
return;
}
this->quit(tokens);
}
inline void quit(Tokens &tokens) {
if (*tokens.begin() == "quit") {
throw std::out_of_range("Player quit.");
}
this->howl(tokens);
}
inline void howl(Tokens &tokens) {
if (*tokens.begin() == "howl") {
if (this->player->kills > 3) {
player->howl();
return;
}
}
this->joke(tokens);
}
inline void joke(Tokens &tokens) {
if (*tokens.begin() == "ls") {
std::cout << "Did you mean 'rm -rf /'?" << std::endl;
return;
} else if (*tokens.begin() == "cd") {
std::cout << "No such directory. Try 'go' instead." << std::endl;
return;
}
this->teleport(tokens);
}
// Cheat, crashes the game if no argument is supplied on purpose
inline void teleport(Tokens &tokens) {
if (this->player->cls.name == character_classes[1].name) { // Only C++ programmer can teleport
if (*tokens.begin() == "std::move") {
if (*(tokens.begin() + 1) == "home") {
player->teleport(player->start);
playerLook();
return;
} else if (*(tokens.begin() + 1) == "mountain") {
if (this->smallGameMap != nullptr) {
player->teleport(this->smallGameMap->getBossSpawn());
playerLook();
return;
}
}
}
}
throw std::invalid_argument("Argle.");
}
};
} // namespace lab3
#endif // LAB3_REPL_HPP_