Skip to content

Commit

Permalink
helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Sep 16, 2024
1 parent ef93626 commit 0443bc2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ validatePartialNavigation currentSubworldName upperLeft unmergedWaypoints portal
correctedWaypoints =
binTuples $
map
(\x -> (wpName $ wpConfig $ value x, fmap (offsetLoc $ upperLeft .-. origin) x))
(\x -> (wpName $ wpConfig $ value x, fmap (offsetLoc $ asVector upperLeft) x))
unmergedWaypoints
bareWaypoints = M.map (NE.map extractLoc) correctedWaypoints
waypointsWithUniqueFlag = M.filter (any $ wpUnique . wpConfig . value) correctedWaypoints
Expand Down
5 changes: 5 additions & 0 deletions src/swarm-topography/Swarm/Game/Location.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Swarm.Game.Location (
-- ** Utility functions
manhattan,
euclidean,
asVector,
getLocsInArea,
getElemsInArea,

Expand Down Expand Up @@ -199,6 +200,10 @@ manhattan (Location x1 y1) (Location x2 y2) = abs (x1 - x2) + abs (y1 - y2)
euclidean :: Location -> Location -> Double
euclidean p1 p2 = norm (fromIntegral <$> (p2 .-. p1))

-- | Converts a 'Point' to a vector offset from the 'origin'.
asVector :: Location -> V2 Int32
asVector loc = loc .-. origin

-- | Get all the locations that are within a certain manhattan
-- distance from a given location.
--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ overlayGridExpanded
baseGrid <> positionedOverlay
where
reorientedOverlayCells = applyOrientationTransform orientation overlayArea
placementAdjustedByOrigin = gridPosition baseGrid .+^ (yamlPlacementOffset .-. origin)
placementAdjustedByOrigin = gridPosition baseGrid .+^ asVector yamlPlacementOffset
positionedOverlay = PositionedGrid placementAdjustedByOrigin reorientedOverlayCells

-- * Validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ instance (Alternative f) => Semigroup (PositionedGrid (f a)) where
-- such that the displacement vector will have:
-- \* negative X component if the origin must be shifted east
-- \* positive Y component if the origin must be shifted south
originDelta@(V2 deltaX deltaY) = overlayLoc .-. origin
originDelta@(V2 deltaX deltaY) = asVector overlayLoc
-- Note that the adjustment vector will only ever have
-- a non-negative X component (i.e. loc of upper-left corner must be shifted east) and
-- a non-positive Y component (i.e. loc of upper-left corner must be shifted south).
Expand Down

0 comments on commit 0443bc2

Please sign in to comment.