Files
loop/src/Dodge/Inventory/CheckSlots.hs
T

40 lines
1.1 KiB
Haskell

module Dodge.Inventory.CheckSlots where
import Control.Lens
import Data.Maybe
import Dodge.Base.You
import Dodge.Data.World
import Dodge.Inventory.ItemSpace
import qualified IntMapHelp as IM
{- | checks whether or not an item will fit in your inventory
if so return Just the next slot to be used
-}
checkInvSlotsYou :: Item -> World -> Maybe Int
checkInvSlotsYou it w
| crNumFreeSlots ycr >= ceiling (_itInvSize it) =
Just $ findItemSlot it inv
| otherwise = Nothing
where
ycr = you w
inv = _crInv ycr
-- Assumes that the item is singular.
-- Do not want to stack floor items for now
findItemSlot :: Item -> IM.IntMap Item -> Int
findItemSlot it inv = case it ^? itUse . useAmount of
Just _ ->
fromMaybe newslot $ IM.findIndex (\it' -> _itType it == _itType it') inv
_ -> newslot
where
newslot = maybe 0 ((+ 1) . fst) $ IM.lookupMax inv
crNumFreeSlots :: Creature -> Int
crNumFreeSlots cr = _crInvCapacity cr - invSize (_crInv cr)
crInvSize :: Creature -> Int
crInvSize = invSize . _crInv
invSize :: IM.IntMap Item -> Int
invSize = sum . fmap itSlotsTaken