diff --git a/packages/pokemon/src/ui/PokedexView.tsx b/packages/pokemon/src/ui/PokedexView.tsx index a55b80d99..587b462b7 100644 --- a/packages/pokemon/src/ui/PokedexView.tsx +++ b/packages/pokemon/src/ui/PokedexView.tsx @@ -154,10 +154,33 @@ function getTypeColor(type: string): Color { /** Group species by evolution chain for visual display */ function groupByChain(): SpeciesId[][] { - return [ - ['bulbasaur', 'ivysaur', 'venusaur'], - ['charmander', 'charmeleon', 'charizard'], - ['squirtle', 'wartortle', 'blastoise'], - ['pikachu'], - ] + const visited = new Set() + const chains: SpeciesId[][] = [] + + for (const id of ALL_SPECIES_IDS) { + if (visited.has(id)) continue + + // Walk back to find chain head + let head: SpeciesId = id + for (const candidate of ALL_SPECIES_IDS) { + const evo = getNextEvolution(candidate) + if (evo?.to === head) { + head = candidate + break + } + } + + // Walk forward to build chain + const chain: SpeciesId[] = [] + let current: SpeciesId | undefined = head + while (current && !visited.has(current)) { + chain.push(current) + visited.add(current) + current = getNextEvolution(current)?.to + } + + if (chain.length > 0) chains.push(chain) + } + + return chains } diff --git a/packages/pokemon/src/ui/SpeciesDetail.tsx b/packages/pokemon/src/ui/SpeciesDetail.tsx index 437b67648..f499a2ed1 100644 --- a/packages/pokemon/src/ui/SpeciesDetail.tsx +++ b/packages/pokemon/src/ui/SpeciesDetail.tsx @@ -1,7 +1,7 @@ import React from 'react' import { Box, Text, type Color } from '@anthropic/ink' import type { SpeciesId, StatName } from '../types' -import { STAT_NAMES, STAT_LABELS } from '../types' +import { STAT_NAMES, STAT_LABELS, ALL_SPECIES_IDS } from '../types' import { getSpeciesData } from '../data/species' import { getNextEvolution } from '../data/evolution' import { StatBar } from './StatBar' @@ -135,12 +135,12 @@ export function SpeciesDetail({ speciesId, caughtLevel, spriteLines }: SpeciesDe /** Render evolution chain arrow */ function EvolutionChain({ speciesId }: { speciesId: SpeciesId }) { - // Find the chain head - const chainHeads: SpeciesId[] = ['bulbasaur', 'charmander', 'squirtle', 'pikachu'] + // Walk back to find chain head let head: SpeciesId = speciesId - for (const starter of chainHeads) { - if (isInChain(speciesId, starter)) { - head = starter + for (const candidate of ALL_SPECIES_IDS) { + const evo = getNextEvolution(candidate) + if (evo?.to === head) { + head = candidate break } } @@ -174,13 +174,3 @@ function EvolutionChain({ speciesId }: { speciesId: SpeciesId }) { ) } -function isInChain(target: SpeciesId, head: SpeciesId): boolean { - let current: SpeciesId | undefined = head - while (current) { - if (current === target) return true - const next = getNextEvolution(current) - current = next ? next.to : undefined - } - return false -} -