about summary refs log tree commit diff
path: root/src/Main.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-09-07T18·49-0400
committerGriffin Smith <root@gws.fyi>2019-09-07T18·52-0400
commitf03ad6bbd60b6ccdd329fc6740bcea2b554980dd (patch)
treeeba7d803e5468ae12edf133acf21a2e227ef1f6c /src/Main.hs
parent73a52e531d940858f0ac334d8b2ccda479ea7b5e (diff)
Add cellular-automata cave generator
Add a cellular-automata-based cave level generator, plus an
optparse-applicative-based CLI for invoking level generators in general.
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs62
1 files changed, 58 insertions, 4 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 1cd4e9445789..4d6ccfd4afc6 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -2,16 +2,70 @@ module Main where
 
 import Xanthous.Prelude
 import Brick
+import qualified Options.Applicative as Opt
+import System.Random
 
 import Xanthous.Game (getInitialState)
 import Xanthous.App (makeApp)
+import Xanthous.Generators
+  ( GeneratorInput(..)
+  , parseGeneratorInput
+  , generateFromInput
+  , showCells
+  )
+import Xanthous.Data (Dimensions, Dimensions'(Dimensions))
 
-ui :: Widget ()
-ui = str "Hello, world!"
+data Command
+  = Run
+  | Generate GeneratorInput Dimensions
 
-main :: IO ()
-main = do
+parseDimensions :: Opt.Parser Dimensions
+parseDimensions = Dimensions
+  <$> Opt.option Opt.auto
+       ( Opt.short 'w'
+       <> Opt.long "width"
+       )
+  <*> Opt.option Opt.auto
+       ( Opt.short 'h'
+       <> Opt.long "height"
+       )
+
+parseCommand :: Opt.Parser Command
+parseCommand = Opt.subparser
+  $ Opt.command "run"
+      (Opt.info
+       (pure Run)
+       (Opt.progDesc "Run the game"))
+  <> Opt.command "generate"
+      (Opt.info
+       (Generate
+        <$> parseGeneratorInput
+        <*> parseDimensions
+        <**> Opt.helper
+       )
+       (Opt.progDesc "Generate a sample level"))
+
+optParser :: Opt.ParserInfo Command
+optParser = Opt.info
+  (parseCommand <**> Opt.helper)
+  (Opt.header "Xanthous: a WIP TUI RPG")
+
+runGame :: IO ()
+runGame =  do
   app <- makeApp
   initialState <- getInitialState
   _ <- defaultMain app initialState
   pure ()
+
+runGenerate :: GeneratorInput -> Dimensions -> IO ()
+runGenerate input dims = do
+  randGen <- getStdGen
+  let res = generateFromInput input dims randGen
+  putStrLn $ showCells res
+
+runCommand :: Command -> IO ()
+runCommand Run = runGame
+runCommand (Generate input dims) = runGenerate input dims
+
+main :: IO ()
+main = runCommand =<< Opt.execParser optParser