Move towards a non-stacking inventory

This commit is contained in:
2024-09-08 20:13:43 +01:00
parent 86795f1a10
commit 3dcf8de3c3
5 changed files with 118 additions and 55 deletions
+19 -1
View File
@@ -33,16 +33,34 @@ invertInventory =
(\k it -> ((_iyBase $ _itType it, itStackAmount it, k) :))
[]
invertInventoryToMap :: IM.IntMap Item -> M.Map ItemBaseType [Int]
invertInventoryToMap =
IM.foldrWithKey
(\k it -> (M.insertWith (++) (_iyBase $ _itType it) [k]))
mempty
splitItAmounts :: [(ItemBaseType, ItAmount, Int)] -> [((ItAmount, ItemBaseType), (ItAmount, Int))]
splitItAmounts = concatMap f
where
f (x, n, y) = [((i, x), (i, y)) | i <- [1 .. n]]
flatSplitItAmounts :: [(ItemBaseType, ItAmount, Int)] -> [((ItAmount, ItemBaseType), [Int])]
flatSplitItAmounts = concatMap f
where
f (x, n, y) = [((i, x), replicate (fromIntegral i) y) | i <- [1 .. n]]
lookupItems :: IM.IntMap Item -> [([(ItAmount, Int)], Item)]
lookupItems = flip multiLookupTrieI combinationsTrie . sortOn fst . splitItAmounts . invertInventory
flatLookupItems :: IM.IntMap Item -> [([[Int]],Item)]
flatLookupItems = flip multiLookupTrieI combinationsTrie . sortOn fst . flatSplitItAmounts
. invertInventory
combineItemListYouX :: World -> [([Int], Item)]
combineItemListYouX = map (first $ concatMap g) . lookupItems . yourInv
combineItemListYouX = map (first $ concat) . flatLookupItems . yourInv
combineItemListYouX' :: World -> [([Int], Item)]
combineItemListYouX' = map (first $ concatMap g) . lookupItems . yourInv
where
g (amount, i) = replicate (_getItAmount amount) i
+7
View File
@@ -2,9 +2,11 @@
module Dodge.Combine.Combinations
( itemCombinations
, flatItemCombinations
, bulletWeapons
) where
import Data.Bifunctor
import Dodge.Item.Ammo
import Dodge.Data.Item
import Dodge.Item
@@ -44,6 +46,11 @@ magazineCombinations =
po xs it = (map o xs, it)
o = (1,)
flatItemCombinations :: [([ItemBaseType], Item)]
flatItemCombinations = map (over _1 (concatMap f)) itemCombinations
where
f (x,it) = replicate (_getItAmount x) it
itemCombinations :: [([(ItAmount, ItemBaseType)], Item)]
itemCombinations =
watchCombinations ++
+7
View File
@@ -7,6 +7,13 @@ import Dodge.Data.Item
import SimpleTrie
import Data.Foldable
flatCombinationsTrie :: Trie ItemBaseType Item
{-# INLINE flatCombinationsTrie #-}
flatCombinationsTrie = foldl'
(flip $ uncurry insertInTrie . first sort)
emptyTrie
flatItemCombinations
combinationsTrie :: Trie (ItAmount, ItemBaseType) Item
{-# INLINE combinationsTrie #-}
combinationsTrie =