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
+10 -3
View File
@@ -3,9 +3,11 @@ module IntMapHelp
, newKey
, insertNewKey
, insertWithNewKeys
, swapKeys
, unsafeSwapKeys
, safeSwapKeys
, findIndex
) where
import LensHelp
--import Data.List hiding (foldr,insert)
import Data.IntMap.Strict
{- | Find a key value one higher than any key in the map, or zero if the map is
@@ -21,8 +23,13 @@ insertNewKey x m = case lookupMax m of
insertWithNewKeys :: [a] -> IntMap a -> IntMap a
insertWithNewKeys xs m = union m $ fromList $ zip [newKey m..] xs
{- | Swaps two keys. Unsafe: assumes both keys exist. -}
swapKeys :: Int -> Int -> IntMap a -> IntMap a
swapKeys i j m = insert i (m ! j) $ insert j (m ! i) m
unsafeSwapKeys :: Int -> Int -> IntMap a -> IntMap a
unsafeSwapKeys i j m = insert i (m ! j) $ insert j (m ! i) m
safeSwapKeys :: Int -> Int -> IntMap a -> IntMap a
safeSwapKeys i j m = m
& at i .~ (m ^? ix j)
& at j .~ (m ^? ix i)
-- ideally this should short circuit, I am not sure whether it does or not
-- though