Dataify impulse/action/strategy function fields

This commit is contained in:
2022-07-21 21:45:31 +01:00
parent 14f3c8bc52
commit 583027112d
7 changed files with 45 additions and 36 deletions
+5 -4
View File
@@ -21,6 +21,7 @@ module Dodge.Creature.Action
) )
where where
import Dodge.Inventory.Add import Dodge.Inventory.Add
import Dodge.CreatureEffect
--import Dodge.ShortShow --import Dodge.ShortShow
import Dodge.Path import Dodge.Path
import Dodge.Default import Dodge.Default
@@ -111,18 +112,18 @@ performAction cr w ac = case ac of
(imps , Nothing ) -> (imps, Just afta) (imps , Nothing ) -> (imps, Just afta)
DoActionWhile f act -> performAction cr w $ DoActionWhilePartial act f act DoActionWhile f act -> performAction cr w $ DoActionWhilePartial act f act
DoActionWhilePartial partAc f resetAc DoActionWhilePartial partAc f resetAc
| f w cr -> case performAction cr w partAc of | doWdCrBl f w cr -> case performAction cr w partAc of
(imps, Just nxta) -> (imps, Just $ DoActionWhilePartial nxta f resetAc) (imps, Just nxta) -> (imps, Just $ DoActionWhilePartial nxta f resetAc)
(imps, Nothing) -> (imps, Just $ DoActionWhilePartial resetAc f resetAc) (imps, Nothing) -> (imps, Just $ DoActionWhilePartial resetAc f resetAc)
| otherwise -> performAction cr w partAc | otherwise -> performAction cr w partAc
DoActionIf f ifa DoActionIf f ifa
| f w cr -> performAction cr w ifa | doWdCrBl f w cr -> performAction cr w ifa
| otherwise -> ([],Nothing) | otherwise -> ([],Nothing)
DoActionIfElse ifa f elsea DoActionIfElse ifa f elsea
| f w cr -> performAction cr w ifa | doWdCrBl f w cr -> performAction cr w ifa
| otherwise -> performAction cr w elsea | otherwise -> performAction cr w elsea
DoActionWhileInterrupt repa f afta DoActionWhileInterrupt repa f afta
| f w cr -> (fst $ performAction cr w repa, Just $ DoActionWhileInterrupt repa f afta) | doWdCrBl f w cr -> (fst $ performAction cr w repa, Just $ DoActionWhileInterrupt repa f afta)
| otherwise -> performAction cr w afta | otherwise -> performAction cr w afta
DoActions [] -> ([], Nothing) DoActions [] -> ([], Nothing)
DoActions acs -> DoActions acs ->
+8 -6
View File
@@ -4,6 +4,7 @@ module Dodge.Creature.Impulse
, followThenClearImpulses , followThenClearImpulses
) where ) where
import Dodge.Data import Dodge.Data
import Dodge.CreatureEffect
import Dodge.Creature.Vocalization import Dodge.Creature.Vocalization
import Dodge.Creature.Impulse.Movement import Dodge.Creature.Impulse.Movement
import Dodge.Creature.Impulse.UseItem import Dodge.Creature.Impulse.UseItem
@@ -50,6 +51,7 @@ followImpulses w cr = foldr f (id, cr) (_apImpulse $ _crActionPlan cr)
followImpulse :: Creature -> World -> Impulse -> (World -> World , Creature) followImpulse :: Creature -> World -> Impulse -> (World -> World , Creature)
followImpulse cr w imp = case imp of followImpulse cr w imp = case imp of
ImpulseNothing -> (id,cr)
RandomImpulse rimp -> RandomImpulse rimp ->
let (newimp,newgen) = runState rimp (_randGen w) let (newimp,newgen) = runState rimp (_randGen w)
in first ((randGen .~ newgen) .) $ followImpulse cr w newimp in first ((randGen .~ newgen) .) $ followImpulse cr w newimp
@@ -69,16 +71,16 @@ followImpulse cr w imp = case imp of
DropItem -> undefined DropItem -> undefined
ChangeStrategy strat -> crup $ cr & crActionPlan . apStrategy .~ strat ChangeStrategy strat -> crup $ cr & crActionPlan . apStrategy .~ strat
AddGoal gl -> crup $ cr & crActionPlan . apGoal .:~ gl AddGoal gl -> crup $ cr & crActionPlan . apGoal .:~ gl
ArbitraryImpulseFunction f -> crup $ f w cr ArbitraryImpulseFunction f -> crup $ doWdCrCr f w cr
ArbitraryImpulse f -> followImpulse cr w (f cr w) ArbitraryImpulse f -> followImpulse cr w (doCrWdImp f cr w)
ArbitraryImpulseEffect f -> (f cr, cr) ArbitraryImpulseEffect f -> (doCrWdWd f cr, cr)
ImpulseUseTargetCID f -> case cr ^? crIntention . targetCr . _Just of ImpulseUseTargetCID f -> case cr ^? crIntention . targetCr . _Just of
Just tcr -> followImpulse cr w (f $ _crID tcr) Just tcr -> followImpulse cr w (doIntImp f $ _crID tcr)
_ -> crup cr _ -> crup cr
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 (doCrImp f tcr)
_ -> crup cr _ -> crup cr
ImpulseUseAheadPos f -> followImpulse cr w (f (_crPos cr +.+ 20 *.* unitVectorAtAngle (_crDir cr))) ImpulseUseAheadPos f -> followImpulse cr w (doP2Imp f (_crPos cr +.+ 20 *.* unitVectorAtAngle (_crDir cr)))
MvForward -> crup $ crMvForward speed cr MvForward -> crup $ crMvForward speed cr
MvTurnToward p -> crup MvTurnToward p -> crup
$ creatureTurnToward p (turnRad $ safeAngleVV (p -.- cpos) (unitVectorAtAngle cdir)) cr $ creatureTurnToward p (turnRad $ safeAngleVV (p -.- cpos) (unitVectorAtAngle cdir)) cr
+1 -1
View File
@@ -144,7 +144,7 @@ reloadOverride cr
reloadActions = reloadActions =
[ holsterWeapon [ holsterWeapon
, 1 `WaitThen` , 1 `WaitThen`
DoActionWhileInterrupt NoAction (const crIsReloading) DoActionWhileInterrupt NoAction (WdCrBlfromCrBl CrIsReloading)
(DoImpulses [ChangeStrategy WatchAndWait]) (DoImpulses [ChangeStrategy WatchAndWait])
] ]
+3 -3
View File
@@ -23,7 +23,7 @@ sentinelAI w = reloadOverride .
[ (crHasTargetLOS [ (crHasTargetLOS
, \ _ cr -> StrategyActions (ShootAt (fromJust $ tcid cr)) , \ _ cr -> StrategyActions (ShootAt (fromJust $ tcid cr))
[ DoActionIf [ DoActionIf
(const $ not . crIsAiming) (WdCrNegate $ WdCrBlfromCrBl CrIsAiming)
(drawWeapon `DoActionThen` (50 `WaitThen` NoAction)) (drawWeapon `DoActionThen` (50 `WaitThen` NoAction))
`DoActionThen` lostest `DoActionThen` lostest
`DoActionWhile` advanceShoot `DoActionWhile` advanceShoot
@@ -38,7 +38,7 @@ sentinelAI w = reloadOverride .
where where
advanceShoot = DoImpulses [UseItem, MoveForward 3] advanceShoot = DoImpulses [UseItem, MoveForward 3]
tcid cr = _crID <$> _targetCr (_crIntention cr) tcid cr = _crID <$> _targetCr (_crIntention cr)
lostest w' cr = maybe False (\cid -> canSee (_crID cr) cid w') (tcid cr) lostest = WdCrLOSTarget -- w' cr = maybe False (\cid -> canSee (_crID cr) cid w') (tcid cr)
--chainCreatureUpdates :: [World -> Creature -> Creature] -> World -> Creature -> Creature --chainCreatureUpdates :: [World -> Creature -> Creature] -> World -> Creature -> Creature
--chainCreatureUpdates ls w cr = foldr (\f -> f w) cr ls --chainCreatureUpdates ls w cr = foldr (\f -> f w) cr ls
@@ -65,7 +65,7 @@ sentinelFireType f = chainCreatureUpdates
(crActionPlan . apStrategy .~ WatchAndWait) (crActionPlan . apStrategy .~ WatchAndWait)
] ]
where where
drawwp = DoActionIfElse NoAction (const crIsAiming) (DoActionThen drawWeapon (WaitThen 50 NoAction)) drawwp = DoActionIfElse NoAction (WdCrBlfromCrBl CrIsAiming) (DoActionThen drawWeapon (WaitThen 50 NoAction))
aiming = AimAt aiming = AimAt
{ _targetID = 0 { _targetID = 0
, _targetSeenAt = V2 0 0 -- hack , _targetSeenAt = V2 0 0 -- hack
+2 -2
View File
@@ -23,7 +23,7 @@ fleeFrom cr mtcr = case mtcr of
shootTillEmpty :: Action shootTillEmpty :: Action
--shootTillEmpty = (crCanShoot `DoActionWhile` DoImpulses [UseItem]) --shootTillEmpty = (crCanShoot `DoActionWhile` DoImpulses [UseItem])
shootTillEmpty = (const crCanShoot `DoActionWhile` DoImpulses [UseItem]) shootTillEmpty = (WdCrBlfromCrBl CrCanShoot `DoActionWhile` DoImpulses [UseItem])
`DoActionThen` 20 `WaitThen` holsterWeapon `DoActionThen` 20 `WaitThen` holsterWeapon
--advanceShoot :: Int -> Action --advanceShoot :: Int -> Action
@@ -39,5 +39,5 @@ shootFirstMiss :: Action
shootFirstMiss = shootFirstMiss =
LeadTarget (V2 30 50) `DoActionThen` LeadTarget (V2 30 50) `DoActionThen`
DoImpulses [UseItem] `DoActionThen` DoImpulses [UseItem] `DoActionThen`
(const crCanShoot `DoActionWhile` DoActions [LeadTarget (V2 0 0),DoImpulses [UseItem]]) (WdCrBlfromCrBl CrCanShoot `DoActionWhile` DoActions [LeadTarget (V2 0 0),DoImpulses [UseItem]])
`DoActionThen` 20 `WaitThen` holsterWeapon `DoActionThen` 20 `WaitThen` holsterWeapon
+16 -11
View File
@@ -10,6 +10,7 @@ circular imports are probably not a good idea.
{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingStrategies #-}
module Dodge.Data module Dodge.Data
( module Dodge.Data ( module Dodge.Data
, module Dodge.Data.CreatureEffect
, module Dodge.Data.WorldEffect , module Dodge.Data.WorldEffect
, module Dodge.Data.Button , module Dodge.Data.Button
, module Dodge.Data.Item.Params , module Dodge.Data.Item.Params
@@ -70,6 +71,7 @@ module Dodge.Data
, module Dodge.Data.RadarBlip , module Dodge.Data.RadarBlip
, module Dodge.Data.PathGraph , module Dodge.Data.PathGraph
) where ) where
import Dodge.Data.CreatureEffect
import Dodge.Data.WorldEffect import Dodge.Data.WorldEffect
import Dodge.Data.Button import Dodge.Data.Button
import Dodge.Data.Item.Params import Dodge.Data.Item.Params
@@ -668,6 +670,7 @@ data ActionPlan
,_apStrategy :: Strategy ,_apStrategy :: Strategy
,_apGoal :: [Goal] ,_apGoal :: [Goal]
} }
data RandImpulse = RandImpulseList [Impulse]
data Impulse data Impulse
= Move Point2 = Move Point2
| MoveForward Float | MoveForward Float
@@ -687,18 +690,19 @@ data Impulse
| MakeSound SoundID | MakeSound SoundID
| ChangeStrategy Strategy | ChangeStrategy Strategy
| AddGoal Goal | AddGoal Goal
| ArbitraryImpulseFunction (World -> Creature -> Creature) | ArbitraryImpulseFunction WdCrCr
| ArbitraryImpulse (Creature -> World -> Impulse) | ArbitraryImpulse CrWdImp
| ArbitraryImpulseEffect (Creature -> World -> World) | ArbitraryImpulseEffect CrWdWd
| ImpulseUseTargetCID | ImpulseUseTargetCID
{_impulseUseTargetCID :: Int -> Impulse {_impulseUseTargetCID :: IntImp
} }
| ImpulseUseTarget | ImpulseUseTarget
{_impulseUseTarget :: Creature -> Impulse {_impulseUseTarget :: CrImp
} }
| ImpulseUseAheadPos | ImpulseUseAheadPos
{_impulseUseAheadPos :: Point2 -> Impulse {_impulseUseAheadPos :: P2Imp
} }
| ImpulseNothing
instance Show Impulse where instance Show Impulse where
show imp = case imp of show imp = case imp of
Move p -> "Move "++shortPoint2 p Move p -> "Move "++shortPoint2 p
@@ -725,6 +729,7 @@ instance Show Impulse where
ImpulseUseTarget {} -> "ImpulseUseTarget" ImpulseUseTarget {} -> "ImpulseUseTarget"
ImpulseUseAheadPos {} -> "ImpulseUseAheadPos" ImpulseUseAheadPos {} -> "ImpulseUseAheadPos"
RandomImpulse {} -> "RandomImpulse" RandomImpulse {} -> "RandomImpulse"
ImpulseNothing -> "ImpulseNothing"
-- deriving (Eq,Ord,Show) -- deriving (Eq,Ord,Show)
infixr 9 `WaitThen` infixr 9 `WaitThen`
infixr 9 `DoActionThen` infixr 9 `DoActionThen`
@@ -760,26 +765,26 @@ data Action
,_waitThenAction :: Action ,_waitThenAction :: Action
} }
| DoActionWhile | DoActionWhile
{_doActionWhileCondition :: World -> Creature -> Bool {_doActionWhileCondition :: WdCrBl
,_doActionWhileAction :: Action ,_doActionWhileAction :: Action
} }
| DoActionWhilePartial | DoActionWhilePartial
{_doActionWhilePartial :: Action {_doActionWhilePartial :: Action
,_doActionWhileCondition :: World -> Creature -> Bool ,_doActionWhileCondition :: WdCrBl
,_doActionWhileAction :: Action ,_doActionWhileAction :: Action
} }
| DoActionIf | DoActionIf
{_doActionIfCondition :: World -> Creature -> Bool {_doActionIfCondition :: WdCrBl
,_doActionIfAction :: Action ,_doActionIfAction :: Action
} }
| DoActionIfElse | DoActionIfElse
{_doActionIfElseIfAction :: Action {_doActionIfElseIfAction :: Action
,_doActionIfElseCondition :: World -> Creature -> Bool ,_doActionIfElseCondition :: WdCrBl
,_doActionIfElseElseAction :: Action ,_doActionIfElseElseAction :: Action
} }
| DoActionWhileInterrupt | DoActionWhileInterrupt
{_doActionWhileThenDo :: Action {_doActionWhileThenDo :: Action
,_doActionWhileThenCondition :: World -> Creature -> Bool ,_doActionWhileThenCondition :: WdCrBl
,_doActionWhileThenThen :: Action ,_doActionWhileThenThen :: Action
} }
| DoActions | DoActions
+10 -9
View File
@@ -37,9 +37,9 @@ updateHumanoid cr = case cr ^?! crType . humanoidAI of
[performActions [performActions
,watchUpdateStrat ,watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) [ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
[DoActionIf (const $ not . crIsAiming) drawWeapon [DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
,DoActionThen ,DoActionThen
(DoActionWhile crHasTargetLOS $ ArbitraryAction chooseMovementSpreadGun) (DoActionWhile WdCrLOSTarget $ ArbitraryAction chooseMovementSpreadGun)
(DoImpulses [ChangeStrategy WatchAndWait]) (DoImpulses [ChangeStrategy WatchAndWait])
] ]
) )
@@ -57,7 +57,7 @@ updateHumanoid cr = case cr ^?! crType . humanoidAI of
[ performActions [ performActions
, watchUpdateStrat , watchUpdateStrat
[ (crHasTargetLOS, \w cr' -> StrategyActions (ShootAt 0) [ (crHasTargetLOS, \w cr' -> StrategyActions (ShootAt 0)
[DoActionIf (const $ not . crIsAiming) drawWeapon,chooseMovementPistol cr' w]) [DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon,chooseMovementPistol cr' w])
, (const crAwayFromPost, const goToPostStrat) , (const crAwayFromPost, const goToPostStrat)
] ]
, perceptionUpdate [0] , perceptionUpdate [0]
@@ -72,9 +72,9 @@ updateHumanoid cr = case cr ^?! crType . humanoidAI of
[ performActions [ performActions
, watchUpdateStrat , watchUpdateStrat
[ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) [ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
[DoActionIf (const $ not . crIsAiming) drawWeapon [DoActionIf (WdCrNegate $ WdCrBlfromCrBl CrIsAiming) drawWeapon
,DoActionThen ,DoActionThen
(DoActionWhile crHasTargetLOS $ ArbitraryAction chooseMovementLtAuto) (DoActionWhile WdCrLOSTarget $ ArbitraryAction chooseMovementLtAuto)
(DoImpulses [ChangeStrategy WatchAndWait]) (DoImpulses [ChangeStrategy WatchAndWait])
] ]
) )
@@ -130,7 +130,7 @@ updateHumanoid cr = case cr ^?! crType . humanoidAI of
) )
, (const $ not . crSafeDistFromTarg 150 , (const $ not . crSafeDistFromTarg 150
, \_ cr' -> StrategyActions Flee , \_ cr' -> StrategyActions Flee
[const (not . crSafeDistFromTarg 150) `DoActionWhile` UseTarget (fleeFrom cr') [WdCrNegate (WdCrSafeDistFromTarget 150) `DoActionWhile` UseTarget (fleeFrom cr')
`DoActionThen` DoImpulses [ChangeStrategy WatchAndWait] ] `DoActionThen` DoImpulses [ChangeStrategy WatchAndWait] ]
) )
, (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) , (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0)
@@ -141,10 +141,10 @@ updateHumanoid cr = case cr ^?! crType . humanoidAI of
YourAI -> stateUpdate yourControl cr YourAI -> stateUpdate yourControl cr
InanimateAI -> id InanimateAI -> id
where where
drawwp = DoActionIfElse NoAction (const crIsAiming) (DoActionThen drawWeapon (WaitThen 50 NoAction)) drawwp = DoActionIfElse NoAction (WdCrBlfromCrBl CrIsAiming) (DoActionThen drawWeapon (WaitThen 50 NoAction))
reloadActions = reloadActions =
[ holsterWeapon [ holsterWeapon
, WaitThen 1 $ DoActionWhileInterrupt NoAction (const crIsReloading) (DoImpulses [ChangeStrategy WatchAndWait]) , WaitThen 1 $ DoActionWhileInterrupt NoAction (WdCrBlfromCrBl CrIsReloading) (DoImpulses [ChangeStrategy WatchAndWait])
] ]
-- bit of a hack to get new random generators after each creature's update -- bit of a hack to get new random generators after each creature's update
defaultImpulsive :: [World -> Creature -> Creature] defaultImpulsive :: [World -> Creature -> Creature]
@@ -268,7 +268,8 @@ retreatFireLauncher = ImpulsesList ( [ UseItem ] : replicate 20 [ Turn 0.16 ])
drawWeapon drawWeapon
`DoActionThen` `DoActionThen`
ImpulsesList ( ImpulsesList (
replicate 100 [UseItem, ImpulseUseTarget $ \tcr -> TurnToward (_crPos tcr) (pi/16) ] ) replicate 100 [UseItem, ImpulseUseTarget (TurnTowardCr (pi/16)) -- $ \tcr -> TurnToward (_crPos tcr) (pi/16) ] )
])
`DoActionThen` `DoActionThen`
20 20
`WaitThen` `WaitThen`