tinker a little with Map generation and display

This commit is contained in:
nek0 2024-05-01 18:04:47 +02:00
parent 13045c4ba9
commit 9b164e4f65
5 changed files with 31 additions and 12 deletions

18
src-lib/Library/Map.hs Normal file
View file

@ -0,0 +1,18 @@
module Library.Map where
import Data.Matrix
import Library.Types
prettyPrintMap
:: ServerMap
-> IO ()
prettyPrintMap tilemap = do
mapM_
(\row -> do
mapM_
(putStr . show)
row
putStr "\n"
)
(toLists tilemap)

View file

@ -31,7 +31,11 @@ instance FromJSON ClientMap
data Tile
= Air -- ^ walkable area
| Wall -- ^ obstacle
deriving (Show, Eq, Generic)
deriving (Eq, Generic)
instance Show Tile where
show Air = " "
show Wall = ""
instance ToJSON Tile

View file

@ -22,6 +22,7 @@ import Options.Applicative
-- internal imports
import Library.Map
import Library.Types
import Server.Communication
@ -48,7 +49,7 @@ main = do
putStrLn "-----------------------------------------------------------------------------------"
arena <- generateArena setMapRows setMapColumns setSpawnerProbability
putStrLn "generated arena:"
print (arenaMap arena)
prettyPrintMap (arenaMap arena)
putStrLn "-----------------------------------------------------------------------------------"
putStrLn "starting game…"
now <- getCurrentTime

View file

@ -61,11 +61,11 @@ generateMap mapRows mapColumns = do
where
divide :: Int -> Int -> Int -> Int -> ServerMap -> IO ServerMap
divide rowOffset colOffset rowSize colSize tilemap
| rowSize <= 3 || colSize <= 3 = pure tilemap
| rowSize <= 4 || colSize <= 4 = pure tilemap
| otherwise = do
-- position wall intersection at random
crossRow <- randomRIO (2, rowSize - 1)
crossCol <- randomRIO (2, colSize - 1)
crossRow <- randomRIO (2, rowSize - 2)
crossCol <- randomRIO (2, colSize - 2)
-- position wall passages at random wall tiles
rowHole1 <- randomRIO (1, crossRow - 1)
@ -74,18 +74,13 @@ generateMap mapRows mapColumns = do
colHole2 <- randomRIO (crossCol + 1, colSize - 1)
-- determine all possible passages
let allHoles =
let holes =
[ (rowHole1, crossCol)
, (rowHole2, crossCol)
, (crossRow, colHole1)
, (crossRow, colHole2)
]
-- drop one of the passages randomly
randomDropIndex <- randomRIO (0, length allHoles - 1)
let randomDrop = allHoles !! randomDropIndex
holes = filter (/= randomDrop) allHoles
crossedMap = M.mapPos
(\(r, c) tile ->
if (r - rowOffset == crossRow && c - colOffset > 0 && c - colOffset <= colSize) ||

View file

@ -18,7 +18,8 @@ common warnings
library
import: warnings
exposed-modules: Library.Types
exposed-modules: Library.Map
Library.Types
Library.Types.Communication
Library.Types.Map
Library.Types.Player