-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.cpp
212 lines (179 loc) · 5.77 KB
/
environment.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
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
#include "environment.h"
#include <iostream>
#include <sstream>
#include "game.h"
namespace da_game {
int Environment::instances;
Environment::Environment() :id (instances) {
instances++;
objects = new std::vector<Object *>;
actors = new std::vector<Actor *>;
Game::add_environment(*this);
}
Environment::~Environment() {
// when an environment is destroyed everything in it need to be deleted
actors->clear();
delete actors;
objects->clear();
delete objects;
}
std::string Environment::description() const {
std::stringstream s;
s << "Objects:" << std::endl;
for (size_t i = 0; i < objects->size(); ++i) {
s << objects->at(i)->id << " " << objects->at(i)->type() << std::endl;
}
s << "Actors:" << std::endl;
for (size_t i = 0; i < actors->size(); ++i) {
s << actors->at(i)->id << " " << actors->at(i)->get_type() << std::endl;
}
s << "Exits:" << std::endl;
std::vector<std::string> exit_names = get_exit_names();
for (size_t i = 0; i < exit_names.size(); ++i) {
s << exit_names[i] << std::endl;
}
return s.str();
}
/**
* Adds the specified exit with the given name (preferably a point
* of the compass) to this environment.
*
* @return True if the exit was added or false if an exit with the
* same value already existed
*/
bool Environment::add_exit(std::string name, Exit * exit) {
std::pair<std::string, Exit *> pair(name, exit);
std::pair<std::map<std::string, Exit *>::iterator, bool> result = exits.insert(pair);
return result.second;
}
/**
* Returns the exit with the given name, if there is a such.
*
* @param name The name of the exit
* @return The exit with the given name
*/
Exit * Environment::get_exit(std::string name) const {
std::map<std::string, Exit *>::const_iterator it = exits.find(name);
if (it != exits.end()) {
return it->second;
}
else {
std::cout << "No such exit" << std::endl;
return 0;
// Throw exception or something, needs a return
}
}
/**
* Returns a list of all available exits in this environment.
*
* @return List of exits
*/
std::vector<std::string> Environment::get_exit_names() const {
std::map<std::string, Exit *>::const_iterator it;
std::vector<std::string> return_exits;
for (it = exits.begin(); it != exits.end(); ++it) {
return_exits.push_back(it->first);
}
return return_exits;
}
/**
* Lets the specified actor enter this environment.
*
* @param actor The actor to enter
*/
void Environment::enter(Actor & actor) {
actors->push_back(&actor);
}
/**
* Lets an actor leave this environment.
*
* @param actor The actor to leave
*/
void Environment::leave(Actor & actor) {
std::vector<Actor *>::iterator it = actors->begin();
for (; it != actors->end(); ++it) {
if (&(**it) == &actor) {
actors->erase(it);
return;
}
}
}
/**
* Someone picks up an object that is owned by this environment.
* That means this environment must hand over the object in
* question.
*
* @param object The object that is being picked up
* @return True if the object really was held by this environment,
* otherwise false.
*/
bool Environment::pick_up(Object * object) {
std::vector<Object *>::iterator it = objects->begin();
for (; it != objects->end(); ++it) {
if (**it == *object) {
objects->erase(it);
return true;
}
}
return false;
}
/**
* Someone drops an object in this environment. That means this
* environment must take care of it.
*
* @param object The object that is being dropped
*/
void Environment::drop(Object * object) {
if (object != 0)
objects->push_back(object);
else
throw;
}
void Environment::save(std::fstream & save) {
std::cout << "Saving environment " << id << std::endl;
save << "ENV" << id << ":";
bool first = true;
first = true;
for (size_t i = 0; i < objects->size(); ++i) {
if (first)
first = false;
else
save << ",";
save << "OBJ" << objects->at(i)->id;
}
save << ":";
first = true;
for (size_t i = 0; i < actors->size(); ++i) {
if (first)
first = false;
else
save << ",";
save << "ACT" << actors->at(i)->id;
}
save << ":";
first = true;
for (std::map<std::string, Exit *>::iterator it = exits.begin(); it != exits.end(); ++it) {
if (first)
first=false;
else
save << ",";
save << "EXI" << it->second->id << "=" << it->first;
}
save << std::endl;
for (size_t i = 0; i < objects->size(); ++i) {
objects->at(i)->save(save );
}
for (std::map<std::string, Exit *>::iterator it = exits.begin(); it != exits.end(); ++it) {
it->second->save(save, it->first);
}
}
std::vector<Object *> Environment::get_objects() {
return *objects;
}
std::vector<Actor *> Environment::get_actors() {
return *actors;
}
Environment * Environment::load(std::string) {
return new Environment;
}
}