Start moving hotkeys out from creatures into world

This commit is contained in:
2024-12-19 15:16:47 +00:00
parent 11b5ef74c5
commit 4526119fcd
11 changed files with 131 additions and 108 deletions
+29 -19
View File
@@ -11,36 +11,46 @@ import Data.Maybe
import Dodge.Data.World
import Control.Monad
assignNewHotkey :: Int -> Creature -> Creature
assignNewHotkey invid cr = assignHotkey invid (newHotkey cr) cr
assignNewHotkey :: Int -> LWorld -> LWorld
assignNewHotkey invid w = assignHotkey invid (newHotkey w) w
newHotkey :: Creature -> Hotkey
newHotkey cr = fromMaybe maxBound $ find (not . (`M.member` usedhks)) [minBound .. maxBound]
--newHotkey :: Creature -> Hotkey
--newHotkey cr = fromMaybe maxBound $ find (not . (`M.member` usedhks)) [minBound .. maxBound]
-- where
-- usedhks = cr ^?! crHotkeys
newHotkey :: LWorld -> Hotkey
newHotkey lw = fromMaybe maxBound $ find (not . (`M.member` usedhks)) [minBound .. maxBound]
where
usedhks = cr ^?! crHotkeys
usedhks = lw ^. hotkeys
-- this will not remove the hotkey from its old slot, assumes there is a hotkey
-- to swap with instead
-- requires there actually is equipment in the slot, too
assignHotkey :: Int -> Hotkey -> Creature -> Creature
assignHotkey invid hk cr = fromMaybe cr $ do
assignHotkey :: Int -> Hotkey -> LWorld -> LWorld
assignHotkey invid hk lw = fromMaybe lw $ do
--guard (invid `IM.member` (cr ^. crInvEquipped))
guard $ isJust $ cr ^? crInv . ix invid . itLocation . ilEquipPosition . _Just
_ <- cr ^? crInv . ix invid . itUse . leftUse
return $ (setHotkey invid hk . moveOldHotkey invid hk) cr
guard $ isJust $ lw ^? creatures . ix 0 . crInv . ix invid . itLocation . ilEquipPosition . _Just
_ <- lw ^? creatures . ix 0 . crInv . ix invid . itUse . leftUse
return $ (setHotkey invid hk . moveOldHotkey invid hk) lw
moveOldHotkey :: Int -> Hotkey -> Creature -> Creature
--moveOldHotkey :: Int -> Hotkey -> Creature -> Creature
--moveOldHotkey invid hk w = fromMaybe w $ do
-- oldhk <- w ^? crInvHotkeys . ix invid
-- oldid <- w ^? crHotkeys . ix hk
-- return $ w & setHotkey oldid oldhk
moveOldHotkey :: Int -> Hotkey -> LWorld -> LWorld
moveOldHotkey invid hk w = fromMaybe w $ do
oldhk <- w ^? crInvHotkeys . ix invid
oldid <- w ^? crHotkeys . ix hk
oldhk <- w ^? creatures . ix 0 . crInvHotkeys . ix invid
oldid <- w ^? hotkeys . ix hk
return $ w & setHotkey oldid oldhk
setHotkey :: Int -> Hotkey -> Creature -> Creature
setHotkey i hk = (crInvHotkeys . at i ?~ hk) . (crHotkeys . at hk ?~ i)
setHotkey :: Int -> Hotkey -> LWorld -> LWorld
setHotkey i hk = (creatures . ix 0 . crInvHotkeys . at i ?~ hk) . (hotkeys . at hk ?~ i)
removeHotkey :: Int -> Creature -> Creature
removeHotkey invid cr = cr & crInvHotkeys . at invid .~ Nothing
removeHotkey :: Int -> LWorld -> LWorld
removeHotkey invid w = w & creatures . ix 0 . crInvHotkeys . at invid .~ Nothing
& fromMaybe id (do
hk <- cr ^? crInvHotkeys . ix invid
return (crHotkeys . at hk .~ Nothing)
hk <- w ^? creatures . ix 0 . crInvHotkeys . ix invid
return (hotkeys . at hk .~ Nothing)
)