contents <- hGetLines handle
hClose handle
-- start brick
- ekitaiMain $ stringToSim contents
+ ekitaiMain (stringToSim contents) (optTimeStep opts)
return 0
-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"
, 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
data ChunkType = Air
| Water
- | Wall deriving (Show, Enum)
+ | Wall
+ | Pump
+ | Drain deriving (Show, Enum)
data ChunkData = ChunkData
{ chunkType :: ChunkType
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
-- 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
Water -> '~'
Air -> ' '
Wall -> '#'
+ Pump -> '@'
+ Drain -> 'O'
_ -> '?'
-- this is redundant, fix this somehow
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
.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
--- /dev/null
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
+
+
+
+ ######
+ ######
+ ######
+
+ ###### ######
+ ###### ######
+ ###### ######
+
+ ###### ###### ######
+ ###### ###### ######
+ ###### ###### ######
+
+ ###### ###### ###### ######
+ ###### ###### ###### ######
+ ###### ###### ###### ######
+
+
+
+
+
+
+
+
+###############################################################
-##~~~~~~~~~ #
-### ~ ~~~~ #
-# ##~~~~~~~ #
-# ## ~ ~~~ #
-# ## #
-# ## ##
-# ###
-# ## #
-# ## #
-# ## #
-# #
-## #
-### #
-# ## #
-# ## #
-# ## #
-# ## ##
-# ###
-# ## #
-# ## #
-# ## #
-## #
-### #
-# ## #
-# ## #
-# ## #
-# ## ##
-# ###
-# ## #
-# ## #
-# ## #
-#############
+# @ #
+# #
+# #
+# #
+# #####
+# ##### #
+# ##### #
+# ##### #
+# ##### #
+# #
+# #
+# #
+#O### #
+# ##### #
+# ##### #
+# ##### #
+# ##### #
+# #
+# #
+# #
+# #####
+# ##### #
+# ##### #
+# ##### #
+# ##### #
+# #
+# #
+# #
+#O### #
+# ##### #
+# ##### #
+# ##### #
+# ##### #
+# #
+# #
+# #
+############################OOOO##