-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
356 lines (293 loc) · 11.6 KB
/
game.cpp
File metadata and controls
356 lines (293 loc) · 11.6 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <map>
#include <sstream>
#include "bag.h"
#include "game.h"
#include "troll.h"
#include "wizard.h"
#include "terminal.h"
#include "bag.h"
#include "room.h"
#include "evil_lair.h"
#include "vampire_factory.h"
#include "light_saber.h"
#include "bad_format.h"
namespace da_game {
std::vector<Actor *> * Game::actors;
std::vector<Environment *> * Game::envs;
Player * Game::player;
Game::Game() {
actors = new std::vector<Actor *>;
envs = new std::vector<Environment *>;
bool running = true;
load();
printStory();
while (running && playerIsAlive() && !playerIsAlone()) {
Terminal::print(player->get_room()->description());
switch (terminal.run()) {
case 1:
running = false;
break;
case 2:
save();
running = false;
break;
case 3:
// clear_game();
std::cout << actors->size() << std::endl;
std::cout << envs->size() << std::endl;
load();
break;
default:
run();
break;
}
}
if (!playerIsAlive()) {
std::cout << "Sry, you died! :(" << std::endl;
}
if (playerIsAlone()) {
std::cout << "Congratz! you won!" << std::endl;
}
}
Game::~Game() {
actors->clear();
delete commands;
}
bool Game::playerIsAlive() {
return player != NULL;
}
/*
* Create the map and all objects
*/
void Game::initialize() {
// Environments
Environment * r1 = new Room();
Environment * r2 = new Room();
Environment * evil = new EvilLair();
new VampireFactory(evil, 2);
// evil->enter(*vamp_fac);
// exits
Exit * e1 = new Exit(r1, true, "e1", true);
Exit * e2 = new Exit(r2);
Exit * eevil = new Exit(evil, true, "evil", true);
r1->add_exit("recurse", e1);
r1->add_exit("west", e2);
r1->add_exit("east", eevil);
r2->add_exit("west", eevil);
evil->add_exit("east", e2);
evil->add_exit("west", e1);
// Create objects
Object * b1 = new Bag();
Object * b2 = new Bag();
Object * b3 = new Bag();
Object * b4 = new Food(47);
Object * k1 = new Key("e1");
Object * k2 = new Key("evil");
r1->drop(b1);
r1->drop(b2);
r1->drop(b3);
r1->drop(k2);
r2->drop(b4);
Object * light_saber = new LightSaber(1000,0.95);
evil->drop(light_saber);
evil->drop(k1);
// Create the player
player = new Player(r1);
r1->enter(*player);
commands = new GameCommands(player);
// Initialize actors
new Troll(r2, 1000, 88);
// r2->enter(*t);
Wizard * w = new Wizard(evil, true, 100, 100);
evil->enter(*w);
Bag bag;
}
void Game::run() {
for (unsigned int i = 0; i < actors->size(); ++i) {
actors->at(i)->run();
}
}
void Game::add_actor(Actor & actor) {
actors->push_back(&actor);
}
void Game::add_environment(Environment & env) {
envs->push_back(&env);
}
void Game::remove_actor(Actor & actor) {
if (&actor == player) {
player = 0;
}
std::vector<Actor *>::iterator it = actors->begin();
for (; it != actors->end(); ++it) {
if (&(**it) == &actor) {
actors->erase(it);
return;
}
}
}
bool Game::playerIsAlone() {
return (playerIsAlive() && (actors->size() == 1));
}
void Game::printStory() {
Terminal::print("When Anakin entered the Jedi-Tempel after beeing order to destroy the Jedi-order by Darth Sidious he suddenly saw a bright light surround him. When it dissapeared he had been transported to the most peculiar place, he heard sounds of vampires and smellt the smell of trolls, he must have been transported to dev random. As Anakin recently had been turned to the dark side he pushed his confusion from his mind and as the anger overtook him he realised he would not be satisfied until EVERYTHING was DEAD.");
}
void Game::save() {
std::fstream file;
file.open ("saveFile", std::ios_base::out|std::ios_base::trunc);
for (std::vector<Actor *>::iterator it = actors->begin(); it != actors->end(); ++it) {
(*it)->save(file);
}
for (std::vector<Environment *>::iterator it = envs->begin(); it != envs->end(); ++it) {
(*it)->save(file);
}
file.close();
}
void Game::load() {
std::ifstream file("saveFile");
if (!file.good()) {
std::cout << "File isint good" << std::endl;
initialize();
return;
}
std::string line;
std::vector<std::string> * lines = new std::vector<std::string>;
std::map<std::string, Environment *> created_envs; // nyckeln är IDt som en sträng
std::map<std::string, Object *> created_objects; // nyckeln är IDt som en sträng
std::map<std::string, Exit *> created_exits; // nyckeln är IDt som en sträng
while (std::getline(file, line)) {
lines->push_back(line);
std::string obj = line.substr(0, 3);
std::string id = line.substr(0, line.find_first_of(':'));
id = id.substr(3);
if (obj == "ACT" || obj == "EXI") {
// supress warnings for actors, which is handled later
}
else if (obj == "ENV") {
Environment * environment = Environment::load(line);
std::cout << "Loading new environment:" << id << std::endl;
created_envs[id] = environment;
// add_environment(*environment);
}
else if (obj == "OBJ") {
Object * object = Object::load(line);
if (object == NULL)
std::cerr << "Game::load(): NULL OBJECT RETURNED " << std::endl;
created_objects[id] = object;
// add_object(*object);
}
else {
std::cerr << "Invalid object type in save file: " << obj << std::endl;
throw BadFileFormatException();
}
}
for (std::vector<std::string>::iterator it = lines->begin(); it != lines->end(); ++it) {
line = *it;
std::string obj = line.substr(0, 3);
std::string id = line.substr(0, line.find_first_of(':'));
id = id.substr(3);
// Put all objects into their environments
if (obj == "ENV") {
std::string objects = line.substr(line.find_first_of(':')+1);
objects = objects.substr(0,objects.find_first_of(':'));
while (true) {
std::string object = objects.substr(0,objects.find_first_of(','));
object =object.substr(3);
if (created_objects.find(object) == created_objects.end())
std::cerr << "Game::load(): object NOT FOUND" << std::endl;
created_envs[id]->drop(created_objects[object]);
if (created_objects.erase(object) == 0)
std::cerr << "ERROR: Didnt remove anything" << std::endl;
if (objects.find_first_of(',') == std::string::npos)
break;
objects = objects.substr(objects.find_first_of(',')+1);
}
}
// Put all objects into their containers
else if (obj == "OBJ") {
// Only interesting of obj is a container
Container * bag = dynamic_cast<Container *>(created_objects[id]);
if (bag != 0) {
// we got a container
std::string objects = line.substr(line.find_first_of(':')+1);
objects = objects.substr(objects.find_first_of(':')+1);
objects = objects.substr(0,objects.find_first_of(':'));
// Lets go through the objects that should be inside our container
while (objects.length() > 0) {
std::string object = objects.substr(0,objects.find_first_of(','));
object =object.substr(3);
bag->add(*created_objects[object]);
created_objects.erase(object);
if (objects.find_first_of(',') == std::string::npos)
break;
objects = objects.substr(objects.find_first_of(',')+1);
}
}
}
}
// Loop through the actors again, they need initalized envs and objs
for (std::vector<std::string>::iterator it = lines->begin(); it != lines->end(); ++it) {
line = *it;
std::string obj = line.substr(0, 3);
std::string id = line.substr(0, line.find_first_of(':'));
id = id.substr(3);
if (obj == "ACT") {
Actor * actor = Actor::load(line, created_envs, created_objects);
if (actor != NULL) {
Player * player = dynamic_cast<Player *>(actor);
if (player != 0) {
commands = new GameCommands(player);
this->player = player;
}
}
}
else if (obj == "EXI") {
Exit * exit = Exit::load(line, created_envs);
created_exits[id] = exit;
}
}
for (std::vector<std::string>::iterator it = lines->begin(); it != lines->end(); ++it) {
line = *it;
std::string obj = line.substr(0, 3);
std::string id = line.substr(0, line.find_first_of(':'));
id = id.substr(3);
if (obj == "ENV") {
std::istringstream input(line);
std::vector<std::string> tokens;
std::string token;
while (std::getline(input, token, ':')) {
tokens.push_back(token);
}
std::map<std::string, std::string> properties;
std::istringstream pss(tokens[3]);
while (std::getline(pss, token, ',')) {
size_t eq_sign = token.find('=');
properties.insert(std::pair<std::string, std::string>(token.substr(0, eq_sign), token.substr(eq_sign+1)));
}
for (std::map<std::string, std::string>::iterator it = properties.begin(); it != properties.end(); ++it) {
created_envs[id]->add_exit(it->second, created_exits[it->first.substr(3)]);
}
}
}
delete lines;
file.close();
}
void Game::clear_game() {
std::cout << "Removing environments" << std::endl;
for (std::vector<Environment *>::iterator it = envs->begin(); it != envs->end(); ++it) {
delete *it;
}
std::cout << "Killing players" << std::endl;
for (std::vector<Actor *>::iterator it = actors->begin(); it != actors->end(); ++it) {
delete *it;
}
}
}
int main() {
std::srand(std::time(0));
da_game::Game game;
}