Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lufia2ac: coop support #1868

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ class CommonContext:
server_locations: typing.Set[int] # all locations the server knows of, missing_location | checked_locations
locations_info: typing.Dict[int, NetworkItem]

# data storage
stored_data: typing.Dict[str, typing.Any]
stored_data_notification_keys: typing.Set[str]

# internals
# current message box through kvui
_messagebox: typing.Optional["kvui.MessageBox"] = None
Expand Down Expand Up @@ -219,6 +223,9 @@ def __init__(self, server_address: typing.Optional[str], password: typing.Option
self.server_locations = set() # all locations the server knows of, missing_location | checked_locations
self.locations_info = {}

self.stored_data = {}
self.stored_data_notification_keys = set()

self.input_queue = asyncio.Queue()
self.input_requests = 0

Expand Down Expand Up @@ -460,6 +467,21 @@ def consume_network_data_package(self, data_package: dict):
for game, game_data in data_package["games"].items():
Utils.store_data_package_for_checksum(game, game_data)

# data storage

def set_notify(self, *keys: str) -> None:
"""Subscribe to be notified of changes to selected data storage keys.

The values can be accessed via the "stored_data" attribute of this context, which is a dictionary mapping the
names of the data storage keys to the latest values received from the server.
"""
if new_keys := (set(keys) - self.stored_data_notification_keys):
self.stored_data_notification_keys.update(new_keys)
async_start(self.send_msgs([{"cmd": "Get",
"keys": list(new_keys)},
{"cmd": "SetNotify",
"keys": list(new_keys)}]))

# DeathLink hooks

def on_deathlink(self, data: typing.Dict[str, typing.Any]) -> None:
Expand Down Expand Up @@ -728,6 +750,11 @@ async def process_server_cmd(ctx: CommonContext, args: dict):
if ctx.locations_scouted:
msgs.append({"cmd": "LocationScouts",
"locations": list(ctx.locations_scouted)})
if ctx.stored_data_notification_keys:
msgs.append({"cmd": "Get",
"keys": list(ctx.stored_data_notification_keys)})
msgs.append({"cmd": "SetNotify",
"keys": list(ctx.stored_data_notification_keys)})
if msgs:
await ctx.send_msgs(msgs)
if ctx.finished_game:
Expand Down Expand Up @@ -791,7 +818,12 @@ async def process_server_cmd(ctx: CommonContext, args: dict):
# we can skip checking "DeathLink" in ctx.tags, as otherwise we wouldn't have been send this
if "DeathLink" in tags and ctx.last_death_link != args["data"]["time"]:
ctx.on_deathlink(args["data"])

elif cmd == "Retrieved":
ctx.stored_data.update(args["keys"])

elif cmd == "SetReply":
ctx.stored_data[args["key"]] = args["value"]
if args["key"] == "EnergyLink":
ctx.current_energy_link_value = args["value"]
if ctx.ui:
Expand Down
8 changes: 4 additions & 4 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def as_simple_string(self) -> str:
return ".".join(str(item) for item in self)


__version__ = "0.4.1"
__version__ = "0.4.2"
ThePhar marked this conversation as resolved.
Show resolved Hide resolved
version_tuple = tuplize_version(__version__)

is_linux = sys.platform.startswith("linux")
Expand Down Expand Up @@ -766,10 +766,10 @@ def read_snes_rom(stream: BinaryIO, strip_header: bool = True) -> bytearray:
return buffer


_faf_tasks: "Set[asyncio.Task[None]]" = set()
_faf_tasks: "Set[asyncio.Task[typing.Any]]" = set()


def async_start(co: Coroutine[typing.Any, typing.Any, bool], name: Optional[str] = None) -> None:
def async_start(co: Coroutine[None, None, typing.Any], name: Optional[str] = None) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like good typing around this function.

"""
Use this to start a task when you don't keep a reference to it or immediately await it,
to prevent early garbage collection. "fire-and-forget"
Expand All @@ -782,6 +782,6 @@ def async_start(co: Coroutine[typing.Any, typing.Any, bool], name: Optional[str]
# ```
# This implementation follows the pattern given in that documentation.

task = asyncio.create_task(co, name=name)
task: asyncio.Task[typing.Any] = asyncio.create_task(co, name=name)
_faf_tasks.add(task)
task.add_done_callback(_faf_tasks.discard)
74 changes: 53 additions & 21 deletions worlds/lufia2ac/Client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import logging
import time
import typing
import uuid
from logging import Logger
from typing import Optional
from typing import Dict, List, Optional

from NetUtils import ClientStatus, NetworkItem
from worlds.AutoSNIClient import SNIClient
from .Enemies import enemy_id_to_name
from .Items import start_id as items_start_id
from .Locations import start_id as locations_start_id
from .Options import BlueChestCount

if typing.TYPE_CHECKING:
from SNIClient import SNIContext
Expand Down Expand Up @@ -59,6 +61,18 @@ async def game_watcher(self, ctx: SNIContext) -> None:
if signature != b"ArchipelagoLufia":
return

uuid_data: Optional[bytes] = await snes_read(ctx, L2AC_TX_ADDR + 16, 16)
if uuid_data is None:
return

coop_uuid: uuid.UUID = uuid.UUID(bytes=uuid_data)
if coop_uuid.version != 4:
coop_uuid = uuid.uuid4()
snes_buffered_write(ctx, L2AC_TX_ADDR + 16, coop_uuid.bytes)

blue_chests_key: str = f"lufia2ac_blue_chests_checked_T{ctx.team}_P{ctx.slot}"
ctx.set_notify(blue_chests_key)

# Goal
if not ctx.finished_game:
goal_data: Optional[bytes] = await snes_read(ctx, L2AC_GOAL_ADDR, 10)
Expand All @@ -78,29 +92,47 @@ async def game_watcher(self, ctx: SNIContext) -> None:
await ctx.send_death(f"{player_name} was totally defeated by {enemy_name}.")

# TX
tx_data: Optional[bytes] = await snes_read(ctx, L2AC_TX_ADDR, 8)
tx_data: Optional[bytes] = await snes_read(ctx, L2AC_TX_ADDR, 12)
if tx_data is not None:
snes_items_sent = int.from_bytes(tx_data[:2], "little")
client_items_sent = int.from_bytes(tx_data[2:4], "little")
client_ap_items_found = int.from_bytes(tx_data[4:6], "little")

if client_items_sent < snes_items_sent:
location_id: int = locations_start_id + client_items_sent
location: str = ctx.location_names[location_id]
client_items_sent += 1

snes_blue_chests_checked: int = int.from_bytes(tx_data[:2], "little")
snes_ap_items_found: int = int.from_bytes(tx_data[6:8], "little")
snes_other_locations_checked: int = int.from_bytes(tx_data[10:12], "little")

blue_chests_checked: Dict[str, int] = ctx.stored_data.get(blue_chests_key) or {}
if blue_chests_checked.get(str(coop_uuid), 0) < snes_blue_chests_checked:
blue_chests_checked[str(coop_uuid)] = snes_blue_chests_checked
if blue_chests_key in ctx.stored_data:
await ctx.send_msgs([{
"cmd": "Set",
"key": blue_chests_key,
"default": {},
"want_reply": True,
"operations": [{
"operation": "update",
"value": {str(coop_uuid): snes_blue_chests_checked},
}],
}])

total_blue_chests_checked: int = min(sum(blue_chests_checked.values()), BlueChestCount.range_end)
snes_buffered_write(ctx, L2AC_TX_ADDR + 8, total_blue_chests_checked.to_bytes(2, "little"))
location_ids: List[int] = [locations_start_id + i for i in range(total_blue_chests_checked)]

loc_data: Optional[bytes] = await snes_read(ctx, L2AC_TX_ADDR + 32, snes_other_locations_checked * 2)
if loc_data is not None:
location_ids.extend(locations_start_id + int.from_bytes(loc_data[2 * i:2 * i + 2], "little")
for i in range(snes_other_locations_checked))

if new_location_ids := [loc_id for loc_id in location_ids if loc_id not in ctx.locations_checked]:
await ctx.send_msgs([{"cmd": "LocationChecks", "locations": new_location_ids}])
for location_id in new_location_ids:
ctx.locations_checked.add(location_id)
await ctx.send_msgs([{"cmd": "LocationChecks", "locations": [location_id]}])

snes_logger.info("New Check: %s (%d/%d)" % (
location,
len(ctx.locations_checked),
len(ctx.missing_locations) + len(ctx.checked_locations)))
snes_buffered_write(ctx, L2AC_TX_ADDR + 2, client_items_sent.to_bytes(2, "little"))
snes_logger.info("%d/%d blue chests" % (
len(list(loc for loc in ctx.locations_checked if not loc & 0x100)),
len(list(loc for loc in ctx.missing_locations | ctx.checked_locations if not loc & 0x100))))

ap_items_found: int = sum(net_item.player != ctx.slot for net_item in ctx.locations_info.values())
if client_ap_items_found < ap_items_found:
snes_buffered_write(ctx, L2AC_TX_ADDR + 4, ap_items_found.to_bytes(2, "little"))
client_ap_items_found: int = sum(net_item.player != ctx.slot for net_item in ctx.locations_info.values())
if client_ap_items_found > snes_ap_items_found:
snes_buffered_write(ctx, L2AC_TX_ADDR + 4, client_ap_items_found.to_bytes(2, "little"))

# RX
rx_data: Optional[bytes] = await snes_read(ctx, L2AC_RX_ADDR, 4)
Expand Down
22 changes: 12 additions & 10 deletions worlds/lufia2ac/Items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

class ItemType(Enum):
BLUE_CHEST = auto()
BOSS = auto()
CAPSULE_MONSTER = auto()
ENEMY_DROP = auto()
ENTRANCE_CHEST = auto()
IRIS_TREASURE = auto()
PARTY_MEMBER = auto()
RED_CHEST = auto()
RED_CHEST_PATCH = auto()
Expand Down Expand Up @@ -451,15 +453,15 @@ def __init__(self, name: str, classification: ItemClassification, code: Optional
# 0x0199: "Bunnysuit"
# 0x019A: "Seethru cape"
# 0x019B: "Seethru silk"
# 0x019C: "Iris sword"
# 0x019D: "Iris shield"
# 0x019E: "Iris helmet"
# 0x019F: "Iris armor"
# 0x01A0: "Iris ring"
# 0x01A1: "Iris jewel"
# 0x01A2: "Iris staff"
# 0x01A3: "Iris pot"
# 0x01A4: "Iris tiara"
"Iris sword": ItemData(0x039C, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris shield": ItemData(0x039D, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris helmet": ItemData(0x039E, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris armor": ItemData(0x039F, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris ring": ItemData(0x03A0, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris jewel": ItemData(0x03A1, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris staff": ItemData(0x03A2, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris pot": ItemData(0x03A3, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
"Iris tiara": ItemData(0x03A4, ItemType.IRIS_TREASURE, ItemClassification.progression_skip_balancing),
# 0x01A5: "Power jelly"
# 0x01A6: "Jewel sonar"
# 0x01A7: "Hook"
Expand Down Expand Up @@ -489,7 +491,7 @@ def __init__(self, name: str, classification: ItemClassification, code: Optional
# 0x01BF: "Truth key"
# 0x01C0: "Mermaid jade"
# 0x01C1: "Engine"
# 0x01C2: "Ancient key"
"Ancient key": ItemData(0x01C2, ItemType.BOSS, ItemClassification.progression_skip_balancing),
# 0x01C3: "Pretty flwr."
# 0x01C4: "Glass angel"
# 0x01C5: "VIP card"
Expand Down
8 changes: 7 additions & 1 deletion worlds/lufia2ac/Locations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import Dict

from BaseClasses import Location
from .Options import BlueChestCount

start_id: int = 0xAC0000
l2ac_location_name_to_id: Dict[str, int] = {f"Blue chest {i + 1}": (start_id + i) for i in range(88)}

l2ac_location_name_to_id: Dict[str, int] = {
**{f"Blue chest {i + 1}": (start_id + i) for i in range(BlueChestCount.range_end + 7 + 6)},
**{f"Iris treasure {i + 1}": (start_id + 0x039C + i) for i in range(9)},
"Boss": start_id + 0x01C2,
}


class L2ACLocation(Location):
Expand Down
4 changes: 2 additions & 2 deletions worlds/lufia2ac/Options.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ class BlueChestCount(Range):
more for each party member or capsule monster if you have shuffle_party_members/shuffle_capsule_monsters enabled.
(You will still encounter blue chests in your world after all the multiworld location checks have been exhausted,
but these chests will then generate items for yourself only.)
Supported values: 10 – 75
Supported values: 10 – 100
Default value: 25
"""

display_name = "Blue chest count"
range_start = 10
range_end = 75
range_end = 100
default = 25


Expand Down
32 changes: 19 additions & 13 deletions worlds/lufia2ac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from random import Random
from typing import Any, ClassVar, Dict, get_type_hints, Iterator, List, Set, Tuple

from BaseClasses import Entrance, Item, ItemClassification, MultiWorld, Region, Tutorial
from BaseClasses import Entrance, Item, ItemClassification, Location, MultiWorld, Region, Tutorial
from Options import AssembleOptions
from Utils import __version__
from worlds.AutoWorld import WebWorld, World
Expand Down Expand Up @@ -50,10 +50,11 @@ class L2ACWorld(World):
item_name_groups: ClassVar[Dict[str, Set[str]]] = {
"Blue chest items": {name for name, data in l2ac_item_table.items() if data.type is ItemType.BLUE_CHEST},
"Capsule monsters": {name for name, data in l2ac_item_table.items() if data.type is ItemType.CAPSULE_MONSTER},
"Iris treasures": {name for name, data in l2ac_item_table.items() if data.type is ItemType.IRIS_TREASURE},
"Party members": {name for name, data in l2ac_item_table.items() if data.type is ItemType.PARTY_MEMBER},
}
data_version: ClassVar[int] = 1
required_client_version: Tuple[int, int, int] = (0, 3, 6)
data_version: ClassVar[int] = 2
required_client_version: Tuple[int, int, int] = (0, 4, 2)

# L2ACWorld specific properties
rom_name: bytearray
Expand Down Expand Up @@ -107,17 +108,20 @@ def create_regions(self) -> None:
L2ACLocation(self.player, f"Chest access {i + 1}-{i + CHESTS_PER_SPHERE}", None, ancient_dungeon)
chest_access.place_locked_item(prog_chest_access)
ancient_dungeon.locations.append(chest_access)
treasures = L2ACLocation(self.player, "Iris Treasures", None, ancient_dungeon)
treasures.place_locked_item(L2ACItem("Treasures collected", ItemClassification.progression, None, self.player))
ancient_dungeon.locations.append(treasures)
for iris in self.item_name_groups["Iris treasures"]:
treasure_name: str = f"Iris treasure {self.item_name_to_id[iris] - self.item_name_to_id['Iris sword'] + 1}"
iris_treasure: Location = \
L2ACLocation(self.player, treasure_name, self.location_name_to_id[treasure_name], ancient_dungeon)
iris_treasure.place_locked_item(self.create_item(iris))
ancient_dungeon.locations.append(iris_treasure)
self.multiworld.regions.append(ancient_dungeon)

final_floor = Region("FinalFloor", self.player, self.multiworld, "Ancient Cave Final Floor")
ff_reached = L2ACLocation(self.player, "Final Floor reached", None, final_floor)
ff_reached.place_locked_item(L2ACItem("Final Floor access", ItemClassification.progression, None, self.player))
final_floor.locations.append(ff_reached)
boss = L2ACLocation(self.player, "Boss", None, final_floor)
boss.place_locked_item(L2ACItem("Boss victory", ItemClassification.progression, None, self.player))
boss: Location = L2ACLocation(self.player, "Boss", self.location_name_to_id["Boss"], final_floor)
boss.place_locked_item(self.create_item("Ancient key"))
final_floor.locations.append(boss)
self.multiworld.regions.append(final_floor)

Expand Down Expand Up @@ -155,8 +159,9 @@ def set_rules(self) -> None:

set_rule(self.multiworld.get_entrance("FinalFloorEntrance", self.player),
lambda state: state.can_reach(f"Blue chest {self.o.blue_chest_count}", "Location", self.player))
set_rule(self.multiworld.get_location("Iris Treasures", self.player),
lambda state: state.can_reach(f"Blue chest {self.o.blue_chest_count}", "Location", self.player))
for i in range(9):
set_rule(self.multiworld.get_location(f"Iris treasure {i + 1}", self.player),
lambda state: state.can_reach(f"Blue chest {self.o.blue_chest_count}", "Location", self.player))
set_rule(self.multiworld.get_location("Boss", self.player),
lambda state: state.can_reach(f"Blue chest {self.o.blue_chest_count}", "Location", self.player))
if self.o.shuffle_capsule_monsters:
Expand All @@ -170,13 +175,14 @@ def set_rules(self) -> None:
lambda state: state.has("Final Floor access", self.player)
elif self.o.goal == Goal.option_iris_treasure_hunt:
self.multiworld.completion_condition[self.player] = \
lambda state: state.has("Treasures collected", self.player)
lambda state: state.has_group("Iris treasures", self.player, int(self.o.iris_treasures_required))
elif self.o.goal == Goal.option_boss:
self.multiworld.completion_condition[self.player] = \
lambda state: state.has("Boss victory", self.player)
lambda state: state.has("Ancient key", self.player)
elif self.o.goal == Goal.option_boss_iris_treasure_hunt:
self.multiworld.completion_condition[self.player] = \
lambda state: state.has("Boss victory", self.player) and state.has("Treasures collected", self.player)
lambda state: (state.has("Ancient key", self.player) and
state.has_group("Iris treasures", self.player, int(self.o.iris_treasures_required)))

def generate_output(self, output_directory: str) -> None:
rom_path: str = os.path.join(output_directory, f"{self.multiworld.get_out_file_name_base(self.player)}.sfc")
Expand Down
Loading