Files
aostats-custom/scripts/gen_gamedata.py
T
bionickatana 7279b3c207
Build / Build source distribution (push) Failing after 13s
Build / Execute unit tests (push) Failing after 8s
Build / Verify generated game data is current (push) Failing after 7s
Build / Upload Release Asset (push) Has been skipped
Build / upload_pypi (push) Has been skipped
Working advanced tracking code
2026-10-13 09:08:38 -06:00

467 lines
16 KiB
Python

"""Generate the small game-data lookup tables the app actually ships.
The raw dumps are ~50 MB and are fetched on demand (see fetch_gamedata.py).
This reduces them to a handful of compact tables under ``src/static``.
Everything here is derived mechanically from the dumps. There are no
hand-written mappings, and no observed damage is ever used to infer a value --
strength comes from item power, which is a property of the item definition.
Usage:
python scripts/gen_gamedata.py [--cache DIR] [--check]
"""
import argparse
import json
import os
import re
import sys
STATIC_DIR = os.path.join('src', 'static')
DEFAULT_CACHE = os.path.join('.cache', 'gamedata')
# Mob template IDs are the enumeration index of the mobs.bin XML, which differs
# from the order of mobs.json by a constant. Six independent template IDs seen
# in real captures resolve to the correct mob on both name and max HP with this
# offset, across ~2300 entries of the file. The rule producing it is not
# understood -- the 16 skipped entries are all category=roaming, which is the
# signature of a filtered enumeration rather than a statement of the filter.
# Accepted as a constant by decision; see .opencode/plans/effective-damage.md.
MOB_TEMPLATE_OFFSET = 16
ARMOR_TYPES = ('physicalarmor', 'magicresistance')
_ITEM_LINE = re.compile(r'\s*(\d+):\s*(\S+?)\s*(?::|$)')
def _load(cache_dir, name):
with open(os.path.join(cache_dir, name), encoding='utf-8') as handle:
return json.load(handle)
def _as_list(value):
if value is None:
return []
if isinstance(value, list):
return value
return [value]
# --------------------------------------------------------------------------
# item_ids.txt
# --------------------------------------------------------------------------
def gen_item_ids(cache_dir):
"""Rewrite src/static/item_ids.txt in the format the app reads.
The app's loader expects ``<id>,<uniquename>``. Upstream ships
`` <id>: <uniquename> : <display name>``; 846 entries have no display
name and end after the unique name.
"""
out_path = os.path.join(STATIC_DIR, 'item_ids.txt')
lines = []
src = os.path.join(cache_dir, 'formatted', 'items.txt')
with open(src, encoding='utf-8', errors='replace') as handle:
for line in handle:
match = _ITEM_LINE.match(line)
if match:
lines.append('{},{}'.format(match.group(1), match.group(2)))
lines.sort(key=lambda entry: int(entry.split(',', 1)[0]))
_write_text(out_path, '\n'.join(lines) + '\n')
return out_path, len(lines)
# --------------------------------------------------------------------------
# item_spells.json
# --------------------------------------------------------------------------
def _index_items(items_root):
"""Unique name -> definition, in document order across all groups."""
index = {}
for group, value in items_root.items():
if group.startswith('@') or group == 'shopcategories':
continue
for entry in _as_list(value):
if isinstance(entry, dict) and entry.get('@uniquename'):
index[entry['@uniquename']] = entry
return index
def _resolve_spells(item_index, name, depth=0, seen=None):
"""Flatten craftingspelllist, following @reference and removespell.
Most weapons carry no spells of their own -- they inherit a whole ability
set from a base item. Resolving that inheritance is what turns 471 weapons
with an apparently empty spell list into full coverage.
"""
seen = seen if seen is not None else set()
if name in seen or depth > 12:
return []
seen.add(name)
entry = item_index.get(name)
if not entry:
return []
spell_list = entry.get('craftingspelllist')
if not spell_list:
return []
if not isinstance(spell_list, dict):
return []
spells = []
reference = spell_list.get('@reference')
if reference:
spells += _resolve_spells(item_index, reference, depth + 1, seen)
removed = set()
for rem in _as_list(spell_list.get('removespell')):
if isinstance(rem, dict) and rem.get('@uniquename'):
removed.add(rem['@uniquename'])
for craft in _as_list(spell_list.get('craftspell')):
if isinstance(craft, dict) and craft.get('@uniquename'):
spells.append(craft)
return [s for s in spells if s['@uniquename'] not in removed]
def gen_item_spells(cache_dir):
item_index = _index_items(_load(cache_dir, 'items.json')['items'])
result = {}
for name in item_index:
spells = _resolve_spells(item_index, name)
if not spells:
continue
entries = [{'slot': s.get('@slots'), 'spell': s['@uniquename']}
for s in spells]
result[name] = entries
# A packet identifies an enchanted item as BASE@LEVEL, but only the
# base item exists in the dump. Emit the variants as aliases so a
# lookup straight from a packet id finds the abilities, rather than
# silently getting nothing.
enchantments = item_index[name].get('enchantments')
if not enchantments:
continue
variants = _as_list(
enchantments.get('enchantment') if isinstance(enchantments, dict)
else enchantments)
for variant in variants:
if isinstance(variant, dict) and variant.get('@enchantmentlevel'):
result['{}@{}'.format(name, variant['@enchantmentlevel'])] = \
entries
out_path = os.path.join(STATIC_DIR, 'item_spells.json')
_write_json(out_path, result, sort_keys=True)
return out_path, len(result)
# --------------------------------------------------------------------------
# debuff_spells.json
# --------------------------------------------------------------------------
def _index_spells(spells_root):
index = {}
for group in ('activespell', 'passivespell', 'togglespell'):
for spell in spells_root.get(group) or []:
if spell.get('@uniquename'):
index[spell['@uniquename']] = spell
return index
def _spell_links(spell_index, name):
"""Spells one hop downstream of ``name``.
An ability rarely carries its debuff directly. ARMORPIERCER declares no
buffovertime at all: it points at ARMORPIERCER_EFFECT via spelleffectarea,
which in turn applies ARMORPIERCER_DEBUFF_EFFECT via applyspell. Only the
last link actually holds the armour numbers, so every hop has to be walked.
"""
spell = spell_index.get(name)
if not spell:
return []
out = []
for applied in _as_list(spell.get('applyspell')):
if isinstance(applied, dict) and applied.get('@spell'):
out.append(applied['@spell'])
for area in _as_list(spell.get('spelleffectarea')):
if isinstance(area, dict) and area.get('@effect'):
out.append(area['@effect'])
return out
def _collect_debuffs(spell_index, name, depth=0, seen=None):
"""Negative armour/resistance effects reachable from ``name``."""
seen = seen if seen is not None else set()
if name in seen or depth > 3:
return []
seen.add(name)
found = []
spell = spell_index.get(name)
if spell:
for effect in _as_list(spell.get('buffovertime')):
if not isinstance(effect, dict):
continue
if effect.get('@type') not in ARMOR_TYPES:
continue
try:
value = float(effect.get('@value') or 0)
except ValueError:
continue
if value < 0:
found.append((name, effect))
for nxt in _spell_links(spell_index, name):
found += _collect_debuffs(spell_index, nxt, depth + 1, seen)
return found
def _flatten_effect(effect):
flat = {
'type': effect.get('@type'),
'value': float(effect.get('@value') or 0),
'time': float(effect.get('@time') or 0),
}
# Armour Piercer scales with how many targets the caster hit, so its
# magnitude must be read off the observed effect, never assumed.
bonus = effect.get('@targetcountvaluebonusfactor')
if bonus:
flat['targetcountvaluebonusfactor'] = float(bonus)
# SUNDERARMOR2 is weaker against players than mobs; pick per victim type.
target_type = effect.get('IfTargetType')
if isinstance(target_type, dict) and target_type.get('@type'):
flat['iftargettype'] = target_type['@type']
target = effect.get('@target')
if target:
flat['target'] = target
return flat
def gen_debuff_spells(cache_dir):
item_index = _index_items(_load(cache_dir, 'items.json')['items'])
spell_index = _index_spells(_load(cache_dir, 'spells.json')['spells'])
abilities = set()
for name in item_index:
for spell in _resolve_spells(item_index, name):
unique = spell.get('@uniquename')
if unique and not unique.startswith('PASSIVE'):
abilities.add(unique)
# effect spell -> abilities that produce it
producers = {}
for ability in abilities:
for effect_name, effect in _collect_debuffs(spell_index, ability):
producers.setdefault(effect_name, {}).setdefault(
ability, []).append(_flatten_effect(effect))
result = {}
for effect_name, by_ability in sorted(producers.items()):
spell = spell_index.get(effect_name, {})
result[effect_name] = {
'abilities': sorted(by_ability),
'category': spell.get('@category'),
'uitype': spell.get('@uitype'),
'maxstack': _maybe_int(spell.get('@maxstack')),
'maxcharges': _maybe_int(spell.get('@maxcharges')),
'effects': sorted(
{json.dumps(e, sort_keys=True): e
for group in by_ability.values() for e in group}.values(),
key=lambda e: (e['type'], e['value']),
),
}
out_path = os.path.join(STATIC_DIR, 'debuff_spells.json')
_write_json(out_path, result, sort_keys=True)
return out_path, len(result)
def _maybe_int(value):
if value in (None, ''):
return None
try:
return int(value)
except (TypeError, ValueError):
return None
# --------------------------------------------------------------------------
# progression.json
# --------------------------------------------------------------------------
def gen_progression(cache_dir):
"""Item-power progression constants for armour and damage.
Albion scales a character's stats from item power, so attacker strength is
a property of the equipped item and not of any observed hit. This keeps the
arithmetic correct as players level their gear.
"""
game = _load(cache_dir, 'gamedata.json')['AO-GameData']
source = game['Items']['ItemPowerProgression']
result = {'slots': {}, 'shares': {}}
for key, value in source.items():
if key in ('armorshare', 'ccrshare', 'hitpointsshare', 'energyshare',
'trackingtime'):
result['shares'][key] = value
elif isinstance(value, dict) and '@armorbase' in value:
result['slots'][key] = {
'armorbase': float(value['@armorbase']),
'armorprogression': float(value['@armorprogression']),
'attackdamageprogression': float(
value.get('@attackdamageprogression') or 0),
'hitpointsprogression': float(
value.get('@hitpointsprogression') or 0),
}
out_path = os.path.join(STATIC_DIR, 'progression.json')
_write_json(out_path, result, sort_keys=True)
return out_path, len(result['slots'])
# --------------------------------------------------------------------------
# mob_armor.json
# --------------------------------------------------------------------------
def gen_mob_armor(cache_dir):
mobs = _load(cache_dir, 'mobs.json')['Mobs']['Mob']
result = {}
for position, mob in enumerate(mobs):
template_id = position + MOB_TEMPLATE_OFFSET
result[str(template_id)] = {
'name': mob.get('@uniquename'),
'tier': _maybe_int(mob.get('@tier')),
'armor': _maybe_int(mob.get('@physicalarmor')),
'resistance': _maybe_int(mob.get('@magicresistance')),
'hitpointsmax': _maybe_int(mob.get('@hitpointsmax')),
'category': mob.get('@category'),
}
out_path = os.path.join(STATIC_DIR, 'mob_armor.json')
_write_json(out_path, result, sort_keys=True)
return out_path, len(result)
# --------------------------------------------------------------------------
# item_power.json
# --------------------------------------------------------------------------
def gen_item_power(cache_dir):
"""Item power per unique name, including enchantment variants.
A packet carries the enchanted variant as part of the item id
(``T5_MAIN_HOLYSTAFF_AVALON@1``), and item power scales with the
enchantment level, so both are needed.
"""
item_index = _index_items(_load(cache_dir, 'items.json')['items'])
result = {}
for name, entry in item_index.items():
base = _maybe_int(entry.get('@itempower'))
if base is not None:
result[name] = base
enchantments = entry.get('enchantments')
if not enchantments:
continue
for variant in _as_list(enchantments.get('enchantment')
if isinstance(enchantments, dict)
else enchantments):
if not isinstance(variant, dict):
continue
level = variant.get('@enchantmentlevel')
power = _maybe_int(variant.get('@itempower'))
if level is None or power is None:
continue
result['{}@{}'.format(name, level)] = power
out_path = os.path.join(STATIC_DIR, 'item_power.json')
_write_json(out_path, result, sort_keys=True)
return out_path, len(result)
# --------------------------------------------------------------------------
def _write_json(path, data, sort_keys=True):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w', encoding='utf-8') as handle:
json.dump(data, handle, indent=1, sort_keys=sort_keys)
handle.write('\n')
def _write_text(path, text):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w', encoding='utf-8') as handle:
handle.write(text)
GENERATORS = (
('item_ids.txt', gen_item_ids),
('item_spells.json', gen_item_spells),
('debuff_spells.json', gen_debuff_spells),
('progression.json', gen_progression),
('mob_armor.json', gen_mob_armor),
('item_power.json', gen_item_power),
)
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--cache', default=DEFAULT_CACHE)
parser.add_argument('--check', action='store_true',
help='regenerate into a temp dir and report drift '
'instead of overwriting the committed tables')
args = parser.parse_args(argv)
if not os.path.isdir(args.cache):
raise SystemExit(
'no game data at {}; run scripts/fetch_gamedata.py first'.format(
args.cache))
if args.check:
return _check(args.cache)
for label, generator in GENERATORS:
path, count = generator(args.cache)
print('{:24s} {:>7d} entries {}'.format(
label, count, os.path.relpath(path)))
return 0
def _check(cache_dir):
import tempfile
import shutil
global STATIC_DIR
original = STATIC_DIR
drifted = []
tmp = tempfile.mkdtemp(prefix='gamedata-check-')
try:
STATIC_DIR = tmp
for label, generator in GENERATORS:
generated, _ = generator(cache_dir)
committed = os.path.join(original, label)
if not os.path.exists(committed):
drifted.append('{}: missing from src/static'.format(label))
elif _read(generated) != _read(committed):
drifted.append('{}: committed copy is stale'.format(label))
finally:
STATIC_DIR = original
shutil.rmtree(tmp, ignore_errors=True)
if drifted:
print('game data is out of date:')
for line in drifted:
print(' - {}'.format(line))
print('\nrun: make gamedata')
return 1
print('game data tables are up to date')
return 0
def _read(path):
with open(path, encoding='utf-8') as handle:
return handle.read()
if __name__ == '__main__':
sys.exit(main())