pokemon/tools/parse/pokemon/evos_moves.py
Zack Buhman 853457a79d generate: add pokemon
This also includes a handful of functions for creating instances of a
pokemon species, though certainly not complete.
2023-08-02 23:49:42 +00:00

86 lines
2.6 KiB
Python

import builtins
from dataclasses import dataclass
from parse.generic import tokenize
from parse.generic import number
from parse.generic import label_data
@dataclass
class EvosMoves:
label: str
evolutions: list
learnset: list
def __init__(self):
self.label = None
self.evolutions = []
self.learnset = []
def is_evolution(tokens):
return tokens[0].startswith('EV_')
def is_learnset_entry(tokens):
return len(tokens) == 2 # hmm... hack? maybe not
def parse_ev(tokens):
ev_type, *rest = tokens
if ev_type == 'EV_ITEM':
item_name, level_requirement, pokemon_name = rest
return ev_type, item_name, number.parse(level_requirement), pokemon_name
elif ev_type == 'EV_LEVEL' or ev_type == 'EV_TRADE':
level_requirement, pokemon_name = rest
return ev_type, 0, number.parse(level_requirement), pokemon_name
else:
assert False, ev_type
def parse_learnset_entry(args):
level_requirement, move_name = args
return number.parse(level_requirement), move_name
def build_tables(tokens):
evos_moves = EvosMoves()
data_tokens = {'dw', 'db'}
type_args = label_data.event(tokens, data_tokens=data_tokens)
ix = 0
while ix < len(type_args):
type, args = type_args[ix]
if type is label_data.Label:
label = args
assert builtins.type(label) is str
if label == 'EvosMovesPointerTable':
ix, pointer_table = label_data.pointer_table(type_args, ix + 1)
yield pointer_table
continue
if evos_moves.label is not None:
yield evos_moves
evos_moves = EvosMoves()
else:
assert evos_moves.evolutions == [], evos_moves
assert evos_moves.learnset == [], evos_moves
evos_moves.label = label
elif type is label_data.Data:
if is_evolution(args):
evos_moves.evolutions.append(parse_ev(args))
elif is_learnset_entry(args):
evos_moves.learnset.append(parse_learnset_entry(args))
elif args == ['0']:
pass # do nothing
else:
assert False, (type, args)
else:
assert False, (type, args)
ix += 1
# yield last evos_moves at the end of parsing
if evos_moves.label != None:
yield evos_moves
def parse(prefix):
path = prefix / "data/pokemon/evos_moves.asm"
with open(path) as f:
return list(build_tables(tokenize.lines(f.read().split('\n'))))