module Game.LambdaHack.Client.AI.PickActorM
( pickActorToMove, setTargetFromDoctrines
) where
import Prelude ()
import Game.LambdaHack.Core.Prelude
import qualified Data.EnumMap.Strict as EM
import Data.Ratio
import Game.LambdaHack.Client.AI.ConditionM
import Game.LambdaHack.Client.AI.PickTargetM
import Game.LambdaHack.Client.Bfs
import Game.LambdaHack.Client.BfsM
import Game.LambdaHack.Client.MonadClient
import Game.LambdaHack.Client.State
import Game.LambdaHack.Common.Actor
import Game.LambdaHack.Common.ActorState
import Game.LambdaHack.Common.Faction
import Game.LambdaHack.Common.Kind
import Game.LambdaHack.Common.Level
import Game.LambdaHack.Common.Misc
import Game.LambdaHack.Common.MonadStateRead
import Game.LambdaHack.Common.Point
import Game.LambdaHack.Common.State
import qualified Game.LambdaHack.Common.Tile as Tile
import Game.LambdaHack.Common.Time
import Game.LambdaHack.Common.Types
import Game.LambdaHack.Content.ModeKind
import Game.LambdaHack.Core.Frequency
import Game.LambdaHack.Core.Random
import qualified Game.LambdaHack.Definition.Ability as Ability
pickActorToMove :: MonadClient m
=> [(ActorId, Actor)] -> [(ActorId, Actor)] -> Maybe ActorId
-> m ActorId
pickActorToMove :: [(ActorId, Actor)]
-> [(ActorId, Actor)] -> Maybe ActorId -> m ActorId
pickActorToMove foeAssocs :: [(ActorId, Actor)]
foeAssocs friendAssocs :: [(ActorId, Actor)]
friendAssocs maidToAvoid :: Maybe ActorId
maidToAvoid = do
COps{TileSpeedup
coTileSpeedup :: COps -> TileSpeedup
coTileSpeedup :: TileSpeedup
coTileSpeedup} <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
ActorMaxSkills
actorMaxSkills <- (State -> ActorMaxSkills) -> m ActorMaxSkills
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> ActorMaxSkills
sactorMaxSkills
Maybe ActorId
mleader <- (StateClient -> Maybe ActorId) -> m (Maybe ActorId)
forall (m :: * -> *) a.
MonadClientRead m =>
(StateClient -> a) -> m a
getsClient StateClient -> Maybe ActorId
sleader
let oldAid :: ActorId
oldAid = ActorId -> Maybe ActorId -> ActorId
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> ActorId
forall a. HasCallStack => [Char] -> a
error ([Char] -> ActorId) -> [Char] -> ActorId
forall a b. (a -> b) -> a -> b
$ "" [Char] -> Maybe ActorId -> [Char]
forall v. Show v => [Char] -> v -> [Char]
`showFailure` Maybe ActorId
maidToAvoid) Maybe ActorId
mleader
Actor
oldBody <- (State -> Actor) -> m Actor
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Actor) -> m Actor) -> (State -> Actor) -> m Actor
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> Actor
getActorBody ActorId
oldAid
let side :: FactionId
side = Actor -> FactionId
bfid Actor
oldBody
arena :: LevelId
arena = Actor -> LevelId
blid Actor
oldBody
Level
lvl <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
arena
Time
localTime <- (State -> Time) -> m Time
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Time) -> m Time) -> (State -> Time) -> m Time
forall a b. (a -> b) -> a -> b
$ LevelId -> State -> Time
getLocalTime LevelId
arena
Bool
condInMelee <- LevelId -> m Bool
forall (m :: * -> *). MonadClient m => LevelId -> m Bool
condInMeleeM LevelId
arena
Faction
fact <- (State -> Faction) -> m Faction
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Faction) -> m Faction)
-> (State -> Faction) -> m Faction
forall a b. (a -> b) -> a -> b
$ (EnumMap FactionId Faction -> FactionId -> Faction
forall k a. Enum k => EnumMap k a -> k -> a
EM.! FactionId
side) (EnumMap FactionId Faction -> Faction)
-> (State -> EnumMap FactionId Faction) -> State -> Faction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> EnumMap FactionId Faction
sfactionD
[(ActorId, Actor)]
ours <- (State -> [(ActorId, Actor)]) -> m [(ActorId, Actor)]
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> [(ActorId, Actor)]) -> m [(ActorId, Actor)])
-> (State -> [(ActorId, Actor)]) -> m [(ActorId, Actor)]
forall a b. (a -> b) -> a -> b
$ FactionId -> LevelId -> State -> [(ActorId, Actor)]
fidActorRegularAssocs FactionId
side LevelId
arena
let pickOld :: m ActorId
pickOld = do
m (Maybe TgtAndPath) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Maybe TgtAndPath) -> m ()) -> m (Maybe TgtAndPath) -> m ()
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId
oldAid, Actor
oldBody)
ActorId -> m ActorId
forall (m :: * -> *) a. Monad m => a -> m a
return ActorId
oldAid
oursNotSleeping :: [(ActorId, Actor)]
oursNotSleeping = ((ActorId, Actor) -> Bool)
-> [(ActorId, Actor)] -> [(ActorId, Actor)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(_, b :: Actor
b) -> Actor -> Watchfulness
bwatch Actor
b Watchfulness -> Watchfulness -> Bool
forall a. Eq a => a -> a -> Bool
/= Watchfulness
WSleep) [(ActorId, Actor)]
ours
case [(ActorId, Actor)]
oursNotSleeping of
_ |
(Bool, Bool) -> Bool
forall a b. (a, b) -> b
snd (Faction -> (Bool, Bool)
autoDungeonLevel Faction
fact) Bool -> Bool -> Bool
&& Maybe ActorId -> Bool
forall a. Maybe a -> Bool
isNothing Maybe ActorId
maidToAvoid -> m ActorId
pickOld
[] -> m ActorId
pickOld
[(aidNotSleeping :: ActorId
aidNotSleeping, bNotSleeping :: Actor
bNotSleeping)] -> do
m (Maybe TgtAndPath) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Maybe TgtAndPath) -> m ()) -> m (Maybe TgtAndPath) -> m ()
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId
aidNotSleeping, Actor
bNotSleeping)
ActorId -> m ActorId
forall (m :: * -> *) a. Monad m => a -> m a
return ActorId
aidNotSleeping
_ -> do
let refresh :: (ActorId, Actor) -> m ((ActorId, Actor), Maybe TgtAndPath)
refresh aidBody :: (ActorId, Actor)
aidBody = do
Maybe TgtAndPath
mtgt <- [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId, Actor)
aidBody
((ActorId, Actor), Maybe TgtAndPath)
-> m ((ActorId, Actor), Maybe TgtAndPath)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ActorId, Actor)
aidBody, Maybe TgtAndPath
mtgt)
[((ActorId, Actor), Maybe TgtAndPath)]
oursTgtRaw <- ((ActorId, Actor) -> m ((ActorId, Actor), Maybe TgtAndPath))
-> [(ActorId, Actor)] -> m [((ActorId, Actor), Maybe TgtAndPath)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (ActorId, Actor) -> m ((ActorId, Actor), Maybe TgtAndPath)
refresh [(ActorId, Actor)]
oursNotSleeping
EnumMap ActorId (Point, Time)
oldFleeD <- (StateClient -> EnumMap ActorId (Point, Time))
-> m (EnumMap ActorId (Point, Time))
forall (m :: * -> *) a.
MonadClientRead m =>
(StateClient -> a) -> m a
getsClient StateClient -> EnumMap ActorId (Point, Time)
sfleeD
let recentlyFled :: ActorId -> Bool
recentlyFled aid :: ActorId
aid = Bool -> ((Point, Time) -> Bool) -> Maybe (Point, Time) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False
(\(_, time :: Time
time) -> Time -> Time -> Bool
timeRecent5 Time
localTime Time
time)
(ActorId
aid ActorId -> EnumMap ActorId (Point, Time) -> Maybe (Point, Time)
forall k a. Enum k => k -> EnumMap k a -> Maybe a
`EM.lookup` EnumMap ActorId (Point, Time)
oldFleeD)
goodGeneric :: ((ActorId, Actor), Maybe TgtAndPath)
-> Maybe ((ActorId, Actor), TgtAndPath)
goodGeneric (_, Nothing) = Maybe ((ActorId, Actor), TgtAndPath)
forall a. Maybe a
Nothing
goodGeneric (_, Just TgtAndPath{tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Maybe AndPath
Nothing}) = Maybe ((ActorId, Actor), TgtAndPath)
forall a. Maybe a
Nothing
goodGeneric ((aid :: ActorId
aid, b :: Actor
b), Just tgt :: TgtAndPath
tgt) = case Maybe ActorId
maidToAvoid of
_ | ActorId
aid ActorId -> ActorId -> Bool
forall a. Eq a => a -> a -> Bool
== ActorId
oldAid Bool -> Bool -> Bool
&& Actor -> Bool
actorWaits Actor
b -> Maybe ((ActorId, Actor), TgtAndPath)
forall a. Maybe a
Nothing
Nothing -> ((ActorId, Actor), TgtAndPath)
-> Maybe ((ActorId, Actor), TgtAndPath)
forall a. a -> Maybe a
Just ((ActorId
aid, Actor
b), TgtAndPath
tgt)
Just aidToAvoid :: ActorId
aidToAvoid ->
if ActorId
aid ActorId -> ActorId -> Bool
forall a. Eq a => a -> a -> Bool
== ActorId
aidToAvoid
then Maybe ((ActorId, Actor), TgtAndPath)
forall a. Maybe a
Nothing
else ((ActorId, Actor), TgtAndPath)
-> Maybe ((ActorId, Actor), TgtAndPath)
forall a. a -> Maybe a
Just ((ActorId
aid, Actor
b), TgtAndPath
tgt)
oursTgt :: [((ActorId, Actor), TgtAndPath)]
oursTgt = (((ActorId, Actor), Maybe TgtAndPath)
-> Maybe ((ActorId, Actor), TgtAndPath))
-> [((ActorId, Actor), Maybe TgtAndPath)]
-> [((ActorId, Actor), TgtAndPath)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ((ActorId, Actor), Maybe TgtAndPath)
-> Maybe ((ActorId, Actor), TgtAndPath)
goodGeneric [((ActorId, Actor), Maybe TgtAndPath)]
oursTgtRaw
actorVulnerable :: ((ActorId, Actor), TgtAndPath) -> m Bool
actorVulnerable ((aid :: ActorId
aid, body :: Actor
body), _) = do
let actorMaxSk :: Skills
actorMaxSk = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
aid
Bool
condAnyHarmfulFoeAdj <-
(State -> Bool) -> m Bool
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Bool) -> m Bool) -> (State -> Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ ActorMaxSkills -> ActorId -> State -> Bool
anyHarmfulFoeAdj ActorMaxSkills
actorMaxSkills ActorId
aid
[(Int, (ActorId, Actor))]
threatDistL <- (State -> [(Int, (ActorId, Actor))]) -> m [(Int, (ActorId, Actor))]
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> [(Int, (ActorId, Actor))])
-> m [(Int, (ActorId, Actor))])
-> (State -> [(Int, (ActorId, Actor))])
-> m [(Int, (ActorId, Actor))]
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)] -> ActorId -> State -> [(Int, (ActorId, Actor))]
meleeThreatDistList [(ActorId, Actor)]
foeAssocs ActorId
aid
(fleeL :: [(Int, Point)]
fleeL, _) <- [(ActorId, Actor)] -> ActorId -> m ([(Int, Point)], [(Int, Point)])
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)] -> ActorId -> m ([(Int, Point)], [(Int, Point)])
fleeList [(ActorId, Actor)]
foeAssocs ActorId
aid
Bool
condSupport1 <- [(ActorId, Actor)] -> Int -> ActorId -> m Bool
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)] -> Int -> ActorId -> m Bool
condSupport [(ActorId, Actor)]
friendAssocs 1 ActorId
aid
Bool
condSolo <- [(ActorId, Actor)] -> ActorId -> m Bool
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)] -> ActorId -> m Bool
condAloneM [(ActorId, Actor)]
friendAssocs ActorId
aid
let condCanFlee :: Bool
condCanFlee = Bool -> Bool
not ([(Int, Point)] -> Bool
forall a. [a] -> Bool
null [(Int, Point)]
fleeL)
heavilyDistressed :: Bool
heavilyDistressed =
ResDelta -> Bool
deltasSerious (Actor -> ResDelta
bcalmDelta Actor
body)
speed1_5 :: Speed
speed1_5 = Rational -> Speed -> Speed
speedScale (3Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
%2) (Skills -> Speed
gearSpeed Skills
actorMaxSk)
condCanMelee :: Bool
condCanMelee = ActorMaxSkills -> ActorId -> Actor -> Bool
actorCanMelee ActorMaxSkills
actorMaxSkills ActorId
aid Actor
body
threatAdj :: [(Int, (ActorId, Actor))]
threatAdj = ((Int, (ActorId, Actor)) -> Bool)
-> [(Int, (ActorId, Actor))] -> [(Int, (ActorId, Actor))]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1) (Int -> Bool)
-> ((Int, (ActorId, Actor)) -> Int)
-> (Int, (ActorId, Actor))
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (ActorId, Actor)) -> Int
forall a b. (a, b) -> a
fst) [(Int, (ActorId, Actor))]
threatDistL
condManyThreatAdj :: Bool
condManyThreatAdj = [(Int, (ActorId, Actor))] -> Int
forall a. [a] -> Int
length [(Int, (ActorId, Actor))]
threatAdj Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 2
condFastThreatAdj :: Bool
condFastThreatAdj =
((Int, (ActorId, Actor)) -> Bool)
-> [(Int, (ActorId, Actor))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(_, (aid2 :: ActorId
aid2, _)) ->
let ar2 :: Skills
ar2 = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
aid2
in Skills -> Speed
gearSpeed Skills
ar2 Speed -> Speed -> Bool
forall a. Ord a => a -> a -> Bool
> Speed
speed1_5)
[(Int, (ActorId, Actor))]
threatAdj
condNonStealthyThreatAdj :: Bool
condNonStealthyThreatAdj =
((Int, (ActorId, Actor)) -> Bool)
-> [(Int, (ActorId, Actor))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(_, (aid2 :: ActorId
aid2, b2 :: Actor
b2)) ->
let ar2 :: Skills
ar2 = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
aid2
in Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkShine Skills
ar2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
Bool -> Bool -> Bool
|| Point -> Bool
isLit (Actor -> Point
bpos Actor
b2))
[(Int, (ActorId, Actor))]
threatAdj
isLit :: Point -> Bool
isLit pos :: Point
pos = TileSpeedup -> ContentId TileKind -> Bool
Tile.isLit TileSpeedup
coTileSpeedup (Level
lvl Level -> Point -> ContentId TileKind
`at` Point
pos)
fleeingMakesSense :: Bool
fleeingMakesSense =
Bool -> Bool
not Bool
condCanMelee
Bool -> Bool -> Bool
|| (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkSight Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 2
Bool -> Bool -> Bool
|| Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkNocto Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 2)
Bool -> Bool -> Bool
&& (Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkShine Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 2
Bool -> Bool -> Bool
|| Bool
condNonStealthyThreatAdj Bool -> Bool -> Bool
|| [(Int, (ActorId, Actor))] -> Bool
forall a. [a] -> Bool
null [(Int, (ActorId, Actor))]
threatAdj)
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$!
Bool -> Bool
not Bool
condFastThreatAdj
Bool -> Bool -> Bool
&& Bool
fleeingMakesSense
Bool -> Bool -> Bool
&& if | Bool
condAnyHarmfulFoeAdj ->
Bool -> Bool
not Bool
condCanMelee
Bool -> Bool -> Bool
|| Bool
condManyThreatAdj Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
condSupport1 Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
condSolo
| Bool
condInMelee -> Bool
False
| Bool
heavilyDistressed -> Bool
True
| Bool
otherwise -> Bool
False
Bool -> Bool -> Bool
&& Bool
condCanFlee
actorFled :: ((ActorId, Actor), TgtAndPath) -> Bool
actorFled ((aid :: ActorId
aid, _), _) = ActorId -> Bool
recentlyFled ActorId
aid
actorHearning :: ((ActorId, Actor), TgtAndPath) -> m Bool
actorHearning (_, TgtAndPath{ tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TEnemyPos{} _ _
, tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Maybe AndPath
Nothing }) =
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
actorHearning (_, TgtAndPath{ tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TEnemyPos{} _ _
, tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Just AndPath{Int
pathLen :: AndPath -> Int
pathLen :: Int
pathLen} })
| Int
pathLen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 2 =
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
actorHearning ((_aid :: ActorId
_aid, b :: Actor
b), _) = do
let closeFoes :: [(ActorId, Actor)]
closeFoes = ((ActorId, Actor) -> Bool)
-> [(ActorId, Actor)] -> [(ActorId, Actor)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 3) (Int -> Bool)
-> ((ActorId, Actor) -> Int) -> (ActorId, Actor) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Point -> Point -> Int
chessDist (Actor -> Point
bpos Actor
b) (Point -> Int)
-> ((ActorId, Actor) -> Point) -> (ActorId, Actor) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Actor -> Point
bpos (Actor -> Point)
-> ((ActorId, Actor) -> Actor) -> (ActorId, Actor) -> Point
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ActorId, Actor) -> Actor
forall a b. (a, b) -> b
snd)
[(ActorId, Actor)]
foeAssocs
actorHears :: Bool
actorHears = ResDelta -> Bool
deltasHears (Actor -> ResDelta
bcalmDelta Actor
b)
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$! Bool
actorHears
Bool -> Bool -> Bool
&& [(ActorId, Actor)] -> Bool
forall a. [a] -> Bool
null [(ActorId, Actor)]
closeFoes
actorMeleeing :: ((ActorId, Actor), TgtAndPath) -> m Bool
actorMeleeing ((aid :: ActorId
aid, _), _) =
(State -> Bool) -> m Bool
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Bool) -> m Bool) -> (State -> Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ ActorMaxSkills -> ActorId -> State -> Bool
anyHarmfulFoeAdj ActorMaxSkills
actorMaxSkills ActorId
aid
(oursVulnerable :: [((ActorId, Actor), TgtAndPath)]
oursVulnerable, oursSafe :: [((ActorId, Actor), TgtAndPath)]
oursSafe) <- (((ActorId, Actor), TgtAndPath) -> m Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m ([a], [a])
partitionM ((ActorId, Actor), TgtAndPath) -> m Bool
actorVulnerable [((ActorId, Actor), TgtAndPath)]
oursTgt
let (oursFled :: [((ActorId, Actor), TgtAndPath)]
oursFled, oursNotFled :: [((ActorId, Actor), TgtAndPath)]
oursNotFled) = (((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
actorFled [((ActorId, Actor), TgtAndPath)]
oursSafe
(oursMeleeingRaw :: [((ActorId, Actor), TgtAndPath)]
oursMeleeingRaw, oursNotMeleeingRaw :: [((ActorId, Actor), TgtAndPath)]
oursNotMeleeingRaw) <-
(((ActorId, Actor), TgtAndPath) -> m Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m ([a], [a])
partitionM ((ActorId, Actor), TgtAndPath) -> m Bool
actorMeleeing [((ActorId, Actor), TgtAndPath)]
oursNotFled
let actorMeleeingCanDisplace :: ((ActorId, Actor), TgtAndPath) -> m Bool
actorMeleeingCanDisplace ( (aid :: ActorId
aid, sb :: Actor
sb)
, TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TEnemy target :: ActorId
target} ) = do
Actor
tb <- (State -> Actor) -> m Actor
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Actor) -> m Actor) -> (State -> Actor) -> m Actor
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> Actor
getActorBody ActorId
target
let actorMaxSk :: Skills
actorMaxSk = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
target
Bool
dEnemy <- (State -> Bool) -> m Bool
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Bool) -> m Bool) -> (State -> Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ ActorId -> ActorId -> Skills -> State -> Bool
dispEnemy ActorId
aid ActorId
target Skills
actorMaxSk
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$! Actor -> Actor -> Bool
checkAdjacent Actor
sb Actor
tb Bool -> Bool -> Bool
&& Bool
dEnemy
actorMeleeingCanDisplace _ = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
(oursMeleeingCanDisplace :: [((ActorId, Actor), TgtAndPath)]
oursMeleeingCanDisplace, oursMeleeing :: [((ActorId, Actor), TgtAndPath)]
oursMeleeing) <-
(((ActorId, Actor), TgtAndPath) -> m Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m ([a], [a])
partitionM ((ActorId, Actor), TgtAndPath) -> m Bool
actorMeleeingCanDisplace [((ActorId, Actor), TgtAndPath)]
oursMeleeingRaw
let adjStash :: ((ActorId, Actor), TgtAndPath) -> Bool
adjStash
( (_, b :: Actor
b)
, TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TStash{} lid :: LevelId
lid pos :: Point
pos} ) =
LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena
Bool -> Bool -> Bool
&& Point -> Point -> Bool
adjacent Point
pos (Actor -> Point
bpos Actor
b)
Bool -> Bool -> Bool
&& Maybe ActorId -> Bool
forall a. Maybe a -> Bool
isNothing (Point -> Level -> Maybe ActorId
posToBigLvl Point
pos Level
lvl)
adjStash _ = Bool
False
(oursAdjStash :: [((ActorId, Actor), TgtAndPath)]
oursAdjStash, oursNotMeleeing :: [((ActorId, Actor), TgtAndPath)]
oursNotMeleeing) =
(((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
adjStash [((ActorId, Actor), TgtAndPath)]
oursNotMeleeingRaw
(oursHearing :: [((ActorId, Actor), TgtAndPath)]
oursHearing, oursNotHearing :: [((ActorId, Actor), TgtAndPath)]
oursNotHearing) <- (((ActorId, Actor), TgtAndPath) -> m Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m ([a], [a])
partitionM ((ActorId, Actor), TgtAndPath) -> m Bool
actorHearning [((ActorId, Actor), TgtAndPath)]
oursNotMeleeing
let actorRanged :: ((ActorId, Actor), TgtAndPath) -> Bool
actorRanged ((aid :: ActorId
aid, body :: Actor
body), _) =
Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ ActorMaxSkills -> ActorId -> Actor -> Bool
actorCanMelee ActorMaxSkills
actorMaxSkills ActorId
aid Actor
body
targetTEnemy :: ((ActorId, Actor), TgtAndPath) -> Bool
targetTEnemy (_, TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TEnemy _}) = Bool
True
targetTEnemy (_, TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TEnemyPos{} lid :: LevelId
lid _}) =
LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena
targetTEnemy ((_, b :: Actor
b), TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TStash{} lid :: LevelId
lid pos :: Point
pos}) =
LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena Bool -> Bool -> Bool
&& Point
pos Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
/= Actor -> Point
bpos Actor
b
targetTEnemy _ = Bool
False
actorNoSupport :: ((ActorId, Actor), TgtAndPath) -> m Bool
actorNoSupport ((aid :: ActorId
aid, _), _) = do
[(Int, (ActorId, Actor))]
threatDistL <- (State -> [(Int, (ActorId, Actor))]) -> m [(Int, (ActorId, Actor))]
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> [(Int, (ActorId, Actor))])
-> m [(Int, (ActorId, Actor))])
-> (State -> [(Int, (ActorId, Actor))])
-> m [(Int, (ActorId, Actor))]
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)] -> ActorId -> State -> [(Int, (ActorId, Actor))]
meleeThreatDistList [(ActorId, Actor)]
foeAssocs ActorId
aid
Bool
condSupport2 <- [(ActorId, Actor)] -> Int -> ActorId -> m Bool
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)] -> Int -> ActorId -> m Bool
condSupport [(ActorId, Actor)]
friendAssocs 2 ActorId
aid
let condThreat :: Int -> Bool
condThreat n :: Int
n = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [(Int, (ActorId, Actor))] -> Bool
forall a. [a] -> Bool
null ([(Int, (ActorId, Actor))] -> Bool)
-> [(Int, (ActorId, Actor))] -> Bool
forall a b. (a -> b) -> a -> b
$ ((Int, (ActorId, Actor)) -> Bool)
-> [(Int, (ActorId, Actor))] -> [(Int, (ActorId, Actor))]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n) (Int -> Bool)
-> ((Int, (ActorId, Actor)) -> Int)
-> (Int, (ActorId, Actor))
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, (ActorId, Actor)) -> Int
forall a b. (a, b) -> a
fst) [(Int, (ActorId, Actor))]
threatDistL
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> Bool -> m Bool
forall a b. (a -> b) -> a -> b
$! Int -> Bool
condThreat 5 Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
condSupport2
(oursRanged :: [((ActorId, Actor), TgtAndPath)]
oursRanged, oursNotRanged :: [((ActorId, Actor), TgtAndPath)]
oursNotRanged) = (((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
actorRanged [((ActorId, Actor), TgtAndPath)]
oursNotHearing
(oursTEnemyAll :: [((ActorId, Actor), TgtAndPath)]
oursTEnemyAll, oursOther :: [((ActorId, Actor), TgtAndPath)]
oursOther) = (((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
targetTEnemy [((ActorId, Actor), TgtAndPath)]
oursNotRanged
notSwapReady :: ((ActorId, Actor), TgtAndPath)
-> ((ActorId, Actor), Maybe TgtAndPath) -> Bool
notSwapReady ((_, b :: Actor
b), TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TStash{} lid :: LevelId
lid pos :: Point
pos}) _ =
LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena Bool -> Bool -> Bool
&& Point
pos Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== Actor -> Point
bpos Actor
b
notSwapReady abt :: ((ActorId, Actor), TgtAndPath)
abt@((_, b :: Actor
b), _)
(ab2 :: (ActorId, Actor)
ab2, Just t2 :: TgtAndPath
t2@TgtAndPath{tapPath :: TgtAndPath -> Maybe AndPath
tapPath=
Just AndPath{pathList :: AndPath -> [Point]
pathList=q :: Point
q : _}}) =
let source :: Point
source = Actor -> Point
bpos Actor
b
tenemy :: Bool
tenemy = ((ActorId, Actor), TgtAndPath) -> Bool
targetTEnemy ((ActorId, Actor), TgtAndPath)
abt
tenemy2 :: Bool
tenemy2 = ((ActorId, Actor), TgtAndPath) -> Bool
targetTEnemy ((ActorId, Actor)
ab2, TgtAndPath
t2)
in Bool -> Bool
not (Point
q Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== Point
source
Bool -> Bool -> Bool
|| Bool
tenemy Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
tenemy2)
notSwapReady _ _ = Bool
True
targetBlocked :: ((ActorId, Actor), TgtAndPath) -> Bool
targetBlocked abt :: ((ActorId, Actor), TgtAndPath)
abt@((aid :: ActorId
aid, _), TgtAndPath{Maybe AndPath
tapPath :: Maybe AndPath
tapPath :: TgtAndPath -> Maybe AndPath
tapPath}) = case Maybe AndPath
tapPath of
Just AndPath{pathList :: AndPath -> [Point]
pathList= q :: Point
q : _} ->
(((ActorId, Actor), Maybe TgtAndPath) -> Bool)
-> [((ActorId, Actor), Maybe TgtAndPath)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\abt2 :: ((ActorId, Actor), Maybe TgtAndPath)
abt2@((aid2 :: ActorId
aid2, body2 :: Actor
body2), _) ->
ActorId
aid2 ActorId -> ActorId -> Bool
forall a. Eq a => a -> a -> Bool
/= ActorId
aid
Bool -> Bool -> Bool
&& Actor -> Point
bpos Actor
body2 Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== Point
q
Bool -> Bool -> Bool
&& ((ActorId, Actor), TgtAndPath)
-> ((ActorId, Actor), Maybe TgtAndPath) -> Bool
notSwapReady ((ActorId, Actor), TgtAndPath)
abt ((ActorId, Actor), Maybe TgtAndPath)
abt2)
[((ActorId, Actor), Maybe TgtAndPath)]
oursTgtRaw
_ -> Bool
False
(oursTEnemyBlocked :: [((ActorId, Actor), TgtAndPath)]
oursTEnemyBlocked, oursTEnemy :: [((ActorId, Actor), TgtAndPath)]
oursTEnemy) =
(((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
targetBlocked [((ActorId, Actor), TgtAndPath)]
oursTEnemyAll
(oursNoSupportRaw :: [((ActorId, Actor), TgtAndPath)]
oursNoSupportRaw, oursSupportRaw :: [((ActorId, Actor), TgtAndPath)]
oursSupportRaw) <-
if [((ActorId, Actor), TgtAndPath)] -> Int
forall a. [a] -> Int
length [((ActorId, Actor), TgtAndPath)]
oursTEnemy Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 2
then ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a. Monad m => a -> m a
return ([], [((ActorId, Actor), TgtAndPath)]
oursTEnemy)
else (((ActorId, Actor), TgtAndPath) -> m Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> m ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m ([a], [a])
partitionM ((ActorId, Actor), TgtAndPath) -> m Bool
actorNoSupport [((ActorId, Actor), TgtAndPath)]
oursTEnemy
let (oursNoSupport :: [((ActorId, Actor), TgtAndPath)]
oursNoSupport, oursSupport :: [((ActorId, Actor), TgtAndPath)]
oursSupport) =
if [((ActorId, Actor), TgtAndPath)] -> Int
forall a. [a] -> Int
length [((ActorId, Actor), TgtAndPath)]
oursSupportRaw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 1
then ([], [((ActorId, Actor), TgtAndPath)]
oursTEnemy)
else ([((ActorId, Actor), TgtAndPath)]
oursNoSupportRaw, [((ActorId, Actor), TgtAndPath)]
oursSupportRaw)
(oursBlocked :: [((ActorId, Actor), TgtAndPath)]
oursBlocked, oursPos :: [((ActorId, Actor), TgtAndPath)]
oursPos) =
(((ActorId, Actor), TgtAndPath) -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((ActorId, Actor), TgtAndPath) -> Bool
targetBlocked ([((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)]))
-> [((ActorId, Actor), TgtAndPath)]
-> ([((ActorId, Actor), TgtAndPath)],
[((ActorId, Actor), TgtAndPath)])
forall a b. (a -> b) -> a -> b
$ [((ActorId, Actor), TgtAndPath)]
oursRanged [((ActorId, Actor), TgtAndPath)]
-> [((ActorId, Actor), TgtAndPath)]
-> [((ActorId, Actor), TgtAndPath)]
forall a. [a] -> [a] -> [a]
++ [((ActorId, Actor), TgtAndPath)]
oursOther
guarding :: ((ActorId, Actor), Maybe TgtAndPath) -> Bool
guarding ((_, b :: Actor
b), Just TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TStash{} lid :: LevelId
lid pos :: Point
pos}) =
LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena Bool -> Bool -> Bool
&& Point
pos Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== Actor -> Point
bpos Actor
b
guarding _ = Bool
False
oursNotSleepingNorGuarding :: [((ActorId, Actor), Maybe TgtAndPath)]
oursNotSleepingNorGuarding = (((ActorId, Actor), Maybe TgtAndPath) -> Bool)
-> [((ActorId, Actor), Maybe TgtAndPath)]
-> [((ActorId, Actor), Maybe TgtAndPath)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> (((ActorId, Actor), Maybe TgtAndPath) -> Bool)
-> ((ActorId, Actor), Maybe TgtAndPath)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ActorId, Actor), Maybe TgtAndPath) -> Bool
guarding) [((ActorId, Actor), Maybe TgtAndPath)]
oursTgtRaw
overheadOurs :: ((ActorId, Actor), TgtAndPath) -> Int
overheadOurs :: ((ActorId, Actor), TgtAndPath) -> Int
overheadOurs (_, TgtAndPath{tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Maybe AndPath
Nothing}) = 100
overheadOurs ((_, b :: Actor
b), TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=TPoint TStash{} lid :: LevelId
lid pos :: Point
pos})
| LevelId
lid LevelId -> LevelId -> Bool
forall a. Eq a => a -> a -> Bool
== LevelId
arena Bool -> Bool -> Bool
&& Point
pos Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== Actor -> Point
bpos Actor
b = 200
overheadOurs abt :: ((ActorId, Actor), TgtAndPath)
abt@( (aid :: ActorId
aid, b :: Actor
b)
, TgtAndPath{tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Just AndPath{pathLen :: AndPath -> Int
pathLen=Int
d, ..}} ) =
let maxSpread :: Int
maxSpread = 3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [((ActorId, Actor), Maybe TgtAndPath)] -> Int
forall a. [a] -> Int
length [((ActorId, Actor), Maybe TgtAndPath)]
oursNotSleepingNorGuarding
lDist :: Point -> [Int]
lDist p :: Point
p = [ Point -> Point -> Int
chessDist (Actor -> Point
bpos Actor
b2) Point
p
| ((aid2 :: ActorId
aid2, b2 :: Actor
b2), _) <- [((ActorId, Actor), Maybe TgtAndPath)]
oursNotSleepingNorGuarding
, ActorId
aid2 ActorId -> ActorId -> Bool
forall a. Eq a => a -> a -> Bool
/= ActorId
aid ]
pDist :: Point -> Int
pDist p :: Point
p = let ld :: [Int]
ld = Point -> [Int]
lDist Point
p
in if [Int] -> Bool
forall a. [a] -> Bool
null [Int]
ld then 0 else [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Int]
ld
aidDist :: Int
aidDist = Point -> Int
pDist (Actor -> Point
bpos Actor
b)
diffDist :: Int
diffDist = Point -> Int
pDist Point
pathGoal Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
aidDist
sign :: Int
sign = if Int
diffDist Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 then -1 else 1
formationValue :: Int
formationValue =
Int
sign Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int -> Int
forall a. Num a => a -> a
abs Int
diffDist Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
maxSpread)
Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
aidDist Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
maxSpread) Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ (2 :: Int)
targetsEnemy :: Bool
targetsEnemy = ((ActorId, Actor), TgtAndPath) -> Bool
targetTEnemy ((ActorId, Actor), TgtAndPath)
abt
fightValue :: Int
fightValue = if Bool
targetsEnemy
then - Int64 -> Int
forall a. Enum a => a -> Int
fromEnum (Actor -> Int64
bhp Actor
b Int64 -> Int64 -> Int64
forall a. Integral a => a -> a -> a
`div` (10 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
oneM))
else 0
isLit :: Point -> Bool
isLit pos :: Point
pos = TileSpeedup -> ContentId TileKind -> Bool
Tile.isLit TileSpeedup
coTileSpeedup (Level
lvl Level -> Point -> ContentId TileKind
`at` Point
pos)
actorMaxSk :: Skills
actorMaxSk = ActorMaxSkills
actorMaxSkills ActorMaxSkills -> ActorId -> Skills
forall k a. Enum k => EnumMap k a -> k -> a
EM.! ActorId
aid
actorShines :: Bool
actorShines = Skill -> Skills -> Int
Ability.getSk Skill
Ability.SkShine Skills
actorMaxSk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
stepsIntoLight :: Bool
stepsIntoLight =
Bool -> Bool
not Bool
actorShines
Bool -> Bool -> Bool
&& Bool -> Bool
not (Point -> Bool
isLit (Point -> Bool) -> Point -> Bool
forall a b. (a -> b) -> a -> b
$ Actor -> Point
bpos Actor
b)
Bool -> Bool -> Bool
&& case [Point]
pathList of
[] -> Bool
False
q :: Point
q : _ -> Point -> Bool
isLit Point
q
in Int
formationValue Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 3
Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
fightValue
Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (case Int
d of
0 -> -400
1 | Bool -> Bool
not Bool
targetsEnemy -> -200
_ -> if Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 8 then Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 4 else 2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
d Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 10)
Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (if ActorId
aid ActorId -> ActorId -> Bool
forall a. Eq a => a -> a -> Bool
== ActorId
oldAid then 0 else 10)
Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (if Bool
stepsIntoLight then 30 else 0)
positiveOverhead :: ((ActorId, Actor), TgtAndPath) -> Int
positiveOverhead abt :: ((ActorId, Actor), TgtAndPath)
abt =
Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
maxBoundInt32 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 1 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ 200 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ((ActorId, Actor), TgtAndPath) -> Int
overheadOurs ((ActorId, Actor), TgtAndPath)
abt
candidates :: [[((ActorId, Actor), TgtAndPath)]]
candidates = [ [((ActorId, Actor), TgtAndPath)]
oursAdjStash
, [((ActorId, Actor), TgtAndPath)]
oursVulnerable
, [((ActorId, Actor), TgtAndPath)]
oursSupport
, [((ActorId, Actor), TgtAndPath)]
oursNoSupport
, [((ActorId, Actor), TgtAndPath)]
oursPos
, [((ActorId, Actor), TgtAndPath)]
oursFled
, [((ActorId, Actor), TgtAndPath)]
oursMeleeingCanDisplace
, [((ActorId, Actor), TgtAndPath)]
oursTEnemyBlocked
, [((ActorId, Actor), TgtAndPath)]
oursMeleeing
, [((ActorId, Actor), TgtAndPath)]
oursHearing
, [((ActorId, Actor), TgtAndPath)]
oursBlocked
]
case ([((ActorId, Actor), TgtAndPath)] -> Bool)
-> [[((ActorId, Actor), TgtAndPath)]]
-> [[((ActorId, Actor), TgtAndPath)]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ([((ActorId, Actor), TgtAndPath)] -> Bool)
-> [((ActorId, Actor), TgtAndPath)]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [((ActorId, Actor), TgtAndPath)] -> Bool
forall a. [a] -> Bool
null) [[((ActorId, Actor), TgtAndPath)]]
candidates of
l :: [((ActorId, Actor), TgtAndPath)]
l : _ -> do
let freq :: Frequency ((ActorId, Actor), TgtAndPath)
freq = Text
-> [(Int, ((ActorId, Actor), TgtAndPath))]
-> Frequency ((ActorId, Actor), TgtAndPath)
forall a. Text -> [(Int, a)] -> Frequency a
toFreq "candidates for AI leader"
([(Int, ((ActorId, Actor), TgtAndPath))]
-> Frequency ((ActorId, Actor), TgtAndPath))
-> [(Int, ((ActorId, Actor), TgtAndPath))]
-> Frequency ((ActorId, Actor), TgtAndPath)
forall a b. (a -> b) -> a -> b
$ (((ActorId, Actor), TgtAndPath)
-> (Int, ((ActorId, Actor), TgtAndPath)))
-> [((ActorId, Actor), TgtAndPath)]
-> [(Int, ((ActorId, Actor), TgtAndPath))]
forall a b. (a -> b) -> [a] -> [b]
map (((ActorId, Actor), TgtAndPath) -> Int
positiveOverhead (((ActorId, Actor), TgtAndPath) -> Int)
-> (((ActorId, Actor), TgtAndPath)
-> ((ActorId, Actor), TgtAndPath))
-> ((ActorId, Actor), TgtAndPath)
-> (Int, ((ActorId, Actor), TgtAndPath))
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((ActorId, Actor), TgtAndPath) -> ((ActorId, Actor), TgtAndPath)
forall a. a -> a
id) [((ActorId, Actor), TgtAndPath)]
l
((aid :: ActorId
aid, b :: Actor
b), _) <- Rnd ((ActorId, Actor), TgtAndPath)
-> m ((ActorId, Actor), TgtAndPath)
forall (m :: * -> *) a. MonadClient m => Rnd a -> m a
rndToAction (Rnd ((ActorId, Actor), TgtAndPath)
-> m ((ActorId, Actor), TgtAndPath))
-> Rnd ((ActorId, Actor), TgtAndPath)
-> m ((ActorId, Actor), TgtAndPath)
forall a b. (a -> b) -> a -> b
$ Frequency ((ActorId, Actor), TgtAndPath)
-> Rnd ((ActorId, Actor), TgtAndPath)
forall a. Show a => Frequency a -> Rnd a
frequency Frequency ((ActorId, Actor), TgtAndPath)
freq
State
s <- m State
forall (m :: * -> *). MonadStateRead m => m State
getState
(StateClient -> StateClient) -> m ()
forall (m :: * -> *).
MonadClient m =>
(StateClient -> StateClient) -> m ()
modifyClient ((StateClient -> StateClient) -> m ())
-> (StateClient -> StateClient) -> m ()
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> StateClient -> StateClient
updateLeader ActorId
aid State
s
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Player -> Doctrine
fdoctrine (Faction -> Player
gplayer Faction
fact)
Doctrine -> [Doctrine] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Doctrine
Ability.TFollow, Doctrine
Ability.TFollowNoItems]
Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
condInMelee) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
m (Maybe TgtAndPath) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Maybe TgtAndPath) -> m ()) -> m (Maybe TgtAndPath) -> m ()
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId
aid, Actor
b)
ActorId -> m ActorId
forall (m :: * -> *) a. Monad m => a -> m a
return ActorId
aid
_ -> ActorId -> m ActorId
forall (m :: * -> *) a. Monad m => a -> m a
return ActorId
oldAid
setTargetFromDoctrines :: MonadClient m
=> [(ActorId, Actor)] -> [(ActorId, Actor)] -> ActorId
-> m ()
setTargetFromDoctrines :: [(ActorId, Actor)] -> [(ActorId, Actor)] -> ActorId -> m ()
setTargetFromDoctrines foeAssocs :: [(ActorId, Actor)]
foeAssocs friendAssocs :: [(ActorId, Actor)]
friendAssocs oldAid :: ActorId
oldAid = do
Maybe ActorId
mleader <- (StateClient -> Maybe ActorId) -> m (Maybe ActorId)
forall (m :: * -> *) a.
MonadClientRead m =>
(StateClient -> a) -> m a
getsClient StateClient -> Maybe ActorId
sleader
let !_A :: ()
_A = Bool -> () -> ()
forall a. HasCallStack => Bool -> a -> a
assert (Maybe ActorId
mleader Maybe ActorId -> Maybe ActorId -> Bool
forall a. Eq a => a -> a -> Bool
/= ActorId -> Maybe ActorId
forall a. a -> Maybe a
Just ActorId
oldAid) ()
Actor
oldBody <- (State -> Actor) -> m Actor
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Actor) -> m Actor) -> (State -> Actor) -> m Actor
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> Actor
getActorBody ActorId
oldAid
Maybe TgtAndPath
moldTgt <- (StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath)
forall (m :: * -> *) a.
MonadClientRead m =>
(StateClient -> a) -> m a
getsClient ((StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath))
-> (StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath)
forall a b. (a -> b) -> a -> b
$ ActorId -> EnumMap ActorId TgtAndPath -> Maybe TgtAndPath
forall k a. Enum k => k -> EnumMap k a -> Maybe a
EM.lookup ActorId
oldAid (EnumMap ActorId TgtAndPath -> Maybe TgtAndPath)
-> (StateClient -> EnumMap ActorId TgtAndPath)
-> StateClient
-> Maybe TgtAndPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateClient -> EnumMap ActorId TgtAndPath
stargetD
let side :: FactionId
side = Actor -> FactionId
bfid Actor
oldBody
arena :: LevelId
arena = Actor -> LevelId
blid Actor
oldBody
Faction
fact <- (State -> Faction) -> m Faction
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Faction) -> m Faction)
-> (State -> Faction) -> m Faction
forall a b. (a -> b) -> a -> b
$ (EnumMap FactionId Faction -> FactionId -> Faction
forall k a. Enum k => EnumMap k a -> k -> a
EM.! FactionId
side) (EnumMap FactionId Faction -> Faction)
-> (State -> EnumMap FactionId Faction) -> State -> Faction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> EnumMap FactionId Faction
sfactionD
let explore :: m ()
explore = m (Maybe TgtAndPath) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Maybe TgtAndPath) -> m ()) -> m (Maybe TgtAndPath) -> m ()
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId
oldAid, Actor
oldBody)
setPath :: Maybe TgtAndPath -> m Bool
setPath mtgt :: Maybe TgtAndPath
mtgt = case (Maybe TgtAndPath
mtgt, Maybe TgtAndPath
moldTgt) of
(Nothing, _) -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
( Just TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=Target
leaderTapTgt},
Just TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=Target
oldTapTgt,tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Just oldTapPath :: AndPath
oldTapPath} )
| Target
leaderTapTgt Target -> Target -> Bool
forall a. Eq a => a -> a -> Bool
== Target
oldTapTgt
Bool -> Bool -> Bool
&& Actor -> Point
bpos Actor
oldBody Point -> Point -> Bool
forall a. Eq a => a -> a -> Bool
== AndPath -> Point
pathSource AndPath
oldTapPath -> do
m (Maybe TgtAndPath) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Maybe TgtAndPath) -> m ()) -> m (Maybe TgtAndPath) -> m ()
forall a b. (a -> b) -> a -> b
$ [(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
forall (m :: * -> *).
MonadClient m =>
[(ActorId, Actor)]
-> [(ActorId, Actor)] -> (ActorId, Actor) -> m (Maybe TgtAndPath)
refreshTarget [(ActorId, Actor)]
foeAssocs [(ActorId, Actor)]
friendAssocs (ActorId
oldAid, Actor
oldBody)
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
(Just TgtAndPath{tapTgt :: TgtAndPath -> Target
tapTgt=Target
leaderTapTgt}, _) -> do
TgtAndPath
tap <- ActorId -> Target -> m TgtAndPath
forall (m :: * -> *).
MonadClient m =>
ActorId -> Target -> m TgtAndPath
createPath ActorId
oldAid Target
leaderTapTgt
case TgtAndPath
tap of
TgtAndPath{tapPath :: TgtAndPath -> Maybe AndPath
tapPath=Maybe AndPath
Nothing} -> Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
_ -> do
(StateClient -> StateClient) -> m ()
forall (m :: * -> *).
MonadClient m =>
(StateClient -> StateClient) -> m ()
modifyClient ((StateClient -> StateClient) -> m ())
-> (StateClient -> StateClient) -> m ()
forall a b. (a -> b) -> a -> b
$ \cli :: StateClient
cli ->
StateClient
cli {stargetD :: EnumMap ActorId TgtAndPath
stargetD = ActorId
-> TgtAndPath
-> EnumMap ActorId TgtAndPath
-> EnumMap ActorId TgtAndPath
forall k a. Enum k => k -> a -> EnumMap k a -> EnumMap k a
EM.insert ActorId
oldAid TgtAndPath
tap (StateClient -> EnumMap ActorId TgtAndPath
stargetD StateClient
cli)}
Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
follow :: m ()
follow = case Maybe ActorId
mleader of
Nothing -> m ()
explore
_ | Actor -> Watchfulness
bwatch Actor
oldBody Watchfulness -> Watchfulness -> Bool
forall a. Eq a => a -> a -> Bool
== Watchfulness
WSleep ->
m ()
explore
Just leader :: ActorId
leader -> do
Bool
onLevel <- (State -> Bool) -> m Bool
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Bool) -> m Bool) -> (State -> Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ ActorId -> LevelId -> State -> Bool
memActor ActorId
leader LevelId
arena
Bool
condInMelee <- LevelId -> m Bool
forall (m :: * -> *). MonadClient m => LevelId -> m Bool
condInMeleeM LevelId
arena
if Bool -> Bool
not Bool
onLevel Bool -> Bool -> Bool
|| Bool
condInMelee then m ()
explore
else do
Maybe TgtAndPath
mtgt <- (StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath)
forall (m :: * -> *) a.
MonadClientRead m =>
(StateClient -> a) -> m a
getsClient ((StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath))
-> (StateClient -> Maybe TgtAndPath) -> m (Maybe TgtAndPath)
forall a b. (a -> b) -> a -> b
$ ActorId -> EnumMap ActorId TgtAndPath -> Maybe TgtAndPath
forall k a. Enum k => k -> EnumMap k a -> Maybe a
EM.lookup ActorId
leader (EnumMap ActorId TgtAndPath -> Maybe TgtAndPath)
-> (StateClient -> EnumMap ActorId TgtAndPath)
-> StateClient
-> Maybe TgtAndPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateClient -> EnumMap ActorId TgtAndPath
stargetD
Bool
tgtPathSet <- Maybe TgtAndPath -> m Bool
setPath Maybe TgtAndPath
mtgt
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
tgtPathSet (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
let nonEnemyPath :: Maybe TgtAndPath
nonEnemyPath = TgtAndPath -> Maybe TgtAndPath
forall a. a -> Maybe a
Just $WTgtAndPath :: Target -> Maybe AndPath -> TgtAndPath
TgtAndPath { tapTgt :: Target
tapTgt = ActorId -> Target
TNonEnemy ActorId
leader
, tapPath :: Maybe AndPath
tapPath = Maybe AndPath
forall a. Maybe a
Nothing }
Bool
nonEnemyPathSet <- Maybe TgtAndPath -> m Bool
setPath Maybe TgtAndPath
nonEnemyPath
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
nonEnemyPathSet
m ()
explore
case Player -> Doctrine
fdoctrine (Player -> Doctrine) -> Player -> Doctrine
forall a b. (a -> b) -> a -> b
$ Faction -> Player
gplayer Faction
fact of
Ability.TExplore -> m ()
explore
Ability.TFollow -> m ()
follow
Ability.TFollowNoItems -> m ()
follow
Ability.TMeleeAndRanged -> m ()
explore
Ability.TMeleeAdjacent -> m ()
explore
Ability.TBlock -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Ability.TRoam -> m ()
explore
Ability.TPatrol -> m ()
explore