Files
loop/src/Dodge/Reloading.hs
T

96 lines
4.3 KiB
Haskell

module Dodge.Reloading (
crCancelReloading,
crToggleReloading,
stepReloading,
tryStartLoading,
) where
import Dodge.Data.Item.Use.Consumption.LoadAction
import Control.Lens
import Dodge.Data.Creature
import Data.Maybe
crCancelReloading :: Creature -> Creature
crCancelReloading cr =
cr
& updateProgress
& crManipulation . manObject . inInventory . iselAction .~ NoInvSelAction
where
updateProgress = fromMaybe id $ do
InInventory (SelItem i _) <- cr ^? crManipulation . manObject
return $ crInv . ix i . itUse . heldConsumption . laSource . _InternalSource . iaProgress %~ const Nothing
stepReloading :: Creature -> Creature
stepReloading cr = case cr ^? crManipulation . manObject . inInventory of
Just (SelItem i (ReloadAction x la))
| x > 0 ->
cr & crManipulation . manObject . inInventory . iselAction . actionProgress -~ 1
| otherwise ->
cr
& crInv . ix i . itUse . heldConsumption . laSource . _InternalSource %~ doLoadAction la
& crInv . ix i . itUse . heldConsumption . laSource . _InternalSource %~ rotateActionProgress
& tryNextLoadAction
_ -> cr
tryNextLoadAction :: Creature -> Creature
tryNextLoadAction cr = case cr ^? crManipulation . manObject . inInventory of
Just (SelItem i _) -> case cr ^? crInv . ix i . itUse . heldConsumption . laSource . _InternalSource . iaProgress . _Just . ix 0 of
Nothing -> cr & crManipulation . manObject . inInventory . iselAction .~ NoInvSelAction
Just la ->
cr & crManipulation . manObject . inInventory . iselAction . actionProgress .~ _actionTime la
& crManipulation . manObject . inInventory . iselAction . reloadAction .~ la
_ -> cr
crToggleReloading :: Creature -> Creature
crToggleReloading cr = case cr ^? crManipulation . manObject . inInventory . iselAction of
Just ReloadAction{} -> cr & crManipulation . manObject . inInventory . iselAction .~ NoInvSelAction
_ -> cr & tryStartLoading
tryStartLoading :: Creature -> Creature
tryStartLoading cr = fromMaybe cr $ do
i <- cr ^? crManipulation . manObject . inInventory . ispItem
hc <- cr ^? crInv . ix i . itUse . heldConsumption . laSource . _InternalSource
return $ startLoading hc cr
startLoading :: InternalAmmo -> Creature -> Creature
startLoading ic cr = case ic ^? iaProgress . _Just . ix 0 of
Just la -> cr & startLoadingStep la
Nothing -> case ic ^? iaCycle of
Nothing -> cr
Just [] -> error "item has empty load cycle"
Just _ | _iaLoaded ic >= _iaMax ic -> cr
Just (la : las) -> fromMaybe (error "item loading error") $ do
i <- cr ^? crManipulation . manObject . inInventory . ispItem
return $ cr & startLoadingStep la
& crInv . ix i . itUse . heldConsumption . laSource . _InternalSource . iaProgress ?~ (la : las)
startLoadingStep :: LoadAction -> Creature -> Creature
startLoadingStep la cr = cr & crManipulation . manObject . inInventory . iselAction .~ ReloadAction (_actionTime la) la
rotateActionProgress :: InternalAmmo -> InternalAmmo
rotateActionProgress ic = case ic ^? iaProgress . _Just of
Just (_ : la : las) -> ic & iaProgress . _Just .~ (la : las)
_ | _iaLoaded ic < _iaMax ic -> ic & iaProgress ?~ _iaCycle ic
_ -> ic & iaProgress .~ Nothing
doLoadAction :: LoadAction -> InternalAmmo -> InternalAmmo
doLoadAction la ic = case la of
LoadEject{} -> ic & iaLoaded .~ 0 & iaPrimed .~ False
LoadInsert{} -> ic & iaLoaded .~ _iaMax ic
LoadAdd{_insertMax = x} ->
ic & iaLoaded +~ min x (_iaMax ic - _iaLoaded ic)
LoadPrime{} -> ic & iaPrimed .~ True
--{- | 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)