import type { PartyMon } from "./pokemon"; /** The happiness a Pokémon has just after hatching (HatchEggs: `ld [hl], $78`), * which replaces the Egg's remaining cycles. */ export const HATCHED_HAPPINESS = 0x78; /** The counters CountStep keeps in work RAM (wStepCount, * wPoisonStepCount) and StepHappiness's toggle (wHappinessStepCount). */ export interface StepCounters { stepCount: number; poisonStepCount: number; happinessStepCount: number; } /** StepHappiness (engine/events/happiness_egg.asm): called each time * wStepCount wraps (every 256 steps), it raises the happiness of every * Pokémon that is not an Egg by one on every other call, so once per 512 * steps, up to 255. It does not go through ChangeHappiness: no bonus and * no cap other than the byte's, and a Pokémon already at 255 stays there. */ export function stepHappiness( counters: Pick, party: PartyMon[], ) { counters.happinessStepCount = (counters.happinessStepCount + 1) & 1; if (counters.happinessStepCount) return; for (const mon of party) if (!mon.egg) mon.happiness = Math.min(0xff, mon.happiness + 1); } /** DoEggStep (engine/pokemon/breeding.asm): from the first slot, each Egg's * cycles (kept in its happiness byte) drop by one until one reaches zero, * which ends the loop and reports that an Egg is ready to hatch; the Eggs * after it are not counted down on that step. A byte already at zero * wraps to 255 (`dec [hl]` has no floor) and does not hatch. */ export function doEggStep(party: PartyMon[]): boolean { for (const mon of party) { if (!mon.egg) continue; mon.happiness = (mon.happiness - 1) & 0xff; if (!mon.happiness) return true; } return false; }