Remove Reader monad from multiple creature ais

This commit is contained in:
2021-12-11 19:32:20 +00:00
parent 37b81c2967
commit c847955b7c
7 changed files with 78 additions and 131 deletions
+3 -85
View File
@@ -1,25 +1,17 @@
{- | Functions updating a creature in a Reader World environment -}
module Dodge.Creature.ReaderUpdate
( doStrategyActionsR
, doStrategyActions
( doStrategyActions
, setTargetMv
, targetYouWhenCognizantR
, targetYouWhenCognizant
, overrideMeleeCloseTargetR
, overrideMeleeCloseTarget
, watchUpdateStratR
, watchUpdateStrat
, reloadOverrideR
, reloadOverride
, overrideInternalRRR
, overrideInternal
, goToTarget
, flockACCR
, flockACC
, setMvPos
, setViewPos
)
where
) where
import Dodge.Data
import Dodge.Creature.Test
import Dodge.Creature.Volition
@@ -29,16 +21,10 @@ import Geometry
import FoldableHelp
import qualified Data.IntMap.Strict as IM
import Control.Monad.Reader
import Control.Lens
import Control.Applicative
import Data.Maybe
overrideMeleeCloseTargetR
:: Creature
-> Reader World Creature
overrideMeleeCloseTargetR cr = return $ maybe cr (tryMeleeAttack cr) (_targetCr $ _crIntention cr)
overrideMeleeCloseTarget :: Creature -> Creature
overrideMeleeCloseTarget cr = maybe cr (tryMeleeAttack cr) (_targetCr $ _crIntention cr)
@@ -73,29 +59,6 @@ setTargetMv targFunc w cr = maybe
(\ctarg -> cr & crIntention . mvToPoint ?~ _crPos ctarg)
(targFunc w cr)
flockACCR :: Creature -> Reader World Creature
flockACCR cr = do
case cr ^? crIntention . targetCr . _Just of
Nothing -> pure cr
Just tcr -> do
w <- ask
let tpos = _crPos tcr
cpos = _crPos cr
isFarACC cr' = _crGroup cr' == _crGroup cr
&& _crID cr' /= _crID cr
&& dist (_crPos cr') tpos > dist cpos tpos
nearACCs = IM.filter isFarACC $ creaturesNearPointI 5 cpos w
macr = safeMinimumOn (dist cpos . _crPos) nearACCs
case macr of
Nothing -> pure cr
Just acr -> do
let r = _crRad acr + _crRad cr + 10
horDir = normalizeV (vNormal (cpos -.- tpos))
horShift = if isLHS tpos cpos (_crPos acr)
then r *.* horDir
else negate r *.* horDir
pure $ cr & crIntention . mvToPoint ?~ tpos +.+ horShift
-- fugly
flockACC :: World -> Creature -> Creature
flockACC w cr = case cr ^? crIntention . targetCr . _Just of
@@ -139,12 +102,6 @@ replaceNullWith :: a -> [a] -> [a]
replaceNullWith x [] = [x]
replaceNullWith _ xs = xs
doStrategyActionsR :: Creature -> Reader World Creature
doStrategyActionsR cr = return $ case cr ^? crActionPlan . crStrategy of
Just (StrategyActions strat acs) -> cr & crActionPlan . crAction .~ acs
& crActionPlan . crStrategy .~ strat
_ -> cr
doStrategyActions :: Creature -> Creature
doStrategyActions cr = case cr ^? crActionPlan . crStrategy of
Just (StrategyActions strat acs) -> cr & crActionPlan . crAction .~ acs
@@ -163,45 +120,11 @@ reloadOverride cr
, WaitThen 1 $ DoActionWhileInterrupt NoAction crIsReloading (DoImpulses [ChangeStrategy WatchAndWait])
]
reloadOverrideR :: Creature -> Reader World Creature
reloadOverrideR cr
| cr ^? crInv . ix (_crInvSel cr) . itConsumption . ammoLoaded == Just 0
&& cr ^. crStance . posture == Aiming
= return $ cr & crActionPlan . crStrategy .~ StrategyActions Reload reloadActions
| otherwise = return cr
where
reloadActions =
[ holsterWeapon
, WaitThen 1 $ DoActionWhileInterrupt NoAction crIsReloading (DoImpulses [ChangeStrategy WatchAndWait])
]
overrideInternalRRR
:: (Creature -> Reader World Bool)
-> (Creature -> Reader World Creature)
-> Creature
-> Reader World Creature
overrideInternalRRR test update cr = do
b <- test cr
if b then update cr else pure cr
overrideInternal
:: (Creature -> Bool)
-> (Creature -> Creature)
-> Creature
-> Creature
overrideInternal :: (Creature -> Bool) -> (Creature -> Creature) -> Creature -> Creature
overrideInternal test update cr
| test cr = update cr
| otherwise = cr
watchUpdateStratR
:: [ ((World, Creature) -> Bool, World -> Creature -> Strategy) ]
-> Creature
-> Reader World Creature
watchUpdateStratR fs cr = reader $ \w -> case cr ^? crActionPlan . crStrategy of
Just WatchAndWait -> cr
& crActionPlan . crStrategy .~ listGuard (fs, \_ _ -> WatchAndWait) (w, cr) w cr
_ -> cr
watchUpdateStrat
:: [ ((World, Creature) -> Bool, World -> Creature -> Strategy) ]
-> World
@@ -218,11 +141,6 @@ listGuard ( (test,y):ps, z ) x
| otherwise = listGuard (ps, z) x
listGuard (_,z) _ = z
targetYouWhenCognizantR :: Creature -> Reader World Creature
targetYouWhenCognizantR cr = reader $ \w -> case cr ^? crPerception . crAwarenessLevel . ix 0 of
Just (Cognizant _) -> cr & crIntention . targetCr ?~ _creatures w IM.! 0
_ -> cr & crIntention . targetCr .~ Nothing
targetYouWhenCognizant :: World -> Creature -> Creature
targetYouWhenCognizant w cr = case cr ^? crPerception . crAwarenessLevel . ix 0 of
-- so this caused a space leak: be careful with ?~