2013-11-29 13:21:58 +04:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2013-10-25 01:40:48 +04:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Graphics.Vty
|
|
|
|
|
|
|
|
import Data.Array
|
|
|
|
import qualified Data.ByteString as B
|
2014-01-27 01:25:13 +04:00
|
|
|
import Data.Default (def)
|
2013-10-25 01:40:48 +04:00
|
|
|
import Data.Word
|
|
|
|
|
|
|
|
import Control.Applicative
|
2013-11-29 13:21:58 +04:00
|
|
|
import Control.Lens hiding (Level)
|
2013-10-25 01:40:48 +04:00
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.RWS
|
|
|
|
import Control.Monad.Writer
|
|
|
|
|
|
|
|
import System.IO
|
|
|
|
import System.Random
|
|
|
|
|
2013-11-20 23:43:13 +04:00
|
|
|
data Dude = Dude
|
2014-04-12 04:51:13 +04:00
|
|
|
{ dudeX :: Int
|
|
|
|
, dudeY :: Int
|
2013-11-20 23:43:13 +04:00
|
|
|
} deriving (Show,Eq)
|
2013-10-25 01:40:48 +04:00
|
|
|
|
|
|
|
data World = World
|
|
|
|
{ dude :: Dude
|
|
|
|
, level :: Level
|
|
|
|
}
|
|
|
|
deriving (Show,Eq)
|
|
|
|
|
|
|
|
data Level = Level
|
2014-08-02 02:28:16 +04:00
|
|
|
{ levelStart :: (Int, Int)
|
|
|
|
, levelEnd :: (Int, Int)
|
|
|
|
, levelGeo :: Array (Int, Int) LevelPiece
|
2013-11-21 03:53:15 +04:00
|
|
|
-- building the geo image is expensive. Cache it. Though VTY should go through greater lengths
|
|
|
|
-- to avoid the need to cache images.
|
2014-08-02 02:28:16 +04:00
|
|
|
, levelGeoImage :: Image
|
2013-10-25 01:40:48 +04:00
|
|
|
}
|
|
|
|
deriving (Show,Eq)
|
|
|
|
|
|
|
|
data LevelPiece
|
|
|
|
= EmptySpace
|
|
|
|
| Rock
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
type Game = RWST Vty () World IO
|
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
main :: IO ()
|
2013-10-25 01:40:48 +04:00
|
|
|
main = do
|
2014-01-27 01:25:13 +04:00
|
|
|
vty <- mkVty def
|
2014-04-12 04:51:13 +04:00
|
|
|
level0 <- mkLevel 1
|
2014-08-02 02:28:16 +04:00
|
|
|
let world0 = World (Dude (fst $ levelStart level0) (snd $ levelStart level0)) level0
|
2014-04-12 04:51:13 +04:00
|
|
|
(_finalWorld, ()) <- execRWST (play >> updateDisplay) vty world0
|
2013-10-25 01:40:48 +04:00
|
|
|
shutdown vty
|
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
mkLevel :: Int -> IO Level
|
2013-11-25 07:48:40 +04:00
|
|
|
mkLevel difficulty = do
|
|
|
|
let size = 80 * difficulty
|
2014-04-12 04:51:13 +04:00
|
|
|
[levelWidth, levelHeight] <- replicateM 2 $ randomRIO (size,size)
|
|
|
|
let randomP = (,) <$> randomRIO (2, levelWidth-3) <*> randomRIO (2, levelHeight-3)
|
2013-11-25 07:48:40 +04:00
|
|
|
start <- randomP
|
|
|
|
end <- randomP
|
2013-10-26 20:43:16 +04:00
|
|
|
-- first the base geography: all rocks
|
2014-08-02 02:13:34 +04:00
|
|
|
let baseGeo = array ((0,0), (levelWidth-1, levelHeight-1))
|
2014-04-12 04:51:13 +04:00
|
|
|
[((x,y),Rock) | x <- [0..levelWidth-1], y <- [0..levelHeight-1]]
|
2013-11-20 23:43:13 +04:00
|
|
|
-- next the empty spaces that make the rooms
|
2013-11-25 07:48:40 +04:00
|
|
|
-- for this we generate a number of center points
|
|
|
|
centers <- replicateM (2 ^ difficulty + difficulty) randomP
|
|
|
|
-- generate rooms for all those points, plus the start and end
|
2014-04-12 04:51:13 +04:00
|
|
|
geo <- foldM (addRoom levelWidth levelHeight) baseGeo (start : end : centers)
|
|
|
|
return $ Level start end geo (buildGeoImage geo)
|
2013-11-20 23:43:13 +04:00
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
addRoom :: Int -> Int -> Array (Int, Int) LevelPiece -> (Int, Int)
|
|
|
|
-> IO (Array (Int, Int) LevelPiece)
|
2014-04-12 04:51:13 +04:00
|
|
|
addRoom levelWidth levelHeight geo (centerX, centerY) = do
|
2013-11-20 23:43:13 +04:00
|
|
|
size <- randomRIO (5,15)
|
2014-04-12 04:51:13 +04:00
|
|
|
let xMin = max 1 (centerX - size)
|
|
|
|
xMax = min (levelWidth - 1) (centerX + size)
|
|
|
|
yMin = max 1 (centerY - size)
|
|
|
|
yMax = min (levelHeight - 1) (centerY + size)
|
|
|
|
let room = [((x,y), EmptySpace) | x <- [xMin..xMax - 1], y <- [yMin..yMax - 1]]
|
2013-11-20 23:56:27 +04:00
|
|
|
return (geo // room)
|
2013-10-25 01:40:48 +04:00
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
imageForGeo :: LevelPiece -> Image
|
2014-04-12 04:51:13 +04:00
|
|
|
imageForGeo EmptySpace = char (defAttr `withBackColor` green) ' '
|
|
|
|
imageForGeo Rock = char defAttr 'X'
|
2013-10-25 01:40:48 +04:00
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
pieceA, dumpA :: Attr
|
2014-04-12 04:51:13 +04:00
|
|
|
pieceA = defAttr `withForeColor` blue `withBackColor` green
|
|
|
|
dumpA = defAttr `withStyle` reverseVideo
|
2013-10-25 01:40:48 +04:00
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
play :: Game ()
|
2013-10-25 01:40:48 +04:00
|
|
|
play = do
|
2014-04-12 04:51:13 +04:00
|
|
|
updateDisplay
|
|
|
|
done <- processEvent
|
2013-10-25 01:40:48 +04:00
|
|
|
unless done play
|
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
processEvent :: Game Bool
|
2014-04-12 04:51:13 +04:00
|
|
|
processEvent = do
|
|
|
|
k <- ask >>= liftIO . nextEvent
|
2013-10-25 01:40:48 +04:00
|
|
|
if k == EvKey KEsc []
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
case k of
|
2014-01-19 12:19:56 +04:00
|
|
|
EvKey (KChar 'r') [MCtrl] -> ask >>= liftIO . refresh
|
2014-04-12 04:51:13 +04:00
|
|
|
EvKey KLeft [] -> moveDude (-1) 0
|
|
|
|
EvKey KRight [] -> moveDude 1 0
|
|
|
|
EvKey KUp [] -> moveDude 0 (-1)
|
|
|
|
EvKey KDown [] -> moveDude 0 1
|
2013-10-25 01:40:48 +04:00
|
|
|
_ -> return ()
|
|
|
|
return False
|
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
moveDude :: Int -> Int -> Game ()
|
2014-04-12 04:51:13 +04:00
|
|
|
moveDude dx dy = do
|
2013-10-25 01:40:48 +04:00
|
|
|
world <- get
|
|
|
|
let Dude x y = dude world
|
2013-11-20 23:56:27 +04:00
|
|
|
let x' = x + dx
|
|
|
|
y' = y + dy
|
|
|
|
-- this is only valid because the level generation assures the border is always Rock
|
2014-08-02 02:28:16 +04:00
|
|
|
case levelGeo (level world) ! (x',y') of
|
2013-11-20 23:56:27 +04:00
|
|
|
EmptySpace -> put $ world { dude = Dude x' y' }
|
|
|
|
_ -> return ()
|
2013-10-25 01:40:48 +04:00
|
|
|
|
2014-04-12 04:51:13 +04:00
|
|
|
updateDisplay :: Game ()
|
|
|
|
updateDisplay = do
|
|
|
|
let info = string defAttr "Move with the arrows keys. Press ESC to exit."
|
2013-11-20 23:43:13 +04:00
|
|
|
-- determine offsets to place the dude in the center of the level.
|
2014-04-12 04:51:13 +04:00
|
|
|
(w,h) <- asks outputIface >>= liftIO . displayBounds
|
|
|
|
theDude <- gets dude
|
|
|
|
let ox = (w `div` 2) - dudeX theDude
|
|
|
|
oy = (h `div` 2) - dudeY theDude
|
2013-11-20 23:43:13 +04:00
|
|
|
-- translate the world images to place the dude in the center of the level.
|
2014-08-02 02:28:16 +04:00
|
|
|
world' <- map (translate ox oy) <$> worldImages
|
2014-04-12 04:51:13 +04:00
|
|
|
let pic = picForLayers $ info : world'
|
2013-11-20 23:43:13 +04:00
|
|
|
vty <- ask
|
|
|
|
liftIO $ update vty pic
|
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
worldImages :: Game [Image]
|
|
|
|
worldImages = do
|
2014-04-12 04:51:13 +04:00
|
|
|
theDude <- gets dude
|
|
|
|
theLevel <- gets level
|
|
|
|
let dudeImage = translate (dudeX theDude) (dudeY theDude) (char pieceA '@')
|
2014-08-02 02:28:16 +04:00
|
|
|
return [dudeImage, levelGeoImage theLevel]
|
2013-11-21 03:53:15 +04:00
|
|
|
|
2014-08-02 02:28:16 +04:00
|
|
|
buildGeoImage :: Array (Int, Int) LevelPiece -> Image
|
2014-04-12 04:51:13 +04:00
|
|
|
buildGeoImage geo =
|
|
|
|
let (geoWidth, geoHeight) = snd $ bounds geo
|
2013-11-21 03:53:15 +04:00
|
|
|
-- seems like a the repeated index operation should be removable. This is not performing random
|
|
|
|
-- access but (presumably) access in order of index.
|
2014-04-12 04:51:13 +04:00
|
|
|
in vertCat [ geoRow
|
|
|
|
| y <- [0..geoHeight-1]
|
|
|
|
, let geoRow = horizCat [ i
|
|
|
|
| x <- [0..geoWidth-1]
|
|
|
|
, let i = imageForGeo (geo ! (x,y))
|
|
|
|
]
|
|
|
|
]
|