about summary refs log tree commit diff
path: root/src/Main.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-12-30T16·31-0500
committerGriffin Smith <root@gws.fyi>2019-12-30T16·31-0500
commite76567b9e776070812838828d8de8220c2a461e7 (patch)
tree40a801de9684b39a3258f4f33f121b14dd407a64 /src/Main.hs
parent6f427fe4d6ba9a03f122d15839298040a7cfb925 (diff)
Add dungeon level generation
Add a dungeon level generator, which:

1. generates an infinite sequence of rectangular rooms within the
   dimensions of the level
2. removes any duplicates from that sequence
3. Generates a graph from the delaunay triangulation of the centerpoints
   of those rooms
4. Generates the minimum-spanning-tree of that delaunay triangulation,
   with weights given by line length in points
5. Adds back a subset (default 10-15%) of edges from the delaunay
   triangulation to the graph
6. Uses the resulting graph to draw corridors between the rooms, using a
   random point on the near edge of each room to pick the points of the
   corridors
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 2e9d8c41ee..b11f1b9f49 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -47,19 +47,22 @@ parseRunParams = RunParams
 data Command
   = Run RunParams
   | Load FilePath
-  | Generate GeneratorInput Dimensions
+  | Generate GeneratorInput Dimensions (Maybe Int)
 
 parseDimensions :: Opt.Parser Dimensions
 parseDimensions = Dimensions
   <$> Opt.option Opt.auto
        ( Opt.short 'w'
        <> Opt.long "width"
+       <> Opt.metavar "TILES"
        )
   <*> Opt.option Opt.auto
        ( Opt.short 'h'
        <> Opt.long "height"
+       <> Opt.metavar "TILES"
        )
 
+
 parseCommand :: Opt.Parser Command
 parseCommand = (<|> Run <$> parseRunParams) $ Opt.subparser
   $ Opt.command "run"
@@ -75,6 +78,8 @@ parseCommand = (<|> Run <$> parseRunParams) $ Opt.subparser
        (Generate
         <$> parseGeneratorInput
         <*> parseDimensions
+        <*> optional
+            (Opt.option Opt.auto (Opt.long "seed"))
         <**> Opt.helper
        )
        (Opt.progDesc "Generate a sample level"))
@@ -91,6 +96,9 @@ runGame :: RunParams -> IO ()
 runGame rparams = do
   app <- makeApp
   gameSeed <- maybe getRandom pure $ seed rparams
+  when (isNothing $ seed rparams)
+    . putStrLn
+    $ "Seed: " <> tshow gameSeed
   let initialState = Game.initialStateFromSeed gameSeed &~ do
         for_ (characterName rparams) $ \cn ->
           Game.character . Character.characterName ?= cn
@@ -112,11 +120,16 @@ loadGame saveFile = do
   pure ()
 
 
-runGenerate :: GeneratorInput -> Dimensions -> IO ()
-runGenerate input dims = do
-  randGen <- getStdGen
-  let res = generateFromInput input dims randGen
+runGenerate :: GeneratorInput -> Dimensions -> Maybe Int -> IO ()
+runGenerate input dims mSeed = do
+  putStrLn "Generating..."
+  genSeed <- maybe getRandom pure mSeed
+  let randGen = mkStdGen genSeed
+      res = generateFromInput input dims randGen
       rs = regions $ amap not res
+  when (isNothing mSeed)
+    . putStrLn
+    $ "Seed: " <> tshow genSeed
   putStr "num regions: "
   print $ length rs
   putStr "region lengths: "
@@ -128,7 +141,7 @@ runGenerate input dims = do
 runCommand :: Command -> IO ()
 runCommand (Run runParams) = runGame runParams
 runCommand (Load saveFile) = loadGame saveFile
-runCommand (Generate input dims) = runGenerate input dims
+runCommand (Generate input dims mSeed) = runGenerate input dims mSeed
 
 main :: IO ()
 main = runCommand =<< Opt.execParser optParser