Implement basic attention and awareness

This commit is contained in:
2021-05-08 17:34:25 +02:00
parent 1aa59cc205
commit 86faf9fd01
12 changed files with 243 additions and 135 deletions
+57
View File
@@ -0,0 +1,57 @@
{- | Deals with changes in a creature's awareness of other creatures. -}
module Dodge.Creature.AlertLevel
where
import Dodge.Data
import Dodge.Creature.AlertLevel.Data
import Dodge.Base.Collide
import Control.Lens
import qualified Data.IntMap.Strict as IM
{- | Ties together (currently) an awareness and attention update -}
basicPerceptionUpdate
:: [Int] -- ^ List of creature ids that may direct attention and awareness
-> World
-> Creature
-> Creature
basicPerceptionUpdate is w cr
= basicAwarenessUpdate w $
basicAttentionUpdate is w cr
{- | Update a creatures awareness based upon the creatures current direction
of attention -}
basicAwarenessUpdate
:: World -> Creature -> Creature
basicAwarenessUpdate w cr = case _crAttentionDir cr of
AttentiveTo is -> cr & crAwarenessLevel %~ updateAwareness is
Fixated i -> cr & crAwarenessLevel
%~ ( IM.insert i (Cognizant 100) . IM.mapMaybe decreaseAwareness)
{- | Increase awareness of a given list of ids -}
updateAwareness :: [Int] -> IM.IntMap AwarenessLevel -> IM.IntMap AwarenessLevel
updateAwareness is imawareness
= foldr (\k -> IM.insertWith (\_ x -> increaseAwareness x) k (AwarenessInt 1))
(IM.mapMaybe decreaseAwareness imawareness) is
-- (IM.mapMaybe decreaseAwareness imawareness) is
{- | Increase awareness level -}
increaseAwareness :: AwarenessLevel -> AwarenessLevel
increaseAwareness (AwarenessInt x)
| x < 100 = AwarenessInt $ x + 2
| otherwise = Cognizant 1
increaseAwareness (Cognizant x)
| x < 100 = Cognizant (x + 2)
| otherwise = Cognizant 100
{- | Decrease awareness level. Returns 'Maybe' value for use with 'IM.mapMaybe'
-}
decreaseAwareness :: AwarenessLevel -> Maybe AwarenessLevel
decreaseAwareness (AwarenessInt 0) = Nothing
decreaseAwareness (AwarenessInt x) = Just $ AwarenessInt (x - 1)
decreaseAwareness (Cognizant 0) = Just $ AwarenessInt 100
decreaseAwareness (Cognizant x) = Just $ Cognizant $ x - 1
{- | Given a fixed group of creatures, direct attention to those of them that
- are in view. -}
basicAttentionUpdate
:: [Int] -- ^ Creatures that may attract this creature's attention
-> World
-> Creature
-> Creature
basicAttentionUpdate cids w cr = cr & crAttentionDir .~
AttentiveTo (filter (\cid -> canSee (_crID cr) cid w) cids)