module Dodge.Inventory.Add where import Control.Lens import Data.Maybe import Dodge.Base.You import Dodge.Combine.Module import Dodge.Data.World import Dodge.FloorItem import Dodge.Inventory.CheckSlots import qualified IntMapHelp as IM putItemInInvID :: Int -> Int -> World -> (Maybe Int, World) putItemInInvID cid flid w = fromMaybe (Nothing, w) $ do --(i, w') <- tryPutItemInInv cid (_floorItems (_cWorld w) IM.! flid) w (i, w') <- tryPutItemInInv cid (w ^?! cWorld . lWorld . floorItems . ix flid) w return (Just i, w') putItemInInvSlot :: Int -> Item -> IM.IntMap Item -> IM.IntMap Item putItemInInvSlot = IM.insertWith (const $ itUse . useAmount +~ 1) -- | Pick up a specific item. tryPutItemInInv :: Int -> FloorItem -> World -> Maybe (Int, World) tryPutItemInInv cid flit w = case maybeInvSlot of Nothing -> Nothing Just i -> Just ( i , w & updateItLocation i & cWorld . lWorld . floorItems %~ IM.delete (_flItID flit) & cWorld . lWorld . creatures . ix cid . crInv %~ putItemInInvSlot i it ) where it = _flIt flit maybeInvSlot = checkInvSlotsYou it w updateItLocation invid w' = w' & cWorld . lWorld . itemLocations . ix (_itID it) .~ InInv cid invid createAndSelectItem :: Item -> World -> World createAndSelectItem itm w = case createPutItem itm w of (Just i, w') -> w' & cWorld . lWorld . creatures . ix 0 . crManipulation .~ Manipulator (SelItem i NoInvSelAction) (Nothing, w') -> w' createPutItem :: Item -> World -> (Maybe Int, World) createPutItem it w = uncurry (putItemInInvID 0) $ copyItemToFloorID (_crPos $ you w) (applyModules it) w