Rename a lot of stuff related to piece types

- srs::Shape => srs::PieceType

- Piece.shape => Piece.ty

- Piece<S> => Piece<T>
This commit is contained in:
tali 2022-12-15 13:34:50 -05:00
parent 41df73bafc
commit 0d52d6b1b5
3 changed files with 54 additions and 57 deletions

View File

@ -148,13 +148,13 @@ where
/// Rotates a piece, performing kicks to find a final location. Returns `Some(idx)` if /// Rotates a piece, performing kicks to find a final location. Returns `Some(idx)` if
/// successful, where `idx` is the index into the kick table performed. Index `0` means it /// successful, where `idx` is the index into the kick table performed. Index `0` means it
/// was not kicked. Returns `None` if the rotation failed. /// was not kicked. Returns `None` if the rotation failed.
pub fn rotate<'c, 'k, S>(piece: &mut Piece<S>, mat: &Mat, dir: Spin) -> Option<usize> pub fn rotate<'c, 'k, T>(piece: &mut Piece<T>, mat: &Mat, dir: Spin) -> Option<usize>
where where
S: Shape<'c> + Kicks<'k>, T: Shape<'c> + Kicks<'k>,
{ {
let r0 = piece.loc.r; let r0 = piece.loc.r;
let r1 = piece.loc.r + dir; let r1 = piece.loc.r + dir;
let kicks = piece.shape.kicks(r0, r1); let kicks = piece.ty.kicks(r0, r1);
piece.loc.r = r1; piece.loc.r = r1;
let cells = piece.cells(); let cells = piece.cells();
@ -208,7 +208,7 @@ mod test {
fn drop(x: i16, y: i16, r: Rot) -> i16 { fn drop(x: i16, y: i16, r: Rot) -> i16 {
let mut piece = Piece { let mut piece = Piece {
shape: Tri, ty: Tri,
loc: Loc { x, y, r }, loc: Loc { x, y, r },
}; };
let did_fall = super::drop(&mut piece, MAT); let did_fall = super::drop(&mut piece, MAT);
@ -238,7 +238,7 @@ mod test {
fn shift(x: i16, y: i16, r: Rot, dir: Horizontal) -> i16 { fn shift(x: i16, y: i16, r: Rot, dir: Horizontal) -> i16 {
let mut piece = Piece { let mut piece = Piece {
shape: Tri, ty: Tri,
loc: Loc { x, y, r }, loc: Loc { x, y, r },
}; };
let did_shift = super::shift(&mut piece, MAT, dir); let did_shift = super::shift(&mut piece, MAT, dir);
@ -278,7 +278,7 @@ mod test {
fn rotate(x: i16, y: i16, r: Rot, dir: Spin) -> (i16, i16, Rot, Option<usize>) { fn rotate(x: i16, y: i16, r: Rot, dir: Spin) -> (i16, i16, Rot, Option<usize>) {
let loc = Loc { x, y, r }; let loc = Loc { x, y, r };
let mut piece = Piece { shape: Tri, loc }; let mut piece = Piece { ty: Tri, loc };
let result = super::rotate(&mut piece, MAT, dir); let result = super::rotate(&mut piece, MAT, dir);
assert_eq!(result.is_some(), piece.loc != loc, "{:?},{:?}", piece, loc); assert_eq!(result.is_some(), piece.loc != loc, "{:?},{:?}", piece, loc);
(piece.loc.x, piece.loc.y, piece.loc.r, result) (piece.loc.x, piece.loc.y, piece.loc.r, result)

View File

@ -198,19 +198,17 @@ impl core::fmt::Debug for Cells<'_> {
/// Represents the current state of a piece. /// Represents the current state of a piece.
#[derive(Clone, Eq, PartialEq, Debug, Hash)] #[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Piece<S> { pub struct Piece<T> {
/// The shape of the piece. /// The piece type (i.e. its shape).
pub shape: S, pub ty: T,
/// The location of the piece. /// The location of the piece.
pub loc: Loc, pub loc: Loc,
} }
impl<'c, S: Shape<'c>> Piece<S> { impl<'c, T: Shape<'c>> Piece<T> {
/// Returns the cells occupied by this piece. /// Returns the cells occupied by this piece.
pub fn cells(&self) -> Cells<'c> { pub fn cells(&self) -> Cells<'c> {
self.shape self.ty.cells(self.loc.r).translate(self.loc.x, self.loc.y)
.cells(self.loc.r)
.translate(self.loc.x, self.loc.y)
} }
} }
@ -220,10 +218,10 @@ pub trait Spawn {
fn spawn_pos(&self) -> (i16, i16); fn spawn_pos(&self) -> (i16, i16);
} }
impl<S: Spawn> Piece<S> { impl<T: Spawn> Piece<T> {
pub fn spawn(shape: S) -> Self { pub fn spawn(ty: T) -> Self {
let loc = shape.spawn_pos().into(); let loc = ty.spawn_pos().into();
Self { shape, loc } Self { ty, loc }
} }
} }
@ -268,7 +266,7 @@ pub mod test {
fn coords(x: i16, y: i16, r: Rot) -> Vec<(i16, i16)> { fn coords(x: i16, y: i16, r: Rot) -> Vec<(i16, i16)> {
let piece = Piece { let piece = Piece {
shape: Tri, ty: Tri,
loc: Loc { x, y, r }, loc: Loc { x, y, r },
}; };
let mut coords: Vec<_> = piece.cells().coords().collect(); let mut coords: Vec<_> = piece.cells().coords().collect();
@ -293,7 +291,7 @@ pub mod test {
fn isects(xs: RangeInclusive<i16>, y: i16, r: Rot) -> Vec<bool> { fn isects(xs: RangeInclusive<i16>, y: i16, r: Rot) -> Vec<bool> {
xs.map(|x| { xs.map(|x| {
let piece = Piece { let piece = Piece {
shape: Tri, ty: Tri,
loc: Loc { x, y, r }, loc: Loc { x, y, r },
}; };
piece.cells().intersects(MAT) piece.cells().intersects(MAT)

View File

@ -1,7 +1,7 @@
//! Implementation of SRS shapes and movement quirks. //! Implementation of SRS shapes and movement quirks.
use crate::input::Kicks as KicksTrait; use crate::input::Kicks;
use crate::piece::{Cells, Rot, Shape as ShapeTrait, Spawn as SpawnTrait}; use crate::piece::{Cells, Rot, Shape, Spawn};
mod code_gen { mod code_gen {
include!(concat!(env!("OUT_DIR"), "/srs.rs")); include!(concat!(env!("OUT_DIR"), "/srs.rs"));
@ -9,7 +9,7 @@ mod code_gen {
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(u8)] #[repr(u8)]
pub enum Shape { pub enum PieceType {
I = code_gen::shape_indices::I as u8, I = code_gen::shape_indices::I as u8,
J = code_gen::shape_indices::J as u8, J = code_gen::shape_indices::J as u8,
L = code_gen::shape_indices::L as u8, L = code_gen::shape_indices::L as u8,
@ -19,7 +19,7 @@ pub enum Shape {
Z = code_gen::shape_indices::Z as u8, Z = code_gen::shape_indices::Z as u8,
} }
impl ShapeTrait<'static> for Shape { impl Shape<'static> for PieceType {
fn cells(&self, r: Rot) -> Cells<'static> { fn cells(&self, r: Rot) -> Cells<'static> {
let i = (*self as usize) * 4 + r as usize; let i = (*self as usize) * 4 + r as usize;
let xs = code_gen::extents::X0[i]..code_gen::extents::X1[i]; let xs = code_gen::extents::X0[i]..code_gen::extents::X1[i];
@ -30,7 +30,7 @@ impl ShapeTrait<'static> for Shape {
} }
} }
impl KicksTrait<'static> for Shape { impl Kicks<'static> for PieceType {
fn kicks(&self, r0: Rot, r1: Rot) -> &'static [(i16, i16)] { fn kicks(&self, r0: Rot, r1: Rot) -> &'static [(i16, i16)] {
let i = (*self as usize) * 16 + (r0 as usize) * 4 + r1 as usize; let i = (*self as usize) * 16 + (r0 as usize) * 4 + r1 as usize;
let idx = code_gen::KICKS_INDEX[i]; let idx = code_gen::KICKS_INDEX[i];
@ -39,13 +39,13 @@ impl KicksTrait<'static> for Shape {
} }
} }
impl SpawnTrait for Shape { impl Spawn for PieceType {
fn spawn_pos(&self) -> (i16, i16) { fn spawn_pos(&self) -> (i16, i16) {
code_gen::SPAWN code_gen::SPAWN
} }
} }
pub type Piece = crate::piece::Piece<Shape>; pub type Piece = crate::piece::Piece<PieceType>;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
@ -57,19 +57,18 @@ mod test {
use alloc::vec::Vec; use alloc::vec::Vec;
use core::ops::Range; use core::ops::Range;
use PieceType::*;
#[test] #[test]
fn test_spawn() { fn test_spawn() {
use Shape::*; for ty in [L, O, J, I, S, T, Z] {
for s in [L, O, J, I, S, T, Z] { assert_eq!(Piece::spawn(ty).loc, (4, 19, Rot::N).into());
assert_eq!(Piece::spawn(s).loc, (4, 19, Rot::N).into());
} }
} }
fn get_cells(s: Shape, r: Rot) -> (Range<i16>, Range<i16>, Vec<(i16, i16)>) { fn get_cells(ty: PieceType, r: Rot) -> (Range<i16>, Range<i16>, Vec<(i16, i16)>) {
let piece = Piece { let loc = Loc { x: 0, y: 0, r };
shape: s, let piece = Piece { ty, loc };
loc: Loc { x: 0, y: 0, r },
};
let mut coords = piece.cells().coords().collect::<Vec<_>>(); let mut coords = piece.cells().coords().collect::<Vec<_>>();
coords.sort(); coords.sort();
let (xs, ys) = piece.cells().extents(); let (xs, ys) = piece.cells().extents();
@ -96,32 +95,32 @@ mod test {
xy xy
} }
fn test_cells_all_rotations(s: Shape, expected_coords: &[(i16, i16)]) { fn test_cells_all_rotations(ty: PieceType, expected_coords: &[(i16, i16)]) {
for r in (0..4).map(Rot::from) { for r in (0..4).map(Rot::from) {
let (act_xs, act_ys, act_coords) = get_cells(s, r); let (act_xs, act_ys, act_coords) = get_cells(ty, r);
let (exp_xs, exp_ys, exp_coords) = compute_cells(expected_coords, r); let (exp_xs, exp_ys, exp_coords) = compute_cells(expected_coords, r);
assert_eq!(act_xs, exp_xs, "{s:?},{r:?}"); assert_eq!(act_xs, exp_xs, "{ty:?},{r:?}");
assert_eq!(act_ys, exp_ys, "{s:?},{r:?}"); assert_eq!(act_ys, exp_ys, "{ty:?},{r:?}");
assert_eq!(act_coords, exp_coords, "{s:?},{r:?}"); assert_eq!(act_coords, exp_coords, "{ty:?},{r:?}");
} }
} }
#[test] #[test]
fn test_srs_cells() { fn test_srs_cells() {
test_cells_all_rotations(Shape::I, &[(-1, 0), (0, 0), (1, 0), (2, 0)]); test_cells_all_rotations(I, &[(-1, 0), (0, 0), (1, 0), (2, 0)]);
test_cells_all_rotations(Shape::J, &[(-1, 1), (-1, 0), (0, 0), (1, 0)]); test_cells_all_rotations(J, &[(-1, 1), (-1, 0), (0, 0), (1, 0)]);
test_cells_all_rotations(Shape::L, &[(1, 1), (-1, 0), (0, 0), (1, 0)]); test_cells_all_rotations(L, &[(1, 1), (-1, 0), (0, 0), (1, 0)]);
test_cells_all_rotations(Shape::O, &[(0, 0), (1, 0), (0, 1), (1, 1)]); test_cells_all_rotations(O, &[(0, 0), (1, 0), (0, 1), (1, 1)]);
test_cells_all_rotations(Shape::S, &[(-1, 0), (0, 0), (0, 1), (1, 1)]); test_cells_all_rotations(S, &[(-1, 0), (0, 0), (0, 1), (1, 1)]);
test_cells_all_rotations(Shape::T, &[(0, 1), (-1, 0), (0, 0), (1, 0)]); test_cells_all_rotations(T, &[(0, 1), (-1, 0), (0, 0), (1, 0)]);
test_cells_all_rotations(Shape::Z, &[(1, 0), (0, 0), (0, 1), (-1, 1)]); test_cells_all_rotations(Z, &[(1, 0), (0, 0), (0, 1), (-1, 1)]);
} }
#[test] #[test]
fn test_srs_o_kicks_in_place() { fn test_srs_o_kicks_in_place() {
use input::Spin::*; use input::Spin::*;
let mut piece = Piece { let mut piece = Piece {
shape: Shape::O, ty: O,
loc: (6, 6, Rot::N).into(), loc: (6, 6, Rot::N).into(),
}; };
assert_eq!(input::rotate(&mut piece, Mat::EMPTY, Cw), Some(0)); assert_eq!(input::rotate(&mut piece, Mat::EMPTY, Cw), Some(0));
@ -148,7 +147,7 @@ mod test {
"xxxxxx.xxx"; "xxxxxx.xxx";
}; };
let mut piece = Piece { let mut piece = Piece {
shape: Shape::T, ty: T,
loc: (5, 3, Rot::N).into(), loc: (5, 3, Rot::N).into(),
}; };
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Ccw), Some(4)); assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Ccw), Some(4));
@ -166,7 +165,7 @@ mod test {
"xxxxx.xxxx"; "xxxxx.xxxx";
}; };
let mut piece = Piece { let mut piece = Piece {
shape: Shape::Z, ty: Z,
loc: (5, 3, Rot::N).into(), loc: (5, 3, Rot::N).into(),
}; };
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Cw), Some(3)); assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Cw), Some(3));
@ -177,18 +176,18 @@ mod test {
#[test] #[test]
fn test_srs_cells_eq() { fn test_srs_cells_eq() {
fn rotate(s: Shape, dir: input::Spin) -> Piece { fn rotate(ty: PieceType, dir: input::Spin) -> Piece {
let mut piece = Piece::spawn(s); let mut piece = Piece::spawn(ty);
input::rotate(&mut piece, Mat::EMPTY, dir).unwrap(); input::rotate(&mut piece, Mat::EMPTY, dir).unwrap();
piece piece
} }
for s in [Shape::S, Shape::Z, Shape::I] { for ty in [S, Z, I] {
let cw = rotate(s, input::Spin::Cw); let cw = rotate(ty, input::Spin::Cw);
let ccw = rotate(s, input::Spin::Ccw); let ccw = rotate(ty, input::Spin::Ccw);
// rotation states differ // rotation states differ
assert_ne!(cw.loc.r, ccw.loc.r, "shape={s:?}"); assert_ne!(cw.loc.r, ccw.loc.r, "ty={ty:?}");
// cells() are equal after translating, since they occupy the same blocks // cells() are equal after translating, since they occupy the same blocks
assert_eq!(cw.cells().translate(-1, 0), ccw.cells(), "shape={s:?}"); assert_eq!(cw.cells().translate(-1, 0), ccw.cells(), "ty={ty:?}");
} }
} }
@ -196,7 +195,7 @@ mod test {
fn test_o_cells_eq() { fn test_o_cells_eq() {
fn cells(x: i16, y: i16, r: Rot) -> Cells<'static> { fn cells(x: i16, y: i16, r: Rot) -> Cells<'static> {
Piece { Piece {
shape: Shape::O, ty: O,
loc: Loc { x, y, r }, loc: Loc { x, y, r },
} }
.cells() .cells()