Implement/improve null/un/binary logic gates

This commit is contained in:
2025-01-08 18:13:20 +00:00
parent d436db2847
commit ef751cbd21
9 changed files with 246 additions and 183 deletions
+43 -5
View File
@@ -8,6 +8,7 @@ module Dodge.Inventory.SelectionList (
getItemValue,
) where
import Numeric
import Dodge.Inventory.Path
import Control.Applicative
import ShortShow
@@ -86,13 +87,34 @@ itemExternalValue itm w cr
| ITEMSCAN <- itm ^. itType
, Just ExamineInventory <- w ^? hud . hudElement . subInventory
= Just (Right "ON")
-- , Just x <- itm ^? itScroll . itsInt
-- , Just invid <- itm ^? itLocation . ilInvID
-- , Just i <- getInventoryPath x ip invid cr
-- , Just itm' <- cr ^? crInv . ix i
-- = getItemValue itm' w cr
| BINGATE <- itm ^. itType = do
invid <- itm ^? itLocation . ilInvID
litm <- cr ^? crInv . ix (invid -2)
ritm <- cr ^? crInv . ix (invid -1)
x <- itm ^? itScroll . itsRangeInt
l <- getItemValue litm w cr ^? _Just . _Left
r <- getItemValue ritm w cr ^? _Just . _Left
Just . Left $ bgateCalc x l r
| UNIGATE <- itm ^. itType = do
invid <- itm ^? itLocation . ilInvID
itm' <- cr ^? crInv . ix (invid -1)
x <- itm ^? itScroll . itsRangeInt
y <- getItemValue itm' w cr ^? _Just . _Left
Just . Left $ ugateCalc x y
| otherwise = mempty
ugateCalc :: Int -> Int -> Int
ugateCalc x y = (x `div` ((2::Int) ^ ((f y) ::Int))::Int) `mod` 2
where
f i | i > 0 = 1
| otherwise = 0
bgateCalc :: Int -> Int -> Int -> Int
bgateCalc x l r = (x `div` ((2::Int) ^ ((2::Int)*f l + f r))) `mod` 2
where
f i | i > 0 = 1
| otherwise = 0
itemScrollDisplay :: Item -> Maybe String
itemScrollDisplay itm
| HELD ALTERIFLE <- itm ^. itType = Just $
@@ -100,12 +122,28 @@ itemScrollDisplay itm
Just 0 -> "L"
_ -> "R"
| UseScope OpticScope{_opticZoom = x} <- itm ^. itUse = Just $ shortShow x
| UNIGATE <- itm ^. itType = do
i <- itm ^? itScroll . itsRangeInt
return $ toBinary 2 i
| BINGATE <- itm ^. itType = do
i <- itm ^? itScroll . itsRangeInt
return $ toBinary 4 i
| ItemScrollInt i <- itm ^. itScroll = Just $ show i
| ItemScrollIntRange _ i <- itm ^. itScroll = Just $ show i
| otherwise = Nothing
toBinary :: Int -> Int -> String
toBinary x = leftPad x '0' . ($ []) . showIntAtBase 2 f
where
f 0 = '0'
f _ = '1'
getItemValue :: Item -> World -> Creature -> Maybe (Either Int String)
getItemValue itm w cr = itemInternalValue itm <|> itemExternalValue itm w cr
<|> itemScrollValue itm
itemScrollValue :: Item -> Maybe (Either Int b)
itemScrollValue itm = Left <$> itm ^? itScroll . failing itsInt itsRangeInt
--itemDisplayPad :: [Char] -> String -> [Char]
--itemDisplayPad ls rs