diff --git a/randomizer/core/src/full_wallet.rs b/randomizer/core/src/full_wallet.rs new file mode 100644 index 0000000..7673ca1 --- /dev/null +++ b/randomizer/core/src/full_wallet.rs @@ -0,0 +1,154 @@ +pub mod fill; +pub mod sites; + +use std::fs; +use std::path::{Path as P, PathBuf as PB}; + +use crate::bfevfl::Bf; +use crate::error::{Error as E, Res}; +use crate::flow_locations::dict_id; +use crate::modpack::{Host, Pack}; +use crate::pickup_pack::write::start; +use crate::romfs::Romfs; +use crate::rsizetable::{self, Rst, TABLE_PATH}; +use crate::slice::edit::Built; +use crate::staging::MARGIN; +use crate::start_items::{Kit, Set}; +use crate::zs; +use crate::TITLE_ID; + +pub const SETTING: &str = "full_wallets"; +pub const NUM: u16 = 610; +pub const COUNT: &str = "PlayerStatus.CurrentRupee"; +pub const FIRST: (&str, i32) = ("Item_Wallet_Small", 200); +pub const CAPS: [(&str, i32); 2] = [("Item_Wallet_Medium", 500), ("Item_Wallet_Large", 1000)]; +pub const FLOWS: &str = "Event/EventFlow"; +pub const NOTE: &str = "Full Wallets: the new-game save sets the rupee count to the best starting wallet's capacity; after each Adult's or Giant's Wallet give (chest, person, reward, shop, pickup) the event flows open the rupee counter and add 1000 rupees, which the game caps at the wallet's capacity. Seen in the game: the chest give. The other gives and the new-game count are not yet seen."; + +pub fn cap(actor: &str) -> Option { + CAPS.iter().find(|(a, _)| *a == actor).map(|(_, n)| *n) +} + +pub fn start_cap(k: &Kit) -> i32 { + CAPS.iter().filter(|(a, _)| k.pushes.contains(a)).map(|(_, n)| *n).max().unwrap_or(FIRST.1) +} + +pub fn start_sets(k: &Kit) -> Vec { + vec![Set::Int(COUNT.to_string(), start_cap(k))] +} + +fn flow_files(base: &P) -> Res> { + let mut out: Vec = sites::fixed().iter().map(|f| sites::path_of(f)).collect(); + let dir: PB = FLOWS.split('/').fold(base.to_path_buf(), |a, s| a.join(s)); + if dir.is_dir() { + for e in fs::read_dir(&dir)? { + let name: String = e?.file_name().to_string_lossy().to_string(); + if name.ends_with(".bfevfl.zs") { + out.push(format!("{FLOWS}/{name}")); + } + } + } + out.sort(); + out.dedup(); + Ok(out) +} + +fn local(base: &P, path: &str) -> PB { + path.split('/').fold(base.to_path_buf(), |a, s| a.join(s)) +} + +pub fn after(on: bool, romfs: &P, load: &P, mod_name: &str, host: Host, mut b: Built) -> Res { + if !on { + return Ok(b); + } + let mut rf: Romfs = Romfs::open(romfs)?; + let base: PB = b.root.join("romfs"); + let own: PB = base.join(TABLE_PATH); + let mut rst: Rst = if own.is_file() { Rst::parse(&rf.zs.decompress(&fs::read(&own)?)?)? } else { rsizetable::load(&mut rf)? }; + let pk: Pack = Pack::create_for(load, TITLE_ID, mod_name, host)?; + if pk.root != b.root { + return Err(E::Msg(format!("the wallet flows went to {} and not to the seed mod {}", pk.root.display(), b.root.display()))); + } + let mut lines: Vec = Vec::new(); + for path in flow_files(&base)? { + let modded: bool = local(&base, &path).is_file(); + let first: Vec = start(&mut rf, Some(&base), &path)?; + let mut bf: Bf = Bf::parse(&first)?; + let mut n: usize = 0; + for f in bf.flows.iter_mut() { + n += sites::patch(f)?; + } + if n == 0 { + continue; + } + let raw: Vec = bf.write()?; + let old: usize = if modded { first.len() } else { rf.read_zs(&path)?.len() }; + pk.write_romfs(&path, &zs::compress_raw(&raw, dict_id(&rf.read(&path)?))?)?; + if raw.len() > old { + rst.grow(&path, old as u64, raw.len() as u64, MARGIN)?; + } + lines.push(format!("{path} ({n})")); + b.counts.files += usize::from(!modded); + } + pk.write_romfs(TABLE_PATH, &rst.to_zs()?)?; + b.report.push_str(&format!("Full Wallets: wallet fills added at {} give points: {}\n", lines.len(), lines.join(", "))); + Ok(b) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_new_game_starts_with_its_best_wallet_full() { + let mut k: Kit = Kit::default(); + assert_eq!(start_sets(&k), vec![Set::Int(COUNT.to_string(), 200)]); + k.pushes.push("Item_Wallet_Medium"); + assert_eq!(start_cap(&k), 500); + k.pushes.push("Item_Wallet_Large"); + assert_eq!(start_sets(&k), vec![Set::Int(COUNT.to_string(), 1000)]); + assert!(NUM >= 600); + assert_eq!(cap(FIRST.0), None); + } + + #[test] + fn a_full_wallets_build_leaves_the_navi_table_alone() { + let navi: &str = "GameParameter/NaviMgrGlobalParam/GlobalGameParameterTable.game__navi__NaviMgrGlobalParam.bgyml"; + let d: crate::slice::data::Data = crate::slice::data::Data::load().unwrap(); + assert!(!crate::required::unpack(&d, false).wants(navi)); + let p: PB = ootrr_game::test_romfs::path(); + if !p.join("Pack").join("ZsDic.pack.zs").is_file() { + eprintln!("skipped, no RomFS dump at {} (set OOTRR_ROMFS)", p.display()); + return; + } + let mut s: crate::settings::Settings = crate::settings::Settings::default(); + s.set(SETTING, crate::settings::Value::Bool(true)).unwrap(); + let g: crate::slice::generate::Generated = crate::slice::generate::generate_data(&d, &s, "full wallet navi", &crate::slice::generate::GenOpts::default()).unwrap(); + let load: PB = std::env::temp_dir().join(format!("ootrr_full_wallet_navi_{}", std::process::id())); + let _ = fs::remove_dir_all(&load); + let b: Built = crate::slice::edit::build_for(&g.seed_file, &p, &load, "fullwallet", Host::Eden).unwrap(); + assert!(b.report.contains("Full Wallets: wallet fills added at"), "{}", b.report); + assert!(!local(&b.root.join("romfs"), navi).exists()); + let _ = fs::remove_dir_all(&load); + } + + #[test] + fn the_fill_tops_up_every_wallet_and_the_caps_match_the_game_wallet_table() { + assert!(CAPS.iter().all(|(_, n)| *n <= fill::FILL)); + assert_eq!(sites::WALLETS.to_vec(), CAPS.iter().map(|c| c.0).collect::>()); + let p: PB = ootrr_game::test_romfs::path(); + if !p.join("Pack").join("ZsDic.pack.zs").is_file() { + eprintln!("skipped, no RomFS dump at {} (set OOTRR_ROMFS)", p.display()); + return; + } + let mut rf: Romfs = Romfs::open(&p).unwrap(); + let y: crate::byml::Byml = crate::byml::Byml::parse(&rf.read_zs(crate::pickup_pack::rows::POUCH_INFO).unwrap()).unwrap(); + for (a, n) in std::iter::once(FIRST).chain(CAPS) { + for id in [a.to_string(), a.replace("Item_", "Pouch_")] { + let r: &crate::byml::Node = crate::pickup_pack::rows::find(&y, &id).unwrap(); + assert_eq!(r.get(crate::pickup_pack::ammo::KIND).and_then(|v| v.as_str()), Some("Rupee"), "{id}"); + assert_eq!(r.get(crate::pickup_pack::ammo::MAX), Some(&crate::byml::Node::I32(n)), "{id}"); + } + } + } +} diff --git a/randomizer/core/src/full_wallet/fill.rs b/randomizer/core/src/full_wallet/fill.rs new file mode 100644 index 0000000..6efeed8 --- /dev/null +++ b/randomizer/core/src/full_wallet/fill.rs @@ -0,0 +1,78 @@ +use crate::bfevfl::flow::NONE; +use crate::bfevfl::{Case, Cont, Event, Flow, Kind, Val}; +use crate::error::{Error as E, Res}; + +pub const PREFIX: &str = "OOTRRFullWallet"; +pub const FILL: i32 = 1000; +pub const HOLD: i32 = 60; +pub const NAME: &str = "UI_MessageTargetActorName"; +pub const UI: &str = "UISystemActor"; +pub const OPEN: &str = "EventOpenRupeeDisplay"; +pub const STABLE: &str = "EventWaitStableNumDisplay"; +pub const CLOSE: &str = "EventCloseRupeeDisplay"; +pub const POUCH: &str = "PouchSystemActor"; +pub const ADD: &str = "EventTriggerIncreaseRupee"; +pub const WAITER: &str = "EventSystemActor"; +pub const WAIT: &str = "EventWait"; +pub const DATA: &str = "GameDataActor"; +pub const ASK: &str = "EventQueryIsGameDataString64"; +pub const SET: &str = "EventTriggerSetGameDataString64"; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Show { + Popup, + Shop, +} + +fn b(v: bool) -> Val { + Val::Bool(u32::from(v)) +} + +fn kv(k: &str, v: Val) -> (String, Val) { + (k.to_string(), v) +} + +fn push(f: &mut Flow, kind: Kind) -> Res { + let at: u16 = u16::try_from(f.events.len()).ok().filter(|i| *i != NONE).ok_or(E::Format("full wallets: the flow has too many events"))?; + f.events.push(Event { name: format!("{PREFIX}{at}"), kind }); + Ok(at) +} + +fn act(f: &mut Flow, who: &str, what: &str, items: Vec<(String, Val)>, next: u16) -> Res { + let (a, x): (u16, u16) = crate::start_state::slot(f, who, what)?; + push(f, Kind::Action { next, actor: a, act: x, params: Some(Cont { items }) }) +} + +pub fn ours(f: &Flow, at: u16) -> bool { + at != NONE && f.events.get(usize::from(at)).is_some_and(|e| e.name.starts_with(PREFIX)) +} + +pub fn fill(f: &mut Flow, show: Show, then: u16) -> Res { + let mut next: u16 = then; + if show == Show::Popup { + next = act(f, UI, CLOSE, vec![kv("IsWaitEnd", b(true))], next)?; + next = act(f, WAITER, WAIT, vec![kv("IsWaitEnd", b(true)), kv("Frames", Val::Int(HOLD))], next)?; + } + next = act(f, UI, STABLE, vec![kv("IsWaitEnd", b(true))], next)?; + next = act(f, POUCH, ADD, vec![kv("Value", Val::Int(FILL))], next)?; + if show == Show::Popup { + let open: Vec<(String, Val)> = vec![kv("IsWaitEnd", b(true)), kv("IsNeedMaxNum", b(false)), kv("IsWaitOpenAnim", b(true)), kv("IsWaitChoiceBtnIn", b(false))]; + next = act(f, UI, OPEN, open, next)?; + } + Ok(next) +} + +pub fn asked(f: &mut Flow, names: &[String], hit: u16, miss: u16) -> Res { + let (a, q): (u16, u16) = crate::progressive::ensure(f, DATA, ASK)?; + let mut at: u16 = miss; + for n in names.iter().rev() { + let c: Cont = Cont { items: vec![kv("GameDataName", Val::Str(NAME.to_string())), kv("Index", Val::Int(-1)), kv("Value", Val::Str(n.clone()))] }; + at = push(f, Kind::Switch { actor: a, query: q, cases: vec![Case { val: 0, ev: hit }, Case { val: 1, ev: at }], params: Some(c) })?; + } + Ok(at) +} + +pub fn named(f: &mut Flow, arg: &str, next: u16) -> Res { + let settle: u16 = act(f, WAITER, WAIT, vec![kv("IsWaitEnd", b(true)), kv("Frames", Val::Int(1))], next)?; + act(f, DATA, SET, vec![kv("GameDataName", Val::Str(NAME.to_string())), kv("Value", Val::Arg(arg.to_string())), kv("Index", Val::Int(-1))], settle) +} diff --git a/randomizer/core/src/full_wallet/sites.rs b/randomizer/core/src/full_wallet/sites.rs new file mode 100644 index 0000000..dafbf64 --- /dev/null +++ b/randomizer/core/src/full_wallet/sites.rs @@ -0,0 +1,503 @@ +use crate::bfevfl::{Flow, Kind, Val}; +use crate::error::{Error as E, Res}; +use crate::full_wallet::fill::{asked, fill, named, ours, Show}; + +pub const WALLETS: [&str; 2] = ["Item_Wallet_Medium", "Item_Wallet_Large"]; +pub const FAMILY: &str = "Wallet"; +pub const POUCH: &str = "PouchSystemActor"; +pub const ADD: &str = "EventTriggerAddOrRemovePouchContent"; +pub const GUEST: &str = "PouchContentGuestPerformer"; +pub const ADD_OWN: &str = "EventTriggerAddOwnToPouch"; + +pub struct Give { + pub flow: &'static str, + pub event: &'static str, + pub show: Show, +} + +pub const GIVES: [Give; 4] = [ + Give { flow: "GetPouchContent", event: "Event0", show: Show::Popup }, + Give { flow: "Dm_SY_GetInEvent", event: "Event124", show: Show::Popup }, + Give { flow: "Dm_SY_GetInEvent", event: "Event442", show: Show::Shop }, + Give { flow: "Dm_SY_GetInEvent", event: "Event430", show: Show::Shop }, +]; +pub const PICKUPS: [(&str, &str); 2] = [("Dm_SY_Get", "Event35"), ("Dm_SY_Get_WithoutPlayer", "Event0")]; +pub const SHOPS: [&str; 1] = ["DisplayShop"]; + +pub fn names() -> Res> { + let mut out: Vec = Vec::new(); + for (t, w) in WALLETS.iter().enumerate() { + let mut n: Vec = vec![w.to_string(), w.replace("Item_", "Pouch_"), w.replace("Item_", "Obj_")]; + n.extend(crate::pickup_tiers::names(FAMILY, t)?); + for x in n { + if !out.contains(&x) { + out.push(x); + } + } + } + Ok(out) +} + +pub fn path_of(flow: &str) -> String { + format!("Event/EventFlow/{flow}.bfevfl.zs") +} + +pub fn fixed() -> Vec<&'static str> { + let mut out: Vec<&str> = GIVES.iter().map(|g| g.flow).chain(PICKUPS.iter().map(|p| p.0)).collect(); + out.sort(); + out.dedup(); + out +} + +fn is(f: &Flow, i: usize, who: &str, what: &str) -> bool { + match f.events.get(i).map(|e| &e.kind) { + Some(Kind::Action { actor, act, .. }) => f.actors.get(usize::from(*actor)).is_some_and(|a| a.name == who && a.actions.get(usize::from(*act)).is_some_and(|x| x == what)), + _ => false, + } +} + +fn next_of(f: &Flow, i: usize) -> Option { + match f.events.get(i).map(|e| &e.kind) { + Some(Kind::Action { next, .. }) | Some(Kind::Sub { next, .. }) => Some(*next), + _ => None, + } +} + +fn set_next(f: &mut Flow, i: usize, to: u16) { + if let Some(Kind::Action { next, .. }) | Some(Kind::Sub { next, .. }) = f.events.get_mut(i).map(|e| &mut e.kind) { + *next = to; + } +} + +fn unique(f: &Flow, name: &str) -> Res { + let hits: Vec = (0..f.events.len()).filter(|&i| f.events[i].name == name).collect(); + match hits.as_slice() { + [i] => Ok(*i), + _ => Err(E::Msg(format!("full wallets: {} has {} events named {name}, expected one, the game files differ", f.name, hits.len()))), + } +} + +fn num(f: &Flow, i: usize) -> Option { + match f.events.get(i)?.kind.params()?.get("Num")? { + Val::Int(n) => Some(*n), + _ => None, + } +} + +pub fn give(f: &mut Flow, g: &Give) -> Res { + let at: usize = unique(f, g.event)?; + let arg: String = match f.events[at].kind.params().and_then(|c| c.get("Actor")) { + Some(Val::Arg(a)) if is(f, at, POUCH, ADD) && num(f, at).is_some_and(|n| n > 0) => a.clone(), + _ => return Err(E::Msg(format!("full wallets: {} {} is not a pouch add of an argument actor, the game files differ", f.name, g.event))), + }; + let then: u16 = next_of(f, at).ok_or(E::Format("full wallets: the pouch add has no next event"))?; + if ours(f, then) { + return Ok(false); + } + let hit: u16 = fill(f, g.show, then)?; + let ask: u16 = asked(f, &names()?, hit, then)?; + let head: u16 = named(f, &arg, ask)?; + set_next(f, at, head); + Ok(true) +} + +pub fn pickup(f: &mut Flow, event: &str) -> Res { + let at: usize = unique(f, event)?; + if !is(f, at, GUEST, ADD_OWN) { + return Err(E::Msg(format!("full wallets: {} {event} is not the pickup's pouch add, the game files differ", f.name))); + } + let then: u16 = next_of(f, at).ok_or(E::Format("full wallets: the pickup's pouch add has no next event"))?; + if ours(f, then) { + return Ok(false); + } + let tails: Vec = (0..f.events.len()).filter(|&i| i == at || (f.events[i].name.starts_with(crate::pickup_tiers::PREFIX) && next_of(f, i) == Some(then))).collect(); + let hit: u16 = fill(f, Show::Popup, then)?; + let head: u16 = asked(f, &names()?, hit, then)?; + for i in tails { + set_next(f, i, head); + } + Ok(true) +} + +pub fn literal(f: &Flow, i: usize) -> bool { + let wallet: bool = matches!(f.events[i].kind.params().and_then(|c| c.get("Actor")), Some(Val::Str(s)) if WALLETS.contains(&s.as_str())); + wallet && is(f, i, POUCH, ADD) && num(f, i).is_some_and(|n| n > 0) +} + +pub fn literals(f: &mut Flow, show: Show) -> Res { + let hits: Vec = (0..f.events.len()).filter(|&i| literal(f, i)).collect(); + let mut n: usize = 0; + for i in hits { + let then: u16 = next_of(f, i).ok_or(E::Format("full wallets: a wallet add has no next event"))?; + if ours(f, then) { + continue; + } + let hit: u16 = fill(f, show, then)?; + set_next(f, i, hit); + n += 1; + } + Ok(n) +} + +pub fn is_site(flow: &str, event: &str) -> bool { + GIVES.iter().any(|g| g.flow == flow && g.event == event) || PICKUPS.iter().any(|p| p.0 == flow && p.1 == event) +} + +pub fn patch(f: &mut Flow) -> Res { + let mut n: usize = 0; + let name: String = f.name.clone(); + for g in GIVES.iter().filter(|g| g.flow == name) { + n += usize::from(give(f, g)?); + } + for (_, e) in PICKUPS.iter().filter(|p| p.0 == name) { + n += usize::from(pickup(f, e)?); + } + if !PICKUPS.iter().any(|p| p.0 == name) { + let show: Show = if SHOPS.contains(&name.as_str()) { Show::Shop } else { Show::Popup }; + n += literals(f, show)?; + } + Ok(n) +} + +#[cfg(test)] +mod tests { + use std::collections::{BTreeMap as BM, BTreeSet as BS}; + use std::path::PathBuf as PB; + + use super::*; + use crate::bfevfl::flow::NONE; + use crate::bfevfl::{Bf, Cont, Event}; + use crate::full_wallet::fill::{self, CLOSE, FILL, HOLD, NAME, OPEN, STABLE}; + use crate::romfs::Romfs; + + fn real() -> Option { + let p: PB = ootrr_game::test_romfs::path(); + if p.join("Pack").join("ZsDic.pack.zs").is_file() { + Some(p) + } else { + eprintln!("skipped, no RomFS dump at {} (set OOTRR_ROMFS)", p.display()); + None + } + } + + fn flow(rf: &mut Romfs, name: &str) -> Flow { + let b: Bf = Bf::parse(&rf.read_zs(&path_of(name)).unwrap()).unwrap(); + b.flows.into_iter().find(|f| f.name == name).unwrap() + } + + fn what(f: &Flow, i: u16) -> String { + match &f.events[usize::from(i)].kind { + Kind::Action { actor, act, .. } => format!("{}.{}", f.actors[usize::from(*actor)].name, f.actors[usize::from(*actor)].actions[usize::from(*act)]), + Kind::Switch { actor, query, .. } => format!("{}.{}", f.actors[usize::from(*actor)].name, f.actors[usize::from(*actor)].queries[usize::from(*query)]), + k => format!("{k:?}"), + } + } + + fn line(f: &Flow, from: u16, stop: u16) -> Vec { + let mut out: Vec = Vec::new(); + let mut at: u16 = from; + while at != stop { + assert!(ours(f, at), "event {at} of {} is not ours", f.name); + out.push(what(f, at)); + at = match &f.events[usize::from(at)].kind { + Kind::Action { next, .. } => *next, + k => panic!("not an action: {k:?}"), + }; + } + out + } + + fn param(f: &Flow, i: u16, k: &str) -> Option { + f.events[usize::from(i)].kind.params().and_then(|c| c.get(k)).cloned() + } + + fn popup() -> Vec { + [format!("{}.{OPEN}", fill::UI), "PouchSystemActor.EventTriggerIncreaseRupee".to_string(), format!("{}.{STABLE}", fill::UI), "EventSystemActor.EventWait".to_string(), format!("{}.{CLOSE}", fill::UI)].to_vec() + } + + fn shop() -> Vec { + ["PouchSystemActor.EventTriggerIncreaseRupee".to_string(), format!("{}.{STABLE}", fill::UI)].to_vec() + } + + fn check_fill(f: &Flow, hit: u16, then: u16, show: Show) { + assert_eq!(line(f, hit, then), if show == Show::Popup { popup() } else { shop() }); + let add: u16 = if show == Show::Popup { match &f.events[usize::from(hit)].kind { Kind::Action { next, .. } => *next, _ => NONE } } else { hit }; + assert_eq!(param(f, add, "Value"), Some(Val::Int(FILL))); + } + + fn asks(f: &Flow, head: u16, then: u16) -> u16 { + let mut at: u16 = head; + let mut seen: Vec = Vec::new(); + let mut hit: Option = None; + while at != then { + let Kind::Switch { cases, .. } = &f.events[usize::from(at)].kind else { panic!("not a question") }; + assert_eq!(what(f, at), "GameDataActor.EventQueryIsGameDataString64"); + assert_eq!(param(f, at, "GameDataName"), Some(Val::Str(NAME.to_string()))); + let Some(Val::Str(n)) = param(f, at, "Value") else { panic!("no name") }; + seen.push(n); + assert_eq!(cases.len(), 2); + assert!(hit.is_none_or(|h| h == cases[0].ev)); + hit = Some(cases[0].ev); + at = cases[1].ev; + } + assert_eq!(seen, names().unwrap()); + hit.unwrap() + } + + #[test] + fn each_give_routine_add_fills_only_after_a_wallet_and_keeps_its_path() { + let Some(p) = real() else { + return; + }; + let mut rf: Romfs = Romfs::open(&p).unwrap(); + for g in GIVES.iter() { + let was: Flow = flow(&mut rf, g.flow); + let mut f: Flow = was.clone(); + let at: usize = unique(&f, g.event).unwrap(); + let then: u16 = next_of(&f, at).unwrap(); + let Some(Val::Arg(arg)) = param(&f, at as u16, "Actor") else { panic!("{} {}", g.flow, g.event) }; + assert!(give(&mut f, g).unwrap()); + let head: u16 = next_of(&f, at).unwrap(); + assert_eq!(what(&f, head), format!("{}.{}", fill::DATA, fill::SET)); + assert_eq!(param(&f, head, "Value"), Some(Val::Arg(arg.clone()))); + assert_eq!(param(&f, head, "GameDataName"), Some(Val::Str(NAME.to_string()))); + let settle: u16 = match &f.events[usize::from(head)].kind { Kind::Action { next, .. } => *next, _ => NONE }; + assert_eq!(what(&f, settle), "EventSystemActor.EventWait"); + assert_eq!(param(&f, settle, "Frames"), Some(Val::Int(1))); + let first: u16 = match &f.events[usize::from(settle)].kind { Kind::Action { next, .. } => *next, _ => NONE }; + let hit: u16 = asks(&f, first, then); + check_fill(&f, hit, then, g.show); + for (i, e) in was.events.iter().enumerate() { + if i != at { + assert_eq!(&f.events[i], e, "{} event {i}", g.flow); + } + } + let again: Flow = f.clone(); + assert!(!give(&mut f, g).unwrap()); + assert_eq!(f, again); + } + } + + #[test] + fn a_ground_pickup_fills_after_its_pouch_add_with_and_without_pickup_tiers() { + let Some(p) = real() else { + return; + }; + let mut rf: Romfs = Romfs::open(&p).unwrap(); + let fams: BS<&str> = ["Wallet"].into_iter().collect(); + for ((name, event), (gf, gi, gn)) in PICKUPS.iter().zip(crate::pickup_tiers::GETS) { + assert_eq!((*name, *event), (gf, gn)); + for tiers in [false, true] { + let mut f: Flow = flow(&mut rf, name); + if tiers { + assert!(crate::pickup_tiers::patch(&mut f, gi, gn, &fams).unwrap()); + } + let at: usize = unique(&f, event).unwrap(); + let then: u16 = next_of(&f, at).unwrap(); + let before: usize = (0..f.events.len()).filter(|&i| next_of(&f, i) == Some(then)).count(); + assert_eq!(before, if tiers { 5 } else { 1 }, "{name} tiers {tiers}"); + assert!(pickup(&mut f, event).unwrap()); + let head: u16 = next_of(&f, at).unwrap(); + let into: usize = (0..f.events.len()).filter(|&i| next_of(&f, i) == Some(head)).count(); + assert_eq!(into, before, "{name} tiers {tiers}"); + let stray: Vec = (0..f.events.len()).filter(|&i| next_of(&f, i) == Some(then) && !ours(&f, i as u16)).collect(); + assert!(stray.is_empty(), "{name} tiers {tiers}: {stray:?}"); + let hit: u16 = asks(&f, head, then); + check_fill(&f, hit, then, Show::Popup); + assert!(!pickup(&mut f, event).unwrap()); + let mut bf: Bf = Bf::parse(&rf.read_zs(&path_of(name)).unwrap()).unwrap(); + let k: usize = bf.flows.iter().position(|x| x.name == *name).unwrap(); + bf.flows[k] = f.clone(); + assert_eq!(Bf::parse(&bf.write().unwrap()).unwrap().flows[k], f); + } + } + } + + #[test] + fn a_literal_wallet_add_fills_and_the_shop_keeps_its_own_counter() { + let Some(p) = real() else { + return; + }; + let mut rf: Romfs = Romfs::open(&p).unwrap(); + for (name, show) in [("DisplayShop", Show::Shop), ("Npc_Hylia_001_Adult", Show::Popup)] { + let mut f: Flow = flow(&mut rf, name); + assert_eq!(patch(&mut f.clone()).unwrap(), 0, "{name} gives no wallet in the game's own files"); + let (a, x): (u16, u16) = crate::start_state::slot(&mut f, POUCH, ADD).unwrap(); + let mut adds: Vec = Vec::new(); + for (actor, n) in [("Item_Wallet_Large", 1), ("Item_Wallet_Medium", -1), ("Item_Wallet_Small", 1), ("Item_Wallet_Medium", 1)] { + let c: Cont = Cont { items: vec![("Num".to_string(), Val::Int(n)), ("Actor".to_string(), Val::Str(actor.to_string()))] }; + adds.push(f.events.len() as u16); + f.events.push(Event { name: format!("T{}", f.events.len()), kind: Kind::Action { next: 0, actor: a, act: x, params: Some(c) } }); + } + assert_eq!(patch(&mut f).unwrap(), 2, "{name}"); + for (k, at) in adds.iter().enumerate() { + let to: u16 = next_of(&f, usize::from(*at)).unwrap(); + assert_eq!(ours(&f, to), k == 0 || k == 3, "{name} add {k}"); + if ours(&f, to) { + check_fill(&f, to, 0, show); + } + } + assert_eq!(patch(&mut f).unwrap(), 0); + } + } + + fn reach(rf: &mut Romfs, cache: &mut BM>, flow: &str, entry: &str, seen: &mut BS<(String, String)>, hits: &mut BS) { + if !seen.insert((flow.to_string(), entry.to_string())) { + return; + } + if !cache.contains_key(flow) { + let f: Option = rf.read_zs(&path_of(flow)).ok().and_then(|r| Bf::parse(&r).ok()).and_then(|b| b.flows.into_iter().find(|f| f.name == flow)); + cache.insert(flow.to_string(), f); + } + let Some(f) = cache.get(flow).cloned().flatten() else { + hits.insert(format!("missing {flow}")); + return; + }; + let Some(e) = f.entries.iter().find(|e| e.name == entry) else { + hits.insert(format!("missing {flow}:{entry}")); + return; + }; + let mut todo: Vec = vec![e.main]; + let mut done: BS = BS::new(); + while let Some(i) = todo.pop() { + if i == NONE || !done.insert(i) { + continue; + } + let ev: &Event = &f.events[usize::from(i)]; + if is_site(flow, &ev.name) { + hits.insert(format!("{flow}:{}", ev.name)); + } + match &ev.kind { + Kind::Action { next, .. } => todo.push(*next), + Kind::Join { next } => todo.push(*next), + Kind::Switch { cases, .. } | Kind::SubSwitch { cases, .. } => todo.extend(cases.iter().map(|c| c.ev)), + Kind::Fork { join, forks } => { + todo.push(*join); + todo.extend(forks.iter().copied()); + } + Kind::Sub { .. } | Kind::Label { .. } => {} + } + if let Kind::Sub { next, flow: to, entry: en, .. } = &ev.kind { + todo.push(*next); + let into: String = if to.is_empty() { flow.to_string() } else { to.clone() }; + reach(rf, cache, &into, en, seen, hits); + } + } + } + + const SWAPS: [(&str, &str); 2] = [("GetInEventInBottle", "GetInEvent_NotTurn_NotNpc"), ("GetInEventInEquipedBottle", "GetInEvent_NotTurn")]; + + #[test] + fn every_flow_location_grant_and_both_chests_reach_a_wallet_fill() { + let Some(p) = real() else { + return; + }; + let mut rf: Romfs = Romfs::open(&p).unwrap(); + let d: crate::flow_locations::Data = crate::flow_locations::Data::load().unwrap(); + let mut cache: BM> = BM::new(); + let mut rows: BM> = BM::new(); + let mut open: Vec = Vec::new(); + let mut made: Vec = Vec::new(); + for l in d.locs.iter().filter(|l| l.supported || l.gated) { + for e in l.edits.iter().filter(|e| e.role == "grant") { + let file: String = e.file.trim_end_matches(".bfevfl").to_string(); + let Ok(raw) = rf.read_zs(&path_of(&file)) else { + made.push(format!("{} ({file})", l.name)); + continue; + }; + let Some(f) = Bf::parse(&raw).unwrap().flows.into_iter().find(|x| x.name == file) else { + made.push(format!("{} ({file})", l.name)); + continue; + }; + let ev: &Event = &f.events[e.index]; + let mut hits: BS = BS::new(); + let via: String = match &ev.kind { + Kind::Sub { flow: to, entry, .. } => { + let into: String = if to.is_empty() { file.clone() } else { to.clone() }; + let entry: &str = SWAPS.iter().find(|s| into == "Dm_SY_GetInEvent" && s.0 == entry).map(|s| s.1).unwrap_or(entry); + reach(&mut rf, &mut cache, &into, entry, &mut BS::new(), &mut hits); + format!("{into}:{entry}") + } + Kind::Action { .. } if is(&f, e.index, POUCH, ADD) => { + hits.insert("literal add".to_string()); + format!("{POUCH}.{ADD}") + } + k => format!("{k:?}"), + }; + let ok: bool = hits.iter().any(|h| !h.starts_with("missing")); + let key: String = format!("{} | {via}", l.cat); + rows.entry(key.clone()).or_default().extend(hits.into_iter()); + if !ok { + open.push(format!("{} ({key})", l.name)); + } + } + } + for path in [crate::progressive::SMALL_FLOW, crate::progressive::BIG_FLOW] { + let name: &str = path.trim_start_matches("Event/EventFlow/").trim_end_matches(".bfevfl.zs"); + let mut hits: BS = BS::new(); + reach(&mut rf, &mut cache, name, "Success_", &mut BS::new(), &mut hits); + assert!(hits.contains("GetPouchContent:Event0"), "{name}: {hits:?}"); + rows.insert(format!("chest | {name}:Success_"), hits); + } + let mut ap: BS = BS::new(); + reach(&mut rf, &mut cache, crate::ap::mailbox::GIVE_FLOW, crate::ap::mailbox::GIVE_ENTRY, &mut BS::new(), &mut ap); + assert!(ap.contains("GetPouchContent:Event0"), "{ap:?}"); + for (k, v) in &rows { + eprintln!("{k} -> {v:?}"); + } + eprintln!("not reached: {open:?}"); + eprintln!("flows the build makes (not in the game files): {made:?}"); + assert!(made.iter().all(|m| m.contains("Cow")), "{made:?}"); + assert_eq!(open, Vec::::new()); + } + + #[test] + fn the_game_files_give_no_wallet_outside_the_give_routines() { + let Some(p) = real() else { + return; + }; + let mut rf: Romfs = Romfs::open(&p).unwrap(); + let dir: PB = p.join("Event").join("EventFlow"); + let mut sites: usize = 0; + let mut files: usize = 0; + for e in std::fs::read_dir(&dir).unwrap() { + let n: String = e.unwrap().file_name().to_string_lossy().to_string(); + let Some(stem) = n.strip_suffix(".bfevfl.zs") else { + continue; + }; + let raw: Vec = rf.read_zs(&path_of(stem)).unwrap(); + let mut b: Bf = Bf::parse(&raw).unwrap(); + let mut here: usize = 0; + for f in b.flows.iter_mut() { + here += patch(f).unwrap(); + } + assert_eq!(here > 0, fixed().contains(&stem), "{stem}"); + if here > 0 { + let back: Bf = Bf::parse(&b.write().unwrap()).unwrap(); + assert_eq!(back.flows, b.flows, "{stem}"); + } + sites += here; + files += 1; + } + assert!(files > 900, "{files}"); + assert_eq!(sites, GIVES.len() + PICKUPS.len()); + } + + #[test] + fn the_hold_is_a_second_and_the_names_cover_each_wallet() { + assert_eq!(HOLD, 60); + let all: Vec = names().unwrap(); + for w in WALLETS { + for x in [w.to_string(), w.replace("Item_", "Pouch_"), w.replace("Item_", "Obj_")] { + assert!(all.contains(&x), "{x}"); + } + } + assert_eq!(crate::progressive::tiers(FAMILY).unwrap().to_vec(), WALLETS.to_vec()); + for t in 0..WALLETS.len() { + for n in crate::pickup_tiers::names(FAMILY, t).unwrap() { + assert!(all.contains(&n), "{n}"); + } + } + assert!(all.iter().all(|n| n.contains("Wallet_Medium") || n.contains("Wallet_Large")), "{all:?}"); + } +} diff --git a/randomizer/core/src/lib.rs b/randomizer/core/src/lib.rs index 19d2163..4c9aa7a 100644 --- a/randomizer/core/src/lib.rs +++ b/randomizer/core/src/lib.rs @@ -47,6 +47,7 @@ pub mod flow_locations; pub mod flow_extra; pub mod flow_sim; pub mod frost; +pub mod full_wallet; pub mod hints; pub mod intro_skip; pub mod gift_keys; diff --git a/randomizer/core/src/settings/list.rs b/randomizer/core/src/settings/list.rs index f1057ab..5795747 100644 --- a/randomizer/core/src/settings/list.rs +++ b/randomizer/core/src/settings/list.rs @@ -774,6 +774,7 @@ pub fn build() -> Schema { s.push(starting_items()); s.push(starting_songs()); s.extend(difficulty()); + s.push(Setting::toggle(crate::full_wallet::NUM, crate::full_wallet::SETTING, "world", "Full Wallets", false).tip("Start with a full wallet. All wallet upgrades come filled with rupees.")); s.sort_by_key(|x| x.num); Schema { version: SCHEMA_VERSION, diff --git a/randomizer/core/src/settings/menu.rs b/randomizer/core/src/settings/menu.rs index afd1f4b..c8d2556 100644 --- a/randomizer/core/src/settings/menu.rs +++ b/randomizer/core/src/settings/menu.rs @@ -366,7 +366,7 @@ const LOGIC: PortSection = port( "Sunlight Arrows", "Light Arrows can be used to light up the sun switches instead of using the Mirror Shield. Item placement logic will respect this option, so it might be required to use this to progress.", ), - gray_check("Full Wallets", "Start with a full wallet. All wallet upgrades come filled with rupees."), + Entry::Plain(crate::full_wallet::SETTING), Entry::View(&PLANT_BEANS), Entry::View(&EPONA), Entry::Plain("night_skulls_suns_song"), diff --git a/randomizer/core/src/slice/edit.rs b/randomizer/core/src/slice/edit.rs index a215d72..9695ed0 100644 --- a/randomizer/core/src/slice/edit.rs +++ b/randomizer/core/src/slice/edit.rs @@ -150,7 +150,9 @@ pub fn build_for_with(seed: &SeedFile, romfs: &P, load: &P, mod_name: &str, host let (seed, cold): (SeedFile, Vec) = crate::frost::prepare(seed)?; let b: Built = crate::slice::seed_packs::build_hooked(&seed, romfs, load, mod_name, host, hook)?; let b: Built = crate::shelf::seed::after(&seed, romfs, load, mod_name, host, b)?; - crate::frost::after(&cold, &seed.seed, romfs, load, mod_name, host, b) + let b: Built = crate::frost::after(&cold, &seed.seed, romfs, load, mod_name, host, b)?; + let on: bool = crate::slice::support::plan(&seed.settings()?.settings)?.full_wallet; + crate::full_wallet::after(on, romfs, load, mod_name, host, b) } pub type Hook<'a> = dyn FnMut(&mut Romfs, &P, &mut Vec) -> Res<()> + 'a; diff --git a/randomizer/core/src/slice/stages.rs b/randomizer/core/src/slice/stages.rs index 73f30ec..46910c9 100644 --- a/randomizer/core/src/slice/stages.rs +++ b/randomizer/core/src/slice/stages.rs @@ -217,6 +217,9 @@ fn start_items(rf: &mut Romfs, c: &Ctx, more: &mut Vec) -> Res<()> { if c.p.first_pickups { crate::start_items::stage::stage_list(rf, more, &crate::first_pickups::bag_sets(&c.p.kit))?; } + if c.p.full_wallet { + crate::start_items::stage::stage_list(rf, more, &crate::full_wallet::start_sets(&c.p.kit))?; + } crate::start_items::stage::stage(rf, more, &c.p.kit) } diff --git a/randomizer/core/src/slice/support.rs b/randomizer/core/src/slice/support.rs index 697e5c8..275ae74 100644 --- a/randomizer/core/src/slice/support.rs +++ b/randomizer/core/src/slice/support.rs @@ -104,6 +104,7 @@ pub struct Plan { pub short: bool, pub first_pickups: bool, pub area_intros: bool, + pub full_wallet: bool, pub trap_chests: bool, } @@ -339,6 +340,10 @@ pub fn plan(s: &Settings) -> Res { bad.push(e.to_string()); false }); + let full_wallet: bool = s.flag(crate::full_wallet::SETTING).unwrap_or_else(|e| { + bad.push(e.to_string()); + false + }); if lacs.is_some() && (!rewards.on || matches!(chests, Mode::Off | Mode::Overworld)) { bad.push(crate::lacs_key::NEEDS.to_string()); } @@ -457,12 +462,15 @@ pub fn plan(s: &Settings) -> Res { if area_intros { notes.push(crate::area_intros::NOTE.to_string()); } + if full_wallet { + notes.push(crate::full_wallet::NOTE.to_string()); + } if lacs.is_some() { notes.push(crate::lacs_key::NOTE.to_string()); notes.push(crate::medal_gate::LACS_NOTE.to_string()); } let own_strict: Vec<(&'static str, &'static str)> = if matches!(chests, Mode::Dungeons | Mode::All) { OWN_STRICT.to_vec() } else { Vec::new() }; - Ok(Plan { own_strict, chests, keys, rewards, hearts, world, songs, harp_colors, hints, fast, chest_sizes, intro, start, gates, excluded, notes, drops, skulls, remake, chus, qol, treasure, day_skulls, kit, open, npc, shops, bridge, debug_names, silver, lacs, rainbow, trials, door, nocturne, prelude, short, first_pickups, area_intros, story, endings, ocarinas, kokiri_sword, difficulty, trap_chests }) + Ok(Plan { own_strict, chests, keys, rewards, hearts, world, songs, harp_colors, hints, fast, chest_sizes, intro, start, gates, excluded, notes, drops, skulls, remake, chus, qol, treasure, day_skulls, kit, open, npc, shops, bridge, debug_names, silver, lacs, rainbow, trials, door, nocturne, prelude, short, first_pickups, area_intros, full_wallet, story, endings, ocarinas, kokiri_sword, difficulty, trap_chests }) } #[cfg(test)]