{-# LANGUAGE DeriveGeneric #-}
-- | Actors in the game: heroes, monsters, etc.
module Game.LambdaHack.Common.Actor
  ( -- * The@ Acto@r type, its components and operations on them
    Actor(..), ResDelta(..), ActorMaxSkills, Watchfulness(..)
  , deltasSerious, deltasHears, deltaBenign, deltaWasBenign, actorCanMelee
  , gearSpeed, actorTemplate, actorWaits, actorWaitsOrSleeps, actorDying
  , hpTooLow, calmEnough, hpEnough, hpFull, canSleep, prefersSleep
  , checkAdjacent, eqpOverfull, eqpFreeN
    -- * Assorted
  , ActorDict, monsterGenChance, smellTimeout
  ) where

import Prelude ()

import Game.LambdaHack.Core.Prelude

import           Data.Binary
import qualified Data.EnumMap.Strict as EM
import           Data.Int (Int64)
import           Data.Ratio
import           GHC.Generics (Generic)

import qualified Game.LambdaHack.Definition.Ability as Ability
import qualified Game.LambdaHack.Core.Dice as Dice
import           Game.LambdaHack.Common.Item
import           Game.LambdaHack.Common.Misc
import           Game.LambdaHack.Common.Point
import           Game.LambdaHack.Core.Random
import           Game.LambdaHack.Common.Time
import           Game.LambdaHack.Common.Types
import           Game.LambdaHack.Common.Vector

-- | Actor attributes that are changing throughout the game.
-- If they appear to be dublets of aspects from actor kinds, e.g. HP,
-- they may be results of casting the dice specified in their respective
-- actor kind and/or may be modified temporarily, but return
-- to the original value from their respective kind over time.
--
-- Other properties of an actor, in particular its current aspects,
-- are derived from the actor's trunk, organs and equipment.
-- A class of the aspects, the boolean ones, are called flags.
-- Another class are skills. Stats are a subclass that determines
-- if particular actions are permitted for the actor (or faction).
data Actor = Actor
  { -- The trunk of the actor's body (present also in @borgan@ or @beqp@)
    Actor -> ItemId
btrunk      :: ItemId       -- ^ the trunk organ of the actor's body

    -- Resources
  , Actor -> Int64
bhp         :: Int64        -- ^ current hit points * 1M
  , Actor -> ResDelta
bhpDelta    :: ResDelta     -- ^ HP delta this turn * 1M
  , Actor -> Int64
bcalm       :: Int64        -- ^ current calm * 1M
  , Actor -> ResDelta
bcalmDelta  :: ResDelta     -- ^ calm delta this turn * 1M

    -- Location
  , Actor -> Point
bpos        :: Point        -- ^ current position
  , Actor -> Maybe Point
boldpos     :: Maybe Point  -- ^ previous position, if any
  , Actor -> LevelId
blid        :: LevelId      -- ^ current level
  , Actor -> FactionId
bfid        :: FactionId    -- ^ faction the actor currently belongs to
  , Actor -> Maybe ([Vector], Speed)
btrajectory :: Maybe ([Vector], Speed)
                                -- ^ trajectory the actor must
                                --   travel and his travel speed

    -- Items
  , Actor -> ItemBag
borgan      :: ItemBag      -- ^ organs
  , Actor -> ItemBag
beqp        :: ItemBag      -- ^ personal equipment
  , Actor -> ItemBag
binv        :: ItemBag      -- ^ personal inventory pack
  , Actor -> Int
bweapon     :: Int          -- ^ number of weapons among eqp and organs

    -- Assorted
  , Actor -> Watchfulness
bwatch      :: Watchfulness -- ^ state of the actor's watchfulness
  , Actor -> Bool
bproj       :: Bool         -- ^ is a projectile? affects being able
                                --   to fly through other projectiles, etc.
  }
  deriving (Int -> Actor -> ShowS
[Actor] -> ShowS
Actor -> String
(Int -> Actor -> ShowS)
-> (Actor -> String) -> ([Actor] -> ShowS) -> Show Actor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Actor] -> ShowS
$cshowList :: [Actor] -> ShowS
show :: Actor -> String
$cshow :: Actor -> String
showsPrec :: Int -> Actor -> ShowS
$cshowsPrec :: Int -> Actor -> ShowS
Show, Actor -> Actor -> Bool
(Actor -> Actor -> Bool) -> (Actor -> Actor -> Bool) -> Eq Actor
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Actor -> Actor -> Bool
$c/= :: Actor -> Actor -> Bool
== :: Actor -> Actor -> Bool
$c== :: Actor -> Actor -> Bool
Eq, (forall x. Actor -> Rep Actor x)
-> (forall x. Rep Actor x -> Actor) -> Generic Actor
forall x. Rep Actor x -> Actor
forall x. Actor -> Rep Actor x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Actor x -> Actor
$cfrom :: forall x. Actor -> Rep Actor x
Generic)

instance Binary Actor

-- | Representation of recent changes to HP of Calm of an actor.
-- This is reset every time the actor perfoms an action, so this is
-- aggregated over actor turn (move), not time turn.
-- The resource changes recorded in the tuple are, respectively,
-- negative and positive.
data ResDelta = ResDelta
  { ResDelta -> (Int64, Int64)
resCurrentTurn  :: (Int64, Int64)  -- ^ resource change this move
  , ResDelta -> (Int64, Int64)
resPreviousTurn :: (Int64, Int64)  -- ^ resource change previous move
  }
  deriving (Int -> ResDelta -> ShowS
[ResDelta] -> ShowS
ResDelta -> String
(Int -> ResDelta -> ShowS)
-> (ResDelta -> String) -> ([ResDelta] -> ShowS) -> Show ResDelta
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ResDelta] -> ShowS
$cshowList :: [ResDelta] -> ShowS
show :: ResDelta -> String
$cshow :: ResDelta -> String
showsPrec :: Int -> ResDelta -> ShowS
$cshowsPrec :: Int -> ResDelta -> ShowS
Show, ResDelta -> ResDelta -> Bool
(ResDelta -> ResDelta -> Bool)
-> (ResDelta -> ResDelta -> Bool) -> Eq ResDelta
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ResDelta -> ResDelta -> Bool
$c/= :: ResDelta -> ResDelta -> Bool
== :: ResDelta -> ResDelta -> Bool
$c== :: ResDelta -> ResDelta -> Bool
Eq, (forall x. ResDelta -> Rep ResDelta x)
-> (forall x. Rep ResDelta x -> ResDelta) -> Generic ResDelta
forall x. Rep ResDelta x -> ResDelta
forall x. ResDelta -> Rep ResDelta x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ResDelta x -> ResDelta
$cfrom :: forall x. ResDelta -> Rep ResDelta x
Generic)

instance Binary ResDelta

type ActorMaxSkills = EM.EnumMap ActorId Ability.Skills

-- | All actors on the level, indexed by actor identifier.
type ActorDict = EM.EnumMap ActorId Actor

data Watchfulness = WWatch | WWait Int | WSleep | WWake
  deriving (Int -> Watchfulness -> ShowS
[Watchfulness] -> ShowS
Watchfulness -> String
(Int -> Watchfulness -> ShowS)
-> (Watchfulness -> String)
-> ([Watchfulness] -> ShowS)
-> Show Watchfulness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Watchfulness] -> ShowS
$cshowList :: [Watchfulness] -> ShowS
show :: Watchfulness -> String
$cshow :: Watchfulness -> String
showsPrec :: Int -> Watchfulness -> ShowS
$cshowsPrec :: Int -> Watchfulness -> ShowS
Show, Watchfulness -> Watchfulness -> Bool
(Watchfulness -> Watchfulness -> Bool)
-> (Watchfulness -> Watchfulness -> Bool) -> Eq Watchfulness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Watchfulness -> Watchfulness -> Bool
$c/= :: Watchfulness -> Watchfulness -> Bool
== :: Watchfulness -> Watchfulness -> Bool
$c== :: Watchfulness -> Watchfulness -> Bool
Eq, (forall x. Watchfulness -> Rep Watchfulness x)
-> (forall x. Rep Watchfulness x -> Watchfulness)
-> Generic Watchfulness
forall x. Rep Watchfulness x -> Watchfulness
forall x. Watchfulness -> Rep Watchfulness x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Watchfulness x -> Watchfulness
$cfrom :: forall x. Watchfulness -> Rep Watchfulness x
Generic)

instance Binary Watchfulness

deltasSerious :: ResDelta -> Bool
deltasSerious :: ResDelta -> Bool
deltasSerious ResDelta{..} = (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resCurrentTurn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
minusM2
                             Bool -> Bool -> Bool
|| (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resPreviousTurn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
minusM2

deltasHears :: ResDelta -> Bool
deltasHears :: ResDelta -> Bool
deltasHears ResDelta{..} = (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resCurrentTurn Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
minusM1
                           Bool -> Bool -> Bool
|| (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resPreviousTurn Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
minusM1

deltaBenign :: ResDelta -> Bool
deltaBenign :: ResDelta -> Bool
deltaBenign ResDelta{(Int64, Int64)
resCurrentTurn :: (Int64, Int64)
resCurrentTurn :: ResDelta -> (Int64, Int64)
resCurrentTurn} =
  (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resCurrentTurn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= 0  -- only the current one

deltaWasBenign :: ResDelta -> Bool
deltaWasBenign :: ResDelta -> Bool
deltaWasBenign ResDelta{(Int64, Int64)
resPreviousTurn :: (Int64, Int64)
resPreviousTurn :: ResDelta -> (Int64, Int64)
resPreviousTurn} =
  (Int64, Int64) -> Int64
forall a b. (a, b) -> a
fst (Int64, Int64)
resPreviousTurn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= 0  -- only the previous one

actorCanMelee :: ActorMaxSkills -> ActorId -> Actor -> Bool
actorCanMelee :: ActorMaxSkills -> ActorId -> Actor -> Bool
actorCanMelee actorMaxSkills :: ActorMaxSkills
actorMaxSkills aid :: ActorId
aid b :: Actor
b =
  let actorMaxSk :: Skills
actorMaxSk = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
aid
      condUsableWeapon :: Bool
condUsableWeapon = Actor -> Int
bweapon Actor
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
      canMelee :: Bool
canMelee = Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMelee Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
  in Bool
condUsableWeapon Bool -> Bool -> Bool
&& Bool
canMelee

-- | The speed from organs and gear; being pushed is ignored.
gearSpeed :: Ability.Skills -> Speed
gearSpeed :: Skills -> Speed
gearSpeed actorMaxSk :: Skills
actorMaxSk = Int -> Speed
toSpeed (Int -> Speed) -> Int -> Speed
forall a b. (a -> b) -> a -> b
$
  Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
minSpeed (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkSpeed Skills
actorMaxSk)  -- see @minimalSpeed@

actorTemplate :: ItemId -> Int64 -> Int64 -> Point -> LevelId -> FactionId
              -> Bool
              -> Actor
actorTemplate :: ItemId
-> Int64 -> Int64 -> Point -> LevelId -> FactionId -> Bool -> Actor
actorTemplate btrunk :: ItemId
btrunk bhp :: Int64
bhp bcalm :: Int64
bcalm bpos :: Point
bpos blid :: LevelId
blid bfid :: FactionId
bfid bproj :: Bool
bproj =
  let btrajectory :: Maybe a
btrajectory = Maybe a
forall a. Maybe a
Nothing
      boldpos :: Maybe a
boldpos = Maybe a
forall a. Maybe a
Nothing
      borgan :: EnumMap k a
borgan = EnumMap k a
forall k a. EnumMap k a
EM.empty
      beqp :: EnumMap k a
beqp = EnumMap k a
forall k a. EnumMap k a
EM.empty
      binv :: EnumMap k a
binv = EnumMap k a
forall k a. EnumMap k a
EM.empty
      bweapon :: Int
bweapon = 0
      bwatch :: Watchfulness
bwatch = Watchfulness
WWatch  -- overriden elsewhere, sometimes
      bhpDelta :: ResDelta
bhpDelta = (Int64, Int64) -> (Int64, Int64) -> ResDelta
ResDelta (0, 0) (0, 0)
      bcalmDelta :: ResDelta
bcalmDelta = (Int64, Int64) -> (Int64, Int64) -> ResDelta
ResDelta (0, 0) (0, 0)
  in $WActor :: ItemId
-> Int64
-> ResDelta
-> Int64
-> ResDelta
-> Point
-> Maybe Point
-> LevelId
-> FactionId
-> Maybe ([Vector], Speed)
-> ItemBag
-> ItemBag
-> ItemBag
-> Int
-> Watchfulness
-> Bool
-> Actor
Actor{..}

actorWaits :: Actor -> Bool
{-# INLINE actorWaits #-}
actorWaits :: Actor -> Bool
actorWaits b :: Actor
b = case Actor -> Watchfulness
bwatch Actor
b of
  WWait{} -> Bool
True
  _ -> Bool
False

actorWaitsOrSleeps :: Actor -> Bool
{-# INLINE actorWaitsOrSleeps #-}
actorWaitsOrSleeps :: Actor -> Bool
actorWaitsOrSleeps b :: Actor
b = case Actor -> Watchfulness
bwatch Actor
b of
  WWait{} -> Bool
True
  WSleep -> Bool
True
  _ -> Bool
False

actorDying :: Actor -> Bool
actorDying :: Actor -> Bool
actorDying b :: Actor
b = Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= 0
               Bool -> Bool -> Bool
|| Actor -> Bool
bproj Actor
b Bool -> Bool -> Bool
&& Bool
-> (([Vector], Speed) -> Bool) -> Maybe ([Vector], Speed) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True ([Vector] -> Bool
forall a. [a] -> Bool
null ([Vector] -> Bool)
-> (([Vector], Speed) -> [Vector]) -> ([Vector], Speed) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Vector], Speed) -> [Vector]
forall a b. (a, b) -> a
fst) (Actor -> Maybe ([Vector], Speed)
btrajectory Actor
b)

hpTooLow :: Actor -> Ability.Skills -> Bool
hpTooLow :: Actor -> Skills -> Bool
hpTooLow b :: Actor
b actorMaxSk :: Skills
actorMaxSk =
  5 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Int64
xM (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMaxHP Skills
actorMaxSk) Bool -> Bool -> Bool
&& Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Int64
xM 40
  Bool -> Bool -> Bool
|| Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
oneM

calmEnough :: Actor -> Ability.Skills -> Bool
calmEnough :: Actor -> Skills -> Bool
calmEnough b :: Actor
b actorMaxSk :: Skills
actorMaxSk =
  let calmMax :: Int
calmMax = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 1 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMaxCalm Skills
actorMaxSk
  in 2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int -> Int64
xM Int
calmMax Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= 3 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Actor -> Int64
bcalm Actor
b Bool -> Bool -> Bool
&& Actor -> Int64
bcalm Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int64
xM 10

hpEnough :: Actor -> Ability.Skills -> Bool
hpEnough :: Actor -> Skills -> Bool
hpEnough b :: Actor
b actorMaxSk :: Skills
actorMaxSk =
  Int -> Int64
xM (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMaxHP Skills
actorMaxSk) Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= 2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Actor -> Int64
bhp Actor
b Bool -> Bool -> Bool
&& Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
oneM

hpFull :: Actor -> Ability.Skills -> Bool
hpFull :: Actor -> Skills -> Bool
hpFull b :: Actor
b actorMaxSk :: Skills
actorMaxSk = Int -> Int64
xM (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMaxHP Skills
actorMaxSk) Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Actor -> Int64
bhp Actor
b

-- | Has the skill and can wake up easily, so can sleep safely.
canSleep :: Ability.Skills -> Bool
canSleep :: Skills -> Bool
canSleep actorMaxSk :: Skills
actorMaxSk = Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkWait Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 3
                      Bool -> Bool -> Bool
&& (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkSight Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
                          Bool -> Bool -> Bool
|| Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkHearing Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0)
                      Bool -> Bool -> Bool
&& Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkAggression Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 2

-- | Can't loot, so sometimes prefers to sleep instead of exploring.
prefersSleep :: Ability.Skills -> Bool
prefersSleep :: Skills -> Bool
prefersSleep actorMaxSk :: Skills
actorMaxSk = Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkMoveItem Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0

checkAdjacent :: Actor -> Actor -> Bool
checkAdjacent :: Actor -> Actor -> Bool
checkAdjacent sb :: Actor
sb tb :: Actor
tb = Actor -> LevelId
blid Actor
sb LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== Actor -> LevelId
blid Actor
tb Bool -> Bool -> Bool
&& Point -> Point -> Bool
adjacent (Actor -> Point
bpos Actor
sb) (Actor -> Point
bpos Actor
tb)

eqpOverfull :: Actor -> Int -> Bool
eqpOverfull :: Actor -> Int -> Bool
eqpOverfull b :: Actor
b n :: Int
n = let size :: Int
size = [Int] -> Int
forall a. Num a => [a] -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ ((Int, ItemTimer) -> Int) -> [(Int, ItemTimer)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ItemTimer) -> Int
forall a b. (a, b) -> a
fst ([(Int, ItemTimer)] -> [Int]) -> [(Int, ItemTimer)] -> [Int]
forall a b. (a -> b) -> a -> b
$ ItemBag -> [(Int, ItemTimer)]
forall k a. EnumMap k a -> [a]
EM.elems (ItemBag -> [(Int, ItemTimer)]) -> ItemBag -> [(Int, ItemTimer)]
forall a b. (a -> b) -> a -> b
$ Actor -> ItemBag
beqp Actor
b
                  in Bool -> Bool -> Bool
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 10 Bool -> (Actor, Int, Int) -> Bool
forall a. Show a => Bool -> a -> Bool
`blame` (Actor
b, Int
n, Int
size))
                     (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 10

eqpFreeN :: Actor -> Int
eqpFreeN :: Actor -> Int
eqpFreeN b :: Actor
b = let size :: Int
size = [Int] -> Int
forall a. Num a => [a] -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ ((Int, ItemTimer) -> Int) -> [(Int, ItemTimer)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ItemTimer) -> Int
forall a b. (a, b) -> a
fst ([(Int, ItemTimer)] -> [Int]) -> [(Int, ItemTimer)] -> [Int]
forall a b. (a -> b) -> a -> b
$ ItemBag -> [(Int, ItemTimer)]
forall k a. EnumMap k a -> [a]
EM.elems (ItemBag -> [(Int, ItemTimer)]) -> ItemBag -> [(Int, ItemTimer)]
forall a b. (a -> b) -> a -> b
$ Actor -> ItemBag
beqp Actor
b
             in Bool -> Int -> Int
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 10 Bool -> (Actor, Int) -> Bool
forall a. Show a => Bool -> a -> Bool
`blame` (Actor
b, Int
size))
                (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ 10 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
size

-- | Chance that a new monster is generated. Depends on the number
-- of monsters already present, and on the level depth and its cave kind.
monsterGenChance :: Dice.AbsDepth -> Dice.AbsDepth -> Int -> Int -> Rnd Bool
monsterGenChance :: AbsDepth -> AbsDepth -> Int -> Int -> Rnd Bool
monsterGenChance (Dice.AbsDepth ldepth :: Int
ldepth) (Dice.AbsDepth totalDepth :: Int
totalDepth)
                 lvlSpawned :: Int
lvlSpawned actorCoeff :: Int
actorCoeff =
  Bool -> Rnd Bool -> Rnd Bool
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
totalDepth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 Bool -> Bool -> Bool
&& Int
ldepth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0) (Rnd Bool -> Rnd Bool) -> Rnd Bool -> Rnd Bool
forall a b. (a -> b) -> a -> b
$
    -- The sustained spawn speed is now trebled compared to the comment below,
    -- to compensate for some monsters generated asleep:
    --
    -- Heroes have to endure a level depth-sized wave of immediate
    -- spawners for each level and only then the monsters start
    -- to trickle more and more slowly, at the speed dictated
    -- by @actorCoeff@ specified in cave kind.
    -- On level 1/10, first 4 monsters spawn immediately, at level 5/10,
    -- 8 spawn immediately. In general at level n, n+3 spawn at once.
    let scaledDepth :: Int
scaledDepth = Int
ldepth Int -> Int -> Int
forall a. Num a => a -> a -> a
* 10 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
totalDepth
        -- Never spawn too rarely so that camping is never safe.
        maxCoeff :: Int
maxCoeff = 100 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 30  -- safe level after 30 spawns flattens out
        coeff :: Int
coeff = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
maxCoeff (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
actorCoeff Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
lvlSpawned Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
scaledDepth Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2)
    in Chance -> Rnd Bool
chance (Chance -> Rnd Bool) -> Chance -> Rnd Bool
forall a b. (a -> b) -> a -> b
$ 3Integer -> Integer -> Chance
forall a. Integral a => a -> a -> Ratio a
%Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
coeff Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` 1)  -- 3 --- trebled

-- | How long until an actor's smell vanishes from a tile.
smellTimeout :: Delta Time
smellTimeout :: Delta Time
smellTimeout = Delta Time -> Int -> Delta Time
timeDeltaScale (Time -> Delta Time
forall a. a -> Delta a
Delta Time
timeTurn) 200