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

51 lines
1.7 KiB
Haskell

module Dodge.Inventory.Add where
import Dodge.Data
import Dodge.Inventory.CheckSlots
import Dodge.FloorItem
import Dodge.Combine.Module
import Dodge.Base.You
import Data.Maybe
import Control.Lens
import qualified Data.IntMap.Strict as IM
putItemInInvID :: Int -> Int -> World -> World
putItemInInvID cid flid w = fromMaybe w
$ tryPutItemInInv cid (_floorItems w IM.! flid) w
putItemInInvSlot :: Int -> Item -> IM.IntMap Item -> IM.IntMap Item
putItemInInvSlot = IM.insertWith (const $ itConsumption . icAmount +~ 1)
{- | Pick up a specific item. -}
tryPutItemInInv :: Int -> FloorItem -> World -> Maybe World
tryPutItemInInv cid flit w = case maybeInvSlot of
Nothing -> Nothing
Just i -> Just $ w
& updateItLocation i
& floorItems %~ IM.delete (_flItID flit)
& creatures . ix cid . crInv %~ putItemInInvSlot 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 cid invid
--{- | 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 cid invid
createPutItem :: Item -> World -> World
createPutItem it w = uncurry (putItemInInvID (_yourID w))
$ copyItemToFloorID (_crPos $ you w) (applyModules it) w