dvruette / pygba

A Python wrapper around the Game Boy Advance emulator mGBA with built-in support for gymnasium environments.
MIT License
10 stars 1 forks source link

Parsing items #3

Open MarcoMeter opened 2 weeks ago

MarcoMeter commented 2 weeks ago

In pokemon_emerald.py you can parse the items like this

state["bags"] = {"items": parse_items(save_block_1["bagPocket_Items"], save_block_2["encryptionKey"]),
                         "key_items": parse_items(save_block_1["bagPocket_KeyItems"], save_block_2["encryptionKey"]),
                         "balls": parse_items(save_block_1["bagPocket_PokeBalls"], save_block_2["encryptionKey"]),
                         "tms": parse_items(save_block_1["bagPocket_TMHM"], save_block_2["encryptionKey"]),
                         "berries": parse_items(save_block_1["bagPocket_Berries"], save_block_2["encryptionKey"])}
def parse_items(bytes, encryption_key):
    result = []
    key_bytes = struct.pack('I', encryption_key)

    for i in range(0, len(bytes), 4):
        item_id = struct.unpack('H', bytes[i:i+2])[0]
        if item_id == 0:
            continue
        encrypted_quantity = struct.unpack('H', bytes[i+2:i+4])[0]
        quantity = encrypted_quantity ^ struct.unpack('H', key_bytes[:2])[0]
        result.append((item_id, quantity))
    return result
dvruette commented 2 weeks ago

This is great! Do you want to make a PR? Otherwise I can also add it next time.