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 icTryStartReloading :: Item -> Maybe Item icTryStartReloading it = do am <- it ^? itConsumption amloaded <- am ^? ammoLoaded ammax <- am ^? ammoBaseMax rstate <- am ^? reloadState if amloaded < ammax && rstate == Nothing' then Just $ it & itConsumption . reloadState .~ Just' (_reloadTime am) & if _reloadType am == ActiveClear then itConsumption . ammoLoaded .~ 0 else id else Nothing crStopReloading :: Creature -> Creature crStopReloading cr = cr & crInv . ix (_crInvSel cr) . itConsumption . reloadState .~ Nothing' stepReloading :: Creature -> Creature stepReloading cr = case cr ^? crInv . ix isel . itConsumption . reloadState . _Just' of Just 0 -> cr & crInv . ix isel %~ doload Just _ -> cr & crInv . ix isel . itConsumption . reloadState . _Just' %~ decreaseToZero Nothing -> cr where isel = _crInvSel cr doload it = case _reloadType itcon of ActivePartial x | x + _ammoLoaded itcon < itMaxAmmo -> it & itConsumption . ammoLoaded %~ (+ x) & itConsumption . reloadState .~ Just' (_reloadTime itcon) | otherwise -> it & itConsumption . ammoLoaded .~ itMaxAmmo & itConsumption . reloadState .~ Nothing' _ -> it & itConsumption . ammoLoaded .~ itMaxAmmo & itConsumption . reloadState .~ Nothing' where itcon = _itConsumption it itMaxAmmo = _ammoBaseMax 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)