#!/usr/bin/env python3
"""Write mob_drop_item groups for the AVA2 new-map monsters from drops_maps.tsv.
Each monster in the stage's mobs-server.tsv gets its tier's rows (by rank and type, see drops_maps.tsv),
plus the map:
rows of every map that spawns it (regen/boss/stone/dungeon.txt, directly or via
group.txt / group_group.txt) and the mob: / mob:- rows covering it.
NPCs, warps and buildings get nothing.
The chance column is converted to the file's pct: per kill = pct*10000 * delta% * 1.5 (no premium) / 4000000,
so pct = chance% / 0.375 at delta 100. Every item vnum is checked against the given item_proto files.
Output: /mob_drop_item.txt (one 'drop' group per monster); install_maps.py appends the
groups of monsters the server has no drop group for.
Usage: make_drops_maps.py ...
"""
import collections,re,sys
from pathlib import Path
stage,table=Path(sys.argv[1]),Path(sys.argv[2])
GOLD=range(4062,4068)
known=set()
for p in sys.argv[3:]:
for l in Path(p).read_bytes().splitlines():
h=l.split(b'\t',1)[0].strip()
if h.isdigit():known.add(int(h))
rows=collections.defaultdict(list);bad=[]
for n,l in enumerate(table.read_text(encoding='utf-8').splitlines(),1):
if not l.strip() or l.startswith('#'):continue
t=l.split('\t')
tier,vnum,count,chance=t[0],int(t[1]),int(t[2]),float(t[3])
if vnum not in known:bad.append('%s:%d: item %d not in item_proto'%(table.name,n,vnum))
if not 0255:raise SystemExit('mob %d: %d rows (max 255)'%(v,len(items)))
out+=['Group\tAVA2MAP_%d'%v,'{','\tMob\t%d'%v,'\tType\tdrop']
out+=['\t%d\t%d\t%d\t%s'%(i,vnum,n,('%.4f'%(c/0.375)).rstrip('0').rstrip('.')) for i,(vnum,n,c) in enumerate(items,1)]
out+=['}'];count[t]+=1
(stage/'mob_drop_item.txt').write_bytes(('\r\n'.join(out)+'\r\n').encode())
unused=[k for k in rows if k.startswith('map:') and k[4:] not in {m for s in maps.values() for m in s}]
unused+=[k for k in rows if k.startswith('mob:') and not any(int(k[4:].partition('-')[0])<=v<=int(k[4:].partition('-')[2] or k[4:]) for v in mobs)]
print('drop groups: %d (%s) -> %s'%(sum(count.values()),', '.join('%s %d'%kv for kv in sorted(count.items())),stage/'mob_drop_item.txt'))
if unused:raise SystemExit('rows for no monster: '+' '.join(unused))