From 259904bfc4ca2f8b90381dfdb887750b9bdaf368 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 1 May 2021 18:48:31 -0400 Subject: [PATCH] start on reading file --- Ekitai.hs | 15 +++++++++++---- Render.hs | 6 +++--- Sim.hs | 56 ++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/Ekitai.hs b/Ekitai.hs index 6fe1195..6c1609e 100644 --- a/Ekitai.hs +++ b/Ekitai.hs @@ -1,4 +1,5 @@ import System.Environment +import System.IO import qualified Data.Vector as V import qualified Brick as B @@ -10,12 +11,18 @@ import Render -- ui :: B.Widget () -- ui = B.str "hello" <+> B.str "World" --- main :: IO () main = do - initialState <- buildInitialState + argv <- getArgs + (opts, fname) <- ekitaiOpts argv + handle <- openFile fname ReadMode + contents <- hGetContents handle + putStr contents + hClose handle + initialState <- buildInitialState $ stringToSim 10 10 contents endState <- B.defaultMain ekitaiApp initialState print endState - -- argv <- getArgs - -- (opts, fname) <- ekitaiOpts argv + +-- main :: IO () +-- main = do -- return 0 diff --git a/Render.hs b/Render.hs index 16fda2b..036944b 100644 --- a/Render.hs +++ b/Render.hs @@ -24,10 +24,10 @@ ekitaiApp = App , appAttrMap = const $ attrMap mempty [] } -buildInitialState :: IO EkitaiState -buildInitialState = +buildInitialState :: Simulation -> IO EkitaiState +buildInitialState sim = pure EkitaiState - { ekitaiStateSim = testSim + { ekitaiStateSim = sim } drawEkitai :: EkitaiState -> [Widget ResourceName] diff --git a/Sim.hs b/Sim.hs index fe8c402..8f20343 100644 --- a/Sim.hs +++ b/Sim.hs @@ -2,7 +2,7 @@ module Sim ( Simulation, simSpace, ChunkType, ChunkData, chunkType, initSimSpace, testSim, physStep, validDirects, - simToString + simToString, stringToSim ) where import Debug.Trace @@ -35,7 +35,7 @@ initSimSpace x y = Simulation } testSim = let a = simSet (initSimSpace 10 10) (ChunkData Water) 5 5 - in simSet a (ChunkData Wall) 5 4 + in simSet a (ChunkData Wall) 5 6 physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] sim sim -- _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} | trace ("" ++ show h) False = undefined @@ -48,30 +48,38 @@ _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} = next = case simGetChunkType sim x y of Water -> updateWaterChunk x y valid acc _ -> acc + -- spawn = simSet next (ChunkData Water) 5 0 -- takes in list of valid chunks, as well as the sim to modify -- updateWaterChunk x y valid sim | trace ("x:" ++ show x ++ " y:" ++ show y ++ " " ++ show valid) False = undefined updateWaterChunk x y valid sim = -- if the chunk below is free, fall straight down - if elem (0,-1) valid && fromEnum (simGetChunkType sim x (y-1)) == fromEnum Air + if elem (0,1) valid && fromEnum (simGetChunkType sim x (y+1)) == fromEnum Air then let moved = simSet sim ChunkData { chunkType=Air } x y - in simSet moved ChunkData { chunkType=Water } x (y-1) - else if elem (-1,-1) valid && fromEnum (simGetChunkType sim (x-1) (y-1)) == fromEnum Air + in simSet moved ChunkData { chunkType=Water } x (y+1) + else if elem (-1,1) valid && fromEnum (simGetChunkType sim (x-1) (y+1)) == fromEnum Air then let moved = simSet sim ChunkData { chunkType=Air } x y - in simSet moved ChunkData { chunkType=Water } (x-1) (y-1) - else if elem (1,-1) valid && fromEnum (simGetChunkType sim (x+1) (y-1)) == fromEnum Air + in simSet moved ChunkData { chunkType=Water } (x-1) (y+1) + else if elem (1,1) valid && fromEnum (simGetChunkType sim (x+1) (y+1)) == fromEnum Air then let moved = simSet sim ChunkData { chunkType=Air } x y - in simSet moved ChunkData { chunkType=Water } (x+1) (y-1) + in simSet moved ChunkData { chunkType=Water } (x+1) (y+1) + else if elem (-1,0) valid && fromEnum (simGetChunkType sim (x-1) y) == fromEnum Air + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } (x-1) y + else if elem (1,0) valid && fromEnum (simGetChunkType sim (x+1) y) == fromEnum Air + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } (x+1) y -- stay put else sim -- gets chunks around a given chunk that are inside grid -- validDirects x y w h | trace ("w:"++ show w ++ "h:" ++ show h) False = undefined validDirects x y w h = filter - -- (\q -> 0 <= (fst q)+x && (fst q)+x < w && (snd q)+y <= 0 && (snd q)+y < h) (\q -> 0 <= (fst q)+x && (fst q)+x < w && 0 <= (snd q)+y && (snd q)+y < h) [(a,b) | a <- [-1..1], b <- [-1..1], not (a==0 && b==0)] @@ -80,6 +88,23 @@ simToString sim@Simulation{simW=w} = let simStr = V.toList $ V.map chunkToChar $ simSpace sim in insert w '\n' simStr +-- from https://stackoverflow.com/questions/12659562/insert-specific-element-y-after-every-n-elements-in-a-list +insert :: Int -> a -> [a] -> [a] +insert n y xs = countdown n xs where + countdown 0 xs = y:countdown n xs + countdown _ [] = [] + countdown m (x:xs) = x:countdown (m-1) xs + +stringToSim :: Int -> Int -> [Char] -> Simulation +stringToSim w h st = + _stringToSim st [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) +_stringToSim st grid acc = + if null grid || null st then acc + else _stringToSim (tail st) (tail grid) next + where x = fst $ head grid + y = snd $ head grid + next = simSet acc (charToChunk $ head st) x y + -- maps each chunktype to an ascii character chunkToChar :: ChunkData -> Char chunkToChar c = @@ -89,10 +114,11 @@ chunkToChar c = Wall -> '#' _ -> '?' --- from https://stackoverflow.com/questions/12659562/insert-specific-element-y-after-every-n-elements-in-a-list -insert :: Int -> a -> [a] -> [a] -insert n y xs = countdown n xs where - countdown 0 xs = y:countdown n xs - countdown _ [] = [] - countdown m (x:xs) = x:countdown (m-1) xs +-- this is redundant, fix this somehow +charToChunk :: Char -> ChunkData +charToChunk c = + ChunkData { chunkType=ctype } + where ctype = if c=='~' then Water + else if c=='#' then Wall + else Air -- 2.20.1