This repository has been archived on 2026-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
xiv_csolver/src/lib.rs

377 lines
12 KiB
Rust

//! # Solver bin for FFXIV Crafting in Rust
//!
//! This project strives to provide a fast, accurate and reliable way to craft
//! in FFXIV and obtain HQ results
//!
//! This project also provides bindings for python to allow hooking it to any form of better GUI/CLI
//!
//! The project is (for now) self-hosted and hosted on [Github](https://github.com/Dandaedre/xiv_csolver)
//!
//! ## Getting started
//!
//! ### Development
//!
//! #### Rust
//! To set up the whole development environment, it is recommended to use cargo-make
//!
//! Doing so will allow you custom commands to
//!
//! ```sh
//! cargo make rebuild
//! ```
//! To rebuild the whole project
//!
//! ```sh
//! cargo make release
//! ```
//! To generate the release files, bins and libs, package them and make them ready to release
//!
//! #### Python
//!
//! It is really recommended to set up a venv to handle the python development
//!
//! All bindings and the explanation of how to use them can be found in the [`io`](crate::io) module.
//!
//! ### Features
//!
//! Use these by running `cargo run/build --features <name of feature>`
//!
//! - ``no_python`` : Don't build the lib & python bindings (WIP, not fully implemented)
//! - ``fast`` : Don't allow some under-optimisation to run (saving global run-time)
//! - ``verbose`` : Allow for more debug messages (note that -v(vv) must still be passed to activate them)
//!
//! ## Regenerating the API Bindings
//!
//! The python bindings are generated using maturin
//!
//! ```sh
//! cargo install maturin
//! maturin develop
//! ```
//! ## Solving and bias
//!
//! The solving algorithm used has a very strong bias toward a particular method:
//!
//! - **Step 1** : Try to push the progression of the craft one step before being finishable
//! - **Step 2** : Try to increase the quality as much as possible.
//! - **Step 3** : Finish the craft
//!
//! To account for a lot of possibilities, step one generates all methods to try and finish the craft
//! and step two tests all methods to up the quality until all crafting points are depleted
//! Doing so ensures by brute force that all permutations are thoroughly tested.
//!
//! While this method works and is very likely the best method for the current patch,
//! a rework of the crafting actions could very well make this method subpar or at least
//! on par with another method.
//!
//! In layman's term, only the groundwork opening is tested and no delicate synthesis spam is tested.
//! However, for now, these two crafting techniques remain subpar and discarding them comes at no
//! loss for the crafter.
//!
//! Others algorithm exists and could very well cover all other basis at the expense of runtime.
//! We have chosen not to implement them as they (for now) would be very unlikely to uncover anything
//! meaningful.
//!
//! ### Leveling
//!
//! We have (for now at least) no intention to release a leveling feature to the crafting solver
//! However, selecting some spells and buffs and have them disabled could be added as a feature
//! hence making a "leveling" option
//!
//! If it is ever released, this feature is likely to be heavily reliant on the python GUI.
//!
//!
//! ## Contributing
//!
//! All form of contributions are welcome !
//!
//! While this project was originally a collab and didn't strive to encompass every need
//! of a modern FFXIV crafter, more polish and finish is always welcome.
//!
//! ## License
//!
//! The original code was released without a license by [RikaKagurasaka](https://github.com/RikaKagurasaka/xiv_craft_solver)
//!
//! All this fork's code is released under [Apache-2.0](http://www.apache.org/licenses/LICENSE-2.0)
#![warn(missing_docs)]
use crate::io::SolverResult;
use crate::{
craft::Craft,
io::Parameters,
specs::{Recipe, Stats},
};
use std::sync::{Arc, Mutex};
use threadpool::ThreadPool;
pub mod action;
pub mod craft;
pub mod io;
mod solver;
pub mod specs;
/// Solve the craft with given arguments, this functions calls threads and must own it's values
pub fn solve_craft<'a>(
recipe: Recipe,
stats: Stats,
params: Parameters,
) -> Option<Vec<SolverResult>> {
// Load the craft with given arguments
let craft = Craft::new(recipe, stats, params);
// Start a threadpool
let pool = ThreadPool::new(params.threads);
#[cfg(feature = "verbose")]
if params.verbose > 0 {
println!("[P1] Starting phase 1...");
}
let phase1_routes = solver::generate_routes_phase1(craft);
let nb_p1 = phase1_routes.len();
#[cfg(feature = "verbose")]
if params.verbose > 0 {
println!(
"[P1] Found {} routes, testing them all...",
phase1_routes.len()
);
if params.verbose > 1 {
for r in &phase1_routes {
println!(
"[P1] {:?} p:{}% q:{}% c:{} d:{}",
r.actions,
r.progression,
r.quality * 100 / r.recipe.quality,
r.cp,
r.durability
);
}
}
}
// Core algorithm, fill all found routes with the best route (doesn't branch, just replace)
let arc_phase2_routes = Arc::new(Mutex::new(Vec::<Craft>::new()));
for route in phase1_routes {
let _phase2_routes = Arc::clone(&arc_phase2_routes);
pool.execute(move || {
if let Some(mut _route) = solver::generate_routes_phase2(route) {
let mut shared = _phase2_routes.lock().unwrap();
shared.append(&mut _route);
};
});
}
pool.join();
let phase2_routes = arc_phase2_routes.lock().unwrap();
let nb_p2 = phase2_routes.len();
// Drop on empty results
if phase2_routes.len() == 0 {
return None;
}
// Print the results if verbose
#[cfg(feature = "verbose")]
if params.verbose > 0 {
println!("[P2] Found {} solutions, sorting", phase2_routes.len());
if params.verbose > 1 {
for r in phase2_routes.iter() {
println!(
"[P2] {:?} p:{}% q:{}% d:{}",
r.actions,
r.progression * 100 / r.recipe.progress,
r.quality * 100 / r.recipe.quality,
r.durability
);
}
}
}
// Prune the results for analysis
// let mut valid_routes : Vec<Craft> = vec![];
let mut valid_solutions: Vec<SolverResult> = vec![];
for route in phase2_routes.iter() {
if route.quality >= route.recipe.quality {
// valid_routes.push(route.clone());
valid_solutions.push(SolverResult::from_craft(route, nb_p1, nb_p2, true));
}
}
// If no craft can make it to 100% HQ, fallback to base results
if valid_solutions.len() == 0 {
for route in phase2_routes.iter() {
valid_solutions.push(SolverResult::from_craft(route, nb_p1, nb_p2, false));
}
}
Some(valid_solutions)
}
/// Load the config from args and make a craft from it
pub fn load_from_config<'a>(
recipe_name: &str,
file_name: &str,
character_name: &str,
) -> (Recipe, Stats) {
//read craft.toml
let config: toml::Value = toml::from_str(
&std::fs::read_to_string(file_name).expect(&format!("Can't open {}", file_name)),
)
.unwrap();
let recp = match config.get(recipe_name) {
Some(r) => r,
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 '{}'",
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 '{}'",
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 '{}'",
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 '{}'",
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 '{}'",
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 '{}'",
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 '{}'",
recipe_name, file_name
))
.as_integer()
.expect("Can't convert quality_modifier as an integer")
as u32,
};
let cfg = match config.get(character_name) {
Some(c) => c,
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 '{}'",
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 '{}'",
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 '{}'",
character_name, file_name
))
.as_integer()
.expect("Can't convert max_cp as an integer") as u32,
};
(recipe, stats)
}
/// Print all routes in the vect, verbose
pub fn print_routes<'a>(routes: &Option<Vec<SolverResult>>) {
match routes {
Some(r) => {
println!("Showing {} routes", r.len());
for c in r.iter() {
println!(
"\n[{}¤][{}@][{}#] {:?} ",
c.quality, c.durability, c.steps, c.actions
)
}
}
None => println!("No routes to print"),
}
}
/// Find the route with the least amount of steps
pub fn find_fast_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => {
if _routes.len() > 0 {
_routes.iter().min_by_key(|key| key.steps)
} else {
None
}
}
None => None,
}
}
/// Find the route with the maximum amount of quality
pub fn find_quality_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => _routes.iter().max_by_key(|key| key.quality),
None => None,
}
}
/// Find the route with the maximum of durability left
pub fn find_safe_route(routes: &Option<Vec<SolverResult>>) -> Option<&SolverResult> {
match routes {
Some(_routes) => {
if _routes.len() > 0 {
_routes.iter().max_by_key(|key| key.durability)
} else {
None
}
}
None => None,
}
}