From 4de2980e358a4ed8bb05e615a3fee16215b03c1f Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 1 May 2021 23:27:14 -0400 Subject: [PATCH] drain and pump --- Ekitai.hs | 2 +- Parse.hs | 23 +++++++++++++---- Render.hs | 4 +-- Sim.hs | 30 +++++++++++++++++++++- makefile | 5 ++-- samples/blocks | 30 ++++++++++++++++++++++ samples/flow | 69 +++++++++++++++++++++++++++----------------------- 7 files changed, 120 insertions(+), 43 deletions(-) create mode 100644 samples/blocks diff --git a/Ekitai.hs b/Ekitai.hs index af47e02..6d0c867 100644 --- a/Ekitai.hs +++ b/Ekitai.hs @@ -24,6 +24,6 @@ main = do contents <- hGetLines handle hClose handle -- start brick - ekitaiMain $ stringToSim contents + ekitaiMain (stringToSim contents) (optTimeStep opts) return 0 diff --git a/Parse.hs b/Parse.hs index 26a0390..367caff 100644 --- a/Parse.hs +++ b/Parse.hs @@ -1,34 +1,47 @@ -module Parse ( ekitaiOpts ) where +module Parse ( ekitaiOpts, optTimeStep ) where import System.Console.GetOpt import System.Environment +import System.Exit import Data.Maybe import Data.Either data Options = Options { optHelp :: Bool , optColor :: Bool + , optTimeStep :: Int } defaultOptions = Options { optHelp = False , optColor = False + , optTimeStep = 50000 } options :: [OptDescr (Options -> Options)] options = [ Option ['h'] ["help"] - (NoArg (\ opts -> opts { optHelp = True })) + (NoArg (\opts -> opts { optHelp = True })) "display's this message" , Option ['c'] ["color", "colour"] - (NoArg (\ opts -> opts { optColor = True })) + (NoArg (\opts -> opts { optColor = True })) "enables color" + , Option ['t'] ["timestep"] + (ReqArg (\t opts -> opts { optTimeStep = read t :: Int }) "timestep") + "sets the simulation time step" ] ekitaiOpts :: [String] -> IO (Options, String) ekitaiOpts argv = case getOpt RequireOrder options argv of (o, [n], []) -> return (foldl (flip id) defaultOptions o, n) - (o, _, []) -> ioError $ userError $ "must supply input file" - (_, _, errs) -> ioError $ userError $ (concat errs) + (o, _, []) -> ioError $ userError $ "missing input file" + (_, _, errs) -> ioError $ userError $ concat errs +-- ++ usageInfo header options +-- where header = "Usage: ekitai [OPTIONS...] simfile" + + -- (o, _, []) -> do + -- opts <- (foldl (flip id) defaultOptions o) + -- if optHelp then userError $ concat usageInfo "Usage: ekitai [OPTIONS...] simfile" options + -- else ioError $ userError $ "missing input file" diff --git a/Render.hs b/Render.hs index bc4a15a..d6d967e 100644 --- a/Render.hs +++ b/Render.hs @@ -32,12 +32,12 @@ ekitaiApp = App , appAttrMap = const $ attrMap mempty [] } -ekitaiMain sim = do +ekitaiMain sim timestep = do chan <- newBChan 10 -- tick game forkIO $ forever $ do writeBChan chan Tick - threadDelay 50000 + threadDelay timestep let buildVty = Graphics.Vty.mkVty Graphics.Vty.defaultConfig initialVty <- buildVty initialState <- buildInitialState sim diff --git a/Sim.hs b/Sim.hs index 97456fc..64aef9b 100644 --- a/Sim.hs +++ b/Sim.hs @@ -17,7 +17,9 @@ data Simulation = Simulation data ChunkType = Air | Water - | Wall deriving (Show, Enum) + | Wall + | Pump + | Drain deriving (Show, Enum) data ChunkData = ChunkData { chunkType :: ChunkType @@ -47,6 +49,8 @@ _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} = valid = validDirects x y w h next = case simGetChunkType sim x y of Water -> updateWaterChunk x y valid acc + Pump -> updatePumpChunk x y valid acc + Drain -> updateDrainChunk x y valid acc _ -> acc -- spawn = simSet next (ChunkData Water) 5 0 @@ -77,6 +81,26 @@ updateWaterChunk x y valid sim = -- stay put else sim +updatePumpChunk x y valid sim = + if elem (0,1) valid && fromEnum (simGetChunkType sim x (y+1)) == fromEnum Air + then simSet sim ChunkData { chunkType=Water } x (y+1) + else if elem (-1,0) valid && fromEnum (simGetChunkType sim (x-1) y) == fromEnum Air + then simSet sim ChunkData { chunkType=Water } (x-1) y + else if elem (1,0) valid && fromEnum (simGetChunkType sim (x+1) y) == fromEnum Air + then simSet sim ChunkData { chunkType=Water } (x+1) y + else sim + +updateDrainChunk x y valid sim = + if elem (0,1) valid && fromEnum (simGetChunkType sim x (y+1)) == fromEnum Water + then simSet sim ChunkData { chunkType=Air } x (y+1) + else if elem (0,-1) valid && fromEnum (simGetChunkType sim x (y-1)) == fromEnum Water + then simSet sim ChunkData { chunkType=Air } x (y-1) + else if elem (1,0) valid && fromEnum (simGetChunkType sim (x+1) y) == fromEnum Water + then simSet sim ChunkData { chunkType=Air } (x+1) y + else if elem (-1,0) valid && fromEnum (simGetChunkType sim (x-1) y) == fromEnum Water + then simSet sim ChunkData { chunkType=Air } (x-1) y + 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 @@ -118,6 +142,8 @@ chunkToChar c = Water -> '~' Air -> ' ' Wall -> '#' + Pump -> '@' + Drain -> 'O' _ -> '?' -- this is redundant, fix this somehow @@ -126,5 +152,7 @@ charToChunk c = ChunkData { chunkType=ctype } where ctype = if c=='~' then Water else if c=='#' then Wall + else if c=='@' then Pump + else if c=='O' then Drain else Air diff --git a/makefile b/makefile index 9960bb7..76cecb7 100644 --- a/makefile +++ b/makefile @@ -6,10 +6,11 @@ HFLAGS=-dynamic -threaded .PHONY: ekitai clean test ekitai: Ekitai.hs - $(HC) $(HFLAGS) --make $< + $(HC) $(HFLAGS) --make $< && \ + mv Ekitai ekitai test: Ekitai.hs $(DHC) $< clean: - rm Ekitai *.o *.hi + rm ekitai *.o *.hi diff --git a/samples/blocks b/samples/blocks new file mode 100644 index 0000000..1e17d7f --- /dev/null +++ b/samples/blocks @@ -0,0 +1,30 @@ + #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# + #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# + #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# + + + + ###### + ###### + ###### + + ###### ###### + ###### ###### + ###### ###### + + ###### ###### ###### + ###### ###### ###### + ###### ###### ###### + + ###### ###### ###### ###### + ###### ###### ###### ###### + ###### ###### ###### ###### + + + + + + + + +############################################################### diff --git a/samples/flow b/samples/flow index 4e8bf19..29c2a4b 100644 --- a/samples/flow +++ b/samples/flow @@ -1,32 +1,37 @@ -##~~~~~~~~~ # -### ~ ~~~~ # -# ##~~~~~~~ # -# ## ~ ~~~ # -# ## # -# ## ## -# ### -# ## # -# ## # -# ## # -# # -## # -### # -# ## # -# ## # -# ## # -# ## ## -# ### -# ## # -# ## # -# ## # -## # -### # -# ## # -# ## # -# ## # -# ## ## -# ### -# ## # -# ## # -# ## # -############# +# @ # +# # +# # +# # +# ##### +# ##### # +# ##### # +# ##### # +# ##### # +# # +# # +# # +#O### # +# ##### # +# ##### # +# ##### # +# ##### # +# # +# # +# # +# ##### +# ##### # +# ##### # +# ##### # +# ##### # +# # +# # +# # +#O### # +# ##### # +# ##### # +# ##### # +# ##### # +# # +# # +# # +############################OOOO## -- 2.20.1