-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddEnchantedVariantsToLeveledLists.pas
856 lines (719 loc) · 34.4 KB
/
addEnchantedVariantsToLeveledLists.pas
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
unit addEnchantedVariantsToLeveledLists;
interface
implementation
uses xEditAPI, Classes, SysUtils, StrUtils, Windows;
// Global variable declarations
var
pluginsToCheck: TStringList;
leveledListsToCheck: TList;
leveledListEditorID, outputFileName, outputPluginFilePath, outputPluginFileName, pluginFileName, pluginFilePath, currentPluginFileName: string;
outputFile, outputPluginFile, leveledListGroup, pluginFile: IInterface;
added_masters, debug_mode, pluginLoaded: boolean;
i, k, n, j: integer;
// Called in logic when debug_mode is enabled for additional logging
procedure LogDebug(msg: string);
begin
if debug_mode then
AddMessage(msg);
end;
//// xEdit Base Script function. Called when the script starts.
////
//// We establish the list of plugins that contain LVLIs for us to
//// add to, and determine if debug mode is enabled directly.
////
//// The user is prompted for an EditorID fuzzy search parameter, as well
//// as the output plugin filename with extension.
////
//// We then gather all LVLIs that meet the fuzzy search parameter from all
//// gathered plugins containing LVLIs, and add those filtered LVLIs to a list
function Initialize: integer;
var
leveledListRecord: IInterface;
begin
// Initialize the pluginsToCheck list and add default plugins
// Add a new `pluginsToCheck.Add('X');` statement to add new plugins
// to search for leveled lists in. If a plugin is not found, it
// will simply be skipped.
pluginsToCheck := TStringList.Create;
pluginsToCheck.Add('Skyrim.esm');
pluginsToCheck.Add('Dawnguard.esm');
pluginsToCheck.Add('Hearthfires.esm');
pluginsToCheck.Add('Dragonborn.esm');
pluginsToCheck.Add('Thaumaturgy.esp');
pluginsToCheck.Add('Serenity.esp');
// Enables extra logging during process. You have to change this to true here
debug_mode := false;
// Prompt for the Leveled List EditorID fuzzy search parameter
leveledListEditorID := InputBox('LVLI EditorID Fuzzy Parameter', 'Specify the fuzzy EditorID search parameter. Prefixes such as "SublistEnch", "LItemEnch", etc apply here:', '');
if leveledListEditorID = '' then
Exit;
// Prompt for the output plugin filename
outputFileName := InputBox('Output Plugin', 'Specify the output plugin, including the file extension. This CAN be an existing file. If it does exist, you will see a message popup:', '');
if outputFileName = '' then
Exit;
//// Create the output plugin if possible. If not, we assume it already exists
//// and will attempt to find the plugin by name and assign as output plugin.
// Attempt to create the output plugin
try
outputFile := AddNewFileName(outputFileName);
except
AddMessage('Output plugin appears to exist already, using existing plugin');
end;
// If unable to create, we assume it exists.
if not Assigned(outputFile) then begin
// Iterate current plugins
for i := 0 to FileCount - 1 do begin
outputPluginFile := FileByIndex(i);
outputPluginFilePath := GetFileName(outputPluginFile);
outputPluginFileName := ExtractFileName(outputPluginFilePath);
// Find a match for the input filename and assign as output plugin
if SameText(outputFileName, outputPluginFileName) then begin
outputFile := outputPluginFile;
outputFileName := outputPluginFileName;
Break;
end;
end;
end;
// We should never enter this block
if not Assigned(outputFile) then begin
AddMessage('Unable to create or find output plugin, something has gone very wrong');
Exit;
end;
// Add the LVLI GRUP to the assigned output plugin
Add(outputFile, 'LVLI', True);
//// Index all LVLIs in checking plugins, and add all with fuzzy search
//// parameter to a list for iteration in the Process function.
leveledListsToCheck := TList.Create;
// Iterate through all specified plugins in pluginsToCheck
for k := 0 to pluginsToCheck.Count - 1 do begin
pluginFileName := pluginsToCheck[k];
pluginLoaded := False;
// Find the plugin by name
for n := 0 to FileCount - 1 do begin
pluginFile := FileByIndex(n);
pluginFilePath := GetFileName(pluginFile);
currentPluginFileName := ExtractFileName(pluginFilePath);
if SameText(currentPluginFileName, pluginFileName) then begin
pluginLoaded := True;
Break;
end;
end;
if not pluginLoaded then begin
AddMessage('Plugin ' + pluginFileName + ' not found.');
Continue;
end;
// Find the LVLI group in the current plugin
leveledListGroup := GroupBySignature(pluginFile, 'LVLI');
if not Assigned(leveledListGroup) then begin
AddMessage('No LVLI GRUP found in plugin: ' + pluginFileName);
Continue;
end;
// Iterate through all LVLIs in the plugin to find lists containing
// the input EditorID fuzzy search parameter
for j := 0 to ElementCount(leveledListGroup) - 1 do begin
leveledListRecord := ElementByIndex(leveledListGroup, j);
// Check if the LVLI's EditorID contains the input EditorID
if Pos(UpperCase(leveledListEditorID), UpperCase(EditorID(leveledListRecord))) > 0 then begin
// Add the LVLI to the list of known LVLIs to search
leveledListsToCheck.Add(leveledListRecord);
end;
end
end;
// Initialize variable for determining if masters added
added_masters := false;
end;
//// xEdit Base Script function. Called on every selected record
////
//// The script has two main blocks, which are executed based on
//// the Signature (WEAP, ARMO) of the record in question to properly
//// process the equipment based on it's type. Skips over any records
//// that are not WEAP or ARMO.
function Process(e: IInterface): integer;
var
weaponRecord, armorRecord, enchantment, entry, weapon_keywords, armor_keywords, iteratingLeveledList, newLeveledListRecord: IInterface;
y, i, z, o, level, currentFlags: integer;
enchantmentEditorID, enchantment_string, tier, weapon_type, armor_type, CurrentKeyword, iteratingLeveledListEI: string;
found: boolean;
masterList: TStringList;
begin
found := False;
//// If the record being processed is a Weapon
if Signature(e) = 'WEAP' then begin
weaponRecord := e;
AddMessage('---------------------------------------------------------------------------------------');
AddMessage('Processing weapon record: ' + EditorID(weaponRecord) + ' / ' + DisplayName(weaponRecord));
// Derive the enchantment attached to the currently iterated weapon
enchantment := LinksTo(ElementByPath(weaponRecord, 'EITM'));
// Craft a string of the Enchantment's display name with whitespace stripped
enchantment_string := StringReplace(DisplayName(enchantment), ' ', '',[rfReplaceAll]);
// If the magic is effect is FireDamange, FrostDamage, ShockDamage,
// or PoisonDamage, remove the "Damage" from string
if SameText(enchantment_string, 'FireDamage') then begin
enchantment_string := 'Fire';
end else if SameText(enchantment_string, 'FrostDamage') then begin
enchantment_string := 'Frost';
end else if SameText(enchantment_string, 'ShockDamage') then begin
enchantment_string := 'Shock';
end else if SameText(enchantment_string, 'PoisonDamage') then begin
enchantment_string := 'Poison';
end;
// If the magic effect is DamageStamina or DamageMagicka,
// remove the "Damage" from string.
if SameText(enchantment_string, 'DamageStamina') then begin
enchantment_string := 'Stamina';
end;
if SameText(enchantment_string, 'DamageMagicka') then begin
enchantment_string := 'Magicka';
end;
// If the magict effect is TurnUndead, remove the "Undead"
// from the string.
if SameText(enchantment_string, 'TurnUndead') then
enchantment_string := 'Turn';
// If the magict effect is SunDamage, remove the "Damage"
// from the string.
if SameText(enchantment_string, 'SunDamage') then
enchantment_string := 'Sun';
// If the magict effect is SilentMoonsEnchant, change
// it to LunarDamage
if SameText(enchantment_string, 'SilentMoonsEnchant') then
enchantment_string := 'LunarDamage';
// Derive weapon type and tier from weapon keywords
weapon_keywords := ElementByPath(weaponRecord, 'KWDA');
tier := '';
weapon_type := '';
for o := ElementCount(weapon_keywords) - 1 downTo 0 do
begin
CurrentKeyword := EditorId(WinningOverride(LinksTo(ElementByIndex(weapon_keywords, o))));
// Replace DLC2WeaponMaterial prefix with WeapMaterial in the keyword if it exists
if pos('DLC2WeaponMaterial', CurrentKeyword) > 0 then
CurrentKeyword := StringReplace(CurrentKeyword, 'DLC2Weapon', 'Weap', [rfReplaceAll]);
// Remove DLC1 prefix in the keyword if it exists
if pos('DLC1Weap', CurrentKeyword) > 0 then
CurrentKeyword := StringReplace(CurrentKeyword, 'DLC1', '', [rfReplaceAll]);
// Strip WeapType to only leave weapon type (i.e Greatsword) and assign to weapon_type
if pos('WeapType', CurrentKeyword) > 0 then
begin
weapon_type := StringReplace(CurrentKeyword, 'WeapType', '', [rfReplaceAll]);
end;
// Strip WeapMaterial to only leave weapon material (i.e Orcish) and assign to tier
if pos('WeapMaterial', CurrentKeyword) > 0 then
begin
tier := StringReplace(CurrentKeyword, 'WeapMaterial', '', [rfReplaceAll]);
end;
end;
// Convert Wood tier weapons to Hunting or Iron, depending on whether
// the weapon type is a bow or not.
if SameText('Bow', weapon_type) then begin
if SameText('Wood', tier) then
tier := 'Hunting'
end;
if not SameText('Bow', weapon_type) then begin
if SameText('Wood', tier) then
tier := 'Iron'
end;
// Convert Steel tier weapons to Imperial, if the weapon type
// is a Bow
if SameText('Bow', weapon_type) then begin
if SameText('Steel', tier) then
tier := 'Imperial'
end;
// Convert Silver tier weapons to Steel
if SameText('Silver', tier) then
tier := 'Steel';
// Check if the weapon is a Crossbow, and assign it to a Crossbow
// weapon type rather than Bow
if SameText('Bow', weapon_type) then begin
if Pos(Uppercase('Crossbow'), Uppercase(DisplayName(weaponRecord))) > 0 then
weapon_type := 'Crossbow'
end;
// Convert new weapon types to the list they would be placed in
if SameText('Pike', weapon_type) then
weapon_type := 'Battleaxe';
if SameText('Halberd', weapon_type) then
weapon_type := 'Battleaxe';
if SameText('Scythe', weapon_type) then
weapon_type := 'Battleaxe';
if SameText('QtrStaff', weapon_type) then
weapon_type := 'Battleaxe';
if SameText('Quarterstaff', weapon_type) then
weapon_type := 'Battleaxe';
if SameText('Rapier', weapon_type) then
weapon_type := 'Sword';
if SameText('Katana', weapon_type) then
weapon_type := 'Sword';
if SameText('CurvedSword', weapon_type) then
weapon_type := 'Sword';
if SameText('Claw', weapon_type) then
weapon_type := 'Dagger';
LogDebug(enchantment_string);
LogDebug(tier);
LogDebug(weapon_type);
// Iterate over all leveled lists verified to contain the EditorID
// fuzzy search parameter
for y := 0 to leveledListsToCheck.Count -1 do begin
// Get current iterating leveled list
iteratingLeveledList := ObjectToElement(leveledListsToCheck[y]);
iteratingLeveledListEI := EditorID(iteratingLeveledList);
// If the effect is a XDamage, and the current leveled list
// is a "WeaknessToX" leveled list, go to the next list
if SameText(enchantment_string, 'Fire') then begin
if Pos('Weakness', iteratingLeveledListEI) > 0 then
Continue;
end else if SameText(enchantment_string, 'Frost') then begin
if Pos('Weakness', iteratingLeveledListEI) > 0 then
Continue;
end else if SameText(enchantment_string, 'Shock') then begin
if Pos('Weakness', iteratingLeveledListEI) > 0 then
Continue;
end else if SameText(enchantment_string, 'Poison') then begin
if Pos('Weakness', iteratingLeveledListEI) > 0 then
Continue;
end;
// If the current leveled list is an "Absorb" list, and the current
// weapon has a "Damage" enchantment, go to the next list.
if SameText(enchantment_string, 'Stamina') then begin
if Pos('Absorb', iteratingLeveledListEI) > 0 then
Continue;
end;
if SameText(enchantment_string, 'Magicka') then begin
if Pos('Absorb', iteratingLeveledListEI) > 0 then
Continue;
end;
// Check that found LVLI EditorID contains enchantment effect name
// with whitespace stripped
if Pos(UpperCase(enchantment_string), UpperCase(iteratingLeveledListEI)) > 0 then begin
// Check that found LVLI EditorID contains the derived tier
// This is bypassed if the weapon type is a Crossbow
if (Pos(UpperCase(tier), UpperCase(iteratingLeveledListEI)) > 0) or SameText('Crossbow', weapon_type) then begin
// If the current LVLI is a "Greatsword" list, and the current
// weapon type is a "Sword", go to the next list.
if SameText(weapon_type, 'Sword') then begin
if Pos('Greatsword', iteratingLeveledListEI) > 0 then
Continue;
end;
// If the current LVLI is a "DwarvenCrossbow" list, and the current
// weapon type is a "Crossbow", go to the next list.
if SameText(weapon_type, 'Crossbow') then begin
if Pos('DwarvenCrossbow', iteratingLeveledListEI) > 0 then
Continue;
end;
// Check that found LVLI EditorID contains the derived weapon type
if Pos(UpperCase(weapon_type), UpperCase(iteratingLeveledListEI)) > 0 then begin
// A full match has been found
AddMessage('Found a matching leveled list: ' + iteratingLeveledListEI);
found := True;
// Adding necessary masters to the output plugin
masterList := TStringList.Create;
try
if not added_masters then begin
// Get master files for the plugin the weapon is from.
// This only needs to be done on the first iteration as
// this script generally only iterates over weapons from
// one plugin, with the enchanted variants.
AddMessage('Adding required masters to the output plugin. ' +
'This may take some time and freeze up xEdit, it only occurs ' +
'on the first weapon record iteration.');
ReportRequiredMasters(GetFile(e), masterList, true, True);
added_masters := true
end;
// Get master files for the winning override of the leveled list.
// This must be done on each iteration to ensure the latest LVLI
// override can be used.
ReportRequiredMasters(
GetFile(
WinningOverride(iteratingLeveledList)
),
masterList, true, True);
// Add all masters to the output plugin from masterList
for z := 0 to masterList.Count - 1 do begin
AddMasterIfMissing(outputFile, masterList[z]);
end;
finally
masterList.Free;
end;
// Create an override for the LVLI entry in the output file
newLeveledListRecord := wbCopyElementToFile(
WinningOverride(iteratingLeveledList),
outputFile,
False,
True
);
// We should never be entering this block
if not Assigned(newLeveledListRecord) then begin
AddMessage('Failed to create override for leveled list: ' + iteratingLeveledListEI + '. Something has gone very wrong');
Exit;
end;
// Determine the LVLO level to assign based on the weapon's tier
// and enchantment effect. The first three values in each tier are
// what that tier generally uses. We are still checking for the
// three MGEF levels that are not normally used in each tier.
if Assigned(enchantment) then begin
// IRON
if SameText(tier, 'Iron') then begin
if Pos('01', EditorID(enchantment)) > 0 then
level := 1
else if Pos('02', EditorID(enchantment)) > 0 then
level := 4
else if Pos('03', EditorID(enchantment)) > 0 then
level := 6
else if Pos('04', EditorID(enchantment)) > 0 then
level := 11
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 1;
// STEEL
end else if SameText(tier, 'Steel') then begin
if Pos('01', EditorID(enchantment)) > 0 then
level := 4
else if Pos('02', EditorID(enchantment)) > 0 then
level := 6
else if Pos('03', EditorID(enchantment)) > 0 then
level := 8
else if Pos('04', EditorID(enchantment)) > 0 then
level := 11
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 4;
// DWARVEN
end else if SameText(tier, 'Dwarven') then begin
if Pos('02', EditorID(enchantment)) > 0 then
level := 7
else if Pos('03', EditorID(enchantment)) > 0 then
level := 9
else if Pos('04', EditorID(enchantment)) > 0 then
level := 11
else if Pos('01', EditorID(enchantment)) > 0 then
level := 7
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 7;
// ELVEN
end else if SameText(tier, 'Elven') then begin
if Pos('02', EditorID(enchantment)) > 0 then
level := 13
else if Pos('03', EditorID(enchantment)) > 0 then
level := 15
else if Pos('04', EditorID(enchantment)) > 0 then
level := 17
else if Pos('01', EditorID(enchantment)) > 0 then
level := 13
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 13;
// ORCISH AND NORDIC
end else if SameText(tier, 'Orcish') or SameText(tier, 'Nordic') then begin
if Pos('03', EditorID(enchantment)) > 0 then
level := 20
else if Pos('04', EditorID(enchantment)) > 0 then
level := 22
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('01', EditorID(enchantment)) > 0 then
level := 20
else if Pos('02', EditorID(enchantment)) > 0 then
level := 20
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 20
// GLASS
end else if SameText(tier, 'Glass') then begin
if Pos('03', EditorID(enchantment)) > 0 then
level := 28
else if Pos('04', EditorID(enchantment)) > 0 then
level := 31
else if Pos('05', EditorID(enchantment)) > 0 then
level := 34
else if Pos('01', EditorID(enchantment)) > 0 then
level := 28
else if Pos('02', EditorID(enchantment)) > 0 then
level := 28
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 28
// EBONY AND STALHRIM
end else if SameText(tier, 'Ebony') or SameText(tier, 'Stalhrim') then begin
if Pos('04', EditorID(enchantment)) > 0 then
level := 37
else if Pos('05', EditorID(enchantment)) > 0 then
level := 40
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else if Pos('01', EditorID(enchantment)) > 0 then
level := 37
else if Pos('02', EditorID(enchantment)) > 0 then
level := 37
else if Pos('03', EditorID(enchantment)) > 0 then
level := 37
else
level := 37
// DAEDRIC
end else if SameText(tier, 'Daedric') then begin
if Pos('04', EditorID(enchantment)) > 0 then
level := 47
else if Pos('05', EditorID(enchantment)) > 0 then
level := 50
else if Pos('06', EditorID(enchantment)) > 0 then
level := 53
else if Pos('01', EditorID(enchantment)) > 0 then
level := 47
else if Pos('02', EditorID(enchantment)) > 0 then
level := 47
else if Pos('03', EditorID(enchantment)) > 0 then
level := 47
else
level := 47;
// EXTRA TIERS
// These are less encompassing but there are still lists
// here that we use
// HUNTING
end else if SameText(tier, 'Hunting') then begin
if Pos('01', EditorID(enchantment)) > 0 then
level := 3
else if Pos('02', EditorID(enchantment)) > 0 then
level := 5
else if Pos('03', EditorID(enchantment)) > 0 then
level := 7
else if Pos('04', EditorID(enchantment)) > 0 then
level := 11
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 3;
// IMPERIAL
end else if SameText(tier, 'Imperial') then begin
if Pos('01', EditorID(enchantment)) > 0 then
level := 3
else if Pos('02', EditorID(enchantment)) > 0 then
level := 4
else if Pos('03', EditorID(enchantment)) > 0 then
level := 5
else if Pos('04', EditorID(enchantment)) > 0 then
level := 11
else if Pos('05', EditorID(enchantment)) > 0 then
level := 25
else if Pos('06', EditorID(enchantment)) > 0 then
level := 43
else
level := 3;
end;
end;
// Adding proper flags to the LVLI
// Without doing this. if you are level 43, you will get the highest tier enchantments for Ebony
// in your roll, but if all the variants only applied to the lower two tiers, you would only get
// the vanilla variant. Setting these flags ensures that you can still roll for the lower level
// enchantment. The lower tier enchantment lists already have these flags set in vanilla.
// Get the flags from the LVLI
currentFlags := GetElementNativeValues(newLeveledListRecord, 'LVLF');
// Set "Calculate from all levels <= player's level" AND
// "Calculate for each item in count" on the LVLI flags.
currentFlags := currentFlags or 1 or 2;
// Apply the changed flags to the LVLI
SetElementNativeValues(newLeveledListRecord, 'LVLF', currentFlags);
// Create the new leveled list entry/LVLO and add it to the LVLI
entry := ElementAssign(ElementByPath(newLeveledListRecord, 'Leveled List Entries'), HighInteger, nil, False);
// Assign the weapon, set the count to 1, and apply the derived level to the created LVLO
SetElementEditValues(entry, 'LVLO\Level', IntToStr(level));
SetElementEditValues(entry, 'LVLO\Reference', Name(weaponRecord));
SetElementEditValues(entry, 'LVLO\Count', '1');
AddMessage('Added weapon ' + EditorID(weaponRecord) + ' / ' + DisplayName(weaponRecord) + ' to leveled list ' + EditorID(newLeveledListRecord) + ' at level ' + IntToStr(level));
// We only want to add each weapon to one leveled list, and using
// mods such as Thaumaturgy can cause it's override to register as
// a separate list. This also increases script execution time.
Break;
end;
end;
end;
end;
//////////////////////////////////////////////////////////////////////////////
//// If the record being processed is an Armor Piece
end else if Signature(e) = 'ARMO' then begin
armorRecord := e;
AddMessage('---------------------------------------------------------------------------------------');
AddMessage('Processing armor record: ' + EditorID(armorRecord) + ' / ' + DisplayName(armorRecord));
// Derive the enchantment attached to the currently iterated armor piece
enchantment := LinksTo(ElementByPath(armorRecord, 'EITM'));
// Craft a string of the Enchantment's EditorID with underscores stripped
enchantment_string := StringReplace(EditorID(enchantment), '_', '',[rfReplaceAll]);
/// Based on the enchantment effect tier found on the armor piece's EditorID,
/// change the enchantment_string to match to leveled list tier that the
/// piece would be placed in.
LogDebug(enchantment_string);
// 01
if (Pos('01A', enchantment_string) > 0) or (Pos('01', enchantment_string) > 0) then
enchantment_string := '01';
// 02
if (Pos('01B', enchantment_string) > 0) or (Pos('02', enchantment_string) > 0) then
enchantment_string := '02';
// 03
if (Pos('02B', enchantment_string) > 0) or (Pos('03', enchantment_string) > 0) then
enchantment_string := '03';
// 04
if (Pos('03B', enchantment_string) > 0) or (Pos('04', enchantment_string) > 0) then
enchantment_string := '04';
// 05
if (Pos('04A', enchantment_string) > 0) or (Pos('05', enchantment_string) > 0) then
enchantment_string := '05';
// 06
if (Pos('04B', enchantment_string) > 0) or (Pos('06', enchantment_string) > 0) then
enchantment_string := '06';
// Derive armor type and tier from armor piece keywords
armor_keywords := ElementByPath(armorRecord, 'KWDA');
tier := '';
armor_type := '';
for o := ElementCount(armor_keywords) - 1 downTo 0 do
begin
CurrentKeyword := EditorId(WinningOverride(LinksTo(ElementByIndex(armor_keywords, o))));
// Skip the keyword if it is ArmorHeavy or ArmorLight
if (Pos('ArmorHeavy', CurrentKeyword) > 0) or (Pos('ArmorLight', CurrentKeyword) > 0) then
Continue;
// Skip the keyword if it contains Vendor
if (Pos('Vendor', CurrentKeyword) > 0) then
Continue;
// Remove DLC2 prefix in the keyword if it exists
if pos('DLC2ArmorMaterial', CurrentKeyword) > 0 then
CurrentKeyword := StringReplace(CurrentKeyword, 'DLC2', '', [rfReplaceAll]);
// Remove DLC1 prefix in the keyword if it exists
if pos('DLC1ArmorMaterial', CurrentKeyword) > 0 then
CurrentKeyword := StringReplace(CurrentKeyword, 'DLC1', '', [rfReplaceAll]);
// Strip ArmorMaterial to only leave weapon material (i.e Orcish) and assign to tier
if pos('ArmorMaterial', CurrentKeyword) > 0 then
begin
tier := StringReplace(CurrentKeyword, 'ArmorMaterial', '', [rfReplaceAll]);
end;
// Strip Armor to only leave armor type (i.e Helmet) and assign to armor_type
if pos('Armor', CurrentKeyword) > 0 then
begin
// Skip if keyword contains ArmorMaterial
if pos('ArmorMaterial', CurrentKeyword) > 0 then
Continue;
armor_type := StringReplace(CurrentKeyword, 'Armor', '', [rfReplaceAll]);
end;
end;
// Convert ImperialLight to Leather, Elven if a Shield
if pos('ImperialLight', tier) > 0 then begin
if pos('Shield', armor_type) > 0 then begin
tier := 'Elven';
end else
tier := 'Leather';
end;
// Convert Leather to Hide, if a Shield
if pos('Leather', tier) > 0 then begin
if pos('Shield', armor_type) > 0 then
tier := 'Hide';
end;
// Convert SteelPlate to Steel if a Shield
if pos('Shield', armor_type) > 0 then begin
if pos('SteelPlate', tier) > 0 then
tier := 'Steel';
end;
// Convert ImperialHeavy to Steel
if pos('ImperialHeavy', tier) > 0 then
tier := 'Steel';
// If armor_type is Shield
if pos('Shield', armor_type) > 0 then begin
// Convert Scaled to Elven
if pos('Scaled', tier) > 0 then
tier := 'Elven';
end;
LogDebug(enchantment_string);
LogDebug(tier);
LogDebug(armor_type);
// Iterate over all leveled lists verified to contain the EditorID
// fuzzy search parameter
for y := 0 to leveledListsToCheck.Count -1 do begin
// Get current iterating leveled list
iteratingLeveledList := ObjectToElement(leveledListsToCheck[y]);
iteratingLeveledListEI := EditorID(iteratingLeveledList);
// Check that found LVLI EditorID contains enchantment magnitude
if Pos(UpperCase(enchantment_string), UpperCase(iteratingLeveledListEI)) > 0 then begin
// Check that found LVLI EditorID contains the derived tier
if Pos(UpperCase(tier), UpperCase(iteratingLeveledListEI)) > 0 then begin
// Check that found LVLI EditorID contains the derived armor type
if Pos(UpperCase(armor_type), UpperCase(iteratingLeveledListEI)) > 0 then begin
// A full match has been found
AddMessage('Found a matching leveled list: ' + iteratingLeveledListEI);
found := True;
// Adding necessary masters to the output plugin
masterList := TStringList.Create;
try
if not added_masters then begin
// Get master files for the plugin the armor is from.
// This only needs to be done on the first iteration as
// this script generally only iterates over armors from
// one plugin, with the enchanted variants.
AddMessage('Adding required masters to the output plugin. ' +
'This may take some time and freeze up xEdit, it only occurs ' +
'on the first armor record iteration.');
ReportRequiredMasters(GetFile(e), masterList, true, True);
added_masters := true
end;
// Get master files for the winning override of the leveled list.
// This must be done on each iteration to ensure the latest LVLI
// override can be used.
ReportRequiredMasters(
GetFile(
WinningOverride(iteratingLeveledList)
),
masterList, true, True);
// Add all masters to the output plugin from masterList
for z := 0 to masterList.Count - 1 do begin
AddMasterIfMissing(outputFile, masterList[z]);
end;
finally
masterList.Free;
end;
// Create an override for the LVLI entry in the output file
newLeveledListRecord := wbCopyElementToFile(WinningOverride(iteratingLeveledList), outputFile, False, True);
if not Assigned(newLeveledListRecord) then begin
AddMessage('Failed to create override for leveled list: ' + iteratingLeveledListEI)
end;
// Adding proper flags to the LVLI
// Without doing this. if you are level 43, you will get the highest tier enchantments for Ebony
// in your roll, but if all the variants only applied to the lower two tiers, you would only get
// the vanilla variant. Setting these flags ensures that you can still roll for the lower level
// enchantment. The lower tier enchantment lists already have these flags set in vanilla.
// Get the flags from the LVLI
currentFlags := GetElementNativeValues(newLeveledListRecord, 'LVLF');
// Set "Calculate from all levels <= player's level" AND
// "Calculate for each item in count" on the LVLI flags.
currentFlags := currentFlags or 1 or 2;
// Apply the changed flags to the LVLI
SetElementNativeValues(newLeveledListRecord, 'LVLF', currentFlags);
// Create the new leveled list entry/LVLO and add it to the LVLI
entry := ElementAssign(ElementByPath(newLeveledListRecord, 'Leveled List Entries'), HighInteger, nil, False);
// Assign the armor, set the count to 1, and set the level to 1
// All enchanted armor leveled SublistEnch lists use 1 for the level
SetElementEditValues(entry, 'LVLO\Level', IntToStr(1));
SetElementEditValues(entry, 'LVLO\Reference', Name(armorRecord));
SetElementEditValues(entry, 'LVLO\Count', '1');
AddMessage('Added armor piece ' + EditorID(armorRecord) + ' / ' + DisplayName(armorRecord) + ' to leveled list ' + EditorID(newLeveledListRecord));
// We only want to add each piece to one leveled list, and using
// mods such as Thaumaturgy can cause it's override to register as
// a separate list. This also increases script execution time.
Break;
end;
end;
end;
end;;
end;
if not found then
AddMessage('No matching leveled list found for:' + EditorID(e) + ' / ' + DisplayName(e));
Result := 0;
end;
end.