# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Note: Must Run "Estimate RAM Size of Enemies First"
import os, json
fn_spawns = os.path.join(os.environ["USE_KH2_GITPATH"], "KH2", "00objentry.bin")

records = json.load(open(os.path.join("..", "Estimate RAM Size of Enemies", "workspace", "enemy_records_filled.json")))
# -

GAMESTARTBYTE = 0x1c94100
STARTBYTE = 0x0
RECORD_LENGTH = 0x60
LIMITER_LOC = 0x5c
UCM_LOC = 0x8 # 2 bytes long
data = open(fn_spawns, "rb").read()
limiters = {}
index = 0
for start in range(0, len(data)-RECORD_LENGTH, RECORD_LENGTH):
    ucmloc = start+UCM_LOC
    limloc = start+LIMITER_LOC
    lim = hex(data[limloc])[2:].zfill(2).upper()
    ucm = "{}{}".format(hex(data[ucmloc+1])[2:].zfill(2), hex(data[ucmloc])[2:].zfill(2)).upper()
    extra = "{}{}{}".format(hex(data[limloc+3])[2:].zfill(2), hex(data[limloc+2])[2:].zfill(2), hex(data[limloc+1])[2:].zfill(2)).upper()
    limiters[ucm] = {"limloc": limloc, "lim": lim, "extra": extra, "index": index}
    index += 1
for k in records:
    ucm = records[k]["ucm"]
    limit = limiters[ucm]
#     if records[k]['type'] == 'enemy':
    print("{}({}): {}".format(k, ucm, limit['lim']))

GAMESTARTBYTE = 0x11c94100
SETLIMIT = '04'
for k in sorted(records.keys(), key=lambda z: records[z]["ucm"]):
    if records[k]['type'] != 'enemy':
        continue
    r = records[k]
    limit = limiters[r["ucm"]]
    address = hex(limit["limloc"]+GAMESTARTBYTE)[2:].zfill(8).upper()
    value = '{}{}'.format(limit["extra"], SETLIMIT).zfill(8).upper()
    code = '{} {}'.format(address, value)    
    print('{}\nlimiter_offset: "{}"'.format(k.ljust(30), address))

GAMESTARTBYTE  = 0x11c94100
OFFSETSTARTBYTE = 0x11C940FC
SETLIMIT = '04'
for k in sorted(records.keys(), key=lambda z: records[z]["ucm"]):
    if records[k]['type'] != 'enemy':
        continue
    r = records[k]
    limit = limiters[r["ucm"]]
    address = hex(limit["limloc"]+GAMESTARTBYTE)[2:].zfill(8).upper()
    mathed = OFFSETSTARTBYTE + (96*int(records[k]["ucm"],16))
    mathed = hex(mathed)[2:].zfill(8).upper()
    if not mathed == address:
        print("{} ({}) is wrong, off by {}".format(k,records[k]["ucm"],hex(int(mathed,16)-int(address,16))))
    else:
        print("{} ({}) is right".format(k, records[k]["ucm"]))



StarcoderdataJupyterScriptsDedupFiltered