module Dodge.Creature.SpreadGunCrit
    ( spreadGunCrit 
    ) where
import Dodge.Data
import Dodge.Default
import Dodge.Creature.Picture
import Dodge.Creature.Update
import Dodge.Creature.Test
import Dodge.Creature.Action
import Dodge.Creature.Volition
import Dodge.Creature.Strategy
import Dodge.Creature.ReaderUpdate
import Dodge.Creature.Perception
--import Dodge.Creature.State.Data
import Dodge.Item.Weapon.BulletGuns
import Dodge.Item.Consumable
import Geometry
import Picture
--import Dodge.RandomHelp

--import Data.Maybe
import qualified Data.IntMap.Strict as IM
import Control.Lens

spreadGunCrit :: Creature
spreadGunCrit = defaultCreature
    { _crPict = basicCrPict
    , _crUpdate = defaultImpulsive
        [performActions
        ,watchUpdateStrat
          [ (crHasTargetLOS, \_ _ -> StrategyActions (ShootAt 0) 
              [DoActionIf (const $ not . crIsAiming) drawWeapon
              ,DoActionThen 
                  (DoActionWhile crHasTargetLOS $ ArbitraryAction chooseMovement)
                  (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)
        ]
    , _crInv = IM.fromList [(0,bangStick 6),(1,medkit 100)]
    , _crRad = 10
    , _crHP = 500
    }
    & crSkin . skinUpper .~ light4 red

chooseMovement :: Creature -> World -> Action
chooseMovement cr w 
    | dist cpos p < 30 && safeAngleVV (p -.- cpos) (unitVectorAtAngle (_crDir cr)) > pi
        = DoImpulses [UseItem, MoveForward (-3)]
    | d < 30 = DoImpulses [UseItem,TurnToward p 0.06]
    | d < 60 = DoImpulses [UseItem,TurnToward p 0.06,MoveForward 3]
    | d < 100 = DoImpulses [TurnToward p 0.06, MoveForward 3 ]
    | d < 200 = DoImpulses [TurnToward p (0.06 + 0.002 *(d-100)), MoveForward 3 ]
    | otherwise
          = DoImpulses [TurnToward p 0.26, MoveForward 3]
  where
    d = dist cpos p
    cpos = _crPos cr
    tcr = _creatures w IM.! 0
    p = _crPos tcr
