module Dodge.Item.Location ( pointerToItemID, pointerToItemLocation, getItem, pointerToItem, pointerYourSelectedItem, pointerYourRootItem, ) where import NewInt import Control.Lens import Data.Maybe import Dodge.Data.World getItem :: Int -> World -> Maybe Item getItem itid w = do itpos <- w ^? cWorld . lWorld . itemLocations . ix itid case itpos of OnFloor flitid -> w ^? cWorld . lWorld . floorItems . unNIntMap . ix (_unNInt flitid) . flIt InInv {_ilCrID = cid, _ilInvID = invid} -> w ^? cWorld . lWorld . creatures . ix cid . crInv . ix invid OnTurret mcid -> w ^? cWorld . lWorld . machines . ix mcid . mcType . _McTurret . tuWeapon InVoid -> Nothing 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) 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) 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