367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
167 lines
7.3 KiB
Python
167 lines
7.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Octo-only CreatureDisplayInfoExtra (humanoid NPC) -> native 70334 rows.
|
|
|
|
REA (runtime/continuation-20261111/creature-extras/rea-*.private.{json,log}):
|
|
- Native 70334 links CreatureDisplayInfo.ExtendedDisplayInfoID -> CreatureDisplayInfoExtra
|
|
(DisplayRaceID u8, DisplaySexID u8, DisplayClassID u8, Flags, BakeMaterialResourcesID,
|
|
HDBakeMaterialResourcesID). 9094 native CDI rows use an extra; all of them use the
|
|
HD ChrModel display model (e.g. humanmale_hd.m2), never the legacy character model.
|
|
- On the 6933 extras shared with Octo (same ID, race and sex) the native
|
|
BakeMaterialResourcesID texture is the Octo Textures/BakedNpcTextures bake re-encoded
|
|
(256x256, mean abs pixel difference ~5/255, TextureFileData UsageType 0). The HD bake
|
|
(512x256, HD layout) is a native re-bake with no Source counterpart.
|
|
- NPCModelItemSlotDisplayInfo (noninline ID; ItemDisplayInfoID, ItemSlot u8, relation
|
|
NpcModelID = extra ID): native ItemSlot equals the legacy NPCItemDisplay index 0..9
|
|
(>=99.9% of 33k shared pairs); 6557/6933 shared extras carry the same item multiset.
|
|
- CreatureDisplayInfoOption (noninline ID; ChrCustomizationOptionID, ChrCustomizationChoiceID,
|
|
relation CreatureDisplayInfoExtraID): per ChrModel option the choice is a function of one
|
|
legacy field (skin/face/hair style/hair color/facial hair), determinism 0.83-1.0.
|
|
The mapping is learned from the shared rows; unseen values (and ChrModels without
|
|
shared rows) fall back to the choice OrderIndex rule only for legacy fields where that
|
|
rule reproduces the learned mapping (hair style 1.0, hair color 0.961; skin 0.35,
|
|
face 0.66, facial hair 0.47 are not admitted).
|
|
"""
|
|
import collections
|
|
|
|
LEGACY_FIELDS = ('skin', 'face', 'hair', 'haircolor', 'facial')
|
|
ITEM_SLOTS = 10 # legacy NPCItemDisplay[10]; native ItemSlot == index (REA)
|
|
CDX_DEFAULTS = dict(DisplayClassID=0, Flags=0)
|
|
MIN_DETERMINISM = 0.8
|
|
MIN_ORDER_RULE = 0.9
|
|
|
|
|
|
def source_extras(rows, text):
|
|
"""Legacy 1.12 CreatureDisplayInfoExtra (19 fields) -> dict."""
|
|
out = {}
|
|
for r in rows:
|
|
if len(r) != 19:
|
|
raise ValueError('Legacy CreatureDisplayInfoExtra width 19 required')
|
|
out[r[0]] = dict(ID=r[0], race=r[1], sex=r[2], skin=r[3], face=r[4], hair=r[5], haircolor=r[6],
|
|
facial=r[7], items=list(r[8:18]), bake=text(r[18]))
|
|
return out
|
|
|
|
|
|
def bake_path(name):
|
|
return 'textures/bakednpctextures/' + name.lower() if name else None
|
|
|
|
|
|
def learn_options(shared, native_options, option_model):
|
|
"""shared: [(legacy extra, native extra id)]; native_options: extra id -> [(option, choice)].
|
|
-> {option: dict(model, field, values{value: choice}, determinism, constant)}"""
|
|
votes = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.defaultdict(collections.Counter)))
|
|
for legacy, ident in shared:
|
|
for option, choice in native_options.get(ident, ()):
|
|
for field in LEGACY_FIELDS:
|
|
votes[option][field][legacy[field]][choice] += 1
|
|
out = {}
|
|
for option, fields in votes.items():
|
|
best = None
|
|
for field, by_value in fields.items():
|
|
total = sum(sum(c.values()) for c in by_value.values())
|
|
hit = sum(c.most_common(1)[0][1] for c in by_value.values())
|
|
if best is None or hit / total > best[1]:
|
|
best = (field, hit / total, total, by_value)
|
|
field, det, total, by_value = best
|
|
choices = collections.Counter()
|
|
for c in by_value.values():
|
|
choices.update(c)
|
|
out[option] = dict(model=option_model.get(option), field=field, determinism=det, n=total,
|
|
values={v: c.most_common(1)[0][0] for v, c in by_value.items()},
|
|
constant=choices.most_common(1)[0][0] if len(choices) == 1 else None)
|
|
return out
|
|
|
|
|
|
def order_rule_accuracy(learned, ordered_choices):
|
|
"""Per legacy field: share of learned (value -> choice) pairs reproduced by
|
|
'choice = ordered_choices[option][value]'."""
|
|
hit, total = collections.Counter(), collections.Counter()
|
|
for option, spec in learned.items():
|
|
order = ordered_choices.get(option, [])
|
|
for value, choice in spec['values'].items():
|
|
total[spec['field']] += 1
|
|
hit[spec['field']] += value < len(order) and order[value] == choice
|
|
return {field: hit[field] / total[field] for field in total}
|
|
|
|
|
|
def order_rule_fields(accuracy):
|
|
return {field for field, share in accuracy.items() if share >= MIN_ORDER_RULE}
|
|
|
|
|
|
def choose_options(legacy, model_options, learned, ordered_choices, order_fields):
|
|
"""-> ([(option, choice)], [(option, reason)]) for one legacy extra on its ChrModel."""
|
|
picked, skipped = [], []
|
|
for option in model_options:
|
|
spec = learned.get(option)
|
|
if spec and spec['determinism'] >= MIN_DETERMINISM:
|
|
value = legacy[spec['field']]
|
|
if value in spec['values']:
|
|
picked.append((option, spec['values'][value]))
|
|
continue
|
|
if spec['constant'] is not None:
|
|
picked.append((option, spec['constant']))
|
|
continue
|
|
field = spec['field'] if spec else None
|
|
if field in order_fields:
|
|
order = ordered_choices.get(option, [])
|
|
if legacy[field] < len(order):
|
|
picked.append((option, order[legacy[field]]))
|
|
continue
|
|
skipped.append((option, 'unlearned' if not spec else 'value-unseen'))
|
|
return picked, skipped
|
|
|
|
|
|
def map_item_display(value, remapped, native_ids, verified):
|
|
"""Legacy NPC item display -> native ItemDisplayInfo ID or None.
|
|
remapped: Source display -> package-imported native ID (items lane);
|
|
native_ids: native IDs; verified: Source IDs whose native row matches the Source row."""
|
|
if not value:
|
|
return None
|
|
if value in remapped:
|
|
return remapped[value]
|
|
if value in native_ids and value in verified:
|
|
return value
|
|
return None
|
|
|
|
|
|
def cdx_record(ident, race, sex, bake_mr, hd_bake_mr=0):
|
|
if not (0 < race < 256 and sex in (0, 1)):
|
|
raise ValueError('Native extra needs u8 race and sex 0/1')
|
|
record = dict(CDX_DEFAULTS)
|
|
record.update(ID=ident, DisplayRaceID=race, DisplaySexID=sex, BakeMaterialResourcesID=bake_mr,
|
|
HDBakeMaterialResourcesID=hd_bake_mr)
|
|
return record
|
|
|
|
|
|
def slot_record(item_display, slot, extra):
|
|
if not 0 <= slot < 256:
|
|
raise ValueError('ItemSlot is u8')
|
|
return dict(ItemDisplayInfoID=item_display, ItemSlot=slot, NpcModelID=extra)
|
|
|
|
|
|
def option_record(option, choice, extra):
|
|
return dict(ChrCustomizationOptionID=option, ChrCustomizationChoiceID=choice, CreatureDisplayInfoExtraID=extra)
|
|
|
|
|
|
def texture_file_data_record(file_data_id, material_resources_id, usage=0):
|
|
return dict(FileDataID=file_data_id, UsageType=usage, MaterialResourcesID=material_resources_id)
|
|
|
|
|
|
class Allocator:
|
|
"""Monotonic ID allocator above a floor, skipping taken IDs."""
|
|
|
|
def __init__(self, first, taken=()):
|
|
self.next = first
|
|
self.taken = set(taken)
|
|
self.first = first
|
|
|
|
def __call__(self):
|
|
while self.next in self.taken:
|
|
self.next += 1
|
|
value = self.next
|
|
self.taken.add(value)
|
|
self.next += 1
|
|
return value
|
|
|
|
def span(self):
|
|
return [self.first, self.next - 1] if self.next > self.first else None
|