Skip to content

Commit

Permalink
Fix mypy errors in Location.py and Messages.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fenhl committed Mar 27, 2024
1 parent 5ee13ba commit 9d71f80
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
6 changes: 5 additions & 1 deletion Location.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ def LocationFactory(locations: str | list[str]) -> Location | list[Location]:
if location in location_table:
match_location = location
else:
match_location = next(filter(lambda k: k.lower() == location.lower(), location_table), None)
match_location = None
for k in location_table:
if k.lower() == location.lower():
match_location = k
break
if match_location is not None:
type, scene, default, addresses, vanilla_item, filter_tags = location_table[match_location]
if addresses is None:
Expand Down
51 changes: 20 additions & 31 deletions Messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@


# convert byte array to an integer
def bytes_to_int(data: bytes, signed: bool = False) -> int:
def bytes_to_int(data: bytes | list[int], signed: bool = False) -> int:
return int.from_bytes(data, byteorder='big', signed=signed)


Expand Down Expand Up @@ -697,7 +697,7 @@ def write(self, rom: Rom, text_start: int, offset: int) -> int:
class Message:
def __init__(self, raw_text: list[int] | bytearray | str, index: int, id: int, opts: int, offset: int, length: int) -> None:
if isinstance(raw_text, str):
raw_text = encode_text_string(raw_text)
raw_text = bytearray(encode_text_string(raw_text))
elif not isinstance(raw_text, bytearray):
raw_text = bytearray(raw_text)

Expand Down Expand Up @@ -924,7 +924,7 @@ def update_message_by_id(messages: list[Message], id: int, text: bytearray | str
if index >= 0:
update_message_by_index(messages, index, text, opts)
else:
add_message(messages, text, id, opts)
add_message(messages, text, id, opts or 0x00)


# Gets the message by its ID. Returns None if the index does not exist
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def display(self) -> str:
def write(self, rom: Rom, shop_table_address: int, index: int) -> None:
entry_offset = shop_table_address + 0x20 * index

data = []
data: list[int] = []
data += int_to_bytes(self.object, 2)
data += int_to_bytes(self.model, 2)
data += int_to_bytes(self.func1, 4)
Expand Down Expand Up @@ -1138,14 +1138,14 @@ def make_player_message(text: str) -> str:
# make sure to call this AFTER move_shop_item_messages()
def update_item_messages(messages: list[Message], world: World) -> None:
new_item_messages = ITEM_MESSAGES + KEYSANITY_MESSAGES
for id, text in new_item_messages:
for id, new_item_text in new_item_messages:
if world.settings.world_count > 1:
update_message_by_id(messages, id, make_player_message(text), 0x23)
update_message_by_id(messages, id, make_player_message(new_item_text), 0x23)
else:
update_message_by_id(messages, id, text, 0x23)
update_message_by_id(messages, id, new_item_text, 0x23)

for id, (text, opt) in MISC_MESSAGES:
update_message_by_id(messages, id, text, opt)
for id, (misc_text, opt) in MISC_MESSAGES:
update_message_by_id(messages, id, misc_text, opt)

# run all keysanity related patching to add messages for dungeon specific items
def add_item_messages(messages: list[Message], shop_items: Iterable[ShopItem], world: World) -> None:
Expand Down Expand Up @@ -1200,7 +1200,7 @@ def read_fffc_message(rom: Rom) -> Message:


# write the messages back
def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[list[int]] = None,
def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[Iterable[int]] = None,
always_allow_skip: bool = True, speed_up_text: bool = True) -> None:
rom.update_dmadata_record_by_key(ENG_TEXT_START, ENG_TEXT_START, ENG_TEXT_START + ENG_TEXT_SIZE_LIMIT)
rom.update_dmadata_record_by_key(JPN_TEXT_START, JPN_TEXT_START, JPN_TEXT_START + JPN_TEXT_SIZE_LIMIT)
Expand Down Expand Up @@ -1288,41 +1288,28 @@ def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[lis


# shuffles the messages in the game, making sure to keep various message types in their own group
SHOP_ITEM_MESSAGES: list[int] = []
SCRUBS_MESSAGE_IDS: list[int] = []
def shuffle_messages(messages: list[Message], except_hints: bool = True) -> list[int]:
if not hasattr(shuffle_messages, "shop_item_messages"):
shuffle_messages.shop_item_messages = []
if not hasattr(shuffle_messages, "scrubs_message_ids"):
shuffle_messages.scrubs_message_ids = []

hint_ids = (
GOSSIP_STONE_MESSAGES + TEMPLE_HINTS_MESSAGES +
[data['id'] for data in misc_item_hint_table.values()] +
[data['id'] for data in misc_location_hint_table.values()] +
[message_id for (message_id, message) in KEYSANITY_MESSAGES] + shuffle_messages.shop_item_messages +
shuffle_messages.scrubs_message_ids +
[0x5036, 0x70F5] # Chicken count and poe count respectively
)

permutation = [i for i, _ in enumerate(messages)]

def is_exempt(m: Message) -> bool:
hint_ids = (
GOSSIP_STONE_MESSAGES + TEMPLE_HINTS_MESSAGES +
[data['id'] for data in misc_item_hint_table.values()] +
[data['id'] for data in misc_location_hint_table.values()] +
[message_id for (message_id, message) in KEYSANITY_MESSAGES] +
shuffle_messages.shop_item_messages +
shuffle_messages.scrubs_message_ids +
[message_id for message_id, _ in KEYSANITY_MESSAGES] +
SHOP_ITEM_MESSAGES + SCRUBS_MESSAGE_IDS +
[0x5036, 0x70F5] # Chicken count and poe count respectively
)
shuffle_exempt = [
0x045C, # Adult shooting gallery helping message when the player wins without having a bow
0x208D, # "One more lap!" for Cow in House race.
0xFFFC, # Character data from JP table used on title and file select screens
]
is_hint = (except_hints and m.id in hint_ids)
is_error_message = (m.id == ERROR_MESSAGE)
is_shuffle_exempt = (m.id in shuffle_exempt)
is_hint = except_hints and m.id in hint_ids
is_error_message = m.id == ERROR_MESSAGE
is_shuffle_exempt = m.id in shuffle_exempt
return is_hint or is_error_message or m.is_id_message() or is_shuffle_exempt

have_goto = list(filter(lambda m: not is_exempt(m) and m.has_goto, messages))
Expand Down Expand Up @@ -1373,7 +1360,8 @@ def update_warp_song_text(messages: list[Message], world: World) -> None:
for id, entr in msg_list.items():
if 'warp_songs_and_owls' in world.settings.misc_hints or not world.settings.warp_songs:
destination = world.get_entrance(entr).connected_region
destination_name = HintArea.at(destination)
assert destination is not None
destination_name: Any = HintArea.at(destination)
color = COLOR_MAP[destination_name.color]
if destination_name.preposition(True) is not None:
destination_name = f'to {destination_name}'
Expand All @@ -1388,6 +1376,7 @@ def update_warp_song_text(messages: list[Message], world: World) -> None:
for id, entr in owl_messages.items():
if 'warp_songs_and_owls' in world.settings.misc_hints:
destination = world.get_entrance(entr).connected_region
assert destination is not None
destination_name = HintArea.at(destination)
color = COLOR_MAP[destination_name.color]
if destination_name.preposition(True) is not None:
Expand Down
11 changes: 6 additions & 5 deletions Patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from ItemPool import song_list, trade_items, child_trade_items
from Location import Location, DisableType
from LocationList import business_scrubs
from Messages import read_messages, update_message_by_id, read_shop_items, update_warp_song_text, \
import Messages
from Messages import SCRUBS_MESSAGE_IDS, SHOP_ITEM_MESSAGES, read_messages, update_message_by_id, read_shop_items, update_warp_song_text, \
write_shop_items, remove_unused_messages, make_player_message, \
add_item_messages, repack_messages, shuffle_messages, \
get_message_by_id, TextCode, new_messages
Expand Down Expand Up @@ -1988,7 +1989,7 @@ def calculate_traded_flags(world):
shop_items[0x000A].description_message = 0x80B5
shop_items[0x000A].purchase_message = 0x80BE

shuffle_messages.shop_item_messages = []
Messages.SHOP_ITEM_MESSAGES = []

# kokiri shop
shop_locations = [location for location in world.get_region('KF Kokiri Shop').locations if location.type == 'Shop'] # Need to filter because of the freestanding item in KF Shop
Expand Down Expand Up @@ -2115,11 +2116,11 @@ def update_scrub_text(message: bytearray, text_replacement: list[str], default_p
set_deku_salesman_data(rom)

# Update scrub messages.
shuffle_messages.scrubs_message_ids = []
Messages.SCRUBS_MESSAGE_IDS = []
for text_id, message in scrub_message_dict.items():
update_message_by_id(messages, text_id, message)
if world.settings.shuffle_scrubs == 'random':
shuffle_messages.scrubs_message_ids.append(text_id)
Messages.SCRUBS_MESSAGE_IDS.append(text_id)

if world.settings.shuffle_grotto_entrances:
# Build the Grotto Load Table based on grotto entrance data
Expand Down Expand Up @@ -3071,7 +3072,7 @@ def place_shop_items(rom: Rom, world: World, shop_items, messages, locations, in
shop_item.description_message = 0x8100 + message_id
shop_item.purchase_message = 0x8100 + message_id + 1

shuffle_messages.shop_item_messages.extend(
Messages.SHOP_ITEM_MESSAGES.extend(
[shop_item.description_message, shop_item.purchase_message])

if item_display.dungeonitem:
Expand Down

0 comments on commit 9d71f80

Please sign in to comment.