Skip to content

Commit

Permalink
expand duplicate trial protection to the entire keep (including game …
Browse files Browse the repository at this point in the history
…medleys)
  • Loading branch information
nbrochu committed Jan 29, 2025
1 parent 636c75f commit 853da1e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
37 changes: 25 additions & 12 deletions worlds/keymasters_keep/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import abc

from random import Random
from typing import Any, Dict, List, Optional, Tuple, Type
from typing import Any, Dict, List, Optional, Set, Tuple, Type

from .enums import KeymastersKeepGamePlatforms

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(
random: Random = None,
include_time_consuming_objectives: bool = False,
include_difficult_objectives: bool = False,
archipelago_options: Any = None
archipelago_options: Any = None,
) -> None:
self.random = random or Random()
self.include_time_consuming_objectives = include_time_consuming_objectives
Expand Down Expand Up @@ -105,39 +105,52 @@ def generate_objectives(
count: int = 1,
include_difficult: bool = False,
include_time_consuming: bool = False,
) -> Tuple[List[str], List[str]]:
objectives_in_use: Set[str] = None,
) -> Tuple[List[str], List[str], Set[str]]:
objectives_in_use = objectives_in_use or set()

optional_constraints: List[str] = list()
optional_constraint_templates: List[GameObjectiveTemplate] = self.optional_game_constraint_templates()

if len(optional_constraint_templates):
template: GameObjectiveTemplate = self.random.choice(self.optional_game_constraint_templates())
optional_constraints.append(template.generate_game_objective(self.random))

filtered_objectives: List[GameObjectiveTemplate] = self.filter_game_objective_templates(
filtered_templates: List[GameObjectiveTemplate] = self.filter_game_objective_templates(
include_difficult=include_difficult,
include_time_consuming=include_time_consuming,
)

weights: List[int] = [template.weight for template in filtered_objectives]
weights: List[int] = [template.weight for template in filtered_templates]
objectives: List[str] = list()

selected_objectives: List[GameObjectiveTemplate] = self.random.choices(
filtered_objectives, weights=weights, k=count
)
passes_templates: int = 0

objectives: List[str] = list()
while len(objectives) < count:
passes_templates += 1

template: GameObjectiveTemplate = self.random.choices(filtered_templates, weights=weights, k=1)[0]

for template in selected_objectives:
passes: int = 0

while True:
passes += 1
objective: str = template.generate_game_objective(self.random)

if objective not in objectives or passes > 10:
if objective not in objectives_in_use:
objectives.append(objective)
objectives_in_use.add(objective)

break

if passes_templates > 50:
objectives.append(objective)
break

if passes > 10:
break

return optional_constraints, objectives
return optional_constraints, objectives, objectives_in_use

@classmethod
def game_name_with_platforms(cls) -> str:
Expand Down
14 changes: 9 additions & 5 deletions worlds/keymasters_keep/game_objective_generator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from random import Random
from typing import Any, List, Tuple, Type, Union
from typing import Any, List, Set, Tuple, Type, Union

from .enums import KeymastersKeepGamePlatforms

Expand Down Expand Up @@ -97,6 +97,7 @@ def generate_from_plan(
game_selection[i] = GameMedleyGame

data: GameObjectiveGeneratorData = list()
objectives_in_use: Set[str] = set()

i: int
count: int
Expand All @@ -114,10 +115,12 @@ def generate_from_plan(

optional_constraints: List[str]
objectives: List[str]
optional_constraints, objectives = game.generate_objectives(

optional_constraints, objectives, objectives_in_use = game.generate_objectives(
count=count,
include_difficult=include_difficult,
include_time_consuming=include_time_consuming,
objectives_in_use=objectives_in_use,
)

data.append((game, optional_constraints, objectives))
Expand All @@ -135,7 +138,7 @@ def generate_from_plan(
random=random,
include_time_consuming_objectives=include_time_consuming,
include_difficult_objectives=include_difficult,
archipelago_options=self.archipelago_options
archipelago_options=self.archipelago_options,
)

# This appears to completely ignore the passed 'include_difficult' value, but in reality, a game that
Expand All @@ -150,10 +153,11 @@ def generate_from_plan(

optional_constraints: List[str]
objectives: List[str]
optional_constraints, objectives = game.generate_objectives(
optional_constraints, objectives, objectives_in_use = game.generate_objectives(
count=count,
include_difficult=include_difficult,
include_time_consuming=include_time_consuming,
objectives_in_use=objectives_in_use,
)

data.append((game, optional_constraints, objectives))
Expand All @@ -166,7 +170,7 @@ def _filter_games(
include_adult_only_or_unrated_games: bool,
include_modern_console_games: bool,
include_difficult_objectives: bool,
include_time_consuming_objectives: bool
include_time_consuming_objectives: bool,
) -> List[Type[Game]]:
filtered_games: List[Type[Game]] = list()

Expand Down
55 changes: 41 additions & 14 deletions worlds/keymasters_keep/games/game_medley_game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, List, Tuple, Type
from typing import Any, List, Set, Tuple, Type

from dataclasses import dataclass
from random import Random
Expand Down Expand Up @@ -36,13 +36,13 @@ def __init__(
include_time_consuming_objectives: bool = False,
include_difficult_objectives: bool = False,
archipelago_options: Any = None,
game_selection: List[Type[Game]] = None
game_selection: List[Type[Game]] = None,
) -> None:
super().__init__(
random=random,
include_time_consuming_objectives=include_time_consuming_objectives,
include_difficult_objectives=include_difficult_objectives,
archipelago_options=archipelago_options
archipelago_options=archipelago_options,
)

self.game_selection = game_selection
Expand All @@ -58,32 +58,59 @@ def generate_objectives(
count: int = 1,
include_difficult: bool = False,
include_time_consuming: bool = False,
) -> Tuple[List[str], List[str]]:
objectives_in_use: Set[str] = None,
) -> Tuple[List[str], List[str], Set[str]]:
objectives_in_use = objectives_in_use or set()

optional_constraints: List[str] = list()
objectives: List[str] = list()

for _ in range(count):
passes_templates: int = 0

while len(objectives) < count:
passes_templates += 1

game: Type[Game] = self.random.choice(self.game_selection)
game_instance: Game = game(random=self.random, archipelago_options=self.archipelago_options)

templates: List[GameObjectiveTemplate] = game_instance.filter_game_objective_templates(
filtered_templates: List[GameObjectiveTemplate] = game_instance.filter_game_objective_templates(
include_difficult=include_difficult,
include_time_consuming=include_time_consuming,
)

if not len(templates):
templates = game_instance.game_objective_templates()
if not len(filtered_templates):
filtered_templates = game_instance.game_objective_templates()

weights: List[int] = [template.weight for template in filtered_templates]

template: GameObjectiveTemplate = self.random.choices(filtered_templates, weights=weights, k=1)[0]

passes: int = 0

while True:
passes += 1

objective: str = template.generate_game_objective(self.random)

if objective not in objectives_in_use:
objective = f"{game.name} -> {objective}"

objectives.append(objective)
objectives_in_use.add(objective)

break

weights: List[int] = [template.weight for template in templates]
if passes_templates > 50:
objective = f"{game.name} -> {objective}"
objectives.append(objective)

objective_template: GameObjectiveTemplate = self.random.choices(templates, weights=weights)[0]
break

objective: str = objective_template.generate_game_objective(self.random)
objective = f"{game.name} -> {objective}"
if passes > 10:
break

objectives.append(objective)
return optional_constraints, objectives, objectives_in_use

return optional_constraints, objectives

# Archipelago Options
# ...

0 comments on commit 853da1e

Please sign in to comment.