From 2911514578bdba286266364c0274e3985b279b6d Mon Sep 17 00:00:00 2001 From: nek0 Date: Fri, 10 Aug 2018 22:45:32 +0200 Subject: [PATCH 1/3] initial state --- src/NPC.hs | 18 +++++++++++------- src/Object.hs | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/NPC.hs b/src/NPC.hs index 28ad51d..c58cdc9 100644 --- a/src/NPC.hs +++ b/src/NPC.hs @@ -6,7 +6,7 @@ import Affection as A import qualified Data.Matrix as M import Data.Ecstasy as E -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe, fromJust) import Data.List (find) import Control.Monad.IO.Class (MonadIO(..)) @@ -83,7 +83,7 @@ updateNPCs imgmat rp dt = do return (e, pos', rot', mdir) mapM_ (\(e, pos', rot', mdir) -> do accessibles <- getObjects pos' - moent <- eover (anEnt e) $ do + moent <- runQueryT e $ do npcState' <- query npcMoveState case npcState' of NPCStanding ttl future -> do @@ -165,21 +165,24 @@ updateNPCs imgmat rp dt = do } } }) - mapM_ (\smoent -> maybe (return()) (\(oent, npcent) -> do - [(t, s)] <- efor (anEnt oent) $ do + (\(_, ent) -> + setEntity e ent + ) (fromJust moent) + maybe (return()) (\(oent, npcent) -> do + Just (t, s) <- runQueryT oent $ do with objType with objState otyp <- query objType ostat <- query objState return (otyp, ostat) setEntity oent =<< objectTransition t s False oent - [(nt, ns)] <- efor (anEnt oent) $ do + Just (nt, ns) <- runQueryT oent $ do with objType with objState otyp <- query objType ostat <- query objState return (otyp, ostat) - emap (anEnt npcent) $ do + Just npc <- runQueryT npcent $ do let ttl = actionTime nt ns future <- liftIO $ newEmptyMVar _ <- liftIO $ forkIO $ @@ -188,7 +191,8 @@ updateNPCs imgmat rp dt = do { npcMoveState = Set $ NPCStanding ttl future , vel = Set $ V2 0 0 } - ) smoent) moent + setEntity npcent npc + ) (fromJust (fst <$> moent)) ) npcs getObjects npos = do diff --git a/src/Object.hs b/src/Object.hs index e746494..6469c26 100644 --- a/src/Object.hs +++ b/src/Object.hs @@ -3,7 +3,7 @@ {-# LANGUAGE FlexibleInstances #-} module Object where -import Affection +import Affection as A import Control.Monad (when) @@ -67,4 +67,4 @@ instance ObjectAction ObjType ObjState where instance ActionTime ObjType ObjState where actionTime ObjCopier "copying" = 5 - actionTime o s = error (show o ++ ": " ++ s ++ ": has not time") + actionTime o s = A.log Error (show o ++ ": " ++ s ++ ": has not time") 0 From 0e5486f048a7f69e2d6cbab6ac97445e3e0d1fab Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 11 Aug 2018 01:12:07 +0200 Subject: [PATCH 2/3] fix and optimization --- src/MainGame/WorldMap.hs | 17 +-- src/NPC.hs | 226 +++++++++++++++++++-------------------- 2 files changed, 119 insertions(+), 124 deletions(-) diff --git a/src/MainGame/WorldMap.hs b/src/MainGame/WorldMap.hs index c6e4175..6a826ab 100644 --- a/src/MainGame/WorldMap.hs +++ b/src/MainGame/WorldMap.hs @@ -675,13 +675,6 @@ updateMap dt = do } -- liftIO $ A.logIO A.Debug ("player position: " ++ show (pos' + colldpos)) return ent - updateNPCs - (imgMat $ stateData ud) - (Prelude.filter - (\p -> pointType p /= RoomExit) - (reachPoints $ stateData ud) - ) - dt tses <- efor allEnts $ do with objType with objState @@ -692,8 +685,16 @@ updateMap dt = do mapM_ (\(t, s, e) -> objectAction dt t s e ) tses + (nws2, _) <- yieldSystemT nws $ updateNPCs + (imgMat $ stateData ud) + (Prelude.filter + (\p -> pointType p /= RoomExit) + (reachPoints $ stateData ud) + ) + dt + nws putAffection ud - { worldState = nws + { worldState = nws2 } checkBoundsCollision2 diff --git a/src/NPC.hs b/src/NPC.hs index c58cdc9..a7903c3 100644 --- a/src/NPC.hs +++ b/src/NPC.hs @@ -6,9 +6,10 @@ import Affection as A import qualified Data.Matrix as M import Data.Ecstasy as E -import Data.Maybe (fromMaybe, fromJust) +import Data.Maybe import Data.List (find) +import Control.Monad.Trans import Control.Monad.IO.Class (MonadIO(..)) import Control.Concurrent.MVar import Control.Concurrent (forkIO) @@ -61,141 +62,129 @@ updateNPCs :: M.Matrix (Maybe ImgId) -> [ReachPoint] -> Double + -> SystemState Entity (AffectionState (AffectionData UserData) IO) -> SystemT Entity (AffectionState (AffectionData UserData) IO) () -updateNPCs imgmat rp dt = do +updateNPCs imgmat rp dt ws = do posbounds <- efor allEnts $ do with pos with obstacle pos' <- query pos bnds <- query obstacle return (pos', bnds) - npcs <- efor allEnts $ do + npcposs <- efor allEnts $ do + with pos with npcMoveState with vel + with rot + with anim + pos' <- query pos + e <- queryEnt + return (e, pos') + eaccess <- getObjects npcposs + moent <- catMaybes <$> (eover allEnts $ do with pos + with npcMoveState + with vel with rot with anim pos' <- query pos rot' <- query rot - let mdir = + let mdir = (pointDir <$> find (\a -> pointCoord a == fmap floor pos') rp) e <- queryEnt - return (e, pos', rot', mdir) - mapM_ (\(e, pos', rot', mdir) -> do - accessibles <- getObjects pos' - moent <- runQueryT e $ do - npcState' <- query npcMoveState - case npcState' of - NPCStanding ttl future -> do - let nttl = ttl - dt - if nttl > 0 + npcState' <- query npcMoveState + case npcState' of + NPCStanding ttl future -> do + let nttl = ttl - dt + if nttl > 0 + then + return $ (Nothing, unchanged + { npcMoveState = Set $ NPCStanding nttl future + , vel = Set $ V2 0 0 + }) + else do + mpath <- liftIO $ tryTakeMVar future + case mpath of + Just path -> + return $ (Nothing, unchanged + { npcMoveState = Set $ NPCWalking path + }) + Nothing -> + return $ (Nothing, unchanged + { npcMoveState = Set $ NPCStanding 1 future + }) + NPCWalking path -> do + pos' <- query pos + 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 return $ (Nothing, unchanged - { npcMoveState = Set $ NPCStanding nttl future - , vel = Set $ V2 0 0 + { npcMoveState = Set $ NPCWalking (tail path) }) - else do - mpath <- liftIO $ tryTakeMVar future - case mpath of - Just path -> - return $ (Nothing, unchanged - { npcMoveState = Set $ NPCWalking path - }) - Nothing -> - return $ (Nothing, unchanged - { npcMoveState = Set $ NPCStanding 1 future - }) - NPCWalking path -> do - pos' <- query pos - 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 + else + return $ (Nothing, unchanged + { vel = Set $ (* 2) <$> signorm (itarget - pos') + }) + else do + future <- liftIO $ newEmptyMVar + stat <- query anim + _ <- liftIO $ forkIO $ + getPath (fmap floor pos') future rp imgmat posbounds + e <- queryEnt + let mdir = + (pointDir <$> find (\a -> pointCoord a == fmap floor pos') rp) + accessibles = fromMaybe [] $ lookup e eaccess + case accessibles of + [] -> do + ttl <- liftIO $ randomRIO (5, 30) return $ (Nothing, unchanged - { npcMoveState = Set $ NPCWalking (tail path) - }) - else - return $ (Nothing, unchanged - { vel = Set $ (* 2) <$> signorm (itarget - pos') - }) - else do - future <- liftIO $ newEmptyMVar - stat <- query anim - rot' <- query rot - pos' <- query pos - _ <- liftIO $ forkIO $ - getPath (fmap floor pos') future rp imgmat posbounds - let mdir = - (pointDir <$> find (\a -> pointCoord a == fmap floor pos') rp) - -- ttl <- liftIO $ randomRIO (5, 30) - -- return $ (Nothing, unchanged - -- { npcMoveState = Set $ NPCStanding ttl future - -- , vel = Set $ V2 0 0 - -- , rot = Set $ fromMaybe rot' mdir - -- , anim = Set stat - -- { asId = (asId stat) - -- { aiDirection = fromMaybe rot' mdir - -- } - -- } - -- }) - case accessibles of - [] -> do - ttl <- liftIO $ randomRIO (5, 30) - return $ (Nothing, unchanged - { npcMoveState = Set $ NPCStanding ttl future - , vel = Set $ V2 0 0 - , rot = Set $ fromMaybe rot' mdir - , anim = Set stat - { asId = (asId stat) - { aiDirection = fromMaybe rot' mdir - } + { npcMoveState = Set $ NPCStanding ttl future + , vel = Set $ V2 0 0 + , rot = Set $ fromMaybe rot' mdir + , anim = Set stat + { asId = (asId stat) + { aiDirection = fromMaybe rot' mdir } - }) - objects -> do - liftIO $ logIO Debug ("applicable objects: " ++ show objects) - rind <- liftIO $ randomRIO (0, length objects - 1) - npcent <- queryEnt - let (oent, _, _) = objects !! rind - return (Just (oent, npcent), unchanged - { rot = Set $ fromMaybe rot' mdir - , anim = Set stat - { asId = (asId stat) - { aiDirection = fromMaybe rot' mdir - } + } + }) + objects -> do + liftIO $ logIO Debug ("applicable objects: " ++ show objects) + rind <- liftIO $ randomRIO (0, length objects - 1) + npcent <- queryEnt + let (oent, _, _) = objects !! rind + return (Just (oent, npcent, future), unchanged + { rot = Set $ fromMaybe rot' mdir + , anim = Set stat + { asId = (asId stat) + { aiDirection = fromMaybe rot' mdir } - }) - (\(_, ent) -> - setEntity e ent - ) (fromJust moent) - maybe (return()) (\(oent, npcent) -> do - Just (t, s) <- runQueryT oent $ do - with objType - with objState - otyp <- query objType - ostat <- query objState - return (otyp, ostat) - setEntity oent =<< objectTransition t s False oent - Just (nt, ns) <- runQueryT oent $ do - with objType - with objState - otyp <- query objType - ostat <- query objState - return (otyp, ostat) - Just npc <- runQueryT npcent $ do - let ttl = actionTime nt ns - future <- liftIO $ newEmptyMVar - _ <- liftIO $ forkIO $ - getPath (fmap floor pos') future rp imgmat posbounds - return unchanged - { npcMoveState = Set $ NPCStanding ttl future - , vel = Set $ V2 0 0 - } - setEntity npcent npc - ) (fromJust (fst <$> moent)) - ) npcs + } + , vel = Set $ V2 0 0 + })) + mapM_ (\(oent, npcent, future) -> do + Just (t, s) <- runQueryT oent $ do + with objType + with objState + otyp <- query objType + ostat <- query objState + return (otyp, ostat) + setEntity oent =<< objectTransition t s False oent + Just (nt, ns) <- runQueryT oent $ do + with objType + with objState + otyp <- query objType + ostat <- query objState + return (otyp, ostat) + emap (anEnt npcent) $ do + let ttl = actionTime nt ns + return unchanged + { npcMoveState = Set $ NPCStanding ttl future + } + ) moent -getObjects npos = do +getObjects npcposs = do candidates <- efor allEnts $ do with pos with objType @@ -205,9 +194,14 @@ getObjects npos = do oacc <- query objAccess ent <- queryEnt return (ent, pos', oacc) - return $ filter (\(_, p, (delta, _)) -> - fmap floor p + delta == fmap floor npos - ) candidates + mapM (\(e, pos) -> + return + ( e + , filter (\(_, p, (delta, _)) -> + fmap floor p + delta == fmap floor pos + ) candidates + ) + ) npcposs getPath :: V2 Int From d8437759fc2d6713cc64393ffe9aaee2536cd7b8 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 11 Aug 2018 02:00:13 +0200 Subject: [PATCH 3/3] some little stuff --- src/MainGame/WorldMap.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MainGame/WorldMap.hs b/src/MainGame/WorldMap.hs index 6a826ab..5a7155e 100644 --- a/src/MainGame/WorldMap.hs +++ b/src/MainGame/WorldMap.hs @@ -85,7 +85,7 @@ loadMapFork ud ad future progress = do fc = FloorConfig (10, 10) [] -- [(5, 5), (5, 20)] - (30, 50) + (40, 40) _ <- liftIO $ swapMVar progress (1 / loadSteps, "Building floor") (mat, gr) <- buildHallFloorIO fc progress (1 / loadSteps) _ <- liftIO $ swapMVar progress (11 / loadSteps, "Converting to images") @@ -102,7 +102,7 @@ loadMapFork ud ad future progress = do _ <- liftIO $ swapMVar progress (13 / loadSteps, "Placing NPCs") logIO A.Debug ("number of reachpoints: " ++ show (length rps)) let nnex = Prelude.filter (\p -> pointType p /= RoomExit) rps - npcposs <- placeNPCs inter mat rps 25 -- (length $ filter (\a -> pointType a == Table) nnex) + npcposs <- placeNPCs inter mat rps 40 -- (length $ filter (\a -> pointType a == Table) nnex) _ <- liftIO $ swapMVar progress (14 / loadSteps, "Preparing MindMap graph") A.logIO A.Debug $ "number of placed NPCs: " ++ show (length npcposs) !(mmintmat, mmgraph) <- buildFloorMap . springField <$>