From a168a335b92f560f82fc38faf20d3a73489ece92 Mon Sep 17 00:00:00 2001 From: justin Date: Fri, 3 Dec 2021 17:18:39 +0000 Subject: [PATCH] Add file --- src/Dodge/Inventory/Add.hs | 44 ++++++++++++++++++++++++++++++++ src/Dodge/Inventory/ItemSpace.hs | 9 +++++++ src/Multiset.hs | 17 ++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/Dodge/Inventory/Add.hs create mode 100644 src/Dodge/Inventory/ItemSpace.hs create mode 100644 src/Multiset.hs diff --git a/src/Dodge/Inventory/Add.hs b/src/Dodge/Inventory/Add.hs new file mode 100644 index 000000000..017d72953 --- /dev/null +++ b/src/Dodge/Inventory/Add.hs @@ -0,0 +1,44 @@ +module Dodge.Inventory.Add where +import Dodge.Data +import Dodge.Inventory.CheckSlots + +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 invsel it = IM.insertWith (const $ itConsumption . itAmount' +~ 1) invsel it + +{- | 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 diff --git a/src/Dodge/Inventory/ItemSpace.hs b/src/Dodge/Inventory/ItemSpace.hs new file mode 100644 index 000000000..798daef0c --- /dev/null +++ b/src/Dodge/Inventory/ItemSpace.hs @@ -0,0 +1,9 @@ +module Dodge.Inventory.ItemSpace where +import Dodge.Data + +import Control.Lens + +itSlotsTaken :: Item -> Int +itSlotsTaken it = case it ^? itConsumption . itAmount' of + Nothing -> ceiling (_itInvSize it) + Just i -> ceiling (_itInvSize it * fromIntegral i) diff --git a/src/Multiset.hs b/src/Multiset.hs new file mode 100644 index 000000000..418c53def --- /dev/null +++ b/src/Multiset.hs @@ -0,0 +1,17 @@ +{-# LANGUAGE TupleSections #-} +module Multiset where +import qualified Data.Map.Strict as M +import Control.Monad + +powlistN :: Int -> [a] ->[[a]] +powlistN _ [] = [[]] +powlistN n (x:xs) + | n <=0 = [[]] + | otherwise = ((x:) <$> powlistN (n-1) xs) ++ powlistN n xs + +-- exponential, so don't use it on long lists +powlist :: [a] -> [[a]] +powlist = filterM (const [True,False]) + +toMultiset :: Ord a => [a] -> M.Map a Int +toMultiset = foldr (uncurry $ M.insertWith (+)) M.empty . map (,1)