Fix inventory swapping equipment bug

This commit is contained in:
2022-05-22 15:50:25 +01:00
parent fd40da2d4a
commit b83392d441
8 changed files with 45 additions and 18 deletions
+21 -6
View File
@@ -50,16 +50,25 @@ rmInvItem cid invid w = case w ^? creatures . ix cid . crInv . ix invid . itCons
Just x | x > 1 -> w & creatures . ix cid . crInv . ix invid . itConsumption . itAmount %~ subtract 1
_ -> w & creatures . ix cid . crInv %~ f
& creatures . ix cid . crInvSel %~ g
& creatures . ix cid . crLeftInvSel . _Just %~ g
& creatures . ix cid . crLeftInvSel %~ g'
& removeAnySlotEquipment
& creatures . ix cid . crInvEquipped %~ IM.delete invid
& creatures . ix cid . crInvEquipped %~ IM.mapKeys g
& creatures . ix cid . crInvEquipped %~ IM.mapKeys g
-- TODO check whether this can be mapKeysMonotonic
where
removeAnySlotEquipment = case w ^? creatures . ix cid . crInvEquipped . ix invid of
Just epos -> creatures . ix cid . crEquipment . at epos .~ Nothing
Nothing -> id
maxk = fmap fst $ IM.lookupMax $ _crInv $ _creatures w IM.! cid
f inv = let (xs,ys) = IM.split invid inv
in xs `IM.union` IM.mapKeys (subtract 1) ys
g x | x > invid || Just x == maxk = max 0 $ x - 1
| otherwise = x
g' Nothing = Nothing
g' (Just x)
| x == invid = Nothing
| x > invid || Just x == maxk = Just $ max 0 $ x - 1
| otherwise = Just x
rmSelectedInvItem :: Int -> World -> World
rmSelectedInvItem cid w = rmInvItem cid (_crInvSel (_creatures w IM.! cid)) w
@@ -184,17 +193,23 @@ changeSwapInvSel k w
| otherwise = w & creatures . ix (_yourID w) . crInvSel .~ ico'
& closeObjects %~ swapIndices (i - n) (ico' - n)
where
updatecreature = ( crInv %~ IM.swapKeys (i `mod` n) swapi )
updatecreature = ( crInv %~ IM.safeSwapKeys (i `mod` n) swapi )
. (crLeftInvSel . _Just %~ updateLeftInvSel)
. (crInvSel %~ (`mod` n) . subtract k)
. (crInvEquipped %~ IM.swapKeys i swapi)
. (crInvEquipped %~ IM.safeSwapKeys i swapi)
. swapSite i swapi
. swapSite swapi i
swapSite a b = case cr ^? crInvEquipped . ix a of
Just epos -> crEquipment . ix epos .~ b
Nothing -> id
cr = you w
swapi = (i - k) `mod` n
updateLeftInvSel li | i == li = swapi
| swapi == li = i
| otherwise = li
i = _crInvSel $ you w
i = _crInvSel cr
ico' = ( (i - (k+n)) `mod` numCO ) + n
n = length $ _crInv $ _creatures w IM.! _yourID w
n = length $ _crInv cr
numCO = length $ _closeObjects w
--swapIntSetKeys :: Int -> Int -> IS.IntSet -> IS.IntSet