Skip to content

Commit

Permalink
Store solved rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
KonaeAkira committed Dec 30, 2024
1 parent f6285f8 commit cb8d519
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 90 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ image = { version = "0.25", default-features = false, features = ["webp"] }
serde = { version = "1.0.215", features = ["derive"] }
ron = "0.8"
log = "0.4"
rand = "0.8.5"

# workaround for https://github.com/emilk/egui/issues/5491
# remove after updating to egui 0.30.1
Expand Down
45 changes: 42 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ pub struct MacroSolverApp {
crafter_config: CrafterConfig,
solver_config: SolverConfig,
macro_view_config: MacroViewConfig,
saved_rotations_data: SavedRotationsData,

stats_edit_window_open: bool,
saved_rotations_window_open: bool,

actions: Vec<Action>,
solver_pending: bool,
solver_interrupt_pending: bool,
Expand Down Expand Up @@ -132,8 +135,11 @@ impl MacroSolverApp {
crafter_config: load(cc, "CRAFTER_CONFIG", Default::default()),
solver_config: load(cc, "SOLVER_CONFIG", Default::default()),
macro_view_config: load(cc, "MACRO_VIEW_CONFIG", Default::default()),
saved_rotations_data: load(cc, "SAVED_ROTATIONS", Default::default()),

stats_edit_window_open: false,
saved_rotations_window_open: false,

actions: Vec::new(),
solver_pending: false,
solver_interrupt_pending: false,
Expand Down Expand Up @@ -361,6 +367,23 @@ impl eframe::App for MacroSolverApp {
ui.style_mut().spacing.item_spacing = egui::vec2(8.0, 3.0);
ui.add(StatsEdit::new(self.locale, &mut self.crafter_config));
});

egui::Window::new(
egui::RichText::new("Saved rotations")
.strong()
.text_style(TextStyle::Body),
)
.open(&mut self.saved_rotations_window_open)
.collapsible(false)
.default_size((400.0, 600.0))
.show(ctx, |ui| {
ui.style_mut().spacing.item_spacing = egui::vec2(8.0, 3.0);
ui.add(SavedRotationsWidget::new(
self.locale,
&mut self.saved_rotations_data,
&mut self.actions,
));
});
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
Expand All @@ -371,6 +394,7 @@ impl eframe::App for MacroSolverApp {
eframe::set_value(storage, "CRAFTER_CONFIG", &self.crafter_config);
eframe::set_value(storage, "SOLVER_CONFIG", &self.solver_config);
eframe::set_value(storage, "MACRO_VIEW_CONFIG", &self.macro_view_config);
eframe::set_value(storage, "SAVED_ROTATIONS", &self.saved_rotations_data);
}

fn auto_save_interval(&self) -> std::time::Duration {
Expand All @@ -387,6 +411,15 @@ impl MacroSolverApp {
self.actions = actions;
self.duration = Some(self.start_time.unwrap().elapsed());
self.solver_pending = false;
self.saved_rotations_data.add_solved_rotation(Rotation::new(
game_data::get_item_name(self.recipe_config.recipe.item_id, false, self.locale),
self.actions.clone(),
&self.recipe_config.recipe,
&self.selected_food,
&self.selected_potion,
&self.crafter_config,
&self.solver_config,
));
}
}
}
Expand Down Expand Up @@ -473,10 +506,16 @@ impl MacroSolverApp {
ui.vertical(|ui| {
self.draw_configuration_widget(ui);
ui.separator();
ui.vertical_centered_justified(|ui| {
if ui.button("Solve").clicked() {
self.on_solve_button_clicked(ui.ctx());
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
if ui.button("History").clicked() {
self.saved_rotations_window_open = true;
}
ui.add_space(-5.0);
ui.vertical_centered_justified(|ui| {
if ui.button("Solve").clicked() {
self.on_solve_button_clicked(ui.ctx());
}
});
});
ui.with_layout(Layout::right_to_left(Align::TOP), |ui| {
ui.label(format!(
Expand Down
8 changes: 6 additions & 2 deletions src/widgets/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl egui::Widget for SolutionAnalysis<'_> {
ui.set_width(ui.available_width());
ui.style_mut().spacing.item_spacing = egui::vec2(8.0, 3.0);

let mut collapsed = false;
let mut collapsed = true;
let distribution = simulator::quality_probability_distribution(
self.settings,
self.actions,
Expand All @@ -49,7 +49,11 @@ impl egui::Widget for SolutionAnalysis<'_> {

ui.vertical(|ui| {
ui.horizontal(|ui| {
collapsed = util::collapse_button(ui, egui::Id::new("analysis_collapsed"));
util::collapse_persisted(
ui,
egui::Id::new("analysis_collapsed"),
&mut collapsed,
);
ui.label(egui::RichText::new("Analysis").strong());
if self.is_expert {
ui.label("N/A (Expert recipes not supported)");
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/food_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Widget for FoodSelect<'_> {
let mut collapsed = false;

ui.horizontal(|ui| {
collapsed = util::collapse_button(ui, Id::new("FOOD_SEARCH_COLLAPSED"));
util::collapse_persisted(ui, Id::new("FOOD_SEARCH_COLLAPSED"), &mut collapsed);
ui.label(egui::RichText::new("Food").strong());
match self.selected_consumable {
None => ui.label("None"),
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/macro_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Widget for MacroView<'_> {
.map(|action| action.time_cost())
.sum::<i16>();
ui.label(format!(
"{} steps | {} seconds",
"{} steps, {} seconds",
self.actions.len(),
duration
));
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ pub use item_name_label::ItemNameLabel;
mod analysis;
pub use analysis::SolutionAnalysis;

mod saved_rotations;
pub use saved_rotations::{Rotation, SavedRotationsData, SavedRotationsWidget};

mod util;
6 changes: 5 additions & 1 deletion src/widgets/potion_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ impl Widget for PotionSelect<'_> {
ui.vertical(|ui| {
let mut collapsed = false;
ui.horizontal(|ui| {
collapsed = util::collapse_button(ui, Id::new("POTION_SEARCH_COLLAPSED"));
util::collapse_persisted(
ui,
Id::new("POTION_SEARCH_COLLAPSED"),
&mut collapsed,
);
ui.label(egui::RichText::new("Potion").strong());
match self.selected_consumable {
None => ui.label("None"),
Expand Down
6 changes: 5 additions & 1 deletion src/widgets/recipe_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ impl Widget for RecipeSelect<'_> {
let mut collapsed = false;

ui.horizontal(|ui| {
collapsed = util::collapse_button(ui, Id::new("RECIPE_SEARCH_COLLAPSED"));
util::collapse_persisted(
ui,
Id::new("RECIPE_SEARCH_COLLAPSED"),
&mut collapsed,
);
ui.label(egui::RichText::new("Recipe").strong());
ui.add(ItemNameLabel::new(
self.recipe_config.recipe.item_id,
Expand Down
Loading

0 comments on commit cb8d519

Please sign in to comment.