about summary refs log tree commit diff
path: root/src/Main.hs
blob: 2da277b640716126ca2ea9474eaceb3375569250 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Main ( 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.Generators.Util (regions)
import           Xanthous.Generators.LevelContents
import           Xanthous.Data (Dimensions, Dimensions'(Dimensions))
import           Data.Array.IArray ( amap )
--------------------------------------------------------------------------------
data Command
  = Run
  | Generate GeneratorInput Dimensions

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 = (<|> pure Run) $ 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
      rs = regions $ amap not res
  putStr "num regions: "
  print $ length rs
  putStr "region lengths: "
  print $ length <$> rs
  putStr "character position: "
  print =<< chooseCharacterPosition res
  putStrLn $ showCells res

runCommand :: Command -> IO ()
runCommand Run = runGame
runCommand (Generate input dims) = runGenerate input dims

main :: IO ()
main = runCommand =<< Opt.execParser optParser