-- -- | -- Module : CopState -- Authors : Sean Seefried (http://www.cse.unsw.edu.au/~sseefried) -- Copyright : (c) 2005 -- License : GPL version 2 or later -- Created : 27 Jun 2005 -- module CopState where import Control.Concurrent.MVar import System.IO.Unsafe import Data.List import KnowledgeBase import Logging import CR import Syntax type Trust = Int data CopState = CopState { copTrust :: [(Name, Trust)] -- (names of cop, 0 <= trust <= 100) , otherLocs :: [(Name, [ (Location, WorldNum)])] , yourLocs :: [ (Location, WorldNum)] } state :: MVar CopState state = unsafePerformIO (newMVar CopState { copTrust = niceError "Not initialised", otherLocs = niceError "Not initialised", yourLocs = niceError "Not initialised" }) {-# NOINLINE state #-} initState :: CR () initState = do kb <- getKB let copNames = kb_copNames kb ownName = kb_ownName kb otherNames = copNames \\ [ownName] setState $ CopState { copTrust = zip otherNames (repeat 0), otherLocs = [ (name, []) | name <- otherNames], yourLocs = [] } getState :: CR CopState getState = liftIO $ readMVar state setState :: CopState -> CR () setState s = liftIO $ modifyMVar_ state $ (const $ return s) addLoc :: Eq a => (a, b) -> [(a,[b])] -> [(a,[b])] addLoc _ [] = [] addLoc p@(a,b) (p'@(a', bs'): rest) | a == a' = (a, b : bs') : rest | otherwise = p' : addLoc p rest addToLocs :: Eq a => (a, [b]) -> [(a,[b])] -> [(a,[b])] addToLocs _ [] = [] addToLocs p@(a,bs) (p'@(a', bs'): rest) | a == a' = (a, bs ++ bs') : rest | otherwise = p' : addToLocs p rest