tracer/src/MainGame/WorldMap.hs

893 lines
32 KiB
Haskell
Raw Normal View History

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
2018-07-19 04:56:31 +02:00
{-# LANGUAGE BangPatterns #-}
2018-06-28 21:07:58 +02:00
module MainGame.WorldMap where
2018-05-30 17:32:00 +02:00
import Affection as A
2018-09-13 00:51:22 +02:00
import Algebra.Graph as AG hiding (Context(..))
2018-06-28 21:07:58 +02:00
2018-02-18 03:11:41 +01:00
import qualified SDL
2018-03-12 00:21:16 +01:00
import NanoVG hiding (V2(..))
2018-02-18 03:11:41 +01:00
2018-05-30 17:32:00 +02:00
import Control.Monad (when, void)
2018-08-10 08:58:26 +02:00
import Control.Monad.State.Strict (evalStateT)
2022-08-04 18:13:53 +02:00
import Control.Monad.Trans.Resource (ResIO, runResourceT)
import Control.Concurrent.MVar
2018-06-08 00:29:46 +02:00
import Control.Concurrent (forkIO)
2019-03-07 01:33:51 +01:00
import Control.Parallel.Strategies hiding (dot)
2018-02-25 10:30:13 +01:00
import Data.Map.Strict as Map
2018-03-01 23:33:08 +01:00
import qualified Data.Set as S
import qualified Data.Text as T
2018-03-03 11:06:38 +01:00
import Data.Matrix as M
2018-02-18 05:31:34 +01:00
import Data.Ecstasy as E
2018-03-04 22:24:30 +01:00
import Data.Maybe
2019-03-09 20:36:21 +01:00
import Data.List as L
2019-10-20 10:53:53 +02:00
import qualified Data.Vector as V
2019-10-28 18:20:34 +01:00
import Data.String
2018-04-14 18:43:05 +02:00
import System.Random (randomRIO)
2018-05-26 08:34:49 +02:00
import Linear hiding (E)
2018-02-18 03:11:41 +01:00
import Foreign.C.Types (CFloat(..))
2018-03-02 02:10:35 +01:00
-- internal imports
2018-03-03 17:03:17 +01:00
import Interior
2018-03-02 02:10:35 +01:00
import Util
2018-03-12 00:21:16 +01:00
import Types
import Floorplan
2018-06-28 21:07:58 +02:00
import MindMap
2018-04-14 11:18:37 +02:00
import NPC
2018-07-30 14:34:46 +02:00
import Animation
import Collision
2018-03-02 02:10:35 +01:00
2020-05-05 10:26:16 +02:00
loadMap :: UserData -> Affection ()
loadMap ud = do
2018-08-10 08:58:26 +02:00
ad <- get
2019-03-28 20:59:31 +01:00
let (Subsystems _ _ k j t) = subsystems ud
2020-05-05 10:26:16 +02:00
uu0 <- partSubscribe k (emitKbdActionMessage ud)
uu1 <- partSubscribe j (emitJoyActionMessage ud)
uu2 <- partSubscribe t (movePlayer2 ud)
uu3 <- partSubscribe t (playerInteract2 ud)
uu4 <- partSubscribe t (changeMaps2 ud)
void $ liftIO $ swapMVar (stateProgress ud) (0, "Ohai!")
_ <- liftIO $ forkIO $ loadMapFork ud ad (stateMVar ud) (stateProgress ud)
void $ liftIO $ swapMVar (stateData ud) None
void $ liftIO $ swapMVar (uuid ud) [ uu0, uu1, uu2, uu3, uu4 ]
void $ liftIO $ swapMVar (state ud) (Main WorldMap)
2018-06-08 00:29:46 +02:00
2020-05-05 10:26:16 +02:00
changeMaps :: UserData -> KeyboardMessage -> Affection ()
changeMaps ud (MsgKeyboardEvent _ _ SDL.Pressed False sym)
2018-06-28 21:07:58 +02:00
| SDL.keysymKeycode sym == SDL.KeycodeF1 = do
2020-05-05 10:26:16 +02:00
curState <- liftIO $ readMVar (state ud)
case curState of
2019-02-12 00:11:53 +01:00
Main MindMap ->
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (state ud) (Main WorldMap)
2019-02-12 00:11:53 +01:00
Main WorldMap ->
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (state ud) (Main MindMap)
2019-02-12 00:11:53 +01:00
_ -> return ()
2018-06-28 21:07:58 +02:00
| otherwise = return ()
2020-05-05 10:26:16 +02:00
changeMaps _ _ = return ()
2018-06-28 21:07:58 +02:00
2020-05-05 10:26:16 +02:00
changeMaps2 :: UserData -> ActionMessage -> Affection ()
changeMaps2 ud (ActionMessage ActSwitchMap _) = do
curState <- liftIO $ readMVar (state ud)
case curState of
2018-10-13 20:12:10 +02:00
Main MindMap ->
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (state ud) (Main WorldMap)
2018-10-13 20:12:10 +02:00
Main WorldMap ->
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (state ud) (Main MindMap)
2018-10-13 20:12:10 +02:00
_ -> return ()
2020-05-05 10:26:16 +02:00
changeMaps2 _ _ = return ()
2018-10-13 20:12:10 +02:00
loadMapFork
:: UserData
2020-05-05 10:26:16 +02:00
-> AffectionData
2022-08-04 18:13:53 +02:00
-> MVar (SystemState Entity (AffectionState AffectionData ResIO), StateData)
2018-07-19 04:51:07 +02:00
-> MVar (Float, T.Text)
-> IO ()
2018-08-10 08:58:26 +02:00
loadMapFork ud ad future progress = do
2019-02-14 22:31:00 +01:00
let loadSteps = 23
2018-09-07 19:12:04 +02:00
increment = 1 / loadSteps
fc = FloorConfig
2018-09-08 14:05:07 +02:00
(V2 10 10)
[(V2 5 5), (V2 5 20)]
2020-01-26 18:07:40 +01:00
(40, 40)
2018-09-07 19:12:04 +02:00
modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Building floor"
)))
2019-02-07 05:23:44 +01:00
(mat, gr) <- buildHallFloorIO fc progress increment -- 11 increments inside
2018-09-07 19:12:04 +02:00
modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Converting to images"
)))
2018-09-16 23:29:02 +02:00
let !imgmat = convertTileToImg mat
2019-02-17 15:24:49 +01:00
!doors = Prelude.foldl
(\acc coord@(r, c) -> if mat M.! coord == Door
then V2 r c : acc
else acc
)
[]
((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat])
2018-09-16 23:29:02 +02:00
!exits = Prelude.foldl
2018-07-21 20:37:01 +02:00
(\acc coord@(r, c) -> if imgmat M.! coord == Just ImgEmpty
then ReachPoint RoomExit (V2 r c) NE 0 : acc
else acc
)
[]
((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat])
2018-09-07 19:12:04 +02:00
modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Placing furniture"
)))
2019-10-20 10:53:53 +02:00
(!inter, !rawrps) <- placeInteriorIO mat imgmat exits (V.toList gr)
let !rps = ReachPoint Elevator (fcElevator fc) SE 0 : rawrps
2018-09-07 19:12:04 +02:00
modifyMVar_ progress (return . (\(p, _) ->
( p + increment
2018-09-07 19:49:16 +02:00
, "Creating WorldState"
2018-09-07 19:12:04 +02:00
)))
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
2022-08-04 18:13:53 +02:00
(nws, mmimgmat) <- runResourceT $ evalStateT (runState (yieldSystemT wState $ do
2018-09-07 19:12:04 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Registering copiers into WorldState"
)))
2018-09-16 23:29:02 +02:00
let !copiers = Prelude.filter (\a -> pointType a == Copier) rps
mapM_ (\(ReachPoint _ icoord _ _) -> do
2018-07-21 06:43:26 +02:00
let reachCoord = fmap ((+ 0.5) . fromIntegral) icoord
void $ createEntity $ newEntity
{ pos = Just $ reachCoord - V2 1 0
, obstacle = Just $ Boundaries (10/36, 8/36) (28/36, 30/36)
2019-02-14 22:31:00 +01:00
, anim = Just $ AnimState (AnimId AnimCopier "open" N) 0 0
2019-02-16 20:38:00 +01:00
, objAccess = Just [(V2 1 0, NW)]
, objType = Just ObjCopier
2018-08-10 08:58:26 +02:00
, objState = Just "idle"
2018-07-21 06:43:26 +02:00
}
2019-10-28 18:20:34 +01:00
)
(A.log
A.Debug
("number of copiers: " <> fromString (show $ length copiers))
copiers
)
2018-09-07 19:12:04 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Registering computers into WorldState"
)))
2019-02-14 22:31:00 +01:00
let !computers = Prelude.filter ((Computer ==) . pointType) rps
mapM_ (\(ReachPoint _ icoord dir _) -> do
2018-07-30 14:34:46 +02:00
let reachCoord = fmap ((+ 0.5) . fromIntegral) icoord
2019-02-09 15:19:16 +01:00
access = case dir of
N -> V2 1 (-1)
NE -> V2 0 (-1)
2019-02-09 22:39:42 +01:00
NW -> V2 1 0
x -> error ("computer placement " ++ show x ++ " not defined")
2018-09-08 14:05:07 +02:00
void $ createEntity $ newEntity
2019-02-09 15:19:16 +01:00
{ pos = Just $ reachCoord - fmap fromIntegral access
2019-02-14 22:31:00 +01:00
, anim = Just $ AnimState (AnimId AnimComputer "off" dir) 0 0
2019-02-16 20:38:00 +01:00
, rot = Just dir
, objAccess = Just [(access, dir)]
2018-07-30 14:34:46 +02:00
, objType = Just ObjComputer
2018-08-11 11:51:20 +02:00
, objState = Just "off"
2018-07-30 14:34:46 +02:00
}
2019-10-28 18:20:34 +01:00
)
(A.log
A.Debug
("number of computers: " <> fromString (show $ length computers))
computers
)
2018-09-07 19:12:04 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Registering toilets into WorldState"
)))
2018-09-16 23:29:02 +02:00
let !toilets = Prelude.filter (\a -> pointType a == Toilet) rps
mapM_ (\(ReachPoint _ icoord dir _) -> do
2018-07-31 22:59:25 +02:00
let reachCoord = fmap ((+ 0.5) . fromIntegral) icoord
void $ createEntity $ newEntity
{ pos = Just $ reachCoord - V2 0 (-1)
, obstacle = Just $ Boundaries (0, 0) (1, 1)
2019-02-14 22:31:00 +01:00
, anim = Just $ AnimState (AnimId AnimToilet "free" N) 0 0
2019-02-16 20:38:00 +01:00
, objAccess = Just [(V2 0 (-1), dir)]
2018-07-31 22:59:25 +02:00
, objType = Just ObjToilet
}
2019-10-28 18:20:34 +01:00
)
(A.log
A.Debug
("number of toilets: " <> fromString (show $ length toilets))
toilets
)
2018-09-07 19:49:16 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Preparing MindMap graph"
)))
(mmintmat, mmgraph) <- liftIO $ buildFloorMap . springField <$>
2018-09-13 00:51:22 +02:00
buildMindMap (length computers) 2
2018-09-07 19:49:16 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Unfolding and Converting MindMap to images"
)))
let !mmimgmat = convertTileToImg $ manhattan mmgraph mmintmat
2018-09-16 23:29:02 +02:00
!pmmpos = (+ 0.5) . (fromIntegral :: Int -> Double) . floor <$> mmPos
2018-09-07 19:49:16 +02:00
(fromJust $ find (\a -> mmId a == 0) (vertexList mmgraph))
2018-09-16 23:29:02 +02:00
!delta = (0, 0) :
2018-09-07 19:49:16 +02:00
Prelude.filter (/= (0,0)) ((,) <$> [-1 .. 1] <*> [-1 .. 1]) :: [(Int, Int)]
2018-09-16 23:29:02 +02:00
!mmmpos = Prelude.foldl (\acc (dr, dc) ->
2018-09-07 19:49:16 +02:00
let (V2 pmr pmc) = floor <$> pmmpos
seekpos = (pmr + fromIntegral dr, pmc + fromIntegral dc)
in if isNothing (mmimgmat M.! seekpos) && mmintmat M.! seekpos == 0
&& isNothing acc
then Just (pmmpos + (fromIntegral <$> V2 dr dc))
else acc
) Nothing delta
void $ createEntity $ newEntity
{ pos = Just (V2 10.5 10.5)
, mmpos = mmmpos
, vel = Just (V2 0 0)
2018-10-13 00:07:19 +02:00
, xyvel = Just (V2 0 0)
2018-09-07 19:49:16 +02:00
, mmvel = Just (V2 0 0)
, player = Just ()
, rot = Just SE
2019-02-16 20:38:00 +01:00
, clearanceLvl = Just 0
2019-02-14 22:31:00 +01:00
, anim = Just $ AnimState (AnimId AnimIntruder "standing" SE) 0 0
2018-09-07 19:49:16 +02:00
}
2019-10-28 18:20:34 +01:00
liftIO $ A.logIO
A.Debug
("number of placed NPCs: " <> fromString (show $ length computers))
2018-09-07 19:12:04 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
( p + increment
, "Registering NPCs into WorldState"
)))
mapM_ (\cpr -> do
2018-05-15 19:27:40 +02:00
fact <- liftIO $ randomRIO (0.5, 1.5)
2018-09-07 23:39:53 +02:00
stats <- liftIO $ NPCStats
<$> (randomRIO (0, 1))
2019-03-07 01:33:51 +01:00
<*> pure 0
2018-09-07 23:39:53 +02:00
<*> (randomRIO (0, 1))
<*> (randomRIO (0, 1))
<*> (randomRIO (0, 1))
<*> (randomRIO (0, 1))
2020-01-26 18:07:40 +01:00
police <- liftIO $ randomRIO (0 :: Int, 9 :: Int)
2019-10-20 10:53:53 +02:00
let room = V.head
(V.filter
((inBounds $ pointCoord cpr) . bounds)
2019-10-20 10:53:53 +02:00
(V.fromList $
V.toList (Types.connects (V.head gr)) ++ (V.toList (V.tail gr)))
)
2018-05-30 17:32:00 +02:00
void $ createEntity $ newEntity
2019-02-14 22:31:00 +01:00
{ pos = Just (fmap ((+ 0.5) . fromIntegral) (pointCoord cpr))
, vel = Just (V2 0 0)
, velFact = Just fact
, rot = Just SE
, npcMoveState = Just (NPCWalking [[pointCoord cpr]])
2019-02-14 22:31:00 +01:00
, npcWorkplace = Just cpr
, npcActionState = Just ASWork
, npcStats = Just stats
, clearanceLvl = Just (clearance room)
2020-01-26 18:07:40 +01:00
, anim = Just $ AnimState
(AnimId (if police == 0 then AnimPoliceM else AnimJDoeM) "standing" SE) 0 0
2018-04-14 11:18:37 +02:00
}
2018-09-13 00:51:22 +02:00
) computers
2018-09-07 19:12:04 +02:00
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
2019-02-14 22:31:00 +01:00
( p + increment
, "Registering doors into WorldState"
)))
mapM_ (\door -> do
2019-10-20 10:53:53 +02:00
let rooms = V.foldl
2019-02-14 22:31:00 +01:00
(\acc coord ->
2019-10-20 10:53:53 +02:00
let rs = V.filter ((inBounds coord) . bounds) graph
2019-02-14 22:31:00 +01:00
in
2019-10-20 10:53:53 +02:00
if not (V.null rs)
then (coord, V.head rs) `V.cons` acc
2019-02-14 22:31:00 +01:00
else acc
)
2019-10-20 10:53:53 +02:00
V.empty
(V.fromList coords)
graph = V.fromList $
(V.toList $ Types.connects (V.head gr)) ++ (V.toList $ V.tail gr)
2019-02-17 15:24:49 +01:00
coords = Prelude.map (door +) deltas
2019-02-14 22:31:00 +01:00
deltas =
[ V2 0 1
, V2 1 0
, V2 (-1) 0
, V2 0 (-1)
]
wall = Prelude.filter
2019-03-28 20:59:31 +01:00
(\ddelta ->
let V2 r c = door + ddelta
2019-02-14 22:31:00 +01:00
in
fromMaybe False (isWall <$> imgmat M.! (r, c)))
deltas
orientation
2019-02-16 20:38:00 +01:00
| head wall == V2 0 1 || head wall == V2 0 (-1) = NW
| head wall == V2 1 0 || head wall == V2 (-1) 0 = NE
2019-10-28 18:20:34 +01:00
| otherwise = error ("strange wall: " ++ show wall)
2019-02-14 22:31:00 +01:00
void $ createEntity $ newEntity
2019-02-17 15:24:49 +01:00
{ pos = Just (fmap ((+ 0.5) . fromIntegral) door)
2019-10-20 10:53:53 +02:00
, clearanceLvl = Just (V.maximum $
0 `V.cons` V.map clearance (V.map snd rooms))
2019-02-16 20:38:00 +01:00
, anim = Just $ AnimState (AnimId AnimDoor0 "shut" orientation) 0 1
2019-02-16 02:21:07 +01:00
, obstacle = Just $ case orientation of
2019-02-16 20:38:00 +01:00
NW -> Boundaries (4/9, 0) (5/9, 1)
NE -> Boundaries (0, 4/9) (1, 5/9)
2019-02-16 02:21:07 +01:00
_ -> error "strange orientation for door"
, ignoreObstacle = Just ()
2019-02-16 20:38:00 +01:00
, rot = Just orientation
, objAccess = Just $ case orientation of
NW ->
[ ((V2 (-1) 0), SE)
, ((V2 1 0), NW)
]
NE ->
[ ((V2 0 1), SW)
, ((V2 0 (-1)), NE)
]
_ -> error "strange orientation for door"
, objType = Just ObjDoor
, objState = Just "shut"
2019-02-14 22:31:00 +01:00
}
) doors
liftIO $ modifyMVar_ progress (return . (\(p, _) ->
2018-09-07 19:12:04 +02:00
( p + increment
, "Handing over"
)))
2018-09-07 19:49:16 +02:00
return mmimgmat
2018-08-10 08:58:26 +02:00
)) ad
2018-09-16 23:29:02 +02:00
let !retMat = M.fromList (nrows inter) (ncols inter) $
2018-03-31 23:22:10 +02:00
Prelude.map
2018-07-21 20:37:01 +02:00
(\a -> if a == Just ImgEmpty || a == Just ImgEmptyNoWalk
then Nothing
else a)
2018-03-31 23:22:10 +02:00
(M.toList inter)
2020-05-05 10:26:16 +02:00
void $ putMVar future (nws, MainData
2018-09-15 19:22:04 +02:00
{ mapMat = mat
, imgMat = retMat
2019-10-20 10:53:53 +02:00
, reachPoints = V.fromList rps
2018-06-28 21:07:58 +02:00
, mmImgMat = mmimgmat
, roomGraph = gr
2018-06-08 00:29:46 +02:00
})
2020-05-05 10:26:16 +02:00
movePlayerKbd :: UserData -> KeyboardMessage -> Affection ()
movePlayerKbd ud (MsgKeyboardEvent _ _ press False sym)
2019-02-12 00:11:53 +01:00
| SDL.keysymKeycode sym == SDL.KeycodeW = do
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $
2019-02-12 00:11:53 +01:00
emap allEnts $ do
with player
(V2 vx _) <- query xyvel
let ry = if (press == SDL.Pressed)
then 1
else 0
return $ unchanged
{ xyvel = Set $ V2 vx ry
}
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
2019-02-12 00:11:53 +01:00
| SDL.keysymKeycode sym == SDL.KeycodeS = do
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $
2019-02-12 00:11:53 +01:00
emap allEnts $ do
with player
(V2 vx _) <- query xyvel
let ry = if (press == SDL.Pressed)
then -1
else 0
return $ unchanged
{ xyvel = Set $ V2 vx ry
}
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
2019-02-12 00:11:53 +01:00
| SDL.keysymKeycode sym == SDL.KeycodeA = do
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $
2019-02-12 00:11:53 +01:00
emap allEnts $ do
with player
(V2 _ vy) <- query xyvel
let rx = if (press == SDL.Pressed)
then -1
else 0
return $ unchanged
{ xyvel = Set $ V2 rx vy
}
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
2019-02-12 00:11:53 +01:00
| SDL.keysymKeycode sym == SDL.KeycodeD = do
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $
2019-02-12 00:11:53 +01:00
emap allEnts $ do
with player
(V2 _ vy) <- query xyvel
let rx = if (press == SDL.Pressed)
then 1
else 0
return $ unchanged
{ xyvel = Set $ V2 rx vy
}
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
2019-02-12 00:11:53 +01:00
| otherwise = return ()
2020-05-05 10:26:16 +02:00
movePlayerKbd _ _ = return ()
2018-02-18 03:11:41 +01:00
2020-05-05 10:26:16 +02:00
movePlayer2 :: UserData -> ActionMessage -> Affection ()
movePlayer2 ud (ActionMessage mov _) = do
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $
2018-10-13 00:07:19 +02:00
emap allEnts $ do
with player
2019-02-12 00:11:53 +01:00
V2 vx vy <- query xyvel
2018-10-13 00:07:19 +02:00
return $ unchanged
2019-02-12 00:11:53 +01:00
{ xyvel = Set $ case mov of
ActUp f -> V2 vx (-f)
ActDown f -> V2 vx f
ActLeft f -> V2 (-f) vy
ActRight f -> V2 f vy
_ -> V2 vx vy
2018-10-13 00:07:19 +02:00
}
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
2018-10-13 00:07:19 +02:00
2020-05-05 10:26:16 +02:00
playerInteract2 :: UserData -> ActionMessage -> Affection ()
playerInteract2 ud (ActionMessage ActActivate _) = do
wState <- liftIO $ readMVar (worldState ud)
(nws, _) <- yieldSystemT wState $ do
2019-01-06 03:52:43 +01:00
pdata <- efor allEnts $ do
2018-10-13 00:07:19 +02:00
with player
with pos
with rot
pos' <- query pos
rot' <- query rot
ent <- queryEnt
return (pos', rot', ent)
2019-01-06 03:52:43 +01:00
let (ppos, pdir, pent) = head pdata
2018-10-13 00:07:19 +02:00
mrelEnts <- efor allEnts $ do
with pos
with objAccess
with objType
with objState
2019-02-16 20:38:00 +01:00
reldirs <- query objAccess
2018-10-13 00:07:19 +02:00
pos' <- query pos
otype <- query objType
ostate <- query objState
ent <- queryEnt
2019-02-16 20:38:00 +01:00
if any (\(rel, dir) -> ((fmap floor ppos :: V2 Int) == (fmap floor pos' :: V2 Int) ||
2018-10-13 00:07:19 +02:00
(fmap floor ppos :: V2 Int) == (fmap floor pos' :: V2 Int) + rel) &&
2019-02-16 20:38:00 +01:00
pdir == dir) reldirs
2018-10-13 00:07:19 +02:00
then return $ Just (otype, ostate, ent)
else return Nothing
let relEnts = catMaybes mrelEnts
2019-10-28 18:20:34 +01:00
liftIO $ A.logIO A.Debug ("relEnts: " <> fromString (show relEnts))
2018-10-13 00:07:19 +02:00
-- liftIO $ A.logIO A.Debug ("dV2: " ++ show (V2 dr dc))
mapM_ (\(t, s, e) ->
setEntity e =<< objectTransition t s True e (Just pent)
) relEnts
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
playerInteract2 _ _ = return ()
2018-10-13 00:07:19 +02:00
2020-05-05 10:26:16 +02:00
drawMap :: UserData -> Affection ()
drawMap ud = do
sData <- liftIO $ readMVar (stateData ud)
wState <- liftIO $ readMVar (worldState ud)
2018-06-08 00:29:46 +02:00
let ctx = nano ud
2020-05-05 10:26:16 +02:00
case sData of
2018-06-08 00:29:46 +02:00
None -> liftIO $ do
progress <- readMVar (stateProgress ud)
drawLoadScreen ud progress
2018-06-08 00:29:46 +02:00
_ -> do
dt <- getDelta
2020-05-05 10:26:16 +02:00
(_, dat) <- yieldSystemT wState $ do
2019-02-21 17:22:38 +01:00
efor allEnts $ do
with pos
pos' <- query pos
2019-03-28 20:59:31 +01:00
player' <- queryMaybe player
2019-02-21 17:22:38 +01:00
stat <- queryMaybe anim
mbnds <- queryMaybe obstacle
t <- queryMaybe objType
s <- queryMaybe objState
pa <- queryMaybe objPlayerActivated
ttl <- queryMaybe objStateTime
let maxt = fromMaybe 1 (actionTime <$> t <*> s)
2019-03-28 20:59:31 +01:00
first = if isJust player'
2019-02-21 17:22:38 +01:00
then Just
pos'
else Nothing
secnd = if isJust stat
then Just
( pos'
, fromJust stat
, mbnds
)
else Nothing
third = if isJust t && isJust s
then Just
( pos'
, fromMaybe False pa
, realToFrac (1 - fromMaybe 0 ttl / maxt)
)
else Nothing
return (first, secnd, third)
let ((playerPos:_), posanims, posActions) = Prelude.foldl
(\(amppo, ampan, ampac) (mppo, mpan, mpac) ->
( if isJust mppo then fromJust mppo : amppo else amppo
, if isJust mpan then fromJust mpan : ampan else ampan
, if isJust mpac then fromJust mpac : ampac else ampac
)
)
([], [], [])
dat
V2 pr pc = playerPos
2020-05-05 10:26:16 +02:00
MainData _ _ _ _ gr = sData
2019-10-20 10:53:53 +02:00
seekGraph = V.foldl V.snoc (Types.connects (V.head gr)) (V.tail gr)
room = V.filter (inBounds (fmap floor playerPos) . bounds) seekGraph
2020-05-05 10:26:16 +02:00
mat = imgMat sData
2018-06-08 00:29:46 +02:00
cols = fromIntegral (ncols mat)
rows = fromIntegral (nrows mat)
tileWidth = 64 :: Double
tileHeight = 32 :: Double
x = realToFrac $ 640 + ((1 - pc) + (1 - pr)) * (tileWidth / 2)
y = realToFrac $ 360 + ((1 - pr) - (1 - pc)) * (tileHeight / 2)
partposanims = M.fromList
2020-05-05 10:26:16 +02:00
(nrows $ mapMat sData)
(ncols $ mapMat sData)
2018-06-08 00:29:46 +02:00
((reverse . fst)
(Prelude.foldl
(\(done, proc) coord ->
let (ndone, nproc) = processList proc coord
in (ndone : done, nproc)
)
([], posanims)
((,)
2020-05-05 10:26:16 +02:00
<$> [1 .. (nrows $ mapMat sData)]
<*> [1 .. (ncols $ mapMat sData)]
2018-06-08 00:29:46 +02:00
)
)
2018-06-03 04:28:39 +02:00
)
2018-06-08 00:29:46 +02:00
processList
2018-07-21 06:43:26 +02:00
:: [(V2 Double, AnimState, Maybe (Boundaries Double))]
2018-06-08 00:29:46 +02:00
-> (Int, Int)
2018-07-21 06:43:26 +02:00
-> ( [(V2 Double, AnimState, Maybe (Boundaries Double))]
, [(V2 Double, AnimState, Maybe (Boundaries Double))]
)
2018-07-03 16:19:27 +02:00
processList list (r, c) =
2018-07-21 06:43:26 +02:00
let delimiter (V2 nr nc, _, _) =
2018-06-08 00:29:46 +02:00
floor nr == r && floor nc == c
in L.partition delimiter list
2018-09-08 21:40:05 +02:00
liftIO $ do
2018-06-08 00:29:46 +02:00
beginPath ctx
moveTo ctx (x + realToFrac tileWidth / 2) y
lineTo ctx
(x + cols * (realToFrac tileWidth / 2))
(y - (realToFrac tileHeight / 2) * (cols - 1))
lineTo ctx
(x + (realToFrac tileWidth / 2) * (cols + rows - 1))
(y + (rows - cols) * (realToFrac tileHeight / 2))
lineTo ctx
(x + (realToFrac tileWidth / 2) * rows)
(y + (realToFrac tileHeight / 2) * (rows - 1))
closePath ctx
fillColor ctx (rgb 255 255 255)
fill ctx
2019-03-07 15:13:40 +01:00
let coordList = concatMap
(\(i, ls) -> Prelude.map
(\(j, t) -> ((i, j), t))
(reverse $ zip [1..] ls)
)
(zip [1..] (toLists mat))
filterList = Prelude.filter
2019-03-28 20:59:31 +01:00
(\((frow, fcol), _) ->
2019-03-07 15:13:40 +01:00
((realToFrac (sx frow fcol) > -tileWidth &&
realToFrac (sy frow fcol) > -tileHeight) &&
((realToFrac (sx frow fcol) :: Double) < 1280 &&
(realToFrac ((sy frow fcol)- (74 - (realToFrac tileHeight :: CFloat))) :: Double) < 720))
)
coordList
!posanimList = Prelude.map
(\((row, col), tile) -> ((row, col), tile, partposanims M.! (row, col)))
filterList
sx row col = realToFrac $ 640 + ((fromIntegral col - pc) +
(fromIntegral row - pr)) * (tileWidth / 2) :: CFloat
sy row col = realToFrac $ 360 - (tileHeight / 2) + ((fromIntegral row - pr) -
(fromIntegral col - pc)) * (tileHeight / 2) :: CFloat
2019-03-28 20:59:31 +01:00
void $ sequence $ parMap rpar
-- mapM_
2019-03-07 15:13:40 +01:00
(\((row, col), tile, posanim) ->
drawTile ud ctx posanim pr pc row col tile
)
posanimList
-- void $ sequence $ parMap rpar (\(i, ls) -> void $ sequence $ parMap rpar
-- (\(j, t) -> drawTile ud ctx (partposanims M.! (i, j)) pr pc i j t)
-- (reverse $ zip [1..] ls))
-- (zip [1..] (toLists mat))
2018-08-10 14:09:07 +02:00
mapM_ (\(V2 sr sc, pa, perc) -> when pa $ do
2018-08-10 10:29:12 +02:00
let lx = realToFrac $ 640 + ((sc - pc) +
(sr - pr)) * (tileWidth / 2) :: CFloat
ly = realToFrac $ 360 - (tileHeight / 2) + ((sr - pr) -
(sc - pc)) * (tileHeight / 2) :: CFloat
fillColor ctx (rgb 0 255 0)
strokeColor ctx (rgb 0 255 0)
strokeWidth ctx 2
beginPath ctx
rect ctx (lx - 25) (ly - 50) 50 10
stroke ctx
closePath ctx
beginPath ctx
rect ctx (lx - 25 * perc) (ly - 50) (50 * perc) 10
fill ctx
closePath ctx
) posActions
2018-06-08 00:29:46 +02:00
fontSize ctx 20
2020-05-05 10:26:16 +02:00
aFonts <- readMVar (assetFonts ud)
fontFace ctx (aFonts Map.! FontBedstead)
2018-06-08 00:29:46 +02:00
textAlign ctx (S.fromList [AlignCenter,AlignTop])
fillColor ctx (rgb 255 128 0)
textBox ctx 0 0 200 (
"FPS: "
<> T.pack (Prelude.take 5 $ show (1/dt))
<> " Clearance: "
2020-01-26 18:07:40 +01:00
<> (if not (Prelude.null room)
2019-10-20 10:53:53 +02:00
then T.pack (show $ clearance $ V.head room)
else "0"
)
2020-01-26 18:07:40 +01:00
<> "\nFor answers call 6350"
)
2018-05-30 16:20:58 +02:00
drawTile
:: UserData
-> Context
2018-07-21 06:43:26 +02:00
-> [(V2 Double, AnimState, Maybe (Boundaries Double))]
2018-05-30 16:20:58 +02:00
-> Double
-> Double
-> Int
-> Int
-> Maybe ImgId
-> IO ()
2020-05-05 10:26:16 +02:00
drawTile ud ctx posanims pr pc row col img = do
let (bef, beh) = L.partition delimiter sorted
aImages <- readMVar (assetImages ud)
save ctx
mapM_ (flip drawAnim fact) beh
maybe (return ()) (Types.draw ud x (y - 42) 64 74
(if fromMaybe False (isWall <$> img) then fact else 1))
((aImages Map.!) <$> case img of
Just ImgEmpty -> Nothing
_ -> img
)
mapM_ (flip drawAnim fact) bef
restore ctx
-- when (floor pr == row && floor pc == col) $ do
-- A.logIO A.Debug ("sorted: " ++ show sorted)
-- A.logIO A.Debug ("beh: " ++ show beh)
-- A.logIO A.Debug ("bef: " ++ show bef)
2018-05-30 16:20:58 +02:00
where
2018-07-30 14:34:46 +02:00
delimiter (V2 nr nc, as, mbnds) =
animFloats (asId as) ||
2018-06-16 11:40:51 +02:00
all delimit mb
where
delimit b
2018-08-07 14:04:12 +02:00
| nnr > fst (matmax b) || nnc < snd (matmin b) =
True
2018-06-16 11:40:51 +02:00
| nnr > fst (matmin b) && nnr < fst (matmax b) =
2018-11-16 07:58:15 +01:00
nnc <= snd (matmin b)
2018-06-16 11:40:51 +02:00
| nnc > snd (matmin b) && nnc < snd (matmax b) =
2018-11-16 07:58:15 +01:00
nnr >= fst (matmax b)
2018-06-16 11:40:51 +02:00
| otherwise =
2018-08-07 14:04:12 +02:00
False
2018-07-21 06:43:26 +02:00
nnr = case mbnds of
2018-08-18 05:01:52 +02:00
Just (Boundaries (_, _) (maxr, _)) -> maxr
2018-07-21 06:43:26 +02:00
Nothing -> nr - fromIntegral ((floor nr) :: Int) :: Double
nnc = case mbnds of
2018-08-18 05:01:52 +02:00
Just (Boundaries (_, minc) (_, _)) -> minc
2018-07-21 06:43:26 +02:00
Nothing -> nc - fromIntegral ((floor nc) :: Int) :: Double
2018-05-30 16:20:58 +02:00
tileWidth = 64 :: Double
tileHeight = 32 :: Double
sorted = sortOn (\(V2 sr sc, _, mbnds) ->
2019-03-28 20:59:31 +01:00
let comp srow scol =
((floor $ (1 - scol) * 100) :: Int) + ((floor $ srow * 100) :: Int)
in
case mbnds of
Just (Boundaries (minr, minc) (maxr, maxc)) ->
2019-02-16 08:39:01 +01:00
comp (minr + ((maxr - minr) / 2)) (minc + (maxc - minc) / 2)
_ ->
comp
(sr - (fromIntegral ((floor sr) :: Int)))
(sc - (fromIntegral ((floor sc) :: Int)))
2018-07-21 06:43:26 +02:00
) posanims
2018-05-31 05:25:08 +02:00
minrs = Prelude.map (fst . matmin) mb
2018-05-30 16:20:58 +02:00
maxrs = Prelude.map (fst . matmax) mb
mincs = Prelude.map (snd . matmin) mb
maxcs = Prelude.map (snd . matmax) mb
x = realToFrac $ 640 + ((fromIntegral col - pc) +
(fromIntegral row - pr)) * (tileWidth / 2) :: CFloat
y = realToFrac $ 360 - (tileHeight / 2) + ((fromIntegral row - pr) -
(fromIntegral col - pc)) * (tileHeight / 2) :: CFloat
dist = distance (V2 (fromIntegral row) (fromIntegral col))
(V2 (realToFrac pr - 1) (realToFrac pc)) / 4
fact =
2019-03-09 20:36:21 +01:00
if pr <= fromIntegral row + (if L.null maxrs then 0.5 else minimum maxrs) &&
pc >= fromIntegral col + (if L.null mincs then 0.5 else maximum mincs)
2018-05-30 16:20:58 +02:00
then min 1 dist
else 1
mb = maybe [] collisionObstacle img
2019-03-09 20:36:21 +01:00
drawAnim (V2 nr nc, as, _) factor = do
let ax = realToFrac $ 640 + ((nc - pc) + (nr - pr)) * 32 - 32
ay = realToFrac $ 360 + ((nr - pr) - (nc - pc)) * 16 - 58
2020-05-05 10:26:16 +02:00
Types.draw ud ax ay 64 74 (if isWallLike (aiVariation $ asId as) then factor else 1) as
2018-05-30 16:20:58 +02:00
2020-05-05 10:26:16 +02:00
updateMap :: UserData -> Double -> Affection ()
updateMap ud dt = do
osData <- liftIO $ readMVar (stateData ud)
if osData == None
2018-06-08 00:29:46 +02:00
then do
2018-09-18 03:13:53 +02:00
mstart <- liftIO $ tryTakeMVar (stateMVar ud)
case mstart of
Just (nws, mendat) -> do
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws
void $ liftIO $ swapMVar (stateData ud) mendat
void $ liftIO $ swapMVar (state ud) (Main WorldMap)
updateMap ud 0.1
updateMap ud 0.1
updateMap ud 0.1
updateMap ud 19
2018-09-18 03:13:53 +02:00
liftIO $ logIO A.Debug "Loaded game data"
Nothing -> return ()
2018-06-08 00:29:46 +02:00
else do
2020-05-05 10:26:16 +02:00
wState <- liftIO $ readMVar (worldState ud)
sData <- liftIO $ readMVar (stateData ud)
aAnim <- liftIO $ readMVar (assetAnimations ud)
(nws, _) <- yieldSystemT wState $ do
2019-02-15 20:02:55 +01:00
obstacleBounds <- efor allEnts $ do
with obstacle
with pos
b <- query obstacle
pos' <- query pos
return (pos', b)
2018-06-08 00:29:46 +02:00
emap allEnts $ do
pos'@(V2 pr pc) <- query pos
2019-03-07 01:33:51 +01:00
vel' <- queryMaybe vel
2019-03-28 11:26:16 +01:00
rot' <- fromMaybe N <$> queryMaybe rot
2019-03-07 01:33:51 +01:00
fact' <- fromMaybe 1 <$> queryMaybe velFact
xyv2 <- queryMaybe xyvel
2018-06-08 00:29:46 +02:00
stat <- query anim
2020-05-05 10:26:16 +02:00
let an = aAnim Map.! asId stat
mat = mapMat sData
2019-03-07 01:33:51 +01:00
ntime = asElapsedTime stat + dt
npos = pos' + fmap (* (dt * fact')) (fromMaybe (V2 0 0) vel')
2018-06-08 00:29:46 +02:00
dpos@(V2 dpr dpc) = npos - pos'
aId = asId stat
lll = (,)
<$> (
if dpr < 0
then [(floor dpr :: Int) .. 0]
else [0 .. (ceiling dpr :: Int)])
<*> (
if dpc < 0
then [(floor dpc :: Int) .. 0]
else [0 .. (ceiling dpc :: Int)])
colldpos = dpos * Prelude.foldl
2018-11-16 07:12:27 +01:00
(\acc a ->
let ret = checkBoundsCollision2 pos' npos dt acc a
2019-10-28 18:20:34 +01:00
in A.log A.Verbose (fromString $ show ret) ret)
2018-06-08 00:29:46 +02:00
(V2 1 1)
(
2019-11-02 09:44:50 +01:00
nub $ concatMap
2018-06-08 00:29:46 +02:00
(\(dr, dc) ->
2018-07-21 20:37:01 +02:00
let bs = (++)
2019-10-20 10:53:53 +02:00
(fromMaybe [] $ collisionObstacle <$> M.unsafeGet
2019-11-02 09:44:50 +01:00
(min (nrows mat) $ max 1 $ fromIntegral $ floor pr + dr)
(min (ncols mat) $ max 1 $ fromIntegral $ floor pc + dc)
2020-05-05 10:26:16 +02:00
(imgMat sData))
2018-07-21 20:37:01 +02:00
(Prelude.map snd $ Prelude.filter
(\((V2 br bc), _) ->
floor pr + dr == floor br &&
floor pc + dc == floor bc
)
obstacleBounds)
2018-06-08 00:29:46 +02:00
in Prelude.map (\(Boundaries (minr, minc) (maxr, maxc))->
Boundaries
(minr + fromIntegral dr, minc + fromIntegral dc)
(maxr + fromIntegral dr, maxc + fromIntegral dc)
) bs
)
lll -- (A.log A.Verbose (show lll ++ " " ++ show len) lll)
2018-06-02 22:04:05 +02:00
)
2019-03-07 01:33:51 +01:00
nstate =
let velo = fromMaybe (V2 0 0) vel'
nstat = if ntime > fromIntegral (asCurrentFrame stat + 1) *
(animDuration an / fromIntegral (length $ animSprites an))
then
let nframe = asCurrentFrame stat + 1
in case animPlay an of
APLoop ->
let (nnframe, nntime) =
if nframe >= length (animSprites an)
then (0, 0)
else (nframe, ntime)
in stat
{ asCurrentFrame = nnframe
, asElapsedTime = nntime
}
APOnce ->
let nnframe = if nframe >= length (animSprites an)
then nframe - 1
else nframe
in stat
{ asCurrentFrame = nnframe
, asElapsedTime = ntime
}
else
stat
{ asElapsedTime = ntime
}
in
case aiName aId of
"walking"
| sqrt (velo `dot` velo) > 0 ->
nstat
{ asId = aId
{ aiDirection = fromMaybe rot' (direction velo)
}
}
| otherwise ->
nstat
{ asId = aId
{ aiDirection = fromMaybe rot' (direction velo)
, aiName = "standing"
}
, asCurrentFrame = 0
}
"standing"
| sqrt (velo `dot` velo) > 0 ->
nstat
{ asId = aId
{ aiDirection = fromMaybe rot' (direction velo)
, aiName = "walking"
}
, asCurrentFrame = 0
}
| otherwise ->
nstat
{ asId = aId
{ aiDirection = fromMaybe rot' (direction velo)
}
}
2019-03-28 20:59:31 +01:00
_ -> nstat
2018-06-08 00:29:46 +02:00
ent = unchanged
2019-03-07 01:33:51 +01:00
{ pos = Set $ pos' + colldpos
, rot = Set $ fromMaybe rot' (direction (fromMaybe (V2 0 0) vel'))
, anim = Set nstate
, vel = case xyv2 of
Just (V2 rx ry) ->
let
V2 dr dc = fmap (* 1.5) (V2 rx ry `rotVec` 45)
in
Set $ 2 * V2 dr dc
Nothing -> Keep
2018-06-08 00:29:46 +02:00
}
return ent
-- allRelEnts <- efor allEnts $ do
-- with pos
-- with rot
-- with clearanceLvl
-- without objType
-- pos' <- query pos
-- rot' <- query rot
-- clvl <- query clearanceLvl
-- entn <- queryEnt
-- return (entn, pos', rot', clvl)
2018-08-10 08:58:26 +02:00
tses <- efor allEnts $ do
2019-02-18 19:14:41 +01:00
with objType
with objState
t <- query objType
s <- query objState
e <- queryEnt
return (t, s, e)
2018-08-10 08:58:26 +02:00
mapM_ (\(t, s, e) ->
objectAction dt t s e
2018-08-10 08:58:26 +02:00
) tses
2018-08-11 01:12:07 +02:00
(nws2, _) <- yieldSystemT nws $ updateNPCs
2020-05-05 10:26:16 +02:00
(imgMat sData)
(mapMat sData)
2018-09-13 00:51:22 +02:00
nws
2019-10-20 10:53:53 +02:00
(V.filter
2018-08-11 01:12:07 +02:00
(\p -> pointType p /= RoomExit)
2020-05-05 10:26:16 +02:00
(reachPoints sData)
2018-08-11 01:12:07 +02:00
)
dt
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) nws2