-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.cpp
191 lines (169 loc) · 4.96 KB
/
location.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
#include "io.h"
#include "location.h"
#include "command.h"
#include "exceptions.h"
Location::Location(){
this->hasBeenVisited = false;
this->locations = LocationConnections();
this->entities = std::set<Entity*>();
}
Entity* Location::matchTokenToEntity(std::string noun){
std::vector<Entity*> candidates = std::vector<Entity*>();
std::set<std::string> tokens = std::set<std::string>();
for (std::set<Entity*>::iterator it = this->entities.begin(); it != this->entities.end(); it++){
tokens = (*it)->getTokens();
if (tokens.count(noun)){
candidates.push_back(*it);
}
}
if (candidates.size() == 1){
return candidates[0];
} else if (candidates.size() > 0) {
throw CannotDoThat("More than one candidate for noun " + noun);
} else {
return 0;
}
}
void Location::processCommand(Command command, Actor* player){
std::string verb = command.verb;
std::string objectNoun = command.object.noun;
std::string objectOfPrepositionNoun = command.prepositionalPhrase.objectOfPreposition.noun;
Entity* object = 0;
Entity* objectOfPreposition = 0;
if (objectNoun != ""){
object = this->matchTokenToEntity(objectNoun);
}
if (objectOfPrepositionNoun != ""){
objectOfPreposition = this->matchTokenToEntity(objectOfPrepositionNoun);
}
if (verb == "go"){
this->handleTravel(player,
objectNoun);
return;
}
if (verb == "look"){
if (objectOfPrepositionNoun != "" && !objectOfPreposition){
throw CannotDoThat("There is no " + objectOfPrepositionNoun + " here to look at.");
}
this->handleLook(player,
objectOfPreposition);
return;
}
if (verb == "talk"){
if (objectOfPrepositionNoun != "" && !objectOfPreposition){
throw CannotDoThat("There is no " + objectOfPrepositionNoun + " here to talk to.");
}
objectOfPreposition->handleTalk();
return;
}
if (verb == "ask"){
if (objectNoun == ""){
std::set<Entity*> objects = this->findActors(player);
if (objects.size() == 0){
throw CannotDoThat("There is no one here to answer your questions.");
} else if (objects.size() > 1){
throw CannotDoThat("To whom are you directing your question?");
} else {
object = *(objects.begin());
}
}
if (objectNoun != "" && !object){
throw CannotDoThat("There is no " + objectNoun + " here to answer your questions.");
}
object->handleAsk(objectOfPrepositionNoun);
return;
}
throw CannotDoThat("I do not understand what you are trying to do");
}
std::set<Entity*> Location::findActors(Actor* player){
std::set<Entity*> results = std::set<Entity*>();
Entity* entity;
for (std::set<Entity*>::iterator it = this->entities.begin(); it != this->entities.end(); it++){
entity = *it;
if (entity->type() == "actor" && entity != player){
results.insert(entity);
}
}
return results;
}
void Location::handleLook(Actor* player,
Entity* target){
if (target){
target->handleLook();
} else {
outputString(this->getDescription());
}
}
void Location::handleTravel(Actor* player, std::string direction){
Location* newLocation = 0;
if (direction == "north"){
newLocation = this->locations.north;
} else if (direction == "south") {
newLocation = this->locations.south;
} else if (direction == "east"){
newLocation = this->locations.east;
} else if (direction == "west"){
newLocation = this->locations.west;
} else if (direction == "up"){
newLocation = this->locations.up;
} else if (direction == "down"){
newLocation = this->locations.down;
} else if (direction == ""){
throw CannotDoThat("Where are you trying to go?");
}
else {
throw CannotDoThat(direction + " is not a direction");
}
if (newLocation){
player->setLocation(newLocation);
player->announceLocation();
} else {
throw CannotDoThat("You cannot go that way");
}
}
std::string Location::getDescription(){
return this->getDescriptionText() + this->getSubDescription();
}
std::string Location::getSubDescription(){
std::string subDescription = std::string();
for (std::set<Entity*>::iterator it = this->entities.begin(); it != this->entities.end(); it++){
subDescription += (*it)->getAnnouncement() + " ";
}
return subDescription;
}
void Location::announce(){
outputString(this->getTitle());
if (!this->hasBeenVisited){
outputString(this->getDescriptionText());
}
this->hasBeenVisited = true;
outputString(this->getSubDescription());
}
void Location::addLocation(Location* connectedLocation, Direction direction){
switch (direction){
case north:
this->locations.north = connectedLocation;
break;
case south:
this->locations.south = connectedLocation;
break;
case east:
this->locations.east = connectedLocation;
break;
case west:
this->locations.west = connectedLocation;
break;
case up:
this->locations.up = connectedLocation;
break;
case down:
this->locations.down = connectedLocation;
break;
}
}
void Location::addEntity(Entity* entityToAdd){
this->entities.insert(entityToAdd);
}
void Location::removeEntity(Entity* entityToRemove){
this->entities.erase(entityToRemove);
}