about summary refs log tree commit diff
path: root/users/grfn/xanthous/src/Xanthous/Game/Memo.hs
blob: 154063b5dde2f50bf525cf4454a6df55dc08d75a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE TemplateHaskell #-}
--------------------------------------------------------------------------------
-- | Memoized versions of calculations
--------------------------------------------------------------------------------
module Xanthous.Game.Memo
  ( MemoState
  , emptyMemoState
  , clear
    -- ** Memo lenses
  , characterVisiblePositions

    -- * Memoized values
  , Memoized(UnMemoized)
  , memoizeWith
  , getMemoized
  , runMemoized
  ) where
--------------------------------------------------------------------------------
import Xanthous.Prelude
--------------------------------------------------------------------------------
import Data.Aeson (ToJSON, FromJSON)
import Data.Aeson.Generic.DerivingVia
import Test.QuickCheck (CoArbitrary, Function, Arbitrary)
--------------------------------------------------------------------------------
import Xanthous.Data (Position)
import Xanthous.Data.Memo
import Xanthous.Util.QuickCheck (GenericArbitrary(GenericArbitrary))
--------------------------------------------------------------------------------

-- | Memoized calculations on the game state
data MemoState = MemoState
  { -- | Memoized version of 'Xanthous.Game.Lenses.characterVisiblePositions',
    -- memoized with the position of the character
    _characterVisiblePositions :: Memoized Position (Set Position)
  }
  deriving stock (Show, Eq, Generic)
  deriving anyclass (NFData, CoArbitrary, Function)
  deriving Arbitrary via GenericArbitrary MemoState
  deriving (ToJSON, FromJSON)
       via WithOptions '[ FieldLabelModifier '[Drop 1] ]
           MemoState
makeLenses ''MemoState

emptyMemoState :: MemoState
emptyMemoState = MemoState { _characterVisiblePositions = UnMemoized }
{-# INLINE emptyMemoState #-}

clear :: ASetter' MemoState (Memoized key val) -> MemoState -> MemoState
clear = flip set UnMemoized
{-# INLINE clear #-}

{-# ANN module ("Hlint: ignore Use newtype instead of data" :: String) #-}