forked from pret/poketcg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrando.py
460 lines (399 loc) · 18.5 KB
/
rando.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
import sys
import json
import random
import subprocess
import shutil
from collections import Counter
from functools import partial
open_utf8 = partial(open, encoding='utf8')
# The base bit-noise constants were crafted to have distinctive and interesting
# bits, and have so far produced excellent experimental test results.
NOISE1 = 0xb5297a4d # 0b0110'1000'1110'0011'0001'1101'1010'0100
NOISE2 = 0x68e31da4 # 0b1011'0101'0010'1001'0111'1010'0100'1101
NOISE3 = 0x1b56c4e9 # 0b0001'1011'0101'0110'1100'0100'1110'1001
PRIME1 = 0x0bd4bcb5 # 0b0000'1011'1101'0100'1011'1100'1011'0101
PRIME2 = 0x0063d68d # 0b0000'0000'0110'0011'1101'0110'1000'1101
CAP = 1 << 32
def noise1d(n, seed=0):
n *= NOISE1
n += seed
n ^= n >> 8
n += NOISE2
n ^= n << 8
n *= NOISE3
n ^= n >> 8
return n % CAP
def noise2d(x, y, seed=0):
return noise1d(x + PRIME1 * y, seed)
def noise3d(x, y, z, seed=0):
return noise1d(x + PRIME1 * y + PRIME2 * z, seed)
def pick(l, n):
return l[n % len(l)]
def calc_offset(p, r, n):
offset = (n % (r * 2 + 1)) - r
return p + offset
def calc_range(p, min, max):
return p % (max - min + 1) + min
class PTCGRando:
RAND_CARDS = 10
RAND_NPC_DECKS = 20
RAND_BOOSTERS = 30
RAND_MASTERS = 40
RAND_STARTER_DECKS = 50
RAND_NPC_NAMES = 60
TYPE_PKMN = 0
TYPE_ENERGY = 1
TYPE_TRAINER = 2
STAGE_BASIC = 0
STAGE_1 = 1
STAGE_2 = 2
STAGE_NONE = 3
def __init__(self):
self.seed = 1
self.data = None
# CARDS
# Groups evolutioary lines in the same boosters
self.group_by_evolution = True
# TRAINERS
# Disable randomizing prize counts
self.exclude_prize = False
# Enabling randomizes prizes from 1-6
self.prize_full_random = False
# While prize_full_random is disabled, adds from -prize_range to +prize_range
self.prize_range = 1
# Disable randomizing trainer decks
self.exclude_decks = False
# Disable randomizing music
self.exclude_music = False
# BOOSTER REWARDS
self.exclude_boosters = False
# Enabling randomizes booster rewards from booster_min to booster_max
self.booster_min = 1
self.booster_max = 6
# DECK GENERATION
self.min_pokemon = 25
self.max_pokemon = 40
self.min_trainer = 5
self.max_trainer = 10
self.exclude_npcs = False
def pkmn_by_evolution(cards, has_evolution=False):
return filter(lambda x: x['has_evolution'] == has_evolution, cards)
def pkmn_by_stage(cards, stage=STAGE_BASIC):
return filter(lambda x: x['stage'] == stage, cards)
def pkmn_by_energy(cards, energy):
return filter(lambda x: energy in x['energy'], cards)
def pkmn_colorless(cards):
return filter(lambda x: ('COLORLESS' in x['energy'], len(x['energy'])) == (True, 1), cards)
def load_data(self, data_file, cards_file, npcs_file):
with open_utf8(data_file) as file:
self.data = json.load(file)
with open_utf8(cards_file) as file:
self.data['cards'] = json.load(file)
card_list = self.data['cards'].values()
self.data['pokemon_cards'] = list(filter(lambda x: x['type'] == PTCGRando.TYPE_PKMN, card_list))
self.data['energy_cards'] = list(filter(lambda x: x['type'] == PTCGRando.TYPE_ENERGY, card_list))
self.data['trainer_cards'] = list(filter(lambda x: x['type'] == PTCGRando.TYPE_TRAINER, card_list))
with open_utf8(npcs_file) as file:
self.data['npcs'] = json.load(file)
def randomize_cards(self):
current_card = ''
with open_utf8('templates/cards.asm', 'r') as src, open_utf8('src/data/cards.asm', 'w') as target:
for i, line in enumerate(src):
if 'CARD_NAME' in line:
current_card = line.split(':')[0]
target.write(line)
elif 'RANDOMIZE_RARITY' in line:
# @Todo add rarity randomizing?
target.write(line)
elif 'RANDOMIZE_SET' in line:
y = self.data['cards'][current_card]['group'] if self.group_by_evolution else self.data['cards'][current_card]['id']
y *= 10
target.write(' db %s | NONE ; sets\n' % pick(self.data['sets'], noise2d(PTCGRando.RAND_CARDS, y, self.seed)))
else:
target.write(line)
def write_duel(self, target, line):
trainer = self.data['npc_duels'][line.split(':')[1].strip()]
y = trainer['id'] * 10
prize = trainer['prize']
deck_id = trainer['deck_id']
music_id = trainer['music_id']
if not self.exclude_prize:
if self.prize_full_random:
prize = calc_range(noise3d(PTCGRando.RAND_NPC_DECKS, y, 10, self.seed), 1, 6)
else:
prize = calc_offset(prize, self.prize_range, noise3d(PTCGRando.RAND_NPC_DECKS, y, 10, self.seed))
if prize < 1:
prize = 1
elif prize > 6:
prize = 6
if not self.exclude_decks:
deck_id = pick(self.data['decks'], noise3d(PTCGRando.RAND_NPC_DECKS, y, 20, self.seed))
if not self.exclude_music:
music_id = pick(self.data['music'], noise3d(PTCGRando.RAND_NPC_DECKS, y, 30, self.seed))
target.write(' start_duel PRIZES_{}, {}, {}\n'.format(prize, deck_id, music_id))
def write_boosters(self, target, line):
trainer = self.data['npc_boosters'][line.split(':')[1].strip()]
y = trainer['id'] * 10
z = 10
booster_count = 0
boosters = []
if self.exclude_boosters:
boosters = trainer['packs']
else:
booster_count = calc_range(noise3d(PTCGRando.RAND_BOOSTERS, y, z, self.seed), self.booster_min, self.booster_max)
if booster_count < 1:
booster_count = 1
for i in range(booster_count):
z += 10
boosters.append(pick(self.data['packs'], noise3d(PTCGRando.RAND_BOOSTERS, y, z, self.seed)))
remainder = booster_count % 3
if remainder > 0:
boosters += ['NO_BOOSTER'] * (3 - remainder)
booster_count = len(boosters)
while len(boosters) >= 3:
booster_set = boosters[:3]
boosters = boosters[3:]
target.write(' give_booster_packs {}\n'.format(', '.join(booster_set)))
def write_master_check(self, target, line):
if len(self.data['master_checks']) == 0:
return
patch_file = pick(self.data['master_checks'], noise2d(PTCGRando.RAND_MASTERS, self.data['masters_checked'], self.seed))
self.data['master_checks'].remove(patch_file)
with open_utf8(patch_file, 'r') as patch:
target.writelines(patch.readlines())
self.data['masters_checked'] = self.data['masters_checked'] + 10
def randomize_bank03(self):
self.data['master_checks'] = [
'templates/patches/isaac_check.asm',
'templates/patches/ken_check.asm',
'templates/patches/mitch_check.asm',
'templates/patches/murray_check.asm'
]
self.data['masters_checked'] = 0
with open_utf8('templates/bank03.asm', 'r') as src, open_utf8('src/engine/bank03.asm', 'w') as target:
for i, line in enumerate(src):
if '; DUEL:' in line:
self.write_duel(target, line)
elif '; BOOSTERS:' in line:
self.write_boosters(target, line)
elif '; MASTER_CHECK:' in line:
self.write_master_check(target, line)
target.write(line)
else:
target.write(line)
def randomize_bank04(self):
with open_utf8('templates/bank04.asm', 'r') as src, open_utf8('src/engine/bank04.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def randomize_home(self):
with open_utf8('templates/home.asm', 'r') as src, open_utf8('src/engine/home.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def generate_deck(self, target, z, deck_name):
y = 10
remaining_trainer = noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed)
remaining_trainer = calc_range(remaining_trainer, self.min_trainer, self.max_trainer)
y += 10
remaining_pokemon = noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed)
remaining_pokemon = calc_range(remaining_pokemon, self.min_pokemon, self.max_pokemon)
y += 10
color_count = noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed) % 10
if color_count < 3:
color_count = 1
elif color_count < 9:
color_count = 2
else:
color_count = 3
colors = self.data['colors'].copy()
y += 10
# 75% chance to remove colorless
if noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed) % 4 != 0:
colors.remove('COLORLESS')
while len(colors) > color_count:
color = colors[noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed) % len(colors)]
colors.remove(color)
y += 10
cards = Counter()
while remaining_trainer > 0:
card = pick(self.data['trainer_cards'], noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed))
if cards[card['constant']] < card['limit']:
cards[card['constant']] += 1
remaining_trainer -= 1
y += 10
pokemons = []
for color in colors:
if color == 'COLORLESS':
pokemons += PTCGRando.pkmn_colorless(self.data['pokemon_cards'])
else:
pokemons += PTCGRando.pkmn_by_energy(self.data['pokemon_cards'], color)
while remaining_pokemon > 0:
card = pick(pokemons, noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed))
if cards[card['constant']] < card['limit']:
cards[card['constant']] += 1
remaining_pokemon -= 1
if card['has_evolution'] and card['target_group']:
pre_evo = next(filter(lambda x: x['text_name'] == card['target_group'], self.data['cards'].values()))
if cards[pre_evo['constant']] < pre_evo['limit']:
cards[pre_evo['constant']] += 1
remaining_pokemon -= 1
if pre_evo['has_evolution'] and pre_evo['target_group']:
pre_evo2 = next(filter(lambda x: x['text_name'] == pre_evo['target_group'], self.data['cards'].values()))
if cards[pre_evo2['constant']] < pre_evo2['limit']:
cards[pre_evo2['constant']] += 1
remaining_pokemon -= 1
y += 10
energies = []
for color in colors:
energies.append(self.data['cards'][self.data['energies'][color]])
remaining_energies = 60 - sum(cards.values())
while remaining_energies > 0:
card = pick(energies, noise3d(PTCGRando.RAND_STARTER_DECKS, y, z, self.seed))
if cards[card['constant']] < card['limit']:
cards[card['constant']] += 1
remaining_energies -= 1
y += 10
self.data['starter_deck_names'][deck_name] = ', '.join([color[:6] for color in colors])
for k in cards:
target.write(' db {}, {}\n'.format(cards[k], k))
def randomize_decks(self):
with open_utf8('templates/decks.asm', 'r') as src, open_utf8('src/data/decks.asm', 'w') as target:
z = 10
for i, line in enumerate(src):
if '; GENERATE_DECK:' in line:
self.generate_deck(target, z, line.split(':')[1].strip())
z += 10
target.write(line)
def randomize_text_offsets(self):
with open_utf8('templates/text_offsets.asm', 'r') as src, open_utf8('src/text/text_offsets.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def randomize_text2(self):
with open_utf8('templates/text2.asm', 'r') as src, open_utf8('src/text/text2.asm', 'w') as target:
for i, line in enumerate(src):
if '; STARTER_DECK' in line:
line = line.format(**self.data['starter_deck_names'])
target.write(line)
def randomize_text3(self):
with open_utf8('templates/text3.asm', 'r') as src, open_utf8('src/text/text3.asm', 'w') as target:
y = 10
npcs = self.data['npcs'].copy()
self.data['npc_names'] = {}
for i, line in enumerate(src):
if '; NPC_NAMES:' in line:
if self.exclude_npcs:
target.write(line.split(':')[1])
else:
npc = pick(npcs, noise2d(PTCGRando.RAND_NPC_NAMES, y, self.seed))
if 'Michael' in line:
self.data['npc_names']['Michael'] = npc
elif 'Chris' in line:
self.data['npc_names']['Chris'] = npc
elif 'Jessica' in line:
self.data['npc_names']['Jessica'] = npc
elif 'Jennifer' in line:
self.data['npc_names']['Jennifer'] = npc
elif 'Nicholas' in line:
self.data['npc_names']['Nicholas'] = npc
elif 'Brandon' in line:
self.data['npc_names']['Brandon'] = npc
npcs.remove(npc)
target.write(' text "{}"\n'.format(npc))
y += 10
elif '; STARTER_DECK' in line:
target.write(line.format(**self.data['starter_deck_names']))
else:
target.write(line)
def randomize_text4(self):
with open_utf8('templates/text4.asm', 'r') as src, open_utf8('src/text/text4.asm', 'w') as target:
for i, line in enumerate(src):
if '; MITCH_CHECK:' in line:
npc = ''
if self.exclude_npcs:
if 'Michael' in line:
npc = 'Michael'
elif 'Chris' in line:
npc = 'Chris'
elif 'Jessica' in line:
npc = 'Jessica'
else:
if 'Michael' in line:
npc = self.data['npc_names']['Michael']
elif 'Chris' in line:
npc = self.data['npc_names']['Chris']
elif 'Jessica' in line:
npc = self.data['npc_names']['Jessica']
target.write(line.format(npc))
else:
target.write(line)
def randomize_text5(self):
with open_utf8('templates/text5.asm', 'r') as src, open_utf8('src/text/text5.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def randomize_text6(self):
with open_utf8('templates/text6.asm', 'r') as src, open_utf8('src/text/text6.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def randomize_text7(self):
with open_utf8('templates/text7.asm', 'r') as src, open_utf8('src/text/text7.asm', 'w') as target:
for i, line in enumerate(src):
if '; ISAAC_CHECK:' in line:
npc = ''
if self.exclude_npcs:
if 'Jennifer' in line:
npc = 'Jennifer'
elif 'Nicholas' in line:
npc = 'Nicholas'
elif 'Brandon' in line:
npc = 'Brandon'
else:
if 'Jennifer' in line:
npc = self.data['npc_names']['Jennifer']
elif 'Nicholas' in line:
npc = self.data['npc_names']['Nicholas']
elif 'Brandon' in line:
npc = self.data['npc_names']['Brandon']
target.write(line.format(npc))
else:
target.write(line)
def randomize_text8(self):
with open_utf8('templates/text8.asm', 'r') as src, open_utf8('src/text/text8.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
def randomize_text9(self):
with open_utf8('templates/text9.asm', 'r') as src, open_utf8('src/text/text9.asm', 'w') as target:
for i, line in enumerate(src):
target.write(line)
seed = random.randint(0, 999999)
if len(sys.argv) > 1:
seed = int(sys.argv[1])
ptcg = PTCGRando()
ptcg.load_data('data/data.json', 'data/cards.json', 'data/npc_names.json')
ptcg.seed = seed
ptcg.randomize_cards()
ptcg.randomize_bank03()
ptcg.randomize_bank04()
ptcg.randomize_home()
ptcg.randomize_decks()
ptcg.randomize_text_offsets()
ptcg.randomize_text2()
ptcg.randomize_text3()
ptcg.randomize_text4()
ptcg.randomize_text5()
ptcg.randomize_text6()
ptcg.randomize_text7()
ptcg.randomize_text8()
ptcg.randomize_text9()
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
# from whichcraft import which
from shutil import which
return which(name) is not None
if is_tool('make'):
make = subprocess.run(['make'], stdout=subprocess.PIPE, universal_newlines=True)
if make.returncode == 0:
lipx = subprocess.run(['python', 'lipx.py', '-c', 'baserom.gbc', 'poketcg.gbc', 'ptcgr_{:06d}.ips'.format(seed)], stdout=subprocess.PIPE, universal_newlines=True)
print('Successfully compiled with seed: {:06d}'.format(seed))
sys.exit(0)
else:
print(make.stdout)
sys.exit(1)