
    UHj                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZ  e	e      j!                         j"                  d   Zed	z  Zed
z  Z	 	 d	 	 	 	 	 	 	 	 	 ddZddZddZddZddZedk(  r e        yy)zISummarize unclassified layer0 tile IDs by tileset for passability review.    )annotationsN)Counterdefaultdict)Path)	load_maps)load   outdatac                   t        t              }t        t              }t        t              }t        d       }t        | j                               D ]>  \  }}	|	j	                  d      xs |	j	                  d      gd   }
|j	                  |
i       }t        |j	                  dg             t        |j	                  dg             z  }|j	                  dg       D ch c]  }t        |       c}|j	                  dg       D ch c]  }t        |       c}z  }t        |	d	   d   |	d	   d
         D ]Y  \  }}||v s||f|v r||
   |xx   d
z  cc<   ||
   |   |xx   d
z  cc<   |dkD  r||
   |xx   d
z  cc<   J||
   |xx   d
z  cc<   [ A g }t        |      D ]  }
||
   j                  |      D ]r  \  }}||
   |   j                  |      D cg c]
  \  }}||d }}}|j                  |
||||
   |   ||
   |   ||
   |   r
||
   |   rdn||
   |   rdnd|d       t  |S c c}w c c}w c c}}w )Nc                      t        t              S )N)r   r        6/home/exedev/hwanse/tools/summarize_tile_class_gaps.py<lambda>zsummarize.<locals>.<lambda>   s    {SZG[ r   layerTilesetstilesetr   passblock	passPairs
blockPairslayersr	   )mapcountmixed)r   tiler   fallbackPassfallbackBlockfallbackKindexamples)
r   r   sorteditemsgetsettuplezipmost_commonappend)mapsclasseslimit_per_tilesetexample_limitcountersfallback_passfallback_blockr    map_nameinfor   entryknownpairknown_pairsr   maskrowsr   name	map_countmap_exampless                         r   	summarizer;      s    )4G(<H-8-AM.9'.BN3>?[3\H . 3$88O,E)1D0EqIGR(EIIfb)*S7B1G-HH/4yyb/IJtuT{J$)IIlB$?N
 E$KN
 
 d8nQ/h1BC 	3JD$u}t ;Wd#q(#Wd#H-2-axg&t,1,w'-2-	33" D(# #G,889JK 	KD% (0'8'>'J'J='Y#D) y1L  KK" -g 6t <!/!8!> %W-d3w8OPT8U  %W-d3   ( 	* KG K N
 s   H2H7H<
c                R    | j                  d      r| d   S | d   r| d   ry| d   ryy)Nr   r   r   r   r   r   )r#   )rows    r   fallback_kindr>   H   s9    
ww~>""
>s?3
>r   c                0   i }| D ]o  }|j                  |d   |d   ddddddddd	      }t        |      }|dxx   dz  cc<   |dxx   |d   z  cc<   || dxx   dz  cc<   || d	xx   |d   z  cc<   q t        |      D cg c]  }||   	 c}S c c}w )
Nr   r   )	r   r7   usespassRows	blockRows	mixedRowspassUses	blockUses	mixedUsesr7   r	   r@   r   RowsUses)
setdefaultr>   r!   )r7   
by_tilesetr=   itemkindkeys         r   summaryrN   R   s    "$J ,$$	Ny>

 S!VVG$vT]q vT]s7|+',( (.j'9:JsO:::s   Bc                   t        |       }g d}|D ]J  }|j                  d|d    d|d    d|d    d|d    d|d	    d|d
    d|d    d|d    d|d    d       L |j                  g d       | D ]h  }dj                  d |d   D              }d|d    d}|j                  d|d    d|d    d|d    dt	        |       d|d    d|d    d| d| d       j |j                  d       dj                  |      S )N)
z# Tile Class Gaps z:Generated from `out/maps.js` and `data/tile_classes.json`.rP   a  These are the highest-use `layer0` tile IDs that are still not explicitly classified as pass/block. `fallback pass/block` shows how the temporary runtime `layer0 > 0` rule currently treats them. `mixed` rows need map-context review before promoting them to a layer0-only class.rP   z
## SummaryrP   zN| tileset | rows | uses | pass rows/uses | block rows/uses | mixed rows/uses |z*| --- | ---: | ---: | ---: | ---: | ---: |z| r   z | r7   r@   rA   /rD   rB   rE   rC   rF   z |)rP   z## RowsrP   z^| tileset | tile | count | fallback | fallback pass | fallback block | review | example maps |z5| --- | ---: | ---: | --- | ---: | ---: | --- | --- |z, c              3  8   K   | ]  }|d     d|d    d  yw)r   (r   )Nr   ).0rK   s     r   	<genexpr>zmarkdown.<locals>.<genexpr>   s-      
 E{m1T']O1-
s   r    z../extract_fld/z.cnsr   r   r   r   z	 | [CNS](z) | rP   
)rN   r(   extendjoinr>   )r7   	summarieslinesrK   r=   r    reviews          r   markdownr]   k   s   IE  
i!T&\N#d6l^3J $z"2!33K !4#4"5SK !4#4"5R9	

 
LL    
99 
J
 
 #3y>"2$7Y CK=CL>S!"#>"#3s?';&< =HD
".	

 
LL99Ur   c                 J   t        j                  t              } | j                  dt        t
        dz         | j                  dt        t        dz         | j                  dt        t
        dz         | j                  d	t        t
        d
z         | j                  dt        t
        dz         | j                  dt        d       | j                  dt        d       | j                         }t        t        |j                        t        |j                        |j                  |j                        }|j                   j"                  j%                  dd       |j                   j'                  t)        j*                  |dd      dz   d       |j,                  j'                  t)        j*                  t/        |      dd      dz   d       |j0                  j'                  t3        |      d       t5        dt7        |       d|j0                          y )N)descriptionz--mapszmaps.js)typedefaultz	--classesztile_classes.jsonz
--json-outztile_class_gaps.jsonz--summary-json-outztile_class_gap_summary.jsonz--outztile_class_gaps.mdz--limit-per-tileset   z--example-limit   T)parentsexist_okF   )ensure_asciiindentrW   zutf-8)encodingzwrote z tile class gap rows -> )argparseArgumentParser__doc__add_argumentr   OUTDATAint
parse_argsr;   r   r)   load_tile_classesr*   r+   r,   json_outparentmkdir
write_textjsondumpssummary_json_outrN   r
   r]   printlen)parserargsr7   s      r   mainr~      s   $$9F
tS9_E
$?R8RS
4?U9UV
,4GdAde
dC:N4NO
-CD
)Q?D$))$,,'	D 	MMtd;MMTZZ5KdR]de$$TZZEZ[%\_c%cnu$vHH9	F3t9+5dhhZ
@Ar   __main__)rb   rc   )
r)   zdict[str, dict]r*   zdict[str, dict[str, list[int]]]r+   rp   r,   rp   return
list[dict])r=   dictr   str)r7   r   r   r   )r7   r   r   r   )r   None)rl   
__future__r   rj   rw   collectionsr   r   pathlibr   summarize_map_tilesr   tile_classesr   rr   __file__resolverd   ROOTrn   ro   r;   r>   rN   r]   r~   __name__r   r   r   <module>r      s    O "   ,  ) 2 H~''*
Ulf}  	2
2,2 2 	2
 2j;2+\B0 zF r   