Rename Perception datatype/fields

This commit is contained in:
2022-06-18 10:08:06 +01:00
parent 66fda3fb93
commit a1c7abedaf
13 changed files with 76 additions and 85 deletions
+1 -1
View File
@@ -260,7 +260,7 @@ canSee i j w = hasLOS p1 p2 w
-- jpos = _crPos (_creatures w IM.! j) -- jpos = _crPos (_creatures w IM.! j)
canSeeIndirect :: Int -> Int -> World -> Bool canSeeIndirect :: Int -> Int -> World -> Bool
canSeeIndirect i j w = any (isJust . uncurry (intersectSegSeg ipos jpos) . _wlLine) canSeeIndirect i j w = not $ any (isJust . uncurry (intersectSegSeg ipos jpos) . _wlLine)
$ IM.filter wlIsOpaque $ wallsAlongLine ipos jpos w $ IM.filter wlIsOpaque $ wallsAlongLine ipos jpos w
where where
ipos = _crPos (_creatures w IM.! i) ipos = _crPos (_creatures w IM.! i)
+1 -1
View File
@@ -15,7 +15,7 @@ targetYouLOS cr w
targetYouCognizant :: Creature -> World -> Maybe Creature targetYouCognizant :: Creature -> World -> Maybe Creature
targetYouCognizant cr w targetYouCognizant cr w
| hasLOS (_crPos cr) (_crPos $ you w) w | hasLOS (_crPos cr) (_crPos $ you w) w
&& isCog (cr ^? crPerception . crAwarenessLevel . ix 0) && isCog (cr ^? crPerception . cpAwareness . ix 0)
= Just $ you w = Just $ you w
| otherwise = Nothing | otherwise = Nothing
where where
+2 -4
View File
@@ -9,10 +9,8 @@ import Control.Lens
{- | Creature attempts to moves under its own steam. {- | Creature attempts to moves under its own steam.
The idea is that this may or may not work, depending on the status of the creature. The idea is that this may or may not work, depending on the status of the creature.
For now, though, this cannot fail. -} For now, though, this cannot fail. -}
crMvBy crMvBy :: Point2 -- ^ Movement translation vector, will be made relative to creature direction
:: Point2 -- ^ Movement translation vector, will be made relative to creature direction -> Creature -> Creature
-> Creature
-> Creature
crMvBy p cr = crMvAbsolute (rotateV (_crDir cr) p) cr crMvBy p cr = crMvAbsolute (rotateV (_crDir cr) p) cr
crMvAbsolute crMvAbsolute
+2 -2
View File
@@ -6,8 +6,8 @@ import Geometry.Data
import Control.Lens import Control.Lens
newtype MemoryState = MemoryState newtype Memory = Memory
{ _soundsToInvestigate :: [Point2] { _soundsToInvestigate :: [Point2]
} }
makeLenses ''MemoryState makeLenses ''Memory
+18 -18
View File
@@ -34,15 +34,15 @@ chaseCritPerceptionUpdate is w =
{- | Update a creatures awareness based upon the creatures' current direction {- | Update a creatures awareness based upon the creatures' current direction
of attention -} -- TODO delete? of attention -} -- TODO delete?
basicAwarenessUpdate :: Creature -> Creature basicAwarenessUpdate :: Creature -> Creature
basicAwarenessUpdate cr = case _crAttentionDir $ _crPerception cr of basicAwarenessUpdate cr = case _cpAttention $ _crPerception cr of
Fixated i -> cr & crPerception . crAwarenessLevel Fixated i -> cr & crPerception . cpAwareness
%~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness) %~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness)
AttentiveTo is -> cr AttentiveTo is -> cr
& crPerception . crAwarenessLevel & crPerception . cpAwareness
%~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) %~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
& maybeBark & maybeBark
where where
oldAwareness = _crAwarenessLevel $ _crPerception cr oldAwareness = _cpAwareness $ _crPerception cr
newAwareness = (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) oldAwareness newAwareness = (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) oldAwareness
becomesCognizant = any isCognizant $ IM.unionWith cogRaised oldAwareness newAwareness becomesCognizant = any isCognizant $ IM.unionWith cogRaised oldAwareness newAwareness
thejitter = do thejitter = do
@@ -65,15 +65,15 @@ basicAwarenessUpdate cr = case _crAttentionDir $ _crPerception cr of
-- TODO fold in randgen update, requires that this is a world to world function -- TODO fold in randgen update, requires that this is a world to world function
chaseCritAwarenessUpdate :: World -> Creature -> Creature chaseCritAwarenessUpdate :: World -> Creature -> Creature
chaseCritAwarenessUpdate w cr = case _crAttentionDir $ _crPerception cr of chaseCritAwarenessUpdate w cr = case _cpAttention $ _crPerception cr of
Fixated i -> cr & crPerception . crAwarenessLevel Fixated i -> cr & crPerception . cpAwareness
%~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness) %~ (IM.insert i (Cognizant 10000) . IM.mapMaybe decreaseAwareness)
AttentiveTo is -> cr AttentiveTo is -> cr
& crPerception . crAwarenessLevel & crPerception . cpAwareness
%~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) %~ (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is)
& maybeBark & maybeBark
where where
oldAwareness = _crAwarenessLevel $ _crPerception cr oldAwareness = _cpAwareness $ _crPerception cr
newAwareness = (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) oldAwareness newAwareness = (IM.mapMaybe decreaseAwareness . IM.unionWith combineAwareness is) oldAwareness
becomesCognizant = any isCognizant $ IM.unionWith cogRaised oldAwareness newAwareness becomesCognizant = any isCognizant $ IM.unionWith cogRaised oldAwareness newAwareness
--randBool = takeOne [False,True] & evalState $ _randGen w --randBool = takeOne [False,True] & evalState $ _randGen w
@@ -92,15 +92,15 @@ chaseCritAwarenessUpdate w cr = case _crAttentionDir $ _crPerception cr of
] ]
| otherwise = id | otherwise = id
cogRaised :: AwarenessLevel -> AwarenessLevel -> AwarenessLevel cogRaised :: Awareness -> Awareness -> Awareness
cogRaised Suspicious{} Cognizant{} = Cognizant 100 cogRaised Suspicious{} Cognizant{} = Cognizant 100
cogRaised _ _ = Suspicious 0 cogRaised _ _ = Suspicious 0
isCognizant :: AwarenessLevel -> Bool isCognizant :: Awareness -> Bool
isCognizant Cognizant {} = True isCognizant Cognizant {} = True
isCognizant _ = False isCognizant _ = False
combineAwareness :: AwarenessLevel -> AwarenessLevel -> AwarenessLevel combineAwareness :: Awareness -> Awareness -> Awareness
combineAwareness (Suspicious x) (Suspicious y) combineAwareness (Suspicious x) (Suspicious y)
| x + y < 5000 = Suspicious $ x + y | x + y < 5000 = Suspicious $ x + y
| otherwise = Cognizant 1000 | otherwise = Cognizant 1000
@@ -110,7 +110,7 @@ combineAwareness (Cognizant x) (Cognizant y) = Cognizant $ min 10000 $ x + y
{- | Decrease awareness level. Returns 'Maybe' value for use with 'IM.mapMaybe' {- | Decrease awareness level. Returns 'Maybe' value for use with 'IM.mapMaybe'
-} -}
decreaseAwareness :: AwarenessLevel -> Maybe AwarenessLevel decreaseAwareness :: Awareness -> Maybe Awareness
decreaseAwareness (Suspicious 0) = Nothing decreaseAwareness (Suspicious 0) = Nothing
decreaseAwareness (Suspicious x) = Just $ Suspicious (x - 50) decreaseAwareness (Suspicious x) = Just $ Suspicious (x - 50)
decreaseAwareness (Cognizant 0) = Just $ Suspicious 1000 decreaseAwareness (Cognizant 0) = Just $ Suspicious 1000
@@ -123,26 +123,26 @@ basicAttentionUpdate
-> World -> World
-> Creature -> Creature
-> Creature -> Creature
basicAttentionUpdate cids w cr = cr & crPerception . crAttentionDir .~ basicAttentionUpdate cids w cr = cr & crPerception . cpAttention .~
AttentiveTo (IM.mapMaybe (newExtraAwareness cr w) $ IM.fromList $ zip cids cids) AttentiveTo (IM.mapMaybe (newExtraAwareness cr w) $ IM.fromList $ zip cids cids)
newExtraAwareness newExtraAwareness
:: Creature -- ^ source creature :: Creature -- ^ source creature
-> World -> World
-> Int -- ^ target creature id -> Int -- ^ target creature id
-> Maybe AwarenessLevel -> Maybe Awareness
newExtraAwareness cr w cid newExtraAwareness cr w cid
| not $ canSeeIndirect (_crID cr) cid w = Nothing | not $ canSeeIndirect (_crID cr) cid w = Nothing
| otherwise = Just . Suspicious $ _viFOV vi ang * _viDist vi d * awakeLevelPerception cr | otherwise = Just . Suspicious $ _viFOV vi ang * _viDist vi d * awakeLevelPerception cr
where where
vi = _crVision $ _crPerception cr vi = _cpVision $ _crPerception cr
tpos = _crPos $ _creatures w IM.! cid tpos = _crPos $ _creatures w IM.! cid
cpos = _crPos cr cpos = _crPos cr
ang = angleVV (unitVectorAtAngle (_crDir cr)) (tpos - cpos) ang = angleVV (unitVectorAtAngle (_crDir cr)) (tpos - cpos)
d = dist tpos cpos d = dist tpos cpos
awakeLevelPerception :: Creature -> Float awakeLevelPerception :: Creature -> Float
awakeLevelPerception cr = case _crAwakeLevel $ _crPerception cr of awakeLevelPerception cr = case _cpVigilance $ _crPerception cr of
Comatose -> 0 Comatose -> 0
Asleep -> 10 Asleep -> 10
Lethargic -> 200 Lethargic -> 200
@@ -162,13 +162,13 @@ rememberSounds w cr = cr
& awakeupdate & awakeupdate
where where
awakeupdate | null closesounds = id awakeupdate | null closesounds = id
| otherwise = crPerception . crAwakeLevel .~ Vigilant | otherwise = crPerception . cpVigilance .~ Vigilant
closesounds = map fst (filter (soundIsClose w cr) (newSounds w)) closesounds = map fst (filter (soundIsClose w cr) (newSounds w))
-- TODO work out correct form for sounds passing through walls -- TODO work out correct form for sounds passing through walls
soundIsClose :: World -> Creature -> (Point2,Float) -> Bool soundIsClose :: World -> Creature -> (Point2,Float) -> Bool
soundIsClose w cr (pos,vol) = dist cpos pos < 2000 soundIsClose w cr (pos,vol) = dist cpos pos < 2000
&& vol > (_auDist . _crAudition $ _crPerception cr) (dist pos cpos) && vol > (_auDist . _cpAudition $ _crPerception cr) (dist pos cpos)
&& hasLOS cpos pos w && hasLOS cpos pos w
where where
cpos = _crPos cr cpos = _crPos cr
+21 -21
View File
@@ -1,20 +1,20 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StrictData #-} {-# LANGUAGE StrictData #-}
module Dodge.Creature.Perception.Data module Dodge.Creature.Perception.Data
( PerceptionState (..) ( Perception (..)
, AwakeLevel (..) , Vigilance (..)
, AttentionDir (..) , Attention (..)
, AwarenessLevel (..) , Awareness (..)
, Vision (..) , Vision (..)
, Audition (..) , Audition (..)
-- lenses -- lenses
, getAttentiveTo , getAttentiveTo
, getFixated , getFixated
, crAttentionDir , cpAttention
, crAwakeLevel , cpVigilance
, crAwarenessLevel , cpAwareness
, crVision , cpVision
, crAudition , cpAudition
, viFOV , viFOV
, viDist , viDist
, auDist , auDist
@@ -23,12 +23,12 @@ module Dodge.Creature.Perception.Data
import Control.Lens import Control.Lens
import qualified Data.IntMap as IM import qualified Data.IntMap as IM
data PerceptionState = PerceptionState data Perception = Perception
{ _crAwakeLevel :: AwakeLevel { _cpVigilance :: Vigilance
, _crAttentionDir :: AttentionDir , _cpAttention :: Attention
, _crAwarenessLevel :: IM.IntMap AwarenessLevel , _cpAwareness :: IM.IntMap Awareness
, _crVision :: Vision , _cpVision :: Vision
, _crAudition :: Audition , _cpAudition :: Audition
} }
data Vision = Eyes data Vision = Eyes
@@ -39,7 +39,7 @@ newtype Audition = Ears
{ _auDist :: Float -> Float { _auDist :: Float -> Float
} }
data AwakeLevel data Vigilance
= Comatose = Comatose
| Asleep | Asleep
| Lethargic | Lethargic
@@ -47,16 +47,16 @@ data AwakeLevel
| Overstrung | Overstrung
deriving (Eq,Ord,Show) deriving (Eq,Ord,Show)
data AttentionDir data Attention
= AttentiveTo {_getAttentiveTo :: IM.IntMap AwarenessLevel } = AttentiveTo {_getAttentiveTo :: IM.IntMap Awareness }
| Fixated {_getFixated :: Int } | Fixated {_getFixated :: Int }
data AwarenessLevel data Awareness
= Suspicious Float = Suspicious Float
| Cognizant Float | Cognizant Float
deriving (Eq,Ord) deriving (Eq,Ord)
makeLenses ''AttentionDir makeLenses ''Attention
makeLenses ''PerceptionState makeLenses ''Perception
makeLenses ''Vision makeLenses ''Vision
makeLenses ''Audition makeLenses ''Audition
+7 -13
View File
@@ -90,8 +90,8 @@ creatureDisplayText cfig w cr
. scale theScale theScale . scale theScale theScale
. stackText . stackText
$ clockCycle 25 (V.fromList $ clockCycle 25 (V.fromList
[ \cr' -> [crDisplayAwake cr'] [ \cr' -> [crDisplayVigilance cr']
, \cr' -> [crDisplayAlert cr'] , \cr' -> [crDisplayAwareness cr']
] ]
) w cr ) w cr
where where
@@ -101,13 +101,13 @@ creatureDisplayText cfig w cr
v = cpos -.- campos v = cpos -.- campos
(V2 x y) = campos +.+ v +.+ _crRad cr *.* normalizeV v (V2 x y) = campos +.+ v +.+ _crRad cr *.* normalizeV v
crDisplayAlert :: Creature -> String crDisplayAwareness :: Creature -> String
crDisplayAlert cr crDisplayAwareness cr
| isSuspicious = "?" | isSuspicious = "?"
| isCognizant = "!" | isCognizant = "!"
| otherwise = "." | otherwise = "."
where where
imAwarenesses = _crAwarenessLevel (_crPerception cr) imAwarenesses = _cpAwareness (_crPerception cr)
isSuspicious = any f imAwarenesses && not (null imAwarenesses) isSuspicious = any f imAwarenesses && not (null imAwarenesses)
isCognizant = any g imAwarenesses && not (null imAwarenesses) isCognizant = any g imAwarenesses && not (null imAwarenesses)
f (Suspicious _) = True f (Suspicious _) = True
@@ -115,14 +115,8 @@ crDisplayAlert cr
g (Cognizant _) = True g (Cognizant _) = True
g _ = False g _ = False
crDisplayAwake :: Creature -> String crDisplayVigilance :: Creature -> String
crDisplayAwake = show . _crAwakeLevel . _crPerception crDisplayVigilance = show . _cpVigilance . _crPerception
--crDisplayAwake cr = case _crAwakeLevel (_crPerception cr) of
-- Comatose -> "-"
-- Asleep -> "Z"
-- Lethargic -> "L"
-- Vigilant -> "V"
-- Overstrung -> "O"
--damageMod :: Creature -> Picture -> Picture --damageMod :: Creature -> Picture -> Picture
--damageMod cr pic = piercingMod $ bluntScale pic --damageMod cr pic = piercingMod $ bluntScale pic
+3 -3
View File
@@ -110,7 +110,7 @@ viewTarget w cr = do
| otherwise -> cr' & crActionPlan . crAction %~ replaceNullWith (PathTo p) | otherwise -> cr' & crActionPlan . crAction %~ replaceNullWith (PathTo p)
Nothing -> cr-- & crPerception . crAwakeLevel .~ Lethargic Nothing -> cr-- & crPerception . crAwakeLevel .~ Lethargic
where where
cr' = cr & crPerception . crAwakeLevel .~ Vigilant cr' = cr & crPerception . cpVigilance .~ Vigilant
replaceNullWith :: a -> [a] -> [a] replaceNullWith :: a -> [a] -> [a]
replaceNullWith x [] = [x] replaceNullWith x [] = [x]
@@ -158,7 +158,7 @@ listGuard ( (test,y):ps, z ) x
listGuard (_,z) _ = z listGuard (_,z) _ = z
targetYouWhenCognizant :: World -> Creature -> Creature targetYouWhenCognizant :: World -> Creature -> Creature
targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0 of targetYouWhenCognizant w cr = case cr ^? crPerception . cpAwareness . ix 0 of
-- so this caused a space leak: be careful with ?~ -- so this caused a space leak: be careful with ?~
-- consider changing targeted creature to be just an index -- consider changing targeted creature to be just an index
Just (Cognizant _) -> _creatures w IM.! 0 `seq` cr & crIntention . targetCr ?~ _creatures w IM.! 0 Just (Cognizant _) -> _creatures w IM.! 0 `seq` cr & crIntention . targetCr ?~ _creatures w IM.! 0
@@ -167,7 +167,7 @@ targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0
turnIfDamaged :: Creature -> Creature turnIfDamaged :: Creature -> Creature
turnIfDamaged cr turnIfDamaged cr
| _crPastDamage cr > 0 = case _crStrategy (_crActionPlan cr) of | _crPastDamage cr > 0 = case _crStrategy (_crActionPlan cr) of
WatchAndWait -> cr & crPerception . crAwakeLevel .~ Vigilant WatchAndWait -> cr & crPerception . cpVigilance .~ Vigilant
& crActionPlan . crStrategy .~ StrategyActions LookAround [TurnToA (_crPos cr -.- unitVectorAtAngle (_crDir cr))] & crActionPlan . crStrategy .~ StrategyActions LookAround [TurnToA (_crPos cr -.- unitVectorAtAngle (_crDir cr))]
_ -> cr _ -> cr
| otherwise = cr | otherwise = cr
+1 -1
View File
@@ -9,7 +9,7 @@ targetYouWhenCognizant
:: World :: World
-> Creature -> Creature
-> Creature -> Creature
targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0 of targetYouWhenCognizant w cr = case cr ^? crPerception . cpAwareness . ix 0 of
Just (Cognizant _) -> cr & crIntention . targetCr ?~ _creatures w IM.! 0 Just (Cognizant _) -> cr & crIntention . targetCr ?~ _creatures w IM.! 0
_ -> cr & crIntention . targetCr .~ Nothing _ -> cr & crIntention . targetCr .~ Nothing
+1
View File
@@ -1,5 +1,6 @@
module Dodge.Creature.Statistics module Dodge.Creature.Statistics
( getCrStrength ( getCrStrength
, getCrDexterity
) where ) where
import Dodge.Data import Dodge.Data
import qualified IntMapHelp as IM import qualified IntMapHelp as IM
+2 -2
View File
@@ -356,8 +356,8 @@ data Creature = Creature
, _crStance :: Stance , _crStance :: Stance
, _crActionPlan :: ActionPlan , _crActionPlan :: ActionPlan
, _crMeleeCooldown :: Int , _crMeleeCooldown :: Int
, _crPerception :: PerceptionState , _crPerception :: Perception
, _crMemory :: MemoryState , _crMemory :: Memory
, _crVocalization :: Vocalization , _crVocalization :: Vocalization
, _crFaction :: Faction , _crFaction :: Faction
, _crGroup :: CrGroup , _crGroup :: CrGroup
+9 -11
View File
@@ -81,21 +81,19 @@ defaultCreatureSkin = CreatureSkin (greyN 0.9) (light4 green) (greyN 0.3)
defaultInanimate :: Creature defaultInanimate :: Creature
defaultInanimate = defaultCreature & crActionPlan .~ Inanimate defaultInanimate = defaultCreature & crActionPlan .~ Inanimate
defaultCreatureMemory :: MemoryState defaultCreatureMemory :: Memory
defaultCreatureMemory = MemoryState defaultCreatureMemory = Memory { _soundsToInvestigate = [] }
{ _soundsToInvestigate = []
}
defaultInvSize :: Int defaultInvSize :: Int
defaultInvSize = 20 defaultInvSize = 20
defaultPerceptionState :: PerceptionState defaultPerceptionState :: Perception
defaultPerceptionState = PerceptionState defaultPerceptionState = Perception
{ _crAwakeLevel = Lethargic { _cpVigilance = Lethargic
, _crAttentionDir = AttentiveTo IM.empty , _cpAttention = AttentiveTo IM.empty
, _crAwarenessLevel = IM.empty , _cpAwareness = IM.empty
, _crVision = defaultVision , _cpVision = defaultVision
, _crAudition = defaultAudition , _cpAudition = defaultAudition
} }
defaultVision :: Vision defaultVision :: Vision
defaultVision = Eyes defaultVision = Eyes
+2 -2
View File
@@ -157,13 +157,13 @@ drawPathing cfig w
crDisplayInfo :: Configuration -> World -> Creature -> Maybe (Point2,[String]) crDisplayInfo :: Configuration -> World -> Creature -> Maybe (Point2,[String])
crDisplayInfo cfig w cr crDisplayInfo cfig w cr
| _crID cr == 0 = Nothing -- | _crID cr == 0 = Nothing
| crOnScreen = Just (_crPos cr, catMaybes | crOnScreen = Just (_crPos cr, catMaybes
-- [fmap show $ ap ^? crGoal -- [fmap show $ ap ^? crGoal
[ fmap show $ cr ^? crHP [ fmap show $ cr ^? crHP
, fmap show $ ap ^? crStrategy , fmap show $ ap ^? crStrategy
, fmap show $ cr ^? crPos , fmap show $ cr ^? crPos
,fmap show $ cr ^? crPerception . crAwakeLevel , fmap show $ cr ^? crPerception . cpVigilance
-- , fmap show $ cr ^? crOldPos -- , fmap show $ cr ^? crOldPos
, fmap show $ ap ^? crAction , fmap show $ ap ^? crAction
, fmap show $ ap ^? crImpulse , fmap show $ ap ^? crImpulse