Refactor, try to limit dependencies

This commit is contained in:
2022-07-28 00:59:56 +01:00
parent 8aa5c17ab9
commit 160560af5f
418 changed files with 15104 additions and 13342 deletions
+240 -208
View File
@@ -1,251 +1,283 @@
module Dodge.Humanoid where
import Dodge.Data
import Dodge.Creature.Boid
import Dodge.Creature.YourControl
import Dodge.Creature.State
import Dodge.Creature.Impulse
import Dodge.Creature.ChainUpdates
import Dodge.Creature.Test
import Dodge.Creature.Volition
import LensHelp
import Dodge.Creature.ReaderUpdate
import Dodge.Creature.Action
import Dodge.Creature.Perception
import Dodge.Creature.Strategy
import Dodge.Creature.SentinelAI
import qualified IntMapHelp as IM
import Geometry
import RandomHelp
import Data.Maybe
import Dodge.Creature.Action
import Dodge.Creature
import Dodge.Data.CreatureEffect
import Dodge.Data.World
import Geometry
import qualified IntMapHelp as IM
import LensHelp
import RandomHelp
updateHumanoid :: Creature -> World -> World
updateHumanoid cr = case cr ^?! crType . humanoidAI of
ChaseAI -> defaultImpulsive
[ const doStrategyActions
, performActions
, const overrideMeleeCloseTarget
, const setViewPos
, setMvPos
, chaseCritMv
, chaseCritPerceptionUpdate [0]
, targetYouWhenCognizant
, const searchIfDamaged
, const (crMeleeCooldown %~ max 0 . subtract 1)
] cr
SpreadGunAI -> defaultImpulsive
[performActions
,watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
[DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
,DoActionThen
(DoActionWhile WdCrLOSTarget $ ArbitraryAction ChooseMovementSpreadGun)
(DoImpulses [ChangeStrategy WatchAndWait])
]
)
, (const crAwayFromPost, const goToPostStrat)
]
,perceptionUpdate [0]
,const doStrategyActions
,const reloadOverride
,targetYouWhenCognizant
,const $ overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(\cr' -> cr' & crActionPlan . apStrategy .~ WatchAndWait)
] cr
PistolAI -> defaultImpulsive
[ performActions
, watchUpdateStrat
[ (crHasTargetLOS, \w cr' -> StrategyActions (ShootAt 0)
[DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon,chooseMovementPistol cr' w])
, (const crAwayFromPost, const goToPostStrat)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $ overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
] cr
LtAutoAI -> defaultImpulsive
[ performActions
, watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
[DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
,DoActionThen
(DoActionWhile WdCrLOSTarget $ ArbitraryAction ChooseMovementLtAuto)
(DoImpulses [ChangeStrategy WatchAndWait])
ChaseAI ->
defaultImpulsive
[ const doStrategyActions
, performActions
, const overrideMeleeCloseTarget
, const setViewPos
, setMvPos
, chaseCritMv
, chaseCritPerceptionUpdate [0]
, targetYouWhenCognizant
, const searchIfDamaged
, const (crMeleeCooldown %~ max 0 . subtract 1)
]
cr
SpreadGunAI ->
defaultImpulsive
[ performActions
, watchUpdateStrat
[
( crHasTargetLOS
, \_ _ ->
StrategyActions
(ShootAt 0)
[ DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
, DoActionThen
(DoActionWhile WdCrLOSTarget $ ArbitraryAction ChooseMovementSpreadGun)
(DoImpulses [ChangeStrategy WatchAndWait])
]
)
, (const crAwayFromPost, const goToPostStrat)
]
)
, (const crAwayFromPost, const goToPostStrat)
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $
overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(\cr' -> cr' & crActionPlan . apStrategy .~ WatchAndWait)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $ overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
] cr
LauncherAI -> defaultImpulsive
[ performActions
, watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) [retreatFireLauncher])
, (const crAwayFromPost, const goToPostStrat)
cr
PistolAI ->
defaultImpulsive
[ performActions
, watchUpdateStrat
[
( crHasTargetLOS
, \w cr' ->
StrategyActions
(ShootAt 0)
[DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon, chooseMovementPistol cr' w]
)
, (const crAwayFromPost, const goToPostStrat)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $
overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $ overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
] cr
SwarmAI -> defaultImpulsive
[ flockToPointUsing' (encircleDistP 100) (meleeHeadingMove 0.1 0.1 (pi/4) 3.9)
, const (crMeleeCooldown %~ max 0 . subtract 1)
, perceptionUpdate [0]
, const doStrategyActions
, targetYouWhenCognizant
] cr
cr
LtAutoAI ->
defaultImpulsive
[ performActions
, watchUpdateStrat
[
( crHasTargetLOS
, \_ _ ->
StrategyActions
(ShootAt 0)
[ DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
, DoActionThen
(DoActionWhile WdCrLOSTarget $ ArbitraryAction ChooseMovementLtAuto)
(DoImpulses [ChangeStrategy WatchAndWait])
]
)
, (const crAwayFromPost, const goToPostStrat)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $
overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
]
cr
LauncherAI ->
defaultImpulsive
[ performActions
, watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) [retreatFireLauncher])
, (const crAwayFromPost, const goToPostStrat)
]
, perceptionUpdate [0]
, const doStrategyActions
, const reloadOverride
, targetYouWhenCognizant
, const $
overrideInternal
(\cr' -> crHasTarget cr' && crStratConMatches (GetTo (V2 0 0)) cr')
(crActionPlan . apStrategy .~ WatchAndWait)
]
cr
SwarmAI ->
defaultImpulsive
[ flockToPointUsing' (encircleDistP 100) (meleeHeadingMove 0.1 0.1 (pi / 4) 3.9)
, const (crMeleeCooldown %~ max 0 . subtract 1)
, perceptionUpdate [0]
, const doStrategyActions
, targetYouWhenCognizant
]
cr
AutoAI -> defaultImpulsive [sentinelAI] cr
FlockArmourChaseAI -> defaultImpulsive
[ watchUpdateStrat []
, const doStrategyActions
, performActions
, targetYouWhenCognizant
, setTargetMv (\_ -> _targetCr . _crIntention)
, flockACC
, perceptionUpdate [0]
, goToTarget
, const overrideMeleeCloseTarget
, const (crMeleeCooldown %~ max 0 . subtract 1)
] cr
FlockArmourChaseAI ->
defaultImpulsive
[ watchUpdateStrat []
, const doStrategyActions
, performActions
, targetYouWhenCognizant
, setTargetMv (\_ -> _targetCr . _crIntention)
, flockACC
, perceptionUpdate [0]
, goToTarget
, const overrideMeleeCloseTarget
, const (crMeleeCooldown %~ max 0 . subtract 1)
]
cr
MiniGunAI -> defaultImpulsive [sentinelFireType (const shootTillEmpty)] cr
LongAI -> defaultImpulsive [sentinelFireType $ const shootTillEmpty] cr
MultGunAI -> defaultImpulsive [sentinelExtraWatchUpdate
[ ( const $ not . crWeaponReady
, \_ _ -> StrategyActions Reload reloadActions
)
, (const $ not . crSafeDistFromTarg 150
, \_ _ -> StrategyActions Flee
[WdCrNegate (WdCrSafeDistFromTarget 150) `DoActionWhile` UseSelf CrFleeFromTarget-- (fleeFrom cr')
`DoActionThen` DoImpulses [ChangeStrategy WatchAndWait] ]
)
, (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
[ drawwp `DoActionThen` shootFirstMiss `DoActionThen` DoImpulses [ChangeStrategy WatchAndWait] ]
)
MultGunAI ->
defaultImpulsive
[ sentinelExtraWatchUpdate
[
( const $ not . crWeaponReady
, \_ _ -> StrategyActions Reload reloadActions
)
,
( const $ not . crSafeDistFromTarg 150
, \_ _ ->
StrategyActions
Flee
[ WdCrNegate (WdCrSafeDistFromTarget 150) `DoActionWhile` UseSelf CrFleeFromTarget -- (fleeFrom cr')
`DoActionThen` DoImpulses [ChangeStrategy WatchAndWait]
]
)
,
( crHasTargetLOS
, \_ _ ->
StrategyActions
(ShootAt 0)
[drawwp `DoActionThen` shootFirstMiss `DoActionThen` DoImpulses [ChangeStrategy WatchAndWait]]
)
]
]
] cr
cr
YourAI -> stateUpdate yourControl cr
InanimateAI -> id
where
drawwp = DoActionIfElse NoAction (WdCrBlfromCrBl CrIsAiming) (DoActionThen drawWeapon (WaitThen 50 NoAction))
reloadActions =
reloadActions =
[ holsterWeapon
, WaitThen 1 $ DoActionWhileInterrupt NoAction (WdCrBlfromCrBl CrIsReloading) (DoImpulses [ChangeStrategy WatchAndWait])
]
-- bit of a hack to get new random generators after each creature's update
defaultImpulsive :: [World -> Creature -> Creature]
-> Creature -> World -> World
defaultImpulsive ::
[World -> Creature -> Creature] ->
Creature ->
World ->
World
defaultImpulsive = fmap (fmap updateRandGen) . stateUpdate . impulsiveAIBefore . chainCreatureUpdates
where
updateRandGen w = let (_,g) = randomR (0,1::Int) (_randGen w)
in w & randGen .~ g
updateRandGen w =
let (_, g) = randomR (0, 1 :: Int) (_randGen w)
in w & randGen .~ g
chooseMovementPistol :: Creature -> World -> Action
chooseMovementPistol cr w = chooseMovementPistol' cr w
`DoActionThen`
DoImpulses [ChangeStrategy WatchAndWait]
chooseMovementPistol cr w =
chooseMovementPistol' cr w
`DoActionThen` DoImpulses [ChangeStrategy WatchAndWait]
chooseMovementPistol' :: Creature -> World -> Action
chooseMovementPistol' cr w = takeOneWeighted [chargeProb,retreatProb,strafeProb,strafeProb]
[chargeActions
,retreatActionsPistol ycr cr
,strafeLeftActions
,strafeRightActions
] & evalState $ g
chooseMovementPistol' cr w =
takeOneWeighted
[chargeProb, retreatProb, strafeProb, strafeProb]
[ chargeActions
, retreatActionsPistol ycr cr
, strafeLeftActions
, strafeRightActions
]
& evalState
$ g
where
g = _randGen w
cpos = _crPos cr
ycr = _creatures (_cWorld w) IM.! 0
ypos = _crPos ycr
chargeProb | dist cpos ypos > 300 = 5
| dist cpos ypos > 150 = 1
| otherwise = 0
strafeProb | dist cpos ypos > 150 = 1
| otherwise = 0
retreatProb | dist cpos ypos < 200 = 1 :: Float
| otherwise = 0
chargeProb
| dist cpos ypos > 300 = 5
| dist cpos ypos > 150 = 1
| otherwise = 0
strafeProb
| dist cpos ypos > 150 = 1
| otherwise = 0
retreatProb
| dist cpos ypos < 200 = 1 :: Float
| otherwise = 0
chargeActions =
[TurnToward ypos 0.1]
`DoImpulsesAlongside`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (V2 3 0)] ++ [[Move (V2 3 0),UseItem]])
strafeLeftActions =
DoImpulses [TurnToward yposr (2*pi)]
`DoActionThen`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (V2 0 3)] ++ [[Move (V2 0 3),UseItem]])
strafeRightActions =
DoImpulses [TurnToward yposl (2*pi)]
`DoActionThen`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (V2 0 (-3))] ++ [[Move (V2 0 (-3)),UseItem]])
`DoImpulsesAlongside` 3
`DoReplicate` ImpulsesList (replicate 9 [Move (V2 3 0)] ++ [[Move (V2 3 0), UseItem]])
strafeLeftActions =
DoImpulses [TurnToward yposr (2 * pi)]
`DoActionThen` 3
`DoReplicate` ImpulsesList (replicate 9 [Move (V2 0 3)] ++ [[Move (V2 0 3), UseItem]])
strafeRightActions =
DoImpulses [TurnToward yposl (2 * pi)]
`DoActionThen` 3
`DoReplicate` ImpulsesList (replicate 9 [Move (V2 0 (-3))] ++ [[Move (V2 0 (-3)), UseItem]])
yposl = ypos -.- 100 *.* vNormal (normalizeV $ ypos -.- cpos)
yposr = ypos +.+ 100 *.* vNormal (normalizeV $ ypos -.- cpos)
retreatActionsPistol :: Creature -> Creature -> Action
retreatActionsPistol tcr cr =
retreatActionsPistol tcr cr =
[TurnToward retreatOffset 0.2]
`DoImpulsesAlongside`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (V2 (-3) 0)] ++ [[UseItem]])
where
`DoImpulsesAlongside` 3
`DoReplicate` ImpulsesList (replicate 9 [Move (V2 (-3) 0)] ++ [[UseItem]])
where
cpos = _crPos cr
tpos = _crPos tcr
retreatOffset =
let a | dist cpos tpos < 50 = 0
| isLeftOfA (_crDir cr) (argV $ tpos -.- cpos)
= -0.7
| otherwise = 0.7
in fromMaybe tpos $ intersectLineLine' cpos
(cpos +.+ rotateV a (tpos -.- cpos))
tpos
(tpos +.+ vNormal (cpos -.- tpos))
retreatOffset =
let a
| dist cpos tpos < 50 = 0
| isLeftOfA (_crDir cr) (argV $ tpos -.- cpos) =
-0.7
| otherwise = 0.7
in fromMaybe tpos $
intersectLineLine'
cpos
(cpos +.+ rotateV a (tpos -.- cpos))
tpos
(tpos +.+ vNormal (cpos -.- tpos))
retreatFireLauncher :: Action
retreatFireLauncher = ImpulsesList ( [ UseItem ] : replicate 20 [ Turn 0.16 ])
`DoActionThen`
holsterWeapon
`DoActionThen`
ImpulsesList ( replicate 30 [ MoveForward 3 ])
`DoActionThen`
drawWeapon
`DoActionThen`
ImpulsesList ( [UseItem] : replicate 20 [ Turn $ negate 0.16 ] )
`DoActionThen`
holsterWeapon
`DoActionThen`
ImpulsesList ( replicate 15 [ MoveForward 3 ] )
`DoActionThen`
drawWeapon
`DoActionThen`
ImpulsesList (
replicate 100 [UseItem, ImpulseUseTarget (TurnTowardCr (pi/16)) -- $ \tcr -> TurnToward (_crPos tcr) (pi/16) ] )
])
`DoActionThen`
20
`WaitThen`
holsterWeapon
`DoActionThen`
ImpulsesList ( replicate 15 [ MoveForward 3 ] )
`DoActionThen`
DoImpulses [ChangeStrategy WatchAndWait]
retreatFireLauncher =
ImpulsesList ([UseItem] : replicate 20 [Turn 0.16])
`DoActionThen` holsterWeapon
`DoActionThen` ImpulsesList (replicate 30 [MoveForward 3])
`DoActionThen` drawWeapon
`DoActionThen` ImpulsesList ([UseItem] : replicate 20 [Turn $ negate 0.16])
`DoActionThen` holsterWeapon
`DoActionThen` ImpulsesList (replicate 15 [MoveForward 3])
`DoActionThen` drawWeapon
`DoActionThen` ImpulsesList
( replicate
100
[ UseItem
, ImpulseUseTarget (TurnTowardCr (pi / 16))
]
)
`DoActionThen` 20
`WaitThen` holsterWeapon
`DoActionThen` ImpulsesList (replicate 15 [MoveForward 3])
`DoActionThen` DoImpulses [ChangeStrategy WatchAndWait]