Files
shenleiandClaude Opus 5.5 38b62cfdbd Port AVA2 drop tables and drop code
make_drops.py builds 203's mob_drop_item / common_drop_new / special_item_group
from AVA2's (unknown items removed and renumbered, AVA2 boxes replace the same
vnum except 50035). The game loads common_drop_new.txt (level-range common
drops), GetDropPct follows AVA2 (premium/glove rates, fixed precedence bug),
a mob's second drop group no longer uses an uninitialised pointer, special
groups allow 1023 entries, and the wiki lists common_drop_new rows as rule 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 10:31:07 +09:00

107 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""Build the 203 drop tables from AVA2's (docs/FEATURE-AVA2-DROPS.md).
mob_drop_item.txt AVA2's, entries whose item 203 lacks are dropped and the rest renumbered
(the reader stops at the first missing index and fails on an unknown item)
common_drop_new.txt AVA2's NEW_COMMON_DROP groups, filtered the same way
special_item_group.txt 203's groups with AVA2's groups replacing those of the same vnum; AVA2-only groups added
common_drop_item.txt empty, like AVA2 (common drops come from common_drop_new.txt)
etc_drop_item.txt empty, like AVA2
Usage: make_drops.py <AVA2 locale dir> <203 locale dir> <output dir> <item_proto.txt>...
The item_proto files are every row the servers will load (the live one plus ava2-items' items-server.tsv).
"""
import json,re,sys
from pathlib import Path
# AVA2 reused these vnums for a different box: 50035 is 203's children's day gift box but AVA2's GM-only
# 'Pct' box handing out thousands of items, so 203's group stays.
KEEP_203={50035}
def vnums(paths):
result=set()
for p in paths:
for line in Path(p).read_bytes().splitlines():
head=line.split(b'\t',1)[0].strip()
if head.isdigit():result.add(int(head))
elif re.fullmatch(rb'\d+~\d+',head): # dragon soul rows cover a vnum range
lo,hi=map(int,head.split(b'~'));result.update(range(lo,hi+1))
return result
def groups(data):
"""[(name bytes, [body line bytes])] of a CTextFileLoader file ('Group <name>' then a braced body)."""
lines=data.replace(b'\r\n',b'\n').split(b'\n');result=[];i=0
while i<len(lines):
m=re.match(rb'\s*group\s+(.*?)\s*$',lines[i],re.I)
if not m:
if lines[i].strip() and not lines[i].strip().startswith((b'#',b'//')):raise SystemExit('unexpected line %r'%lines[i])
i+=1;continue
assert lines[i+1].strip()==b'{',lines[i:i+2]
j=i+2;body=[]
while lines[j].strip()!=b'}':body.append(lines[j]);j+=1
result.append((m.group(1),body));i=j+1
return result
def key(body,name):
for line in body:
t=line.split()
if t and t[0].lower()==name.encode():return t[1:]
return None
def entries(body):
return [line.split() for line in body if line.split() and line.split()[0].isdigit()]
def render(name,head,items):
out=[b'Group\t'+name,b'{']+[b'\t'+b'\t'.join(t) for t in head]
out+=[b'\t'+b'\t'.join([str(k).encode()]+t[1:]) for k,t in enumerate(items,1)]
return out+[b'}']
def filtered(gs,known,item_column,dropped):
"""Groups with unknown items removed and indexes renumbered; groups left empty are skipped."""
out=[]
for name,body in gs:
head=[line.split() for line in body if line.split() and not line.split()[0].isdigit()]
items=[]
for t in entries(body):
token=t[item_column]
if token.isdigit() and int(token) not in known:dropped.setdefault(int(token),0);dropped[int(token)]+=1;continue
items.append(t)
if items:out+=render(name,head,items)
return out
def build(ava,live,output,protos):
known=vnums(protos);report={}
output.mkdir(parents=True,exist_ok=True)
mob=groups((ava/'mob_drop_item.txt').read_bytes())
assert all(key(b,'type')[0].lower()==b'drop' for _,b in mob) # every AVA2 group is a 'drop' group
assert max(len(entries(b)) for _,b in mob)<256
dropped={};text=filtered(mob,known,1,dropped)
(output/'mob_drop_item.txt').write_bytes(b'\n'.join(text)+b'\n')
report['mob_groups']=sum(1 for l in text if l==b'{');report['mob_dropped']=dropped
common=groups((ava/'common_drop_new.txt').read_bytes())
dropped={};text=filtered(common,known,1,dropped)
(output/'common_drop_new.txt').write_bytes(b'\n'.join(text)+b'\n')
report['common_new_groups']=sum(1 for l in text if l==b'{');report['common_new_dropped']=dropped
theirs=groups((ava/'special_item_group.txt').read_bytes());ours=groups((live/'special_item_group.txt').read_bytes())
assert all((key(b,'type') or [b'x'])[0].lower()!=b'attr' for _,b in theirs)
theirs=[(n,b) for n,b in theirs if int(key(b,'vnum')[0]) not in KEEP_203]
replaced={int(key(b,'vnum')[0]) for _,b in theirs}
dropped={};text=[]
for name,body in ours:
if int(key(body,'vnum')[0]) in replaced:continue
text+=[b'Group\t'+name,b'{']+body+[b'}']
text+=filtered(theirs,known,1,dropped)
(output/'special_item_group.txt').write_bytes(b'\n'.join(text)+b'\n')
kept=[int(key(b,'vnum')[0]) for _,b in ours if int(key(b,'vnum')[0]) not in replaced]
report.update(special_kept_203=len(kept),special_from_ava2=len(replaced),
special_replaced=sorted(replaced&{int(key(b,'vnum')[0]) for _,b in ours}),special_dropped=dropped)
for name in ('common_drop_item.txt','etc_drop_item.txt'):(output/name).write_bytes(b'')
for k in [k for k in report if k.endswith('_dropped')]:report[k]={str(v):n for v,n in sorted(report[k].items())}
(output/'report.json').write_text(json.dumps(report,indent=1)+'\n')
return report
if __name__=='__main__':
if len(sys.argv)<5:sys.exit(__doc__)
r=build(Path(sys.argv[1]),Path(sys.argv[2]),Path(sys.argv[3]),sys.argv[4:])
print(json.dumps({k:(len(v) if isinstance(v,(list,dict)) else v) for k,v in r.items()}))