Use visualization for all printing.

This commit is contained in:
hal8174 2024-09-19 16:03:52 +02:00
parent 7d412ce610
commit 1c44d7aec1
10 changed files with 216 additions and 299 deletions

View file

@ -1,14 +1,10 @@
use super::{
common::{print_map, PathField},
Problem,
};
use crate::prelude::*;
use super::{common::PathField, Problem};
use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map};
use crate::{common::visualize::Visualize, prelude::*};
use std::{
ops::RangeInclusive,
time::{Duration, Instant},
};
use termcolor::ColorSpec;
#[derive(Default)]
struct Field {
@ -354,7 +350,7 @@ impl ConflictAvoidance {
}
// dbg!(&candidates);
while timeout.is_some_and(|t| t < Instant::now()) {
while timeout.is_none_or(|t| t > Instant::now()) {
candidates.sort_by_key(|c| -c.area());
// dbg!(&candidates);
let c = match candidates.pop() {
@ -499,53 +495,14 @@ impl ConflictAvoidance {
}
true
}
}
pub fn print(&self) {
let mut m: Map<Option<(usize, &str)>> = Map::new(self.map.width, self.map.height);
for (i, problem) in self.belts.iter().enumerate() {
for p in problem {
match p {
PathField::Belt { pos, dir } => match dir {
Direction::Up => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Right => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Down => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Left => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
},
PathField::Underground { pos, dir, len } => {
match dir {
Direction::Up => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Right => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
Direction::Down => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
Direction::Left => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
};
let end_pos = pos.in_direction(dir, *len as PositionType);
match dir {
Direction::Up => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Right => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Down => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Left => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
};
}
}
}
let last_pos = problem.last().unwrap().pos();
m.set(last_pos.x as usize, last_pos.y as usize, Some((i, "T")));
}
impl Visualize for ConflictAvoidance {
fn visualize(&self) -> crate::common::visualize::Visualization {
let mut v = crate::common::visualize::Visualization::new(Position::new(
self.map.width as i32,
self.map.height as i32,
));
// create conflict map
let mut conflicts: Map<usize> = Map::new(self.map.width, self.map.height);
@ -554,6 +511,13 @@ impl ConflictAvoidance {
for y in 0..self.map.height {
if self.map.get(x, y).blocked {
*conflicts.get_mut(x, y) += 1;
v.add_symbol(
Position::new(x as i32, y as i32),
crate::common::visualize::Symbol::Block,
Some(crate::common::visualize::Color::white()),
None,
);
}
}
}
@ -572,31 +536,62 @@ impl ConflictAvoidance {
}
}
}
// Print body
let _ = print_map(self.map.width as i32, self.map.height as i32, |x, y| {
let mut color = ColorSpec::new();
if let Some((xrange, yrange)) = &self.range {
if xrange.contains(&x) && yrange.contains(&y) {
color.set_bg(Some(termcolor::Color::Rgb(96, 96, 0)));
for (i, path) in self.belts.iter().enumerate() {
for p in path {
match p {
PathField::Belt { pos, dir } => {
v.add_symbol(
*pos,
crate::common::visualize::Symbol::Arrow(*dir),
Some(crate::common::visualize::Color::index(i)),
None,
);
}
PathField::Underground { pos, dir, len } => {
v.add_symbol(
*pos,
crate::common::visualize::Symbol::ArrowEnter(*dir),
Some(crate::common::visualize::Color::index(i)),
None,
);
v.add_symbol(
pos.in_direction(dir, *len as i32),
crate::common::visualize::Symbol::ArrowExit(*dir),
Some(crate::common::visualize::Color::index(i)),
None,
);
}
}
}
}
if conflicts.get(x as usize, y as usize) > &1 {
color.set_bg(Some(termcolor::Color::Black));
for x in 0..self.map.width {
for y in 0..self.map.height {
if conflicts.get(x, y) > &1 {
v.overwrite_background(
Position::new(x as i32, y as i32),
Some(crate::common::visualize::Color::new(100, 80, 80)),
);
}
}
}
if let Some((i, c)) = m.get(x as usize, y as usize) {
color.set_fg(Some(crate::common::color::COLORS[*i]));
(color, c)
} else if self.map.get(x as usize, y as usize).blocked {
(color, "#")
} else if x % 8 == 0 || y % 8 == 0 {
(color, "")
} else {
(color, " ")
for path in &self.belts {
for p in &path[1..] {
match p {
PathField::Belt { pos, dir: _ } => {
*conflicts.get_mut(pos.x as usize, pos.y as usize) += 1
}
PathField::Underground { pos, dir, len } => {
*conflicts.get_mut(pos.x as usize, pos.y as usize) += 1;
let end = pos.in_direction(dir, *len as PositionType);
*conflicts.get_mut(end.x as usize, end.y as usize) += 1;
}
}
}
});
}
v
}
}