Skip to content

Commit

Permalink
fix: fix auto solve causes previous actions loss
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Jun 16, 2024
1 parent cb6e9bd commit 390e162
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use nalgebra::Vector2;
use serde::{Deserialize, Serialize};
use soukoban::{Level, Map};

use crate::board::Board;
use crate::database;
use crate::solve::solver::*;
use crate::utils::PushState;
Expand Down Expand Up @@ -80,8 +81,8 @@ pub enum AutoMoveState {
#[derive(Resource)]
pub struct SolverState {
pub solver: Mutex<Solver>,
pub level: Level,
pub stopwatch: Stopwatch,
pub origin_board: Board,
}

impl Default for SolverState {
Expand All @@ -92,8 +93,10 @@ impl Default for SolverState {
Strategy::default(),
LowerBoundMethod::default(),
)),
level: Level::from_map(Map::with_dimensions(Vector2::new(0, 0))),
stopwatch: Stopwatch::new(),
origin_board: Board::with_level(Level::from_map(Map::with_dimensions(Vector2::new(
0, 0,
)))),
}
}
}
12 changes: 6 additions & 6 deletions src/systems/auto_solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub fn load_solver(
let board = &board.single().board;
let SolverState {
solver,
level,
stopwatch,
origin_board,
} = &mut *solver_state;
*level = board.level.clone();
*origin_board = board.clone();
let solver = solver.get_mut().unwrap();
*solver = Solver::new(
level.clone(),
origin_board.level.clone(),
config.solver.strategy,
config.solver.lower_bound_method,
);
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn despawn_lowerbound_marks(
/// Resets the board to the state before automatic solution
pub fn reset_board(mut board: Query<&mut Board>, solver_state: Res<SolverState>) {
let board = &mut board.single_mut().board;
*board = crate::board::Board::with_level(solver_state.level.clone());
*board = solver_state.origin_board.clone();
}

pub fn update_solver(
Expand All @@ -94,11 +94,11 @@ pub fn update_solver(
let board = &mut board.single_mut().board;
let SolverState {
solver,
level,
stopwatch,
origin_board,
} = &mut *solver_state;

*board = crate::board::Board::with_level(level.clone());
*board = crate::board::Board::with_level(origin_board.level.clone());

let solver = solver.get_mut().unwrap();
let timeout = Duration::from_millis(50);
Expand Down

0 comments on commit 390e162

Please sign in to comment.