{-# LANGUAGE RankNTypes, FlexibleContexts #-} {- This module is part of Antisplice. Copyleft (c) 2014 Marvin Cohrs All wrongs reversed. Sharing is an act of love, not crime. Please share Antisplice with everyone you like. Antisplice is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Antisplice is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Antisplice. If not, see . -} -- | Some utility functions for constructing paths between rooms. module Game.Antisplice.Paths where import Data.Monoid import Game.Antisplice.Action import Game.Antisplice.Monad.Dungeon import Game.Antisplice.Rooms import Game.Antisplice.Utils.Graph import Game.Antisplice.Utils.None -- | A one-way path from 'f' to 't'. unipath :: MonadDungeon m => NodeId -> NodeId -> Direction -> m () unipath f t d = establishWay f t d none -- | A mutual path. The specified direction is for the path from 'f' to 't', the opposite one is chosen for 't' to 'f'. bipath :: MonadDungeon m => NodeId -> NodeId -> Direction -> m () bipath f t d = unipath f t d >> unipath t f (opp d) where opp North = South opp NorthEast = SouthWest opp East = West opp SouthEast = NorthWest opp South = North opp SouthWest = NorthEast opp West = East opp NorthWest = SouthEast opp Up = Down opp Down = Up -- | A unipath that is guarded by a prerequisite. guardedPath :: MonadDungeon m => NodeId -> NodeId -> Direction -> Prerequisite -> m () guardedPath f t d p = establishWay f t d $ PathState p noneM noneM -- | A composable wrapper for a path state newtype Gate = Gate { runGate :: PathState } instance None Gate where none = Gate $ PathState (return True) noneM noneM instance Monoid Gate where mempty = none mappend g h = Gate $ PathState (runPrerequisite $ Prerequisite (pathPrerequisiteOf $ runGate g) <> Prerequisite (pathPrerequisiteOf $ runGate h)) (pathTriggerBeforeWalkOf (runGate g) >> pathTriggerBeforeWalkOf (runGate h)) (pathTriggerAfterWalkOf (runGate g) >> pathTriggerAfterWalkOf (runGate h)) instance IsAction Gate where g #&& h = Gate $ PathState (runPrerequisite $ Prerequisite (pathPrerequisiteOf $ runGate g) #&& Prerequisite (pathPrerequisiteOf $ runGate h)) (pathTriggerBeforeWalkOf (runGate g) >> pathTriggerBeforeWalkOf (runGate h)) (pathTriggerAfterWalkOf (runGate g) >> pathTriggerAfterWalkOf (runGate h)) g #|| h = Gate $ PathState (runPrerequisite $ Prerequisite (pathPrerequisiteOf $ runGate g) #|| Prerequisite (pathPrerequisiteOf $ runGate h)) (pathPrerequisiteOf (runGate g) >>= \b -> if b then pathTriggerBeforeWalkOf (runGate g) else pathTriggerBeforeWalkOf (runGate h)) (pathPrerequisiteOf (runGate g) >>= \b -> if b then pathTriggerAfterWalkOf (runGate g) else pathTriggerAfterWalkOf (runGate h)) g !&& h = Gate $ PathState (runPrerequisite $ Prerequisite (pathPrerequisiteOf $ runGate g) !&& Prerequisite (pathPrerequisiteOf $ runGate h)) (pathTriggerBeforeWalkOf (runGate g) >> pathTriggerBeforeWalkOf (runGate h)) (pathTriggerAfterWalkOf (runGate g) >> pathTriggerAfterWalkOf (runGate h)) g !|| h = Gate $ PathState (runPrerequisite $ Prerequisite (pathPrerequisiteOf $ runGate g) !|| Prerequisite (pathPrerequisiteOf $ runGate h)) (pathPrerequisiteOf (runGate g) >>= \b -> pathPrerequisiteOf (runGate h) >> if b then pathTriggerBeforeWalkOf (runGate g) else pathTriggerBeforeWalkOf (runGate h)) (pathPrerequisiteOf (runGate g) >>= \b -> pathPrerequisiteOf (runGate h) >> if b then pathTriggerAfterWalkOf (runGate g) else pathTriggerAfterWalkOf (runGate h)) -- | Typeclass for everything that may be converted to a gate. class Gatifiable g where toGate :: g -> Gate instance Gatifiable Gate where toGate = id instance Gatifiable PrerequisiteBox where toGate g = Gate $ PathState (runPrerequisite g) noneM noneM instance Gatifiable PathState where toGate g = Gate g instance Gatifiable ActionBefore where toGate (ActionBefore g) = Gate $ PathState (askAction g) (runAction g) noneM instance Gatifiable ActionAfter where toGate (ActionAfter g) = Gate $ PathState (askAction g) noneM (runAction g) gatedPath :: (Gatifiable g,MonadDungeon m) => NodeId -> NodeId -> Direction -> g -> m () gatedPath f t d g = establishWay f t d (runGate $ toGate g)