Extend inv item location information to include "attached" items

This commit is contained in:
2024-09-24 21:26:26 +01:00
parent fbb60bf358
commit fe00af0e73
17 changed files with 184 additions and 131 deletions
+8 -1
View File
@@ -47,8 +47,15 @@ tryPutItemInInv cid flit w = case maybeInvSlot of
where
it = _flIt flit
maybeInvSlot = checkInvSlotsYou it w
-- not sure if the following is necessary
updateItLocation invid w' = w' & cWorld . lWorld . itemLocations . ix (_itID it)
.~ InInv cid invid False False
.~ InInv
{_ilCrID = cid
, _ilInvID = invid
, _ilIsRoot = False
, _ilIsSelected = False
, _ilIsAttached = False
}
---- should select the item on the floor if no inventory space?
--createAndSelectItem :: Item -> World -> World
+26 -6
View File
@@ -5,6 +5,8 @@ module Dodge.Inventory.Location
, tryGetRootItemInvID
)
where
import qualified Data.IntSet as IS
import Dodge.Base.You
import Dodge.Data.SelectionList
import Dodge.Data.Item.Use.Consumption.LoadAction
@@ -15,6 +17,15 @@ import Dodge.Item.Grammar
import Data.Maybe
import Control.Applicative
-- assumes all item locations inside the items are correct
tryGetRootAttachedFromInvID :: Int -> IM.IntMap Item -> Maybe (Int, IS.IntSet)
tryGetRootAttachedFromInvID invid im = do
let imroots = invRootMap im
theroot = fromMaybe invid $ imroots ^? ix invid . _1 . _Just
t <- imroots ^? ix theroot . _2
return (theroot, foldMap (IS.singleton . (^?! _1 . itLocation . ilInvID)) t)
-- this assumes the creature inventory is well formed, specifically the
-- location ids
tryGetRootItemInvID :: Int -> Creature -> Maybe Int
@@ -22,8 +33,8 @@ tryGetRootItemInvID i cr = do
let adj = case invAdj' (_crInv cr) of
Left str -> error $ "tryToGetRootItemInvID: "++ str
Right x -> x
thetree <- adj ^? ix i -- to check that the index does point to SOME item in the inventory
thetree ^? _1 . _Just . _1 <|> Just i
theroot <- adj ^? ix i
theroot ^? _1 . _Just . _1 <|> Just i
updateRootItemID :: Creature -> Creature
updateRootItemID cr = fromMaybe cr $ do
@@ -44,8 +55,13 @@ crUpdateInvidLocations mo crid lw invid itm = lw
& itemLocations %~ IM.insert itid newloc
where
itid = itm ^. itID
newloc = InInv crid invid (Just invid == mo ^? inInventory . imSelectedItem)
(Just invid == mo ^? inInventory . imRootItem)
newloc = InInv
{ _ilCrID = crid
, _ilInvID = invid
, _ilIsRoot = Just invid == mo ^? inInventory . imSelectedItem
, _ilIsSelected = Just invid == mo ^? inInventory . imRootItem
, _ilIsAttached = invid `IS.member` (mo ^. inInventory . imAttachedItems)
}
-- this should be looked at, as it is sometimes used in functions that need not
-- concern the player creature
@@ -61,8 +77,12 @@ setInvPosFromSS w =
case i of
(-1) -> Just $ InInventory SortInventory
0 -> do
rootid <- tryGetRootItemInvID j (you w)
return $ InInventory $ SelectedItem j rootid
(rootid,aset) <- tryGetRootAttachedFromInvID j (you w ^. crInv)
return $ InInventory $ SelectedItem
{ _imSelectedItem = j
, _imRootItem = rootid
, _imAttachedItems = aset
}
1 -> Just SelNothing
2 -> Just $ InNearby SortNearby
3 -> Just $ InNearby $ SelCloseObject j