-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEasyFindZonePath.cpp
612 lines (509 loc) · 14.6 KB
/
EasyFindZonePath.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include "EasyFind.h"
#include "EasyFindConfiguration.h"
#include "EasyFindWindow.h"
#include "EasyFindZoneConnections.h"
#include <fstream>
#include <filesystem>
static ZonePathRequest s_activeZonePathRequest;
// /travelto State
static bool s_travelToActive = false;
static EQZoneIndex s_currentZone = 0;
static bool s_findNextPath = false;
int FindTransferIndexByName(std::string_view name)
{
ZoneGuideManagerClient& zoneMgr = ZoneGuideManagerClient::Instance();
for (int i = 0; i < zoneMgr.transferTypes.GetLength(); ++i)
{
if (zoneMgr.transferTypes[i].description == name)
return i;
}
return -1;
}
// Generates a path to the zone by utilizing data from the ZoneGuideManagerClient.
std::vector<ZonePathNode> ZonePath_GeneratePath(EQZoneIndex fromZone, EQZoneIndex toZone,
std::string& outputMessage)
{
ZoneGuideManagerClient& zoneMgr = ZoneGuideManagerClient::Instance();
if (fromZone == toZone)
{
outputMessage = "Already at target zone";
return {};
}
EQZoneInfo* toZoneInfo = pWorldData->GetZone(toZone);
if (!toZoneInfo)
{
outputMessage = "Ending zone is not valid";
return {};
}
EQZoneInfo* fromZoneInfo = pWorldData->GetZone(fromZone);
if (!fromZoneInfo)
{
outputMessage = "Starting zone is not valid";
return {};
}
// Implements a breadth-first search of the zone connections
EQZoneIndex currentZoneId = pWorldData->GetZoneBaseId(fromZone);
std::deque<EQZoneIndex> queue;
struct ZonePathGenerationData {
int depth = -1;
int pathMinLevel = -1;
int prevZoneTransferTypeIndex = -1;
EQZoneIndex prevZone = 0;
// information about the origin of this link
const ParsedFindableLocation* location = nullptr;
const ZoneGuideConnection* connection = nullptr;
};
std::unordered_map<EQZoneIndex, ZonePathGenerationData> pathData;
queue.push_back(currentZoneId);
pathData[fromZone].depth = 0;
pathData[fromZone].pathMinLevel = 0;
if (const ZoneGuideZone* z = zoneMgr.GetZone(currentZoneId))
pathData[fromZone].pathMinLevel = z->minLevel;
// TODO: Handle bind zones (gate)
// TODO: Handle teleport spell zones (translocate, etc)
auto addTransfer = [&](EQZoneIndex destZoneId, int transferTypeIndex, int minLevel,
const ParsedFindableLocation* location, const ZoneGuideConnection* connection)
{
auto& data = pathData[destZoneId];
auto& prevData = pathData[currentZoneId];
data.location = location;
data.connection = connection;
if (data.depth == -1)
{
queue.push_back(destZoneId);
data.prevZoneTransferTypeIndex = transferTypeIndex;
data.prevZone = currentZoneId;
data.pathMinLevel = std::max(prevData.pathMinLevel, minLevel);
data.depth = prevData.depth + 1;
}
else if (data.prevZone && (data.depth == prevData.depth + 1)
&& pathData[data.prevZone].pathMinLevel > prevData.pathMinLevel)
{
// lower level preference?
data.prevZoneTransferTypeIndex = transferTypeIndex;
data.prevZone = currentZoneId;
data.pathMinLevel = std::max(prevData.pathMinLevel, minLevel);
}
};
int otherIndex = FindTransferIndexByName("Other");
int zoneLineIndex = FindTransferIndexByName("Zone Line");
int translocatorIndex = FindTransferIndexByName("Translocator");
// Explore the zone graph and cost everything out.
while (!queue.empty())
{
currentZoneId = queue.front();
queue.pop_front();
// Did we find a connection to the destination?
if (pathData[toZone].depth > -1 && pathData[toZone].depth < pathData[currentZoneId].depth)
{
break;
}
const EZZoneData& ezZoneData = g_zoneConnections->GetZoneData(currentZoneId);
if (ZoneGuideZone* currentZone = zoneMgr.GetZone(currentZoneId))
{
// Search the zone guide with modified parameters.
for (const ZoneGuideConnection& connection : currentZone->zoneConnections)
{
// Skip connection if it is disabled by the user.
if (connection.disabled)
continue;
// Make sure that progression server expansion is available.
if (connection.requiredExpansions != 0 && pEverQuestInfo->bProgressionServer)
{
if ((pEverQuestInfo->ProgressionOpenExpansions & connection.requiredExpansions) != connection.requiredExpansions)
continue;
}
// Make sure we didn't remove this connection
if (std::find(ezZoneData.removedConnections.begin(), ezZoneData.removedConnections.end(), connection.destZoneId) != ezZoneData.removedConnections.end())
continue;
// Make sure that the transfer types are supported
if (g_configuration->IsDisabledTransferType(connection.transferTypeIndex))
continue;
addTransfer(connection.destZoneId, connection.transferTypeIndex, currentZone->minLevel, nullptr, &connection);
continue;
}
}
// Search our own connections
for (const ParsedFindableLocation& location : ezZoneData.findableLocations)
{
if (!location.IsZoneConnection())
continue;
if (!location.CheckRequirements())
continue;
int transferTypeIndex = otherIndex;
if (location.type == LocationType::Location)
transferTypeIndex = zoneLineIndex;
else if (location.type == LocationType::Translocator)
transferTypeIndex = translocatorIndex;
if (location.zoneId != 0)
addTransfer(location.zoneId, transferTypeIndex, 0, &location, nullptr);
for (const auto& dest : location.translocatorDestinations)
{
if (dest.zoneId != 0)
addTransfer(dest.zoneId, translocatorIndex, 0, &location, nullptr);
}
}
}
// Work backwards from the destination and build the route.
EQZoneIndex zoneId = toZone;
int transferTypeIndex = -1;
const ParsedFindableLocation* location = nullptr;
const ZoneGuideConnection* connection = nullptr;
std::vector<ZonePathNode> reversedPath;
while (zoneId != 0)
{
reversedPath.emplace_back(zoneId, transferTypeIndex, location, connection);
transferTypeIndex = pathData[zoneId].prevZoneTransferTypeIndex;
zoneId = pathData[zoneId].prevZone;
location = pathData[zoneId].location;
connection = pathData[zoneId].connection;
}
if (reversedPath.size() <= 1)
{
outputMessage = "Could not find path to target zone.";
}
std::vector<ZonePathNode> newPath;
newPath.reserve(reversedPath.size());
// If we made it back to the start, then flip the list around and return it.
if (!reversedPath.empty() && reversedPath.back().zoneId == fromZone)
{
for (auto riter = reversedPath.rbegin(); riter != reversedPath.rend(); ++riter)
{
newPath.push_back(*riter);
}
}
return newPath;
}
void ZonePath_FollowActive()
{
s_activeZonePathRequest.clear();
for (const ZonePathData& pathData : ZoneGuideManagerClient::Instance().activePath)
s_activeZonePathRequest.zonePath.emplace_back(pathData);
s_travelToActive = true;
s_findNextPath = true;
}
void StopTravelTo(bool success)
{
ZonePath_SetActive({}, false);
s_travelToActive = false;
}
static bool ActivateNextPath()
{
// Wait to update until we have all of our locations updated.
CFindLocationWndOverride* pFindLocWnd = pFindLocationWnd.get_as<CFindLocationWndOverride>();
if (pFindLocWnd && pFindLocWnd->IsCustomLocationsAdded())
{
s_findNextPath = false;
if (!s_activeZonePathRequest.zonePath.empty())
{
EQZoneIndex nextZoneId = 0;
int transferTypeIndex = -1;
// Find the next zone to travel to!
for (size_t i = 0; i < s_activeZonePathRequest.zonePath.size() - 1; ++i)
{
if (s_activeZonePathRequest.zonePath[i].zoneId == s_currentZone)
{
nextZoneId = s_activeZonePathRequest.zonePath[i + 1].zoneId;
transferTypeIndex = s_activeZonePathRequest.zonePath[i].transferTypeIndex;
break;
}
}
if (nextZoneId != 0)
{
if (pFindLocWnd->FindZoneConnectionByZoneIndex(nextZoneId, false))
return true;
StopTravelTo(false);
}
else
{
SPDLOG_ERROR("Unable to find the next zone to travel to!");
StopTravelTo(false);
}
}
}
return false;
}
void ZonePath_SetActive(const ZonePathRequest& zonePathData, bool travel)
{
ZonePathArray pathArray((int)zonePathData.zonePath.size());
for (const ZonePathNode& pathData : zonePathData.zonePath)
{
pathArray.Add(ZonePathData(pathData.zoneId, pathData.transferTypeIndex));
}
bool hasItems = !pathArray.empty();
ZoneGuideManagerClient::Instance().activePath = std::move(pathArray);
s_travelToActive = travel;
s_findNextPath = travel;
s_activeZonePathRequest = zonePathData;
if (ActivateNextPath())
{
if (pZonePathWnd)
{
pZonePathWnd->zonePathDirty = true;
pZonePathWnd->Show(hasItems);
}
}
}
static void UpdateForZoneChange()
{
// Update current zone
if (!pFindLocationWnd || !pZonePathWnd)
return;
// Wait to update until we have all of our locations updated.
CFindLocationWndOverride* pFindLocWnd = pFindLocationWnd.get_as<CFindLocationWndOverride>();
if (!pFindLocWnd->IsCustomLocationsAdded())
return;
s_currentZone = pWorldData->GetZoneBaseId(pLocalPlayer->GetZoneID());
// If zone path is active then update it.
if (!s_activeZonePathRequest.zonePath.empty())
{
EQZoneIndex destZone = s_activeZonePathRequest.zonePath.back().zoneId;
if (destZone == s_currentZone)
{
std::string targetQuery = std::move(s_activeZonePathRequest.targetQuery);
SPDLOG_INFO("Arrived at our destination zone: \ay{}\ax!", GetFullZone(destZone));
StopTravelTo(true);
if (!targetQuery.empty())
{
FindWindow_FindLocation(targetQuery, false);
}
}
else
{
// Update the path if we took a wrong turn
bool found = false;
for (const ZonePathNode& pathData : s_activeZonePathRequest.zonePath)
{
if (pathData.zoneId == s_currentZone)
{
found = true;
break;
}
}
if (!found)
{
std::string message;
auto newPath = ZonePath_GeneratePath(s_currentZone, destZone, message);
if (newPath.empty())
{
SPDLOG_WARN("Path generation failed: {}", message);
}
auto newRequest = s_activeZonePathRequest;
newRequest.zonePath = newPath;
ZonePath_SetActive(newRequest, s_travelToActive);
}
}
if (s_travelToActive)
{
s_findNextPath = true;
}
}
}
void ZonePath_OnPulse()
{
if (s_currentZone != pLocalPC->zoneId)
{
UpdateForZoneChange();
}
if (s_findNextPath)
{
ActivateNextPath();
}
}
void ZonePath_NavCanceled(bool message)
{
if (s_travelToActive)
{
StopTravelTo(false);
if (message)
{
SPDLOG_INFO("Stopping /travelto because nav was stopped.");
}
}
}
void ZonePath_Stop()
{
ZoneGuideManagerClient& zoneGuide = ZoneGuideManagerClient::Instance();
bool isActive = s_travelToActive || !s_activeZonePathRequest.zonePath.empty() || !zoneGuide.activePath.IsEmpty();
if (isActive)
{
StopTravelTo(false);
SPDLOG_INFO("/travelto stopped");
}
else
{
SPDLOG_WARN("No /travelto is active.");
}
Navigation_Stop();
}
namespace YAML
{
template <>
struct convert<CXStr>
{
static Node encode(const CXStr& str)
{
YAML::Node node(NodeType::Scalar);
node = std::string{ str };
return node;
}
static bool decode(const Node& node, CXStr& str)
{
if (!node.IsScalar())
return false;
try {
str = node.as<std::string>();
}
catch (const YAML::BadConversion&) {
return false;
}
return true;
}
};
template <>
struct convert<ZoneGuideConnection>
{
static Node encode(const ZoneGuideConnection& zone)
{
YAML::Node node;
node["DestZoneId"] = (int)zone.destZoneId;
node["DestZone"] = GetShortZone((int)zone.destZoneId);
node["TransferType"] = ZoneGuideManagerClient::Instance().GetZoneTransferTypeNameByIndex(zone.transferTypeIndex);
if (zone.requiredExpansions != 0)
node["RequiredExpansion"] = GetHighestExpansionOwnedName((EQExpansionOwned)zone.requiredExpansions);
return node;
}
static bool decode(const Node& node, ZoneGuideConnection& zone)
{
return false;
}
};
template <>
struct convert<ZoneGuideContinent>
{
static Node encode(const ZoneGuideContinent& zone)
{
YAML::Node node;
node["Id"] = zone.id;
node["Name"] = zone.name;
return node;
}
static bool decode(const Node& node, ZoneGuideContinent& zone)
{
return false;
}
};
template <>
struct convert<ZoneGuideZoneType>
{
static Node encode(const ZoneGuideZoneType& zone)
{
YAML::Node node;
node["Id"] = zone.id;
node["DisplaySequence"] = zone.displaySequence;
node["Name"] = zone.name;
return node;
}
static bool decode(const Node& node, ZoneGuideZoneType& zone)
{
return false;
}
};
template <>
struct convert<ZoneGuideTransferType>
{
static Node encode(const ZoneGuideTransferType& zone)
{
YAML::Node node;
node["Id"] = zone.id;
node["Description"] = zone.description;
return node;
}
static bool decode(const Node& node, ZoneGuideTransferType& zone)
{
return false;
}
};
template <typename T>
struct convert<ArrayClass<T>> {
static Node encode(const ArrayClass<T>& arr) {
Node node(NodeType::Sequence);
for (const T& value : arr)
node.push_back(value);
return node;
}
static bool decode(const Node& node, ArrayClass<T>& arr) {
return false;
}
};
template <>
struct convert<ZoneGuideZone>
{
static Node encode(const ZoneGuideZone& zone)
{
YAML::Node node;
node["ZoneId"] = zone.zoneId;
EQZoneInfo* pZoneInfo = pWorldData->GetZone(zone.zoneId);
if (pZoneInfo)
{
node["Name"] = pZoneInfo->LongName;
node["ShortName"] = pZoneInfo->ShortName;
}
else
{
node["Name"] = "Unknown";
node["ShortName"] = "Unknown";
}
node["Continent"] = ZoneGuideManagerClient::Instance().GetContinentNameByIndex(zone.continentIndex);
node["MinLevel"] = zone.minLevel;
node["MaxLevel"] = zone.maxLevel;
std::vector<std::string> types;
for (int i = 0; i < zone.types.GetNumBits(); ++i)
{
if (zone.types.IsBitSet(i))
{
types.push_back(std::string(ZoneGuideManagerClient::Instance().GetZoneTypeNameByIndex(i)));
}
}
node["Types"] = types;
node["Connections"] = zone.zoneConnections;
return node;
}
static bool decode(const Node& node, ZoneGuideZone& zone)
{
return false;
}
};
}
void ZonePath_DumpConnections()
{
// Dump all the connection data from the ZoneGuideManager
ZoneGuideManagerClient& mgr = ZoneGuideManagerClient::Instance();
try
{
SPDLOG_INFO("Dumping zone connections from ZoneGuideManager...");
std::string outputFile = (std::filesystem::path(gPathResources) / "ZoneGuide.yaml").string();
YAML::Node node;
node["Continents"] = mgr.continents;
node["ZoneTypes"] = mgr.zoneTypes;
node["TransferTypes"] = mgr.transferTypes;
std::vector<ZoneGuideZone> zones;
for (const ZoneGuideZone& zone : mgr.zones)
{
if (zone.zoneId != 0)
zones.push_back(zone);
}
node["Zones"] = zones;
YAML::Emitter out;
out.SetIndent(4);
out.SetFloatPrecision(3);
out.SetDoublePrecision(3);
out << node;
std::fstream file(outputFile, std::ios::out);
file << out.c_str();
}
catch (const std::exception& exc)
{
WriteChatf("\arError: %s", exc.what());
}
}