forked from kvanderlaag/MUDserver
/
Room.cpp
341 lines (287 loc) · 8.15 KB
/
Room.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
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
#include "Room.h"
/**
* Create a new room with an id and name
* Room inherits GameEntity
*/
Room::Room(int id, std::string name, GameWorld* world) :
GameEntity(id, name, world),
exits_(new EntityList()),
items_(new EntityList()),
players_(new EntityList()),
mobs_(new EntityList())
{
std::cout << "Created a room..." << std::endl;
}
/** Create a new room with an id, name, and description */
Room::Room(int id, std::string name, std::string description, GameWorld* world) :
GameEntity(id, name, description, world),
exits_(new EntityList()),
items_(new EntityList()),
players_(new EntityList()),
mobs_(new EntityList())
{
//std::cout << "Created a room..." << std::endl;
}
/** Remove a room */
Room::~Room( )
{
std::cout << "Destroying room " << GetName() << std::endl;
}
/**
* Each room will have a list of exits (denotes which rooms can be accessed from that room)
* This function will add an exit to the existing exit list of a room
*/
void Room::AddExit(GameEntity& roomexit)
{
exits_->AddEntity(roomexit);
}
/**
* Each room will have a list of items (denotes what items are available to be picked up by characters in the room)
* This function will add an item to the existing item list of a room
*/
void Room::AddItem(GameEntity& item)
{
items_->AddEntity(item);
}
void Room::AddMasterItem(int id) {
original_items_.push_back(id);
}
void Room::AddMasterMob(int id)
{
original_mobs_.push_back(id);
}
void Room::SpawnItem(int masterId) {
Item& copyFrom = (Item&) *(GetWorld().GetMasterItems().GetEntity(masterId));
Item* spawnItem = new Item(GetWorld().GetItems().GetNextId(), copyFrom);
GetWorld().GetItems().AddEntity(*spawnItem);
AddItem(*spawnItem);
//std::cout << "Spawning " << spawnItem->GetName() << " in " << GetName() << " (Master ID: " << spawnItem->GetMasterId() << ")" << std::endl;
std::ostringstream outString;
outString << "\n" << cGreen << spawnItem->GetName() << cDefault << " pops into existence.\n";
std::vector<GameEntity*> roomPlayers = players_->GetEntityVector();
for (GameEntity* p : roomPlayers) {
Message* msg = new Message(outString.str(), ((Player*) p)->GetConnectionId(), Message::outputMessage);
GetWorld().GetParent().PutMessage(msg);
}
}
void Room::SpawnMob(int masterId)
{
NPC& copyFrom = (NPC&)*(GetWorld().GetMasterMobs().GetEntity(masterId));
NPC* spawnMob = new NPC(GetWorld().GetMobs().GetNextId(), copyFrom);
spawnMob->SetRoomId(GetId());
GetWorld().GetMobs().AddEntity(*spawnMob);
AddMob(*spawnMob);
//std::cout << "Spawning " << spawnItem->GetName() << " in " << GetName() << " (Master ID: " << spawnItem->GetMasterId() << ")" << std::endl;
std::ostringstream outString;
outString << "\n" << cGreen << spawnMob->GetName() << cDefault << " enters the room.\n";
std::vector<GameEntity*> roomPlayers = players_->GetEntityVector();
for (GameEntity* p : roomPlayers) {
Message* msg = new Message(outString.str(), ((Player*)p)->GetConnectionId(), Message::outputMessage);
GetWorld().GetParent().PutMessage(msg);
}
}
/**
* Each room will have a list of players (denotes what players are currently in the room at the time)
* This function will add a player to the existing player list of a room
* Players get added to the player list when the enter a room
*/
void Room::AddPlayer(GameEntity& player)
{
players_->AddEntity(player);
}
/**
* Remove an exit from the existing exit list of a room
*/
void Room::RemoveExit(int id)
{
exits_->RemoveEntity(id);
}
/**
* Remove an item from the existing item list of a room
* Items are removed when a player picks up/takes an item
*/
void Room::RemoveItem(int id)
{
items_->RemoveEntity(id);
}
/**
* Remove a player from the existing player list of a room
* Players are removed when they leave a roome
*/
void Room::RemovePlayer(int id)
{
players_->RemoveEntity(id);
}
void Room::RemoveMob(int id)
{
mobs_->RemoveEntity(id);
}
/**
* Functions as a check made to see if a certain room exists in a room's exit list (using a room id)
* Return the GameEntity(id) if it is found in the exit list, else return null if it is not
*/
GameEntity* Room::GetExit(std::string exit)
{
if (exit == "e")
exit = "east";
else if (exit == "w")
exit = "west";
else if (exit == "n")
exit = "north";
else if (exit == "s")
exit = "south";
for (std::map<int, std::string>::iterator it = directions_.begin(); it != directions_.end(); it++) {
if (it->second == exit) {
return exits_->GetEntity(it->first);
}
}
return nullptr;
}
/**
* Gets an item GameEntity using an id if it's in the item list.
* Return the GameEntity(id) if it is found in the item list, else return null if it is not
*/
GameEntity* Room::GetItem(int id)
{
return items_->GetEntity(id);
}
/**
* Gets a player GameEntity using an id if it's in the player list.
* Return the GameEntity(id) if it is found in the player list, else return null if it is not
*/
GameEntity* Room::GetPlayer(int id)
{
return players_->GetEntity(id);
}
GameEntity * Room::GetMob(int id)
{
return mobs_->GetEntity(id);
}
GameEntity* Room::FindExit(std::string name) const {
GameEntity* exit = exits_->FindEntity(name);
if (exit) {
return exit;
}
return nullptr;
}
GameEntity* Room::FindItem(std::string name) const {
std::vector<GameEntity*> items = items_->GetEntityVector();
for (GameEntity* i : items) {
std::string lowername = i->GetName();
for (size_t j = 0; j < lowername.length(); ++j) {
lowername.at(j) = std::tolower(lowername.at(j));
}
if (lowername == name || ((Item*) i)->FindShortName(name)) {
return i;
}
}
return nullptr;
}
GameEntity* Room::FindPlayer(std::string name) const {
std::vector<GameEntity*> players = players_->GetEntityVector();
std::string lowername(name);
for (size_t i = 0; i < lowername.length(); ++i) {
lowername.at(i) = std::tolower(lowername.at(i));
}
for (GameEntity* p : players) {
std::string pName(p->GetName());
for (size_t i = 0; i < pName.length(); ++i) {
pName.at(i) = std::tolower(pName.at(i));
}
if (pName == lowername) {
return p;
}
}
return nullptr;
}
GameEntity * Room::FindMob(std::string name) const
{
std::vector<GameEntity*> mobs = mobs_->GetEntityVector();
std::string lowername(name);
for (size_t i = 0; i < lowername.length(); ++i) {
lowername.at(i) = std::tolower(lowername.at(i));
}
for (GameEntity* m : mobs) {
std::string pName(m->GetName());
for (size_t i = 0; i < pName.length(); ++i) {
pName.at(i) = std::tolower(pName.at(i));
}
if (pName == lowername) {
return m;
}
}
return nullptr;
}
/**
* Looks through all the lists ands finds the entity with the matching name
* TODO this is slooow, make a map with name -> entity
*/
GameEntity* Room::FindEntity(std::string name) const
{
GameEntity* esearch = exits_->FindEntity(name);
if (esearch)
{
return esearch;
}
GameEntity* isearch = items_->FindEntity(name);
if (isearch)
{
return isearch;
}
GameEntity* psearch = players_->FindEntity(name);
if (psearch)
{
return psearch;
}
return nullptr;
}
std::vector<GameEntity*> Room::GetPlayerVector()
{
return players_->GetEntityVector();
}
std::vector<GameEntity*> Room::GetItemVector()
{
return items_->GetEntityVector();
}
std::vector<GameEntity*> Room::GetMobVector()
{
return mobs_->GetEntityVector();
}
std::map<int, std::string> Room::GetExitVector()
{
return std::map<int, std::string>(directions_);
}
void Room::AddDirection(int id, std::string dir) {
directions_.insert(std::pair<int, std::string>(id, dir));
}
void Room::AddMob(GameEntity & entity)
{
mobs_->AddEntity(entity);
}
void Room::RespawnItems() {
std::vector<GameEntity*> currentItems = items_->GetEntityVector();
std::map<int, int> items_to_add;
for (int i : original_items_) {
items_to_add.insert(std::pair<int, int>(i, i));
}
for (GameEntity* i : currentItems) {
items_to_add.erase(((Item* )i)->GetMasterId());
}
for (std::pair<int, int> i : items_to_add) {
SpawnItem(i.second);
}
}
void Room::RespawnMobs()
{
std::vector<GameEntity*> currentMobs = mobs_->GetEntityVector();
std::map<int, int> mobs_to_add;
for (int i : original_mobs_) {
mobs_to_add.insert(std::pair<int, int>(i, i));
}
for (GameEntity* m : currentMobs) {
mobs_to_add.erase(((NPC*)m)->GetMasterId());
}
for (std::pair<int, int> m : mobs_to_add) {
SpawnMob(m.second);
}
}