diff --git a/src/Dodge/Inventory/CheckSlots.hs b/src/Dodge/Inventory/CheckSlots.hs new file mode 100644 index 000000000..0454bfc32 --- /dev/null +++ b/src/Dodge/Inventory/CheckSlots.hs @@ -0,0 +1,26 @@ +module Dodge.Inventory.CheckSlots where +import Dodge.Data +import Dodge.Base.You + +--import Control.Lens +import qualified Data.IntMap.Strict 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 >= _itInvSize it + = Just $ maybe 0 ((+1) . fst) $ IM.lookupMax inv + | otherwise = Nothing + where + ycr = you w + inv = _crInv ycr + +crNumFreeSlots :: Creature -> Int +crNumFreeSlots cr = _crInvCapacity cr - invSize (_crInv cr) + +crInvSize :: Creature -> Int +crInvSize = invSize . _crInv + +invSize :: IM.IntMap Item -> Int +invSize = sum . fmap _itInvSize diff --git a/src/Dodge/Inventory/PickUp.hs b/src/Dodge/Inventory/PickUp.hs new file mode 100644 index 000000000..53d68b8d3 --- /dev/null +++ b/src/Dodge/Inventory/PickUp.hs @@ -0,0 +1,25 @@ +module Dodge.Inventory.PickUp where +import Dodge.Data +import Dodge.Inventory.CheckSlots + +import Control.Lens +import qualified Data.IntMap.Strict as IM + +putItemInInvID :: Int -> Int -> World -> World +putItemInInvID cid flid w = putItemInInv cid (_floorItems w IM.! flid) w + + +{- | Pick up a specific item. -} +putItemInInv :: Int -> FloorItem -> World -> World +putItemInInv cid flit w = case maybeInvSlot of + Nothing -> w + Just i -> w + & updateItLocation i + & floorItems %~ IM.delete (_flItID flit) + & creatures . ix cid . crInv %~ IM.insertWith (const $ itAmount +~ 1) i it + where + it = _flIt flit + maybeInvSlot = checkInvSlotsYou it w + updateItLocation invid w' = case _itID it of + Nothing -> w' + Just j -> w' & itemPositions . ix j .~ InInv 0 invid diff --git a/src/Dodge/Item/Instance.hs b/src/Dodge/Item/Instance.hs new file mode 100644 index 000000000..a36bcd4a6 --- /dev/null +++ b/src/Dodge/Item/Instance.hs @@ -0,0 +1,6 @@ +module Dodge.Item.Instance + ( module Dodge.Item.Weapon + , module Dodge.Item.Craftable + ) where +import Dodge.Item.Weapon +import Dodge.Item.Craftable