
    NIj1                       d Z ddlmZ ddlZddlZddl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
ZddZddZdddZdddZdddZdddZddZedk(  r e        yy)zACompare layer0 and layer1 tile-class fallback passability by map.    )annotationsN)Path)	load_maps)load   outdatac                J    |t        | j                  d      xs g       v rdS |S )NemptyLayer1Tilesr   )setget)infotiles     ?/home/exedev/hwanse/tools/summarize_collision_fallback_delta.pyeffective_layer1r      s(    DHH%78>B??1ITI    c                z   |j                  d      xs |j                  d      gd   }|j                  |i       }t        |j                  dg             }t        |j                  dg             }|j                  dg       D ch c]  }t        |       }}|j                  dg       D ch c]  }t        |       }	}d}
d}d}d}d}g }t        t	        |d   d   |d   d	               D ]  \  }\  }}||f}||v s||v r|
d	z  }
||	v s||v r|d	z  })|dkD  }|dkD  xr t        ||      dk(  }|r9|s7|d	z  }t        |      d
k  s\|j                  ||d   z  ||d   z  ||d       |r|r|d	z  }|d	z  } t        |d   d         }| |||
|||||rt        |dz  |z  d      |d
S d|d
S c c}w c c}w )NlayerTilesetstilesetr   passblock	passPairs
blockPairslayersr      width)xylayer0layer1d      )
mapr   	tileCountexplicitPassexplicitBlockfallbackSamePassfallbackSameBlocknewlyPassablenewlyPassablePctexamples)	r   r   tuple	enumeratezipr   lenappendround)namer   classesr   entry
pass_tilesblock_tilespair
pass_pairsblock_pairsexplicit_passexplicit_blockfallback_same_passfallback_same_blocknewly_passabler+   indexr   
raw_layer1layer0_passlayer1_pass
tile_counts                         r   summarize_maprD      s7   xx(ATXXi-@,A1EGKK$EUYYvr*+Jeii,-K*/))K*DE$%+EJE+099\2+FG45;GKGMNNH'0T(^A5FXWXHY1Z'[ %##

#::!5QM;&K"7aNqjqjL%5dJ%G1%L{aN8}q g.$w-/$(	!  [!#1$/%2 T(^A&'J%'.0'KUE.3"6"CQG  \] I FGs   F3-F8c           	     |    t        | j                               D cg c]  \  }}t        |||       c}}S c c}}w )N)sorteditemsrD   )mapsr3   r2   r   s       r   	summarizerI   N   s<     !.D$ 	dD'*  s   8c                J   t        | d d      d d }t        | d d      d d }g d}|D ]Z  dj                  fdd	   d d
 D              xs d}|j                  dd    dd    dd    dd    dd    d| d       \ |j                  g d       |D ]Z  dj                  fdd	   d d
 D              xs d}|j                  dd    dd    dd    dd    dd    d| d       \ |j                  g d       | D ]o  dj                  fdd	   D              xs d}|j                  dd    dd    dd    dd    dd    dd    dd    dd    dd    d| d       q |j                  d       d j                  |      S )!Nc                    | d   S Nr)    rows    r   <lambda>zmarkdown.<locals>.<lambda>V       S-A r   Tkeyreverse   c                    | d   S Nr*   rM   rN   s    r   rP   zmarkdown.<locals>.<lambda>W       3/A+B r   )
z# Collision Fallback Delta z:Generated from `out/maps.js` and `data/tile_classes.json`.rY   a5  Compares the current runtime fallback (`layer0 > 0`) against the previous layer1-sensitive fallback (`layer0 > 0 && effective layer1 == 0`) after explicit pass/block tile and pair classes are applied. `newly passable` is the number of cells that the current fallback opens compared with the previous fallback.rY   z## Top By CountrY   3| map | tileset | newly passable | first examples || --- | --- | ---: | --- |, c              3  ~   K   | ]4  }d |d    d|d    d dd    d|d    d|d    d|d    d|d    d	 6 y
w[r   ,r   ](/game.html?map=r#   &startTile=&focusTile=&collision=1&overview=1)NrM   .0itemrO   
web_prefixs     r   	<genexpr>zmarkdown.<locals>.<genexpr>g   o      
  S	{!DI;bOCJ<$s)Ad3i[DI;aS	{Rjl
   :=r+      -z| r#   z | r   r)   /r$    (r*   z%) | z |)rY   z## Top By PercentrY   rZ   r[   c              3  ~   K   | ]4  }d |d    d|d    d dd    d|d    d|d    d|d    d|d    d	 6 y
wr^   rM   rf   s     r   rj   zmarkdown.<locals>.<genexpr>x   rk   rl   )rY   z## All MapsrY   zy| map | tileset | newly passable | explicit pass | explicit block | fallback same pass | fallback same block | examples |z6| --- | --- | ---: | ---: | ---: | ---: | ---: | --- |c              3     K   | ]@  }d |d    d|d    d|d    d|d    d d	d
    d|d    d|d    d|d    d|d    d B yw)r_   r   r`   r    L0=r    L1=r    ra   rb   r#   rc   rd   re   NrM   rf   s     r   rj   zmarkdown.<locals>.<genexpr>   s      
  S	{!DI;d4>*:$tH~>NbQ[P\\klopulvkw$s)Ad3i[DI;aS	{Rjl
s   AA	r%   r&   r'   r(   rY   
)rF   joinr0   extend)rowsri   	top_counttop_pctlinesr+   rO   s    `    @r   markdownr|   U   s   t!A4PQTRTUITBDQRUSUVGE  	
99 
 J+
 
  	 	
 	UCI/s?#$Ac+&6%7r#>P:Q9RRWX`Waace	
	
 
LL    	
99 
 J+
 
  	 	
 	UCI/s?#$Ac+&6%7r#>P:Q9RRWX`Waace	
	
 
LL    
99 
 J
 
  	 	
 	UCI/s?#$Ac+&6%7r#>P:Q9RRW>"#3s?';&<C%&'s3/B+C*DCzQSU	

 
LL99Ur   c                J   g }| d   d | D ]  }|d    d|d    }|r| d|d    d|d    }| d	| d
    d|d    d|d    d|d    d|d    d}|j                  dt        j                  |       dt        j                  |       d        dj                  |      xs dS )Nr+   r   r`   r   rs   r   rt   r    rb   r#   rc   rd   &collision=1&overview=1z	<a href="">z</a>r\   rn   )r0   htmlescaperv   )rO   ri   limit
with_tileslinksrh   labelhrefs           r   example_linksr      s    EJ' P9+QtCyk*gT$x.!1d8n5EFEl/#e*$s)Ad3i[DI;aS	{Rik 	 	yT!2 32dkk%6H5INOP 99U"s"r   c                   g }| D ]U  }| d|d    d}|r|j                  dj                  ddt        j                  |       dt        j                  |d          dd	t        j                  |d
          dd	|d    d|d    d|d    dd	t	        ||       ddg             |j                  dj                  ddt        j                  |       dt        j                  |d          dd	t        j                  |d
          dd	|d    d|d    d|d    dd	|d    dd	|d    dd	|d    dd	|d    dd	t	        ||dd       ddg
             X |rdnd}dj                  dd| dddj                  |      dd g      S )!Nrb   r#   r~   ru   z<tr>z  <td><a href="r   z	</a></td>z  <td>r   z</td>r)   ro   r$   rp   r*   z%)</td>z</tr>r%   r&   r'   r(   r   TzM<tr><th>map</th><th>tileset</th><th>newly passable</th><th>examples</th></tr>z<tr><th>map</th><th>tileset</th><th>newly passable</th><th>explicit pass</th><th>explicit block</th><th>fallback same pass</th><th>fallback same block</th><th>examples</th></tr>z<table>z<thead>z</thead>z<tbody>z</tbody>z</table>)r0   rv   r   r   r   )rx   ri   compactbodyrO   	open_hrefheaderss          r   
table_htmlr      s   D !l/#e*=TU	KK		)$++i*@)ADKKPSTYPZD[C\\ef S^!< =UC _!5 6aK8H7ICPbLcKddkl sJ!? @F	 KK		)$++i*@)ADKKPSTYPZD[C\\ef S^!< =UC _!5 6aK8H7ICPbLcKddkl ^!4 5U; _!5 6e< %7!8 9? %8!9 :%@ sJ4!H IO!D  	X A 
 99i77)8!<iSWZdfpqrrr   c                   t        | d d      d d }t        | d d      d d }t        d | D              }t        d | D              }|rt        |dz  |z  d	      nd
}dj                  g ddddddddddddddddddddd d!d"d#d$d%d&d'd(| d)d(| d*| d+d(t        d, | D               d-d.d/t	        ||d      d0t	        ||d      d1t	        | |d2      d3d4d5      S )6Nc                    | d   S rL   rM   rN   s    r   rP   zhtml_page.<locals>.<lambda>   rQ   r   TrR   rU   c                    | d   S rW   rM   rN   s    r   rP   zhtml_page.<locals>.<lambda>   rX   r   c              3  &   K   | ]	  }|d      yw)r)   NrM   rg   rO   s     r   rj   zhtml_page.<locals>.<genexpr>   s     9SC(9   c              3  &   K   | ]	  }|d      yw)r$   NrM   r   s     r   rj   zhtml_page.<locals>.<genexpr>   s     73c+&7r   r!   r"   r   ru   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">z)  <title>Collision Fallback Delta</title>z	  <style>zd    :root { color-scheme: dark; font-family: system-ui, sans-serif; background: #111; color: #eee; }z&    body { margin: 0; padding: 24px; }z,    h1 { margin: 0 0 8px; font-size: 24px; }z0    h2 { margin: 26px 0 10px; font-size: 18px; }zM    p { margin: 0 0 14px; color: #bbb; max-width: 980px; line-height: 1.45; }zO    .summary { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0 4px; }zc    .metric { border: 1px solid #333; background: #181818; border-radius: 6px; padding: 9px 11px; }z7    .metric strong { display: block; font-size: 18px; }z2    .metric span { color: #aaa; font-size: 12px; }zX    table { width: 100%; border-collapse: collapse; margin: 0 0 16px; font-size: 13px; }zi    th, td { border-bottom: 1px solid #303030; padding: 7px 8px; text-align: left; vertical-align: top; }zR    th { position: sticky; top: 0; background: #181818; z-index: 1; color: #ddd; }zp    td:nth-child(3), td:nth-child(4), td:nth-child(5), td:nth-child(6), td:nth-child(7) { white-space: nowrap; }z0    a { color: #7cc7ff; text-decoration: none; }z+    a:hover { text-decoration: underline; }z
  </style>z</head>z<body>z#  <h1>Collision Fallback Delta</h1>z  <p>Compares the current runtime fallback (<code>layer0 &gt; 0</code>) against the previous layer1-sensitive fallback (<code>layer0 &gt; 0 &amp;&amp; effective layer1 == 0</code>) after explicit pass/block classes are applied.</p>z  <section class="summary">z     <div class="metric"><strong>z0</strong><span>newly passable cells</span></div>z%</strong><span>of z cells</span></div>c              3  ,   K   | ]  }|d    s	d  yw)r)   r   NrM   r   s     r   rj   zhtml_page.<locals>.<genexpr>   s     2]OH\12]s   
z)</strong><span>maps affected</span></div>z  </section>z  <h2>Top By Count</h2>z  <h2>Top By Percent</h2>z  <h2>All Maps</h2>Fz</body>z</html>rY   )rF   sumr1   rv   r   )rx   ri   ry   rz   	total_newtotal_tiles	total_pcts          r   	html_pager      s   t!A4PQTRTUITBDQRUSUVG9D99I7$77K;Fi#o3Q7AI99*	
*	
*	
 *	
 '	*	

 U*	
 8*	
 *	
 s*	
 5*	
 ;*	
 ?*	
 \*	
 ^*	
 r*	
 F*	
  A!*	
" g#*	
$ x%*	
& a'*	
( )*	
* ?+*	
, :-*	
. /*	
0 1*	
2 3*	
4 25*	
6 v7*	
8 *9*	
: /yk9ij;*	
< /yk9L[MYlm=*	
> /s2]2]/].^  _H  I?*	
@ A*	
B &C*	
D y*d3E*	
F (G*	
H w
D1I*	
J "K*	
L tZ/M*	
N O*	
P Q*	
R S*	
, ,r   c                 \   t        j                  t              } | j                  dt        t
        dz         | j                  dt        t        dz         | j                  dt        t
        dz         | j                  d	t        d d
       | j                         }t        t        |j                        t        |j                              }|j                  j                  j                  dd       |j                  j!                  t#        j$                  |dd      dz   d       |j&                  M|j&                  j                  j                  dd       |j&                  j!                  t)        |      d       t+        dt-        |       d|j                          y )N)descriptionz--mapszmaps.js)typedefaultz	--classesztile_classes.jsonz
--json-outzcollision_fallback_delta.jsonz
--html-outz!Optional HTML report output path.)r   r   helpT)parentsexist_okF)r`   :)ensure_ascii
separatorsru   zutf-8)encodingzwrote z" collision fallback delta rows -> )argparseArgumentParser__doc__add_argumentr   OUTDATA
parse_argsrI   r   rH   load_tile_classesr3   json_outparentmkdir
write_textjsondumpshtml_outr   printr/   )parserargsrx   s      r   mainr     sT   $$9F
tS9_E
$?R8RS
4?^9^_
4DghDYtyy)+<T\\+JKDMMtd;MM

4e
CdJ   }} ""4$"?  47 C	F3t9+?
OPr   __main__)r   dictr   intreturnr   )r2   strr   r   r3   dict[str, dict]r   r   )rH   r   r3   r   r   
list[dict])z../web)rx   r   ri   r   r   r   )rm   F)
rO   r   ri   r   r   r   r   boolr   r   )F)rx   r   ri   r   r   r   r   r   )r   None)r   
__future__r   r   r   r   pathlibr   summarize_map_tilesr   tile_classesr   r   __file__resolver   ROOTr   r   r   rD   rI   r|   r   r   r   r   __name__rM   r   r   <module>r      s    G "     ) 2 H~''*
Ulf}J4n@F#'sT2jQ( zF r   