Fill the outer edges of generated levels

To avoid the character being able to go OOB.

This is something we had in the Rust version but I hadn't ported over yet
This commit is contained in:
Griffin Smith 2019-09-14 15:16:27 -04:00
parent 33c831d23d
commit 6678ac986c
2 changed files with 13 additions and 1 deletions

View file

@ -98,6 +98,7 @@ generate' params dims = do
let steps' = params ^. steps let steps' = params ^. steps
when (steps' > 0) when (steps' > 0)
$ for_ [0 .. pred steps'] . const $ stepAutomata cells dims params $ for_ [0 .. pred steps'] . const $ stepAutomata cells dims params
lift $ fillOuterEdgesM cells
pure cells pure cells
stepAutomata :: forall s g. MCells s -> Dimensions -> Params -> CellM g s () stepAutomata :: forall s g. MCells s -> Dimensions -> Params -> CellM g s ()

View file

@ -7,6 +7,7 @@ module Xanthous.Generators.Util
, randInitialize , randInitialize
, numAliveNeighborsM , numAliveNeighborsM
, numAliveNeighbors , numAliveNeighbors
, fillOuterEdgesM
, cloneMArray , cloneMArray
, floodFill , floodFill
, regions , regions
@ -20,7 +21,7 @@ import Control.Monad.Random
import Data.Monoid import Data.Monoid
import Data.Foldable (Foldable, toList) import Data.Foldable (Foldable, toList)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Xanthous.Util (foldlMapM', between) import Xanthous.Util (foldlMapM')
import Xanthous.Data (Dimensions, width, height) import Xanthous.Data (Dimensions, width, height)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -93,6 +94,16 @@ numAliveNeighbors cells (x, y) =
neighborPositions :: [(Int, Int)] neighborPositions :: [(Int, Int)]
neighborPositions = [(i, j) | i <- [-1..1], j <- [-1..1], (i, j) /= (0, 0)] neighborPositions = [(i, j) | i <- [-1..1], j <- [-1..1], (i, j) /= (0, 0)]
fillOuterEdgesM :: (MArray a Bool m, Ix i, Ix j) => a (i, j) Bool -> m ()
fillOuterEdgesM arr = do
((minX, minY), (maxX, maxY)) <- getBounds arr
for_ (range (minX, maxX)) $ \x -> do
writeArray arr (x, minY) True
writeArray arr (x, maxY) True
for_ (range (minY, maxY)) $ \y -> do
writeArray arr (minX, y) True
writeArray arr (maxX, y) True
safeGet :: (IArray a e, Ix i) => a i e -> i -> Maybe e safeGet :: (IArray a e, Ix i) => a i e -> i -> Maybe e
safeGet arr idx = safeGet arr idx =
let (minIdx, maxIdx) = bounds arr let (minIdx, maxIdx) = bounds arr