diff --git a/services/flysim/crates/flysim/examples/scene_probe.rs b/services/flysim/crates/flysim/examples/scene_probe.rs index 6950d94..1bba3dc 100644 --- a/services/flysim/crates/flysim/examples/scene_probe.rs +++ b/services/flysim/crates/flysim/examples/scene_probe.rs @@ -1743,6 +1743,7 @@ fn offers_survey( layer: &mut flysim::macros::MacroLayer, decoder: &mut PopulationDecoder, hold_ms: f64, + checkpoint: &flysim::store::Checkpoint, ) { use flybrain_gb::pokemon_red::macros::cartridge::MacroState; use flybrain_gb::pokemon_red::macros::geography::{self, Amenity}; @@ -1764,6 +1765,9 @@ fn offers_survey( let hp: Vec = party.mons.iter().map(|mon| format!("{}/{} {:?}", mon.hp, mon.max_hp, mon.status)).collect(); let bag: Vec<(u8, u8)> = state::bag(gb).iter().map(|item| (item.id, item.count)).collect(); + let owned: u32 = (0..19u16).map(|i| gb.read8(ram::wPokedexOwned + i).count_ones()).sum(); + let species: Vec = party.mons.iter().map(|mon| mon.species).collect(); + let row66 = format!("- row66: party count {} species {species:?}, box count {}, pokedex owned {owned}", gb.read8(ram::wPartyCount), gb.read8(0xda80)); let money = state::money(gb); let last = blackout(gb); let player = state::player(gb); @@ -1775,6 +1779,7 @@ fn offers_survey( println!("- party {hp:?}; needs rest {}", palette::party_needs_rest(st)); println!("- money {money}, bag {bag:?}"); println!("- wLastBlackoutMap ($d719) = {last:#04x}"); + println!("{row66}"); let centers: Vec<(String, bool)> = CENTERS.iter().map(|map| (format!("{map:#04x}"), st.map_visited(*map))).collect(); let marts: Vec<(String, bool)> = @@ -1820,6 +1825,10 @@ fn offers_survey( let mut was_full = true; let mut counts: BTreeMap<&str, u64> = BTreeMap::new(); let mut maps_seen: BTreeMap = BTreeMap::new(); + let owned_now = |gb: &mut Emulator| -> u32 { (0..19u16).map(|i| gb.read8(ram::wPokedexOwned + i).count_ones()).sum() }; + let mut last_owned = owned_now(gb); + let mut last_mons = u32::from(gb.read8(ram::wPartyCount)) + u32::from(gb.read8(0xda80)); + let mut was_wild = false; for frame in 0..budget { let bound = layer.bound_channels(); if *ms >= next_pick && !bound.is_empty() && layer.running().is_none() { @@ -1882,6 +1891,15 @@ fn offers_survey( } } let executed = legacy.execute(Some(layer), &active, 0, *ms, gb, adapter); + if let (Some(from), Some(to)) = (std::env::var("FLY_PROBE_SCREEN_FROM").ok().and_then(|v| v.parse::().ok()), std::env::var("FLY_PROBE_SCREEN_TO").ok().and_then(|v| v.parse::().ok())) + && frame >= from && frame <= to && (frame - from) % env_usize("FLY_PROBE_SCREEN_EVERY", 100) == 0 + { + let battle = state::battle(gb).map(|battle| format!("{:?}", battle.menu)); + println!(" screen f{frame} mask {:#04x} scene {} pad {names:?} running {:?} battle-menu {battle:?} text {:?} prompt {} wIsInBattle {} boxes {:?} listid {:#04x} itemlist {:02x?} shop {:?} stock {:?}", executed.mask, layer.scene_name(), layer.running(), state::text_box(gb), state::yes_no_prompt(gb), gb.read8(ram::wIsInBattle), state::drawn_boxes(gb), gb.read8(ram::wListMenuID), (0..6u16).map(|i| gb.read8(ram::wItemList + i)).collect::>(), state::shop(gb).map(|s| s.screen), state::shop_stock(gb)); + for line in screen_text(gb) { + println!(" | {line}"); + } + } // `FLY_PROBE_TRACE_MACRO=BUY BALL` prints every frame of that macro's first three runs: // the mask, the scene, the counter's screen, the cursor and the wallet. if let Some(want) = trace_macro.as_deref() @@ -1953,6 +1971,23 @@ fn offers_survey( } was_wiped = wiped; was_full = full; + let owned = owned_now(gb); + if owned > last_owned { + *counts.entry("row66 pokedex owned +").or_default() += u64::from(owned - last_owned); + log.push(format!("f{frame} pokedex owned {last_owned} -> {owned}")); + } + last_owned = owned; + let mons = u32::from(gb.read8(ram::wPartyCount)) + u32::from(gb.read8(0xda80)); + if mons > last_mons && mons <= 26 { + *counts.entry("row66 mons gained (party+box)").or_default() += u64::from(mons - last_mons); + log.push(format!("f{frame} party+box {last_mons} -> {mons}")); + } + if mons <= 26 { last_mons = mons; } + let wild = gb.read8(ram::wIsInBattle) == 1; + if wild && !was_wild { + *counts.entry(if balls(gb) > 0 { "row66 wild battles with a ball" } else { "row66 wild battles, no ball" }).or_default() += 1; + } + was_wild = wild; let now_balls = balls(gb); if now_balls > last_balls { *counts.entry("balls bought").or_default() += u64::from(now_balls - last_balls); @@ -1974,8 +2009,22 @@ fn offers_survey( last_blackout = now_blackout; } if map != last_map { + // Row 66: `FLY_PROBE_SAVE_MART=` with `FLY_PROBE_SAVE` writes the first frame the fly + // walks into that mart with the money for a ball, no ball and no parcel. + if let (Some(want), Some(path)) = (std::env::var("FLY_PROBE_SAVE_MART").ok().and_then(|v| u8::from_str_radix(v.trim_start_matches("0x"), 16).ok()), std::env::var_os("FLY_PROBE_SAVE")) + && map == Some(want) + && money >= 200 + && frame >= env_usize("FLY_PROBE_SAVE_AFTER", 0) + && balls(gb) == 0 + && !state::bag(gb).iter().any(|item| item.id == 0x46) + && !std::path::Path::new(&path).exists() + { + let bytes = save_state(checkpoint, gb, adapter, frame, &path); + log.push(format!("f{frame} saved {bytes} bytes to {}", std::path::Path::new(&path).display())); + } if map.is_some_and(|map| CENTERS.contains(&map) || MARTS.contains(&map)) { - log.push(format!("f{frame} entered {map:?} (party full {full}, money {money})")); + let parcel = state::bag(gb).iter().any(|item| item.id == 0x46); + log.push(format!("f{frame} entered {map:?} (party full {full}, money {money}, balls {}, parcel {parcel})", balls(gb))); } last_map = map; } @@ -2152,7 +2201,7 @@ fn main() { // Row 62's offer survey: which shop, centre and ball buttons are dealt, frame by frame. if std::env::var("FLY_PROBE_CATCH").is_ok_and(|value| value == "offers") { - offers_survey(&mut gb, &mut adapter, &mut ms, &mut layer, &mut decoder, hold_ms); + offers_survey(&mut gb, &mut adapter, &mut ms, &mut layer, &mut decoder, hold_ms, &checkpoint); return; } diff --git a/services/flysim/crates/flysim/examples/trap_hunt.rs b/services/flysim/crates/flysim/examples/trap_hunt.rs index b5ad5e5..7c5947e 100644 --- a/services/flysim/crates/flysim/examples/trap_hunt.rs +++ b/services/flysim/crates/flysim/examples/trap_hunt.rs @@ -235,6 +235,39 @@ struct Trace { payouts_by_kind: BTreeMap<&'static str, (u64, f64)>, move_starts: (u64, u64), wall_seconds: f64, + row66: Row66, +} + +/// Row 66's shop/ball/catch tally: pad frames, starts and outcomes of the watched buttons, and +/// the cartridge beside them. +#[derive(Default)] +struct Row66 { + /// channel -> [free frames on the pad, starts, done, other outcomes] + tally: BTreeMap<&'static str, [u64; 4]>, + free_frames: u64, + counts: BTreeMap<&'static str, u64>, + log: Vec, + last: Option<(u32, u32, u32, u32, u8)>, + was_wild: bool, +} + +const ROW66_WATCH: [&str; 8] = [ + "GO SHOP", "TALK", "CONFIRM", "BUY BALL", "BUY POTION", "LEAVE", "THROW BALL", "GO HEAL", +]; + +fn row66_read(gb: &mut flybrain_gb::Emulator) -> (u32, u32, u32, u32, u8) { + use flybrain_gb::MemoryReader; + use flybrain_gb::pokemon_red::symbols::ram; + let balls: u32 = flybrain_gb::pokemon_red::state::bag(gb) + .iter() + .filter(|item| (0x01..=0x04).contains(&item.id)) + .map(|item| u32::from(item.count)) + .sum(); + let money = flybrain_gb::pokemon_red::state::money(gb); + let mons = u32::from(gb.read8(ram::wPartyCount)) + u32::from(gb.read8(0xda80)); + let owned: u32 = (0..19u16).map(|i| gb.read8(ram::wPokedexOwned + i).count_ones()).sum(); + let map = flybrain_gb::pokemon_red::state::player(gb).map_or(0xff, |player| player.map); + (balls, money, mons, owned, map) } /// Which sub-state of a battle this frame is, or `None` when no battle is running. @@ -330,6 +363,20 @@ impl FrameObserver for Hunt { .then(|| flybrain_gb::pokemon_red::state::player(parts.emulator).map(|p| p.map)) .flatten(); self.battle_sub = battle_sub_state(parts.emulator); + if let Some(layer) = parts.macros.as_deref() + && layer.running().is_none() + { + let names: Vec = layer.feed_palette().into_iter().map(|slot| slot.name).collect(); + if !names.is_empty() { + let row = &mut self.trace.row66; + row.free_frames += 1; + for watch in ROW66_WATCH { + if names.iter().any(|name| name == watch) { + row.tally.entry(watch).or_default()[0] += 1; + } + } + } + } if let Some(sub) = self.battle_sub { *self.trace.battle_frames.entry(sub).or_insert(0) += 1; let pad = self.trace.battle_pads.entry(sub).or_default(); @@ -344,6 +391,20 @@ impl FrameObserver for Hunt { let location = frame.location; let trace = &mut self.trace; for event in &executed.events { + if std::env::var("FLY_TRAP_LOG_MACROS").is_ok_and(|value| value == "1") { + let pad: Vec = parts.macros.as_deref().map(|layer| layer.feed_palette().into_iter().map(|slot| slot.name).collect()).unwrap_or_default(); + println!( + "macro {:8.2}s {:?} {} {} pad {pad:?}", + (ms - trace.began_ms) / 1000.0, + location, + event.name, + event.outcome.map_or("start", |outcome| outcome.as_str()) + ); + } + if let Some(watch) = ROW66_WATCH.iter().find(|watch| **watch == event.name) { + let key = match event.outcome { None => 1, Some(o) if o.as_str() == "done" => 2, Some(_) => 3 }; + trace.row66.tally.entry(watch).or_default()[key] += 1; + } match event.outcome { None => { trace.starts.push((ms, event.name)); @@ -564,6 +625,7 @@ fn run( payouts_by_kind: BTreeMap::new(), move_starts: (0, 0), wall_seconds: 0.0, + row66: Row66::default(), seeded: seeded_note, refusals: BTreeMap::new(), refusal_run: (None, 0), @@ -640,6 +702,32 @@ fn run( ); } + { + use flybrain_gb::MemoryReader; + let minute = (ms - began_ms) / MINUTE_MS; + let now = row66_read(parts.emulator); + let wild = parts.emulator.read8(flybrain_gb::pokemon_red::symbols::ram::wIsInBattle) == 1; + let row = &mut hunt.trace.row66; + if wild && !row.was_wild { + *row.counts.entry(if now.0 > 0 { "wild battles with a ball" } else { "wild battles, no ball" }).or_default() += 1; + } + row.was_wild = wild; + if let Some(last) = row.last { + if now.0 > last.0 { *row.counts.entry("balls gained").or_default() += u64::from(now.0 - last.0); row.log.push(format!("{minute:.2} min balls {} -> {} (map {:#04x})", last.0, now.0, now.4)); } + if now.0 < last.0 { *row.counts.entry("balls spent").or_default() += u64::from(last.0 - now.0); row.log.push(format!("{minute:.2} min balls {} -> {} (map {:#04x})", last.0, now.0, now.4)); } + if now.1 < last.1 && [0x2a, 0x38].contains(&now.4) { row.log.push(format!("{minute:.2} min money {} -> {} in mart {:#04x}", last.1, now.1, now.4)); } + if now.2 > last.2 && now.2 <= 26 { *row.counts.entry("mons gained (party+box)").or_default() += u64::from(now.2 - last.2); row.log.push(format!("{minute:.2} min party+box {} -> {}", last.2, now.2)); } + if now.3 > last.3 { *row.counts.entry("pokedex owned +").or_default() += u64::from(now.3 - last.3); row.log.push(format!("{minute:.2} min pokedex owned {} -> {}", last.3, now.3)); } + if now.4 != last.4 && [0x2a, 0x38, 0x43].contains(&now.4) { + *row.counts.entry("mart entries").or_default() += 1; + let parcel = flybrain_gb::pokemon_red::state::bag(parts.emulator).iter().any(|item| item.id == 0x46); + row.log.push(format!("{minute:.2} min entered mart {:#04x} (money {}, balls {}, parcel {parcel})", now.4, now.1, now.0)); + } + } else { + row.log.push(format!("start: balls {} money {} party+box {} owned {} map {:#04x}", now.0, now.1, now.2, now.3, now.4)); + } + row.last = Some(now); + } let location = frame.location; if let Some((map, x, y)) = location { hunt.trace.steps.push((ms, map, x, y)); @@ -1000,6 +1088,18 @@ fn main() { trace.ended_in.0, trace.ended_in.1, trace.ended_why ); walk_report(&trace); + { + let row = &trace.row66; + println!("\n## Row 66: shop, balls, catches\n\n{} free frames with a pad\n", row.free_frames); + println!("| button | frames on the pad | starts | done | other |\n| --- | ---: | ---: | ---: | ---: |"); + for watch in ROW66_WATCH { + let [a, b, c, d] = row.tally.get(watch).copied().unwrap_or_default(); + println!("| {watch} | {a} | {b} | {c} | {d} |"); + } + println!("\n- counts: {:?}", row.counts); + if let Some(last) = row.last { println!("- end: balls {} money {} party+box {} owned {} map {:#04x}", last.0, last.1, last.2, last.3, last.4); } + for line in row.log.iter().take(150) { println!("- {line}"); } + } for (rank, label, ms) in &trace.rungs { println!("- rung {rank} {label} at {:.2} brain minutes", ms / MINUTE_MS); }