Repository navigation
Expand file tree
/
Copy pathGuideEngine.lua
More file actions
2862 lines (2749 loc) · 110 KB
/
Copy pathGuideEngine.lua
File metadata and controls
2862 lines (2749 loc) · 110 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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local _, ns = ...
local Engine = { state = nil, currentGuide = nil, currentGoal = nil, status = nil }
ns.Engine = Engine
local VALID_KINDS = {
accept = true, objective = true, turnin = true, gossip = true, travel = true, note = true,
confirm = true,
}
local STARTER_GUIDE_IDS = {
["leveling-era-durotar"] = true,
["leveling-era-mulgore"] = true,
["leveling-era-tirisfal-glades"] = true,
["leveling-era-dun-morogh"] = true,
["leveling-era-elwynn-forest"] = true,
["leveling-era-teldrassil"] = true,
}
local function FocusedPickupGuide(guide)
local category = type(guide) == "table" and guide.category
return category == "Dungeon Quest Guides" or category == "Raid Quests"
end
-- Same camp, not the same zone. Deviate Hides and Deviate Eradication share a
-- pin; the Crossroads and Ratchet do not.
local PICKUP_STOP_RADIUS = 0.02
local function RouteDestination(goal)
local route = type(goal) == "table" and goal.route
if type(route) ~= "table" then return nil end
return route[#route]
end
local function SamePickupStop(first, second)
if type(first) ~= "table" or type(second) ~= "table" then return false end
if first.mapID ~= second.mapID then return false end
if type(first.x) ~= "number" or type(first.y) ~= "number"
or type(second.x) ~= "number" or type(second.y) ~= "number" then
return false
end
local dx = first.x - second.x
local dy = first.y - second.y
return (dx * dx) + (dy * dy) <= (PICKUP_STOP_RADIUS * PICKUP_STOP_RADIUS)
end
local function PickupStopKind(goal)
return goal.kind == "accept" or goal.kind == "turnin" or goal.kind == "gossip"
end
local SHORT_TIMER_SECONDS = 30 * 60
local trackedQuestIDs
local trackedAchievementIDs
ns.questPrerequisites = ns.questPrerequisites or {}
function ns:RegisterQuestPrerequisite(spec)
if type(spec) ~= "table" or type(spec.quest) ~= "number"
or (spec.mode ~= "all" and spec.mode ~= "any")
or type(spec.quests) ~= "table" or #spec.quests == 0 then
error("Forever GuideMate: invalid quest prerequisite", 2)
end
local seen = {}
for _, questID in ipairs(spec.quests) do
if type(questID) ~= "number" or questID == spec.quest or seen[questID] then
error("Forever GuideMate: invalid quest prerequisite for " .. spec.quest, 2)
end
seen[questID] = true
end
self.questPrerequisites[spec.quest] = self.questPrerequisites[spec.quest] or {}
self.questPrerequisites[spec.quest][#self.questPrerequisites[spec.quest] + 1] = spec
end
local function Contains(values, expected)
if type(values) ~= "table" then
return values == expected
end
for _, value in ipairs(values) do
if value == expected then
return true
end
end
return false
end
local function HasOutdoorDestination(goal)
local destination = RouteDestination(goal)
return type(destination) == "table" and type(destination.mapID) == "number"
end
-- True when this step is work inside the current instance. Outdoor capital
-- turn-ins that dependOn dungeon objectives still have world pins; those must
-- not count as in-instance or they preempt Maur/bosses mid-run (RFC/BFD).
local function GoalMatchesInstance(goal, state, guide)
if type(goal) ~= "table" or type(state) ~= "table" or type(state.instanceID) ~= "number" then
return false
end
local function Walk(condition)
if type(condition) ~= "table" then return false end
if condition.instance and Contains(condition.instance, state.instanceID) then
return true
end
for _, key in ipairs({ "all", "any" }) do
if type(condition[key]) == "table" then
for _, child in ipairs(condition[key]) do
if Walk(child) then return true end
end
end
end
return false
end
local seen = {}
local function Matches(candidate)
if type(candidate) ~= "table" or type(candidate.id) ~= "string" or seen[candidate.id] then
return false
end
seen[candidate.id] = true
if HasOutdoorDestination(candidate) then
return false
end
-- Authored enter steps put instance on complete, not conditions.
if Walk(candidate.conditions) or Walk(candidate.complete) then
return true
end
if type(guide) ~= "table" then return false end
for _, dependencyID in ipairs(candidate.dependsOn or {}) do
local dependency = Engine:GetGoal(guide, dependencyID)
if Matches(dependency) then return true end
end
return false
end
return Matches(goal)
end
local function Unknown(reason)
return nil, reason or "Eligibility could not be verified."
end
-- A partial catalog read must not treat every unread quest as failed.
local function QuestChecked(state, questID)
if type(state) ~= "table" then
return false
end
if type(state.watchedQuests) == "table" then
return state.watchedQuests[questID] == true
end
return state.questCompletionKnown == true
end
local function ObjectiveFinished(objective)
if type(objective) ~= "table" then
return nil
end
-- A counted kill is done only when the count is met. Forever can set
-- finished while the count is still short, and a finished number can be
-- the progress so far rather than a boolean. Dialogue objectives have no
-- count, so a finished flag of true or a number above 0 still means done.
if type(objective.numRequired) == "number" and objective.numRequired > 0
and type(objective.numFulfilled) == "number" then
return objective.numFulfilled >= objective.numRequired
end
if objective.finished == true or (type(objective.finished) == "number" and objective.finished > 0) then
return true
end
if objective.finished == false then
return false
end
return nil
end
local function FindObjective(objectives, spec)
if type(objectives) ~= "table" then
return nil
end
if type(spec.text) == "string" and spec.text ~= "" then
local needle = string.lower(spec.text)
for _, objective in ipairs(objectives) do
if type(objective) == "table" and type(objective.text) == "string"
and string.find(string.lower(objective.text), needle, 1, true) then
return objective
end
end
end
if type(spec.index) == "number" then
return objectives[spec.index]
end
end
function ns.EvaluateCondition(condition, state)
if condition == nil then
return true
end
if type(condition) ~= "table" then
return false, "Invalid condition."
end
-- spine import leaves empty `{}` children as no-op flags.
if next(condition) == nil then
return true
end
if condition.all then
local unknownReason
for _, child in ipairs(condition.all) do
local result, reason = ns.EvaluateCondition(child, state)
if result == false then
return false, reason
elseif result == nil then
unknownReason = unknownReason or reason
end
end
if unknownReason then
return Unknown(unknownReason)
end
return true
end
if condition.any then
local unknownReason
local falseReason
for _, child in ipairs(condition.any) do
local result, reason = ns.EvaluateCondition(child, state)
if result == true then
return true
elseif result == nil then
unknownReason = unknownReason or reason
else
falseReason = falseReason or reason
end
end
if unknownReason then
return Unknown(unknownReason)
end
return false, falseReason or "No alternative condition matched."
end
if condition["not"] then
local result, reason = ns.EvaluateCondition(condition["not"], state)
if result == nil then
return Unknown(reason)
end
return not result, result and "Excluded by guide condition." or nil
end
if condition.faction then
if not state.faction then
return Unknown("Faction is unavailable.")
end
local matches = Contains(condition.faction, state.faction)
return matches, matches and nil or "This step is for " .. tostring(condition.faction) .. "."
end
if condition.race then
if not state.raceID then
return Unknown("Race is unavailable.")
end
local matches = Contains(condition.race, state.raceID)
return matches, matches and nil or "Race requirement not met."
end
if condition.class then
if not state.classID then
return Unknown("Class is unavailable.")
end
local matches = Contains(condition.class, state.classID)
return matches, matches and nil or "Class requirement not met."
end
if condition.level then
if type(state.level) ~= "number" then
return Unknown("Level is unavailable.")
end
local matches = (not condition.level.min or state.level >= condition.level.min)
and (not condition.level.max or state.level <= condition.level.max)
return matches, matches and nil or "Level requirement not met."
end
if condition.profession then
if not state.professionsKnown then
return Unknown("Profession information is unavailable.")
end
local rank = state.professions[condition.profession.skillLineID]
local matches = rank ~= nil and rank >= (condition.profession.minRank or 1)
return matches, matches and nil or "Profession requirement not met."
end
if condition.map then
if not state.mapID then
return Unknown("Current map is unavailable.")
end
local matches = Contains(condition.map, state.mapID)
if not matches and ns.Navigation then
if type(condition.map) ~= "table" then
matches = ns.Navigation:OnMap(state.mapID, condition.map)
else
for _, value in ipairs(condition.map) do
if ns.Navigation:OnMap(state.mapID, value) then
matches = true
break
end
end
end
end
return matches, matches and nil or "Travel to the required map."
end
if condition.instance then
if not state.instanceID then
return Unknown("Current instance is unavailable.")
end
local matches = Contains(condition.instance, state.instanceID)
return matches, matches and nil or "Enter the required instance."
end
if condition.item then
local name = type(condition.item) == "string" and condition.item or condition.item.name
if type(name) ~= "string" or name == "" then
return false, "Invalid item condition."
end
if type(condition.item) == "table" and condition.item.minCount then
local count = ns.PlayerState and ns.PlayerState.GetItemCount
and ns.PlayerState:GetItemCount(name, state)
if count == nil then return Unknown("Item count is unavailable.") end
local enough = count >= condition.item.minCount
return enough, enough and nil or ("Requires %d %s in your bags."):format(condition.item.minCount, name)
end
local has = ns.PlayerState and ns.PlayerState.HasItem
and ns.PlayerState:HasItem(name, state)
return has and true or false, has and nil or "Item is not in your bags."
end
if condition.achievement then
local achievementID = type(condition.achievement) == "table" and condition.achievement.id or nil
if type(achievementID) ~= "number" then
return false, "Invalid achievement condition."
end
local achievements = type(state) == "table" and state.achievements or nil
local achievement = achievements and achievements[achievementID]
if type(achievement) ~= "table" or type(achievement.completed) ~= "boolean" then
return Unknown("Achievement state is unavailable.")
end
if achievement.completed then return true end
return false, "Achievement is incomplete."
end
if condition.achievementCriterion then
local spec = type(condition.achievementCriterion) == "table" and condition.achievementCriterion or {}
local achievementID = spec.id
local name = type(spec.name) == "string" and string.lower(spec.name) or nil
if type(achievementID) ~= "number" or not name or name == "" then
return false, "Invalid achievement criterion condition."
end
local achievements = type(state) == "table" and state.achievements or nil
local achievement = achievements and achievements[achievementID]
if type(achievement) ~= "table" then
return Unknown("Achievement criterion state is unavailable.")
end
local criteria = achievement.criteriaByName
if type(criteria) ~= "table" or type(criteria[name]) ~= "boolean" then
return Unknown("Achievement criterion state is unavailable.")
end
if criteria[name] then return true end
return false, "Achievement criterion is incomplete."
end
if condition.quest then
local questID = condition.quest.id
local wanted = condition.quest.state
local active = state.quests and state.quests[questID]
local completed = state.completedQuests and state.completedQuests[questID]
if wanted == "active" then
if not state.questLogKnown then
return Unknown("Quest log is unavailable.")
end
return active ~= nil, active and nil or "Quest is not in the log."
elseif wanted == "complete" then
if completed then
return true
end
if active then
return active.complete and true or false, active.complete and nil or "Quest objectives are incomplete."
end
if not state.questLogKnown or not QuestChecked(state, questID) then
return Unknown("Quest completion is unavailable.")
end
return false, "Quest is not complete."
elseif wanted == "completed" then
if not QuestChecked(state, questID) then
return Unknown("Quest completion is unavailable.")
end
return completed and true or false, completed and nil or "Quest has not been turned in."
elseif wanted == "activeOrCompleted" then
if active or completed then
return true
end
if not state.questLogKnown or not QuestChecked(state, questID) then
return Unknown("Quest state is unavailable.")
end
return false, "Quest has not been accepted."
elseif wanted == "notCompleted" then
if not QuestChecked(state, questID) then
return Unknown("Quest completion is unavailable.")
end
return not completed, completed and "Quest is already complete." or nil
end
return false, "Unknown quest condition."
end
if condition.questObjective then
local spec = condition.questObjective
local questID = type(spec) == "table" and spec.id or nil
if type(questID) ~= "number" then
return false, "Invalid quest objective."
end
if state.completedQuests and state.completedQuests[questID] then
return true
end
local active = state.quests and state.quests[questID]
if active and active.complete then
return true
end
if not state.questLogKnown then
return Unknown("Quest log is unavailable.")
end
if not active then
if not QuestChecked(state, questID) then
return Unknown("Quest completion is unavailable.")
end
-- The log can lag on login while the quest is still in progress.
return Unknown("Quest is not in the quest log.")
end
local objective = FindObjective(active.objectives, spec)
if objective == nil then
return Unknown("Quest objective is unavailable.")
end
if type(spec.count) == "number" then
if type(objective.numFulfilled) ~= "number" then
return Unknown("Quest objective count is unavailable.")
end
return objective.numFulfilled >= spec.count, "Quest objective count is incomplete."
end
local finished = ObjectiveFinished(objective)
if finished == nil then
return Unknown("Quest objective is unavailable.")
end
return finished, finished and nil or "Quest objective is incomplete."
end
return false, "Unknown condition type."
end
local function ValidateDeclarative(value, path, seen)
if type(value) == "function" then
return false, path .. " cannot contain functions"
end
if type(value) == "table" then
seen = seen or {}
if seen[value] == "visiting" then return false, path .. " cannot contain cyclic tables" end
if seen[value] then return true end
seen[value] = "visiting"
for key, child in pairs(value) do
local kind = type(child)
if kind == "table" or kind == "function" then
local valid, reason = ValidateDeclarative(child, "", seen)
if not valid then return false, path .. "." .. tostring(key) .. reason end
end
end
seen[value] = true
end
return true
end
local function TimerQuestID(goal)
local timer = goal.timer
if type(timer) == "table" and type(timer.quest) == "number" then
return timer.quest
end
local quest = goal.complete and goal.complete.quest
return type(quest) == "table" and quest.id or nil
end
local function ValidateTimer(goal)
local timer = goal.timer
if timer == nil then
return true
end
local seconds = type(timer) == "number" and timer or type(timer) == "table" and timer.seconds
if type(seconds) ~= "number" or seconds <= 0 then
return false, "Goal " .. goal.id .. " has an invalid timer."
end
if type(timer) == "table" and timer.quest ~= nil and type(timer.quest) ~= "number" then
return false, "Goal " .. goal.id .. " has an invalid timer."
end
if type(TimerQuestID(goal)) ~= "number" then
return false, "Goal " .. goal.id .. " has a timer without a quest."
end
return true
end
local function QuestIDFromCondition(condition)
if type(condition) ~= "table" then
return nil
end
local quest = condition.quest
if type(quest) == "table" and type(quest.id) == "number" then
return quest.id
end
local objective = condition.questObjective
if type(objective) == "table" and type(objective.id) == "number" then
return objective.id
end
for _, key in ipairs({ "all", "any" }) do
local group = condition[key]
if type(group) == "table" then
for _, child in ipairs(group) do
local questID = QuestIDFromCondition(child)
if questID then
return questID
end
end
end
end
return nil
end
local function GoalQuestID(goal)
local complete = type(goal) == "table" and goal.complete or nil
return QuestIDFromCondition(complete)
end
function Engine:GetGoalQuestID(goal)
return GoalQuestID(goal)
end
local function TrustedQuestResult(state, goal)
if type(state) ~= "table" or not state.questLogKnown then
return false
end
if state.questCompletionKnown then
return true
end
if type(state.watchedQuests) ~= "table" then
return false
end
local questID = GoalQuestID(goal)
return type(questID) == "number" and state.watchedQuests[questID] == true
end
local function ApplyClientQuestData(guide)
if guide.clientQuestData == false then return end
for _, goal in ipairs(guide.goals) do
if goal.kind == "objective" or goal.kind == "gossip" or goal.kind == "turnin" then
if goal.useClientPin == nil then goal.useClientPin = true end
end
if goal.kind == "objective" and goal.useClientText == nil then
goal.useClientText = true
end
end
end
local function ApplyQuestPrerequisites(guide)
if guide.routeMode == "ordered" then
local turnins = {}
for _, goal in ipairs(guide.goals) do
if goal.kind == "turnin" then
local questID = GoalQuestID(goal)
if questID then turnins[questID] = goal.id end
end
end
for _, goal in ipairs(guide.goals) do
goal.questPrerequisites = {}
for _, requirement in ipairs(goal.requiredQuests or {}) do
local group = { mode = requirement.mode, questIDs = {},
conditions = requirement.conditions, goalIDs = {} }
for _, questID in ipairs(requirement.quests) do
if turnins[questID] then
group.questIDs[#group.questIDs + 1] = questID
group.goalIDs[#group.goalIDs + 1] = turnins[questID]
end
end
if goal.kind == "accept" then goal.questPrerequisites[#goal.questPrerequisites + 1] = group end
end
end
return
end
local turnins, objectives = {}, {}
for index, goal in ipairs(guide.goals) do
if goal.kind == "turnin" then
local questID = GoalQuestID(goal)
if questID then turnins[questID] = turnins[questID] or { id = goal.id, index = index } end
elseif goal.kind == "objective" or goal.kind == "gossip" then
local questID = GoalQuestID(goal)
if questID then
objectives[questID] = objectives[questID] or {}
objectives[questID][#objectives[questID] + 1] = goal.id
end
end
end
-- A turn-in is never ready until every objective authored for that quest is
-- complete. Augmenting legacy data here keeps the public guide format
-- backward compatible while the lint enforces the finalized invariant.
local function GoalByID(goalID)
if not guide.goalByID then return nil end
local index = guide.goalByID[goalID]
return index and guide.goals[index] or nil
end
for _, goal in ipairs(guide.goals) do
if goal.kind == "turnin" then
local questID = GoalQuestID(goal)
local present = {}
goal.dependsOn = goal.dependsOn or {}
for _, dependency in ipairs(goal.dependsOn) do present[dependency] = true end
for _, objectiveID in ipairs(objectives[questID] or {}) do
if present[objectiveID] then
-- already linked
else
local objective = GoalByID(objectiveID)
if goal.segmentID and objective and objective.segmentID
and objective.segmentID ~= goal.segmentID then
-- Shared quest ids repeat on every starter chapter in the
-- merged Era guide. Only wire objectives from this chapter.
else
goal.dependsOn[#goal.dependsOn + 1] = objectiveID
end
end
end
end
end
if FocusedPickupGuide(guide) then return end
for goalIndex, goal in ipairs(guide.goals) do
if goal.kind == "accept" then
local rules = ns.questPrerequisites[GoalQuestID(goal)]
if rules then
goal.questPrerequisites = goal.questPrerequisites or {}
for _, rule in ipairs(rules) do
local group = { mode = rule.mode, conditions = rule.conditions, questIDs = {}, goalIDs = {} }
for _, questID in ipairs(rule.quests) do
local turnin = turnins[questID]
if turnin and turnin.index < goalIndex then
group.questIDs[#group.questIDs + 1] = questID
group.goalIDs[#group.goalIDs + 1] = turnin.id
elseif rule.mode == "all" then
local softPrereqs = guide.casualSpine == true
or (type(guide.title) == "string"
and string.find(guide.title, "(Era)", 1, true))
if not softPrereqs then
error(("Forever GuideMate: guide %s accept %s needs turn-in quest %d")
:format(guide.id, goal.id, questID), 3)
end
end
end
if #group.questIDs == 0 then
local softPrereqs = guide.casualSpine == true
or (type(guide.title) == "string"
and string.find(guide.title, "(Era)", 1, true))
if not softPrereqs then
error(("Forever GuideMate: guide %s accept %s needs at least one prerequisite turn-in")
:format(guide.id, goal.id), 3)
end
else
goal.questPrerequisites[#goal.questPrerequisites + 1] = group
end
end
end
end
end
end
local function ValidateGuide(guide)
if type(guide) ~= "table" or type(guide.id) ~= "string" or guide.id == "" then
return false, "Guide id is required."
end
if type(guide.title) ~= "string" or type(guide.category) ~= "string" then
return false, "Guide title and category are required."
end
if type(guide.revision) ~= "number" or type(guide.goals) ~= "table" or #guide.goals == 0 then
return false, "Guide revision and goals are required."
end
local declarative, reason = ValidateDeclarative(guide, "guide")
if not declarative then
return false, reason
end
local goalIDs = {}
for index, goal in ipairs(guide.goals) do
if type(goal.id) ~= "string" or goal.id == "" or goalIDs[goal.id] then
return false, "Goal ids must be non-empty and unique."
end
if not VALID_KINDS[goal.kind] or type(goal.text) ~= "string" then
return false, "Goal " .. goal.id .. " has an invalid kind or text."
end
local timerValid, timerReason = ValidateTimer(goal)
if not timerValid then
return false, timerReason
end
goalIDs[goal.id] = index
for _, leg in ipairs(goal.route or {}) do
if type(leg.mapID) ~= "number" or type(leg.x) ~= "number" or type(leg.y) ~= "number"
or leg.x < 0 or leg.x > 1 or leg.y < 0 or leg.y > 1 then
return false, "Goal " .. goal.id .. " has an invalid route leg."
end
end
end
for _, goal in ipairs(guide.goals) do
for _, dependency in ipairs(goal.dependsOn or {}) do
if not goalIDs[dependency] then
return false, "Goal " .. goal.id .. " has an unknown dependency."
end
end
end
local visiting, visited = {}, {}
local function Visit(goalID)
if visiting[goalID] then return false end
if visited[goalID] then return true end
visiting[goalID] = true
local goal = guide.goals[goalIDs[goalID]]
local dependencies = {}
for _, dependency in ipairs(goal.dependsOn or {}) do dependencies[#dependencies + 1] = dependency end
for _, group in ipairs(goal.questPrerequisites or {}) do
for _, dependency in ipairs(group.goalIDs or {}) do dependencies[#dependencies + 1] = dependency end
end
for _, dependency in ipairs(dependencies) do
if not goalIDs[dependency] or not Visit(dependency) then return false end
end
visiting[goalID] = nil
visited[goalID] = true
return true
end
for goalID in pairs(goalIDs) do
if not Visit(goalID) then return false, "Guide dependencies must be acyclic." end
end
return true
end
function ns:RegisterGuide(guide)
if self.ExpandClassActions then self:ExpandClassActions(guide) end
ApplyClientQuestData(guide)
if guide.routeMode == "ordered" then
for _, goal in ipairs(guide.goals) do
if goal.useQuestNavigation == nil and not goal.instructionOnly and not goal.checkpointQuest
and (goal.kind == "turnin" or (goal.kind == "objective" and goal.complete and goal.complete.questObjective)) then
goal.useQuestNavigation = true
end
end
end
ApplyQuestPrerequisites(guide)
local valid, reason = ValidateGuide(guide)
if not valid then
error("Forever GuideMate: " .. reason, 2)
end
if self.guides[guide.id] then
error("Forever GuideMate: duplicate guide id " .. guide.id, 2)
end
self.guides[guide.id] = guide
self.guideOrder[#self.guideOrder + 1] = guide.id
trackedQuestIDs = nil
trackedAchievementIDs = nil
end
-- Starter chapters are parallel. After the chosen starter, every later chapter
-- for that faction stays on the route in listed order.
local ERA_STARTER_BY_RACE = {
[1] = "leveling-era-elwynn-forest",
[2] = "leveling-era-durotar",
[3] = "leveling-era-dun-morogh",
[4] = "leveling-era-teldrassil",
[5] = "leveling-era-tirisfal-glades",
[6] = "leveling-era-mulgore",
[7] = "leveling-era-dun-morogh",
[8] = "leveling-era-durotar",
[95] = "leveling-zephras-isle",
[96] = "leveling-zephras-isle",
}
local function IsEraGuide(guide)
if type(guide) ~= "table" or guide.series == "era" then return false end
if type(guide.id) == "string" and string.sub(guide.id, 1, 13) == "leveling-era-" then
return true
end
return type(guide.title) == "string" and string.find(guide.title, "(Era)", 1, true) ~= nil
end
local function GuideFactionAndLevel(guide)
local faction, level
local function Walk(condition)
if type(condition) ~= "table" then return end
if condition.faction == "Alliance" or condition.faction == "Horde" then
faction = condition.faction
end
if type(condition.level) == "table" and type(condition.level.min) == "number" then
level = condition.level.min
end
for _, key in ipairs({ "all", "any" }) do
if type(condition[key]) == "table" then
for _, child in ipairs(condition[key]) do Walk(child) end
end
end
end
Walk(guide.conditions)
return faction, level or 1
end
local function AndCondition(left, right)
if left == nil then return right end
if right == nil then return left end
return { all = { left, right } }
end
local function CopyEraGoal(goal, segment, gate)
local copy = {}
for key, value in pairs(goal) do
copy[key] = value
end
copy.id = segment.id .. ":" .. goal.id
copy.segmentID = segment.id
if type(goal.dependsOn) == "table" then
local dependsOn = {}
for index, dependency in ipairs(goal.dependsOn) do
dependsOn[index] = segment.id .. ":" .. dependency
end
copy.dependsOn = dependsOn
end
if type(goal.questPrerequisites) == "table" then
copy.questPrerequisites = {}
for groupIndex, group in ipairs(goal.questPrerequisites) do
local groupCopy = {}
for key, value in pairs(group) do groupCopy[key] = value end
groupCopy.goalIDs = {}
for index, dependency in ipairs(group.goalIDs or {}) do
groupCopy.goalIDs[index] = segment.id .. ":" .. dependency
end
copy.questPrerequisites[groupIndex] = groupCopy
end
end
copy.conditions = AndCondition(goal.conditions, segment.routeMode == "ordered" and { faction = segment.faction } or gate)
return copy
end
local function RemoveGuide(id)
ns.guides[id] = nil
for index = #ns.guideOrder, 1, -1 do
if ns.guideOrder[index] == id then
table.remove(ns.guideOrder, index)
end
end
end
local function BuildCasualGuide(faction, sources)
local segments = {}
local goals = {}
local segmentByID = {}
-- Each source chapter keeps its own priority space (10, 20, …). Remap onto
-- one increasing spine so later zones cannot sort ahead of earlier ones.
local routePriority = 0
for _, source in ipairs(sources) do
local _, levelMin = GuideFactionAndLevel(source)
local segment = {
id = source.id,
routeMode = source.routeMode,
title = source.title,
faction = faction,
levelMin = levelMin,
maps = {},
goals = {},
gate = source.conditions,
}
local maxLocal = 0
for _, goal in ipairs(source.goals or {}) do
local localPriority = type(goal.priority) == "number" and goal.priority or 0
if localPriority > maxLocal then maxLocal = localPriority end
end
for _, goal in ipairs(source.goals or {}) do
local copy = CopyEraGoal(goal, segment, source.conditions)
local localPriority = type(goal.priority) == "number" and goal.priority or 0
copy.priority = routePriority + localPriority
for _, leg in ipairs(copy.route or {}) do
if type(leg.mapID) == "number" then
segment.maps[leg.mapID] = (segment.maps[leg.mapID] or 0) + 1
end
end
segment.goals[#segment.goals + 1] = copy
goals[#goals + 1] = copy
end
routePriority = routePriority + maxLocal + 1000
segmentByID[segment.id] = segment
segments[#segments + 1] = segment
end
local goalByID = {}
for index, goal in ipairs(goals) do
goalByID[goal.id] = index
end
if #segments == 0 then return nil end
return {
id = "leveling-casual-" .. string.lower(faction),
routeMode = sources[1] and sources[1].routeMode,
title = "Forever Casual Route",
category = "Leveling Quest Guides",
revision = sources[1] and sources[1].revision or 1,
series = "casual",
compactLibrary = true,
-- Built from leveling spines; Forever weave prerequisites may still be mid-port.
casualSpine = true,
segments = segments,
segmentByID = segmentByID,
goalByID = goalByID,
conditions = {
all = {
{ level = { min = 12 } },
{ faction = faction },
},
},
goals = goals,
}
end
function ns:FinalizeGuides()
if self.guidesFinalized then return end
self.guidesFinalized = true
local allianceSources, hordeSources = {}, {}
local retired = {}
for _, guideID in ipairs(self.guideOrder) do
local guide = self.guides[guideID]
if IsEraGuide(guide) and not STARTER_GUIDE_IDS[guide.id] then
local faction = GuideFactionAndLevel(guide)
if faction == "Alliance" then
allianceSources[#allianceSources + 1] = guide
elseif faction == "Horde" then
hordeSources[#hordeSources + 1] = guide
end
retired[guide.id] = faction
end
end
if next(retired) == nil then return end
trackedQuestIDs = nil
trackedAchievementIDs = nil
if ns.SkipLineage then ns.SkipLineage:InvalidateDependents() end
-- Keep registration / TOC order (Horde: Silverpine then Barrens).
-- Sorting by levelMin alone is fine; do not reorder by title.
for guideID in pairs(retired) do
RemoveGuide(guideID)
end
self.retiredEraGuides = retired
self.retiredCasualByFaction = retired
local alliance = BuildCasualGuide("Alliance", allianceSources)
local horde = BuildCasualGuide("Horde", hordeSources)
if alliance then self:RegisterGuide(alliance) end
if horde then self:RegisterGuide(horde) end
end
local function CollectQuestIDs(value, found)
if type(value) ~= "table" then
return
end
if type(value.quest) == "table" and type(value.quest.id) == "number" then
found[value.quest.id] = true
end
if type(value.questID) == "number" then
found[value.questID] = true
end
for _, group in ipairs(value.requiredQuests or {}) do
for _, questID in ipairs(group.quests or {}) do found[questID] = true end
end
for _, child in pairs(value) do
if type(child) == "table" then
CollectQuestIDs(child, found)
end
end
end
local function CollectAchievementIDs(value, found)
if type(value) ~= "table" then return end
if type(value.achievement) == "table" and type(value.achievement.id) == "number" then
found[value.achievement.id] = true
end
local criterion = value.achievementCriterion
if type(criterion) == "table" and type(criterion.id) == "number" then
found[criterion.id] = true
end
for _, child in pairs(value) do
if type(child) == "table" then CollectAchievementIDs(child, found) end
end
end
function ns.GuideUsesQuest(guide, questID)
local found = {}
CollectQuestIDs(guide, found)
return found[questID] == true
end
function ns.GetTrackedQuestIDs()
if trackedQuestIDs then
return trackedQuestIDs
end
local found = {}
for _, guide in pairs(ns.guides) do
CollectQuestIDs(guide, found)
end
local ids = {}
for questID in pairs(found) do
ids[#ids + 1] = questID
end
table.sort(ids)
trackedQuestIDs = ids
return ids
end
function ns.GetTrackedAchievementIDs()
if trackedAchievementIDs then return trackedAchievementIDs end
local found = {}
for _, guide in pairs(ns.guides) do
CollectAchievementIDs(guide, found)
end
local ids = {}
for achievementID in pairs(found) do ids[#ids + 1] = achievementID end
table.sort(ids)
trackedAchievementIDs = ids