2018-04-14 09:18:37 +00:00
|
|
|
module NPC where
|
|
|
|
|
|
|
|
import Affection as A
|
|
|
|
|
|
|
|
import qualified Data.Matrix as M
|
|
|
|
import Data.Ecstasy as E
|
2018-06-04 03:29:20 +00:00
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.List (find)
|
2018-04-14 09:18:37 +00:00
|
|
|
|
2018-04-14 16:43:05 +00:00
|
|
|
import Control.Monad.IO.Class (MonadIO(..))
|
2018-05-16 14:23:23 +00:00
|
|
|
import Control.Concurrent.MVar
|
2018-06-23 22:43:09 +00:00
|
|
|
import Control.Concurrent (forkIO)
|
2018-04-14 16:43:05 +00:00
|
|
|
|
2018-04-14 09:18:37 +00:00
|
|
|
import Linear
|
|
|
|
|
|
|
|
import System.Random
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
2018-04-14 11:34:28 +00:00
|
|
|
import Util
|
|
|
|
|
2018-06-04 03:29:20 +00:00
|
|
|
import Types
|
2018-04-14 09:18:37 +00:00
|
|
|
|
2018-04-14 11:34:28 +00:00
|
|
|
placeNPCs
|
|
|
|
:: M.Matrix (Maybe ImgId)
|
2018-04-14 16:43:05 +00:00
|
|
|
-> M.Matrix TileState
|
2018-04-14 11:34:28 +00:00
|
|
|
-> [ReachPoint]
|
|
|
|
-> Int
|
2018-06-07 22:29:46 +00:00
|
|
|
-> IO [V2 Double]
|
2018-07-03 14:19:27 +00:00
|
|
|
placeNPCs imgmat tilemat rp count =
|
2018-04-14 09:18:37 +00:00
|
|
|
doPlace 1 []
|
|
|
|
where
|
2018-06-07 22:29:46 +00:00
|
|
|
doPlace :: Int -> [V2 Double] -> IO [V2 Double]
|
2018-07-03 14:19:27 +00:00
|
|
|
doPlace nr acc =
|
2018-04-14 09:18:37 +00:00
|
|
|
if nr <= count
|
|
|
|
then do
|
2018-06-07 22:29:46 +00:00
|
|
|
r <- randomRIO (1, M.nrows imgmat)
|
|
|
|
c <- randomRIO (1, M.ncols imgmat)
|
2018-07-03 00:20:17 +00:00
|
|
|
if null (maybe [] collisionObstacle $ imgmat M.! (r, c)) &&
|
2018-04-14 16:43:05 +00:00
|
|
|
tilemat M.! (r, c) == Hall
|
2018-04-14 09:18:37 +00:00
|
|
|
then doPlace (nr + 1) ((V2 (fromIntegral r) (fromIntegral c)) : acc)
|
2018-04-14 16:43:05 +00:00
|
|
|
else do
|
2018-06-07 22:29:46 +00:00
|
|
|
i <- randomRIO (0, length nonexits - 1)
|
2018-04-14 16:43:05 +00:00
|
|
|
doPlace
|
|
|
|
(nr + 1)
|
|
|
|
((fmap fromIntegral $ pointCoord (nonexits !! i)) : acc)
|
2018-04-14 09:18:37 +00:00
|
|
|
else
|
|
|
|
return acc
|
2018-04-14 16:43:05 +00:00
|
|
|
nonexits =
|
|
|
|
filter
|
|
|
|
(\p ->
|
|
|
|
pointType p /= RoomExit
|
|
|
|
)
|
|
|
|
rp
|
|
|
|
|
|
|
|
updateNPCs
|
2018-04-22 09:59:14 +00:00
|
|
|
:: MonadIO m
|
2018-04-14 16:43:05 +00:00
|
|
|
=> M.Matrix (Maybe ImgId)
|
|
|
|
-> [ReachPoint]
|
|
|
|
-> Double
|
|
|
|
-> SystemT Entity m ()
|
|
|
|
updateNPCs imgmat rp dt =
|
2018-05-17 11:06:13 +00:00
|
|
|
emap allEnts $ do
|
2018-06-23 22:43:09 +00:00
|
|
|
with npcMoveState
|
2018-04-14 16:43:05 +00:00
|
|
|
with vel
|
|
|
|
with pos
|
2018-06-04 03:29:20 +00:00
|
|
|
with rot
|
|
|
|
with anim
|
2018-06-23 22:43:09 +00:00
|
|
|
npcState' <- query npcMoveState
|
2018-04-14 16:43:05 +00:00
|
|
|
case npcState' of
|
2018-05-16 14:23:23 +00:00
|
|
|
NPCStanding ttl future -> do
|
2018-04-14 16:43:05 +00:00
|
|
|
let nttl = ttl - dt
|
|
|
|
if nttl > 0
|
|
|
|
then
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-06-23 22:43:09 +00:00
|
|
|
{ npcMoveState = Set $ NPCStanding nttl future
|
2018-04-14 16:43:05 +00:00
|
|
|
, vel = Set $ V2 0 0
|
|
|
|
}
|
|
|
|
else do
|
2018-05-16 14:23:23 +00:00
|
|
|
mpath <- liftIO $ tryTakeMVar future
|
|
|
|
case mpath of
|
|
|
|
Just path ->
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-06-23 22:43:09 +00:00
|
|
|
{ npcMoveState = Set $ NPCWalking path
|
2018-05-16 14:23:23 +00:00
|
|
|
}
|
|
|
|
Nothing ->
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-06-23 22:43:09 +00:00
|
|
|
{ npcMoveState = Set $ NPCStanding 1 future
|
2018-05-16 14:23:23 +00:00
|
|
|
}
|
2018-04-14 16:43:05 +00:00
|
|
|
NPCWalking path -> do
|
2018-05-17 11:06:13 +00:00
|
|
|
pos' <- query pos
|
2018-04-14 16:43:05 +00:00
|
|
|
if not (null path)
|
|
|
|
then do
|
|
|
|
let itarget = V2 0.5 0.5 + (fromIntegral <$> head path) :: V2 Double
|
|
|
|
if distance pos' itarget < 0.1
|
|
|
|
then
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-06-23 22:43:09 +00:00
|
|
|
{ npcMoveState = Set $ NPCWalking (tail path)
|
2018-04-14 16:43:05 +00:00
|
|
|
}
|
|
|
|
else
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-05-13 20:01:05 +00:00
|
|
|
{ vel = Set $ (* 2) <$> signorm (itarget - pos')
|
2018-04-14 16:43:05 +00:00
|
|
|
}
|
|
|
|
else do
|
|
|
|
ttl <- liftIO $ randomRIO (5, 30)
|
2018-05-16 14:23:23 +00:00
|
|
|
future <- liftIO $ newEmptyMVar
|
2018-06-04 03:29:20 +00:00
|
|
|
rot' <- query rot
|
2018-07-03 14:19:27 +00:00
|
|
|
stat <- query anim
|
2018-06-04 03:29:20 +00:00
|
|
|
let mdir =
|
|
|
|
(pointDir <$> find (\a -> pointCoord a == fmap floor pos') rp)
|
2018-06-03 02:28:39 +00:00
|
|
|
-- _ <- liftIO $ forkOS $ getPath (fmap floor pos') future rp imgmat
|
2018-06-23 22:43:09 +00:00
|
|
|
_ <- liftIO $ forkIO $ getPath (fmap floor pos') future rp imgmat
|
2018-05-17 11:06:13 +00:00
|
|
|
return $ unchanged
|
2018-06-23 22:43:09 +00:00
|
|
|
{ npcMoveState = Set $ NPCStanding ttl future
|
2018-06-04 03:29:20 +00:00
|
|
|
, vel = Set $ V2 0 0
|
|
|
|
, rot = Set $ fromMaybe rot' mdir
|
2018-07-03 14:19:27 +00:00
|
|
|
, anim = Set stat
|
|
|
|
{ asId = (asId stat)
|
2018-06-04 03:29:20 +00:00
|
|
|
{ aiDirection = fromMaybe rot' mdir
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 16:43:05 +00:00
|
|
|
}
|
2018-05-16 14:23:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
getPath
|
|
|
|
:: V2 Int
|
|
|
|
-> MVar [V2 Int]
|
|
|
|
-> [ReachPoint]
|
|
|
|
-> M.Matrix (Maybe ImgId)
|
|
|
|
-> IO ()
|
|
|
|
getPath pos' mvar rp imgmat = do
|
|
|
|
let seekRP = filter (\p -> pointType p /= RoomExit) rp
|
|
|
|
ntargeti <- randomRIO (0, length seekRP - 1)
|
|
|
|
let ntarget = pointCoord (seekRP !! ntargeti)
|
|
|
|
path = astarAppl imgmat ntarget pos'
|
|
|
|
logIO A.Verbose ("seeking path from " ++ show pos' ++ " to " ++ show ntarget)
|
|
|
|
case path of
|
|
|
|
Nothing -> getPath pos' mvar rp imgmat
|
|
|
|
Just p -> putMVar mvar p
|