module Dodge.Creature.YourControl ( yourControl, ) where import Control.Monad import Data.Foldable import qualified Data.Map.Strict as M import Data.Maybe import Dodge.Base.Coordinate import Dodge.Base.You import Dodge.Creature.Impulse.Movement import Dodge.Creature.Impulse.UseItem import Dodge.Data.World import Dodge.Hotkey import Dodge.InputFocus import Dodge.WASD import Geometry import LensHelp import qualified SDL -- | The AI equivalent for your control. yourControl :: Creature -> World -> World yourControl _ w | inInputFocus w = w | not intopinv = w & cWorld . lWorld . creatures . ix 0 %~ wasdWithAiming w | otherwise = w & cWorld . lWorld . creatures . ix 0 %~ (wasdWithAiming w . mouseActionsCr (w ^. input . mouseButtons)) & pressedMBEffectsTopInventory pkeys & handleHotkeys where pkeys = w ^. input . mouseButtons intopinv = fromMaybe False $ do subinv <- w ^? hud . hudElement . subInventory Just $ case subinv of NoSubInventory{} -> True _ -> False handleHotkeys :: World -> World handleHotkeys w | SDL.ButtonRight `M.member` _mouseButtons (_input w) = foldl' tryAssignHotkey w allHotkeys | otherwise = foldl' useHotKey w (M.intersection hotkeys (w ^. input . pressedKeys)) where hotkeys = M.mapKeys hotkeyToScancode $ w ^?! cWorld . lWorld . creatures . ix 0 . crHotkeys useHotKey :: World -> Int -> World useHotKey w invid = useItemHotkey 0 invid w allHotkeys :: [SDL.Scancode] allHotkeys = map hotkeyToScancode [minBound .. maxBound] hotkeyToScancode :: Hotkey -> SDL.Scancode hotkeyToScancode x = case x of HotkeyQ -> SDL.ScancodeQ HotkeyE -> SDL.ScancodeE Hotkey1 -> SDL.Scancode1 Hotkey2 -> SDL.Scancode2 Hotkey3 -> SDL.Scancode3 Hotkey4 -> SDL.Scancode4 Hotkey5 -> SDL.Scancode5 Hotkey6 -> SDL.Scancode6 Hotkey7 -> SDL.Scancode7 Hotkey8 -> SDL.Scancode8 Hotkey9 -> SDL.Scancode9 Hotkey0 -> SDL.Scancode0 scancodeToHotkey :: SDL.Scancode -> Hotkey scancodeToHotkey x = case x of SDL.ScancodeQ -> HotkeyQ SDL.ScancodeE -> HotkeyE SDL.Scancode1 -> Hotkey1 SDL.Scancode2 -> Hotkey2 SDL.Scancode3 -> Hotkey3 SDL.Scancode4 -> Hotkey4 SDL.Scancode5 -> Hotkey5 SDL.Scancode6 -> Hotkey6 SDL.Scancode7 -> Hotkey7 SDL.Scancode8 -> Hotkey8 SDL.Scancode9 -> Hotkey9 SDL.Scancode0 -> Hotkey0 _ -> undefined tryAssignHotkey :: World -> SDL.Scancode -> World tryAssignHotkey w sc = fromMaybe w $ do pt <- w ^? input . pressedKeys . ix sc guard (pt == InitialPress) cr <- w ^? cWorld . lWorld . creatures . ix 0 itid <- cr ^? crManipulation . manObject . imSelectedItem return $ w & cWorld . lWorld . creatures . ix 0 %~ assignHotkey itid (scancodeToHotkey sc) {- | The order of these MAY be important, in particular the setting of crMvAim within wasdMovement should probably be done first -} wasdWithAiming :: World -> Creature -> Creature wasdWithAiming w cr = wasdAim inp cam . wasdTwist $ wasdMovement inp cam speed cr where speed = _mvSpeed $ _crMvType cr inp = w ^. input cam = w ^. wCam wasdAim :: Input -> Camera -> Creature -> Creature wasdAim inp cam cr | SDL.ButtonRight `M.member` _mouseButtons inp = aimTurn mouseDir cr | otherwise = creatureTurnTowardDir (_crMvAim cr) 0.2 cr where mouseDir = argV $ mouseWorldPos inp cam - (cr ^. crPos) -- aim with the root item wasdTwist :: Creature -> Creature wasdTwist cr | _posture (_crStance cr) == Aiming = fromMaybe cr $ do itRef <- cr ^? crManipulation . manObject . imRootItem astance <- cr ^? crInv . ix itRef . itUse . heldAim . aimStance case (astance, cr ^. crTwist) of (TwoHandUnder, 0) -> return $ cr & crTwist .~ twistamount * pi & crDir -~ twistamount * pi (TwoHandOver, 0) -> return $ cr & crTwist .~ twistamount * pi & crDir -~ twistamount * pi _ -> Nothing | otherwise = cr & crDir +~ _crTwist cr & crTwist .~ 0 --remove twistk where twistamount = 1.6 wasdMovement :: Input -> Camera -> Float -> Creature -> Creature wasdMovement inp cam speed = theMovement . setMvAim where setMvAim = fromMaybe id $ do dir <- safeArgV movDir return $ crMvAim .~ (cam ^. camRot + dir) movDir = wasdDir inp movAbs = rotateV (cam ^. camRot) $ normalizeV movDir theMovement | movDir == V2 0 0 = id | otherwise = crMvAbsolute (speed *.* movAbs) aimTurn :: Float -> Creature -> Creature aimTurn a cr = creatureTurnTowardDir a (x * 0.2) cr where x = fromMaybe 1 $ do itRef <- cr ^? crManipulation . manObject . imRootItem cr ^? crInv . ix itRef . itUse . heldAim . aimTurnSpeed {- | Set posture according to mouse presses. (should you be aiming without a selected item?) -} mouseActionsCr :: M.Map SDL.MouseButton Int -> Creature -> Creature mouseActionsCr pkeys | SDL.ButtonRight `M.member` pkeys = crStance . posture .~ Aiming | otherwise = crStance . posture .~ AtEase -- where -- noaction = fromMaybe True $ do -- theaction <- cr ^? crManipulation . manObject . inInventory . iselAction -- return $ theaction == NoInvSelAction pressedMBEffectsTopInventory :: M.Map SDL.MouseButton Int -> World -> World pressedMBEffectsTopInventory pkeys w | isDown SDL.ButtonLeft && isDown SDL.ButtonRight && inTopInv = youhammerdown $ useRootItem 0 w | isDown SDL.ButtonLeft && inTopInv = youhammerdown $ useItemLeftClick (you w) w | isDown SDL.ButtonMiddle = w & wCam . camRot -~ rotation | otherwise = w where youhammerdown = set (cWorld . lWorld . creatures . ix 0 . crHammerPosition) HammerDown inTopInv = case w ^. hud . hudElement of DisplayInventory{_subInventory = NoSubInventory{}} -> True _ -> False isDown but = but `M.member` pkeys rotation = maybe 0 (angleBetween $ w ^. input . mousePos) (w ^. input . heldPos . at SDL.ButtonMiddle)