From 5fe266ca9cb1086e6ebc7974947bf4cb7c58cb01 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 14 Apr 2018 13:34:28 +0200 Subject: [PATCH] place npcs only in reachable areas --- src/Interior.hs | 5 +---- src/NPC.hs | 22 +++++++++++++++++++--- src/Test.hs | 4 ++-- src/Types/Map.hs | 4 ++++ src/Types/UserData.hs | 1 + src/Util.hs | 4 ++++ 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Interior.hs b/src/Interior.hs index 8bf1231..e2f5056 100644 --- a/src/Interior.hs +++ b/src/Interior.hs @@ -14,6 +14,7 @@ import System.Random -- internal imports import Navigation +import Util import Types.Interior import Types.Map @@ -139,7 +140,3 @@ insertMat i into (roffs, coffs) = ) into ((,) <$> [1 .. nrows i] <*> [1 .. ncols i]) - -inBounds :: V2 Int -> Boundaries Int -> Bool -inBounds (V2 r c) (Boundaries (minr, minc) (maxr, maxc)) = - (r >= minr && r <= maxr) && (c >= minc && c <= maxc) diff --git a/src/NPC.hs b/src/NPC.hs index 520593c..842dfb8 100644 --- a/src/NPC.hs +++ b/src/NPC.hs @@ -13,7 +13,12 @@ import NanoVG hiding (V2(..)) -- internal imports +import Navigation +import Util + import Types.UserData +import Types.Interior +import Types.Map drawNPCs :: Context @@ -40,8 +45,13 @@ drawNPCs ctx npcposs prow pcol row col = do ) fnpcposs -placeNPCs :: M.Matrix (Maybe ImgId) -> Int -> Affection UserData [V2 Double] -placeNPCs imgmat count = +placeNPCs + :: M.Matrix (Maybe ImgId) + -> [ReachPoint] + -> [Graph] + -> Int + -> Affection UserData [V2 Double] +placeNPCs imgmat rp gr count = doPlace 1 [] where doPlace :: Int -> [V2 Double] -> Affection UserData [V2 Double] @@ -50,8 +60,14 @@ placeNPCs imgmat count = then do r <- liftIO $ randomRIO (1, M.nrows imgmat) c <- liftIO $ randomRIO (1, M.ncols imgmat) - if null (imgObstacle $ imgmat M.! (r, c)) + if null (imgObstacle $ imgmat M.! (r, c)) && + isReachable imgmat [V2 r c] (exits r c) then doPlace (nr + 1) ((V2 (fromIntegral r) (fromIntegral c)) : acc) else doPlace nr acc else return acc + applRooms row col = + filter (\r -> graphIsRoom r && inBounds (V2 row col) (bounds r)) gr + exits row col= concatMap (\b -> + filter (\p -> pointType p == RoomExit && inBounds (pointCoord p) b) rp + ) (map bounds (applRooms row col)) diff --git a/src/Test.hs b/src/Test.hs index 67e29e3..7ea5380 100644 --- a/src/Test.hs +++ b/src/Test.hs @@ -49,8 +49,8 @@ loadMap = do [] ((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat]) -- liftIO $ A.logIO A.Debug (show exits) - (inter, _) <- liftIO $ placeInteriorIO mat imgmat exits gr - npcposs <- placeNPCs inter 10 + (inter, rps) <- liftIO $ placeInteriorIO mat imgmat exits gr + npcposs <- placeNPCs inter rps gr 10 (nws, _) <- yieldSystemT (worldState ud) $ do void $ newEntity $ defEntity { pos = Just (V2 20.5 20.5) diff --git a/src/Types/Map.hs b/src/Types/Map.hs index 3dae8fd..10373f2 100644 --- a/src/Types/Map.hs +++ b/src/Types/Map.hs @@ -56,5 +56,9 @@ data Graph } deriving (Show, Eq) +graphIsRoom :: Graph -> Bool +graphIsRoom (GRoom _ _) = True +graphIsRoom _ = False + class Size a where size :: a -> Double diff --git a/src/Types/UserData.hs b/src/Types/UserData.hs index fde9074..96f1bf4 100644 --- a/src/Types/UserData.hs +++ b/src/Types/UserData.hs @@ -149,6 +149,7 @@ data Entity f = Entity , obstacle :: Component f 'Field (Boundaries Double) , player :: Component f 'Unique () , npc :: Component f 'Field () + , npcPath :: Component f 'Field (Maybe [V2 Int]) } deriving (Generic) diff --git a/src/Util.hs b/src/Util.hs index c5f4f73..0f792ff 100644 --- a/src/Util.hs +++ b/src/Util.hs @@ -116,3 +116,7 @@ relativizeMouseCoords (V2 ix iy) = do dx = fromIntegral rx - hx dy = fromIntegral ry - hy return $ V2 (dx / hx) (dy / hy) + +inBounds :: V2 Int -> Boundaries Int -> Bool +inBounds (V2 r c) (Boundaries (minr, minc) (maxr, maxc)) = + (r >= minr && r <= maxr) && (c >= minc && c <= maxc)