Partially working change to selected items

This commit is contained in:
2023-02-07 12:07:13 +00:00
parent 5f7d662454
commit c354949ca9
27 changed files with 386 additions and 288 deletions
+30 -22
View File
@@ -4,6 +4,7 @@ module Dodge.Creature.Impulse.UseItem (
itemEffect,
) where
import Control.Monad
import Data.Foldable
import Control.Lens
import qualified Data.Map.Strict as M
@@ -22,7 +23,8 @@ import qualified SDL
useItem :: Creature -> World -> World
useItem cr' w = fromMaybe (f w) $ do
cr <- w ^? cWorld . lWorld . creatures . ix (_crID cr')
it <- cr ^? crInv . ix (crSel cr)
itRef <- cr ^? crInvSel . isel . ispItem
it <- cr ^? crInv . ix itRef
return $ itemEffect cr it w
where
f = cWorld . lWorld . creatures . ix (_crID cr') . crHammerPosition .~ HammerDown
@@ -34,9 +36,10 @@ itemEffect cr it w = case it ^. itUse of
LeftUse{} -> doequipmentchange
EquipUse{} -> doequipmentchange
-- ConsumeUse will cause problems if the item is not selected
(ConsumeUse eff _) -> setuhamdown $ hammerTest $ useC eff it cr . rmInvItem (_crID cr) (crSel cr)
(ConsumeUse eff _) -> setuhamdown $ hammerTest $ useC eff it cr . rmInvItem (_crID cr) itRef
CraftUse{} -> setuhamdown w
where
itRef = cr ^?! crInvSel . isel . ispItem -- unsafe!! TODO change
hammerTest f = case _crHammerPosition cr of
HammerUp -> f w
_ -> w & setuhamdown
@@ -44,7 +47,7 @@ itemEffect cr it w = case it ^. itUse of
doequipmentchange =
setuhamdown $
hammerTest
( toggleEquipmentAt (_rbOptions w) (crSel cr) cr
( toggleEquipmentAt (_rbOptions w) itRef cr
. activateEquipmentAt (_rbOptions w) cr
)
@@ -105,29 +108,34 @@ toggleEquipmentAt rbops invid cr w = case rbops ^? opAllocateEquipment of
where
crpoint = cWorld . lWorld . creatures . ix (_crID cr)
itmat i = _crInv cr IM.! i
itm = itmat (crSel cr)
itm = itmat itRef
itRef = cr ^?! crInvSel . isel . ispItem -- unsafe!! TODO change
onequip itm' = useE ((_eeOnEquip . _equipEffect . _itUse) itm') itm'
onremove itm' = useE ((_eeOnRemove . _equipEffect . _itUse) itm') itm'
useLeftItem :: Int -> World -> World
useLeftItem cid w
| _crInvLock cr = w
| itmIsConsumable = useItem cr w -- I believe this ONLY sets equipment options
| itmIsEquipable = useItem cr w -- I believe this ONLY sets equipment options
| otherwise = fromMaybe w $ do
invid <- cr ^. crLeftInvSel . lisMPos
itm <- cr ^? crInv . ix invid
f <- cr ^? crInv . ix invid . itUse . leftUse
return
. (runIdentity . pointerToItemLocation (_itLocation itm) (return . (itUse . leftHammer .~ HammerDown)))
. useL f itm cr
$ w
useLeftItem cid w = fromMaybe w $ do
cr <- w ^? cWorld . lWorld . creatures . ix cid
guard . not $ _crInvLock cr
let mitRef = cr ^? crInvSel . isel . ispItem
itmIsConsumable = isJust $ do
itRef <- mitRef
cr ^? crInv . ix itRef . itUse . cUse
itmIsEquipable = isJust $ do
itRef <- mitRef
guard . not $ cr ^. crLeftInvSel . lisMPos /= Just itRef
cr ^? crInv . ix itRef . itUse . equipEffect . eeUse
if itmIsConsumable || itmIsEquipable
then return $ useItem cr w -- I believe this ONLY sets equipment options
else do
invid <- cr ^. crLeftInvSel . lisMPos
itm <- cr ^? crInv . ix invid
f <- cr ^? crInv . ix invid . itUse . leftUse
return
. (runIdentity . pointerToItemLocation (_itLocation itm) (return . (itUse . leftHammer .~ HammerDown)))
. useL f itm cr
$ w
where
cr = w ^?! cWorld . lWorld . creatures . ix cid-- _creatures (_cWorld w) IM.! cid
itmIsConsumable =
isJust (cr ^? crInv . ix (crSel cr) . itUse . cUse)
itmIsEquipable =
isJust (cr ^? crInv . ix (crSel cr) . itUse . equipEffect . eeUse)
&& cr ^. crLeftInvSel . lisMPos /= Just (crSel cr)
-- TODO determine itmShouldBeUsed with reference to config options