diff --git a/src/craft.rs b/src/craft.rs index 80a65d1..c4055f3 100644 --- a/src/craft.rs +++ b/src/craft.rs @@ -1,7 +1,7 @@ use std::fmt::{Debug, Formatter,Display}; use crate::action::Action; use crate::specs::{Buff, BuffState, Recipe, Stats, Success}; - +use crate::libs::Parameters; #[derive(Clone)] @@ -16,11 +16,11 @@ pub struct Craft<'a> { pub cp: i32, pub success: Success, pub actions: Vec<&'a Action>, - pub depth: u32, + pub args: Parameters, } impl<'a> Craft<'a> { - pub fn new(recipe: Recipe, stats: Stats, depth: u32) -> Craft<'a> { + pub fn new(recipe: Recipe, stats: Stats, params: Parameters) -> Craft<'a> { Self { recipe, stats, @@ -32,7 +32,7 @@ impl<'a> Craft<'a> { cp: stats.max_cp as i32, success: Success::Pending, actions: Vec::new(), - depth: depth, + args: params, } } diff --git a/src/libs.rs b/src/libs.rs index 67c20cf..256ffd4 100644 --- a/src/libs.rs +++ b/src/libs.rs @@ -10,14 +10,14 @@ use clap::Parser; // use crate::Recipe; // use crate::Craft; -#[derive(Debug, Clone)] -pub struct Config<'a> { - pub craft: Craft<'a>, - // pub recipe: solver::Recipe, - pub depth: usize, +#[derive(Debug, Clone, Copy)] +pub struct Parameters { + pub threads: usize, + pub verbose: u8, + pub depth: u32, } -#[derive(Parser, Debug)] +#[derive(Parser, Debug, Clone)] #[command(author, version, about, long_about = None)] pub struct Args { /// Name of the receipe @@ -46,9 +46,20 @@ pub struct Args { } /// Solve the craft with given arguments -pub fn solve_craft<'a>(craft: Craft<'static>, args: &'a Args){ -// Start timer +pub fn solve_craft<'a>() -> Option>{ + let args = Args::parse(); + let params = Parameters{ + depth: args.depth, + threads: args.threads, + verbose: args.verbose, + }; + + // Load the craft with given arguments + let craft = load_config(&args.recipe_name, &args.file_name, &args.character_name,params); + + // Start timer let now = Instant::now(); + let craft = craft.clone(); // Start a threadpool let pool = ThreadPool::new(args.threads); @@ -57,7 +68,7 @@ pub fn solve_craft<'a>(craft: Craft<'static>, args: &'a Args){ println!("Solving...\n"); println!("[P1] Starting phase 1..."); } - let phase1_routes = generate_routes_phase1(craft); + let phase1_routes = generate_routes_phase1(craft.clone()); if args.verbose>0{ println!("[P1] Found {} routes, testing them all...",phase1_routes.len()); @@ -70,7 +81,6 @@ pub fn solve_craft<'a>(craft: Craft<'static>, args: &'a Args){ r.cp, r.durability, ); - // println!("[P1] {}",r); }; } } @@ -106,12 +116,11 @@ pub fn solve_craft<'a>(craft: Craft<'static>, args: &'a Args){ } } - let top_route = match phase2_routes.iter().max_by_key(|route| route.quality) { Some(top) => top, None => { println!("[P2] No route could finish the craft.\n[P2] Runtime {}ms. Now exiting...",now.elapsed().as_millis()); - return + return None; }, }; @@ -145,72 +154,74 @@ pub fn solve_craft<'a>(craft: Craft<'static>, args: &'a Args){ println!("Press enter to exit..."); let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); + // Some(top_route.clone()) + None } -/// Load the config -pub fn load_config<'a>(args: &Args ) -> Craft<'a> { +/// Load the config from args and make a craft from it +pub fn load_config<'a>(recipe_name: &str, file_name: &str, character_name: &str, params: Parameters) -> Craft<'a> { //read craft.toml let config: toml::Value = toml::from_str( - &std::fs::read_to_string(&args.file_name) - .expect(&format!("Can't open {}",&args.file_name)) + &std::fs::read_to_string(file_name) + .expect(&format!("Can't open {}",file_name)) ).unwrap(); - let recp = match config.get(&args.recipe_name){ + let recp = match config.get(recipe_name){ Some(r) => r, - None => panic!("Can't find value '{}' in '{}'",&args.recipe_name, &args.file_name) + None => panic!("Can't find value '{}' in '{}'",recipe_name, file_name) }; // Load receipe values let recipe = Recipe { durability: recp .get("durability").expect(&format!("Can't find 'durability' in recipe '{}' on file '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert durability as an integer") as u32, progress: recp .get("progress").expect(&format!("Can't find 'progress' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert progress as an integer") as u32, quality: recp .get("quality").expect(&format!("Can't find 'quality' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert quality as an integer") as u32, progress_divider: recp .get("progress_divider").expect(&format!("Can't find 'progress_divider' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert progress_divider as an integer") as u32, quality_divider: recp .get("quality_divider").expect(&format!("Can't find 'quality_divider' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert quality_divider as an integer") as u32, progress_modifier: recp .get("progress_modifier").expect(&format!("Can't find 'progress_modifier' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert progress_modifier as an integer") as u32, quality_modifier: recp .get("quality_modifier").expect(&format!("Can't find 'quality_modifier' in recipe '{}' on '{}'", - &args.recipe_name,&args.file_name)) + recipe_name,file_name)) .as_integer().expect("Can't convert quality_modifier as an integer") as u32, }; - let cfg = match config.get(&args.character_name){ + let cfg = match config.get(character_name){ Some(c) => c, - None => panic!("Can't find '{}' in file '{}'",&args.character_name,&args.file_name), + None => panic!("Can't find '{}' in file '{}'",character_name,file_name), }; let stats = Stats { craftsmanship: cfg .get("craftsmanship").expect(&format!("Can't find 'craftsmanship' in character '{}' on file '{}'", - &args.character_name,&args.file_name)) + character_name,file_name)) .as_integer().expect("Can't convert craftsmanship as an integer") as u32, control: cfg .get("control").expect(&format!("Can't find 'control' in character '{}' on file '{}'", - &args.character_name,&args.file_name)) + character_name,file_name)) .as_integer().expect("Can't convert control as an integer") as u32, max_cp: cfg .get("max_cp").expect(&format!("Can't find 'max_cp' in character '{}' on file '{}'", - &args.character_name,&args.file_name)) + character_name,file_name)) .as_integer().expect("Can't convert max_cp as an integer") as u32, }; - let craft = Craft::new(recipe, stats, args.depth); + let craft = Craft::new(recipe, stats, params); craft } diff --git a/src/main.rs b/src/main.rs index 9a78cd8..9edb99c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,8 @@ pub mod libs; struct CustomError(String); fn main() { - // Get args - let args = Args::parse(); - - // Load the craft with given arguments - let craft = libs::load_config(&args); - libs::solve_craft(craft,&args); + libs::solve_craft(); () } diff --git a/src/solver.rs b/src/solver.rs index 9d31f73..69774d4 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -81,7 +81,7 @@ pub fn generate_routes_phase1<'a>(craft: Craft<'a>) -> Vec> { // A higher value will yield more initial guesses with minimal benefits // A lover value will yield less initial guesses and might spent too much CP to get to the required progression // TODO: Pass a flag to get more attempts - if craft.step_count < craft.depth { queue.push(craft); } + if craft.step_count < craft.args.depth { queue.push(craft); } } } routes