Unify some perception updates

This commit is contained in:
2026-04-03 12:13:29 +01:00
parent b7b42f50a5
commit 4e415a41ed
4 changed files with 81 additions and 65 deletions
+62 -47
View File
@@ -1,10 +1,10 @@
--{-# LANGUAGE TupleSections #-}
module Dodge.Creature.Perception (
perceptionUpdate,
chaseCritPerceptionUpdate,
visionCheck,
) where
import Dodge.SoundLogic.ExternallyGeneratedSounds
import Dodge.Creature.Vocalization
import Control.Lens
import Control.Monad
@@ -27,17 +27,17 @@ perceptionUpdate ::
World ->
Creature ->
Creature
perceptionUpdate is w = rememberSounds w . basicAwarenessUpdate . basicAttentionUpdate is w
perceptionUpdate is w = rememberSounds w . basicAwarenessUpdate w . basicAttentionUpdate is w
chaseCritPerceptionUpdate :: [Int] -> World -> Creature -> Creature
chaseCritPerceptionUpdate is w =
rememberSounds w . chaseCritAwarenessUpdate w . basicAttentionUpdate is w
--chaseCritPerceptionUpdate :: [Int] -> World -> Creature -> Creature
--chaseCritPerceptionUpdate is w =
-- rememberSounds w . chaseCritAwarenessUpdate w . basicAttentionUpdate is w
{- | Update a creatures awareness based upon the creatures' current direction
of attention
-} -- TODO delete?
basicAwarenessUpdate :: Creature -> Creature
basicAwarenessUpdate cr = case _cpAttention $ _crPerception cr of
basicAwarenessUpdate :: World -> Creature -> Creature
basicAwarenessUpdate w cr = case _cpAttention $ _crPerception cr of
Fixated i ->
cr & crPerception . cpAwareness
%~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness)
@@ -45,7 +45,7 @@ basicAwarenessUpdate cr = case _cpAttention $ _crPerception cr of
cr
& crPerception . cpAwareness
%~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
& maybeBark
& maybeBecomeCognizant
where
oldAwareness = _cpAwareness $ _crPerception cr
newAwareness =
@@ -53,50 +53,65 @@ basicAwarenessUpdate cr = case _cpAttention $ _crPerception cr of
oldAwareness
becomesCognizant =
any isCognizant $ IM.unionWith cogRaised oldAwareness newAwareness
thejitter = [RandomImpulse $ RandImpulseCircMove 1]
maybeBark = fromMaybe id $ do
maybeBecomeCognizant = fromMaybe id $ do
guard becomesCognizant
sid <- crVocalizationSound cr
return $
crActionPlan . apAction
.~ [ImpulsesList ([Bark sid] : replicate 5 thejitter)]
.~ [ImpulsesList (crImpulsesOnCognizant w cr)
, AimAt 0 (w ^?! cWorld . lWorld . creatures . ix 0 . crPos . _xy)
]
crImpulsesOnCognizant :: World -> Creature -> [[Impulse]]
crImpulsesOnCognizant w cr = case cr ^. crType of
ChaseCrit {} | Just sid <- cognizantVoc w cr -> [Bark sid]: replicate 5 [RandomImpulse $ RandImpulseCircMove 3]
<> [[ChangeStrategy $ CloseToMelee 0]]
HoverCrit {} | Just sid <- cognizantVoc w cr -> [[Bark sid]]
<> [[ChangeStrategy $ CloseToMelee 0]]
_ | Just sid <- cognizantVoc w cr -> [Bark sid]: replicate 5 [RandomImpulse $ RandImpulseCircMove 1]
_ -> replicate 5 [RandomImpulse $ RandImpulseCircMove 3]
cognizantVoc :: World -> Creature -> Maybe SoundID
cognizantVoc w cr = case cr ^. crType of
ChaseCrit {} -> Just (evalState (takeOne (crWarningSounds cr)) (_randGen w))
HoverCrit {} -> Just beep3QuickS
_ -> Nothing
-- TODO fold in randgen update, requires that this is a world to world function
chaseCritAwarenessUpdate :: World -> Creature -> Creature
chaseCritAwarenessUpdate w cr = case _cpAttention $ _crPerception cr of
Fixated i ->
cr & crPerception . cpAwareness
%~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness)
AttentiveTo is ->
cr
& crPerception . cpAwareness
%~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
& maybeBark
where
oldAwareness = _cpAwareness $ _crPerception cr
newAwareness =
(IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
oldAwareness
becomesCognizant =
any isCognizant $
IM.unionWith cogRaised oldAwareness newAwareness
thejitter = [RandomImpulse $ RandImpulseCircMove 3]
maybeBark = fromMaybe id $ do
guard becomesCognizant
guard $ cr ^. crVocalization == VocReady
let soundid = evalState (takeOne (crWarningSounds cr)) (_randGen w)
numjits = fst $ randomR (15, 25) (_randGen w)
return $
(crActionPlan . apStrategy .~ WarningCry)
. ( crActionPlan . apAction
.~ [ ImpulsesList
( [Bark soundid] :
replicate numjits thejitter
++ [[ChangeStrategy $ CloseToMelee 0]]
)
, AimAt 0 (w ^?! cWorld . lWorld . creatures . ix 0 . crPos . _xy)
]
)
--chaseCritAwarenessUpdate :: World -> Creature -> Creature
--chaseCritAwarenessUpdate w cr = case _cpAttention $ _crPerception cr of
-- Fixated i ->
-- cr & crPerception . cpAwareness
-- %~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness)
-- AttentiveTo is ->
-- cr
-- & crPerception . cpAwareness
-- %~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
-- & maybeBark
-- where
-- oldAwareness = _cpAwareness $ _crPerception cr
-- newAwareness =
-- (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
-- oldAwareness
-- becomesCognizant =
-- any isCognizant $
-- IM.unionWith cogRaised oldAwareness newAwareness
-- thejitter = [RandomImpulse $ RandImpulseCircMove 3]
-- maybeBark = fromMaybe id $ do
-- guard becomesCognizant
-- guard $ cr ^. crVocalization == VocReady
-- let soundid = evalState (takeOne (crWarningSounds cr)) (_randGen w)
-- numjits = fst $ randomR (15, 25) (_randGen w)
-- return $
-- (crActionPlan . apStrategy .~ WarningCry)
-- . ( crActionPlan . apAction
-- .~ [ ImpulsesList
-- ( [Bark soundid] :
-- replicate numjits thejitter
-- ++ [[ChangeStrategy $ CloseToMelee 0]]
-- )
-- , AimAt 0 (w ^?! cWorld . lWorld . creatures . ix 0 . crPos . _xy)
-- ]
-- )
cogRaised :: Awareness -> Awareness -> Awareness
cogRaised Suspicious{} Cognizant{} = Cognizant 100
+1 -1
View File
@@ -18,7 +18,7 @@ chaseCritInternal w cr =
, setViewPos
, setMvPos
, chaseCritMv
, chaseCritPerceptionUpdate [0]
, perceptionUpdate [0]
, targetYouWhenCognizant
, const searchIfDamaged
, const (crType . meleeCooldown %~ max 0 . subtract 1)
+2 -2
View File
@@ -339,8 +339,8 @@ chasmSpitTerminal = do
tToBTree "chasmTerm" $
Node
(addDoorToggleTerminal' i1 (PS 150 0) y')
--[ treePost [triggerDoorRoom i1, deadEndPSType (PutCrit chaseCrit)]
[ treePost [triggerDoorRoom i1, deadEndPSType (PutCrit hoverCrit)]
[ treePost [triggerDoorRoom i1, deadEndPSType (PutCrit chaseCrit)]
--[ treePost [triggerDoorRoom i1, deadEndPSType (PutCrit hoverCrit)]
--[ treePost [triggerDoorRoom i1, deadEndRoom]
, treePost [triggerDoorRoom i1, deadEndPSType (PutCrit hoverCrit)]
, return $ cleatOnward $ triggerDoorRoom i1