Pokemon Emerald: Make use of `NamedTuple._replace` (#3727)

This commit is contained in:
Bryce Wilson 2024-09-08 09:48:48 -07:00 committed by GitHub
parent cabfef669a
commit 05b257adf9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 26 deletions

View File

@ -276,15 +276,13 @@ def _str_to_pokemon_data_type(string: str) -> TrainerPokemonDataTypeEnum:
return TrainerPokemonDataTypeEnum.ITEM_CUSTOM_MOVES return TrainerPokemonDataTypeEnum.ITEM_CUSTOM_MOVES
@dataclass class TrainerPokemonData(NamedTuple):
class TrainerPokemonData:
species_id: int species_id: int
level: int level: int
moves: Optional[Tuple[int, int, int, int]] moves: Optional[Tuple[int, int, int, int]]
@dataclass class TrainerPartyData(NamedTuple):
class TrainerPartyData:
pokemon: List[TrainerPokemonData] pokemon: List[TrainerPokemonData]
pokemon_data_type: TrainerPokemonDataTypeEnum pokemon_data_type: TrainerPokemonDataTypeEnum
address: int address: int

View File

@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Dict, List, Set from typing import TYPE_CHECKING, Dict, List, Set
from .data import NUM_REAL_SPECIES, UNEVOLVED_POKEMON, TrainerPokemonData, data from .data import NUM_REAL_SPECIES, UNEVOLVED_POKEMON, data
from .options import RandomizeTrainerParties from .options import RandomizeTrainerParties
from .pokemon import filter_species_by_nearby_bst from .pokemon import filter_species_by_nearby_bst
from .util import int_to_bool_array from .util import int_to_bool_array
@ -111,6 +111,6 @@ def randomize_opponent_parties(world: "PokemonEmeraldWorld") -> None:
hm_moves[3] if world.random.random() < 0.25 else level_up_moves[3] hm_moves[3] if world.random.random() < 0.25 else level_up_moves[3]
) )
new_party.append(TrainerPokemonData(new_species.species_id, pokemon.level, new_moves)) new_party.append(pokemon._replace(species_id=new_species.species_id, moves=new_moves))
trainer.party.pokemon = new_party trainer.party = trainer.party._replace(pokemon=new_party)

View File

@ -4,8 +4,7 @@ Functions related to pokemon species and moves
import functools import functools
from typing import TYPE_CHECKING, Dict, List, Set, Optional, Tuple from typing import TYPE_CHECKING, Dict, List, Set, Optional, Tuple
from .data import (NUM_REAL_SPECIES, OUT_OF_LOGIC_MAPS, EncounterTableData, LearnsetMove, MiscPokemonData, from .data import (NUM_REAL_SPECIES, OUT_OF_LOGIC_MAPS, EncounterTableData, LearnsetMove, SpeciesData, data)
SpeciesData, data)
from .options import (Goal, HmCompatibility, LevelUpMoves, RandomizeAbilities, RandomizeLegendaryEncounters, from .options import (Goal, HmCompatibility, LevelUpMoves, RandomizeAbilities, RandomizeLegendaryEncounters,
RandomizeMiscPokemon, RandomizeStarters, RandomizeTypes, RandomizeWildPokemon, RandomizeMiscPokemon, RandomizeStarters, RandomizeTypes, RandomizeWildPokemon,
TmTutorCompatibility) TmTutorCompatibility)
@ -461,7 +460,7 @@ def randomize_learnsets(world: "PokemonEmeraldWorld") -> None:
type_bias, normal_bias, species.types) type_bias, normal_bias, species.types)
else: else:
new_move = 0 new_move = 0
new_learnset.append(LearnsetMove(old_learnset[cursor].level, new_move)) new_learnset.append(old_learnset[cursor]._replace(move_id=new_move))
cursor += 1 cursor += 1
# All moves from here onward are actual moves. # All moves from here onward are actual moves.
@ -473,7 +472,7 @@ def randomize_learnsets(world: "PokemonEmeraldWorld") -> None:
new_move = get_random_move(world.random, new_move = get_random_move(world.random,
{move.move_id for move in new_learnset} | world.blacklisted_moves, {move.move_id for move in new_learnset} | world.blacklisted_moves,
type_bias, normal_bias, species.types) type_bias, normal_bias, species.types)
new_learnset.append(LearnsetMove(old_learnset[cursor].level, new_move)) new_learnset.append(old_learnset[cursor]._replace(move_id=new_move))
cursor += 1 cursor += 1
species.learnset = new_learnset species.learnset = new_learnset
@ -581,8 +580,10 @@ def randomize_starters(world: "PokemonEmeraldWorld") -> None:
picked_evolution = world.random.choice(potential_evolutions) picked_evolution = world.random.choice(potential_evolutions)
for trainer_name, starter_position, is_evolved in rival_teams[i]: for trainer_name, starter_position, is_evolved in rival_teams[i]:
new_species_id = picked_evolution if is_evolved else starter.species_id
trainer_data = world.modified_trainers[data.constants[trainer_name]] trainer_data = world.modified_trainers[data.constants[trainer_name]]
trainer_data.party.pokemon[starter_position].species_id = picked_evolution if is_evolved else starter.species_id trainer_data.party.pokemon[starter_position] = \
trainer_data.party.pokemon[starter_position]._replace(species_id=new_species_id)
def randomize_legendary_encounters(world: "PokemonEmeraldWorld") -> None: def randomize_legendary_encounters(world: "PokemonEmeraldWorld") -> None:
@ -594,10 +595,7 @@ def randomize_legendary_encounters(world: "PokemonEmeraldWorld") -> None:
world.random.shuffle(shuffled_species) world.random.shuffle(shuffled_species)
for i, encounter in enumerate(data.legendary_encounters): for i, encounter in enumerate(data.legendary_encounters):
world.modified_legendary_encounters.append(MiscPokemonData( world.modified_legendary_encounters.append(encounter._replace(species_id=shuffled_species[i]))
shuffled_species[i],
encounter.address
))
else: else:
should_match_bst = world.options.legendary_encounters in { should_match_bst = world.options.legendary_encounters in {
RandomizeLegendaryEncounters.option_match_base_stats, RandomizeLegendaryEncounters.option_match_base_stats,
@ -621,9 +619,8 @@ def randomize_legendary_encounters(world: "PokemonEmeraldWorld") -> None:
if should_match_bst: if should_match_bst:
candidates = filter_species_by_nearby_bst(candidates, sum(original_species.base_stats)) candidates = filter_species_by_nearby_bst(candidates, sum(original_species.base_stats))
world.modified_legendary_encounters.append(MiscPokemonData( world.modified_legendary_encounters.append(encounter._replace(
world.random.choice(candidates).species_id, species_id=world.random.choice(candidates).species_id
encounter.address
)) ))
@ -637,10 +634,7 @@ def randomize_misc_pokemon(world: "PokemonEmeraldWorld") -> None:
world.modified_misc_pokemon = [] world.modified_misc_pokemon = []
for i, encounter in enumerate(data.misc_pokemon): for i, encounter in enumerate(data.misc_pokemon):
world.modified_misc_pokemon.append(MiscPokemonData( world.modified_misc_pokemon.append(encounter._replace(species_id=shuffled_species[i]))
shuffled_species[i],
encounter.address
))
else: else:
should_match_bst = world.options.misc_pokemon in { should_match_bst = world.options.misc_pokemon in {
RandomizeMiscPokemon.option_match_base_stats, RandomizeMiscPokemon.option_match_base_stats,
@ -672,9 +666,8 @@ def randomize_misc_pokemon(world: "PokemonEmeraldWorld") -> None:
if len(player_filtered_candidates) > 0: if len(player_filtered_candidates) > 0:
candidates = player_filtered_candidates candidates = player_filtered_candidates
world.modified_misc_pokemon.append(MiscPokemonData( world.modified_misc_pokemon.append(encounter._replace(
world.random.choice(candidates).species_id, species_id=world.random.choice(candidates).species_id
encounter.address
)) ))