Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions randomizer/core/src/full_wallet.rs
Original file line number Diff line number Diff line change
@@ -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<i32> {
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<Set> {
vec![Set::Int(COUNT.to_string(), start_cap(k))]
}

fn flow_files(base: &P) -> Res<Vec<String>> {
let mut out: Vec<String> = 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<Built> {
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<String> = Vec::new();
for path in flow_files(&base)? {
let modded: bool = local(&base, &path).is_file();
let first: Vec<u8> = 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<u8> = 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::<Vec<&str>>());
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}");
}
}
}
}
78 changes: 78 additions & 0 deletions randomizer/core/src/full_wallet/fill.rs
Original file line number Diff line number Diff line change
@@ -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<u16> {
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<u16> {
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<u16> {
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<u16> {
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<u16> {
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)
}
Loading
Loading