Files
loop/src/Dodge/Item/Location.hs
T

53 lines
1.7 KiB
Haskell

module Dodge.Item.Location (
pointerToItemID,
pointerToItemLocation,
pointerToItem,
pointerYourSelectedItem,
pointerYourRootItem,
) where
import NewInt
import Control.Lens
import Data.Maybe
import Dodge.Data.World
pointerToItemLocation ::
Applicative f =>
ItemLocation ->
(Item -> f Item) ->
World ->
f World
pointerToItemLocation InInv {_ilCrID = cid, _ilInvID = invid}
= cWorld . lWorld . creatures . ix cid . crInv . ix invid
pointerToItemLocation (OnFloor flid) = cWorld . lWorld . floorItems . unNIntMap . ix (_unNInt flid) . flIt
pointerToItemLocation _ = const pure
pointerYourSelectedItem :: Applicative a => (Item -> a Item) -> World -> a World
pointerYourSelectedItem f w = fromMaybe (pure w) $ do
itinvid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . imSelectedItem
Just $ pointerToItemLocation (InInv 0 itinvid True True True Nothing) f w
-- note the ilIsRoot/Selected/Attached booleans are irrelevant
pointerYourRootItem :: Applicative a => (Item -> a Item) -> World -> a World
pointerYourRootItem f w = fromMaybe (pure w) $ do
itinvid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . imRootSelectedItem
Just $ pointerToItemLocation (InInv 0 itinvid True True True Nothing) f w
pointerToItem ::
Applicative f =>
Item ->
(Item -> f Item) ->
World ->
f World
pointerToItem = pointerToItemLocation . _itLocation
pointerToItemID ::
Applicative f =>
NewInt ItmInt ->
(Item -> f Item) ->
World ->
f World
pointerToItemID itid f w = fromMaybe (pure w) $ do
itloc <- w ^? cWorld . lWorld . itemLocations . ix (_unNInt itid)
return $ pointerToItemLocation itloc f w