From 3e6dfd4eca26254d1c2c7c52d0ae28fe80d8ab02 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 01:29:35 +0100 Subject: [PATCH] asve --- crates/border-wars/src/map/hex.rs | 41 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 79a1b65..4bb867c 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -55,6 +55,11 @@ pub trait Number: fn abs(self) -> Self { if self < Self::ZERO { -self } else { self } } + + /// Converts `self` to an `usize`. + fn to_usize(self) -> usize; + + fn from_usize(value: usize) -> Self; } macro_rules! number_impl { @@ -65,7 +70,17 @@ macro_rules! number_impl { const ZERO: Self = [< 0 $t >]; const ONE: Self = [< 1 $t >]; const TWO: Self = [< 2 $t >]; + + fn to_usize(self) -> usize { + self as usize + } + + fn from_usize(value: usize) -> Self { + value as $t + } + } + )*}}; } @@ -118,8 +133,8 @@ impl HexDirection { pub struct HexRing { current: HexPosition, direction: HexDirection, - radius: T, - index: T, + radius: usize, + index: usize, } impl Iterator for HexRing { @@ -160,16 +175,16 @@ impl Iterator for HexRing { pub struct HexSpiral { origin: HexPosition, current: HexRing, - radius: T, - index: T, + radius: usize, + index: usize, } impl Iterator for HexSpiral { type Item = HexPosition; fn next(&mut self) -> Option { - if self.index == T::TWO { - self.index += T::ONE; + if self.index == 0 { + self.index += 1; return Some(self.origin); } if self.index > self.radius { @@ -177,7 +192,7 @@ impl Iterator for HexSpiral { } let mut result = self.current.next(); if result.is_none() && self.index < self.radius { - self.index += T::ONE; + self.index += 1; self.current = self.origin.ring(self.index); result = self.current.next(); } @@ -198,22 +213,22 @@ impl HexPosition { } /// Returns the hexagonal ring of the given radius. - pub fn ring(self, radius: T) -> HexRing { + pub fn ring(self, radius: usize) -> HexRing { HexRing { - current: self + HexDirection::DownLeft.to_vector() * radius, + current: self + HexDirection::DownLeft.to_vector() * radius.from_usize(), direction: HexDirection::Right, radius, - index: T::ZERO, + index: 0, } } /// Returns the hexagonal spiral of the given radius. - pub fn spiral(self, radius: T) -> HexSpiral { + pub fn spiral(self, radius: usize) -> HexSpiral { HexSpiral { origin: self, - current: self.ring(T::ONE), + current: self.ring(1), radius, - index: T::ZERO, + index: 0, } } }