-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocation.cpp
441 lines (381 loc) · 8.66 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#pragma once
#ifndef WORLDSIM_LOCATION_CPP
#define WORLDSIM_LOCATION_CPP
/* WorldSim: Location.cpp
#include "Location.cpp"
*/
#include "Location.hpp"
// Location class implementations
Location::Location()
{
isOutside = false;
capacity = 0;
nIngress = 0;
darkness = 0;
maxBranches = 0;
}
void Location::link(Location* location)
{
vLinkedLocations.push(location);
location->vLinkedLocations.push(this);
}
bool Location::putCharacter(Character* c)
{
if ( hasRoom() == false )
{
return false;
}
if ( c->location != nullptr )
{
c->location->removeCharacter(c);
}
vCharacter.push(c);
c->location = this;
return true;
}
bool Location::hasRoom()
{
if ( capacity == -1 || capacity > vCharacter.size() )
{
return true;
}
return false;
}
void Location::removeCharacter(Character* c)
{
vCharacter.remove(c);
c->location = 0;
}
std::string Location::getName()
{
return "unknown";
}
ResourceRequirement Location::getResourceRequirement()
{
return LocationManager::getResourceRequirement(type);
}
int Location::availableBranches()
{
return maxBranches - vLinkedLocations.size();
}
// Location_Settlement_Exterior class implementations
Location_Settlement_Exterior::Location_Settlement_Exterior()
{
type = LOCATION_OUTSIDE;
isOutside = true;
maxBranches = 100;
capacity = -1;
}
std::string Location_Settlement_Exterior::getName()
{
return "outside";
}
Location_Wilderness::Location_Wilderness()
{
type = LOCATION_WILDERNESS;
isOutside = true;
maxBranches = 4;
capacity = -1;
}
std::string Location_Wilderness::getName()
{
return "wilderness";
}
// Location_Settlement_Walls class implementations
Location_Settlement_Walls::Location_Settlement_Walls()
{
type = LOCATION_WALLS;
maxBranches = 2;
capacity = 12;
}
std::string Location_Settlement_Walls::getName()
{
return "walls";
}
// Location_Main_Hall class implementations
Location_Main_Hall::Location_Main_Hall()
{
type = LOCATION_MAIN_HALL;
maxBranches = 4;
capacity = 50;
}
std::string Location_Main_Hall::getName()
{
return "main hall";
}
// Location_Hall class implementations
Location_Hall::Location_Hall()
{
type = LOCATION_HALL;
maxBranches = 4;
capacity = 25;
}
std::string Location_Hall::getName()
{
return "hall";
}
// Location_Dwelling class implementations
Location_Dwelling::Location_Dwelling()
{
type = LOCATION_DWELLING;
maxBranches = 1;
}
// Location_Mine class implementations
Location_Mine::Location_Mine()
{
type = LOCATION_MINE;
maxBranches = 1;
size = 0;
nIron = 0;
nCopper = 0;
capacity=5;
}
std::string Location_Mine::getName()
{
return "mine";
}
Location_Farm::Location_Farm()
{
type = LOCATION_FARM;
maxBranches = 1;
capacity=3;
}
std::string Location_Farm::getName()
{
return "farm";
}
Location_Weaponsmith::Location_Weaponsmith()
{
type = LOCATION_WEAPONSMITH;
maxBranches = 1;
capacity=3;
vCanMake.push(ITEM_SWORD);
vCanMake.push(ITEM_LONGBOW);
vCanMake.push(ITEM_SPEAR);
}
std::string Location_Weaponsmith::getName()
{
return "weaponsmith";
}
// LocationManager class implementations
LocationManager::LocationManager()
{
// Constructor body (if needed)
biome=0;
}
void LocationManager::buildDwarvenFortress()
{
Location_Settlement_Exterior* exterior = new Location_Settlement_Exterior();
vLocation.push(exterior);
Location_Settlement_Walls* walls = new Location_Settlement_Walls();
vLocation.push(walls);
walls->link(exterior);
Location_Main_Hall* hall = new Location_Main_Hall();
vLocation.push(hall);
hall->link(walls);
Location_Wilderness* wild = new Location_Wilderness();
vLocation.push(wild);
wild->link(exterior);
}
void LocationManager::putCharacter(Character* c, enumLocation location)
{
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->type == location && vLocation(i)->putCharacter(c))
{
}
}
}
Vector <Location*>* LocationManager::getLocation(enumLocation location)
{
Vector <Location*>* vMatchLocation = new Vector <Location*>();
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->type == location)
{
vMatchLocation->push(vLocation(i));
}
}
return vMatchLocation;
}
int LocationManager::totalAvailableBranches()
{
int totalFreeBranches = 0;
for (int i = 0; i < vLocation.size(); ++i)
{
totalFreeBranches += vLocation(i)->availableBranches();
}
return totalFreeBranches;
}
Location* LocationManager::getBuildableBranch()
{
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->availableBranches() > 0)
{
return vLocation(i);
}
}
return nullptr;
}
int LocationManager::getMiningCapacity()
{
int totalMiningCapacity = 0;
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->type == LOCATION_MINE)
{
totalMiningCapacity += static_cast<Location_Mine*>(vLocation(i))->capacity;
}
}
return totalMiningCapacity;
}
int LocationManager::getFarmingCapacity()
{
int totalFarmingCapacity = 0;
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->type == LOCATION_FARM)
{
totalFarmingCapacity += static_cast<Location_Farm*>(vLocation(i))->capacity;
}
}
return totalFarmingCapacity;
}
Location* LocationManager::addLocation(enumLocation locationType)
{
Location* buildable = getBuildableBranch();
if (buildable == nullptr)
{
// No buildable branch available
return nullptr;
}
Location* newLocation = nullptr;
switch (locationType)
{
case LOCATION_OUTSIDE:
newLocation = new Location_Settlement_Exterior();
break;
case LOCATION_WALLS:
newLocation = new Location_Settlement_Walls();
break;
case LOCATION_MAIN_HALL:
newLocation = new Location_Main_Hall();
break;
case LOCATION_HALL:
newLocation = new Location_Hall();
break;
case LOCATION_DWELLING:
newLocation = new Location_Dwelling();
break;
case LOCATION_MINE:
newLocation = new Location_Mine();
break;
case LOCATION_FARM:
newLocation = new Location_Farm();
break;
case LOCATION_WEAPONSMITH:
newLocation = new Location_Weaponsmith();
break;
// Add cases for other location types
default:
// Handle unknown location type
return nullptr;
}
if (newLocation != nullptr)
{
vLocation.push(newLocation);
buildable->link(newLocation);
}
return newLocation;
}
bool LocationManager::has(enumLocation locationType)
{
for (int i = 0; i < vLocation.size(); ++i)
{
if (vLocation(i)->type == locationType)
{
return true; // Location of the specified type exists
}
}
return false; // No location of the specified type found
}
ResourceRequirement LocationManager::getResourceRequirement(enumLocation locationType)
{
switch (locationType)
{
case LOCATION_OUTSIDE:
return ResourceRequirement(/* Resources for LOCATION_OUTSIDE */);
case LOCATION_WALLS:
return ResourceRequirement(RESOURCE_STONE, 12); // Example requirement
case LOCATION_MAIN_HALL:
return ResourceRequirement(RESOURCE_STONE, 50); // Example requirement
case LOCATION_HALL:
return ResourceRequirement(RESOURCE_STONE, 25); // Example requirement
case LOCATION_DWELLING:
return ResourceRequirement(RESOURCE_STONE, 9); // Example requirement
case LOCATION_MINE:
return ResourceRequirement(/* Resources for LOCATION_MINE */);
case LOCATION_FARM:
return ResourceRequirement(/* Resources for LOCATION_FARM */);
// Add cases for other location types
default:
return ResourceRequirement( /* NONE */ ); // Return empty requirement for unknown types
}
}
Location* LocationManager::getLocationToMake(ItemType item)
{
for (Location* loc : vLocation)
{
// Check if the location can make the specified item
for (ItemType itemType : loc->vCanMake)
{
if (itemType == item)
{
return loc; // Return the location that can make this item
}
}
}
return nullptr; // Return nullptr if no location can make the item
}
std::string LocationManager::toString()
{
std::map<enumLocation, int> locationCounts;
std::stringstream ss;
// Count the occurrences of each location type
for (int i = 0; i < vLocation.size(); ++i)
{
Location* loc = vLocation(i);
if (loc != nullptr)
{
locationCounts[loc->type]++;
}
}
// Iterate through the map and build the string
for (const auto& pair : locationCounts)
{
std::string locationName = locationTypeToString(pair.first);
ss << locationName << " x" << pair.second << "\n";
}
return ss.str();
}
void LocationManager::printAll()
{
std::map<enumLocation, int> locationCounts;
// Count the occurrences of each location type
for (int i = 0; i < vLocation.size(); ++i)
{
Location* loc = vLocation(i);
if (loc != nullptr)
{
locationCounts[loc->type]++;
}
}
// Iterate through the map and print the results
for (const auto& pair : locationCounts)
{
std::string locationName = locationTypeToString(pair.first);
std::cout << locationName << " x" << pair.second << "\n";
}
}
#endif // WORLDSIM_LOCATION_CPP