/
dwrandomizer.py
executable file
·1001 lines (875 loc) · 34.6 KB
/
dwrandomizer.py
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
#!/usr/bin/env python3
import argparse
import random
import sys
import hashlib
import struct
import math
from worldmap import WorldMap,MapGrid
import ips
VERSION = "1.1"
#sha1sums of various roms
prg0sums = ['6a50ce57097332393e0e8751924fd56456ef083c', #Dragon Warrior (U) (PRG0) [!].nes
'66330df6fe3e3c85adb8183721e5f88c149e52eb', #Dragon Warrior (U) (PRG0) [b1].nes
'49974889619f1d8c39b6c20fa208c62a0a73ecce', #Dragon Warrior (U) (PRG0) [b1][o1].nes
'd98b8a3fc93bb2f1f5016326556b68998dd5f85d', #Dragon Warrior (U) (PRG0) [b2].nes
'e81a693efe322be9584c97b55c6d7ae38ae44a66', #Dragon Warrior (U) (PRG0) [o1].nes
'6e1a52b7b3a13494536bbab7248690861665001a', #Dragon Warrior (U) (PRG0) [o2].nes
'3077d5bd5c5c3744398b122d5ee1bba7055c8d45'] #Dragon Warrior (U) (PRG0) [o3].nes
prg1sums = ['1ecc63aaac50a9612eaa8b69143858c3e48dd0ae'] #Dragon Warrior (U) (PRG1) [!].nes
class Rom:
# Slices for various data. Offsets include iNES header.
# alphabet - 0x5f is breaking space, 60 is non-breaking (I think)
alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + \
"__'______.,-_?!_)(_______________ "
token_dialogue_slice = slice(0xa238, 0xa299)
will_not_work_slice = slice(0xad95, 0xadad)
chest_content_slice = slice(0x5de0, 0x5e59, 4)
enemy_stats_slice = slice(0x5e5b, 0x60db)
warps_slice = slice(0xf3d8, 0xf50b)
zones_slice = slice(0xf55f, 0xf5c3)
mp_req_slice = slice(0x1d63, 0x1d6d) #mp requirements for each spell
xp_req_slice = slice(0xf36b, 0xf3a7)
player_stats_slice = slice(0x60dd, 0x6191)
weapon_shop_inv_slice = slice(0x19a1, 0x19cc)
new_spell_slice = slice(0xeaf9, 0xeb1f, 4)
chests_slice = slice(0x5ddd, 0x5e59)
title_text_slice = slice(0x3f36, 0x3fc5)
token_slice = slice(0xe11e, 0xe12b, 6)
flute_slice = slice(0xe15d, 0xe16a, 6)
armor_slice = slice(0xe173, 0xe180, 6)
encounter_1_slice = slice(0xcd64, 0xcd71, 6) # axe knight
encounter_2_slice = slice(0xcd7b, 0xcd88, 6) # green dragon
encounter_3_slice = slice(0xcd98, 0xcda5, 6) # golem
encounter_1_run_slice = slice(0xe8e7, 0xe8f4, 6) # axe knight
encounter_2_run_slice = slice(0xe90e, 0xe91b, 6) # green dragon
encounter_3_run_slice = slice(0xe93b, 0xe948, 6) # golem
encounter_enemies_slice = slice(0xcd74, 0xcdaf, 29)
encounter_2_kill_slice = slice(0xe97e, 0xe985, 6) # green dragon
encounter_3_kill_slice = slice(0xe990, 0xe997, 6) #golem
# patch format: patch[addr] = (step, data...)
patches = {}
def __init__(self, filename):
input_file = open(filename, 'rb')
self.rom_data = bytearray(input_file.read())
input_file.close()
self.revert()
def sha1(self, data=None):
"""
Returns a sha1 checksum of the rom data
"""
if data:
return hashlib.sha1(data).hexdigest()
else:
return hashlib.sha1(self.rom_data).hexdigest()
def verify_checksum(self):
"""
Verifies the checksum of the input ROM against known ROM checksums.
"""
digest = self.sha1()
if digest in prg0sums:
return 0
elif digest in prg1sums:
return 1
return False
def ascii2dw(self, text):
"""
Converts an ascii string to bytes suitable for insertion into the ROM.
:Parameters:
text : string
The text to be converted.
rtype: bytearray
return: Dialoge bytes suitable for insertion into the ROM.
"""
return bytearray([self.alphabet.find(i) for i in text])
def dw2ascii(self, dialogue):
"""
Converts a string of bytes from the ROM to ascii text.
:Parameters:
dialogue : bytearray
The bytes from the ROM to be converted.
rtype: string
return: The ascii text after conversion.
"""
return ''.join([self.alphabet[i] for i in list(dialogue)])
def shuffle_chests(self):
"""
Shuffles the contents of all chests in the game. Checks are used to ensure no
quest items are located in Charlock chests.
"""
chest_contents = self.chests[3::4]
for i in range(len(chest_contents)):
# replace the harp with a key.
if chest_contents[i] == 0xd:
chest_contents[i] = 3
# change all gold to large gold stash
if (chest_contents[i] >= 18 and chest_contents[i] <= 20):
chest_contents[i] = 21
# 50/50 chance to have erdrick's token in a chest. If not, large gold.
if chest_contents[i] == 0x17:
if random.randint(0,1):
self.token_loc[0] = 0 # remove token from the ground
chest_contents[i] = 10 # put it in a chest
else:
chest_contents[i] = 3 # else put in a key.
random.shuffle(chest_contents)
# make sure required quest items aren't in Charlock
charlock_chests = chest_contents[11:17] + chest_contents[24:25]
charlock_chest_indices = (11, 12, 13, 14, 15, 16, 24)
quest_items = (10, 13, 15, 16)
for item in quest_items:
if (item in charlock_chests):
for i in charlock_chest_indices:
if chest_contents[i] == item:
# this could probably be cleaner...
j = self.non_charlock_chest()
while chest_contents[j] in quest_items:
j = self.non_charlock_chest()
chest_contents[j], chest_contents[i] = chest_contents[i], chest_contents[j]
# make sure there's a key in the throne room
if not (3 in chest_contents[4:7]):
for i in range(len(chest_contents)):
if chest_contents[i] == 3:
j = random.randint(4, 6)
# if key is in charlock and chest[j] contains a quest item, try again
while (i in charlock_chest_indices and (chest_contents[j] in quest_items)):
j = random.randint(4, 6)
chest_contents[j], chest_contents[i] = chest_contents[i], chest_contents[j]
break
# make sure staff of rain guy doesn't have the stones (potentially unwinnable)
# if (chest_contents[19] == 15):
# j = self.non_charlock_chest()
# # don't remove the key from the throne room
# while (j == 19 or (j in (4, 5, 6) and chest_contents[j] == 3)):
# j = self.non_charlock_chest()
# chest_contents[19],chest_contents[j] = chest_contents[j],chest_contents[19]
self.chests[3::4] = chest_contents
def non_charlock_chest(self):
"""
Gives a random index of a chest that is not in Charlock
rtype: int
return: A chest index that is not in Charlock.
"""
chest = random.randint(0, 23)
# avoid 11-16 and chest 24 (they are in charlock)
chest = chest + 6 if (chest > 10) else chest
chest = chest + 1 if (chest > 23) else chest
return chest
def randomize_attack_patterns(self, ultra=False):
"""
Randomizes attack patterns of enemies (whether or not they use spells/fire
breath).
:Parameters:
ultra : bool
Whether or not to apply ultra randomization
"""
new_patterns = [] # attack patterns
new_ss_resist = self.enemy_stats[4::16]
for i in range(38):
new_ss_resist[i] |= 0xf
if random.randint(0,1): # 50/50 chance
resist = random.randint(0,round(i/5))
new_ss_resist[i] &= (0xf0 | resist)
if ultra:
new_patterns.append((random.randint(0,255)))
else:
if i <= 20:
# heal, sleep, stopspell, hurt
new_patterns.append((random.randint(0,11) << 4) | random.randint(0,3))
elif i < 30:
# healmore, heal, sleep, stopspell, fire breath, hurtmore
new_patterns.append((random.randint(0,15) << 4) | random.randint(4, 11))
else:
# healmore, sleep, stopspell, strong fire breath, fire breath, hurtmore
#we'll be nice and not give Axe Knight Dragonlord's breath.
slot2 = random.randint(4, 11) if i == 33 else random.randint(4, 15)
new_patterns.append((random.choice((0, 1, 3)) << 6) |
(random.randint(0, 3) << 4) | slot2)
else:
new_patterns.append(0)
new_patterns.append(87) #Dragonlord form 1
new_patterns.append(14) #Dragonlord form 2
self.enemy_stats[3::16] = bytearray(new_patterns)
self.enemy_stats[4::16] = bytearray(new_ss_resist)
def shuffle_towns(self):
"""
Shuffles the locations of towns on the map.
"""
self.owmap.shuffle_warps()
def generate_map(self):
"""
Generates a new overworld map.
"""
if not self.owmap.generate():
self.owmap.revert()
return False
# Stick encounter 3 in Charlock for now...
self.encounter_3_loc = (6, 25, 22)
self.encounter_3_kill[1] = self.encounter_3_loc[0]
# Let's not remember killing it...
self.encounter_3_kill[1] = 0
return True
def randomize_zones(self, ultra=False):
"""
Randomizes which enemies are present in each zone.
:Parameters:
ultra : bool
Whether or not to apply ultra randomization
"""
new_zones = list()
if ultra:
# zone 0-1
for i in range(0,10):
new_zones.append(random.randint(0, 6))
# zones 2-15
for i in range(0,70):
new_zones.append(random.randint(0, 37))
# zones 16-18 (Charlock)
for i in range(0,15):
new_zones.append(random.randint(29,37))
# zone 19
for i in range(0,5):
new_zones.append(random.randint(0, 37))
# randomize forced encounter enemies:
# Golem, Axe Knight, Blue Dragon, Stoneman, Armored Knight, Red Dragon
for i in range(3):
self.encounter_enemies[i] = random.choice((24, 33, 34, 35, 36, 37))
self.encounter_2_kill[0] = self.encounter_enemies[1]
self.encounter_3_kill[0] = self.encounter_enemies[2]
else:
#zones 2-19
for j in range(5):
new_zones.append(int(random.randint(0, 6)/2))
# zones 1-13 (Overworld)
for i in range(1, 14):
for j in range(5):
enemy = random.randint(i * 2 - 2, min(37,round(i*3)))
while enemy == 24: # don't add golem
enemy = random.randint(i * 2 - 2, min(37,round(i*3)))
new_zones.append(enemy)
#zone 14 - Garin's Grave?
for j in range(5):
new_zones.append(random.randint(7, 17))
#zone 15 - Lower Garin's Grave
for j in range(5):
new_zones.append(random.randint(15, 23))
# zone 16-18 - Charlock
for i in range(16, 19):
for j in range(5):
new_zones.append(random.randint(13+i, 37))
# zone 19 - Rimuldar Tunnel
for j in range(5):
new_zones.append(random.randint(3, 11))
self.zones = new_zones
def randomize_shops(self):
"""
Randomizes the items available in each weapon shop
"""
weapons = (0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16)
new_shop_inv = []
# add 5 items to each shop
for i in range(7):
this_shop = []
while(len(this_shop) < 5):
new_item = random.choice(weapons)
if not new_item in this_shop:
this_shop.append(new_item)
new_shop_inv.append(this_shop)
# add an extra item to one shop since we have 36 slots.
six_item_shop = random.randint(0, 6)
new_item = random.choice(weapons)
while new_item in new_shop_inv[six_item_shop]:
new_item = random.choice(weapons)
new_shop_inv[six_item_shop].append(new_item)
# create the bytearray to insert into the rom. Shops are separated by an
# 0xfd byte
self.shop_inventory = []
for shop in new_shop_inv:
shop.sort()
self.shop_inventory += shop + [0xfd]
def shuffle_searchables(self):
"""
Shuffles the 3 searchable items in the game. (E.Armor, F.Flute, E.Token)
"""
# move the token, then shuffle
tantegel = self.owmap.warps_from[self.owmap.tantegel_warp][1:3]
grid = MapGrid(self.owmap.grid)
new_token_loc = self.owmap.accessible_land(grid, tuple(tantegel))
self.token_loc[1:3] = new_token_loc
searchables = [self.token_loc, self.flute_loc, self.armor_loc]
random.shuffle(searchables)
self.token_loc = searchables[0]
self.flute_loc = searchables[1]
self.armor_loc = searchables[2]
def randomize_growth(self, ultra=False):
"""
Randomizes player growth.
:Parameters:
ultra : bool
Whether or not to apply ultra randomization.
"""
player_str = list(self.player_stats[0:180:6])
player_agi = list(self.player_stats[1:180:6])
player_hp = list(self.player_stats[2:180:6])
player_mp = list(self.player_stats[3:180:6])
if ultra:
# set these to make DL normally beatable at lvl 15
player_str = inverted_power_curve( 4, 155, 1.275)
player_agi = inverted_power_curve( 4, 145, 1.225)
player_hp = inverted_power_curve(10, 230, 0.93 )
player_mp = inverted_power_curve( 0, 220, 0.965)
else:
for i in range(len(player_str)):
player_str[i] = round(player_str[i] * random.uniform(0.8, 1.2))
for i in range(len(player_agi)):
player_agi[i] = round(player_agi[i] * random.uniform(0.8, 1.2))
for i in range(len(player_hp)):
player_hp[i] = round(player_hp[i] * random.uniform(0.8, 1.2))
for i in range(len(player_mp)):
player_mp[i] = round(player_mp[i] * random.uniform(0.8, 1.2))
player_str.sort()
player_agi.sort()
player_hp.sort()
player_mp.sort()
self.player_stats[0:180:6] = player_str
self.player_stats[1:180:6] = player_agi
self.player_stats[2:180:6] = player_hp
self.player_stats[3:180:6] = player_mp
def randomize_spell_learning(self, ultra=False):
"""
Randomizes the level at which spells are learned.
:Parameters:
ultra : bool
Whether or not to use ultra randomization.
"""
self.move_repel() # in case this hasn't been called yet.
# choose the levels for new spells
if ultra:
for i in range(len(self.new_spell_levels)):
self.new_spell_levels[i] = random.randint(0, 16)
else:
for i in range(len(self.new_spell_levels)):
self.new_spell_levels[i] += random.randint(-2, 2)
self.update_spell_masks()
def update_spell_masks(self):
"""
Updates the spell masks to be written to the rom, based on the data in
player_stats.
"""
new_masks = []
player_mp = self.player_stats[3::6]
#adjust the spell masks to fit the new levels
for i in range(30):
mask = 0
for j in range(len(self.new_spell_levels)):
if self.new_spell_levels[j] <= i+1:
mask |= 1 << j
new_masks.append(mask)
if mask: # give the player at least 6 mp if they have a spell to use.
player_mp[i] = max(6, player_mp[i])
mask1 = []
mask2 = []
# split the masks for use in the rom
for mask in new_masks:
mask1.append(mask & 0xff)
mask2.append(mask >> 8)
self.player_stats[3::6] = player_mp
self.player_stats[5::6] = mask1
self.player_stats[4::6] = mask2
def update_drops(self):
"""
Raises enemy XP and gold drops to those of the remakes.
"""
remake_xp = [ 1, 2, 3, 4, 8, 12, 16, 14, 15, 18, 20, 25, 28,
31, 40, 42,255, 47, 52, 58, 58, 64, 70, 72,255, 6,
78, 83, 90,120,135,105,120,130,180,155,172,255, 0, 0]
# These are +1 for proper values in the ROM
remake_gold=[ 3, 5, 7, 9, 17, 21, 26, 22, 20, 31, 26, 43, 51,
49, 61, 63, 7, 76, 81, 96,111,106,111,121, 11,255,
151,136,149,186,161,170,186,166,151,149,153,144, 0, 0]
self.enemy_stats[6::16] = bytearray(remake_xp)
self.enemy_stats[7::16] = bytearray(remake_gold)
def update_mp_reqs(self, ultra=False):
"""
Lowers the MP requirements of spells to that of the remake
"""
#magic required for each spell, in order
self.mp_reqs = [3, 2, 2, 2, 2, 6, 8, 2, 8, 5]
# if ultra:
# for i in range(10):
# self.mp_reqs[i] = random.randint(1, 8)
def lower_xp_reqs(self, ultra=False):
"""
Lowers the XP requirements for a faster game.
"""
xp = struct.unpack( "<HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH", self.xp_reqs )
if ultra:
xp = [round(x * 0.5) for x in xp]
else:
xp = [round(x * 0.75) for x in xp]
self.xp_reqs = struct.pack( "<HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH", *xp )
def update_enemy_hp(self):
"""
Updates HP of enemies to that of the remake, where possible.
"""
#dragonlord hp should be 204 and 350, but we want him beatable at lv 18
remake_hp = [ 2, 3, 5, 7, 12, 13, 13, 22, 26, 43, 16, 24, 28,
18, 33, 39, 3, 48, 37, 35, 44, 37, 40, 40,153,110,
47, 48, 38, 70, 72, 74, 65, 67, 98,135, 99,106,100,165]
# randomize Dragonlord's second form HP somewhat
remake_hp[-1] -= random.randint(0, 15) # 150 - 165
self.enemy_stats[2::16] = bytearray(remake_hp)
def fix_fighters_ring(self):
"""
Adds functionality for the fighter's ring (+2 to attack)
"""
# ring patch
self.add_patch(0xf10c, 1, 0x20, 0x54, 0xff, 0xea)
self.add_patch(0xff64 ,1, 0x85, 0xcd, 0xa5, 0xcf, 0x29, 0x20, 0xf0, 0x07,
0xa5, 0xcc, 0x18, 0x69, 0x02, 0x85, 0xcc, 0xa5, 0xcf, 0x60)
def move_repel(self):
"""
Moves the repel spell to level 8
"""
self.new_spell_levels[7] = 8
self.update_spell_masks()
def buff_heal(self):
"""
Buffs the heal spell slightly to have a range of 10-25 instead of 10-15
"""
self.add_patch(0xdbce, 1, 15)
def patch_northern_shrine(self):
"""
Removes the 2 blocks from around the shrine guardian so you can walk around
him.
"""
self.add_patch(0xd77, 10, 0x66, 0x66)
def bump_zone_0_encounters(self):
"""
Sets the encounter rate of Zone 0 to be the same as other zones.
"""
# skip the extra rng for zone 0.
self.add_patch(0xced1, 1, *([0xea]*13))
def revert(self):
"""
Reverts all previous changes to the ROM
"""
self.owmap = WorldMap(self.rom_data)
self.enemy_stats = self.rom_data[self.enemy_stats_slice]
self.mp_reqs = self.rom_data[self.mp_req_slice]
self.xp_reqs = self.rom_data[self.xp_req_slice]
self.zones = self.rom_data[self.zones_slice]
self.shop_inventory = self.rom_data[self.weapon_shop_inv_slice]
self.token_loc = self.rom_data[self.token_slice]
self.flute_loc = self.rom_data[self.flute_slice]
self.armor_loc = self.rom_data[self.armor_slice]
self.encounter_1_loc = self.rom_data[self.encounter_1_slice] # axe knight
self.encounter_2_loc = self.rom_data[self.encounter_2_slice] # green dragon
self.encounter_3_loc = self.rom_data[self.encounter_3_slice] # golem
self.encounter_enemies = self.rom_data[self.encounter_enemies_slice]
# set position 1 to these to disable remembering of killing them.
self.encounter_2_kill = self.rom_data[self.encounter_2_kill_slice]
self.encounter_3_kill = self.rom_data[self.encounter_3_kill_slice]
self.player_stats = self.rom_data[self.player_stats_slice]
self.new_spell_levels = self.rom_data[self.new_spell_slice]
self.chests = self.rom_data[self.chests_slice]
self.title_screen_text = self.rom_data[self.title_text_slice]
self.patches = {}
def token_dialogue(self):
"""
Generates new dialogue data for the NPC who tells you where the token is.
rtype: bytearray
return: new data to replace the npc dialogue.
"""
if self.token_loc[0] == 1:
x, y = self.token_loc[1:3]
elif self.flute_loc[0] == 1:
x, y = self.flute_loc[1:3]
elif self.armor_loc[0] == 1:
x, y = self.armor_loc[1:3]
else:
return (self.ascii2dw("Thou must go and fight!") +
bytearray([0xfc, 0xfb, 0x50]) +
self.ascii2dw( "Go forth, descendent of Erdrick, "
"I have complete faith in thy victory! "))
tx, ty = self.owmap.warps_from[self.owmap.tantegel_warp][1:3]
north_south = "north" if y < ty else "south"
east_west = "west" if x < tx else "east"
return (self.ascii2dw("Thou may go and search.") +
bytearray([0xfc, 0xfb, 0x50]) + self.ascii2dw(
("From Tantegel Castle travel %2d leagues to the %s and %2d to the %s.") %
(abs(y - ty), north_south, abs(x - tx), east_west)))
def add_tantegel_exit(self):
"""
Adds a quicker exit for the Tantegel throne room.
"""
# add new stairs to the throne room and 1st floor
self.add_patch(0x43a, 1, 0x47)
self.add_patch(0x2b9, 1, 0x45)
# add a new exit to the first floor
self.add_patch(0x2d7, 1, 0x66)
# replace the usless grave warps with some for tantegel
self.add_patch(0xf45f, 1, 5, 1, 8)
self.add_patch(0xf4f8, 1, 4, 1, 7)
#remove the top set of stairs associated with the old warp in the grave
self.add_patch(0x1298, 1, 0x22)
def commit(self):
"""
Commits all changes to the ROM
"""
orig_data = self.rom_data[:]
self.rom_data[self.will_not_work_slice] = \
self.ascii2dw("The spell had no effect.")
self.rom_data[self.token_dialogue_slice] = self.token_dialogue()
# commit map
self.owmap.commit()
self.rom_data[self.enemy_stats_slice] = self.enemy_stats
self.rom_data[self.mp_req_slice] = self.mp_reqs
self.rom_data[self.xp_req_slice] = self.xp_reqs
self.rom_data[self.zones_slice] = self.zones
self.rom_data[self.weapon_shop_inv_slice] = self.shop_inventory
self.rom_data[self.token_slice] = self.token_loc
self.rom_data[self.flute_slice] = self.flute_loc
self.rom_data[self.armor_slice] = self.armor_loc
self.rom_data[self.encounter_1_slice] = self.encounter_1_loc
self.rom_data[self.encounter_2_slice] = self.encounter_2_loc
self.rom_data[self.encounter_3_slice] = self.encounter_3_loc
self.rom_data[self.encounter_1_run_slice] = self.encounter_1_loc
self.rom_data[self.encounter_2_run_slice] = self.encounter_2_loc
self.rom_data[self.encounter_3_run_slice] = self.encounter_3_loc
self.rom_data[self.encounter_enemies_slice] = self.encounter_enemies
# update the kill bit code
self.rom_data[self.encounter_2_kill_slice] = self.encounter_2_kill
self.rom_data[self.encounter_3_kill_slice] = self.encounter_3_kill
self.rom_data[self.player_stats_slice] = self.player_stats
self.rom_data[self.new_spell_slice] = self.new_spell_levels
self.rom_data[self.chests_slice] = self.chests
self.rom_data[self.title_text_slice] = self.title_screen_text
self.apply_patches()
self.ips = ips.Patch.create(orig_data, self.rom_data)
def add_patch(self, addr, *data):
"""
Adds a new manual rom patch
:Parameters:
addr : int
The address where the new data is to be applied
data : array
The first argument should be the distance between each byte to be placed
in the rom. The rest is the data to be patched into the ROM
rtype:
return:
"""
self.patches[addr] = data
def apply_patches(self):
"""
Applies any manual patches
"""
# apply any manual patches
while len(self.patches):
addr,patch = self.patches.popitem()
self.rom_data[addr:addr+(len(patch)-1)*patch[0]:patch[0]] = patch[1:]
def write(self, output_filename, content=None):
"""
Writes out the new ROM file
:Parameters:
output_filename : string
The name of the new file
"""
outputfile = open(output_filename, 'wb')
if content:
outputfile.write(content)
else:
outputfile.write(self.rom_data)
outputfile.close()
def update_title_screen(self, seed, flags):
"""
Adds flags and seed number to the title screen
:Parameters:
seed : int
The random seed used to generate this ROM.
flags: str
The flags used to generate this ROM.
"""
# There are no lower case characters in the title screen alphabet :(
# I'm not sure how to deal with this yet wrt flags.
padding = lambda s: struct.pack('BBB', 0xf7, s, 0x5f)
padline = lambda p: padding(math.floor((32 - len(p))/2)) + self.ascii2dw(p) + \
padding(math.ceil((32 - len(p))/2)) + b'\xfc'
blank_line = struct.pack('BBBB', 0xf7, 32, 0x5f, 0xfc)
new_text = b''
new_text += blank_line
new_text += padline("RANDOMIZER")
new_text += blank_line
new_text += padline(VERSION.upper())
new_text += blank_line
new_text += blank_line
new_text += blank_line
new_text += blank_line
new_text += padline("FLAGS " + flags.upper())
new_text += blank_line
new_text += padline("SEED " + str(seed))
new_text += blank_line
needed_bytes = len(self.title_screen_text) - len(new_text) - 4
if needed_bytes > 31: # current data is too short...
new_text = new_text[:-len(blank_line)] + (b'\x5f' * 32 + b'\xfc')
needed_bytes = len(self.title_screen_text) - len(new_text) - 4
new_text += b'\x5f' * needed_bytes + padding(32 - needed_bytes) + b'\xfc'
new_text = new_text.replace(b'\x47', b'\x61').replace(b'\x49', b'\x63')
self.title_screen_text = new_text
def inverted_power_curve(min_, max_, power, count=30):
range_ = max_ - min_
p_range = range_ ** (1/power)
points = []
for i in range(count):
points.append(round(max_ - ((random.random() * p_range) ** power)))
points.sort()
return points
def main():
parser = argparse.ArgumentParser(prog="DWRandomizer",
description="A randomizer for Dragon Warrior for NES")
parser.add_argument("-r","--no-remake", action="store_false",
help="Do not set enemy HP, XP/Gold drops and MP use up to that of the "
"remake. This will make grind times much longer.")
parser.add_argument("-c","-C","--no-chests", action="store_false",
help="Do not randomize chest contents.")
parser.add_argument("-d","-D","--defaults", action="store_true",
help="Run the randomizer with the default options.")
parser.add_argument("--force", action="store_false",
help="Skip checksums and force randomization. This may produce an invalid"
" ROM if the incorrect file is used.")
parser.add_argument("-i","-I","--no-searchitems", action="store_false",
help="Do not randomize the locations of searchable items (Fairy Flute, "
"Erdrick's Armor, Erdrick's Token).")
parser.add_argument("--ips", action="store_true",
help="Also create an IPS patch for the original ROM")
parser.add_argument("-f","--fast-leveling", action="store_true",
help="Set XP requirements for each level to 75%% of normal.")
parser.add_argument("-F","--very-fast-leveling", action="store_true",
help="Set XP requirements for each level to 50%% of normal.")
parser.add_argument("-g","--no-growth", action="store_false",
help="Do not randomize player stat growth.")
parser.add_argument("-G","--ultra-growth", action="store_true",
help="Enable ultra randomization of player stat growth.")
parser.add_argument("-l","-L","--no-repel", action="store_false",
help="Do not move repel to level 8.")
parser.add_argument("-m","--no-spells", action="store_false",
help="Do not randomize the level spells are learned.")
parser.add_argument("-M","--ultra-spells", action="store_true",
help="Enable ultra randomization of the level spells are learned.")
parser.add_argument("--no-map", action="store_false",
help="Do not generate a new world map.")
parser.add_argument("-p","--no-patterns", action="store_false",
help="Do not randomize enemy attack patterns.")
parser.add_argument("-P","--ultra-patterns", action="store_true",
help="Enable ultra randomization of enemy attack patterns.")
parser.add_argument("-s","-S","--seed", type=int,
help="Specify a seed to be used for randomization.")
parser.add_argument("-t","-T","--no-towns", action="store_false",
help="Do not randomize towns.")
parser.add_argument("-w","-W","--no-shops", action="store_false",
help="Do not randomize weapon shops.")
parser.add_argument("-u","-U","--ultra", action="store_true",
help="Enable all '--ultra' options.")
parser.add_argument("-z","--no-zones", action="store_false",
help="Do not randomize enemy zones.")
parser.add_argument("-Z","--ultra-zones", action="store_true",
help="Enable ultra randomization of enemy zones.")
parser.add_argument("-v","-V","--version", action="version",
version="%%(prog)s %s" % VERSION)
parser.add_argument("filename", help="The rom file to use for input")
args = parser.parse_args()
# if the user didn't enter any flags, ask for options.
if len(sys.argv) <= 2:
prompt_for_options(args)
randomize(args)
# So the command prompt doesn't just disappear
if sys.platform == 'win32':
print("\n\n")
input("Press enter to exit...")
def prompt_for_options(args):
"""
Prompts the user for randomizer options
"""
print()
while True:
seed = input("Enter seed number (leave blank for random seed): ")
if seed:
try:
args.seed = int(seed)
break
except:
print("\nThat is not a valid number. ", end="")
else:
break
custom = True
mode = input("\nRandomization mode - ultra, normal, custom (u/n/C): ")
if (mode.lower().startswith("u")):
args.ultra = True
custom = False
elif (mode.lower().startswith("n")):
custom = False
mode = input("\nLeveling speed - normal, fast, very fast (N/f/v): ")
if (mode.lower().startswith("v")):
args.very_fast_leveling = True
elif (mode.lower().startswith("f")):
args.fast_leveling = True
if custom:
if input("\nGenerate a random world map? (Y/n) ").lower().startswith("n"):
args.no_map = False
if args.no_map:
if input("\nRandomize town & cave locations? (Y/n) ").lower().startswith("n"):
args.no_towns = False
if input("\nRandomize weapon shops? (Y/n) ").lower().startswith("n"):
args.no_shops = False
if input("\nRandomize chests? (Y/n) ").lower().startswith("n"):
args.no_chests = False
if input("\nRandomize searchable items? (Y/n) ").lower().startswith("n"):
args.no_searchitems = False
growth = input("\nStat growth randomization - ultra, default, none (u/D/n): ")
if (growth.lower().startswith("u")):
args.ultra_growth = True
elif (growth.lower().startswith("n")):
args.growth = False
if input("\nMove REPEL to level 8? (Y/n) ").lower().startswith("n"):
args.no_repel = False
spells = input("\nSpell learning randomization - ultra, default, none (u/D/n): ")
if (spells.lower().startswith("u")):
args.ultra_spells = True
elif (spells.lower().startswith("n")):
args.no_spells = False
zones = input("\nEnemy zone randomization - ultra, default, none (u/D/n): ")
if (zones.lower().startswith("u")):
args.ultra_zones = True
elif (zones.lower().startswith("n")):
args.no_zones = False
patterns = input("\nEnemy attack randomization - ultra, default, none (u/D/n): ")
if (patterns.lower().startswith("u")):
args.ultra_patterns = True
elif (patterns.lower().startswith("n")):
args.no_patterns = False
ips = input("\nGenerate an IPS patch file along with the ROM? y/N): ")
if (ips.lower().startswith("y")):
args.ips= True
def randomize(args):
rom = Rom(args.filename)
print("\n\nDWRandomizer %s" % VERSION)
if not args.seed:
args.seed = random.randint(0, sys.maxsize)
print("Randomizing %s using random seed %d..." % (args.filename, args.seed))
random.seed(args.seed)
flags = ""
prg = ""
if args.force:
print("Verifying checksum...")
result = rom.verify_checksum()
if result is False:
print("Checksum does not match any known ROM.")
answer = input("Do you want to attempt to randomize anyway? ")
if not answer.lower().startswith("y"):
sys.exit(-1)
else:
print("Processing Dragon Warrior PRG%d ROM..." % result)
prg = "PRG%d." % result
else:
print("Skipping checksum...")
print("Fixing functionality of the fighter's ring (+2 atk)...")
rom.fix_fighters_ring()
print("Buffing HEAL slightly...")
rom.buff_heal()
print("Fixing Northern Shrine...")
rom.patch_northern_shrine()
print("Bumping encounter rate for zone 0...")
rom.bump_zone_0_encounters()
print("Adding a new throne room exit...")
rom.add_tantegel_exit()
if args.no_map:
print("Generating new overworld map...")
flags += "A"
while not rom.generate_map():
print("Error: " + str(rom.owmap.error) + ", retrying...")
if args.no_searchitems:
print("Shuffling searchable item locations...")
flags += "I"
rom.shuffle_searchables()
if args.no_chests:
print("Shuffling chest contents...")
flags += "C"
rom.shuffle_chests()
if args.no_towns:
print("Shuffling town locations...")
flags += "T"
rom.shuffle_towns()
if args.no_zones:
if args.ultra or args.ultra_zones:
print("Ultra randomizing enemy zones...")
flags += "Z"
rom.randomize_zones(True)
else:
print("Randomizing enemy zones...")
flags += "z"
rom.randomize_zones()
if args.no_patterns:
if args.ultra or args.ultra_patterns:
print("Ultra randomizing enemy attack patterns...")
flags += "P"
rom.randomize_attack_patterns(True)
else:
print("Randomizing enemy attack patterns...")
flags += "p"
rom.randomize_attack_patterns()
if args.no_shops:
print("Randomizing weapon shops...")
flags += "W"
rom.randomize_shops()
if args.no_growth:
if args.ultra or args.ultra_growth:
print("Ultra randomizing player stat growth...")
flags += "G"
rom.randomize_growth(True)
else:
print("Randomizing player stat growth...")
flags += "g"
rom.randomize_growth()
if args.no_remake:
print("Increasing XP/Gold drops to remake levels...")
flags += "R"
rom.update_drops()
print("Setting enemy HP to approximate remake levels...")
rom.update_enemy_hp()
print("Lowering MP requirements to remake levels...")
rom.update_mp_reqs()
if args.very_fast_leveling:
print("Setting XP requirements for levels to 50% of normal...")
flags += "F"
rom.lower_xp_reqs(True)
elif args.fast_leveling:
print("Setting XP requirements for levels to 75% of normal...")
flags += "f"
rom.lower_xp_reqs()
if args.no_repel:
print("Moving REPEL to level 8...")
flags += "L"
rom.move_repel()
if args.no_spells:
if args.ultra or args.ultra_spells:
print("Ultra randomizing level spells are learned...")
flags += "M"
rom.randomize_spell_learning(True)
else:
print("Randomizing level spells are learned...")
flags += "m"
rom.randomize_spell_learning()
print("Updating title screen...")
rom.update_title_screen(args.seed, flags)
rom.commit()
output_filename = "DWRando.%s.%d.%snes" % (flags, args.seed, prg)
print("Writing output file %s..." % output_filename)
rom.write(output_filename)
if args.ips:
output_filename = "DWRando.%s.%d.%sips" % (flags, args.seed, prg)
print("Writing output file %s..." % output_filename)
rom.write(output_filename, rom.ips.encode())
print ("New ROM Checksum: %s" % rom.sha1())
print ("IPS Checksum: %s" % rom.sha1(rom.ips.encode()))
if __name__ == "__main__":
main()