AI refactor

This commit is contained in:
2021-05-14 12:04:12 +02:00
parent 2dd3756682
commit 0b26761be5
14 changed files with 282 additions and 762 deletions
+53 -6
View File
@@ -16,10 +16,13 @@ import Dodge.Item.Weapon
import Dodge.Item.Consumable
import Geometry
import Picture
import Dodge.RandomHelp
import Data.Maybe
import qualified Data.IntMap.Strict as IM
import Control.Lens
import Control.Monad.State
import System.Random
pistolCrit :: Creature
pistolCrit = defaultCreature
@@ -27,7 +30,7 @@ pistolCrit = defaultCreature
, _crUpdate = stateUpdate $ impulsiveAI $ composeInternalAIs
[ performActions
, watchUpdateStrat
[ (crHasTargetLOS, \w cr -> StrategyActions (ShootAt 0) [chooseMovement cr w])
[ (crHasTargetLOS, \w cr -> StrategyActions (ShootAt 0) [DoActionIf (not . crIsAiming) DrawWeapon,chooseMovement cr w])
, (crAwayFromPost, goToPostStrat)
]
, basicPerceptionUpdate [0]
@@ -48,15 +51,58 @@ pistolCrit = defaultCreature
, _crRad = 10
, _crHP = 500
}
chooseMovement :: Creature -> World -> Action
chooseMovement cr w = undefined
chooseMovement cr w = chooseMovement' cr w
`DoActionThen`
DoImpulses [ChangeStrategy WatchAndWait]
retreatAction :: Creature -> Creature -> Action
retreatAction tcr cr =
chooseMovement' :: Creature -> World -> Action
chooseMovement' cr w = takeOneWeighted [chargeProb,retreatProb,strafeProb,strafeProb]
[chargeActions
,retreatActions ycr cr
,strafeLeftActions
,strafeRightActions
] & evalState $ g
where
g = _randGen w
cpos = _crPos cr
ycr = _creatures 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
chargeActions =
[TurnToward ypos 0.1]
`DoImpulsesAlongside`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (3,0)] ++ [[Move (3,0),UseItem]])
strafeLeftActions =
DoImpulses [TurnToward yposr (2*pi)]
`DoActionThen`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (0,3)] ++ [[Move (0,3),UseItem]])
strafeRightActions =
DoImpulses [TurnToward yposl (2*pi)]
`DoActionThen`
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (0,-3)] ++ [[Move (0,-3),UseItem]])
yposl = ypos -.- 100 *.* (vNormal $ normalizeV $ ypos -.- cpos)
yposr = ypos +.+ 100 *.* (vNormal $ normalizeV $ ypos -.- cpos)
retreatActions :: Creature -> Creature -> Action
retreatActions tcr cr =
[TurnToward retreatOffset 0.2]
`DoImpulsesAlongside`
NoAction
3
`DoReplicate`
ImpulsesList (replicate 9 [Move (-3,0)] ++ [[UseItem]])
where
cpos = _crPos cr
tpos = _crPos tcr
@@ -70,3 +116,4 @@ retreatAction tcr cr =
tpos
(tpos +.+ vNormal (cpos -.- tpos))