Allow for stacking of items and combining sensibly

This commit is contained in:
2021-12-03 17:18:06 +00:00
parent b2a9192fe4
commit b41e3e3637
24 changed files with 166 additions and 159 deletions
+16 -4
View File
@@ -1,21 +1,33 @@
module Dodge.Inventory.CheckSlots where
import Dodge.Data
import Dodge.Base.You
import Dodge.Inventory.ItemSpace
import qualified IntMapHelp as IM
import Data.Maybe
--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
| 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 _itConsumption it of
ItemItselfConsumable _
-> fromMaybe newslot $ IM.findIndex (\it' -> _itCombineType it == _itCombineType it') inv
_ -> newslot
where
newslot = maybe 0 ((+1) . fst) $ IM.lookupMax inv
crNumFreeSlots :: Creature -> Int
crNumFreeSlots cr = _crInvCapacity cr - invSize (_crInv cr)
@@ -23,4 +35,4 @@ crInvSize :: Creature -> Int
crInvSize = invSize . _crInv
invSize :: IM.IntMap Item -> Int
invSize = sum . fmap _itInvSize
invSize = sum . fmap itSlotsTaken
-25
View File
@@ -1,25 +0,0 @@
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