92 lines
3.9 KiB
Python
92 lines
3.9 KiB
Python
"""Build the runtime peer look index from the effective client appearance tables."""
|
|
|
|
from collections import defaultdict
|
|
from itertools import groupby
|
|
from operator import itemgetter
|
|
|
|
|
|
def qualify(source, families, explicit, modified, appearances, item_ids, displays_with_art):
|
|
"""Use effective tables after all removals and overrides have been applied.
|
|
|
|
modified maps child ID to (item, modifier, parent, flags); appearances
|
|
maps parent ID to (display, condition). Item and ItemSparse must both exist.
|
|
"""
|
|
defaults = defaultdict(set)
|
|
for child, (item, modifier, parent, flags) in modified.items():
|
|
if not modifier and not flags:
|
|
defaults[item].add(child)
|
|
|
|
known = set()
|
|
chosen = {}
|
|
for item in item_ids:
|
|
links = {explicit[item]} if item in explicit else defaults.get(item, ())
|
|
# Do not choose among multiple effective defaults or link another item's row.
|
|
if len(links) != 1:
|
|
continue
|
|
child, = links
|
|
row = modified.get(child)
|
|
if row is None or row[0] != item or row[1] or row[3]:
|
|
continue
|
|
display, condition = appearances.get(row[2], (0, 0))
|
|
if not condition and display in displays_with_art:
|
|
known.add(item)
|
|
chosen[item] = display
|
|
|
|
donors = []
|
|
for (source_display, inventory_type), (native_display, item) in sorted(families.items()):
|
|
if source.get(item) != (source_display, inventory_type):
|
|
continue
|
|
if chosen.get(item) == native_display:
|
|
donors.append((source_display, inventory_type, native_display, item, explicit.get(item, 0)))
|
|
return known, donors
|
|
|
|
|
|
def ranges(items):
|
|
for _, entries in groupby(enumerate(sorted(items)), key=lambda entry: entry[1] - entry[0]):
|
|
values = list(map(itemgetter(1), entries))
|
|
yield values[0], values[-1]
|
|
|
|
|
|
def header(known, donors):
|
|
if not known or not donors:
|
|
raise ValueError("Empty peer appearance index")
|
|
lines = [
|
|
"// Generated by ops/peer_item_appearances.py from the assembled client data.",
|
|
"#pragma once",
|
|
"#ifndef GW_ITEM_RENDER_PROFILE_HEADER",
|
|
'#define GW_ITEM_RENDER_PROFILE_HEADER "gateway/item_render_profile.hpp"',
|
|
"#endif",
|
|
"#include GW_ITEM_RENDER_PROFILE_HEADER",
|
|
"namespace gateway::peer_item_appearances {",
|
|
"struct ItemRange { uint32_t first, last; };",
|
|
"struct Donor { uint32_t source_display, inventory_type, native_display, item, explicit_appearance; };",
|
|
]
|
|
def table(signature, rows):
|
|
lines.append(signature + "[] = {")
|
|
for row in rows:
|
|
lines.append(" {" + ", ".join(map(str, row)) + "},")
|
|
lines.append("};")
|
|
table("inline constexpr ItemRange KnownItems", ranges(known))
|
|
table("inline constexpr Donor Donors", donors)
|
|
lines.append("""
|
|
inline bool has_native_look(uint32_t item) {
|
|
const auto it = std::lower_bound(std::begin(KnownItems), std::end(KnownItems), item,
|
|
[](const ItemRange& range, uint32_t id) { return range.last < id; });
|
|
return it != std::end(KnownItems) && it->first <= item;
|
|
}
|
|
|
|
inline uint32_t render_item(uint32_t source_display, uint32_t inventory_type) {
|
|
const auto key = std::pair{source_display, inventory_type};
|
|
const auto donor = std::lower_bound(std::begin(Donors), std::end(Donors), key,
|
|
[](const Donor& d, const auto& k) { return std::pair{d.source_display, d.inventory_type} < k; });
|
|
if (donor == std::end(Donors) || donor->source_display != source_display ||
|
|
donor->inventory_type != inventory_type) return 0;
|
|
const auto* active = item_render_profile::find(source_display, inventory_type);
|
|
if (!active || active->native_display != donor->native_display || active->render_item != donor->item ||
|
|
item_render_profile::modified_appearance(donor->item) != donor->explicit_appearance) return 0;
|
|
return donor->item;
|
|
}
|
|
} // namespace gateway::peer_item_appearances
|
|
""")
|
|
return "\n".join(lines)
|