Replace some instances of sorting with minimums
This commit is contained in:
@@ -8,6 +8,7 @@ import Geometry
|
||||
|
||||
--import Data.List
|
||||
import Data.Maybe
|
||||
import Data.Foldable
|
||||
import qualified Data.IntMap.Strict as IM
|
||||
import Control.Lens
|
||||
|
||||
@@ -16,15 +17,23 @@ import Control.Lens
|
||||
--
|
||||
-- > safeMinimumOn id [] == Nothing
|
||||
-- > safeMinimumOn length ["test","extra","a"] == Just "a"
|
||||
safeMinimumOn :: (Ord b) => (a -> b) -> [a] -> Maybe a
|
||||
safeMinimumOn _ [] = Nothing
|
||||
safeMinimumOn f (x:xs) = g x (f x) xs
|
||||
safeMinimumOn' :: (Ord b) => (a -> b) -> [a] -> Maybe a
|
||||
safeMinimumOn' _ [] = Nothing
|
||||
safeMinimumOn' f (x:xs) = g x (f x) xs
|
||||
where
|
||||
g v _ [] = Just v
|
||||
g v mv (y:ys) | my < mv = g y my ys
|
||||
| otherwise = g v mv ys
|
||||
where my = f y
|
||||
|
||||
safeMinimumOn :: (Foldable t,Ord b) => (a -> b) -> t a -> Maybe a
|
||||
safeMinimumOn f = foldl' g Nothing
|
||||
where
|
||||
g (Just x) y
|
||||
| f x < f y = Just x
|
||||
| otherwise = Just y
|
||||
g Nothing y = Just y
|
||||
|
||||
hasLOS :: Point2 -> Point2 -> World -> Bool
|
||||
{-# INLINE hasLOS #-}
|
||||
hasLOS p1 p2 w = not $ pointHitsWalls p1 p2 $ wallsAlongLine p1 p2 w
|
||||
@@ -44,7 +53,6 @@ hitPointLines p1 p2
|
||||
reflectPointWalls :: Point2 -> Point2 -> IM.IntMap Wall -> Maybe (Point2,Point2)
|
||||
reflectPointWalls p1 p2 ws
|
||||
= safeMinimumOn (dist p1 . fst)
|
||||
. IM.elems
|
||||
$ IM.mapMaybe
|
||||
(( \(x,y) ->
|
||||
fmap ( (, reflectIn (x -.- y) (p2 -.- p1))
|
||||
@@ -63,7 +71,6 @@ reflectPointWallsDamped
|
||||
-> Maybe (Point2,Point2)
|
||||
reflectPointWallsDamped dfact p1 p2 ws
|
||||
= safeMinimumOn (dist p1 . fst)
|
||||
. IM.elems
|
||||
$ IM.mapMaybe
|
||||
(( \(x,y) -> fmap ((, reflectInParam dfact (x -.- y) (p2 -.- p1))
|
||||
. (+.+ errorNormalizeV 40 (vNormal (x -.- y))))
|
||||
@@ -84,14 +91,12 @@ furthestPointWalkable :: Point2 -> Point2 -> IM.IntMap Wall -> Point2
|
||||
furthestPointWalkable p1 p2 ws
|
||||
= fromMaybe p2
|
||||
. safeMinimumOn (dist p1)
|
||||
. IM.elems
|
||||
$ IM.mapMaybe ( uncurry (intersectSegSeg' p1 p2) . _wlLine) ws
|
||||
|
||||
collidePointIndirect :: Point2 -> Point2 -> IM.IntMap Wall -> Maybe Point2
|
||||
{-# INLINE collidePointIndirect #-}
|
||||
collidePointIndirect p1 p2 ws
|
||||
= safeMinimumOn (dist p1)
|
||||
. IM.elems
|
||||
. IM.mapMaybe ( uncurry (intersectSegSeg' p1 p2) . _wlLine)
|
||||
$ IM.filter (not . _wlIsSeeThrough) ws
|
||||
{- | Checks to see whether someone can fire bullets effectively between two points.
|
||||
@@ -99,7 +104,6 @@ collidePointIndirect p1 p2 ws
|
||||
collidePointFire :: Point2 -> Point2 -> IM.IntMap Wall -> Maybe Point2
|
||||
collidePointFire p1 p2 ws
|
||||
= safeMinimumOn (dist p1)
|
||||
. IM.elems
|
||||
. IM.mapMaybe ( uncurry (intersectSegSeg' p1 p2) . _wlLine )
|
||||
$ IM.filter (\wl -> not (_wlIsSeeThrough wl && isJust (wl ^? blHP))) ws
|
||||
{- | Checks to see whether someone can fire bullets effectively between two points.
|
||||
|
||||
Reference in New Issue
Block a user