module Dodge.Reloading ( stepReloading , startReloadingWeapon , crStopReloading ) where import Dodge.Data import Dodge.Base --import qualified Data.IntMap.Strict as IM --import Data.Maybe import Control.Lens startReloadingWeapon :: Creature -> World -> Maybe World startReloadingWeapon cr = (creatures . ix (_crID cr) . crInv . ix (_crInvSel cr)) icTryStartReloading -- TODO clean this up, rethink datatypes icTryStartReloading :: Item -> Maybe Item icTryStartReloading it = do am <- it ^? itConsumption amloaded <- am ^? laLoaded ammax <- am ^? laMax rstate <- am ^? laReloadState if amloaded < ammax && rstate == Nothing' then Just $ it & itConsumption . laReloadState .~ Just' (_laReloadTime am) & if _laReloadType am == ActiveClear then itConsumption . laLoaded .~ 0 else id else Nothing crStopReloading :: Creature -> Creature crStopReloading cr = cr & crInv . ix (_crInvSel cr) . itConsumption . laReloadState .~ Nothing' -- ugly TODO cleanup stepReloading :: Creature -> Creature stepReloading cr = case cr ^? crInv . ix isel . itConsumption . laReloadState . _Just' of Just 0 -> cr & crInv . ix isel %~ doload Just _ -> cr & crInv . ix isel . itConsumption . laReloadState . _Just' %~ decreaseToZero Nothing -> cr where isel = _crInvSel cr doload it = case _laReloadType itcon of ActivePartial x | x + _laLoaded itcon < itMaxAmmo -> it & itConsumption . laLoaded %~ (+ x) & itConsumption . laReloadState .~ Just' (_laReloadTime itcon) | otherwise -> it & itConsumption . laLoaded .~ itMaxAmmo & itConsumption . laReloadState .~ Nothing' _ -> it & itConsumption . laLoaded .~ itMaxAmmo & itConsumption . laReloadState .~ Nothing' where itcon = _itConsumption it itMaxAmmo = _laMax itcon --{- | Start reloading if clip is empty. -} --crAutoReload :: Creature -> Creature --crAutoReload cr = case cr ^? ptrItConsumption' . ammoLoaded of -- Just 0 | _posture (_crStance cr) /= Aiming -- -> cr & ptrItConsumption . reloadState .~ strictify reloadT -- & ptrItConsumption . ammoLoaded %~ (`fromMaybe` maxA) -- _ -> cr -- where -- ptrItConsumption = crInv . ix (_crInvSel cr) . itConsumption -- ptrItConsumption' = crInv . ix (_crInvSel cr) . itConsumption -- reloadT = cr ^? ptrItConsumption' . reloadTime -- maxA = fmap itMaxAmmo $ cr ^? crInv . ix (_crInvSel cr)