about summary refs log tree commit diff
path: root/src/Xanthous
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-09-14T19·16-0400
committerGriffin Smith <root@gws.fyi>2019-09-14T19·16-0400
commit6678ac986c0ccdc2a809da4fc99de7bcc0eb21f4 (patch)
tree2e4e15477b2134736eadc9674d0578320515894c /src/Xanthous
parent33c831d23d09d1e80a1dcfacb373dcedec55f694 (diff)
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
Diffstat (limited to 'src/Xanthous')
-rw-r--r--src/Xanthous/Generators/CaveAutomata.hs1
-rw-r--r--src/Xanthous/Generators/Util.hs13
2 files changed, 13 insertions, 1 deletions
diff --git a/src/Xanthous/Generators/CaveAutomata.hs b/src/Xanthous/Generators/CaveAutomata.hs
index a2f0a165e3c1..fd4c68ddbe90 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 47ee81b2931e..6a2d27839cf6 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