packages feed

Allure-0.4.2: src/Dungeon.hs

module Dungeon (Dungeon, fromList, currentFirst, adjust, (!)) where

import Data.Binary
import qualified Data.Map as M
import qualified Data.List as L

import Level
import WorldLoc

-- | The complete dungeon is a map from level names to levels.
-- We usually store all but the current level in this data structure.
newtype Dungeon = Dungeon{dungeonLevelMap :: M.Map LevelId Level}
  deriving Show

instance Binary Dungeon where
  put dng = put (M.assocs (dungeonLevelMap dng))
  get = fmap fromList get

-- | Create a dungeon from a list of levels.
fromList :: [(LevelId, Level)] -> Dungeon
fromList = Dungeon . M.fromList

-- | Association list corresponding to the dungeon.
-- Starts at the supplied level id (usually the current level)
-- to try to speed up the searches and keep the dungeon lazy.
currentFirst :: LevelId -> Dungeon -> [(LevelId, Level)]
currentFirst slevel (Dungeon m) =
  (slevel, m M.! slevel)
  : L.filter ((/= slevel) . fst) (M.assocs m)

adjust :: (Level -> Level) -> LevelId -> Dungeon -> Dungeon
adjust f ln (Dungeon m) = Dungeon (M.adjust f ln m)

(!) :: Dungeon -> LevelId -> Level
(!) (Dungeon m) slid = m M.! slid