Fix space leak when assigning targeted creature

This commit is contained in:
2021-12-10 02:56:14 +00:00
parent e5db9ba5d0
commit 15e1fbc060
7 changed files with 33 additions and 18 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ spawnerCrit = defaultCreature
miniGunCrit :: Creature miniGunCrit :: Creature
miniGunCrit = defaultCreature miniGunCrit = defaultCreature
{ _crPict = basicCrPict red { _crPict = basicCrPict red
, _crUpdate = stateUpdate' $ impulsiveAI' $ , _crUpdate = stateUpdate' $ impulsiveAI $
sentinelFireType (const shootTillEmpty) sentinelFireType (const shootTillEmpty)
, _crActionPlan = ActionPlan , _crActionPlan = ActionPlan
{ _crImpulse = [] { _crImpulse = []
+13 -9
View File
@@ -1,7 +1,7 @@
module Dodge.Creature.Impulse module Dodge.Creature.Impulse
( impulsiveAIR ( impulsiveAIR
-- , impulsiveAI
, impulsiveAI , impulsiveAI
, impulsiveAI'
) where ) where
import Dodge.Data import Dodge.Data
import Dodge.Creature.Vocalization import Dodge.Creature.Vocalization
@@ -15,6 +15,7 @@ import System.Random
import Control.Lens import Control.Lens
import Control.Monad.Reader import Control.Monad.Reader
import Data.Bifunctor import Data.Bifunctor
--import Data.Maybe
impulsiveAIR impulsiveAIR
:: (Creature -> Reader World Creature) :: (Creature -> Reader World Creature)
@@ -23,17 +24,17 @@ impulsiveAIR
-> (World -> World , Maybe Creature) -> (World -> World , Maybe Creature)
impulsiveAIR impf cr w = second Just $ followImpulses w . ($ w) . runReader $ impf cr impulsiveAIR impf cr w = second Just $ followImpulses w . ($ w) . runReader $ impf cr
--impulsiveAI :: (World -> Creature -> Creature)
-- -> Creature
-- -> World
-- -> (World -> World , Maybe Creature)
--impulsiveAI impf cr w = second Just $ followImpulses w $ impf w cr
impulsiveAI :: (World -> Creature -> Creature) impulsiveAI :: (World -> Creature -> Creature)
-> Creature
-> World
-> (World -> World , Maybe Creature)
impulsiveAI impf cr w = second Just $ followImpulses w $ impf w cr
impulsiveAI' :: (World -> Creature -> Creature)
-> Creature -> Creature
-> World -> World
-> (World -> World , Creature) -> (World -> World , Creature)
impulsiveAI' impf cr w = followImpulses w $ impf w cr impulsiveAI impf cr w = followImpulses w $ impf w cr
followImpulses :: World -> Creature -> (World -> World, Creature) followImpulses :: World -> Creature -> (World -> World, Creature)
followImpulses w cr = foldr followImpulses w cr = foldr
@@ -70,6 +71,10 @@ followImpulse cr w imp = case imp of
ImpulseUseTarget f -> case cr ^? crIntention . targetCr . _Just of ImpulseUseTarget f -> case cr ^? crIntention . targetCr . _Just of
Just tcr -> followImpulse cr w (f tcr) Just tcr -> followImpulse cr w (f tcr)
_ -> (id,cr) _ -> (id,cr)
-- ImpulseUseTarget f -> fromMaybe (id,cr) $ do
-- cid <- cr ^? crIntention . targetCr . _Just
-- tcr <- w ^? creatures . ix cid
-- return $ followImpulse cr w (f tcr)
ImpulseUseAheadPos f -> followImpulse cr w (f (_crPos cr +.+ 20 *.* unitVectorAtAngle (_crDir cr))) ImpulseUseAheadPos f -> followImpulse cr w (f (_crPos cr +.+ 20 *.* unitVectorAtAngle (_crDir cr)))
MvForward -> (id, crMvForward speed cr) MvForward -> (id, crMvForward speed cr)
MvTurnToward p -> (id, creatureTurnToward p (turnRad $ safeAngleVV (p -.- cpos) (unitVectorAtAngle cdir)) cr) MvTurnToward p -> (id, creatureTurnToward p (turnRad $ safeAngleVV (p -.- cpos) (unitVectorAtAngle cdir)) cr)
@@ -86,4 +91,3 @@ followImpulse cr w imp = case imp of
hitCr i = over (creatures . ix i . crState . crDamage) (addDam i) hitCr i = over (creatures . ix i . crState . crDamage) (addDam i)
. soundStart (CrSound cid) cpos hitS Nothing . soundStart (CrSound cid) cpos hitS Nothing
addDam i dams = Blunt 100 cpos (posFromID i) (posFromID i) : dams addDam i dams = Blunt 100 cpos (posFromID i) (posFromID i) : dams
+5 -4
View File
@@ -8,7 +8,7 @@ import Dodge.Data
import qualified Data.IntMap.Strict as IM import qualified Data.IntMap.Strict as IM
import Control.Lens import Control.Lens
--import Data.Maybe import Data.Maybe
useItem :: Int -> World -> World useItem :: Int -> World -> World
useItem cid w = itemEffect cr it w useItem cid w = itemEffect cr it w
@@ -17,9 +17,10 @@ useItem cid w = itemEffect cr it w
it = _crInv cr IM.! _crInvSel cr it = _crInv cr IM.! _crInvSel cr
tryUseItem :: Creature -> World -> World tryUseItem :: Creature -> World -> World
tryUseItem cr' w = case w ^? creatures . ix (_crID cr') of tryUseItem cr' w = fromMaybe w $ do
Just cr -> itemEffect cr (_crInv cr IM.! _crInvSel cr) w cr <- w ^? creatures . ix (_crID cr')
Nothing -> w it <- cr ^? crInv . ix (_crInvSel cr)
return $ itemEffect cr it w
itemEffect :: Creature -> Item -> World -> World itemEffect :: Creature -> Item -> World -> World
itemEffect cr it w = case it ^? itUse of itemEffect cr it w = case it ^? itUse of
+7
View File
@@ -1,6 +1,7 @@
--{-# LANGUAGE TupleSections #-} --{-# LANGUAGE TupleSections #-}
module Dodge.Creature.Perception module Dodge.Creature.Perception
( perceptionUpdate ( perceptionUpdate
, perceptionUp
, perceptionUpdate' , perceptionUpdate'
, newSounds , newSounds
) )
@@ -36,6 +37,12 @@ perceptionUpdate'
-> Creature -> Creature
-> Creature -> Creature
perceptionUpdate' is w cr = rememberSounds' w $ basicAwarenessUpdate' $ basicAttentionUpdate' is w cr perceptionUpdate' is w cr = rememberSounds' w $ basicAwarenessUpdate' $ basicAttentionUpdate' is w cr
--perceptionUpdate' is w cr = basicAwarenessUpdate' $ basicAttentionUpdate' is w cr
perceptionUp :: Int -> World -> Creature -> Creature
perceptionUp i w cr | canSee i (_crID cr) w = cr & crPerception . crAwarenessLevel . at i ?~ Cognizant 100
| otherwise = cr & crPerception . crAwarenessLevel . at i .~ Nothing
{- | Update a creatures awareness based upon the creatures' current direction {- | Update a creatures awareness based upon the creatures' current direction
of attention -} of attention -}
+2 -1
View File
@@ -201,5 +201,6 @@ targetYouWhenCognizantR cr = reader $ \w -> case cr ^? crPerception . crAwarenes
targetYouWhenCognizant :: World -> Creature -> Creature targetYouWhenCognizant :: World -> Creature -> Creature
targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0 of targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0 of
Just (Cognizant _) -> cr & crIntention . targetCr ?~ _creatures w IM.! 0 -- so this caused a space leak: be careful with ?~
Just (Cognizant _) -> _creatures w IM.! 0 `seq` cr & crIntention . targetCr ?~ _creatures w IM.! 0
_ -> cr & crIntention . targetCr .~ Nothing _ -> cr & crIntention . targetCr .~ Nothing
+2 -1
View File
@@ -63,7 +63,8 @@ sentinelFireType f = chainCreatureUpdatesLR
) )
, (crAwayFromPost, goToPostStrat) , (crAwayFromPost, goToPostStrat)
] ]
, Left $ perceptionUpdate' [0] -- , Left $ perceptionUpdate' [0]
, Left $ perceptionUp 0
, Right doStrategyActions , Right doStrategyActions
, Right reloadOverride , Right reloadOverride
, Left targetYouWhenCognizant , Left targetYouWhenCognizant
+3 -2
View File
@@ -46,7 +46,8 @@ stateUpdate :: CRUpdate -> Creature -> World -> World
stateUpdate f cr w = let (fw,mcr) = stateUpdateDamage doDamage f cr w stateUpdate f cr w = let (fw,mcr) = stateUpdateDamage doDamage f cr w
in fw $ w & creatures . at (_crID cr) .~ mcr in fw $ w & creatures . at (_crID cr) .~ mcr
stateUpdate' :: CRUpdate' -> Creature -> World -> World stateUpdate' :: (Creature -> World -> (World -> World, Creature))
-> Creature -> World -> World
stateUpdate' f cr w = let (fw,mcr) = stateUpdateDamage' doDamage f cr w stateUpdate' f cr w = let (fw,mcr) = stateUpdateDamage' doDamage f cr w
in fw $ w & creatures . at (_crID cr) .~ mcr in fw $ w & creatures . at (_crID cr) .~ mcr
@@ -73,7 +74,7 @@ stateUpdateDamage' :: (Creature -> Creature) -> CRUpdate' -> CRUpdate
stateUpdateDamage' damageupdate u cr w = case u (updateMovement cr) w of stateUpdateDamage' damageupdate u cr w = case u (updateMovement cr) w of
(f, upcr) -> (f, upcr) ->
( invSideEff upcr . movementSideEff cr . deathEff . f ( invSideEff upcr . movementSideEff cr . deathEff . f
, (stepReloading . damageupdate) <$> crOrCorpse upcr , stepReloading . damageupdate <$> crOrCorpse upcr
) )
where where
crOrCorpse cr' crOrCorpse cr'