-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathPassiveSpec.lua
More file actions
2197 lines (2020 loc) · 73.8 KB
/
PassiveSpec.lua
File metadata and controls
2197 lines (2020 loc) · 73.8 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
-- Path of Building
--
-- Class: Passive Spec
-- Passive tree spec class.
-- Manages node allocation and pathing for a given passive spec
--
local pairs = pairs
local ipairs = ipairs
local t_insert = table.insert
local t_remove = table.remove
local m_min = math.min
local m_max = math.max
local m_floor = math.floor
local b_lshift = bit.lshift
local b_rshift = bit.rshift
local band = bit.band
local bor = bit.bor
local PassiveSpecClass = newClass("PassiveSpec", "UndoHandler", function(self, build, treeVersion, convert)
self.UndoHandler()
self.build = build
-- Initialise and build all tables
self:Init(treeVersion, convert)
self:SelectClass(0)
end)
function PassiveSpecClass:Init(treeVersion, convert)
self.treeVersion = treeVersion
self.tree = main:LoadTree(treeVersion)
self.ignoredNodes = { }
self.ignoreAllocatingSubgraph = false
local previousTreeNodes = { }
if convert then
previousTreeNodes = self.build.spec.nodes
end
-- Make a local copy of the passive tree that we can modify
self.nodes = { }
for _, treeNode in pairs(self.tree.nodes) do
-- Exclude proxy or groupless nodes, as well as expansion sockets
if treeNode.group and not treeNode.isProxy and not treeNode.group.isProxy and (not treeNode.expansionJewel or not treeNode.expansionJewel.parent) then
self.nodes[treeNode.id] = setmetatable({
linked = { },
power = { }
}, treeNode)
end
end
for id, node in pairs(self.nodes) do
-- if the node is allocated and between the old and new tree has the same ID but does not share the same name, add to list of nodes to be ignored
if convert and previousTreeNodes[id] and self.build.spec.allocNodes[id] and node.name ~= previousTreeNodes[id].name then
self.ignoredNodes[id] = previousTreeNodes[id]
end
for _, otherId in ipairs(node.linkedId) do
t_insert(node.linked, self.nodes[otherId])
end
end
-- List of currently allocated nodes
-- Keys are node IDs, values are nodes
self.allocNodes = { }
-- List of nodes allocated in subgraphs; used to maintain allocation when loading, and when rebuilding subgraphs
self.allocSubgraphNodes = { }
-- List of cluster nodes to allocate
self.allocExtendedNodes = { }
-- Table of jewels equipped in this tree
-- Keys are node IDs, values are items
self.jewels = { }
-- Tree graphs dynamically generated from cluster jewels
-- Keys are subgraph IDs, values are graphs
self.subGraphs = { }
-- Keys are mastery node IDs, values are mastery effect IDs
self.masterySelections = { }
-- Keys are node IDs, values are the replacement node
self.hashOverrides = { }
-- Cached highlight path for Split Personality jewels
self.splitPersonalityPath = { }
end
function PassiveSpecClass:Load(xml, dbFileName)
self.title = xml.attrib.title
local url
for _, node in pairs(xml) do
if type(node) == "table" then
if node.elem == "URL" then
-- Legacy format
if type(node[1]) ~= "string" then
launch:ShowErrMsg("^1Error parsing '%s': 'URL' element missing content", dbFileName)
return true
end
url = node[1]
elseif node.elem == "Sockets" then
for _, child in ipairs(node) do
if child.elem == "Socket" then
if not child.attrib.nodeId then
launch:ShowErrMsg("^1Error parsing '%s': 'Socket' element missing 'nodeId' attribute", dbFileName)
return true
end
if not child.attrib.itemId then
launch:ShowErrMsg("^1Error parsing '%s': 'Socket' element missing 'itemId' attribute", dbFileName)
return true
end
-- there are files which have been saved poorly and have empty jewel sockets saved as sockets with itemId zero.
-- this check filters them out to prevent dozens of invalid jewels
jewelIdNum = tonumber(child.attrib.itemId)
if jewelIdNum > 0 then
self.jewels[tonumber(child.attrib.nodeId)] = jewelIdNum
end
end
end
end
end
end
if xml.attrib.nodes then
-- New format
if not xml.attrib.classId then
launch:ShowErrMsg("^1Error parsing '%s': 'Spec' element missing 'classId' attribute", dbFileName)
return true
end
if not xml.attrib.ascendClassId then
launch:ShowErrMsg("^1Error parsing '%s': 'Spec' element missing 'ascendClassId' attribute", dbFileName)
return true
end
local hashList = { }
for hash in xml.attrib.nodes:gmatch("%d+") do
t_insert(hashList, tonumber(hash))
end
local masteryEffects = { }
if xml.attrib.masteryEffects then
for mastery, effect in xml.attrib.masteryEffects:gmatch("{(%d+),(%d+)}") do
masteryEffects[tonumber(mastery)] = tonumber(effect)
end
end
for _, node in pairs(xml) do
if type(node) == "table" then
if node.elem == "Overrides" then
for _, child in ipairs(node) do
if not child.attrib.nodeId then
launch:ShowErrMsg("^1Error parsing '%s': 'Override' element missing 'nodeId' attribute", dbFileName)
return true
end
-- In case a tattoo has been replaced by a different one attempt to find the new name for it
if not self.tree.tattoo.nodes[child.attrib.dn] then
for name ,data in pairs(self.tree.tattoo.nodes) do
if data["activeEffectImage"] == child.attrib["activeEffectImage"] and data["icon"] == child.attrib["icon"] then
self.tree.tattoo.nodes[child.attrib.dn] = data
ConPrintf("[PassiveSpecClass:Load] " .. child.attrib.dn .. " tattoo has been substituted with " .. name)
end
end
end
-- If the above failed remove the tattoo to avoid crashing
if self.tree.tattoo.nodes[child.attrib.dn] then
local nodeId = tonumber(child.attrib.nodeId)
self.hashOverrides[nodeId] = copyTable(self.tree.tattoo.nodes[child.attrib.dn], true)
self.hashOverrides[nodeId].id = nodeId
else
ConPrintf("[PassiveSpecClass:Load] Failed to find a tattoo with dn of: " .. child.attrib.dn)
end
end
end
end
end
self:ImportFromNodeList(tonumber(xml.attrib.classId), tonumber(xml.attrib.ascendClassId), tonumber(xml.attrib.secondaryAscendClassId or 0), hashList, self.hashOverrides, masteryEffects)
elseif url then
self:DecodeURL(url)
end
self:ResetUndo()
end
function PassiveSpecClass:Save(xml)
local allocNodeIdList = { }
for nodeId in pairs(self.allocNodes) do
t_insert(allocNodeIdList, nodeId)
end
local masterySelections = { }
for mastery, effect in pairs(self.masterySelections) do
t_insert(masterySelections, "{"..mastery..","..effect.."}")
end
xml.attrib = {
title = self.title,
treeVersion = self.treeVersion,
-- New format
classId = tostring(self.curClassId),
ascendClassId = tostring(self.curAscendClassId),
secondaryAscendClassId = tostring(self.curSecondaryAscendClassId),
nodes = table.concat(allocNodeIdList, ","),
masteryEffects = table.concat(masterySelections, ",")
}
t_insert(xml, {
-- Legacy format
elem = "URL",
[1] = self:EncodeURL("https://www.pathofexile.com/passive-skill-tree/")
})
local sockets = {
elem = "Sockets"
}
for nodeId, itemId in pairs(self.jewels) do
-- jewel socket contents should not be saved unless they contain a valid jewel
if itemId > 0 then
local socket = { elem = "Socket", attrib = { nodeId = tostring(nodeId), itemId = tostring(itemId) }}
t_insert( sockets, socket )
end
end
t_insert(xml, sockets)
local overrides = {
elem = "Overrides"
}
if self.hashOverrides then
for nodeId, node in pairs(self.hashOverrides) do
local override = { elem = "Override", attrib = { nodeId = tostring(nodeId), icon = tostring(node.icon), activeEffectImage = tostring(node.activeEffectImage), dn = tostring(node.dn) } }
for _, modLine in ipairs(node.sd) do
t_insert(override, modLine)
end
t_insert(overrides, override)
end
end
t_insert(xml, overrides)
end
function PassiveSpecClass:PostLoad()
self:BuildClusterJewelGraphs()
end
-- Import passive spec from the provided class IDs and node hash list
function PassiveSpecClass:ImportFromNodeList(classId, ascendClassId, secondaryAscendClassId, hashList, hashOverrides, masteryEffects, treeVersion)
if hashOverrides == nil then hashOverrides = {} end
if treeVersion and treeVersion ~= self.treeVersion then
self:Init(treeVersion)
self.build.treeTab.showConvert = self.treeVersion ~= latestTreeVersion
end
self:ResetNodes()
self:SelectClass(classId)
self:SelectAscendClass(ascendClassId)
self:SelectSecondaryAscendClass(secondaryAscendClassId)
self.hashOverrides = hashOverrides
-- move above setting allocNodes so we can compare mastery with selection
wipeTable(self.masterySelections)
for mastery, effect in pairs(masteryEffects) do
-- ignore ggg codes from profile import
if (tonumber(effect) < 65536) then
self.masterySelections[mastery] = effect
end
end
for id, override in pairs(hashOverrides) do
local node = self.nodes[id]
if node then
override.effectSprites = self.tree.spriteMap[override.activeEffectImage]
override.sprites = self.tree.spriteMap[override.icon]
self:ReplaceNode(node, override)
end
end
for _, id in pairs(hashList) do
local node = self.nodes[id]
if node then
-- check to make sure the mastery node has a corresponding selection, if not do not allocate
if node.type ~= "Mastery" or (node.type == "Mastery" and self.masterySelections[id]) then
node.alloc = true
self.allocNodes[id] = node
end
else
t_insert(self.allocSubgraphNodes, id)
end
end
for _, id in pairs(self.allocExtendedNodes) do
local node = self.nodes[id]
if node then
node.alloc = true
self.allocNodes[id] = node
end
end
-- Rebuild all the node paths and dependencies
self:BuildAllDependsAndPaths()
end
function PassiveSpecClass:AllocateDecodedNodes(nodes, isCluster, endian)
for i = 1, #nodes - 1, 2 do
local id
if endian == "big" then
id = nodes:byte(i) * 256 + nodes:byte(i + 1)
else
id = nodes:byte(i) + nodes:byte(i + 1) * 256
end
if isCluster then
id = id + 65536
end
local node = self.nodes[id]
if node then
node.alloc = true
self.allocNodes[id] = node
end
end
end
function PassiveSpecClass:AllocateMasteryEffects(masteryEffects, endian)
for i = 1, #masteryEffects - 1, 4 do
local effectId, id
if endian == "big" then
effectId = masteryEffects:byte(i) * 256 + masteryEffects:byte(i + 1)
id = masteryEffects:byte(i + 2) * 256 + masteryEffects:byte(i + 3)
else
-- "little". NOTE: poeplanner swap effectId and id too.
effectId = masteryEffects:byte(i + 2) + masteryEffects:byte(i + 3) * 256
id = masteryEffects:byte(i) + masteryEffects:byte(i + 1) * 256
-- Assign the node, representing the Mastery, not required for GGG urls.
local node = self.nodes[id]
if node then
node.alloc = true
self.allocNodes[id] = node
end
end
local effect = self.tree.masteryEffects[effectId]
if effect then
self.allocNodes[id].sd = effect.sd
self.allocNodes[id].allMasteryOptions = false
self.allocNodes[id].reminderText = { "Tip: Right click to select a different effect" }
self.tree:ProcessStats(self.allocNodes[id])
self.masterySelections[id] = effectId
self.allocatedMasteryCount = self.allocatedMasteryCount + 1
if not self.allocatedMasteryTypes[self.allocNodes[id].name] then
self.allocatedMasteryTypes[self.allocNodes[id].name] = 1
self.allocatedMasteryTypeCount = self.allocatedMasteryTypeCount + 1
else
local prevCount = self.allocatedMasteryTypes[self.allocNodes[id].name]
self.allocatedMasteryTypes[self.allocNodes[id].name] = prevCount + 1
if prevCount == 0 then
self.allocatedMasteryTypeCount = self.allocatedMasteryTypeCount + 1
end
end
else
-- if there is no effect/selection on the latest tree then we do not want to allocate the mastery
self.allocNodes[id] = nil
self.nodes[id].alloc = false
end
end
end
-- Decode the given poeplanner passive tree URL
function PassiveSpecClass:DecodePoePlannerURL(url, return_tree_version_only)
-- poeplanner uses little endian numbers (GGG using BIG).
-- If return_tree_version_only is True, then the return value will either be an error message or the tree version.
-- both error messages begin with 'Invalid'
local function byteToInt(bytes, start)
-- get a little endian number from two bytes
return bytes:byte(start) + bytes:byte(start + 1) * 256
end
local b = common.base64.decode(url:gsub("^.+/",""):gsub("-","+"):gsub("_","/"))
if not b or #b < 15 then
return "Invalid tree link (unrecognised format)."
end
-- Quick debug for when we change tree versions. Print the first 20 or so bytes
-- s = ""
-- for i = 1, 20 do
-- s = s..i..":"..string.format('%02X ', b:byte(i))
-- end
-- print(s)
--[[
PoEPlanner URL format:
serializationVersion: u16
buildType: u8 (either normal or royale)
isPoE2: u8
treeBuild: TreeBuild
equipmentBuild: EquipmentBuild
skillBuild: SkillBuild
buildConfig: BuildConfig
compressedNotesLength: u16
compressedNotesBytes: []u8 (gzip)
TreeBuild:
treeSerializationVersion: u16
treeVersion: u16
character: u8
ascendancy: u8
banditChoice: u8
nodeCount: u16
nodeHashes: []u16
clusterNodeCount: u16
clusterNodeHashes: []u16
ascendancyNodeCount: u16
ascendancyNodeHashes: []u16
selectedMasteryEffectCount: u16
selectedMasteryEffects: {masteryID: u16, effectID: u16}
selectedAttributeChoiceCount: u16
selectedAttributeChoices: {nodeHash: u16, choice: u8} (for choice: 0: none, 1: str, 2: dex, 3: int)
]]
-- If we only want the tree version, exit now
if not poePlannerVersions[byteToInt(b, 7)] then
return "Invalid tree version found in link."
end
if return_tree_version_only then
return poePlannerVersions[byteToInt(b, 7)]
end
-- 9 is Class, 10 is Ascendancy
local classId = b:byte(9)
local ascendClassId = b:byte(10)
-- print("classId, ascendClassId", classId, ascendClassId)
-- 11 is Bandit
-- bandit = b[9]
-- print("bandit", bandit, bandit_list[bandit])
self:ResetNodes()
self:SelectClass(classId)
self:SelectAscendClass(ascendClassId)
-- 12-13 is node count
idx = 12
local nodesCount = byteToInt(b, idx)
local nodesEnd = idx + 2 + (nodesCount * 2)
local nodes = b:sub(idx + 2, nodesEnd - 1)
-- print("idx + 2 , nodesEnd, nodesCount, len(nodes)", idx + 2, nodesEnd, nodesCount, #nodes)
self:AllocateDecodedNodes(nodes, false, "little")
idx = nodesEnd
local clusterCount = byteToInt(b, idx)
local clusterEnd = idx + 2 + (clusterCount * 2)
local clusterNodes = b:sub(idx + 2, clusterEnd - 1)
-- print("idx + 2 , clusterEnd, clusterCount, len(clusterNodes)", idx + 2, clusterEnd, clusterCount, #clusterNodes)
self:AllocateDecodedNodes(clusterNodes, true, "little")
-- poeplanner has Ascendancy nodes in a separate array
idx = clusterEnd
local ascendancyCount = byteToInt(b, idx)
local ascendancyEnd = idx + 2 + (ascendancyCount * 2)
local ascendancyNodes = b:sub(idx + 2, ascendancyEnd - 1)
-- print("idx + 2 , ascendancyEnd, ascendancyCount, len(ascendancyNodes)", idx + 2, ascendancyEnd, ascendancyCount, #ascendancyNodes)
self:AllocateDecodedNodes(ascendancyNodes, false, "little")
idx = ascendancyEnd
local masteryCount = byteToInt(b, idx)
local masteryEnd = idx + 2 + (masteryCount * 4)
local masteryEffects = b:sub(idx + 2, masteryEnd - 1)
-- print("idx + 2 , masteryEnd, masteryCount, len(masteryEffects)", idx + 2, masteryEnd, masteryCount, #masteryEffects)
self:AllocateMasteryEffects(masteryEffects, "little")
end
-- Decode the given GGG passive tree URL
function PassiveSpecClass:DecodeURL(url)
local b = common.base64.decode(url:gsub("^.+/",""):gsub("-","+"):gsub("_","/"))
if not b or #b < 6 then
return "Invalid tree link (unrecognised format)"
end
local ver = b:byte(1) * 16777216 + b:byte(2) * 65536 + b:byte(3) * 256 + b:byte(4)
if ver > 6 then
return "Invalid tree link (unknown version number '"..ver.."')"
end
local classId = b:byte(5)
local ascendancyIds = (ver >= 4) and b:byte(6) or 0
local ascendClassId = band(ascendancyIds, 3)
local secondaryAscendClassId = b_rshift(band(ascendancyIds, 12), 2)
if not self.tree.classes[classId] then
return "Invalid tree link (bad class ID '"..classId.."')"
end
self:ResetNodes()
self:SelectClass(classId)
self:SelectAscendClass(ascendClassId)
self:SelectSecondaryAscendClass(secondaryAscendClassId)
local nodesStart = ver >= 4 and 8 or 7
local nodesEnd = ver >= 5 and 7 + (b:byte(7) * 2) or -1
local nodes = b:sub(nodesStart, nodesEnd)
self:AllocateDecodedNodes(nodes, false, "big")
if ver < 5 then
return
end
local clusterStart = nodesEnd + 1
local clusterEnd = clusterStart + (b:byte(clusterStart) * 2)
local clusterNodes = b:sub(clusterStart + 1, clusterEnd)
self:AllocateDecodedNodes(clusterNodes, true, "big")
if ver < 6 then
return
end
local masteryStart = clusterEnd + 1
local masteryEnd = masteryStart + (b:byte(masteryStart) * 4)
local masteryEffects = b:sub(masteryStart + 1, masteryEnd)
self:AllocateMasteryEffects(masteryEffects, "big")
end
-- Encodes the current spec into a URL, using the official skill tree's format
-- Prepends the URL with an optional prefix
function PassiveSpecClass:EncodeURL(prefix)
local a = { 0, 0, 0, 6, self.curClassId, bor(b_lshift(self.curSecondaryAscendClassId or 0, 2), self.curAscendClassId) }
local nodeCount = 0
local clusterCount = 0
local masteryCount = 0
local clusterNodeIds = {}
local masteryNodeIds = {}
for id, node in pairs(self.allocNodes) do
if node.type ~= "ClassStart" and node.type ~= "AscendClassStart" and id < 65536 and nodeCount < 255 then
t_insert(a, m_floor(id / 256))
t_insert(a, id % 256)
nodeCount = nodeCount + 1
if self.masterySelections[node.id] then
local effect_id = self.masterySelections[node.id]
t_insert(masteryNodeIds, m_floor(effect_id / 256))
t_insert(masteryNodeIds, effect_id % 256)
t_insert(masteryNodeIds, m_floor(node.id / 256))
t_insert(masteryNodeIds, node.id % 256)
masteryCount = masteryCount + 1
end
elseif id >= 65536 then
local clusterId = id - 65536
t_insert(clusterNodeIds, m_floor(clusterId / 256))
t_insert(clusterNodeIds, clusterId % 256)
clusterCount = clusterCount + 1
end
end
t_insert(a, 7, nodeCount)
t_insert(a, clusterCount)
for _, id in pairs(clusterNodeIds) do
t_insert(a, id)
end
t_insert(a, masteryCount)
for _, id in pairs(masteryNodeIds) do
t_insert(a, id)
end
return (prefix or "")..common.base64.encode(string.char(unpack(a))):gsub("+","-"):gsub("/","_")
end
-- Change the current class, preserving currently allocated nodes if they connect to the new class's starting node
function PassiveSpecClass:SelectClass(classId)
if self.curClassId then
-- Deallocate the current class's starting node
local oldStartNodeId = self.curClass.startNodeId
self.nodes[oldStartNodeId].alloc = false
self.allocNodes[oldStartNodeId] = nil
end
self:ResetAscendClass()
self.curClassId = classId
local class = self.tree.classes[classId]
self.curClass = class
self.curClassName = class.name
-- Allocate the new class's starting node
local startNode = self.nodes[class.startNodeId]
startNode.alloc = true
self.allocNodes[startNode.id] = startNode
-- Reset the ascendancy class
-- This will also rebuild the node paths and dependencies
self:SelectAscendClass(0)
end
function PassiveSpecClass:ResetAscendClass()
if self.curAscendClassId then
-- Deallocate the current ascendancy class's start node
local ascendClass = self.curClass.classes[self.curAscendClassId] or self.curClass.classes[0]
local oldStartNodeId = ascendClass.startNodeId
if oldStartNodeId then
self.nodes[oldStartNodeId].alloc = false
self.allocNodes[oldStartNodeId] = nil
end
end
end
function PassiveSpecClass:SelectAscendClass(ascendClassId)
self:ResetAscendClass()
self.curAscendClassId = ascendClassId
local ascendClass = self.curClass.classes[ascendClassId] or self.curClass.classes[0]
self.curAscendClass = ascendClass
self.curAscendClassName = ascendClass.name
self.curAscendClassBaseName = ascendClass.id
if ascendClass.startNodeId then
-- Allocate the new ascendancy class's start node
local startNode = self.nodes[ascendClass.startNodeId]
startNode.alloc = true
self.allocNodes[startNode.id] = startNode
end
-- Rebuild all the node paths and dependencies
self:BuildAllDependsAndPaths()
end
function PassiveSpecClass:SelectSecondaryAscendClass(ascendClassId)
-- if Secondary Ascendancy does not exist on this tree version
if not self.tree.alternate_ascendancies then
return
end
if self.curSecondaryAscendClassId then
-- Deallocate the current ascendancy class's start node
local ascendClass = self.tree.alternate_ascendancies[self.curSecondaryAscendClassId]
if ascendClass then
local oldStartNodeId = ascendClass.startNodeId
if oldStartNodeId then
self.nodes[oldStartNodeId].alloc = false
self.allocNodes[oldStartNodeId] = nil
end
end
end
self.curSecondaryAscendClassId = ascendClassId
if ascendClassId == 0 then
self.curSecondaryAscendClass = nil
self.curSecondaryAscendClassName = "None"
elseif self.tree.alternate_ascendancies[self.curSecondaryAscendClassId] then
local ascendClass = self.tree.alternate_ascendancies[self.curSecondaryAscendClassId]
self.curSecondaryAscendClass = ascendClass
self.curSecondaryAscendClassName = ascendClass.name
if ascendClass.startNodeId then
-- Allocate the new ascendancy class's start node
local startNode = self.nodes[ascendClass.startNodeId]
startNode.alloc = true
self.allocNodes[startNode.id] = startNode
end
end
-- Rebuild all the node paths and dependencies
self:BuildAllDependsAndPaths()
end
-- Determines if the given class's start node is connected to the current class's start node
-- Attempts to find a path between the nodes which doesn't pass through any ascendancy nodes (i.e. Ascendant)
function PassiveSpecClass:IsClassConnected(classId)
for _, other in ipairs(self.nodes[self.tree.classes[classId].startNodeId].linked) do
-- For each of the nodes to which the given class's start node connects...
if other.alloc then
-- If the node is allocated, try to find a path back to the current class's starting node
other.visited = true
local visited = { }
local found = self:FindStartFromNode(other, visited, true)
for i, n in ipairs(visited) do
n.visited = false
end
other.visited = false
if found then
-- Found a path, so the given class's start node is definitely connected to the current class's start node
-- There might still be nodes which are connected to the current tree by an entirely different path though
-- E.g. via Ascendant or by connecting to another "first passive node"
return true
end
end
end
return false
end
-- Find and allocate the shortest path to connect to a target class's starting node
function PassiveSpecClass:ConnectToClass(classId)
local classData = self.tree.classes[classId]
if not classData then
return false
end
local targetStartNode = self.nodes[classData.startNodeId]
if not targetStartNode then
return false
end
local function isMainTreeNode(node)
return node
and not node.isProxy
and not node.ascendancyName
and node.type ~= "ClassStart"
and node.type ~= "AscendClassStart"
end
local visited = {}
local prev = {}
local queue = { targetStartNode }
visited[targetStartNode] = true
local head = 1
local foundNode = nil
while queue[head] and not foundNode do
local node = queue[head]
head = head + 1
if node ~= targetStartNode and node.alloc and node.connectedToStart and node.type ~= "ClassStart" and node.type ~= "AscendClassStart" then
foundNode = node
break
end
for _, linked in ipairs(node.linked) do
if isMainTreeNode(linked) and not visited[linked] then
visited[linked] = true
prev[linked] = node
queue[#queue + 1] = linked
end
end
end
if not foundNode then
return false
end
local pathBack = {}
local current = foundNode
while current do
t_insert(pathBack, current)
if current == targetStartNode then
break
end
current = prev[current]
end
if pathBack[#pathBack] ~= targetStartNode then
return false
end
local altPath = { pathBack[1] }
for idx = 2, #pathBack - 1 do
altPath[idx] = pathBack[idx]
local node = pathBack[idx]
if not node.alloc then
self:AllocNode(node, altPath)
end
end
return true
end
-- Clear the allocated status of all non-class-start nodes
function PassiveSpecClass:ResetNodes()
for id, node in pairs(self.nodes) do
if node.type ~= "ClassStart" and node.type ~= "AscendClassStart" then
node.alloc = false
self.allocNodes[id] = nil
end
end
wipeTable(self.masterySelections)
end
-- Allocate the given node, if possible, and all nodes along the path to the node
-- An alternate path to the node may be provided, otherwise the default path will be used
-- The path must always contain the given node, as will be the case for the default path
function PassiveSpecClass:AllocNode(node, altPath)
if not node.path then
-- Node cannot be connected to the tree as there is no possible path
return
end
-- Allocate all nodes along the path
if #node.intuitiveLeapLikesAffecting > 0 then
node.alloc = true
self.allocNodes[node.id] = node
else
for _, pathNode in ipairs(altPath or node.path) do
pathNode.alloc = true
self.allocNodes[pathNode.id] = pathNode
end
end
if node.isMultipleChoiceOption then
-- For multiple choice passives, make sure no other choices are allocated
local parent = node.linked[1]
for _, optNode in ipairs(parent.linked) do
if optNode.isMultipleChoiceOption and optNode.alloc and optNode ~= node then
optNode.alloc = false
self.allocNodes[optNode.id] = nil
end
end
end
-- Rebuild all dependencies and paths for all allocated nodes
self:BuildAllDependsAndPaths()
end
function PassiveSpecClass:DeallocSingleNode(node)
node.alloc = false
self.allocNodes[node.id] = nil
if node.type == "Mastery" then
self:AddMasteryEffectOptionsToNode(node)
self.masterySelections[node.id] = nil
end
end
-- Deallocate the given node, and all nodes which depend on it (i.e. which are only connected to the tree through this node)
function PassiveSpecClass:DeallocNode(node)
for _, depNode in ipairs(node.depends) do
self:DeallocSingleNode(depNode)
end
-- Rebuild all paths and dependencies for all allocated nodes
self:BuildAllDependsAndPaths()
end
-- Count the number of allocated nodes and allocated ascendancy nodes
function PassiveSpecClass:CountAllocNodes()
local used, ascUsed, secondaryAscUsed, sockets = 0, 0, 0, 0
for _, node in pairs(self.allocNodes) do
if node.type ~= "ClassStart" and node.type ~= "AscendClassStart" then
if node.ascendancyName then
if not node.isMultipleChoiceOption then
ascUsed = ascUsed + 1
if self.tree.secondaryAscendNameMap and self.tree.secondaryAscendNameMap[node.ascendancyName] then
secondaryAscUsed = secondaryAscUsed + 1
end
end
else
used = used + 1
end
if node.type == "Socket" then
sockets = sockets + 1
end
end
end
return used, ascUsed, secondaryAscUsed, sockets
end
-- Attempt to find a class start node starting from the given node
-- Unless noAscent == true it will also look for an ascendancy class start node
function PassiveSpecClass:FindStartFromNode(node, visited, noAscend)
-- Mark the current node as visited so we don't go around in circles
node.visited = true
t_insert(visited, node)
-- For each node which is connected to this one, check if...
for _, other in ipairs(node.linked) do
-- Either:
-- - the other node is a start node, or
-- - there is a path to a start node through the other node which didn't pass through any nodes which have already been visited
local startIndex = #visited + 1
if other.alloc and
(other.type == "ClassStart" or other.type == "AscendClassStart" or
(not other.visited and node.type ~= "Mastery" and self:FindStartFromNode(other, visited, noAscend))
) then
if node.ascendancyName and not other.ascendancyName then
-- Pathing out of Ascendant, un-visit the outside nodes
for i = startIndex, #visited do
visited[i].visited = false
visited[i] = nil
end
elseif not noAscend or other.type ~= "AscendClassStart" then
return true
end
end
end
end
function PassiveSpecClass:GetJewel(itemId)
if not itemId or itemId == 0 then
return
end
local item = self.build.itemsTab.items[itemId]
if not item or not item.jewelData then
return
end
return item
end
-- Perform a breadth-first search of the tree, starting from this node, and determine if it is the closest node to any other nodes
function PassiveSpecClass:BuildPathFromNode(root)
root.pathDist = 0
root.path = { }
local queue = { root }
local o, i = 1, 2 -- Out, in
while o < i do
-- Nodes are processed in a queue, until there are no nodes left
-- All nodes that are 1 node away from the root will be processed first, then all nodes that are 2 nodes away, etc
local node = queue[o]
o = o + 1
local curDist = node.pathDist
-- Iterate through all nodes that are connected to this one
for _, other in ipairs(node.linked) do
-- Paths must obey these rules:
-- 1. They must not pass through class or ascendancy class start nodes (but they can start from such nodes)
-- 2. They cannot pass between different ascendancy classes or between an ascendancy class and the main tree
-- The one exception to that rule is that a path may start from an ascendancy node and pass into the main tree
-- This permits pathing from the Ascendant 'Path of the X' nodes into the respective class start areas
-- 3. They must not pass away from mastery nodes
if not other.pathDist then
ConPrintTable(other, true)
end
if node.type ~= "Mastery" and other.type ~= "ClassStart" and other.type ~= "AscendClassStart" and other.pathDist > curDist and (node.ascendancyName == other.ascendancyName or (curDist == 0 and not other.ascendancyName)) then
-- The shortest path to the other node is through the current node
other.pathDist = curDist
if not other.alloc then
other.pathDist = other.pathDist + 1
end
other.path = wipeTable(other.path)
other.path[1] = other
for i, n in ipairs(node.path) do
other.path[i+1] = n
end
-- Add the other node to the end of the queue
queue[i] = other
i = i + 1
end
end
end
end
-- Determine this node's distance from the class' start
-- Only allocated nodes can be traversed
function PassiveSpecClass:SetNodeDistanceToClassStart(root)
root.distanceToClassStart = 0
if not root.alloc or not root.connectedToStart then
return
end
-- Stop once the current class' starting node is reached
local targetNodeId = self.curClass.startNodeId
local nodeDistanceToRoot = { }
nodeDistanceToRoot[root.id] = 0
local queue = { root }
local o, i = 1, 2 -- Out, in
while o < i do
-- Nodes are processed in a queue, until there are no nodes left or the starting node is reached
-- All nodes that are 1 node away from the root will be processed first, then all nodes that are 2 nodes away, etc
-- Only allocated nodes are queued
local node = queue[o]
o = o + 1
local curDist = nodeDistanceToRoot[node.id] + 1
-- Iterate through all nodes that are connected to this one
for _, other in ipairs(node.linked) do
-- If this connected node is the correct class start node, then record the distance to the node and return
if other.id == targetNodeId then
root.distanceToClassStart = curDist - 1
return
end
-- Otherwise, record the distance to this node if it hasn't already been visited
if other.alloc and node.type ~= "Mastery" and other.type ~= "ClassStart" and other.type ~= "AscendClassStart" and not nodeDistanceToRoot[other.id] then
nodeDistanceToRoot[other.id] = curDist;
-- Add the other node to the end of the queue
queue[i] = other
i = i + 1
end
end
end
end
-- Determine the shortest path from the given node to the class' start
-- Only allocated nodes can be traversed
function PassiveSpecClass:GetShortestPathToClassStart(rootId)
local root = self.nodes[rootId]
if not root or not root.alloc or not root.connectedToStart then
return nil
end
-- Stop once the current class' starting node is reached
local targetNodeId = self.curClass.startNodeId
local parent = { }
parent[root.id] = nil
local queue = { root }
local o, i = 1, 2 -- Out, in
while o < i do
local node = queue[o]
o = o + 1
-- Iterate through all nodes that are connected to this one
for _, other in ipairs(node.linked) do
-- If this connected node is the correct class start node, then construct and return the path
if other.id == targetNodeId then
local path = { [root.id] = true, [other.id] = true }
local cur = node
while cur do
path[cur.id] = true
cur = parent[cur.id]
end
return path
end
-- Otherwise, record the parent of this node if it hasn't already been visited
if other.alloc and node.type ~= "Mastery" and other.type ~= "ClassStart" and other.type ~= "AscendClassStart" and not parent[other.id] and other.id ~= root.id then
parent[other.id] = node
-- Add the other node to the end of the queue
queue[i] = other
i = i + 1
end
end
end
return nil
end