module Game.Antisplice.Action where
import Control.Monad
import Data.Monoid
import Game.Antisplice.Monad.Dungeon
import Game.Antisplice.Rooms
import Game.Antisplice.Stats
import Game.Antisplice.Utils.Counter
import Game.Antisplice.Utils.None
class IsAction a where
infixl 6 #&&
(#&&) :: a -> a -> a
infixl 6 #||
(#||) :: a -> a -> a
infixl 6 !&&
(!&&) :: a -> a -> a
infixl 6 !||
(!||) :: a -> a -> a
instance Monoid PrerequisiteBox where
mempty = none
p `mappend` q = Prerequisite $ do
a <- runPrerequisite p
b <- runPrerequisite q
return (a && b)
instance IsAction PrerequisiteBox where
p #&& q = Prerequisite $ do
a <- runPrerequisite p
if a then runPrerequisite q
else return False
p #|| q = Prerequisite $ do
a <- runPrerequisite p
if a then return True
else runPrerequisite q
p !&& q = p <> q
p !|| q = Prerequisite $ do
a <- runPrerequisite p
b <- runPrerequisite q
return (a || b)
data Action = Action { askAction :: Prerequisite, runAction :: Handler }
instance None Action where
none = Action (return True) noneM
instance Monoid Action where
mempty = none
a `mappend` b = Action (runPrerequisite $ Prerequisite (askAction a) <> Prerequisite (askAction b)) (runAction a >> runAction b)
instance IsAction Action where
a #&& b = Action
(runPrerequisite $ Prerequisite (askAction a) #&& Prerequisite (askAction b))
(runAction a >> runAction b)
a #|| b = Action
(runPrerequisite $ Prerequisite (askAction a) #|| Prerequisite (askAction b))
(askAction a >>= \q -> if q then runAction a else runAction b)
a !&& b = a <> b
a !|| b = Action
(runPrerequisite $ Prerequisite (askAction a) !|| Prerequisite (askAction b))
(askAction a >>= \q -> askAction b >> if q then runAction a else runAction b)
newtype ActionAfter = ActionAfter { runActionAfter :: Action }
newtype ActionBefore = ActionBefore { runActionBefore :: Action }
instance None ActionAfter where
none = ActionAfter none
instance None ActionBefore where
none = ActionBefore none
instance Monoid ActionAfter where
mempty = none
a `mappend` b = ActionAfter $ runActionAfter a <> runActionAfter b
instance Monoid ActionBefore where
mempty = none
a `mappend` b = ActionBefore $ runActionBefore a <> runActionBefore b
instance IsAction ActionAfter where
a #&& b = ActionAfter $ runActionAfter a #&& runActionAfter b
a #|| b = ActionAfter $ runActionAfter a #|| runActionAfter b
a !&& b = ActionAfter $ runActionAfter a !&& runActionAfter b
a !|| b = ActionAfter $ runActionAfter a !|| runActionAfter b
instance IsAction ActionBefore where
a #&& b = ActionBefore $ runActionBefore a #&& runActionBefore b
a #|| b = ActionBefore $ runActionBefore a #|| runActionBefore b
a !&& b = ActionBefore $ runActionBefore a !&& runActionBefore b
a !|| b = ActionBefore $ runActionBefore a !|| runActionBefore b
consumeCurrencyA :: CurrencyId -> Int -> Action
consumeCurrencyA c h = Action
(do
c1 <- getCurrency c
return (c1 > h))
(modifyCurrency c (subtract h))
dealDamageA :: ChattyDungeonM Int -> Action
dealDamageA m = Action (return True) (dealDamage =<< m)
implyCooldownA :: MonadCounter m => Integer -> m Action
implyCooldownA ms = do
cid <- liftM CooldownId countOn
return (Action
(liftM not $ getCooldown cid)
(setCooldown cid True >> schedule ms (setCooldown cid False)))
implyGlobalCooldownA :: Action
implyGlobalCooldownA = Action
(liftM not $ getCooldown GlobalCooldown)
(do
setCooldown GlobalCooldown True
cd <- calcStat CooldownDuration
schedule (fromIntegral cd) $ setCooldown GlobalCooldown False)