-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZOOrkEngine.cpp
More file actions
450 lines (392 loc) · 16 KB
/
ZOOrkEngine.cpp
File metadata and controls
450 lines (392 loc) · 16 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "ZOOrkEngine.h"
#include "Mushroom.h"
#include "Merchant.h"
#include "FairyFlower.h"
#include <utility>
// Constructor for ZOOrkEngine which initializes the start room and the magic forest
ZOOrkEngine::ZOOrkEngine(std::shared_ptr<Room> start, std::shared_ptr<Room> magic_forest)
: startRoom(start), magicForest(magic_forest) {
player = Player::instance();
player->setCurrentRoom(start.get());
player->getCurrentRoom()->enter();
}
// Method to run the game loop
void ZOOrkEngine::run() {
while (!gameOver) { // Game loop
std::cout << "> ";
std::string input;
std::getline(std::cin, input);
std::vector<std::string> words = tokenizeString(input);
std::string command = words[0];
std::vector<std::string> arguments(words.begin() + 1, words.end());
// Handle different commands
if (command == "go") {
handleGoCommand(arguments);
} else if ((command == "look") || (command == "inspect")) {
handleLookCommand(arguments);
} else if ((command == "take") || (command == "get")) {
handleTakeCommand(arguments);
} else if (command == "drop") {
handleDropCommand(arguments);
} else if (command == "inventory") {
handleInventoryCommand(arguments);
} else if (command == "equipment") {
handleEquipmentCommand(arguments);
} else if (command == "use") {
handleUseCommand(arguments);
} else if (command == "unattach") {
handleUnattachCommand(arguments);
} else if (command == "lift") {
handleLiftCommand(arguments);
} else if (command == "open") {
handleOpenCommand(arguments);
} else if (command == "interact") {
handleInteractCommand(arguments);
} else if (command == "feed") {
handleFeedCommand(arguments);
} else if (command == "buy") {
handleBuyCommand(arguments);
} else if (command == "sell") {
handleSellCommand(arguments);
} else if (command == "attack") {
handleAttackCommand(arguments);
} else if (command == "quit") {
handleQuitCommand(arguments);
} else if (command == "check-info") {
handleCheckInfoCommand(arguments);
} else if (command == "quest-status") {
handleQuestStatusCommand(arguments);
} else {
std::cout << "I don't understand that command.\n";
}
}
}
// Method to handle the attack command
void ZOOrkEngine::handleAttackCommand(std::vector<std::string> arguments) {
if (arguments.empty()) { // Check if the player has specified a target
std::cout << "Attack whom?\n";
return;
}
auto targetName = arguments[0];
auto target = player->getCurrentRoom()->getCharacter(targetName); // Get the target character
if (target) { // If the target exists, attack it
player->attackEnemy(target);
} else {
std::cout << "There is no " << targetName << " here.\n\n";
}
}
// Method to handle the equipment command
void ZOOrkEngine::handleEquipmentCommand(std::vector<std::string> arguments) {
player->listEquipment();
}
// Method to handle the unattach command
void ZOOrkEngine::handleUnattachCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Unattach what?\n";
return;
}
player->unattachItem(arguments[0]);
}
// Method to handle the go command
void ZOOrkEngine::handleGoCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Go where?\n\n";
return;
}
std::string direction = arguments[0]; // Get the direction specified by the player
Room* currentRoom = player->getCurrentRoom(); // Get the current room
// Check if the player is in the magic forest and is trying to go in the wrong direction
if (currentRoom->getName() == "magic-forest" && direction != "east" && direction != "west") {
std::cout << "It is impossible to go " << direction << "!\n\n";
return;
}
// Check if the player is in the ancient tree and is trying to go up
if (currentRoom->getName() == "ancient-tree" && direction == "up") {
if (!ancientTreeTop) {
ancientTreeTop = std::make_shared<Room>("ancient-tree-top",
"You are on top of the ancient tree. You can see the whole forest from here.\n");
Passage::createBasicPassage(currentRoom, ancientTreeTop.get(), "up", true);
}
}
// Check if the player is in the ancient tree top and is trying to go down
auto passage = currentRoom->getPassage(direction);
player->setCurrentRoom(passage->getTo());
passage->enter(); // Enter the passage
}
// Method to handle the look command
void ZOOrkEngine::handleLookCommand(std::vector<std::string> arguments) {
Room* currentRoom = player->getCurrentRoom();
// Check if the player is in the dark cave and initialize the goblins
if (currentRoom->getName() == "dark-cave") {
currentRoom->initializeGoblins();
return;
}
// Check if the player is in the devastated village and initialize the villagers
if (!arguments.empty()) {
std::string itemName = arguments[0];
auto item = currentRoom->getItem(itemName);
if (item) { // If the item exists, display its description
std::cout << item->getDescription() << "\n\n";
return;
}
item = player->getItem(itemName);
if (item) { // If the item exists in the player's inventory, display its description
std::cout << item->getDescription() << "\n\n";
return;
}
std::cout << "There is no " << itemName << " here.\n\n";
return;
}
// Check if the player is in the start room and the door is locked
if (currentRoom->getName() == "south-of-house" && !plantMoved && !keyTaken) {
std::cout << "There is a pot of plant here.\n\n";
return;
} else if (currentRoom->getName() == "south-of-house" && plantMoved && !keyTaken) {
std::cout << "There is a key here.\n\n";
return;
}
// Check if the player is in the ancient tree top and the mushroom is not taken
if (currentRoom->getName() == "ancient-tree-top" && !mushroomTaken) {
std::cout << "There is a bunch of mushroom.\n\n";
mushroomFound = true;
return;
}
if (currentRoom->getName() == "ancient-tree-top" && mushroomTaken) {
std::cout << "You are on the tree\n\n";
mushroomFound = true;
return;
}
// Check if the player is in the magic forest and the deer is not found
if (currentRoom->getName() == "magic-forest" && !deerFound) {
std::cout << "Suddenly you hear a rustling sound near a bush nearby. It seems like something is there.\n\n";
deerFound = true;
return;
}
if (currentRoom->getName() == "magic-forest" && deerFound && !deerFeed) {
std::cout << "You see a wounded deer lying near the bush. It looks like it needs help.\n\n";
return;
}
// Check if the player is in the ancient temple and the merchant is not found
if (currentRoom->getName() == "ancient-temple" || currentRoom->getName() == "dark-cave" || currentRoom->getName() == "devastated-village" || currentRoom->getName() == "mystic-lake" || currentRoom->getName() == "tower-of-sorcery" || currentRoom->getName() == "dragon-lair") {
auto characters = currentRoom->getCharacters();
if (!characters.empty()) {
for (const auto& character : characters) {
std::cout << "- " << character->getName() << ": " << character->getDescription() << "\n\n";
}
}
return;
}
std::cout << currentRoom->getDescription() << "\n";
}
// Method to handle the take command
void ZOOrkEngine::handleTakeCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Take what?\n\n";
return;
}
std::string itemName = arguments[0];
Room* currentRoom = player->getCurrentRoom();
if (itemName == "plant" || itemName == "pot" || itemName == "a pot of plant") {
std::cout << "Invalid item: plant cannot be taken.\n\n";
return;
}
// Check if the player is in the ancient tree top and the mushroom is not taken
if (currentRoom->getName() == "ancient-tree-top" && itemName == "mushroom") {
std::cout << "You take the healing mushroom from the ancient tree.\n\n";
auto mushroom = std::make_shared<Mushroom>("mushroom", "A mushroom with healing properties.", 1, 3);
player->addItem(mushroom);
mushroomTaken = true;
return;
}
auto item = currentRoom->retrieveItem(itemName);
if (item) {
player->addItem(item);
std::cout << "You take the " << item->getName() << ".\n\n";
if (itemName == "key") { // Check if the player has taken the key
keyTaken = true;
}
} else {
std::cout << "There is no " << arguments[0] << " here.\n\n";
}
}
// Method to handle the drop command
void ZOOrkEngine::handleDropCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Drop what?\n\n";
return;
}
auto item = player->retrieveItem(arguments[0]);
if (item) {
player->getCurrentRoom()->addItem(item);
std::cout << "You drop the " << item->getName() << " (Quantity: " << item->getQuantity() << ").\n\n";
} else {
std::cout << "You don't have " << arguments[0] << ".\n\n";
}
}
// Method to handle the inventory command
void ZOOrkEngine::handleInventoryCommand(std::vector<std::string> arguments) {
player->listInventory();
}
// Method to handle the use command
void ZOOrkEngine::handleUseCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Use what?\n\n";
return;
}
auto itemName = arguments[0];
auto item = player->getItem(itemName);
if (!item) {
std::cout << "You don't have " << itemName << ".\n\n";
return;
}
if (itemName == "key") {
Room* currentRoom = player->getCurrentRoom();
if (currentRoom->getName() == "start-room" && currentRoom->isLocked()) { // Check if the player is in the start room and the door is locked
std::cout << "You use the key to unlock the door.\n\n";
currentRoom->setLocked(false); // Unlock the door
item->decreaseQuantity(); // Decrease the quantity of the key
if (item->getQuantity() <= 0) { // Check if the key has been used up
player->removeItem(itemName); // Remove the key from the player's inventory
}
} else {
std::cout << "You can't use the key here.\n\n";
}
} else {
player->useItem(itemName); // Use the item
}
}
// Method to handle the lift command
void ZOOrkEngine::handleLiftCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Lift what?\n\n";
return;
}
if (arguments[0] == "plant") {
Room* currentRoom = player->getCurrentRoom();
currentRoom->pickUpPlant();
plantMoved = currentRoom->isPlantMoved();
} else {
std::cout << "You can't lift that.\n\n";
}
}
// Method to handle the open command
void ZOOrkEngine::handleOpenCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Open what?\n\n";
return;
}
if (arguments[0] == "door") {
Room* currentRoom = player->getCurrentRoom();
if (currentRoom->getName() == "start-room" && !currentRoom->isLocked()) {
std::cout << "A blinding light engulfs you as you step through the door...\n";
player->setCurrentRoom(magicForest.get());
std::cout << "You wake up in a new world.\n\n";
} else {
std::cout << "The door is locked. You need to unlock it first.\n\n";
}
} else {
std::cout << "You can't open that.\n\n";
}
}
// Method to handle the interact command
void ZOOrkEngine::handleInteractCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Interact with whom?\n\n";
return;
}
player->interactWithCharacter(arguments[0]);
}
// Method to handle the feed command
void ZOOrkEngine::handleFeedCommand(std::vector<std::string> arguments) {
if (arguments.size() < 3 || arguments[1] != "to") {
std::cout << "Feed what to whom?\n\n";
return;
}
std::string itemName = arguments[0];
std::string characterName = arguments[2];
auto item = player->getItem(itemName);
if (!item) {
std::cout << "You don't have " << itemName << ".\n\n";
return;
}
auto character = player->getCurrentRoom()->getCharacter(characterName);
if (character && itemName == "mushroom") {
std::cout << "You feed the healing mushroom to the wounded deer.\n";
player->removeItem(itemName);
mushroomFound = false;
std::cout << "The deer recovers and thanks you.\nIt gives you a fairy flower as a token of gratitude.\n";
player->addExperience(50);
auto fairyFlower = std::make_shared<FairyFlower>("fairy-flower", "A beautiful flower given by the deer.", 1, 199);
player->addItem(fairyFlower);
player->getCurrentRoom()->removeCharacter(characterName);
deerFeed = true;
} else {
std::cout << "You can't feed that.\n\n";
}
}
// Method to handle the buy command
void ZOOrkEngine::handleBuyCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Buy what?\n\n";
return;
}
std::string itemName = arguments[0];
auto merchant = std::dynamic_pointer_cast<Merchant>(player->getCurrentRoom()->getCharacter("merchant"));
if (merchant) {
merchant->sellItem(itemName, player);
} else {
std::cout << "There is no merchant here.\n\n";
}
}
// Method to handle the check-info command
void ZOOrkEngine::handleCheckInfoCommand(std::vector<std::string> arguments) {
std::cout << "Gold: " << player->getGold() << " \n";
std::cout << "HP: " << player->getHealth() << "\n";
std::cout << "Attack: " << player->getAttack() << "\n";
std::cout << "Level: " << player->getLevel() << "\n";
std::cout << "EXP: " << player->getExperience() << " / " << player->getNextLevelExperience() << "\n\n";
}
// Method to handle the sell command
void ZOOrkEngine::handleSellCommand(std::vector<std::string> arguments) {
if (arguments.empty()) {
std::cout << "Sell what?\n\n";
return;
}
std::string itemName = arguments[0];
auto merchant = std::dynamic_pointer_cast<Merchant>(player->getCurrentRoom()->getCharacter("merchant"));
if (merchant) {
merchant->buyItemFromPlayer(itemName, player);
} else {
std::cout << "There is no merchant here.\n\n";
}
}
// Method to handle the quest-status command
void ZOOrkEngine::handleQuestStatusCommand(std::vector<std::string> arguments) {
player->checkQuestStatus();
}
// Method to handle the quit command
void ZOOrkEngine::handleQuitCommand(std::vector<std::string> arguments) {
std::string input;
std::cout << "Are you sure you want to QUIT?\n> ";
std::cin >> input;
std::string quitStr = makeLowercase(input);
if (quitStr == "y" || (quitStr == "yes")) {
gameOver = true;
}
}
// Method to tokenize a string
std::vector<std::string> ZOOrkEngine::tokenizeString(const std::string &input) {
std::vector<std::string> tokens;
std::stringstream ss(input);
std::string token;
while (std::getline(ss, token, ' ')) {
tokens.push_back(makeLowercase(token));
}
return tokens;
}
// Method to convert a string to lowercase
std::string ZOOrkEngine::makeLowercase(std::string input) {
std::string output = std::move(input);
std::transform(output.begin(), output.end(), output.begin(), ::tolower);
return output;
}