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

Core: move PlandoConnections and PlandoTexts to the options system #2904

Merged
merged 59 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
c2be561
initial commit, works but need to port games
Silvris Nov 25, 2023
aab79bb
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Jan 2, 2024
9ec268d
port lttp and minecraft
Silvris Jan 3, 2024
226932e
rewrite can_connect to return true by default
Silvris Jan 3, 2024
1f43d4d
text support + update to always use entrance/exit
Silvris Feb 4, 2024
913a49a
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Mar 5, 2024
fea2a34
remove PlandoConnection stub, finish texts support
Silvris Mar 5, 2024
184129a
actually error check direction
Silvris Mar 5, 2024
6fee5a2
Update __init__.py
Silvris Mar 5, 2024
5571fc5
remove now unused imports
Silvris Mar 5, 2024
51cda33
fix some issues
beauxq Mar 5, 2024
19b9c03
Merge pull request #4 from beauxq/silvris-plando_options
Silvris Mar 6, 2024
7b69879
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Mar 6, 2024
1b88395
support KDL3
Silvris Mar 6, 2024
65bc9bf
Update Options.py
Silvris Mar 6, 2024
6a3b982
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Mar 15, 2024
e0bf453
support tunic plando connections
Silvris Mar 15, 2024
ed582ae
forgot the option
Silvris Mar 15, 2024
4cf6616
missed cases
Silvris Mar 15, 2024
c8a0e53
scope Direction under PlandoConnection
Silvris Mar 15, 2024
a76b008
invert first if, fix missed Direction reference
Silvris Mar 15, 2024
7859129
swap to options API for plando connections
alwaysintreble Mar 15, 2024
194de1b
fix a bunch of bugs
alwaysintreble Mar 15, 2024
8d3366f
only do plando when setting is enabled
alwaysintreble Mar 15, 2024
3ca1179
Merge pull request #5 from alwaysintreble/messenger_plando_api
Silvris Mar 16, 2024
3fb3c3e
fix bad unused import
Silvris Mar 16, 2024
8086f0b
Merge branch 'main' into plando_options
Silvris Mar 22, 2024
b840d15
Merge branch 'main' into plando_options
Silvris Mar 24, 2024
a0d3002
Apply suggestions from code review
Silvris Mar 24, 2024
b0f5dea
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Apr 3, 2024
9737715
fix LttP circular imports
Silvris Apr 4, 2024
aa894a0
allow passing lists into entrance and exit
Silvris Apr 4, 2024
4c5f409
fix log warnings when option is empty but extant
Silvris Apr 4, 2024
0ad6af1
Merge branch 'main' into plando_options
Silvris Apr 14, 2024
39284e1
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Apr 16, 2024
1e4a2f8
change plando text to use list of str for text
Silvris Apr 16, 2024
0591b2c
Update worlds/tunic/options.py
Silvris Apr 16, 2024
9ae1db0
Update worlds/tunic/options.py
Silvris Apr 16, 2024
8bbbd86
Merge branch 'main' into plando_options
Silvris Apr 16, 2024
a96c57f
wrap in sorted
Silvris Apr 21, 2024
d3fc611
Merge remote-tracking branch 'upstream/main' into plando_options
Silvris Apr 21, 2024
b27d71d
Merge branch 'main' into plando_options
Silvris Apr 30, 2024
3a1bcff
The Messenger: fix portal plando
alwaysintreble May 3, 2024
2ad1270
The Messenger: rename portal plando so that transition plando can be …
alwaysintreble May 3, 2024
ddc8b00
fix circular import
alwaysintreble May 3, 2024
f5c2dc4
won't let me inherit entrances and exits :(
alwaysintreble May 3, 2024
af84aef
Merge pull request #6 from alwaysintreble/fix_plando_portal
Silvris May 4, 2024
ea37f2a
Merge branch 'main' into plando_options
Silvris May 5, 2024
9a2593a
Update __init__.py
Silvris May 5, 2024
e6eac0c
allow duplicate exits, apply to TUNIC
Silvris May 11, 2024
3d2bde4
Update Options.py
Silvris May 13, 2024
22e4d59
Merge branch 'main' into plando_options
Silvris May 13, 2024
3b1dbd9
update typing on direction with todo
Silvris May 14, 2024
6af6ba2
Merge branch 'main' into plando_options
Silvris May 15, 2024
d3dfb96
fix incorrect can_connect
Silvris May 16, 2024
1edf701
Merge branch 'main' into plando_options
Silvris May 20, 2024
8fad93b
Merge branch 'main' into plando_options
Silvris May 29, 2024
2991ea2
adjust tunic shop handling
Silvris May 31, 2024
a18e668
Merge branch 'main' into plando_options
Silvris May 31, 2024
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
29 changes: 2 additions & 27 deletions Generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
from settings import get_settings
from Utils import parse_yamls, version_tuple, __version__, tuplize_version
from worlds.alttp.EntranceRandomizer import parse_arguments
from worlds.alttp.Text import TextTable
from worlds.AutoWorld import AutoWorldRegister
from worlds.generic import PlandoConnection
from worlds import failed_world_loads


Expand Down Expand Up @@ -481,35 +479,12 @@ def roll_settings(weights: dict, plando_options: PlandoOptions = PlandoOptions.b
if PlandoOptions.items in plando_options:
ret.plando_items = game_weights.get("plando_items", [])
if ret.game == "A Link to the Past":
roll_alttp_settings(ret, game_weights, plando_options)
if PlandoOptions.connections in plando_options:
ret.plando_connections = []
options = game_weights.get("plando_connections", [])
for placement in options:
if roll_percentage(get_choice("percentage", placement, 100)):
ret.plando_connections.append(PlandoConnection(
get_choice("entrance", placement),
get_choice("exit", placement),
get_choice("direction", placement, "both")
))
roll_alttp_settings(ret, game_weights)

return ret


def roll_alttp_settings(ret: argparse.Namespace, weights, plando_options):

ret.plando_texts = {}
if PlandoOptions.texts in plando_options:
tt = TextTable()
tt.removeUnwantedText()
options = weights.get("plando_texts", [])
for placement in options:
if roll_percentage(get_choice_legacy("percentage", placement, 100)):
at = str(get_choice_legacy("at", placement))
if at not in tt:
raise Exception(f"No text target \"{at}\" found.")
ret.plando_texts[at] = str(get_choice_legacy("text", placement))

def roll_alttp_settings(ret: argparse.Namespace, weights):
ret.sprite_pool = weights.get('sprite_pool', [])
ret.sprite = get_choice_legacy('sprite', weights, "Link")
if 'random_sprite_on_event' in weights:
Expand Down
226 changes: 225 additions & 1 deletion Options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dataclasses import dataclass

from schema import And, Optional, Or, Schema
from typing_extensions import Self

from Utils import get_fuzzy_results, is_iterable_except_str

Expand Down Expand Up @@ -932,6 +933,228 @@ class ItemSet(OptionSet):
convert_name_groups = True


class PlandoText(typing.NamedTuple):
at: str
text: typing.List[str]
percentage: int = 100


PlandoTextsFromAnyType = typing.Union[
typing.Iterable[typing.Union[typing.Mapping[str, typing.Any], PlandoText, typing.Any]], typing.Any
]


class PlandoTexts(Option[typing.List[PlandoText]], VerifyKeys):
default = ()
supports_weighting = False
display_name = "Plando Texts"

def __init__(self, value: typing.Iterable[PlandoText]) -> None:
self.value = list(deepcopy(value))
super().__init__()

def verify(self, world: typing.Type[World], player_name: str, plando_options: "PlandoOptions") -> None:
from BaseClasses import PlandoOptions
if self.value and not (PlandoOptions.texts & plando_options):
# plando is disabled but plando options were given so overwrite the options
self.value = []
logging.warning(f"The plando texts module is turned off, "
f"so text for {player_name} will be ignored.")

@classmethod
def from_any(cls, data: PlandoTextsFromAnyType) -> Self:
texts: typing.List[PlandoText] = []
if isinstance(data, typing.Iterable):
Silvris marked this conversation as resolved.
Show resolved Hide resolved
for text in data:
if isinstance(text, typing.Mapping):
if random.random() < float(text.get("percentage", 100)/100):
at = text.get("at", None)
if at is not None:
given_text = text.get("text", [])
if isinstance(given_text, str):
given_text = [given_text]
texts.append(PlandoText(
at,
given_text,
text.get("percentage", 100)
))
elif isinstance(text, PlandoText):
if random.random() < float(text.percentage/100):
Silvris marked this conversation as resolved.
Show resolved Hide resolved
texts.append(text)
else:
raise Exception(f"Cannot create plando text from non-dictionary type, got {type(text)}")
cls.verify_keys([text.at for text in texts])
return cls(texts)
else:
raise NotImplementedError(f"Cannot Convert from non-list, got {type(data)}")

@classmethod
def get_option_name(cls, value: typing.List[PlandoText]) -> str:
return str({text.at: " ".join(text.text) for text in value})

def __iter__(self) -> typing.Iterator[PlandoText]:
yield from self.value

def __getitem__(self, index: typing.SupportsIndex) -> PlandoText:
return self.value.__getitem__(index)

def __len__(self) -> int:
return self.value.__len__()


class ConnectionsMeta(AssembleOptions):
def __new__(mcs, name, bases, attrs):
Silvris marked this conversation as resolved.
Show resolved Hide resolved
if name != "PlandoConnections":
Silvris marked this conversation as resolved.
Show resolved Hide resolved
Silvris marked this conversation as resolved.
Show resolved Hide resolved
Silvris marked this conversation as resolved.
Show resolved Hide resolved
Silvris marked this conversation as resolved.
Show resolved Hide resolved
Silvris marked this conversation as resolved.
Show resolved Hide resolved
assert "entrances" in attrs, f"Please define valid entrances for {name}"
attrs["entrances"] = frozenset((connection.lower() for connection in attrs["entrances"]))
assert "exits" in attrs, f"Please define valid exits for {name}"
attrs["exits"] = frozenset((connection.lower() for connection in attrs["exits"]))
if "__doc__" not in attrs:
attrs["__doc__"] = PlandoConnections.__doc__
cls = super().__new__(mcs, name, bases, attrs)
return cls


class PlandoConnection(typing.NamedTuple):
class Direction:
entrance = "entrance"
exit = "exit"
both = "both"

entrance: str
exit: str
direction: Direction # entrance, exit or both
Silvris marked this conversation as resolved.
Show resolved Hide resolved
percentage: int = 100


PlandoConFromAnyType = typing.Union[
typing.Iterable[typing.Union[typing.Mapping[str, typing.Any], PlandoConnection, typing.Any]], typing.Any
]


class PlandoConnections(Option[typing.List[PlandoConnection]], metaclass=ConnectionsMeta):
"""Generic connections plando. Format is:
- entrance: "Entrance Name"
exit: "Exit Name"
direction: "Direction"
percentage: 100
Direction must be one of 'entrance', 'exit', or 'both', and defaults to 'both' if omitted.
Percentage is an integer from 1 to 100, and defaults to 100 when omitted."""

display_name = "Plando Connections"

default = ()
supports_weighting = False

entrances: typing.ClassVar[typing.AbstractSet[str]]
exits: typing.ClassVar[typing.AbstractSet[str]]

duplicate_exits: bool = False
"""Whether or not exits should be allowed to be duplicate."""

def __init__(self, value: typing.Iterable[PlandoConnection]):
self.value = list(deepcopy(value))
super(PlandoConnections, self).__init__()

@classmethod
def validate_entrance_name(cls, entrance: str) -> bool:
return entrance.lower() in cls.entrances

@classmethod
def validate_exit_name(cls, exit: str) -> bool:
return exit.lower() in cls.exits

@classmethod
def can_connect(cls, entrance: str, exit: str) -> bool:
"""Checks that a given entrance can connect to a given exit.
By default, this will always return true unless overridden."""
return True
Copy link
Collaborator

Choose a reason for hiding this comment

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

are we sure this should be the default behavior? it seems like enough of the worlds that use this would be fine with it (and my world is too) but it almost feels backwards.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It primarily does two things.

As you've said, it's sufficient for worlds that don't have wild constraints on possible connections. It also simplifies backward compatibility with the currently implemented games, as they already have to check for whether or not the plando is a valid connection, so we can skip the option-based check (the exception being Minecraft, as it's can_connect was simple enough to implement on the option itself).


@classmethod
def validate_plando_connections(cls, connections: typing.Iterable[PlandoConnection]) -> None:
used_entrances: typing.List[str] = []
used_exits: typing.List[str] = []
for connection in connections:
entrance = connection.entrance
exit = connection.exit
direction = connection.direction
if direction not in (PlandoConnection.Direction.entrance,
PlandoConnection.Direction.exit,
PlandoConnection.Direction.both):
raise ValueError(f"Unknown direction: {direction}")
if entrance in used_entrances:
raise ValueError(f"Duplicate Entrance {entrance} not allowed.")
if not cls.duplicate_exits and exit in used_exits:
raise ValueError(f"Duplicate Exit {exit} not allowed.")
used_entrances.append(entrance)
used_exits.append(exit)
if not cls.validate_entrance_name(entrance):
raise ValueError(f"{entrance.title()} is not a valid entrance.")
if not cls.validate_exit_name(exit):
raise ValueError(f"{exit.title()} is not a valid exit.")
if not cls.can_connect(entrance, exit):
raise ValueError(f"Connection between {entrance.title()} and {exit.title()} is invalid.")

@classmethod
def from_any(cls, data: PlandoConFromAnyType) -> Self:
if not isinstance(data, typing.Iterable):
raise Exception(f"Cannot create plando connections from non-List value, got {type(data)}.")

value: typing.List[PlandoConnection] = []
for connection in data:
if isinstance(connection, typing.Mapping):
percentage = connection.get("percentage", 100)
if random.random() < float(percentage / 100):
entrance = connection.get("entrance", None)
if is_iterable_except_str(entrance):
entrance = random.choice(sorted(entrance))
exit = connection.get("exit", None)
if is_iterable_except_str(exit):
exit = random.choice(sorted(exit))
direction = connection.get("direction", "both")

if not entrance or not exit:
raise Exception("Plando connection must have an entrance and an exit.")
value.append(PlandoConnection(
entrance,
exit,
direction,
percentage
))
elif isinstance(connection, PlandoConnection):
if random.random() < float(connection.percentage / 100):
value.append(connection)
else:
raise Exception(f"Cannot create connection from non-Dict type, got {type(connection)}.")
cls.validate_plando_connections(value)
return cls(value)

def verify(self, world: typing.Type[World], player_name: str, plando_options: "PlandoOptions") -> None:
from BaseClasses import PlandoOptions
if self.value and not (PlandoOptions.connections & plando_options):
# plando is disabled but plando options were given so overwrite the options
self.value = []
logging.warning(f"The plando connections module is turned off, "
f"so connections for {player_name} will be ignored.")

@classmethod
def get_option_name(cls, value: typing.List[PlandoConnection]) -> str:
return ", ".join(["%s %s %s" % (connection.entrance,
"<=>" if connection.direction == PlandoConnection.Direction.both else
"<=" if connection.direction == PlandoConnection.Direction.exit else
"=>",
connection.exit) for connection in value])

def __getitem__(self, index: typing.SupportsIndex) -> PlandoConnection:
return self.value.__getitem__(index)

def __iter__(self) -> typing.Iterator[PlandoConnection]:
yield from self.value

def __len__(self) -> int:
return len(self.value)


class Accessibility(Choice):
"""Set rules for reachability of your items/locations.
Locations: ensure everything can be reached and acquired.
Expand Down Expand Up @@ -1083,7 +1306,8 @@ class ItemLinks(OptionList):
])

@staticmethod
def verify_items(items: typing.List[str], item_link: str, pool_name: str, world, allow_item_groups: bool = True) -> typing.Set:
def verify_items(items: typing.List[str], item_link: str, pool_name: str, world,
allow_item_groups: bool = True) -> typing.Set:
pool = set()
for item_name in items:
if item_name not in world.item_names and (not allow_item_groups or item_name not in world.item_name_groups):
Expand Down
27 changes: 25 additions & 2 deletions worlds/alttp/Options.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import typing

from BaseClasses import MultiWorld
from Options import Choice, Range, Option, Toggle, DefaultOnToggle, DeathLink, StartInventoryPool, PlandoBosses,\
FreeText, Removed
from Options import Choice, Range, Option, Toggle, DefaultOnToggle, DeathLink, \
StartInventoryPool, PlandoBosses, PlandoConnections, PlandoTexts, FreeText, Removed
from .EntranceShuffle import default_connections, default_dungeon_connections, \
inverted_default_connections, inverted_default_dungeon_connections
from .Text import TextTable


class GlitchesRequired(Choice):
Expand Down Expand Up @@ -721,7 +724,27 @@ class AllowCollect(DefaultOnToggle):
display_name = "Allow Collection of checks for other players"


class ALttPPlandoConnections(PlandoConnections):
entrances = set([connection[0] for connection in (
*default_connections, *default_dungeon_connections, *inverted_default_connections,
*inverted_default_dungeon_connections)])
exits = set([connection[1] for connection in (
*default_connections, *default_dungeon_connections, *inverted_default_connections,
*inverted_default_dungeon_connections)])


class ALttPPlandoTexts(PlandoTexts):
"""Text plando. Format is:
- text: 'This is your text'
at: text_key
percentage: 100
Percentage is an integer from 1 to 100, and defaults to 100 when omitted."""
valid_keys = TextTable.valid_keys


alttp_options: typing.Dict[str, type(Option)] = {
"plando_connections": ALttPPlandoConnections,
"plando_texts": ALttPPlandoTexts,
"start_inventory_from_pool": StartInventoryPool,
"goal": Goal,
"mode": Mode,
Expand Down
4 changes: 2 additions & 2 deletions worlds/alttp/Rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2538,12 +2538,12 @@ def hint_text(dest, ped_hint=False):
tt['menu_start_2'] = "{MENU}\n{SPEED0}\n≥@'s house\n Dark Chapel\n{CHOICE3}"
tt['menu_start_3'] = "{MENU}\n{SPEED0}\n≥@'s house\n Dark Chapel\n Mountain Cave\n{CHOICE2}"

for at, text in world.plando_texts[player].items():
for at, text, _ in world.plando_texts[player]:

if at not in tt:
raise Exception(f"No text target \"{at}\" found.")
else:
tt[at] = text
tt[at] = "\n".join(text)

rom.write_bytes(0xE0000, tt.getBytes())

Expand Down
10 changes: 6 additions & 4 deletions worlds/alttp/Shops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

from BaseClasses import CollectionState
from .SubClasses import ALttPLocation
from .EntranceShuffle import door_addresses

from .Items import item_name_groups
from .Options import small_key_shuffle, RandomizeShopInventories

from .StateHelpers import has_hearts, can_use_bombs, can_hold_arrows

logger = logging.getLogger("Shops")
Expand Down Expand Up @@ -66,6 +66,7 @@ def item_count(self) -> int:
return 0

def get_bytes(self) -> List[int]:
from .EntranceShuffle import door_addresses
# [id][roomID-low][roomID-high][doorID][zero][shop_config][shopkeeper_config][sram_index]
entrances = self.region.entrances
config = self.item_count
Expand Down Expand Up @@ -181,7 +182,7 @@ def push_shop_inventories(multiworld):


def create_shops(multiworld, player: int):

from .Options import RandomizeShopInventories
player_shop_table = shop_table.copy()
if multiworld.include_witch_hut[player]:
player_shop_table["Potion Shop"] = player_shop_table["Potion Shop"]._replace(locked=False)
Expand Down Expand Up @@ -304,6 +305,7 @@ class ShopData(NamedTuple):


def set_up_shops(multiworld, player: int):
from .Options import small_key_shuffle
# TODO: move hard+ mode changes for shields here, utilizing the new shops

if multiworld.retro_bow[player]:
Expand Down Expand Up @@ -426,7 +428,7 @@ def get_price_modifier(item):

def get_price(multiworld, item, player: int, price_type=None):
"""Converts a raw Rupee price into a special price type"""

from .Options import small_key_shuffle
if price_type:
price_types = [price_type]
else:
Expand Down
Loading
Loading