License | BSD-3-Clause |
---|---|
Safe Haskell | None |
Language | Haskell2010 |
Swarm.Game.Location
Description
Locations and headings.
Synopsis
- type Location = Point V2 Int32
- pattern Location :: Int32 -> Int32 -> Location
- class HasLocation a where
- type Heading = V2 Int32
- applyTurn :: Direction -> Heading -> Heading
- relativeTo :: AbsoluteDir -> AbsoluteDir -> PlanarRelativeDir
- toDirection :: Heading -> Maybe Direction
- toAbsDirection :: Heading -> Maybe AbsoluteDir
- nearestDirection :: Heading -> AbsoluteDir
- isCardinal :: Direction -> Bool
- north :: Heading
- south :: Heading
- east :: Heading
- west :: Heading
- manhattan :: Location -> Location -> Int32
- euclidean :: Location -> Location -> Double
- asVector :: Location -> V2 Int32
- getLocsInArea :: Location -> Int32 -> [Location]
- getElemsInArea :: Location -> Int32 -> Map Location e -> [e]
- class Additive (Diff p) => Affine (p :: Type -> Type) where
- newtype Point (f :: Type -> Type) a = P (f a)
- origin :: forall (f :: Type -> Type) a. (Additive f, Num a) => Point f a
- toHeading :: AbsoluteDir -> Heading
Documentation
type Location = Point V2 Int32 Source #
A Location
is a pair of (x,y)
coordinates, both up to 32 bits.
The positive x-axis points east and the positive y-axis points
north. These are the coordinates that are shown to players.
See also the Coords
type defined in Swarm.Game.World, which
use a (row, column) format instead, which is more convenient for
internal use. The Swarm.Game.World module also defines
conversions between Location
and Coords
.
pattern Location :: Int32 -> Int32 -> Location Source #
A convenient way to pattern-match on Location
values.
class HasLocation a where Source #
Minimal complete definition
Methods
modifyLoc :: (Location -> Location) -> a -> a Source #
offsetLoc :: V2 Int32 -> a -> a Source #
Translation by a vector
Instances
HasLocation Waypoint Source # | |
HasLocation LocatedStructure Source # | |
Defined in Swarm.Game.Scenario.Topography.Structure.Recognition.Static Methods modifyLoc :: (Location -> Location) -> LocatedStructure -> LocatedStructure Source # offsetLoc :: V2 Int32 -> LocatedStructure -> LocatedStructure Source # | |
HasLocation (PositionedGrid a) Source # | |
Defined in Swarm.Game.Scenario.Topography.Structure.Overlay Methods modifyLoc :: (Location -> Location) -> PositionedGrid a -> PositionedGrid a Source # offsetLoc :: V2 Int32 -> PositionedGrid a -> PositionedGrid a Source # |
Heading and Direction functions
relativeTo :: AbsoluteDir -> AbsoluteDir -> PlanarRelativeDir Source #
Return the PlanarRelativeDir
which would result in turning to
the first (target) direction from the second (reference) direction.
>>>
DWest `relativeTo` DSouth
DRight>>>
DWest `relativeTo` DWest
DForward
toDirection :: Heading -> Maybe Direction Source #
Possibly convert a heading into a Direction
---that is, if the
vector happens to be a unit vector in one of the cardinal
directions.
>>>
toDirection (V2 0 (-1))
Just (DAbsolute DSouth)>>>
toDirection (V2 3 7)
Nothing
toAbsDirection :: Heading -> Maybe AbsoluteDir Source #
Like toDirection
, but preserve the type guarantee of an absolute direction
nearestDirection :: Heading -> AbsoluteDir Source #
Compute the absolute direction nearest to a given Heading
.
Logic adapted from https://gamedev.stackexchange.com/questions/49290/#comment213403_49300.
Utility functions
getLocsInArea :: Location -> Int32 -> [Location] Source #
Get all the locations that are within a certain manhattan distance from a given location.
>>>
getLocsInArea (P (V2 0 0)) 1
[P (V2 0 0),P (V2 0 1),P (V2 0 (-1)),P (V2 1 0),P (V2 (-1) 0)]>>>
map (\i -> length (getLocsInArea origin i)) [0..8]
[1,5,13,25,41,61,85,113,145]
See also Swarm.Game.Step.Const.genDiamondSides
.
getElemsInArea :: Location -> Int32 -> Map Location e -> [e] Source #
Get elements that are within a certain manhattan distance from location.
>>>
v2s i = [(p, manhattan origin p) | x <- [-i..i], y <- [-i..i], let p = Location x y]
>>>
v2s 0
[(P (V2 0 0),0)]>>>
map (\i -> length (getElemsInArea origin i (Map.fromList $ v2s i))) [0..8]
[1,5,13,25,41,61,85,113,145]
The last test is the sequence "Centered square numbers": https://oeis.org/A001844
Re-exports for convenience
class Additive (Diff p) => Affine (p :: Type -> Type) where #
An affine space is roughly a vector space in which we have forgotten or at least pretend to have forgotten the origin.
a .+^ (b .-. a) = b@ (a .+^ u) .+^ v = a .+^ (u ^+^ v)@ (a .-. b) ^+^ v = (a .+^ v) .-. q@
Methods
(.-.) :: Num a => p a -> p a -> Diff p a infixl 6 #
Get the difference between two points as a vector offset.
(.+^) :: Num a => p a -> Diff p a -> p a infixl 6 #
Add a vector offset to a point.
(.-^) :: Num a => p a -> Diff p a -> p a infixl 6 #
Subtract a vector offset from a point.
Instances
Affine ZipList | |||||
Defined in Linear.Affine | |||||
Affine Complex | |||||
Defined in Linear.Affine | |||||
Affine Identity | |||||
Defined in Linear.Affine | |||||
Affine IntMap | |||||
Affine Plucker | |||||
Defined in Linear.Affine | |||||
Affine Quaternion | |||||
Defined in Linear.Affine Associated Types
Methods (.-.) :: Num a => Quaternion a -> Quaternion a -> Diff Quaternion a # (.+^) :: Num a => Quaternion a -> Diff Quaternion a -> Quaternion a # (.-^) :: Num a => Quaternion a -> Diff Quaternion a -> Quaternion a # | |||||
Affine V0 | |||||
Affine V1 | |||||
Affine V2 | |||||
Affine V3 | |||||
Affine V4 | |||||
Affine Vector | |||||
Affine Maybe | |||||
Affine [] | |||||
Ord k => Affine (Map k) | |||||
Additive f => Affine (Point f) | |||||
Defined in Linear.Affine Associated Types
| |||||
(Eq k, Hashable k) => Affine (HashMap k) | |||||
Defined in Linear.Affine | |||||
Dim n => Affine (V n) | |||||
(Affine f, Affine g) => Affine (Product f g) | |||||
Defined in Linear.Affine | |||||
Affine ((->) b) | |||||
newtype Point (f :: Type -> Type) a #
A handy wrapper to help distinguish points from vectors at the type level
Constructors
P (f a) |
Instances
FromJSON Location Source # | |||||
Defined in Swarm.Game.Location | |||||
ToJSON Location Source # | |||||
Generic1 (Point f :: Type -> Type) | |||||
Defined in Linear.Affine | |||||
Unbox (f a) => Vector Vector (Point f a) | |||||
Defined in Linear.Affine Methods basicUnsafeFreeze :: Mutable Vector s (Point f a) -> ST s (Vector (Point f a)) basicUnsafeThaw :: Vector (Point f a) -> ST s (Mutable Vector s (Point f a)) basicLength :: Vector (Point f a) -> Int basicUnsafeSlice :: Int -> Int -> Vector (Point f a) -> Vector (Point f a) basicUnsafeIndexM :: Vector (Point f a) -> Int -> Box (Point f a) basicUnsafeCopy :: Mutable Vector s (Point f a) -> Vector (Point f a) -> ST s () | |||||
Unbox (f a) => MVector MVector (Point f a) | |||||
Defined in Linear.Affine Methods basicLength :: MVector s (Point f a) -> Int basicUnsafeSlice :: Int -> Int -> MVector s (Point f a) -> MVector s (Point f a) basicOverlaps :: MVector s (Point f a) -> MVector s (Point f a) -> Bool basicUnsafeNew :: Int -> ST s (MVector s (Point f a)) basicInitialize :: MVector s (Point f a) -> ST s () basicUnsafeReplicate :: Int -> Point f a -> ST s (MVector s (Point f a)) basicUnsafeRead :: MVector s (Point f a) -> Int -> ST s (Point f a) basicUnsafeWrite :: MVector s (Point f a) -> Int -> Point f a -> ST s () basicClear :: MVector s (Point f a) -> ST s () basicSet :: MVector s (Point f a) -> Point f a -> ST s () basicUnsafeCopy :: MVector s (Point f a) -> MVector s (Point f a) -> ST s () basicUnsafeMove :: MVector s (Point f a) -> MVector s (Point f a) -> ST s () basicUnsafeGrow :: MVector s (Point f a) -> Int -> ST s (MVector s (Point f a)) | |||||
Representable f => Representable (Point f) | |||||
Foldable f => Foldable (Point f) | |||||
Defined in Linear.Affine Methods fold :: Monoid m => Point f m -> m # foldMap :: Monoid m => (a -> m) -> Point f a -> m # foldMap' :: Monoid m => (a -> m) -> Point f a -> m # foldr :: (a -> b -> b) -> b -> Point f a -> b # foldr' :: (a -> b -> b) -> b -> Point f a -> b # foldl :: (b -> a -> b) -> b -> Point f a -> b # foldl' :: (b -> a -> b) -> b -> Point f a -> b # foldr1 :: (a -> a -> a) -> Point f a -> a # foldl1 :: (a -> a -> a) -> Point f a -> a # elem :: Eq a => a -> Point f a -> Bool # maximum :: Ord a => Point f a -> a # minimum :: Ord a => Point f a -> a # | |||||
Eq1 f => Eq1 (Point f) | |||||
Ord1 f => Ord1 (Point f) | |||||
Defined in Linear.Affine | |||||
Read1 f => Read1 (Point f) | |||||
Defined in Linear.Affine | |||||
Show1 f => Show1 (Point f) | |||||
Traversable f => Traversable (Point f) | |||||
Applicative f => Applicative (Point f) | |||||
Functor f => Functor (Point f) | |||||
Monad f => Monad (Point f) | |||||
Serial1 f => Serial1 (Point f) | |||||
Defined in Linear.Affine Methods serializeWith :: MonadPut m => (a -> m ()) -> Point f a -> m () # deserializeWith :: MonadGet m => m a -> m (Point f a) # | |||||
Distributive f => Distributive (Point f) | |||||
Hashable1 f => Hashable1 (Point f) | |||||
Defined in Linear.Affine | |||||
Additive f => Affine (Point f) | |||||
Defined in Linear.Affine Associated Types
| |||||
Metric f => Metric (Point f) | |||||
Defined in Linear.Affine | |||||
Finite f => Finite (Point f) | |||||
R1 f => R1 (Point f) | |||||
Defined in Linear.Affine | |||||
R2 f => R2 (Point f) | |||||
R3 f => R3 (Point f) | |||||
R4 f => R4 (Point f) | |||||
Additive f => Additive (Point f) | |||||
Defined in Linear.Affine Methods (^+^) :: Num a => Point f a -> Point f a -> Point f a # (^-^) :: Num a => Point f a -> Point f a -> Point f a # lerp :: Num a => a -> Point f a -> Point f a -> Point f a # liftU2 :: (a -> a -> a) -> Point f a -> Point f a -> Point f a # liftI2 :: (a -> b -> c) -> Point f a -> Point f b -> Point f c # | |||||
Apply f => Apply (Point f) | |||||
Bind f => Bind (Point f) | |||||
(Typeable f, Typeable a, Data (f a)) => Data (Point f a) | |||||
Defined in Linear.Affine Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Point f a -> c (Point f a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Point f a) # toConstr :: Point f a -> Constr # dataTypeOf :: Point f a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Point f a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Point f a)) # gmapT :: (forall b. Data b => b -> b) -> Point f a -> Point f a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Point f a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Point f a -> r # gmapQ :: (forall d. Data d => d -> u) -> Point f a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Point f a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Point f a -> m (Point f a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Point f a -> m (Point f a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Point f a -> m (Point f a) # | |||||
Storable (f a) => Storable (Point f a) | |||||
Defined in Linear.Affine | |||||
Monoid (f a) => Monoid (Point f a) | |||||
Semigroup (f a) => Semigroup (Point f a) | |||||
Generic (Point f a) | |||||
Defined in Linear.Affine Associated Types
| |||||
Ix (f a) => Ix (Point f a) | |||||
Defined in Linear.Affine Methods range :: (Point f a, Point f a) -> [Point f a] # index :: (Point f a, Point f a) -> Point f a -> Int # unsafeIndex :: (Point f a, Point f a) -> Point f a -> Int # inRange :: (Point f a, Point f a) -> Point f a -> Bool # rangeSize :: (Point f a, Point f a) -> Int # unsafeRangeSize :: (Point f a, Point f a) -> Int # | |||||
Num (f a) => Num (Point f a) | |||||
Defined in Linear.Affine | |||||
Read (f a) => Read (Point f a) | |||||
Fractional (f a) => Fractional (Point f a) | |||||
Show (f a) => Show (Point f a) | |||||
Binary (f a) => Binary (Point f a) | |||||
Serial (f a) => Serial (Point f a) | |||||
Defined in Linear.Affine | |||||
Serialize (f a) => Serialize (Point f a) | |||||
NFData (f a) => NFData (Point f a) | |||||
Defined in Linear.Affine | |||||
Eq (f a) => Eq (Point f a) | |||||
Ord (f a) => Ord (Point f a) | |||||
Hashable (f a) => Hashable (Point f a) | |||||
Defined in Linear.Affine | |||||
Ixed (f a) => Ixed (Point f a) | |||||
Defined in Linear.Affine | |||||
Wrapped (Point f a) | |||||
Epsilon (f a) => Epsilon (Point f a) | |||||
Defined in Linear.Affine | |||||
Random (f a) => Random (Point f a) | |||||
Unbox (f a) => Unbox (Point f a) | |||||
Defined in Linear.Affine | |||||
t ~ Point g b => Rewrapped (Point f a) t | |||||
Defined in Linear.Affine | |||||
Traversable f => Each (Point f a) (Point f b) a b | |||||
type Rep1 (Point f :: Type -> Type) | |||||
newtype MVector s (Point f a) | |||||
Defined in Linear.Affine | |||||
type Rep (Point f) | |||||
Defined in Linear.Affine | |||||
type Diff (Point f) | |||||
Defined in Linear.Affine | |||||
type Size (Point f) | |||||
Defined in Linear.Affine | |||||
type Rep (Point f a) | |||||
Defined in Linear.Affine | |||||
type Index (Point f a) | |||||
Defined in Linear.Affine | |||||
type IxValue (Point f a) | |||||
Defined in Linear.Affine | |||||
type Unwrapped (Point f a) | |||||
Defined in Linear.Affine | |||||
newtype Vector (Point f a) | |||||
Defined in Linear.Affine |
origin :: forall (f :: Type -> Type) a. (Additive f, Num a) => Point f a #
Vector spaces have origins.
toHeading :: AbsoluteDir -> Heading Source #