2019-09-13 21:24:05 +02:00
|
|
|
module Main ( main ) where
|
|
|
|
--------------------------------------------------------------------------------
|
2019-10-12 18:59:42 +02:00
|
|
|
import Xanthous.Prelude hiding (finally)
|
2019-09-13 21:24:05 +02:00
|
|
|
import Brick
|
2019-09-07 20:49:59 +02:00
|
|
|
import qualified Options.Applicative as Opt
|
2019-09-13 21:24:05 +02:00
|
|
|
import System.Random
|
2019-10-12 18:59:42 +02:00
|
|
|
import Control.Monad.Random (getRandom)
|
|
|
|
import Control.Exception (finally)
|
2019-11-29 20:33:52 +01:00
|
|
|
import System.Exit (die)
|
2019-09-13 21:24:05 +02:00
|
|
|
--------------------------------------------------------------------------------
|
2019-10-12 18:59:42 +02:00
|
|
|
import qualified Xanthous.Game as Game
|
2019-09-13 21:24:05 +02:00
|
|
|
import Xanthous.App (makeApp)
|
|
|
|
import Xanthous.Generators
|
2019-10-12 18:59:42 +02:00
|
|
|
( GeneratorInput
|
|
|
|
, parseGeneratorInput
|
|
|
|
, generateFromInput
|
|
|
|
, showCells
|
|
|
|
)
|
|
|
|
import qualified Xanthous.Entities.Character as Character
|
2019-09-13 21:24:05 +02:00
|
|
|
import Xanthous.Generators.Util (regions)
|
|
|
|
import Xanthous.Generators.LevelContents
|
|
|
|
import Xanthous.Data (Dimensions, Dimensions'(Dimensions))
|
|
|
|
import Data.Array.IArray ( amap )
|
|
|
|
--------------------------------------------------------------------------------
|
2019-10-12 18:59:42 +02:00
|
|
|
|
|
|
|
data RunParams = RunParams
|
|
|
|
{ seed :: Maybe Int
|
|
|
|
, characterName :: Maybe Text
|
|
|
|
}
|
|
|
|
deriving stock (Show, Eq)
|
|
|
|
|
|
|
|
parseRunParams :: Opt.Parser RunParams
|
|
|
|
parseRunParams = RunParams
|
|
|
|
<$> optional (Opt.option Opt.auto
|
|
|
|
( Opt.long "seed"
|
|
|
|
<> Opt.help "Random seed for the game."
|
|
|
|
))
|
|
|
|
<*> optional (Opt.strOption
|
|
|
|
( Opt.short 'n'
|
|
|
|
<> Opt.long "name"
|
|
|
|
<> Opt.help
|
|
|
|
( "Name for the character. If not set on the command line, "
|
|
|
|
<> "will be prompted for at runtime"
|
|
|
|
)
|
|
|
|
))
|
|
|
|
|
2019-09-07 20:49:59 +02:00
|
|
|
data Command
|
2019-10-12 18:59:42 +02:00
|
|
|
= Run RunParams
|
2019-11-29 20:33:52 +01:00
|
|
|
| Load FilePath
|
2019-09-07 20:49:59 +02:00
|
|
|
| Generate GeneratorInput Dimensions
|
2019-08-25 19:28:10 +02:00
|
|
|
|
2019-09-07 20:49:59 +02:00
|
|
|
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
|
2019-10-12 18:59:42 +02:00
|
|
|
parseCommand = (<|> Run <$> parseRunParams) $ Opt.subparser
|
2019-09-07 20:49:59 +02:00
|
|
|
$ Opt.command "run"
|
|
|
|
(Opt.info
|
2019-10-12 18:59:42 +02:00
|
|
|
(Run <$> parseRunParams)
|
2019-09-07 20:49:59 +02:00
|
|
|
(Opt.progDesc "Run the game"))
|
2019-11-29 20:33:52 +01:00
|
|
|
<> Opt.command "load"
|
|
|
|
(Opt.info
|
|
|
|
(Load <$> Opt.argument Opt.str (Opt.metavar "FILE"))
|
|
|
|
(Opt.progDesc "Load a saved game"))
|
2019-09-07 20:49:59 +02:00
|
|
|
<> 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")
|
|
|
|
|
2019-11-29 20:33:52 +01:00
|
|
|
thanks :: IO ()
|
|
|
|
thanks = putStr "\n\n" >> putStrLn "Thanks for playing Xanthous!"
|
|
|
|
|
2019-10-12 18:59:42 +02:00
|
|
|
runGame :: RunParams -> IO ()
|
|
|
|
runGame rparams = do
|
2019-08-25 19:28:10 +02:00
|
|
|
app <- makeApp
|
2019-10-12 18:59:42 +02:00
|
|
|
gameSeed <- maybe getRandom pure $ seed rparams
|
|
|
|
let initialState = Game.initialStateFromSeed gameSeed &~ do
|
|
|
|
for_ (characterName rparams) $ \cn ->
|
|
|
|
Game.character . Character.characterName ?= cn
|
|
|
|
_game' <- defaultMain app initialState `finally` do
|
|
|
|
putStr "\n\n"
|
|
|
|
putStrLn "Thanks for playing Xanthous!"
|
|
|
|
when (isNothing $ seed rparams)
|
|
|
|
. putStrLn
|
|
|
|
$ "Seed: " <> tshow gameSeed
|
|
|
|
putStr "\n\n"
|
2019-08-25 19:28:10 +02:00
|
|
|
pure ()
|
2019-09-07 20:49:59 +02:00
|
|
|
|
2019-11-29 20:33:52 +01:00
|
|
|
loadGame :: FilePath -> IO ()
|
|
|
|
loadGame saveFile = do
|
|
|
|
app <- makeApp
|
|
|
|
gameState <- maybe (die "Invalid save file!") pure
|
|
|
|
=<< Game.loadGame . fromStrict <$> readFile @IO saveFile
|
|
|
|
_game' <- gameState `deepseq` defaultMain app gameState `finally` thanks
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
|
2019-09-07 20:49:59 +02:00
|
|
|
runGenerate :: GeneratorInput -> Dimensions -> IO ()
|
|
|
|
runGenerate input dims = do
|
|
|
|
randGen <- getStdGen
|
|
|
|
let res = generateFromInput input dims randGen
|
2019-09-13 21:24:05 +02:00
|
|
|
rs = regions $ amap not res
|
|
|
|
putStr "num regions: "
|
|
|
|
print $ length rs
|
|
|
|
putStr "region lengths: "
|
|
|
|
print $ length <$> rs
|
|
|
|
putStr "character position: "
|
|
|
|
print =<< chooseCharacterPosition res
|
2019-09-07 20:49:59 +02:00
|
|
|
putStrLn $ showCells res
|
|
|
|
|
|
|
|
runCommand :: Command -> IO ()
|
2019-10-12 18:59:42 +02:00
|
|
|
runCommand (Run runParams) = runGame runParams
|
2019-11-29 20:33:52 +01:00
|
|
|
runCommand (Load saveFile) = loadGame saveFile
|
2019-09-07 20:49:59 +02:00
|
|
|
runCommand (Generate input dims) = runGenerate input dims
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = runCommand =<< Opt.execParser optParser
|