From 55e0c98db3302eb58456cbfd33c43f43110786cc Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 6 Dec 2023 23:06:39 -0500 Subject: [PATCH] Add a crate to start defining 2-d grids --- Cargo.lock | 4 +++ Cargo.toml | 1 + gaming-grid/Cargo.toml | 8 ++++++ gaming-grid/src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 gaming-grid/Cargo.toml create mode 100644 gaming-grid/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f02baf5..57ce7a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1219,6 +1219,10 @@ dependencies = [ "slab", ] +[[package]] +name = "gaming-grid" +version = "0.1.0" + [[package]] name = "gcd" version = "2.3.0" diff --git a/Cargo.toml b/Cargo.toml index 1957e1d..dfa80a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ members = [ "emseries", "file-service", "fluent-ergonomics", + "gaming-grid", "geo-types", "gm-control-panel", "hex-grid", diff --git a/gaming-grid/Cargo.toml b/gaming-grid/Cargo.toml new file mode 100644 index 0000000..e1239b8 --- /dev/null +++ b/gaming-grid/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "gaming-grid" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/gaming-grid/src/lib.rs b/gaming-grid/src/lib.rs new file mode 100644 index 0000000..e68e0cb --- /dev/null +++ b/gaming-grid/src/lib.rs @@ -0,0 +1,61 @@ +// The gaming grid is a collection of coordinates and grouping utilities that I have developed from +// doing Advent of Code and from building the hex map coordinate system. +// +// This initial version works with square spaces, with the intent to be unified with the hexagonal +// coordinates. + +/// A Group is a collection of locations that create a group, in whatever that means for a game. In +/// Go, this means a group of stones. In Advent of Code 2023 Day 3, that would be a "part" or a +/// "component". +/// +/// AddrType is the data type of the addresses used in the coordinate system. +trait Group { + /// Return a list of all coordinates that make up the body of a group. + fn body(&self) -> dyn Iterator>; + + /// Return a list of all coordinates within x distance of the border of a group. By definition, + /// this will not include any coordinates within the group. + fn halo(&self, distance: u32) -> dyn Iterator>; +} + +struct Coordinate { + row: T, + col: T, +} + +/// The rules by which objects are adjacent to one another. +enum Adjacency { + /// Only calculate adjacency according to those objects that are on the same row. + Horizontal, + + /// Only calculate adjacency according to those objects that are in the same column. + Vertical, + + /// Calculate adjacency according to orthogonal rules. Two objects are adjacent if they are in + /// the same row or the same column. + Orthogonal, + + /// Calculate adjacency according to how the King moves in Chess: one space in any direction is + /// adjacent. + Chess, +} + +/// Which set of rules to use when calculating a distance on a square board. +enum DistanceTraversal { + /// Taxicab distance is calculated exactly along orthogonal lines, in the way that a taxi would + /// have to move in a city that is on a perfect square. + Taxicab, + + /// Chess rules allow for diagonal movement in addition to handicap, similar to how a Queen + /// moves. + Chess, + + /// Circular rules calculate actual distance based on a true circular calculation. Unlike Chess + /// rules (where a distance of 8 takes a queen from one corner of the board to another), under + /// Circular rules a distance of 8 along the diagonal would only take one between five and six + /// spaces along both the X and Y axis. + Circular, +} + +// bounding function: filters out coordinates that are outside of the bounds of the board. What +// exactly that means depends on the bounding function and the extra rules injected into it.