Buckets:

download
raw
25.3 kB
from __future__ import annotations
import copy
import math
import pprint
import typing
from itertools import groupby
from typing import TYPE_CHECKING
from randovania.game_description.assignment import PickupTarget
from randovania.game_description.pickup.pickup_entry import StartingPickupBehavior
from randovania.generator import reach_lib
from randovania.generator.filler import filler_logging
from randovania.generator.filler.filler_library import UnableToGenerate, UncollectedState
from randovania.generator.filler.filler_logging import debug_print_collect_event
from randovania.generator.filler.weighted_locations import WeightedLocations
from randovania.lib import random_lib
from randovania.resolver import debug
if TYPE_CHECKING:
from collections.abc import Callable, Mapping
from collections.abc import Set as AbstractSet
from random import Random
from randovania.game_description.game_patches import GamePatches
from randovania.game_description.pickup.pickup_entry import PickupEntry
from randovania.game_description.resources.pickup_index import PickupIndex
from randovania.generator.filler.action import Action
from randovania.generator.filler.player_state import PlayerState
from randovania.generator.filler.weights import ActionWeights
from randovania.generator.generator_reach import GeneratorReach
def _index_age_weight(age: float) -> float:
return min(10.0, age + 1.0) ** -2
def _calculate_uncollected_index_weights(
uncollected_indices: AbstractSet[PickupIndex],
assigned_indices: AbstractSet[PickupIndex],
index_age: Mapping[PickupIndex, float],
indices_groups: list[set[PickupIndex]],
) -> dict[PickupIndex, float]:
"""
Calculates a weight
:param uncollected_indices
:param assigned_indices
:param index_age: the higher the age for an index, the smaller it's weight
:param indices_groups: indices separated in distinct groups.
In practice, one group for each region respecting preset exclusions
"""
result = {}
for indices in indices_groups:
weight_from_collected_indices = math.sqrt(len(indices) / ((1 + len(assigned_indices & indices)) ** 2))
for index in sorted(uncollected_indices & indices):
result[index] = weight_from_collected_indices * _index_age_weight(index_age[index])
return result
def _pickup_unlocks_nothing(
player_state: PlayerState, pickup: PickupEntry, evaluated_reach: GeneratorReach | None
) -> bool:
"""
Whether collecting the pickup right now would leave the player's set of reachable nodes unchanged.
:param evaluated_reach: The reach from collecting the pickup, when action weighting already calculated it.
"""
reachable = player_state.reach.set_of_reachable_node_indices()
if evaluated_reach is not None:
return not (evaluated_reach.set_of_reachable_node_indices() - reachable)
reach = copy.deepcopy(player_state.reach)
reach.advance_to(reach.state.assign_pickup_resources(pickup), is_safe=True)
if reach.set_of_reachable_node_indices() - reachable:
return False
return not (reach_lib.advance_after_action(reach).set_of_reachable_node_indices() - reachable)
def _ignore_index_age_for_inert_pickup(
locations: WeightedLocations,
player_state: PlayerState,
pickup: PickupEntry,
evaluated_reach: GeneratorReach | None,
) -> WeightedLocations:
"""
Cancels the index age term for pickups that unlock nothing, so they spread out instead of
piling onto the lone freshest location. Other pickups pass through unchanged.
"""
if not _pickup_unlocks_nothing(player_state, pickup, evaluated_reach):
return locations
return locations.rescale(lambda player, index: 1 / _index_age_weight(player.pickup_index_ages[index]))
def _get_next_player(
rng: Random, player_states: list[PlayerState], locations_weighted: WeightedLocations
) -> PlayerState | None:
"""
Gets the next player a pickup should be placed for.
:param rng:
:param player_states:
:param locations_weighted: Which locations are available and their weight.
:return:
"""
all_uncollected: dict[PlayerState, set[PickupIndex]] = {
player_state: UncollectedState.pickup_indices_from_reach(player_state.reach) for player_state in player_states
}
max_actions = max(player_state.num_actions for player_state in player_states)
max_uncollected = max(len(uncollected) for uncollected in all_uncollected.values())
def _calculate_weight(player: PlayerState) -> float:
return 1 + (max_actions - player.num_actions) * (max_uncollected - len(all_uncollected[player]))
weighted_players = {
player_state: _calculate_weight(player_state)
for player_state in player_states
if not player_state.victory_condition_satisfied() and player_state.potential_actions(locations_weighted)
}
if weighted_players:
if debug.debug_level() > debug.LogLevel.NORMAL:
print(f">>>>> Player Weights: {weighted_players}")
return random_lib.select_element_with_weight(rng, weighted_players)
else:
if all(player_state.victory_condition_satisfied() for player_state in player_states):
debug.debug_print("Finished because we can win")
return None
else:
total_actions = sum(player_state.num_actions for player_state in player_states)
unfinished_players = ", ".join(
[str(player_state) for player_state in player_states if not player_state.victory_condition_satisfied()]
)
raise UnableToGenerate(
f"{unfinished_players} with no possible actions after {total_actions} total actions."
)
class EvaluatedAction(typing.NamedTuple):
action: Action
reach: GeneratorReach
multiplier: float
offset: float
def apply_weight_modifiers_to(self, base: float) -> float:
"""Applies the multiplier and offset to the given weight."""
return base * self.multiplier + self.offset
def replace_reach(self, new_reach: GeneratorReach) -> EvaluatedAction:
return EvaluatedAction(self.action, new_reach, self.multiplier, self.offset)
def _evaluate_action(player_state: PlayerState, action_weights: ActionWeights, action: Action) -> EvaluatedAction:
"""
Calculates the weight offsets and multipliers for the given action, as well as the reach
you'd get by collecting all resources and pickups of the given action.
:param base_reach:
:param action:
:return:
"""
multiplier = 1.0
offset = 0.0
base_reach = player_state.reach
potential_reach = copy.deepcopy(base_reach)
resources, pickups = action.split_pickups()
if resources:
for resource in resources:
potential_reach.act_on(resource)
multiplier *= action_weights.DANGEROUS_ACTION_MULTIPLIER
pickups_that_solve, uncollected_resource_nodes, _ = player_state.calculate_potential_actions(potential_reach)
# If our potential reach does not allow us to progress in any way, don't try to consider it.
if (
not pickups_that_solve
and not uncollected_resource_nodes
and not potential_reach.victory_condition_satisfied()
):
multiplier = 0.0
if pickups:
potential_reach.advance_to(potential_reach.state.assign_pickups_resources(pickups), is_safe=True)
multiplier *= sum(pickup.generator_params.probability_multiplier for pickup in pickups) / len(pickups)
offset += sum(pickup.generator_params.probability_offset for pickup in pickups) / len(pickups)
return EvaluatedAction(
action,
reach_lib.advance_after_action(potential_reach),
multiplier,
offset,
)
def weighted_potential_actions(
player_state: PlayerState, status_update: Callable[[str], None], locations_weighted: WeightedLocations
) -> tuple[dict[Action, float], dict[Action, EvaluatedAction]]:
"""
Weights all potential actions based on current criteria.
:param player_state:
:param status_update:
:param locations_weighted: Which locations are available and their weight.
:return: The weight of each action, plus the evaluation each weight was derived from.
"""
evaluated_actions: dict[Action, EvaluatedAction] = {}
actions = player_state.potential_actions(locations_weighted)
if len(actions) == 1:
debug.debug_print(f"{actions[0]}")
debug.debug_print("Only one action, weighting skipped")
return dict.fromkeys(actions, 1.0), evaluated_actions
current_uncollected = UncollectedState.from_reach(player_state.reach)
current_unsafe_uncollected = UncollectedState.from_reach_only_unsafe(player_state.reach)
action_weights = player_state.game_enum.generator.action_weights
options_considered = 0
def update_for_option() -> None:
nonlocal options_considered
options_considered += 1
status_update(f"Checked {options_considered} of {len(actions)} options.")
options_considered = 0
for action in actions:
evaluated_actions[action] = _evaluate_action(player_state, action_weights, action)
update_for_option()
actions_weights = {
action: _calculate_weights_for(evaluation, current_uncollected, current_unsafe_uncollected)
for action, evaluation in evaluated_actions.items()
}
if sum(actions_weights.values()) == 0:
debug.debug_print("Using backup weights")
current_reachable_nodes = player_state.reach.set_of_reachable_node_indices()
actions_weights = {
action: action_weights.ADDITIONAL_NODES_WEIGHT_MULTIPLIER
* len(evaluation.reach.set_of_reachable_node_indices() - current_reachable_nodes)
for action, evaluation in evaluated_actions.items()
}
# Apply offset only at the end in order to preserve when all actions are weight 0
final_weights = {
action: evaluated_actions[action].apply_weight_modifiers_to(base_weight)
for action, base_weight in actions_weights.items()
}
if debug.debug_level() > debug.LogLevel.NORMAL:
for action, weight in final_weights.items():
print(f"{action.name} - {weight}")
return final_weights, evaluated_actions
def increment_index_age(locations_weighted: WeightedLocations, increment: float) -> None:
"""
Increments the pickup index's age for every already collected index.
"""
for player, location, _ in locations_weighted.all_items():
was_present = location in player.pickup_index_ages
player.pickup_index_ages[location] += increment
if not was_present:
# if it wasn't present, then we get to log!
filler_logging.print_new_pickup_index(player, location)
def _print_header(player_states: list[PlayerState]) -> None:
def _name_for_index(state: PlayerState, index: PickupIndex) -> str:
return state.get_full_name_for_pickup_node_at(index)
debug.debug_print(
"{}\nRetcon filler started with standard pickups:\n{}".format(
"*" * 100,
"\n".join(
"{}: {}".format(
player_state.name,
pprint.pformat(
{
item.name: player_state.pickups_left.count(item)
for item in sorted(set(player_state.pickups_left), key=lambda item: item.name)
}
),
)
for player_state in player_states
),
)
)
debug.debug_print(
"Static assignments:\n{}".format(
"\n".join(
"{}: {}".format(
player_state.name,
pprint.pformat(
{
_name_for_index(player_state, index): target.pickup.name
for index, target in player_state.reach.state.patches.pickup_assignment.items()
}
),
)
for player_state in player_states
)
)
)
debug.debug_print(
"Game specific:\n{}".format(
"\n".join(
f"{player_state.name}: {pprint.pformat(player_state.reach.state.patches.game_specific)}"
for player_state in player_states
)
)
)
def retcon_playthrough_filler(
rng: Random,
player_states: list[PlayerState],
status_update: Callable[[str], None],
) -> tuple[list[GamePatches], tuple[str, ...]]:
"""
Runs the retcon logic.
:param rng:
:param player_states:
:param status_update:
:return: A GamePatches for each player and a sequence of placed items.
"""
_print_header(player_states)
last_message = "Starting."
def action_report(message: str) -> None:
status_update(f"{last_message} {message}")
for player_state in player_states:
player_state.update_for_new_state()
actions_log = []
while True:
all_locations_weighted = _calculate_all_pickup_indices_weight(player_states)
if debug.debug_level() > debug.LogLevel.NORMAL:
player_health = {
player_state: player_state.reach.state.game_state_debug_string() for player_state in player_states
}
print(f">>>> Player Health: {player_health}")
current_player = _get_next_player(rng, player_states, all_locations_weighted)
if current_player is None:
break
weighted_actions, evaluated_actions = weighted_potential_actions(
current_player, action_report, all_locations_weighted
)
action = random_lib.select_element_with_weight_and_uniform_fallback(rng, weighted_actions)
evaluation = evaluated_actions.get(action) if action.num_pickups == 1 else None
new_resources, new_pickups = action.split_pickups()
new_pickups.sort()
for new_resource in new_resources:
debug_print_collect_event(new_resource)
# This action is potentially dangerous. Use `act_on` to remove invalid paths
current_player.reach.act_on(new_resource)
if new_pickups:
# Group all the like pickups based on if they can be placed in bulk
nested = [
list(group)
for _, group in groupby(new_pickups, key=lambda pickup: pickup.generator_params.bulk_placement)
]
# Shuffle them so we dont always pick the same progression first
rng.shuffle(nested)
# Check if we can place in bulk, else only use one element
if nested[0][0].generator_params.bulk_placement:
new_pickups = nested[0]
else:
new_pickups = [nested[0][0]]
debug.debug_print(f"\n>>> Will place {len(new_pickups)} pickups")
for i, new_pickup in enumerate(new_pickups):
if i > 0:
current_player.reach = reach_lib.advance_after_action(current_player.reach)
current_player.hint_state.advance_hint_seen_count(current_player.reach.state)
all_locations_weighted = _calculate_all_pickup_indices_weight(player_states)
log_entry = _assign_pickup_somewhere(
new_pickup,
current_player,
player_states,
rng,
all_locations_weighted,
evaluation.reach if evaluation is not None else None,
)
actions_log.append(log_entry)
debug.debug_print(f"* {log_entry}")
# TODO: this item is potentially dangerous and we should remove the invalidated paths
current_player.pickups_left.remove(new_pickup)
current_player.num_actions += 1
last_message = f"{sum(player.num_actions for player in player_states)} actions performed."
status_update(last_message)
current_player.reach = reach_lib.advance_after_action(current_player.reach)
current_player.update_for_new_state()
return [player_state.reach.state.patches for player_state in player_states], tuple(actions_log)
def debug_print_weighted_locations(all_locations_weighted: WeightedLocations, player_states: list[PlayerState]) -> None:
print("==> Weighted Locations")
for owner, index, weight in all_locations_weighted.all_items():
node_name = owner.get_full_name_for_pickup_node_at(index)
print(f"[{player_states[owner.index].name}] {node_name} - {weight}")
def should_be_starting_pickup(player: PlayerState, locations: WeightedLocations, pickup_entry: PickupEntry) -> bool:
if pickup_entry.start_case == StartingPickupBehavior.CAN_NEVER_BE_STARTING:
return False
cur_starting_pickups = player.num_starting_pickups_placed
minimum_starting_pickups = player.configuration.minimum_random_starting_pickups
maximum_starting_pickups = player.configuration.maximum_random_starting_pickups
result = cur_starting_pickups < minimum_starting_pickups or locations.is_empty()
# Prefer a starting pickup over off-db locations.
# This simulates how in solo games, you get more starting pickups if you start in weird places
if player.count_self_locations(locations) == 0 and cur_starting_pickups < maximum_starting_pickups:
result = True
return result
def _assign_pickup_somewhere(
action: PickupEntry,
current_player: PlayerState,
player_states: list[PlayerState],
rng: Random,
all_locations: WeightedLocations,
evaluated_reach: GeneratorReach | None,
) -> str:
"""
Assigns a PickupEntry to a free, collected PickupIndex or as a starting item.
:param action:
:param current_player:
:param player_states:
:param rng:
:param all_locations:
:param evaluated_reach: The reach obtained by collecting `action`, if it was already calculated.
:return:
"""
assert action in current_player.pickups_left
usable_locations = current_player.filter_usable_locations(all_locations, action)
if not should_be_starting_pickup(current_player, usable_locations, action):
if debug.debug_level() > debug.LogLevel.HIGH:
debug_print_weighted_locations(all_locations, player_states)
usable_locations = _ignore_index_age_for_inert_pickup(usable_locations, current_player, action, evaluated_reach)
index_owner_state, pickup_index = usable_locations.select_location(rng)
index_owner_state.assign_pickup(
pickup_index,
PickupTarget(action, current_player.index),
UncollectedState.pickup_indices_from_reach(index_owner_state.reach),
all_locations,
)
increment_index_age(all_locations, action.generator_params.index_age_impact)
all_locations.remove(index_owner_state, pickup_index)
if pickup_index in index_owner_state.reach.state.collected_pickup_indices(index_owner_state.reach.graph):
current_player.reach.advance_to(current_player.reach.state.assign_pickup_resources(action))
else:
# FIXME: isn't that condition always true?
debug.debug_print(f"ERROR! Assigned {action.name} to {pickup_index}, but location wasn't collected!")
spoiler_entry = pickup_placement_spoiler_entry(
current_player,
action,
pickup_index,
index_owner_state,
len(player_states) > 1,
)
else:
current_player.num_starting_pickups_placed += 1
if current_player.num_starting_pickups_placed > current_player.configuration.maximum_random_starting_pickups:
raise UnableToGenerate("Attempting to place more extra starting items than the number allowed.")
spoiler_entry = f"{action.name} as starting item"
if len(player_states) > 1:
spoiler_entry += f" for {current_player.name}"
current_player.reach.advance_to(current_player.reach.state.assign_pickup_to_starting_items(action))
return spoiler_entry
def _calculate_all_pickup_indices_weight(player_states: list[PlayerState]) -> WeightedLocations:
result = []
total_assigned_pickups = sum(player_state.num_assigned_pickups for player_state in player_states)
# print("================ WEIGHTS! ==================")
for player_state in player_states:
delta = total_assigned_pickups - player_state.num_assigned_pickups
player_weight = 1 + delta
# print(f"** {player_state.name} -- {player_weight}")
pickup_index_weights = _calculate_uncollected_index_weights(
player_state.all_indices & UncollectedState.pickup_indices_from_reach(player_state.reach),
set(player_state.reach.state.patches.pickup_assignment),
player_state.pickup_index_ages,
player_state.indices_groups,
)
all_weights = {}
for pickup_index, weight in pickup_index_weights.items():
all_weights[pickup_index] = weight * player_weight
result.append((player_state, all_weights))
# for (player_state, pickup_index), weight in all_weights.items():
# wl = player_state.game.region_list
# print(f"> {player_state.name} - {wl.node_name(wl.node_from_pickup_index(pickup_index))}: {weight}")
# print("============================================")
return WeightedLocations(result)
def _calculate_weights_for(
evaluation: EvaluatedAction,
current_uncollected: UncollectedState,
current_unsafe_uncollected: UncollectedState,
) -> float:
"""
Calculate a weight to be used for this action, based on what's collected in the reach.
"""
potential_reach = evaluation.reach
action_weights = potential_reach.graph.game_enum.generator.action_weights
if potential_reach.victory_condition_satisfied():
return action_weights.VICTORY_WEIGHT
potential_uncollected = UncollectedState.from_reach(potential_reach)
potential_uncollected_diff = potential_uncollected - current_uncollected
potential_unsafe_uncollected = UncollectedState.from_reach_only_unsafe(potential_reach) - current_unsafe_uncollected
if debug.debug_level() > debug.LogLevel.HIGH:
def print_weight_factors(uncollected: UncollectedState) -> None:
print(f" indices: {uncollected.pickup_indices}")
print(f" events: {[event.long_name for event in uncollected.events]}")
print(f" hints: {[hint.as_string for hint in uncollected.hints]}")
print(f">>> {evaluation.action}")
print(" safe resources:")
print_weight_factors(potential_uncollected_diff)
print(" unsafe resources:")
print_weight_factors(potential_unsafe_uncollected)
# this used to weigh actions according to *how many* resources were unlocked, but we've determined
# that the results are more fun if we only care about something being unlocked at all
pickups_weight = potential_uncollected_diff.pickups_weight(action_weights)
events_weight = potential_uncollected_diff.events_weight(action_weights)
hints_weight = potential_uncollected_diff.hints_weight(action_weights)
# if there were no safe resources of that type, check again for unsafe resources but weigh them as dangerous
if not pickups_weight:
pickups_weight = potential_unsafe_uncollected.pickups_weight(action_weights)
pickups_weight *= action_weights.DANGEROUS_ACTION_MULTIPLIER
if not events_weight:
events_weight = potential_unsafe_uncollected.events_weight(action_weights)
events_weight *= action_weights.DANGEROUS_ACTION_MULTIPLIER
if not hints_weight:
hints_weight = potential_unsafe_uncollected.hints_weight(action_weights)
hints_weight *= action_weights.DANGEROUS_ACTION_MULTIPLIER
# we're only concerned about *something* being unlocked by this action
# so we just take the maximum instead of summing them together
total_weight = max(pickups_weight, events_weight)
# hints are actually an added bonus, so they get *added* to the total weight
total_weight += hints_weight
# If this action fills all remaining Pickup Locations, then we prefer actions that directly unlock more locations
old_spots_for_pickups = len(current_uncollected.pickup_indices)
new_spots_for_pickups = len(potential_uncollected.pickup_indices)
action_consumes_all_spots = old_spots_for_pickups > 0 and old_spots_for_pickups <= evaluation.action.num_pickups
if action_consumes_all_spots and new_spots_for_pickups <= evaluation.action.num_pickups:
total_weight = 0.0
return total_weight
def pickup_placement_spoiler_entry(
location_owner: PlayerState,
action: PickupEntry,
pickup_index: PickupIndex,
index_owner: PlayerState,
add_indices: bool,
) -> str:
return "{}{} at {}{}".format(
f"{location_owner.name}'s " if add_indices else "",
action.name,
f"{index_owner.name}'s " if add_indices else "",
index_owner.get_full_name_for_pickup_node_at(pickup_index),
)

Xet Storage Details

Size:
25.3 kB
·
Xet hash:
33668143d804159474d376b3b3757472fa212271aeee8fb4d1e2b5409d2d720a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.