Fix writter

This commit is contained in:
Adrian Woźniak 2024-09-16 12:32:01 +02:00
parent 353e59d66a
commit 6b7681bad6

View File

@ -451,12 +451,22 @@ impl<const X: usize, const Y: usize, const SIZE: usize> AsciiBroad<X, Y, SIZE> {
}
}
struct BufWriter<'s>(&'s mut [MazePart]);
struct BufWriter<'s>(&'s mut [MazePart], usize);
impl<'s> BufWriter<'s> {
pub fn new(b: &'s mut [MazePart]) -> Self {
Self(b, 0)
}
pub fn push(&mut self, part: MazePart) {
self.0[0] = part;
self.0 = &mut self.0[1..];
self.0[self.1] = part;
self.1 += 1;
}
pub fn copy(&mut self, src: &[MazePart]) {
src.iter()
.take_while(|p| **p != MazePart::Noop)
.for_each(|p| {
self.push(*p);
});
}
}
@ -464,22 +474,12 @@ pub struct BinaryMap<const X: usize, const Y: usize, const SIZE: usize>;
impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) {
let push = |p: MazePart, buffer: &mut [MazePart]| {
buffer[0] = p;
&mut buffer[1..]
};
let copy = |src: &[MazePart], dst: &mut [MazePart]| {
src.iter()
.take_while(|p| **p != MazePart::Noop)
.for_each(|p| {
dst = push(*p, dst);
});
};
let mut bw = BufWriter::new(buffer);
buffer = push(MazePart::Wall, buffer);
bw.push(MazePart::Wall);
(0..X).into_iter().for_each(|_| {
push(MazePart::Wall, buffer);
push(MazePart::Wall, buffer);
bw.push(MazePart::Wall);
bw.push(MazePart::Wall);
});
let mut top_line = [MazePart::Noop; SIZE];
@ -488,34 +488,28 @@ impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
for y in 0..Y {
top_line.fill(MazePart::Noop);
bottom_line.fill(MazePart::Noop);
let tl = &mut top_line;
let bl = &mut bottom_line;
let mut tl = BufWriter::new(&mut top_line);
let mut bl = BufWriter::new(&mut bottom_line);
push(MazePart::Wall, tl);
push(MazePart::Wall, bl);
tl.push(MazePart::Wall);
bl.push(MazePart::Wall);
for x in 0..X {
push(MazePart::Passage, tl);
push(
if grid.is_carved((x, y), Cell::EAST) {
tl.push(MazePart::Passage);
tl.push(if grid.is_carved((x, y), Cell::EAST) {
MazePart::Passage
} else {
MazePart::Wall
},
tl,
);
});
push(
if grid.is_carved((x, y), Cell::SOUTH) {
bl.push(if grid.is_carved((x, y), Cell::SOUTH) {
MazePart::Passage
} else {
MazePart::Wall
},
bl,
);
});
}
copy(&top_line, buffer);
copy(&bottom_line, buffer);
bw.copy(&top_line);
bw.copy(&bottom_line);
}
}
}