module Dodge.Creature.LauncherCrit
    ( launcherCrit
    )
    where
import Dodge.Data
import Dodge.Default
import Dodge.Creature.Picture
import Dodge.Creature.Test
import Dodge.Creature.Volition
import Dodge.Creature.Strategy
import Dodge.Creature.ReaderUpdate
import Dodge.Creature.Perception
import Dodge.Creature.State
import Dodge.Item.Weapon.Launcher
import Dodge.Creature.Impulse
import Dodge.Creature.Action
import Geometry.Data
import Picture

import qualified Data.IntMap.Strict as IM
import Control.Lens
import Control.Monad.Reader

launcherCrit :: Creature
launcherCrit = defaultCreature
    { _crPict = basicCrPict red
    , _crUpdate = stateUpdate $ impulsiveAIR $
        performActionsR
        >=> watchUpdateStratR
            [ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) [retreatFire])
            , (crAwayFromPost, goToPostStrat)
            ]
        >=> perceptionUpdate [0]
        >=> doStrategyActionsR
        >=> reloadOverrideR
        >=> targetYouWhenCognizantR
        >=> overrideInternalRRR
            (\cr -> (&&) <$> crHasTargetR cr <*> crStratConMatchesR (GetTo (V2 0 0)) cr)
            (pure . (crActionPlan . crStrategy .~ WatchAndWait))
    , _crActionPlan = ActionPlan
        { _crImpulse    = []
        , _crAction     = []
        , _crStrategy   = StrategyActions WatchAndWait [StartSentinelPost]
        , _crGoal       = []
        }
    , _crInv = IM.fromList [(0,launcher)]
    , _crInvSel = 0
    , _crRad = 10
    , _crState = defaultState
    , _crHP = 300
    }

retreatFire :: Action
retreatFire = 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 $ \tcr -> TurnToward (_crPos tcr) (pi/16) ] )
    `DoActionThen`
    20
    `WaitThen`
    holsterWeapon
    `DoActionThen`
    ImpulsesList ( replicate 15 [ MoveForward 3 ] )
    `DoActionThen`
    DoImpulses [ChangeStrategy WatchAndWait]
    
    
