module Dodge.Creature.Impulse.UseItem ( useRootItem, useItemLeftClick, itemUseEffect, useItemHotkey, ) where import Dodge.SelectUse import Dodge.Item.Grammar import Dodge.Data.ComposedItem import Dodge.Data.DoubleTree import Control.Lens import Control.Monad --import Data.Foldable import Data.Maybe import Dodge.Cuse import Dodge.Data.World import Dodge.Euse import Dodge.HeldUse import Dodge.Hotkey import Dodge.Inventory import Dodge.Item.Location import Dodge.Luse import qualified IntMapHelp as IM useRootItem :: Int -> World -> World useRootItem crid w = fromMaybe w $ do cr <- w ^? cWorld . lWorld . creatures . ix crid itRef <- cr ^? crManipulation . manObject . imRootItem it <- invTrees (_crInv cr) ^? ix itRef return $ itemUseEffect cr it w & worldEventFlags . at InventoryChange ?~ () itemUseEffect :: Creature -> LabelDoubleTree ComposeLinkType Item -> World -> World itemUseEffect cr it w = case it ^. ldtValue . itUse of UseHeld{} -> heldEffect it cr w & pointerToItem itm . itUse . heldHammer .~ HammerDown UseHotkey{} -> doequipmentchange EquipUse{} -> doequipmentchange (ConsumeUse eff) -> useC eff (_ldtValue it) cr w CraftUse{} -> w AttachUse{} -> selectUse it cr w TargetingUse {} -> w AmmoMagUse{} -> w ScopeUse{} -> w BulletModUse{} -> w where itm = it ^. ldtValue doequipmentchange = fromMaybe w $ do guard (_crHammerPosition cr == HammerUp) invid <- itm ^? itLocation . ilInvID return $ toggleEquipmentAt invid cr w toggleEquipmentAt :: Int -> Creature -> World -> World toggleEquipmentAt invid cr w = case getEquipmentAllocation w of DoNotMoveEquipment -> w PutOnEquipment{_allocNewPos = newp} -> w & crpoint . crEquipment . at newp ?~ invid & crpoint . crInvEquipped . at invid ?~ newp & onequip itm cr & crpoint %~ assignNewHotkey invid MoveEquipment{_allocNewPos = newp, _allocOldPos = oldp} -> w & crpoint . crEquipment . at newp ?~ invid & crpoint . crEquipment . at oldp .~ Nothing & crpoint . crInvEquipped . at invid ?~ newp SwapEquipment{_allocNewPos = newp, _allocOldPos = oldp, _allocSwapID = sid} -> w & crpoint . crEquipment . at newp ?~ invid & crpoint . crEquipment . at oldp ?~ sid & crpoint . crInvEquipped . at invid ?~ newp & crpoint . crInvEquipped . at sid ?~ oldp ReplaceEquipment{_allocNewPos = newp, _allocRemoveID = rid} -> w & crpoint . crEquipment . at newp ?~ invid & crpoint . crInvEquipped . at invid ?~ newp & crpoint . crInvEquipped . at rid .~ Nothing & onremove (itmat rid) cr & onequip itm cr & crpoint %~ removeHotkey rid & crpoint %~ assignNewHotkey invid RemoveEquipment{_allocOldPos = oldp} -> w & crpoint . crEquipment . at oldp .~ Nothing & crpoint . crInvEquipped . at invid .~ Nothing & onremove itm cr & crpoint %~ removeHotkey invid where crpoint = cWorld . lWorld . creatures . ix (_crID cr) itmat i = _crInv cr IM.! i itm = itmat itRef itRef = cr ^?! crManipulation . manObject . imSelectedItem -- unsafe!! TODO change? onequip itm' = useE ((_eeOnEquip . _equipEffect . _itUse) itm') itm' onremove itm' = useE ((_eeOnRemove . _equipEffect . _itUse) itm') itm' -- need to make sure that the root item is not erroneously selected in the -- following useItemLeftClick :: Creature -> World -> World useItemLeftClick cr w = fromMaybe w $ do guard . not $ _crInvLock cr invid <- cr ^? crManipulation . manObject . imSelectedItem ituse <- cr ^? crInv . ix invid . itUse case ituse of UseHeld{} -> return w ConsumeUse{} -> return $ useItemLeftClick' cr w EquipUse{} -> return $ useItemLeftClick' cr w UseHotkey{} -> return $ useItemLeftClick' cr w AmmoMagUse{} -> return w _ -> Nothing useItemLeftClick' :: Creature -> World -> World --useItemLeftClick' cr' w = fromMaybe (f w) $ do useItemLeftClick' cr' w = fromMaybe w $ do cr <- w ^? cWorld . lWorld . creatures . ix (_crID cr') itRef <- cr ^? crManipulation . manObject . imSelectedItem it <- invTrees (_crInv cr) ^? ix itRef return $ itemUseEffect cr it w & worldEventFlags . at InventoryChange ?~ () -- & f -- where -- f = cWorld . lWorld . creatures . ix (_crID cr') . crHammerPosition .~ HammerDown useItemHotkey :: Int -> Int -> World -> World useItemHotkey crid invid w = fromMaybe w $ do cr <- w ^? cWorld . lWorld . creatures . ix crid itm <- cr ^? crInv . ix invid luse <- itm ^? itUse . leftUse return $ w & cWorld . lWorld . creatures . ix crid . crInv . ix invid . itUse . leftHammer .~ HammerDown & useL luse itm cr -- TODO determine itmShouldBeUsed with reference to config options