forked from PekanMmd/Pokemon-XD-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXDExtensions.swift
2507 lines (1955 loc) · 71.5 KB
/
XDExtensions.swift
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
//
// XDExtensions.swift
// Colosseum Tool
//
// Created by The Steez on 04/06/2018.
//
import Foundation
enum XGRegions : UInt32 {
case US = 0x47585845 // GXXE
case EU = 0x47585850 // GXXP
case JP = 0x4758584A // GXXJ
var index : Int {
switch self {
// arbitrary values
case .US: return 0
case .EU: return 1
case .JP: return 2
}
}
}
class XGMapRel : XGRelocationTable {
@objc var characters = [XGCharacter]()
@objc var entryLocations = [XGMapEntryLocation]()
@objc var treasure = [XGTreasure]()
@objc var roomID = 0
var isValid = true
@objc var script : XGScript? {
let scriptFile = XGFiles.scd(file.fileName.removeFileExtensions())
return scriptFile.exists ? scriptFile.scriptData : nil
}
override convenience init(file: XGFiles) {
self.init(file: file, checkScript: true)
}
init(file: XGFiles, checkScript: Bool) {
super.init(file: file)
if self.numberOfPointers < kNumberMapPointers {
if verbose {
printg("Map: \(file.path) has the incorrect number of map pointers. Possibly a colosseum file.")
}
self.isValid = false
return
}
let firstIP = self.getPointer(index: MapRelIndexes.FirstInteractionLocation.rawValue)
let numberOfIPs = self.getValueAtPointer(index: MapRelIndexes.NumberOfInteractionLocations.rawValue)
for i in 0 ..< numberOfIPs {
let startOffset = firstIP + (i * kSizeOfMapEntryLocation)
if startOffset + 16 < file.fileSize {
let ip = XGMapEntryLocation(file: file, index: i, startOffset: startOffset)
entryLocations.append(ip)
}
}
if let room = XGRoom.roomWithName(file.fileName.removeFileExtensions()) {
self.roomID = room.roomID
}
for i in 0 ..< CommonIndexes.NumberTreasureBoxes.value {
let treasure = XGTreasure(index: i)
if treasure.roomID == self.roomID {
self.treasure.append(treasure)
}
}
let firstCharacter = self.getPointer(index: MapRelIndexes.FirstCharacter.rawValue)
let numberOfCharacters = self.getValueAtPointer(index: MapRelIndexes.NumberOfCharacters.rawValue)
let script = checkScript ? self.script : nil
for i in 0 ..< numberOfCharacters {
let startOffset = firstCharacter + (i * kSizeOfCharacter)
if startOffset + kSizeOfCharacter < file.fileSize {
let character = XGCharacter(file: file, index: i, startOffset: startOffset)
if character.isValid {
if character.hasScript {
if script != nil {
if character.scriptIndex < script!.ftbl.count {
character.scriptName = script!.ftbl[character.scriptIndex].name
}
}
}
} else {
self.isValid = false
}
characters.append(character)
}
}
}
}
extension XGISO {
@objc var fsysList : [String] {
return [
"common.fsys",
"common_dvdeth.fsys",
"deck_archive.fsys",
"field_common.fsys",
"fight_common.fsys",
"people_archive.fsys"
]
}
@objc var menuFsysList : [String] {
// also any file with "menu" in the name
return [
"battle_disk.fsys",
"evolution.fsys",
"mewwaza.fsys",
"title.fsys",
"worldmap.fsys"
]
}
@objc func extractDecks() {
let deckData = XGFiles.fsys("deck_archive").fsysData
let decksOrdered : [XGDecks] = [.DeckBingo, .DeckColosseum, .DeckDarkPokemon, .DeckHundred, .DeckImasugu, .DeckSample, .DeckStory, .DeckVirtual]
for i in 0 ..< decksOrdered.count {
let deck = decksOrdered[i]
if !deck.file.exists {
if verbose {
printg("Extracting deck: \(deck.file.fileName)")
}
if let data = deckData.decompressedDataForFileWithIndex(index: i * 2) {
data.file = deck.file
data.save()
} else {
printg("Couldn't extract deck \(deck.file.fileName), doesn't exist in deck archive")
}
}
}
}
@objc func extractSpecificStringTables() {
stringsLoaded = false
let fightFile = XGFiles.msg("fight")
if !fightFile.exists {
let fight = XGFiles.fsys("fight_common").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
fight.file = fightFile
fight.save()
}
let pocket_menu = XGFiles.msg("pocket_menu")
let nameentrymenu = XGFiles.msg("name_entry_menu")
let system_tool = XGFiles.msg("system_tool")
let m3shrine1frl = XGFiles.msg("M3_shrine_1F_rl")
let relivehall_menu = XGFiles.msg("relivehall_menu")
let pda_menu = XGFiles.msg("pda_menu")
let p_exchange = XGFiles.msg("p_exchange")
let world_map = XGFiles.msg("world_map")
if !pocket_menu.exists {
let pm = XGFiles.fsys("pocket_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
pm.file = pocket_menu
pm.save()
}
if !nameentrymenu.exists {
let nem = XGFiles.fsys("pcbox_name_entry_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
nem.file = nameentrymenu
nem.save()
}
if !system_tool.exists {
let st = XGFiles.fsys("pcbox_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
st.file = system_tool
st.save()
}
if !m3shrine1frl.exists {
let m3rl = XGFiles.fsys("hologram_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
m3rl.file = m3shrine1frl
m3rl.save()
}
if !relivehall_menu.exists {
let rh = XGFiles.fsys("relivehall_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
rh.file = relivehall_menu
rh.save()
}
if !pda_menu.exists {
let pda = XGFiles.fsys("pda_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
pda.file = pda_menu
pda.save()
}
if !p_exchange.exists {
let pex = XGFiles.fsys("pokemonchange_menu").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
pex.file = p_exchange
pex.save()
}
if !world_map.exists {
let wm = XGFiles.fsys("worldmap").fsysData.decompressedDataForFileWithFiletype(type: .msg)!
wm.file = world_map
wm.save()
}
}
}
extension XGUtility {
class func importFsys() {
printg("importing files to .fsys archives")
if !DeckDataEmptyLZSS.file.exists {
DeckDataEmptyLZSS.save()
}
ISO.extractFSYS()
let common = XGFiles.fsys("common").fsysData
if XGFiles.common_rel.exists {
common.shiftAndReplaceFileWithIndexEfficiently(0, withFile: XGFiles.common_rel.compress(), save: false)
}
common.shiftAndReplaceFileWithIndexEfficiently(2, withFile: DeckDataEmptyLZSS.file, save: false) //EU file
if XGDecks.DeckDarkPokemon.file.exists {
common.shiftAndReplaceFileWithIndexEfficiently(4, withFile: XGFiles.deck(.DeckDarkPokemon).compress(), save: false)
}
common.save()
if XGFiles.tableres2.exists {
XGFiles.fsys("common_dvdeth").fsysData.shiftAndReplaceFileWithIndexEfficiently(0, withFile: XGFiles.tableres2.compress(), save: true)
}
let deckArchive = XGFiles.fsys("deck_archive").fsysData
for i in 0 ..< deckArchive.numberOfEntries {
// remove eu files to make space for increased file sizes assuming they're all the odd ones
if i % 2 == 1 {
deckArchive.shiftAndReplaceFileWithIndexEfficiently(i, withFile: DeckDataEmptyLZSS.file, save: false)
}
}
let decksOrdered : [XGDecks] = [.DeckBingo, .DeckColosseum, .DeckDarkPokemon, .DeckHundred, .DeckImasugu, .DeckSample, .DeckStory, .DeckVirtual]
for i in 0 ..< decksOrdered.count {
let deck = decksOrdered[i]
if deck.file.exists {
deckArchive.shiftAndReplaceFileWithIndexEfficiently(i * 2, withFile: deck.file.compress(), save: false)
}
}
deckArchive.save()
if XGFiles.msg("fight").exists {
XGFiles.fsys("fight_common").fsysData.shiftAndReplaceFileWithType(.msg, withFile: XGFiles.msg("fight").compress(), save: true)
}
if !XGFiles.fsys("pocket_menu").exists {
ISO.extractMenuFSYS()
}
if XGFiles.pocket_menu.exists {
XGFiles.fsys("pocket_menu").fsysData.shiftAndReplaceFileWithType(.rel, withFile: XGFiles.pocket_menu.compress(), save: true)
}
}
class func updateHealingItems() {
let kBattleItemDataStartOffset = 0x402570
let kSizeOfBattleItemEntry = 0x10
let kBattleItemHPToRestoreOffset = 0xA
// N.B. A lot of these values are bit masks and so a single item can have many of these effects at once
// byte 0x3 status to heal mask (0x20 sleep, 0x10 poison, 0x8 burn, 0x4 freeze, 0x2 paralysis, 0x1 confusion, 0x3F all)
// 0x40 level up, 0x80 guard spec
// byte 0x4 0x80 increase max pp, 0x40 revive, 0x20 restore pp, 0x10 evolution stone, 0x8 max out pp
// byte 0xb pp to restore (0x7f for all pp)
// bytes 5-7 friendship change
// byte 0xa hp to restore (0xfd hp gained by level up, 0xfe for half, 0xff for full)
// byte 0x8 hp evs
// byte 0x9 attack evs
// byte 0xc defense evs
// byte 0xd speed evs
// byte 0xe spdef evs
// byte 0xf spatk evs
// byte 0x0 0x20 dire hit, 0x2 x attack
// byte 0x1 0x10 x defend, 0x1 x speed
// byte 0x2 0x10 x accuracy, 0x1 x special
if let dol = XGFiles.dol.data {
let healingItems = [13,19,20,21,22,26,27,28,29,30,31,32,33,44,45,139,142]
for i in healingItems {
let item = XGItem(index: i)
if i >= 30 && i <= 33 {
if item.parameter == 0 {
// for some reason these are 0 by default in xd
// the following code uses it so let's set it manually
item.parameter = [50, 200, 0, 255][i - 30]
item.save()
}
}
let id = item.inBattleUseID
if id > 0 {
let offset = kBattleItemDataStartOffset + (kSizeOfBattleItemEntry * id)
dol.replaceByteAtOffset(offset + kBattleItemHPToRestoreOffset, withByte: item.parameter)
}
}
dol.save()
}
}
class func updateFunctionalItems() {
// Maybe I should stop being so pedantic and use shorter variable names...
let kFunctionalItemDataStartOffset = 0x402570
let kSizeOfFunctionalItemEntry = 0x10
let kFunctionalItemFirstFriendshipOffset = 0x5
if let dol = XGFiles.dol.data {
for i in allItemsArray() {
let item = i.data
let index = item.inBattleUseID
if index > 0 && index <= 63 {
let start = kFunctionalItemDataStartOffset + (index * kSizeOfFunctionalItemEntry) + kFunctionalItemFirstFriendshipOffset
for i in 0 ... 2 {
dol.replaceByteAtOffset(start + i, withByte: item.friendshipEffects[i])
}
}
}
dol.save()
}
}
class func updateValidItems() {
let itemListStart = CommonIndexes.ValidItems.startOffset
let rel = XGFiles.common_rel.data!
printg("Updating items list...")
for i in 1 ..< CommonIndexes.NumberOfItems.value {
let currentOffset = itemListStart + (i * 2)
if XGItems.item(i).nameID != 0 {
rel.replace2BytesAtOffset(currentOffset, withBytes: i)
} else {
rel.replace2BytesAtOffset(currentOffset, withBytes: 0)
}
}
rel.save()
}
class func updateShadowMoves() {
let rel = XGFiles.common_rel.data!
for i in 0 ..< kNumberOfMoves {
let m = XGMoves.move(i).data
if m.isShadowMove {
rel.replaceWordAtOffset(m.startOffset + 0x14, withBytes: 0x00023101)
}
}
rel.save()
}
class func prepareShinyness() {
if isShinyAvailable {
for mon in XGDecks.DeckStory.allActivePokemon {
if mon.isShadowPokemon {
mon.shinyness = .random
} else {
mon.shinyness = mon.shinyness == .always ? .always : .never
}
mon.save()
}
}
}
class func prepareForQuickCompilation() {
updateValidItems()
updateHealingItems()
updateFunctionalItems()
updateTutorMoves()
updatePokeSpots()
updateShadowMonitor()
importSpecificStringTables()
}
class func prepareForCompilation() {
fixUpTrainerPokemon()
prepareShinyness()
updateShadowMoves()
updateValidItems()
updateHealingItems()
updateFunctionalItems()
updateTutorMoves()
updatePokeSpots()
updateShadowMonitor()
importSpecificStringTables()
}
class func importSpecificStringTables() {
printg("importing menu string tables")
let pocket_menu = XGFiles.msg("pocket_menu")
let d4tower1f3 = XGFiles.msg("D4_tower_1F_3")
let m5labo2f = XGFiles.msg("M5_labo_2F")
let d4tower1f2 = XGFiles.msg("D4_tower_1F_2")
let nameentrymenu = XGFiles.msg("name_entry_menu")
let system_tool = XGFiles.msg("system_tool")
let m3shrine1frl = XGFiles.msg("M3_shrine_1F_rl")
let relivehall_menu = XGFiles.msg("relivehall_menu")
let pda_menu = XGFiles.msg("pda_menu")
let d2pc1f = XGFiles.msg("D2_pc_1F")
let d7out = XGFiles.msg("D7_out")
let p_exchange = XGFiles.msg("p_exchange")
let world_map = XGFiles.msg("world_map")
let fight = XGFiles.msg("fight")
let msgs = [pocket_menu, d4tower1f2, d4tower1f3, m5labo2f, nameentrymenu, system_tool, m3shrine1frl, relivehall_menu, pda_menu, d2pc1f, d7out, p_exchange, world_map, fight]
for file in XGFolders.MenuFSYS.files + [.fsys("fight_common"), .fsys("field_common")] where file.fileType == .fsys {
let fsys = file.fsysData
for msg in msgs {
if msg.exists {
for i in 0 ..< fsys.numberOfEntries {
if fsys.fileTypeForFile(index: i) == .msg && fsys.fileNameForFileWithIndex(index: i).removeFileExtensions() == msg.fileName.removeFileExtensions() {
fsys.shiftAndReplaceFileWithIndexEfficiently(i, withFile: msg.compress(), save: false)
}
}
}
}
fsys.save()
}
}
class func patchDol() {
let patches = [0,2,3,6,7]
for i in patches {
XGDolPatcher.applyPatch(XGDolPatches(rawValue: i)!)
}
}
class func renameZaprong(newName: String) {
let offset = 0x420904
let dol = XGFiles.dol.data!
dol.replaceBytesFromOffset(offset, withByteStream: newName.map({ (c) -> Int in
let charScalar = String(c).unicodeScalars
let charValue = Int(charScalar[charScalar.startIndex].value)
return charValue
}))
dol.save()
}
//MARK: - Obtainable pokemon
class func obtainablePokemon() -> [XGPokemon] {
var pokes = [XGPokemon]()
for i in 1 ..< XGDecks.DeckDarkPokemon.DDPKEntries {
let p = XGDeckPokemon.ddpk(i).pokemon
pokes.append(p)
}
pokes.append(XGTradeShadowPokemon().species)
for i in 0 ..< 2 {
pokes.append(XGDemoStarterPokemon(index: i).species)
}
for i in 0 ..< 3 {
pokes.append(XGMtBattlePrizePokemon(index: i).species)
}
for i in 0 ..< 4 {
pokes.append(XGTradePokemon(index: i).species)
}
for s in 0 ... 2 {
let spot = XGPokeSpots(rawValue: s)!
for p in 0 ..< spot.numberOfEntries {
pokes.append(XGPokeSpotPokemon(index: p, pokespot: spot).pokemon)
}
}
for p in pokes {
for e in XGPokemonStats(index: p.index).evolutions {
let evo = XGPokemon.pokemon(e.evolvesInto)
pokes.append(evo)
for ev in XGPokemonStats(index: evo.index).evolutions {
let evol = XGPokemon.pokemon(ev.evolvesInto)
pokes.append(evol)
}
}
}
var obts = [XGPokemon]()
for p in pokes {
if !(obts.contains{ $0.index == p.index }) {
obts.append(p)
}
}
return obts
}
class func saveObtainablePokemon() {
let k = obtainablePokemon().sorted{$0 < $1}.filter{$0.index != 0}
var s = "XG Obtainable Pokemon:\t\(k.count)\n"
for p in k {
s += "\(p.index):\t\(p)\n"
}
saveString(s, toFile: .nameAndFolder("output/obtainable pokemon.txt",.Documents))
}
class func saveObtainableTypes() {
var pokes = [XGPokemon]()
for i in 0 ..< XGDecks.DeckDarkPokemon.DDPKEntries {
let p = XGDeckPokemon.ddpk(i).pokemon
pokes.append(p)
}
pokes.append(XGTradeShadowPokemon().species)
for i in 0 ..< 2 {
pokes.append(XGDemoStarterPokemon(index: i).species)
}
for i in 0 ..< 3 {
pokes.append(XGMtBattlePrizePokemon(index: i).species)
}
for i in 0 ..< 4 {
pokes.append(XGTradePokemon(index: i).species)
}
for s in 0 ... 2 {
let spot = XGPokeSpots(rawValue: s)!
for p in 0 ..< spot.numberOfEntries {
pokes.append(XGPokeSpotPokemon(index: p, pokespot: spot).pokemon)
}
}
var typesCount = [Int]()
for _ in 0...17 {
typesCount.append(0)
}
for i in pokes {
guard i.index != 0 else {
continue
}
let poke = XGPokemonStats(index: i.index)
let t1 = poke.type1.rawValue
let t2 = poke.type2.rawValue
typesCount[t1] = typesCount[t1] + 1
if t2 != t1 {
typesCount[t2] = typesCount[t2] + 1
}
}
var string = "XG Obtainable types count:\n"
for i in 0...17 {
let name = XGMoveTypes(rawValue: i)!.name
string += name
string += "\t:\t"
string += "\(typesCount[i])\n"
}
saveString(string, toFile: XGFiles.nameAndFolder("XG Obtainable Types Count.txt", XGFolders.Reference))
}
class func saveObtainablePokemonByLocation(prefix: String) {
func tabsForName(_ name: String) -> String {
return name.length >= 9 ? "\t" : (name.length >= 3 ? "\t\t" : "\t\t\t")
}
var string = "Pokemon XD Obtainable Pokemon by Location:\n\n"
string += "\n************************************************************************************\nStarter Pokemon\n\n"
for i in 0 ..< 2 {
let pokemon = XGDemoStarterPokemon(index: i)
string += pokemon.species.name.string + "\t\(tabsForName(pokemon.species.name.string))Lv. " + "\(pokemon.level)" + "\n"
}
string += "\n************************************************************************************\nShadow Pokemon" + " \(XGDecks.DeckDarkPokemon.allActivePokemon.count)" + "\n\n"
var shadows = [Int]()
for trainer in XGDecks.DeckStory.allTrainers {
if !trainer.hasShadow {
continue
}
var str = trainer.trainerString
var range = str.startIndex ..< str.index(str.startIndex, offsetBy: 1)
let sub1 = str.substring(from: 0, to: 1)
if str == "NULL" {
str = "-"
} else if str.range(of: "Opening", options: [], range: nil, locale: nil) != nil {
str = str.replacingOccurrences(of: "_p", with: " Player Team", options: [], range: nil)
str = str.replacingOccurrences(of: "_e", with: " Enemy Team", options: [], range: nil)
str = str.replacingOccurrences(of: "Opening", with: "Opening Battle ", options: [], range: nil)
} else if str.range(of: "Vdisk", options: [], range: nil, locale: nil) != nil {
str = str.replacingOccurrences(of: "_p", with: " Player Team", options: [], range: nil)
str = str.replacingOccurrences(of: "_e", with: " Enemy Team", options: [], range: nil)
str = str.replacingOccurrences(of: "Vdisk", with: "Battle CD ", options: [], range: nil)
} else if sub1 == "N" {
str = str.replacingCharacters(in: range, with: "Mt.Battle Zone ")
} else if sub1 == "P" {
str = str.replacingCharacters(in: range, with: "Pyrite Colosseum ")
} else if sub1 == "O" {
str = str.replacingCharacters(in: range, with: "Orre Colosseum ")
} else if sub1 == "T" {
str = str.replacingCharacters(in: range, with: "Tower Colosseum ")
} else {
range = str.startIndex ..< str.index(str.startIndex, offsetBy: 2)
let sub = str.substring(from: 0, to: 2)
str = XGMaps(rawValue: sub)?.name ?? str
}
str = str.replacingOccurrences(of: "_col_", with: " Colosseum ", options: [], range: nil)
str = str.replacingOccurrences(of: "555_", with: "Battle ", options: [], range: nil)
str = str.replacingOccurrences(of: "Esaba", with: "Pokespot", options: [], range: nil)
str = str.replacingOccurrences(of: "_", with: " ", options: [], range: nil)
str = str.replacingOccurrences(of: "Mirabo", with: "Miror B.", options: [], range: nil)
str = str.replacingOccurrences(of: "mirabo", with: "Miror B.", options: [], range: nil)
str = str.replacingOccurrences(of: "haihu", with: "Gift", options: [], range: nil)
let location = str
for poke in trainer.pokemon {
if poke.isShadow {
if !shadows.contains(poke.index) {
shadows.append(poke.index)
let pokemon = poke.data
let name = trainer.trainerClass.name.string.replacingOccurrences(of: "[07]{00}", with: "") + " " + trainer.name.string
var spaces = " "
for i in 1 ... 20 {
if i > name.length {
spaces += " "
}
}
string += "Shadow " + pokemon.species.name.string + "\(tabsForName(pokemon.species.name.string))Lv. " + String(format: "%02d+\t\t", pokemon.level)
string += name + spaces + "\t\t\t" + location + "\n"
}
}
}
}
string += "\n************************************************************************************\nPokespots\n\n"
string += "----------------------------------\n"
string += "Rock Pokespot:\n"
for i in 0 ..< XGPokeSpots.rock.numberOfEntries {
let poke = XGPokeSpotPokemon(index: i, pokespot: .rock)
string += "Wild " + poke.pokemon.name.string + " \t\(tabsForName(poke.pokemon.name.string))Lv. " + "\(poke.minLevel) - \(poke.maxLevel)" + "\n"
}
string += "----------------------------------\n"
string += "Oasis Pokespot:\n"
for i in 0 ..< XGPokeSpots.oasis.numberOfEntries {
let poke = XGPokeSpotPokemon(index: i, pokespot: .oasis)
string += "Wild " + poke.pokemon.name.string + " \t\(tabsForName(poke.pokemon.name.string))Lv. " + "\(poke.minLevel) - \(poke.maxLevel)" + "\n"
}
string += "----------------------------------\n"
string += "Cave Pokespot:\n"
for i in 0 ..< XGPokeSpots.cave.numberOfEntries {
let poke = XGPokeSpotPokemon(index: i, pokespot: .cave)
string += "Wild " + poke.pokemon.name.string + " \t\(tabsForName(poke.pokemon.name.string))Lv. " + "\(poke.minLevel) - \(poke.maxLevel)" + "\n"
}
if XGPokeSpots.all.numberOfEntries > 2 {
string += "\n************************************************************************************\nBoss Encounters\n\n"
for i in 2 ..< XGPokeSpots.all.numberOfEntries {
let poke = XGPokeSpotPokemon(index: i, pokespot: .all)
if poke.pokemon.index > 0 {
string += "Wild " + poke.pokemon.name.string + " \t\(tabsForName(poke.pokemon.name.string))Lv. " + "\(poke.minLevel) - \(poke.maxLevel)" + "\n"
}
}
}
string += "\n************************************************************************************\nHordel Trade\n\n"
let pokemon = XGTradePokemon(index: 0)
string += "Hordel's " + pokemon.species.name.string + "\t\(tabsForName(pokemon.species.name.string))Lv. " + "\(pokemon.level)" + "\n"
string += "\n************************************************************************************\nDuking Trades\n\n"
for i in 1 ..< 4 {
let pokemon = XGTradePokemon(index: i)
string += "Duking's " + pokemon.species.name.string + "\(tabsForName(pokemon.species.name.string))Lv. " + "\(pokemon.level)" + "\n"
}
string += "\n************************************************************************************\nMt. Battle Prizes\n\n"
for i in 0 ..< 3 {
let pokemon = XGMtBattlePrizePokemon(index: i)
string += "Mt. Battle " + pokemon.species.name.string + "\t\(tabsForName(pokemon.species.name.string))Lv. " + "\(pokemon.level)" + "\n"
}
saveString(string, toFile: XGFiles.nameAndFolder(prefix + "Obtainable Pokemon List.txt", XGFolders.Reference))
}
class func updateShadowMonitor() {
printg("updating shadow monitor")
let start = 0x4014EA
var indices = [Int]()
let deck = XGDecks.DeckDarkPokemon
var pokes = [XGDeckPokemon]()
for i in 1..<deck.DDPKEntries {
pokes.append(.ddpk(i))
}
for poke in pokes {
if poke.pokemon.index != 0 {
indices.append(poke.pokemon.index)
} else {
indices.append(0x118)
}
}
let dol = XGFiles.dol.data!
dol.replaceBytesFromOffset(start, withShortStream: indices)
dol.save()
}
class func updateTutorMoves() {
// Need to update class function which determines which pokemon stats move tutor move index
// Corresponds to each tutor move. This class function will set it so that it is in the same
// Order as the tutor moves indexes.
printg("updating tutor moves")
let dol = XGFiles.dol.data!
let offsets = [
0x1C2ECA, // 0
0x1C2EBE, // 1
0x1C2ED6, // 2
0x1C2EEE, // 3
0x1C2EA6, // 4
0x1C2EB2, // 5
0x1C2F06, // 6
0x1C2F2A, // 7
0x1C2F1E, // 8
0x1C2F12, // 9
0x1C2EE2, // 10
0x1C2EFA, // 11
]
for i in 1...12 {
let tmove = XGTMs.tutor(i).move
let offset = offsets[i-1]
dol.replaceWordAtOffset(offset + 6, withBytes: kNopInstruction)
dol.replace2BytesAtOffset(offset, withBytes: tmove.index)
}
dol.save()
}
class func updatePokeSpots() {
// Need to change array values at bottom of m2 guild script.
// You need the national dex index of the pokemon you trade.
// Then replace the values with 1000 + the national dex index.
// I.E. the pokemon's name ID.
// Wooper is 1194 0x4aa, trapinch is 1328 0x530, surskit is 1283 0x503.
// Also need to get national ids for the pokemon you get from trade.
// Now that the xds script editor is done it's not very reliable to hardcode the offsets
// Instead the xds scripts should be edited and compiled.
// However for those making simple modifications or using the randomiser
// it may still be preferrable to hard code if the script file
// won't be tampered with.
// I'm using the file size as the benchmark as it's really unlikely to remain the same after an xds compilation
// since the compiler strips all debug data. If it happens to be the same length
// after editing then oh well, unlucky mate :-p
printg("updating pokespots")
let scriptFile = XGFiles.scd("M2_guild_1F_2.scd")
if scriptFile.exists {
if scriptFile.fileSize == 0x1770 {
let script = scriptFile.data!
let trapinches = [0x0B4A,0x0D1E]
let surskits = [0x0B9A,0x0D62]
let woopers = [0x0BEA,0x0DA6]
let trapinch = XGPokeSpotPokemon(index: 2, pokespot: .rock).pokemon.nameID
let surskit = XGPokeSpotPokemon(index: 2, pokespot: .oasis).pokemon.nameID
let wooper = XGPokeSpotPokemon(index: 2, pokespot: .cave).pokemon.nameID
for offset in trapinches {
script.replace2BytesAtOffset(offset, withBytes: trapinch)
}
for offset in surskits {
script.replace2BytesAtOffset(offset, withBytes: surskit)
}
for offset in woopers {
script.replace2BytesAtOffset(offset, withBytes: wooper)
}
script.replace2BytesAtOffset(0x0D3A, withBytes: trapinch)
script.replace2BytesAtOffset(0x1756, withBytes: trapinch)
script.replace2BytesAtOffset(0x0D7E, withBytes: surskit)
script.replace2BytesAtOffset(0x175E, withBytes: surskit)
script.replace2BytesAtOffset(0x0DC2, withBytes: wooper)
script.replace2BytesAtOffset(0x1766, withBytes: wooper)
let meditite = XGTradePokemon(index: 1).species.nameID
let shuckle = XGTradePokemon(index: 2).species.nameID
let larvitar = XGTradePokemon(index: 3).species.nameID
script.replace2BytesAtOffset(0x0D32, withBytes: meditite)
script.replace2BytesAtOffset(0x0D76, withBytes: shuckle)
script.replace2BytesAtOffset(0x0DBA, withBytes: larvitar)
script.save()
}
}
}
class func printOccurencesOfMove(search: XGMoves) {
if search.index == 0 {
return
}
for deck in TrainerDecksArray {
for trainer in deck.allTrainers {
for mon in trainer.pokemon.map({ (p) -> XGTrainerPokemon in
return p.data
}) {
if mon.moves.contains(where: { (m) -> Bool in
return m.index == search.index
}) {
printg(trainer.index,trainer.name, trainer.locationString)
} else if mon.shadowMoves.contains(where: { (m) -> Bool in
return m.index == search.index
}) {
printg(trainer.index,trainer.name, trainer.locationString)
}
}
}
}
for i in 1 ..< kNumberOfPokemon {
let mon = XGPokemonStats(index: i)
if mon.levelUpMoves.contains(where: { (m) -> Bool in
return m.move.index == search.index
}) {
printg(mon.name)
}
}
for i in 0 ..< kNumberOfBingoCards {
let card = XGBattleBingoCard(index: i)
for panel in card.panels {
switch panel {
case .pokemon(let mon):
if mon.move.index == search.index {
printg("Battle Bingo Card ", i)
}
default:
break
}
}
}
}
class func printOccurencesOfPokemon(search: XGPokemon) {
for deck in TrainerDecksArray {
for trainer in deck.allTrainers {
for mon in trainer.pokemon.map({ (p) -> XGTrainerPokemon in
return p.data
}) {
if mon.species.index == search.index {
printg(trainer.name, trainer.locationString)
}
}
}
}
}
class func documentObtainablePokemonStats(title: String, forXG: Bool) {
var indices = [String]()
var hexIndices = [String]()
var names = [String]()
var type1s = [String]()
var type2s = [String]()
var ability1s = [String]()
var ability2s = [String]()
var atks = [String]()
var defs = [String]()
var spas = [String]()
var spds = [String]()
var spes = [String]()
var hps = [String]()
var item1s = [String]()
var item2s = [String]()
var levelRates = [String]()
var baseExps = [String]()
var catchRates = [String]()
var happinesses = [String]()
var genderRatios = [String]()
// vertical only
var lumHeaders = [String]()
var levelUpMoves = [ [String] ]()
var TMHeaders = [String]()
var TMs = [ [String] ]()
var tutorHeaders = [String]()
var tutors = [ [String] ]()
for _ in 0 ..< kNumberOfLevelUpMoves {
levelUpMoves.append([String]())
}
for _ in 0 ..< kNumberOfTMs {
TMs.append([String]())
}
for _ in 0 ..< kNumberOfTutorMoves {
tutors.append([String]())
}
for poke in obtainablePokemon().sorted(by: { (p1, p2) -> Bool in
return p1.index < p2.index
}) {
let mon = poke.stats
indices.append(poke.index.string)