Skip to content

Commit

Permalink
Merge pull request #299 from lhearachel/build-refactor/encounters
Browse files Browse the repository at this point in the history
Refactor generation of encounter data files and packing of pl_enc_data.narc
  • Loading branch information
lhearachel authored Nov 12, 2024
2 parents c7d4869 + 364945a commit 44bc33a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 72 deletions.
19 changes: 11 additions & 8 deletions res/field/encounters/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
enc_bin_gen = generator(
encounters_new_py,
arguments: [ '@INPUT@', '@OUTPUT@', ],
output: '@[email protected]'
)

pl_enc_data_srcs = files(
'000.json',
'001.json',
Expand Down Expand Up @@ -181,20 +187,17 @@ pl_enc_data_srcs = files(
'179.json',
'180.json',
'181.json',
'182.json'
'182.json',
)

pl_enc_tbl_narc = custom_target('pl_enc_data.narc',
output: 'pl_enc_data.narc',
input: pl_enc_data_srcs,
env: json2bin_env,
input: enc_bin_gen.process(pl_enc_data_srcs, env: json2bin_env),
depends: [ py_consts_generators ],
command: [
encounters_py,
'--knarc', knarc_exe,
'--source-dir', '@CURRENT_SOURCE_DIR@',
'--private-dir', '@PRIVATE_DIR@',
'--output-dir', '@OUTDIR@',
knarc_exe,
'-d', '@PRIVATE_DIR@',
'-p', '@OUTPUT@',
]
)

Expand Down
66 changes: 66 additions & 0 deletions tools/json2bin/encounter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
import itertools
import json
import pathlib
import sys

from consts import species


def as_species(s: str) -> bytes:
return species.PokemonSpecies[s].value.to_bytes(4, 'little')

def convert_land(encs: list) -> bytes:
return b''.join(itertools.chain.from_iterable([
(
int(encs[i]['level']).to_bytes(4, 'little'),
as_species(encs[i]['species']),
)
for i in range(12)
]))

def convert_water(encs: list) -> bytes:
return b''.join(itertools.chain.from_iterable([
(
int(encs[i]['level_max']).to_bytes(1, 'little'),
int(encs[i]['level_min']).to_bytes(1, 'little'),
(0).to_bytes(2, 'little'),
as_species(encs[i]['species']),
)
for i in range(5)
]))


input_path = pathlib.Path(sys.argv[1])
output_path = pathlib.Path(sys.argv[2])

data = {}
with open(input_path, 'r', encoding='utf-8') as input_file:
data = json.load(input_file)

packables = bytearray([])
packables.extend(int(data['land_rate']).to_bytes(4, 'little'))
packables.extend(convert_land(data['land_encounters']))

for enc_type, i in itertools.product(['swarms', 'morning', 'night'], range(2)):
packables.extend(as_species(data[enc_type][i]))

for i in range(4):
packables.extend(as_species(data['radar'][i]))

for key in ['rate_form0', 'rate_form1', 'rate_form2', 'rate_form3', 'rate_form4', 'unown_table']:
packables.extend(int(data[key]).to_bytes(4, 'little'))

for version, i in itertools.product(['ruby', 'sapphire', 'emerald', 'firered', 'leafgreen'], range(2)):
packables.extend(as_species(data[version][i]))

packables.extend(data['surf_rate'].to_bytes(4, 'little'))
packables.extend(convert_water(data['surf_encounters']))
packables.extend((0).to_bytes(44, 'little'))

for rod in ['old', 'good', 'super']:
packables.extend(data[f'{rod}_rod_rate'].to_bytes(4, 'little'))
packables.extend(convert_water(data[f'{rod}_rod_encounters']))

with open(output_path, 'wb') as output_file:
output_file.write(packables)
63 changes: 0 additions & 63 deletions tools/json2bin/encounters.py

This file was deleted.

3 changes: 2 additions & 1 deletion tools/json2bin/meson.build
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
json2bin_env = environment()
json2bin_env.set('PYTHONPATH', meson.project_build_root()) # access to constants geneated by constgen

encounters_py = find_program('encounters.py', native: true)
events_py = find_program('events.py', native: true)
movedata_py = find_program('movedata.py', native: true)
pokemon_personal_data_py = find_program('pokemon_personal_data.py', native: true)
pokemon_wotbl_data_py = find_program('pokemon_wotbl_data.py', native: true)
trainer_data_py = find_program('trainer_data.py', native: true)
evo_py = find_program('evo.py', native: true)
pl_poke_data_py = find_program('pl_poke_data.py', native: true)

encounters_new_py = find_program('encounter.py', native: true)

0 comments on commit 44bc33a

Please sign in to comment.