Skip to content

Commit

Permalink
Add reverse board scan.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm committed Oct 25, 2020
1 parent a758066 commit d604dde
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 23 deletions.
33 changes: 33 additions & 0 deletions src/board.rs
Expand Up @@ -161,6 +161,7 @@ pub fn update_board(
board_id,
Robots::new(board, all_robots),
RobotId::Global,
false,
);
if change.is_some() {
return change;
Expand Down Expand Up @@ -194,6 +195,7 @@ pub fn update_board(
board_id,
robots,
RobotId::from(board.level_at(&coord).2),
false,
);
if change.is_some() {
return change;
Expand Down Expand Up @@ -408,6 +410,37 @@ pub fn update_board(
}
}

debug!("updating board in reverse: {},{}", board.width, board.height);
for y in (0..board.height).rev() {
for x in (0..board.width).rev() {
let coord = Coordinate(x as u16, y as u16);
match board.thing_at(&coord) {
Thing::Robot | Thing::RobotPushable => {
let robots = Robots::new(board, all_robots);
debug!("running robot at {},{}", x, y);
let change = update_robot(
state,
audio,
key,
world_path,
counters,
boards,
board,
board_id,
robots,
RobotId::from(board.level_at(&coord).2),
true,
);
if change.is_some() {
return change;
}
}

_ => (),
}
}
}

state.message_color += 1;
if state.message_color > 0x0F {
state.message_color = 0x01;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -2033,6 +2033,8 @@ impl Robot {}
pub enum RunStatus {
NotRun,
FinishedRunning,
FinishedWithoutRunning,
MustRunOnReverse,
}

impl Default for RunStatus {
Expand Down
64 changes: 44 additions & 20 deletions src/robot.rs
Expand Up @@ -335,39 +335,49 @@ pub fn update_robot(
board_id: usize,
mut robots: Robots,
robot_id: RobotId,
reverse_scan: bool,
) -> Option<GameStateChange> {
let robot = robots.get_mut(robot_id);
if !robot.alive || robot.status == RunStatus::FinishedRunning {
if !robot.alive ||
matches!(robot.status, RunStatus::FinishedRunning | RunStatus::FinishedWithoutRunning) ||
(robot.status == RunStatus::MustRunOnReverse && !reverse_scan)
{
debug!("alive: {}, status: {:?}", robot.alive, robot.status);
return None;
}

robot.cycle_count += 1;
if robot.cycle_count < robot.cycle {
debug!(
"delaying {:?} (cycle {}/{})",
robot.name, robot.cycle_count, robot.cycle
);
return None;
if !reverse_scan {
robot.cycle_count += 1;
if robot.cycle_count < robot.cycle {
debug!(
"delaying {:?} (cycle {}/{})",
robot.name, robot.cycle_count, robot.cycle
);
robot.status = RunStatus::FinishedWithoutRunning;
return None;
}

if let Some(dir) = robot.walk {
move_robot(
&mut robots,
robot_id,
board,
dir,
&mut *state.update_done,
false,
);
}
}

let robot = robots.get_mut(robot_id);
robot.cycle_count = 0;

debug!("executing {:?}", robot.name);

if let Some(dir) = robot.walk {
move_robot(
&mut robots,
robot_id,
board,
dir,
&mut *state.update_done,
false,
);
}

let mut lines_run = 0;
let mut mode = Relative::None;
let mut message_box_lines = vec![];
let mut first_cmd = true;

const CYCLES: u8 = 40;
let state_change = loop {
Expand Down Expand Up @@ -448,6 +458,9 @@ pub fn update_robot(
}
CommandResult::NoAdvance => {
if cmd.is_cycle_ending() {
if matches!(cmd, Command::End | Command::Wait(..)) && first_cmd {
robots.get_mut(robot_id).status = RunStatus::FinishedWithoutRunning;
}
break None;
}
}
Expand All @@ -456,9 +469,14 @@ pub fn update_robot(
break None;
}
}

first_cmd = false;
};

robots.get_mut(robot_id).status = RunStatus::FinishedRunning;
let robot = robots.get_mut(robot_id);
if robot.status == RunStatus::NotRun {
robot.status = RunStatus::FinishedRunning;
}

state_change
}
Expand Down Expand Up @@ -1873,6 +1891,9 @@ pub fn jump_robot_to_label<S: Into<EvaluatedByteString>>(robot: &mut Robot, labe
debug!("jumping to previous stack position");
robot.current_loc = 0;
robot.current_line = line;
if robot.status == RunStatus::FinishedWithoutRunning {
robot.status = RunStatus::MustRunOnReverse;
}
return true;
}
}
Expand All @@ -1888,6 +1909,9 @@ pub fn jump_robot_to_label<S: Into<EvaluatedByteString>>(robot: &mut Robot, labe
}
robot.current_loc = 0;
robot.current_line = pos as u16 + 1;
if robot.status == RunStatus::FinishedWithoutRunning {
robot.status = RunStatus::MustRunOnReverse;
}
true
} else {
false
Expand Down
5 changes: 2 additions & 3 deletions src/robotic.rs
Expand Up @@ -791,16 +791,15 @@ impl Command {
| Command::CopyRobotNamed(..)
| Command::CopyRobotXY(..)
| Command::CopyRobotDir(..)
| Command::Cycle(..)
| Command::Slash(..)
| Command::Teleport(..)
| Command::MoveAll(..)
| Command::Copy(..)
| Command::CopyDir(..)
| Command::CopyBlock(..)
| Command::ColorFadeIn
| Command::ColorFadeOut
| Command::SwapWorld(..)
| Command::CopyOverlayBlock(..) => true,
| Command::SwapWorld(..) => true,
_ => false,
}
}
Expand Down

0 comments on commit d604dde

Please sign in to comment.