-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexit.cpp
More file actions
213 lines (174 loc) · 5.67 KB
/
exit.cpp
File metadata and controls
213 lines (174 loc) · 5.67 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
#include "exit.h"
#include "environment.h"
#include <sstream>
#include <iostream>
namespace da_game {
int Exit::instances;
/**
* Creates a new exit with some default parameters, but without
* any outfall. This is probably useless unless an outfall is
* later set.
*/
Exit::Exit() : id(instances++), description(""), has_lock(false), key_code(""), locked(false) {
}
/**
* Constructs a new exit with the given outfall and description.
*
* Notice: All parameters except outfall are given default values
* if not specified.
*
* @param outfall Where this exit leads to
* @param has_lock Whether or not this exit has a lock
* @param key_code The key code for this exit
* @param locked The locked state of this exit
* @param description A short description of this exit
*/
Exit::Exit(Environment * outfall, bool has_lock, std::string key_code, bool locked, std::string description)
: id(instances++), outfall(outfall), description(description), has_lock(has_lock), key_code(key_code), locked(locked) {
}
/**
* Returns the outfall of this exit, that is, where it leads to.
*
* @return The outfall
*/
Environment * Exit::get_outfall() const {
return outfall;
}
/**
* Returns whether or not this exit is locked.
*
* @return true if locked, false otherwise
*/
bool Exit::is_locked() {
return this->locked;
}
/**
* Sets what environment this exit leads to.
*
* @param outfall The new environment this exit should lead to
*/
void Exit::set_outfall(Environment * outfall) {
this->outfall = outfall;
}
/**
* Sets the description of this exit to the one specified.
*
* @param description The new description
*/
void Exit::set_description(std::string description) {
this->description = description;
}
/**
* Sets this exit's key code that is required in order to unlock
* it.
*
* @param key_code The new key code
*/
void Exit::set_key_code(std::string key_code) {
this->key_code = key_code;
}
/**
* Sets the locked state of this exit.
*
* @param locked Whether or not this exit is locked
*/
void Exit::set_locked(bool locked) {
this->locked = locked;
}
/**
* Locks this exit iff it has a lock and the key code from the
* specified key agrees to the one of this exit.
*
* @param key The key to lock with
* @return The new locked state of this exit
*/
bool Exit::lock(Key * key) {
if (!this->has_lock) {
return this->locked;
}
if (key->get_key_code() == this->key_code) {
this->locked = true;
std::cerr << "Locked exit" << std::endl;
}
return this->locked;
}
/**
* Unlocks this exit iff this exit has a lock and the key code
* of the specified key equals the one of this exit.
*
* @param key The key to unlock with
* @return The new locked state of this exit
*/
bool Exit::unlock(Key * key) {
if (!this->has_lock) {
return this->locked;
}
if (key->get_key_code() == this->key_code) {
this->locked = false;
std::cerr << "Unlocked exit" << std::endl;
}
return this->locked;
}
/**
* Toggles the status of this exit's lock; if it is locked it gets
* unlocked and vice versa. However, this does only work if the
* correct key is provided.
*
* @param key The key to lock/unlock with
* @return The new locked status of this exit
*/
bool Exit::toggle_lock(Key * key) {
if (is_locked()) {
return unlock(key);
} else {
return lock(key);
}
}
void Exit::save(std::fstream & save, std::string env_description) {
save << "EXI" << id;
save << ":ENV" << outfall->id;
save << ":has_lock=" << has_lock;
save << ",key_code=" << key_code;
save << ",locked=";
if (locked)
save << "true";
else
save << "false";
save << ",description=" << description;
save << ",env_desc=" << env_description;
save << std::endl;
}
Exit * Exit::load(std::string line, const std::map<std::string, Environment *> & envs) {
line = line.substr(line.find_first_of(':')+1);
std::string tmp;
tmp = line.substr(0,line.find_first_of(':'));
tmp = tmp.substr(3);
std::string envID = tmp;
// std::istringstream iss(tmp);
// int envID;
// iss >> envID;
line = line.substr(line.find_first_of(':')+1);
tmp = line.substr(9);
tmp = tmp.substr(0,1);
bool has_lock = false;
if (tmp == "1")
has_lock = true;
line = line.substr(line.find_first_of(',')+1);
tmp = line.substr(9);
tmp = tmp.substr(0,tmp.find_first_of(','));
std::string key_code = tmp;
line = line.substr(line.find_first_of(',')+1);
tmp = line.substr(7);
tmp = tmp.substr(0,tmp.find_first_of(','));
bool locked = false;
if (tmp == "true")
locked = true;
line = line.substr(line.find_first_of(',')+1);
std::string description = line.substr(12);
description = description.substr(0,description.find_first_of(','));
line = line.substr(line.find_first_of(',')+1);
std::string env_desc = line.substr(9);
Exit * exit = new Exit(envs.find(envID)->second, has_lock, key_code, locked, description);
return exit;
}
}