
    r Jj(                    $   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d	Zd
ZdZddZddZddZddZddZ	 	 	 	 	 	 	 	 ddZddZedfddZddZedk(  r e        yy)zLSummarize event/object VM handlers that reference battle action text tables.    )annotationsN)Counter)Path)Any   out0x36
0x0041ff44
0x004d24acc                p    	 t        j                  | j                  d            S # t        $ r |cY S w xY w)Nutf-8encoding)jsonloads	read_textFileNotFoundError)pathfallbacks     D/home/exedev/hwanse/tools/summarize_battle_action_handler_context.py	load_jsonr      s5    zz$..'.:;; s   $' 55c                    | j                  d      xs g D ci c].  }|j                  d      rt        |j                  d            |0 c}S c c}w )Ntableskey)getstr)text_tablesrows     r   table_by_keyr      sO     ??8,2775> 	CGGENS   s   3Ac           	        t               }| j                  d      xs g D ]  }|j                  d      xs i }dD ]F  }|j                  |      xs g D ],  }|j                  d      }|s|t        |      xx   dz  cc<   . H |j                  d      xs i j                         D ](  \  }}|t        |      xx   t	        |xs d      z  cc<   *  t        t        |j                                     S )NblocksvmTrace)groundedControlEventsliteralTextEventsrenderEventssourceEventslineAdvanceEvents	opcodeHexr   opcodeCountsr   )r   r   r   itemsintdictsorted)event_dialogue_blockscountsblocktrace	event_keyeventopcodecounts           r   opcode_storage_countsr6   $   s    "9F&**84: 3		)$*
 
	-I 9-3 -;/3v;'1,'-
	- $ii7=2DDF 	3MFE3v;3uz?2	33 v||~&''    c                    | j                  d      xs d| j                  d      xs d| j                  d      xs d| j                  d      xs d| j                  dd      | j                  d      xs g d	S )
NrefVaHex valueHexclusterStartVaHexclusterEndVaHex
entryCountr   sampleTexts)r9   r;   r<   r=   r>   r?   )r   )r   s    r   compact_table_refr@   8   sq    GGJ'-2GGJ'-2 WW%89?R77#45;gglA.ww}-3 r7   c                $   g }| j                  d      xs g D ]  }|j                  d      dvr|j                  |j                  d      xs g |j                  d      xs d|j                  d      xs d|j                  d      xs d|j                  dd	      |j                  d
      xs g D cg c]  }t        |       c}|j                  d      xs i |j                  d      xs d|j                  d      xs dd	        |S c c}w )Nrowsclassification>   	item-menu
text-tablebattle-actionopcodeshandlerVaHexr:   endVaHextextTableRefCountr   textTableRefsdirectCallTargetCountssamples	codeBytes)	rG   rH   rI   rC   rJ   rK   rL   rM   codeBytesPrefix)r   appendr@   )event_handler_text_refsrB   r   refs       r   matched_handler_rowsrS   C   s   D&**628b 77#$,XXwwy)/RGGN39r
+1r!gg&67=2!$)<a!@ADAYA_]_a/4a&)gg.F&G&M2wwy)/R"ww{39r

 
	 K bs   2Dc                |   t        |      }|j                  d      xs i }|j                  d      xs g }|D cg c]   }t        |j                  d      xs d      " }}t        |      }t	        |       }	t        d |	D        i       }
|
j                  d      xs g }t        d |D        i       }t        |
xr |xr
 |d d g d	k(        }t        |j                  t              xs d
      }d}d}i dddg dddd|dt        d|
j                  d      xs t        d|
j                  d      xs ddt        d|j                  d      xs ddt        |      d|d d dt        |      d|d |
j                  d!      xs i d"|d#|d
kD  d$|ddd||	|t        |      |d
k(  ||d%g d&d'd(S c c}w ))NatahoActionsentriestextr:   c              3     K   | ]6  }t         |j                  d g       v r|j                  d      t        k(  r| 8 yw)rG   rH   N)BATTLE_ACTION_OPCODEr   BATTLE_ACTION_HANDLER.0r   s     r   	<genexpr>z build_summary.<locals>.<genexpr>b   s=      	
#swwy"'=='+@@ 	
s   <>rK   c              3  R   K   | ]  }|j                  d       t        k(  s| ! yw)r<   N)r   BATTLE_ACTION_TABLE)r\   rR   s     r   r]   z build_summary.<locals>.<genexpr>k   s"     [sww/B'CGZ'Z[s   ''   )u   정권u   돌려차기u	   던지기r   FscopezLbattle action text handler context inside the event/object VM dispatch tablesource)z out/event_handler_text_refs.jsonzout/text_tables.jsonzout/event_dialogue_blocks.jsonpromotionStatusz5battle-action-handler-grounded-entry-dispatch-missingbattleActionHandlerGroundedbattleActionOpcodeHexbattleActionHandlerVaHexrH   battleActionHandlerEndVaHexrI   battleActionTableVaHexbattleActionTableKeyr   battleActionTableEntryCountbattleActionTableSamples   $battleActionHandlerTextTableRefCountbattleActionHandlerTableRef)battleActionHandlerDirectCallTargetCountsrL   %battleActionHandlerStorageOpcodeCount#battleActionOpcodeInDialogueStoragebattleEntryDispatchProofFound)rd   battleActionTextTableGrounded+battleActionOpcodeAbsentFromDialogueStoragerr   originalCombatFormulaIdentified)zRevent/object VM command storage that executes opcode 0x36 from a battle-entry pathzKhandler/call edge from event VM battle action handler to battle scene entryz5enemy row or formation table consumed by this handlerzHoriginal combat formula or reward table writes reached from this handleru  Event/object VM opcode 0x36 is grounded as a battle-action text handler because handler 0x0041ff44 references the 0x004d24ac action text table (정권/돌려차기/던지기/마시기). That is useful original battle UI/action evidence, but opcode 0x36 does not appear in the current dialogue storage scan and there is no proven event path from a btl_* dialogue candidate into a battle scene, enemy row, formation, reward table, or combat formula.)enemyRowProofFoundformationTableProofFoundrewardTableProofFoundru   matchedHandlerRowschecksmissingEvidence
conclusion)r   r   r   r6   rS   nextboolr+   rY   rZ   r_   len)rQ   r   r.   r   ataho_actionsaction_entriesr   action_textsstorage_countsrB   
battle_rowbattle_refs	table_refbattle_action_handler_groundedstorage_opcode_count!battle_entry_dispatch_proof_found"original_combat_formula_identifieds                    r   build_summaryr   V   s   
 +&FJJ~.4"M"&&y17RN:HI3C-2.ILI*+@AN 78D	
	

 	J ..17RK[[
I &* 	H	H! GG&"
 ~112FGL1M(-%).&/_/ 
/ 	R/ 	&'E/ 	 !5/ 	#JNN>$B$[F[/ 	&z~~j'A'GR/ 	!"5/ 	 1 1% 8 JN/ 	&s>':/ 	#L!$4/  	/K0@!/" 	&y#/$ 	4Z^^D\5]5cac%/& 	01E'/( 	./Ca/G)/* 	()J+/, $$)!&+M"+I-1)_;OST;T-N/Q

LS/ /1 Js   %F9c                |   dj                  d | d   j                         D              }dj                  d | j                  d      xs g D              }dj                  d | d   D              }dj                  d	d
dddddddddt        j                  | d          ddt        j                  | d          ddt        j                  | d          dt        j                  | d          dt        j                  | d          dt        j                  | d           d	d!d"d#| d$d%d&d#| d$d'd(| d)d*d+dg      S ),Nr:   c           	   3     K   | ]?  \  }}d t        j                  |       dt        j                  t        |             d A yw)<tr><td></td><td><code>z</code></td></tr>N)htmlescaper   )r\   r   values      r   r]   zhtml_page.<locals>.<genexpr>   sD      C 4;;s#$ODKKE
4K3LL]^s   AArz   c              3  B  K   | ]  }d t        j                  dj                  |j                  d      xs g              dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d	      xs d       d
t        j                  t	        |j                  d      xs d             d
t        j                  |j                  d      xs d       d  yw)r   z, rG   r   rH   r:   z..rI   z</code></td><td>rC   z	</td><td>rJ   r   rM   -z
</td></tr>N)r   r   joinr   r   r[   s     r   r]   zhtml_page.<locals>.<genexpr>   s      	 	{{499SWWY%7%=2>?@ A[[!8!>B?@4;;swwWaObOhfhCiBj k{{377#34:;< ={{3sww':;@qABC D{{3779-456 7		s   DDry   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li>z</li>N)r   r   )r\   items     r   r]   zhtml_page.<locals>.<genexpr>   s#     ]$T[[./u5]s   #%r{   
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>Battle Action Handler Context</title>a    <style>body{font-family:system-ui,sans-serif;background:#101010;color:#eee;margin:24px}table{border-collapse:collapse;width:100%;margin:16px 0}td,th{border:1px solid #333;padding:6px 8px;vertical-align:top}th{background:#1d1d1d}code{color:#f5d76e}</style>z</head>z<body>z(  <h1>Battle Action Handler Context</h1>z  <p>r|   z</p>z  <p>Promotion status <code>rc   z</code>.</p>z  <p>Opcode <code>re   z</code>, handler <code>rf   z</code>, table <code>rh   z</code> / <code>ri   z  <h2>Checks</h2>z=  <table><thead><tr><th>check</th><th>value</th></tr></thead>z	  <tbody>z</tbody></table>z  <h2>Matched Handler Rows</h2>zr  <table><thead><tr><th>opcodes</th><th>handler</th><th>class</th><th>table refs</th><th>samples</th></tr></thead>z  <h2>Missing Evidence</h2>z  <ul>z</ul>z</body>z</html>)r   r*   r   r   r   )summary
check_rowshandler_rowsmissings       r   	html_pager      s    !(+113 J 77 	 ;;34:	 	L gg]'J[B\]]G99"P8 	L2
GL12348
&t{{7;L3M'N&O|\
T[[1H)IJK LW%?@AB CW%=>?@ AW%;<=>l	L 	G
J</0)|
L>!12%
	
7  r7   c                   |j                  dd       |dz  }|j                  t        j                  | dd      dz   d	       |9|j                  j                  dd       |j                  t        |       d	       |S )
NT)parentsexist_okz"battle_action_handler_context.jsonF   )ensure_asciiindentr   r   r   )mkdir
write_textr   dumpsparentr   )r   out_dirhtml_outjson_outs       r   write_outputsr      s    MM$M.==H

7q9D@   dT:Ig.AOr7   c                    t        j                         } | j                  dt        t               | j                  dt               | j                         }t        t        |j                  dz  i       t        |j                  dz  i       t        |j                  dz  i             }t        ||j                  |j                        }t        d|        y )	Nz	--out-dir)typedefaultz
--html-out)r   zevent_handler_text_refs.jsonztext_tables.jsonzevent_dialogue_blocks.jsonz'wrote battle action handler context -> )argparseArgumentParseradd_argumentr   OUT
parse_argsr   r   r   r   r   print)parserargsr   r   s       r   mainr      s    $$&F
$<
40D$,,!??D$,,!33R8$,,!==rBG
 WdllDMMBH	3H:
>?r7   __main__)r   r   r   r   returnr   )r   r,   r   zdict[str, dict])r.   r,   r   zdict[str, int])r   r,   r   r,   )rQ   r,   r   z
list[dict])rQ   r,   r   r,   r.   r,   r   r,   )r   r,   r   r   )r   r,   r   r   r   zPath | Noner   r   )r   None)__doc__
__future__r   r   r   r   collectionsr   pathlibr   typingr   __file__resolver   ROOTr   rY   rZ   r_   r   r   r6   r@   rS   r   r   r   r   __name__ r7   r   <module>r      s    R "       H~''*
Ul $ " ((&O!OO  O 
	Od,^ 25d 
@ zF r7   