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.
This commit is contained in:
Griffin Smith 2019-09-13 15:24:05 -04:00
parent 9ebdc6fbb4
commit c06edf3cc6
9 changed files with 171 additions and 34 deletions

View file

@ -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