From 6678ac986c0ccdc2a809da4fc99de7bcc0eb21f4 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sat, 14 Sep 2019 15:16:27 -0400 Subject: 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 --- src/Xanthous/Generators/CaveAutomata.hs | 1 + src/Xanthous/Generators/Util.hs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Xanthous/Generators/CaveAutomata.hs b/src/Xanthous/Generators/CaveAutomata.hs index a2f0a165e3..fd4c68ddbe 100644 --- a/src/Xanthous/Generators/CaveAutomata.hs +++ b/src/Xanthous/Generators/CaveAutomata.hs @@ -98,6 +98,7 @@ generate' params dims = do let steps' = params ^. steps when (steps' > 0) $ for_ [0 .. pred steps'] . const $ stepAutomata cells dims params + lift $ fillOuterEdgesM cells pure cells stepAutomata :: forall s g. MCells s -> Dimensions -> Params -> CellM g s () diff --git a/src/Xanthous/Generators/Util.hs b/src/Xanthous/Generators/Util.hs index 47ee81b293..6a2d27839c 100644 --- a/src/Xanthous/Generators/Util.hs +++ b/src/Xanthous/Generators/Util.hs @@ -7,6 +7,7 @@ module Xanthous.Generators.Util , randInitialize , numAliveNeighborsM , numAliveNeighbors + , fillOuterEdgesM , cloneMArray , floodFill , regions @@ -20,7 +21,7 @@ import Control.Monad.Random import Data.Monoid import Data.Foldable (Foldable, toList) -------------------------------------------------------------------------------- -import Xanthous.Util (foldlMapM', between) +import Xanthous.Util (foldlMapM') import Xanthous.Data (Dimensions, width, height) -------------------------------------------------------------------------------- @@ -93,6 +94,16 @@ numAliveNeighbors cells (x, y) = neighborPositions :: [(Int, Int)] 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 arr idx = let (minIdx, maxIdx) = bounds arr -- cgit 1.4.1