about summary refs log tree commit diff
path: root/src/Xanthous/Generators/LevelContents.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-09-13T19·24-0400
committerGriffin Smith <root@gws.fyi>2019-09-13T19·24-0400
commitc06edf3cc698f36e995719dc6e192c5663110f6d (patch)
treedd7872fbad6dc4b0e04f943ef63a64c3e002479b /src/Xanthous/Generators/LevelContents.hs
parent9ebdc6fbb446fea5e505172a6b3dd459beaf3552 (diff)
Place the chacracter in the level at startup time
Randomly select a position in the largest contiguous region of the
generated level in which to place the character at startup time.
Diffstat (limited to 'src/Xanthous/Generators/LevelContents.hs')
-rw-r--r--src/Xanthous/Generators/LevelContents.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/Xanthous/Generators/LevelContents.hs b/src/Xanthous/Generators/LevelContents.hs
new file mode 100644
index 000000000000..f8d9b8a2045a
--- /dev/null
+++ b/src/Xanthous/Generators/LevelContents.hs
@@ -0,0 +1,26 @@
+--------------------------------------------------------------------------------
+module Xanthous.Generators.LevelContents
+  ( chooseCharacterPosition
+  ) where
+--------------------------------------------------------------------------------
+import Xanthous.Prelude
+--------------------------------------------------------------------------------
+import Control.Monad.Random
+import Data.Array.IArray (amap)
+--------------------------------------------------------------------------------
+import Xanthous.Generators.Util
+import Xanthous.Random
+--------------------------------------------------------------------------------
+
+chooseCharacterPosition :: MonadRandom m => Cells -> m (Word, Word)
+chooseCharacterPosition cells = choose $ impureNonNull candidates
+  where
+    -- cells ends up with true = wall, we want true = can put a character here
+    placeableCells = amap not cells
+
+    -- find the largest contiguous region of cells in the cave.
+    candidates
+      = maximumBy (compare `on` length)
+      $ fromMaybe (error "No regions generated! this should never happen.")
+      $ fromNullable
+      $ regions placeableCells