Allow for partial reloading

This commit is contained in:
2021-12-01 17:40:09 +00:00
parent d75ea2b252
commit 016a1faf80
21 changed files with 181 additions and 143 deletions
+15 -14
View File
@@ -145,27 +145,28 @@ performAction cr w ac = case ac of
NoAction -> ([],Nothing)
startReloadingWeapon :: Creature -> World -> Maybe World
startReloadingWeapon cr w =
let cid = _crID cr
it = _crInv cr IM.! _crInvSel cr
itRef = creatures . ix cid . crInv . ix (_crInvSel cr)
in case it of
Weapon {_itConsumption = LoadableAmmo {_wpMaxAmmo=maxA,_wpLoadedAmmo=lA ,_wpReloadState=rS,_wpReloadTime=rT}}
| lA < maxA && rS == 0 -> Just $ set ( itRef . itConsumption . wpLoadedAmmo) maxA
$ set ( itRef . itConsumption . wpReloadState) rT w
_ -> Nothing
startReloadingWeapon cr w = case it of
Weapon {_itConsumption = LoadableAmmo {_ammoMax=maxA,_ammoLoaded=lA ,_reloadState=rS,_reloadTime=rT}}
| lA < maxA && rS == Nothing' ->
Just -- $ set ( itRef . itConsumption . ammoLoaded) maxA
$ set ( itRef . itConsumption . reloadState) (Just' rT) w
_ -> Nothing
where
cid = _crID cr
it = _crInv cr IM.! _crInvSel cr
itRef = creatures . ix cid . crInv . ix (_crInvSel cr)
{- | Start reloading if clip is empty. -}
crAutoReload :: Creature -> Creature
crAutoReload cr = case cr ^? ptrItConsumption' . wpLoadedAmmo of
crAutoReload cr = case cr ^? ptrItConsumption' . ammoLoaded of
Just 0 | _posture (_crStance cr) /= Aiming
-> cr & ptrItConsumption . wpReloadState %~ (`fromMaybe` reloadT)
& ptrItConsumption . wpLoadedAmmo %~ (`fromMaybe` maxA)
-> 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' . wpReloadTime
maxA = cr ^? ptrItConsumption' . wpMaxAmmo
reloadT = cr ^? ptrItConsumption' . reloadTime
maxA = cr ^? ptrItConsumption' . ammoMax
{- | Teleport a creature to the mouse position -}
blinkAction
:: Creature
+1 -1
View File
@@ -122,7 +122,7 @@ reloadOverrideR
:: Creature
-> Reader World Creature
reloadOverrideR cr
| cr ^? crInv . ix (_crInvSel cr) . itConsumption . wpLoadedAmmo == Just 0
| cr ^? crInv . ix (_crInvSel cr) . itConsumption . ammoLoaded == Just 0
&& cr ^. crStance . posture == Aiming
= return $ cr & crActionPlan . crStrategy .~ StrategyActions Reload reloadActions
| otherwise = return cr
+21 -7
View File
@@ -45,7 +45,8 @@ stateUpdate :: CRUpdate -> CRUpdate
stateUpdate u cr w = case u (updateMovement cr) w of
(f, maybeCr) ->
( Endo $ invSideEff (fromMaybe cr maybeCr) . movementSideEff cr . deathEff . appEndo f
, fmap (stepReloading . doDamage . crAutoReload) $ crOrCorpse =<< maybeCr
--, fmap (stepReloading . doDamage . crAutoReload) $ crOrCorpse =<< maybeCr
, fmap (stepReloading . doDamage) $ crOrCorpse =<< maybeCr
)
where
crOrCorpse cr'
@@ -145,13 +146,16 @@ invSideEff cr w = weaponReloadSounds cr $ IM.foldrWithKey f w (_crInv cr)
weaponReloadSounds :: Creature -> World -> World
weaponReloadSounds cr w = case cr ^? crInv . ix (_crInvSel cr) . itConsumption of
Just am@LoadableAmmo{} -> case _wpReloadType am of
PassiveReload stype |_wpReloadTime am ==_wpReloadState am
Just am@LoadableAmmo{} -> case _reloadType am of
PassiveReload stype |Just' (_reloadTime am) == _reloadState am
-> soundContinue (CrReloadSound 0) (_crPos cr) stype Nothing w
PassiveReload _ -> w
ActiveReload | _wpReloadState am == 1 -> stopSoundFrom (CrReloadSound cid) w
ActiveReload | _wpReloadState am == 0 -> w
ActiveReload | _reloadState am == Just' 0 -> stopSoundFrom (CrReloadSound cid) w
ActiveReload | _reloadState am == Nothing' -> w
ActiveReload -> soundContinue (CrReloadSound cid) (_crPos cr) reloadS (Just 1) w
PartialActive{} | _reloadState am == Just' 0 -> stopSoundFrom (CrReloadSound cid) w
PartialActive{} | _reloadState am == Nothing' -> w
PartialActive{} -> soundContinue (CrReloadSound cid) (_crPos cr) reloadS (Just 1) w
Just ChargeableAmmo {} -> w
Just ItemItselfConsumable {} -> w
Nothing -> w
@@ -186,8 +190,18 @@ isFrictionless cr = case cr ^? crStance . carriage of
_ -> False
stepReloading :: Creature -> Creature
stepReloading cr = over (crInv . ix iSel . itConsumption . wpReloadState) decreaseToZero cr
stepReloading cr = case cr ^? crInv . ix isel . itConsumption . reloadState . _Just' of
Just 0 -> cr & crInv . ix isel . itConsumption . reloadState .~ Nothing'
& crInv . ix isel . itConsumption %~ doload
Just _ -> cr & crInv . ix isel . itConsumption . reloadState . _Just' %~ decreaseToZero
Nothing -> cr
where
iSel = _crInvSel cr
isel = _crInvSel cr
doload itcon = itcon & ammoLoaded %~ (min (_ammoMax itcon) . (+ amount))
where
amount = case _reloadType itcon of
PartialActive x -> x
_ -> _ammoMax itcon
+3 -3
View File
@@ -35,12 +35,12 @@ onBoth :: (a -> b -> c) -> (d -> a) -> (d -> b) -> d -> c
onBoth f g h x = f (g x) (h x)
crIsReloading :: (World, Creature) -> Bool
crIsReloading (_,cr) = case cr ^? crInv . ix (_crInvSel cr) . itConsumption . wpReloadState of
crIsReloading (_,cr) = case cr ^? crInv . ix (_crInvSel cr) . itConsumption . reloadState . _Just' of
Just t -> t > 0
_ -> False
crIsReloadingR :: Creature -> Reader World Bool
crIsReloadingR cr = return $ case cr ^? crInv . ix (_crInvSel cr) . itConsumption . wpReloadState of
crIsReloadingR cr = return $ case cr ^? crInv . ix (_crInvSel cr) . itConsumption . reloadState . _Just' of
Just t -> t > 0
_ -> False
@@ -114,7 +114,7 @@ crAwayFromPostR cr = return $ case find sentinelGoal $ _crGoal $ _crActionPlan c
sentinelGoal _ = False
crHasAmmo :: (World,Creature) -> Bool
crHasAmmo (_,cr) = maybe False (> 0) $ cr ^? crInv . ix (_crInvSel cr) . itConsumption . wpLoadedAmmo
crHasAmmo (_,cr) = maybe False (> 0) $ cr ^? crInv . ix (_crInvSel cr) . itConsumption . ammoLoaded
crCanShoot :: (World,Creature) -> Bool
crCanShoot p = crIsAiming p && crHasAmmo p