module Game.LambdaHack.Server.State
( StateServer(..), ActorTime, ActorPushedBy
, emptyStateServer, updateActorTime, lookupActorTime, ageActor
) where
import Prelude ()
import Game.LambdaHack.Core.Prelude
import Data.Binary
import qualified Data.EnumMap.Strict as EM
import qualified Data.EnumSet as ES
import qualified Data.HashMap.Strict as HM
import qualified System.Random as R
import Game.LambdaHack.Common.Analytics
import Game.LambdaHack.Common.Perception
import Game.LambdaHack.Common.State
import Game.LambdaHack.Common.Time
import Game.LambdaHack.Common.Types
import Game.LambdaHack.Server.Fov
import Game.LambdaHack.Server.ItemRev
import Game.LambdaHack.Server.ServerOptions
data StateServer = StateServer
{ sactorTime :: ActorTime
, strajTime :: ActorTime
, strajPushedBy :: ActorPushedBy
, sfactionAn :: FactionAnalytics
, sactorAn :: ActorAnalytics
, sgenerationAn :: GenerationAnalytics
, sactorStasis :: ES.EnumSet ActorId
, sdiscoKindRev :: DiscoveryKindRev
, suniqueSet :: UniqueSet
, sitemRev :: ItemRev
, sflavour :: FlavourMap
, sacounter :: ActorId
, sicounter :: ItemId
, snumSpawned :: EM.EnumMap LevelId Int
, sundo :: ()
, sclientStates :: EM.EnumMap FactionId State
, sperFid :: PerFid
, sperValidFid :: PerValidFid
, sperCacheFid :: PerCacheFid
, sfovLucidLid :: FovLucidLid
, sfovClearLid :: FovClearLid
, sfovLitLid :: FovLitLid
, sarenas :: [LevelId]
, svalidArenas :: Bool
, srandom :: R.StdGen
, srngs :: RNGs
, sbreakLoop :: Bool
, sbreakASAP :: Bool
, swriteSave :: Bool
, soptions :: ServerOptions
, soptionsNxt :: ServerOptions
}
deriving Show
type ActorTime =
EM.EnumMap FactionId (EM.EnumMap LevelId (EM.EnumMap ActorId Time))
type ActorPushedBy = EM.EnumMap ActorId ActorId
emptyStateServer :: StateServer
emptyStateServer =
StateServer
{ sactorTime = EM.empty
, strajTime = EM.empty
, strajPushedBy = EM.empty
, sfactionAn = EM.empty
, sactorAn = EM.empty
, sgenerationAn = EM.fromAscList $ zip [minBound..maxBound] (repeat EM.empty)
, sactorStasis = ES.empty
, sdiscoKindRev = emptyDiscoveryKindRev
, suniqueSet = ES.empty
, sitemRev = HM.empty
, sflavour = emptyFlavourMap
, sacounter = toEnum 0
, sicounter = toEnum 0
, snumSpawned = EM.empty
, sundo = ()
, sclientStates = EM.empty
, sperFid = EM.empty
, sperValidFid = EM.empty
, sperCacheFid = EM.empty
, sfovLucidLid = EM.empty
, sfovClearLid = EM.empty
, sfovLitLid = EM.empty
, sarenas = []
, svalidArenas = False
, srandom = R.mkStdGen 42
, srngs = RNGs { dungeonRandomGenerator = Nothing
, startingRandomGenerator = Nothing }
, sbreakLoop = False
, sbreakASAP = False
, swriteSave = False
, soptions = defServerOptions
, soptionsNxt = defServerOptions
}
updateActorTime :: FactionId -> LevelId -> ActorId -> Time -> ActorTime
-> ActorTime
updateActorTime !fid !lid !aid !time =
EM.adjust (EM.adjust (EM.insert aid time) lid) fid
lookupActorTime :: FactionId -> LevelId -> ActorId -> ActorTime
-> Maybe Time
lookupActorTime !fid !lid !aid !atime = do
m1 <- EM.lookup fid atime
m2 <- EM.lookup lid m1
EM.lookup aid m2
ageActor :: FactionId -> LevelId -> ActorId -> Delta Time -> ActorTime
-> ActorTime
ageActor !fid !lid !aid !delta =
EM.adjust (EM.adjust (EM.adjust (`timeShift` delta) aid) lid) fid
instance Binary StateServer where
put StateServer{..} = do
put sactorTime
put strajTime
put strajPushedBy
put sfactionAn
put sactorAn
put sgenerationAn
put sactorStasis
put sdiscoKindRev
put suniqueSet
put sitemRev
put sflavour
put sacounter
put sicounter
put snumSpawned
put sclientStates
put (show srandom)
put srngs
put soptions
get = do
sactorTime <- get
strajTime <- get
strajPushedBy <- get
sfactionAn <- get
sactorAn <- get
sgenerationAn <- get
sactorStasis <- get
sdiscoKindRev <- get
suniqueSet <- get
sitemRev <- get
sflavour <- get
sacounter <- get
sicounter <- get
snumSpawned <- get
sclientStates <- get
g <- get
srngs <- get
soptions <- get
let srandom = read g
sundo = ()
sperFid = EM.empty
sperValidFid = EM.empty
sperCacheFid = EM.empty
sfovLucidLid = EM.empty
sfovClearLid = EM.empty
sfovLitLid = EM.empty
sarenas = []
svalidArenas = False
sbreakLoop = False
sbreakASAP = False
swriteSave = False
soptionsNxt = defServerOptions
return $! StateServer{..}