module Game.LambdaHack.Server.ItemM
( registerItem, moveStashIfNeeded, randomResetTimeout, embedItemOnPos
, prepareItemKind, rollItemAspect, rollAndRegisterItem
, placeItemsInDungeon, embedItemsInDungeon, mapActorCStore_
#ifdef EXPOSE_INTERNAL
, onlyRegisterItem, computeRndTimeout, createCaveItem, createEmbedItem
#endif
) where
import Prelude ()
import Game.LambdaHack.Core.Prelude
import qualified Data.EnumMap.Strict as EM
import qualified Data.EnumSet as ES
import qualified Data.HashMap.Strict as HM
import Game.LambdaHack.Atomic
import Game.LambdaHack.Common.Actor
import Game.LambdaHack.Common.ActorState
import Game.LambdaHack.Common.Faction
import Game.LambdaHack.Common.Item
import qualified Game.LambdaHack.Common.ItemAspect as IA
import Game.LambdaHack.Common.Kind
import Game.LambdaHack.Common.Level
import Game.LambdaHack.Common.MonadStateRead
import Game.LambdaHack.Common.Point
import qualified Game.LambdaHack.Common.PointArray as PointArray
import Game.LambdaHack.Common.State
import qualified Game.LambdaHack.Common.Tile as Tile
import Game.LambdaHack.Common.Time
import Game.LambdaHack.Common.Types
import Game.LambdaHack.Content.CaveKind (citemFreq, citemNum)
import Game.LambdaHack.Content.ItemKind (ItemKind)
import qualified Game.LambdaHack.Content.ItemKind as IK
import Game.LambdaHack.Content.TileKind (TileKind)
import Game.LambdaHack.Core.Frequency
import Game.LambdaHack.Core.Random
import qualified Game.LambdaHack.Definition.Ability as Ability
import Game.LambdaHack.Definition.Defs
import Game.LambdaHack.Server.ItemRev
import Game.LambdaHack.Server.MonadServer
import Game.LambdaHack.Server.ServerOptions
import Game.LambdaHack.Server.State
onlyRegisterItem :: MonadServerAtomic m => ItemKnown -> m ItemId
onlyRegisterItem :: ItemKnown -> m ItemId
onlyRegisterItem itemKnown :: ItemKnown
itemKnown@(ItemKnown _ arItem :: AspectRecord
arItem _) = do
ItemRev
itemRev <- (StateServer -> ItemRev) -> m ItemRev
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer StateServer -> ItemRev
sitemRev
case ItemKnown -> ItemRev -> Maybe ItemId
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup ItemKnown
itemKnown ItemRev
itemRev of
Just iid :: ItemId
iid -> ItemId -> m ItemId
forall (m :: * -> *) a. Monad m => a -> m a
return ItemId
iid
Nothing -> do
ItemId
icounter <- (StateServer -> ItemId) -> m ItemId
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer StateServer -> ItemId
sicounter
Bool
executedOnServer <-
UpdAtomic -> m Bool
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m Bool
execUpdAtomicSer (UpdAtomic -> m Bool) -> UpdAtomic -> m Bool
forall a b. (a -> b) -> a -> b
$ ItemId -> AspectRecord -> UpdAtomic
UpdDiscoverServer ItemId
icounter AspectRecord
arItem
let !_A :: ()
_A = Bool -> () -> ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert Bool
executedOnServer ()
(StateServer -> StateServer) -> m ()
forall (m :: * -> *).
MonadServer m =>
(StateServer -> StateServer) -> m ()
modifyServer ((StateServer -> StateServer) -> m ())
-> (StateServer -> StateServer) -> m ()
forall a b. (a -> b) -> a -> b
$ \ser :: StateServer
ser ->
StateServer
ser { sitemRev :: ItemRev
sitemRev = ItemKnown -> ItemId -> ItemRev -> ItemRev
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert ItemKnown
itemKnown ItemId
icounter (StateServer -> ItemRev
sitemRev StateServer
ser)
, sicounter :: ItemId
sicounter = ItemId -> ItemId
forall a. Enum a => a -> a
succ ItemId
icounter }
ItemId -> m ItemId
forall (m :: * -> *) a. Monad m => a -> m a
return (ItemId -> m ItemId) -> ItemId -> m ItemId
forall a b. (a -> b) -> a -> b
$! ItemId
icounter
registerItem :: MonadServerAtomic m
=> Bool -> ItemFullKit -> ItemKnown -> Container -> m ItemId
registerItem :: Bool -> ItemFullKit -> ItemKnown -> Container -> m ItemId
registerItem verbose :: Bool
verbose (itemFull :: ItemFull
itemFull@ItemFull{Item
itemBase :: ItemFull -> Item
itemBase :: Item
itemBase, ContentId ItemKind
itemKindId :: ItemFull -> ContentId ItemKind
itemKindId :: ContentId ItemKind
itemKindId, ItemKind
itemKind :: ItemFull -> ItemKind
itemKind :: ItemKind
itemKind}, kit :: ItemQuant
kit)
itemKnown :: ItemKnown
itemKnown@(ItemKnown _ arItem :: AspectRecord
arItem _) containerRaw :: Container
containerRaw = do
Container
container <- case Container
containerRaw of
CActor aid :: ActorId
aid CEqp -> do
Actor
b <- (State -> Actor) -> m Actor
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Actor) -> m Actor) -> (State -> Actor) -> m Actor
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> Actor
getActorBody ActorId
aid
Container -> m Container
forall (m :: * -> *) a. Monad m => a -> m a
return (Container -> m Container) -> Container -> m Container
forall a b. (a -> b) -> a -> b
$! if Actor -> Int
eqpFreeN Actor
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ItemQuant -> Int
forall a b. (a, b) -> a
fst ItemQuant
kit
then Container
containerRaw
else ActorId -> CStore -> Container
CActor ActorId
aid CStore
CStash
_ -> Container -> m Container
forall (m :: * -> *) a. Monad m => a -> m a
return Container
containerRaw
ItemId
iid <- ItemKnown -> m ItemId
forall (m :: * -> *). MonadServerAtomic m => ItemKnown -> m ItemId
onlyRegisterItem ItemKnown
itemKnown
let slore :: SLore
slore = AspectRecord -> Container -> SLore
IA.loreFromContainer AspectRecord
arItem Container
container
(StateServer -> StateServer) -> m ()
forall (m :: * -> *).
MonadServer m =>
(StateServer -> StateServer) -> m ()
modifyServer ((StateServer -> StateServer) -> m ())
-> (StateServer -> StateServer) -> m ()
forall a b. (a -> b) -> a -> b
$ \ser :: StateServer
ser ->
StateServer
ser {sgenerationAn :: GenerationAnalytics
sgenerationAn = (EnumMap ItemId Int -> EnumMap ItemId Int)
-> SLore -> GenerationAnalytics -> GenerationAnalytics
forall k a. Enum k => (a -> a) -> k -> EnumMap k a -> EnumMap k a
EM.adjust ((Int -> Int -> Int)
-> ItemId -> Int -> EnumMap ItemId Int -> EnumMap ItemId Int
forall k a.
Enum k =>
(a -> a -> a) -> k -> a -> EnumMap k a -> EnumMap k a
EM.insertWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) ItemId
iid (ItemQuant -> Int
forall a b. (a, b) -> a
fst ItemQuant
kit)) SLore
slore
(StateServer -> GenerationAnalytics
sgenerationAn StateServer
ser)}
[UpdAtomic]
moveStash <- Container -> m [UpdAtomic]
forall (m :: * -> *).
MonadStateRead m =>
Container -> m [UpdAtomic]
moveStashIfNeeded Container
container
(UpdAtomic -> m ()) -> [UpdAtomic] -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
(a -> m ()) -> t a -> m ()
mapM_ UpdAtomic -> m ()
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m ()
execUpdAtomic [UpdAtomic]
moveStash
UpdAtomic -> m ()
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m ()
execUpdAtomic (UpdAtomic -> m ()) -> UpdAtomic -> m ()
forall a b. (a -> b) -> a -> b
$ Bool -> ItemId -> Item -> ItemQuant -> Container -> UpdAtomic
UpdCreateItem Bool
verbose ItemId
iid Item
itemBase ItemQuant
kit Container
container
let worth :: Int
worth = Int -> ItemKind -> Int
itemPrice (ItemQuant -> Int
forall a b. (a, b) -> a
fst ItemQuant
kit) ItemKind
itemKind
case Container
container of
_ | Int
worth Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
CActor _ COrgan -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
CTrunk{} -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
_ -> UpdAtomic -> m ()
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m ()
execUpdAtomic (UpdAtomic -> m ()) -> UpdAtomic -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> UpdAtomic
UpdAlterGold Int
worth
Bool
knowItems <- (StateServer -> Bool) -> m Bool
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer ((StateServer -> Bool) -> m Bool)
-> (StateServer -> Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ ServerOptions -> Bool
sknowItems (ServerOptions -> Bool)
-> (StateServer -> ServerOptions) -> StateServer -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateServer -> ServerOptions
soptions
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
knowItems (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ case Container
container of
CTrunk{} -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
_ -> UpdAtomic -> m ()
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m ()
execUpdAtomic (UpdAtomic -> m ()) -> UpdAtomic -> m ()
forall a b. (a -> b) -> a -> b
$ Container
-> ItemId -> ContentId ItemKind -> AspectRecord -> UpdAtomic
UpdDiscover Container
container ItemId
iid ContentId ItemKind
itemKindId AspectRecord
arItem
case Container
container of
CActor _ cstore :: CStore
cstore | CStore
cstore CStore -> [CStore] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CStore
CEqp, CStore
COrgan] ->
Int -> ItemId -> ItemFull -> [ItemTimer] -> Container -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
Int -> ItemId -> ItemFull -> [ItemTimer] -> Container -> m ()
randomResetTimeout (ItemQuant -> Int
forall a b. (a, b) -> a
fst ItemQuant
kit) ItemId
iid ItemFull
itemFull [] Container
container
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
ItemId -> m ItemId
forall (m :: * -> *) a. Monad m => a -> m a
return ItemId
iid
moveStashIfNeeded :: MonadStateRead m => Container -> m [UpdAtomic]
moveStashIfNeeded :: Container -> m [UpdAtomic]
moveStashIfNeeded c :: Container
c = case Container
c of
CActor aid :: ActorId
aid CStash -> do
Actor
b <- (State -> Actor) -> m Actor
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Actor) -> m Actor) -> (State -> Actor) -> m Actor
forall a b. (a -> b) -> a -> b
$ ActorId -> State -> Actor
getActorBody ActorId
aid
Maybe (LevelId, Point)
mstash <- (State -> Maybe (LevelId, Point)) -> m (Maybe (LevelId, Point))
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Maybe (LevelId, Point)) -> m (Maybe (LevelId, Point)))
-> (State -> Maybe (LevelId, Point)) -> m (Maybe (LevelId, Point))
forall a b. (a -> b) -> a -> b
$ \s :: State
s -> Faction -> Maybe (LevelId, Point)
gstash (Faction -> Maybe (LevelId, Point))
-> Faction -> Maybe (LevelId, Point)
forall a b. (a -> b) -> a -> b
$ State -> FactionDict
sfactionD State
s FactionDict -> FactionId -> Faction
forall k a. Enum k => EnumMap k a -> k -> a
EM.! Actor -> FactionId
bfid Actor
b
case Maybe (LevelId, Point)
mstash of
Just (lid :: LevelId
lid, pos :: Point
pos) -> do
ItemBag
bagStash <- (State -> ItemBag) -> m ItemBag
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> ItemBag) -> m ItemBag)
-> (State -> ItemBag) -> m ItemBag
forall a b. (a -> b) -> a -> b
$ LevelId -> Point -> State -> ItemBag
getFloorBag LevelId
lid Point
pos
[UpdAtomic] -> m [UpdAtomic]
forall (m :: * -> *) a. Monad m => a -> m a
return ([UpdAtomic] -> m [UpdAtomic]) -> [UpdAtomic] -> m [UpdAtomic]
forall a b. (a -> b) -> a -> b
$! if ItemBag -> Bool
forall k a. EnumMap k a -> Bool
EM.null ItemBag
bagStash
then [ Bool -> FactionId -> LevelId -> Point -> UpdAtomic
UpdLoseStashFaction Bool
False (Actor -> FactionId
bfid Actor
b) LevelId
lid Point
pos
, Bool -> FactionId -> LevelId -> Point -> UpdAtomic
UpdSpotStashFaction Bool
True (Actor -> FactionId
bfid Actor
b) (Actor -> LevelId
blid Actor
b) (Actor -> Point
bpos Actor
b) ]
else []
Nothing -> [UpdAtomic] -> m [UpdAtomic]
forall (m :: * -> *) a. Monad m => a -> m a
return [Bool -> FactionId -> LevelId -> Point -> UpdAtomic
UpdSpotStashFaction Bool
True (Actor -> FactionId
bfid Actor
b) (Actor -> LevelId
blid Actor
b) (Actor -> Point
bpos Actor
b)]
_ -> [UpdAtomic] -> m [UpdAtomic]
forall (m :: * -> *) a. Monad m => a -> m a
return []
randomResetTimeout :: MonadServerAtomic m
=> Int -> ItemId -> ItemFull -> [ItemTimer] -> Container
-> m ()
randomResetTimeout :: Int -> ItemId -> ItemFull -> [ItemTimer] -> Container -> m ()
randomResetTimeout k :: Int
k iid :: ItemId
iid itemFull :: ItemFull
itemFull beforeIt :: [ItemTimer]
beforeIt toC :: Container
toC = do
LevelId
lid <- (State -> LevelId) -> m LevelId
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> LevelId) -> m LevelId)
-> (State -> LevelId) -> m LevelId
forall a b. (a -> b) -> a -> b
$ Container -> State -> LevelId
lidFromC Container
toC
Time
localTime <- (State -> Time) -> m Time
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> Time) -> m Time) -> (State -> Time) -> m Time
forall a b. (a -> b) -> a -> b
$ LevelId -> State -> Time
getLocalTime LevelId
lid
Maybe ItemTimer
mrndTimeout <- Rnd (Maybe ItemTimer) -> m (Maybe ItemTimer)
forall (m :: * -> *) a. MonadServer m => Rnd a -> m a
rndToAction (Rnd (Maybe ItemTimer) -> m (Maybe ItemTimer))
-> Rnd (Maybe ItemTimer) -> m (Maybe ItemTimer)
forall a b. (a -> b) -> a -> b
$ Time -> ItemFull -> Rnd (Maybe ItemTimer)
computeRndTimeout Time
localTime ItemFull
itemFull
case Maybe ItemTimer
mrndTimeout of
Just rndT :: ItemTimer
rndT -> do
ItemBag
bagAfter <- (State -> ItemBag) -> m ItemBag
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> ItemBag) -> m ItemBag)
-> (State -> ItemBag) -> m ItemBag
forall a b. (a -> b) -> a -> b
$ Container -> State -> ItemBag
getContainerBag Container
toC
let afterIt :: [ItemTimer]
afterIt = case ItemId
iid ItemId -> ItemBag -> Maybe ItemQuant
forall k a. Enum k => k -> EnumMap k a -> Maybe a
`EM.lookup` ItemBag
bagAfter of
Nothing -> [Char] -> [ItemTimer]
forall a. (?callStack::CallStack) => [Char] -> a
error ([Char] -> [ItemTimer]) -> [Char] -> [ItemTimer]
forall a b. (a -> b) -> a -> b
$ "" [Char] -> (ItemId, ItemBag, Container) -> [Char]
forall v. Show v => [Char] -> v -> [Char]
`showFailure` (ItemId
iid, ItemBag
bagAfter, Container
toC)
Just (_, it2 :: [ItemTimer]
it2) -> [ItemTimer]
it2
resetIt :: [ItemTimer]
resetIt = [ItemTimer]
beforeIt [ItemTimer] -> [ItemTimer] -> [ItemTimer]
forall a. [a] -> [a] -> [a]
++ Int -> ItemTimer -> [ItemTimer]
forall a. Int -> a -> [a]
replicate Int
k ItemTimer
rndT
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([ItemTimer]
afterIt [ItemTimer] -> [ItemTimer] -> Bool
forall a. Eq a => a -> a -> Bool
/= [ItemTimer]
resetIt) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
UpdAtomic -> m ()
forall (m :: * -> *). MonadServerAtomic m => UpdAtomic -> m ()
execUpdAtomic (UpdAtomic -> m ()) -> UpdAtomic -> m ()
forall a b. (a -> b) -> a -> b
$ ItemId -> Container -> [ItemTimer] -> [ItemTimer] -> UpdAtomic
UpdTimeItem ItemId
iid Container
toC [ItemTimer]
afterIt [ItemTimer]
resetIt
Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
computeRndTimeout :: Time -> ItemFull -> Rnd (Maybe ItemTimer)
computeRndTimeout :: Time -> ItemFull -> Rnd (Maybe ItemTimer)
computeRndTimeout localTime :: Time
localTime ItemFull{itemDisco :: ItemFull -> ItemDisco
itemDisco=ItemDiscoFull itemAspect :: AspectRecord
itemAspect} = do
let t :: Int
t = AspectRecord -> Int
IA.aTimeout AspectRecord
itemAspect
if Int
t Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 0 then do
Int
rndT <- Int -> Rnd Int
forall a. Integral a => a -> Rnd a
randomR0 Int
t
let rndTurns :: Delta Time
rndTurns = Delta Time -> Int -> Delta Time
timeDeltaScale (Time -> Delta Time
forall a. a -> Delta a
Delta Time
timeTurn) (Int
t Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rndT)
Maybe ItemTimer -> Rnd (Maybe ItemTimer)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ItemTimer -> Rnd (Maybe ItemTimer))
-> Maybe ItemTimer -> Rnd (Maybe ItemTimer)
forall a b. (a -> b) -> a -> b
$ ItemTimer -> Maybe ItemTimer
forall a. a -> Maybe a
Just (ItemTimer -> Maybe ItemTimer) -> ItemTimer -> Maybe ItemTimer
forall a b. (a -> b) -> a -> b
$ Time -> Delta Time -> ItemTimer
createItemTimer Time
localTime Delta Time
rndTurns
else Maybe ItemTimer -> Rnd (Maybe ItemTimer)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ItemTimer
forall a. Maybe a
Nothing
computeRndTimeout _ _ = [Char] -> Rnd (Maybe ItemTimer)
forall a. (?callStack::CallStack) => [Char] -> a
error "computeRndTimeout: server ignorant about an item"
createCaveItem :: MonadServerAtomic m => Point -> LevelId -> m ()
createCaveItem :: Point -> LevelId -> m ()
createCaveItem pos :: Point
pos lid :: LevelId
lid = do
COps{ContentData CaveKind
cocave :: COps -> ContentData CaveKind
cocave :: ContentData CaveKind
cocave} <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
Level{ContentId CaveKind
lkind :: Level -> ContentId CaveKind
lkind :: ContentId CaveKind
lkind} <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
lid
let container :: Container
container = LevelId -> Point -> Container
CFloor LevelId
lid Point
pos
litemFreq :: Freqs ItemKind
litemFreq = CaveKind -> Freqs ItemKind
citemFreq (CaveKind -> Freqs ItemKind) -> CaveKind -> Freqs ItemKind
forall a b. (a -> b) -> a -> b
$ ContentData CaveKind -> ContentId CaveKind -> CaveKind
forall a. ContentData a -> ContentId a -> a
okind ContentData CaveKind
cocave ContentId CaveKind
lkind
Frequency (ContentId ItemKind, ItemKind)
freq <- Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
forall (m :: * -> *).
MonadServerAtomic m =>
Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
prepareItemKind 0 LevelId
lid Freqs ItemKind
litemFreq
Maybe (ItemId, ItemFullKit)
mIidEtc <- Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
forall (m :: * -> *).
MonadServerAtomic m =>
Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
rollAndRegisterItem Bool
True LevelId
lid Frequency (ContentId ItemKind, ItemKind)
freq Container
container Maybe Int
forall a. Maybe a
Nothing
LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
createKitItems LevelId
lid Point
pos Maybe (ItemId, ItemFullKit)
mIidEtc
createEmbedItem :: MonadServerAtomic m
=> LevelId -> Point -> GroupName ItemKind -> m ()
createEmbedItem :: LevelId -> Point -> GroupName ItemKind -> m ()
createEmbedItem lid :: LevelId
lid pos :: Point
pos grp :: GroupName ItemKind
grp = do
let container :: Container
container = LevelId -> Point -> Container
CEmbed LevelId
lid Point
pos
Frequency (ContentId ItemKind, ItemKind)
freq <- Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
forall (m :: * -> *).
MonadServerAtomic m =>
Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
prepareItemKind 0 LevelId
lid [(GroupName ItemKind
grp, 1)]
Maybe (ItemId, ItemFullKit)
mIidEtc <- Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
forall (m :: * -> *).
MonadServerAtomic m =>
Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
rollAndRegisterItem Bool
True LevelId
lid Frequency (ContentId ItemKind, ItemKind)
freq Container
container Maybe Int
forall a. Maybe a
Nothing
LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
createKitItems LevelId
lid Point
pos Maybe (ItemId, ItemFullKit)
mIidEtc
createKitItems :: MonadServerAtomic m
=> LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
createKitItems :: LevelId -> Point -> Maybe (ItemId, ItemFullKit) -> m ()
createKitItems lid :: LevelId
lid pos :: Point
pos mIidEtc :: Maybe (ItemId, ItemFullKit)
mIidEtc = case Maybe (ItemId, ItemFullKit)
mIidEtc of
Nothing -> [Char] -> m ()
forall a. (?callStack::CallStack) => [Char] -> a
error ([Char] -> m ()) -> [Char] -> m ()
forall a b. (a -> b) -> a -> b
$ "" [Char] -> (LevelId, Point, Maybe (ItemId, ItemFullKit)) -> [Char]
forall v. Show v => [Char] -> v -> [Char]
`showFailure` (LevelId
lid, Point
pos, Maybe (ItemId, ItemFullKit)
mIidEtc)
Just (_, (itemFull :: ItemFull
itemFull, _)) -> do
COps
cops <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
Level
lvl <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
lid
let ikit :: [(GroupName ItemKind, CStore)]
ikit = ItemKind -> [(GroupName ItemKind, CStore)]
IK.ikit (ItemKind -> [(GroupName ItemKind, CStore)])
-> ItemKind -> [(GroupName ItemKind, CStore)]
forall a b. (a -> b) -> a -> b
$ ItemFull -> ItemKind
itemKind ItemFull
itemFull
nearbyPassable :: [Point]
nearbyPassable = Int -> [Point] -> [Point]
forall a. Int -> [a] -> [a]
take (20 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [(GroupName ItemKind, CStore)] -> Int
forall a. [a] -> Int
length [(GroupName ItemKind, CStore)]
ikit)
([Point] -> [Point]) -> [Point] -> [Point]
forall a b. (a -> b) -> a -> b
$ COps -> Level -> Point -> [Point]
nearbyPassablePoints COps
cops Level
lvl Point
pos
walkable :: Point -> Bool
walkable p :: Point
p = TileSpeedup -> ContentId TileKind -> Bool
Tile.isWalkable (COps -> TileSpeedup
coTileSpeedup COps
cops) (Level
lvl Level -> Point -> ContentId TileKind
`at` Point
p)
good :: Point -> Bool
good p :: Point
p = Point -> Bool
walkable Point
p Bool -> Bool -> Bool
&& Point
p Point -> EnumMap Point ItemBag -> Bool
forall k a. Enum k => k -> EnumMap k a -> Bool
`EM.notMember` Level -> EnumMap Point ItemBag
lfloor Level
lvl
kitPos :: [((GroupName ItemKind, CStore), Point)]
kitPos = [(GroupName ItemKind, CStore)]
-> [Point] -> [((GroupName ItemKind, CStore), Point)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(GroupName ItemKind, CStore)]
ikit ([Point] -> [((GroupName ItemKind, CStore), Point)])
-> [Point] -> [((GroupName ItemKind, CStore), Point)]
forall a b. (a -> b) -> a -> b
$ (Point -> Bool) -> [Point] -> [Point]
forall a. (a -> Bool) -> [a] -> [a]
filter Point -> Bool
good [Point]
nearbyPassable
[Point] -> [Point] -> [Point]
forall a. [a] -> [a] -> [a]
++ (Point -> Bool) -> [Point] -> [Point]
forall a. (a -> Bool) -> [a] -> [a]
filter Point -> Bool
walkable [Point]
nearbyPassable
[Point] -> [Point] -> [Point]
forall a. [a] -> [a] -> [a]
++ Point -> [Point]
forall a. a -> [a]
repeat Point
pos
[((GroupName ItemKind, CStore), Point)]
-> (((GroupName ItemKind, CStore), Point) -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t a -> (a -> m ()) -> m ()
forM_ [((GroupName ItemKind, CStore), Point)]
kitPos ((((GroupName ItemKind, CStore), Point) -> m ()) -> m ())
-> (((GroupName ItemKind, CStore), Point) -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \((ikGrp :: GroupName ItemKind
ikGrp, cstore :: CStore
cstore), p :: Point
p) -> do
let container :: Container
container = if CStore
cstore CStore -> CStore -> Bool
forall a. Eq a => a -> a -> Bool
== CStore
CGround
then LevelId -> Point -> Container
CFloor LevelId
lid Point
p
else LevelId -> Point -> Container
CEmbed LevelId
lid Point
pos
itemFreq :: Freqs ItemKind
itemFreq = [(GroupName ItemKind
ikGrp, 1)]
Frequency (ContentId ItemKind, ItemKind)
freq <- Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
forall (m :: * -> *).
MonadServerAtomic m =>
Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
prepareItemKind 0 LevelId
lid Freqs ItemKind
itemFreq
Maybe (ItemId, ItemFullKit)
mresult <- Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
forall (m :: * -> *).
MonadServerAtomic m =>
Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
rollAndRegisterItem Bool
False LevelId
lid Frequency (ContentId ItemKind, ItemKind)
freq Container
container Maybe Int
forall a. Maybe a
Nothing
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Maybe (ItemId, ItemFullKit) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (ItemId, ItemFullKit)
mresult) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
embedItemOnPos :: MonadServerAtomic m
=> LevelId -> Point -> ContentId TileKind -> m ()
embedItemOnPos :: LevelId -> Point -> ContentId TileKind -> m ()
embedItemOnPos lid :: LevelId
lid pos :: Point
pos tk :: ContentId TileKind
tk = do
COps{ContentData TileKind
cotile :: COps -> ContentData TileKind
cotile :: ContentData TileKind
cotile} <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
let embedGroups :: [GroupName ItemKind]
embedGroups = ContentData TileKind -> ContentId TileKind -> [GroupName ItemKind]
Tile.embeddedItems ContentData TileKind
cotile ContentId TileKind
tk
(GroupName ItemKind -> m ()) -> [GroupName ItemKind] -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
(a -> m ()) -> t a -> m ()
mapM_ (LevelId -> Point -> GroupName ItemKind -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
LevelId -> Point -> GroupName ItemKind -> m ()
createEmbedItem LevelId
lid Point
pos) [GroupName ItemKind]
embedGroups
prepareItemKind :: MonadServerAtomic m
=> Int -> LevelId -> Freqs ItemKind
-> m (Frequency (ContentId IK.ItemKind, ItemKind))
prepareItemKind :: Int
-> LevelId
-> Freqs ItemKind
-> m (Frequency (ContentId ItemKind, ItemKind))
prepareItemKind lvlSpawned :: Int
lvlSpawned lid :: LevelId
lid itemFreq :: Freqs ItemKind
itemFreq = do
COps
cops <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
UniqueSet
uniqueSet <- (StateServer -> UniqueSet) -> m UniqueSet
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer StateServer -> UniqueSet
suniqueSet
AbsDepth
totalDepth <- (State -> AbsDepth) -> m AbsDepth
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> AbsDepth
stotalDepth
Level{AbsDepth
ldepth :: Level -> AbsDepth
ldepth :: AbsDepth
ldepth} <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
lid
Frequency (ContentId ItemKind, ItemKind)
-> m (Frequency (ContentId ItemKind, ItemKind))
forall (m :: * -> *) a. Monad m => a -> m a
return (Frequency (ContentId ItemKind, ItemKind)
-> m (Frequency (ContentId ItemKind, ItemKind)))
-> Frequency (ContentId ItemKind, ItemKind)
-> m (Frequency (ContentId ItemKind, ItemKind))
forall a b. (a -> b) -> a -> b
$! COps
-> UniqueSet
-> Freqs ItemKind
-> AbsDepth
-> AbsDepth
-> Int
-> Frequency (ContentId ItemKind, ItemKind)
newItemKind COps
cops UniqueSet
uniqueSet Freqs ItemKind
itemFreq AbsDepth
ldepth AbsDepth
totalDepth Int
lvlSpawned
rollItemAspect :: MonadServerAtomic m
=> Frequency (ContentId IK.ItemKind, ItemKind) -> LevelId
-> m NewItem
rollItemAspect :: Frequency (ContentId ItemKind, ItemKind) -> LevelId -> m NewItem
rollItemAspect freq :: Frequency (ContentId ItemKind, ItemKind)
freq lid :: LevelId
lid = do
COps
cops <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
FlavourMap
flavour <- (StateServer -> FlavourMap) -> m FlavourMap
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer StateServer -> FlavourMap
sflavour
DiscoveryKindRev
discoRev <- (StateServer -> DiscoveryKindRev) -> m DiscoveryKindRev
forall (m :: * -> *) a. MonadServer m => (StateServer -> a) -> m a
getsServer StateServer -> DiscoveryKindRev
sdiscoKindRev
AbsDepth
totalDepth <- (State -> AbsDepth) -> m AbsDepth
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> AbsDepth
stotalDepth
Level{AbsDepth
ldepth :: AbsDepth
ldepth :: Level -> AbsDepth
ldepth} <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
lid
NewItem
m2 <- Rnd NewItem -> m NewItem
forall (m :: * -> *) a. MonadServer m => Rnd a -> m a
rndToAction (Rnd NewItem -> m NewItem) -> Rnd NewItem -> m NewItem
forall a b. (a -> b) -> a -> b
$ COps
-> Frequency (ContentId ItemKind, ItemKind)
-> FlavourMap
-> DiscoveryKindRev
-> AbsDepth
-> AbsDepth
-> Rnd NewItem
newItem COps
cops Frequency (ContentId ItemKind, ItemKind)
freq FlavourMap
flavour DiscoveryKindRev
discoRev AbsDepth
ldepth AbsDepth
totalDepth
case NewItem
m2 of
NewItem (ItemKnown _ arItem :: AspectRecord
arItem _) ItemFull{ContentId ItemKind
itemKindId :: ContentId ItemKind
itemKindId :: ItemFull -> ContentId ItemKind
itemKindId} _ -> do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Flag -> AspectRecord -> Bool
IA.checkFlag Flag
Ability.Unique AspectRecord
arItem) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
(StateServer -> StateServer) -> m ()
forall (m :: * -> *).
MonadServer m =>
(StateServer -> StateServer) -> m ()
modifyServer ((StateServer -> StateServer) -> m ())
-> (StateServer -> StateServer) -> m ()
forall a b. (a -> b) -> a -> b
$ \ser :: StateServer
ser ->
StateServer
ser {suniqueSet :: UniqueSet
suniqueSet = ContentId ItemKind -> UniqueSet -> UniqueSet
forall k. Enum k => k -> EnumSet k -> EnumSet k
ES.insert ContentId ItemKind
itemKindId (StateServer -> UniqueSet
suniqueSet StateServer
ser)}
NoNewItem -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
NewItem -> m NewItem
forall (m :: * -> *) a. Monad m => a -> m a
return NewItem
m2
rollAndRegisterItem :: MonadServerAtomic m
=> Bool
-> LevelId
-> Frequency (ContentId IK.ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
rollAndRegisterItem :: Bool
-> LevelId
-> Frequency (ContentId ItemKind, ItemKind)
-> Container
-> Maybe Int
-> m (Maybe (ItemId, ItemFullKit))
rollAndRegisterItem verbose :: Bool
verbose lid :: LevelId
lid freq :: Frequency (ContentId ItemKind, ItemKind)
freq container :: Container
container mk :: Maybe Int
mk = do
NewItem
m2 <- Frequency (ContentId ItemKind, ItemKind) -> LevelId -> m NewItem
forall (m :: * -> *).
MonadServerAtomic m =>
Frequency (ContentId ItemKind, ItemKind) -> LevelId -> m NewItem
rollItemAspect Frequency (ContentId ItemKind, ItemKind)
freq LevelId
lid
case NewItem
m2 of
NoNewItem -> Maybe (ItemId, ItemFullKit) -> m (Maybe (ItemId, ItemFullKit))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (ItemId, ItemFullKit)
forall a. Maybe a
Nothing
NewItem itemKnown :: ItemKnown
itemKnown itemFull :: ItemFull
itemFull kit :: ItemQuant
kit -> do
let f :: Int -> ItemQuant
f k :: Int
k = if Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 Bool -> Bool -> Bool
&& [ItemTimer] -> Bool
forall a. [a] -> Bool
null (ItemQuant -> [ItemTimer]
forall a b. (a, b) -> b
snd ItemQuant
kit)
then ItemQuant
quantSingle
else (Int
k, ItemQuant -> [ItemTimer]
forall a b. (a, b) -> b
snd ItemQuant
kit)
!kit2 :: ItemQuant
kit2 = ItemQuant -> (Int -> ItemQuant) -> Maybe Int -> ItemQuant
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ItemQuant
kit Int -> ItemQuant
f Maybe Int
mk
ItemId
iid <- Bool -> ItemFullKit -> ItemKnown -> Container -> m ItemId
forall (m :: * -> *).
MonadServerAtomic m =>
Bool -> ItemFullKit -> ItemKnown -> Container -> m ItemId
registerItem Bool
verbose (ItemFull
itemFull, ItemQuant
kit2) ItemKnown
itemKnown Container
container
Maybe (ItemId, ItemFullKit) -> m (Maybe (ItemId, ItemFullKit))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (ItemId, ItemFullKit) -> m (Maybe (ItemId, ItemFullKit)))
-> Maybe (ItemId, ItemFullKit) -> m (Maybe (ItemId, ItemFullKit))
forall a b. (a -> b) -> a -> b
$ (ItemId, ItemFullKit) -> Maybe (ItemId, ItemFullKit)
forall a. a -> Maybe a
Just (ItemId
iid, (ItemFull
itemFull, ItemQuant
kit2))
placeItemsInDungeon :: forall m. MonadServerAtomic m
=> EM.EnumMap LevelId (EM.EnumMap FactionId Point) -> m ()
placeItemsInDungeon :: EnumMap LevelId (EnumMap FactionId Point) -> m ()
placeItemsInDungeon factionPositions :: EnumMap LevelId (EnumMap FactionId Point)
factionPositions = do
COps{ContentData CaveKind
cocave :: ContentData CaveKind
cocave :: COps -> ContentData CaveKind
cocave, TileSpeedup
coTileSpeedup :: TileSpeedup
coTileSpeedup :: COps -> TileSpeedup
coTileSpeedup} <- (State -> COps) -> m COps
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> COps
scops
AbsDepth
totalDepth <- (State -> AbsDepth) -> m AbsDepth
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> AbsDepth
stotalDepth
let initialItems :: (LevelId, Level) -> m ()
initialItems (lid :: LevelId
lid, lvl :: Level
lvl@Level{ContentId CaveKind
lkind :: ContentId CaveKind
lkind :: Level -> ContentId CaveKind
lkind, AbsDepth
ldepth :: AbsDepth
ldepth :: Level -> AbsDepth
ldepth}) = do
Int
litemNum <- Rnd Int -> m Int
forall (m :: * -> *) a. MonadServer m => Rnd a -> m a
rndToAction (Rnd Int -> m Int) -> Rnd Int -> m Int
forall a b. (a -> b) -> a -> b
$ AbsDepth -> AbsDepth -> Dice -> Rnd Int
castDice AbsDepth
ldepth AbsDepth
totalDepth
(CaveKind -> Dice
citemNum (CaveKind -> Dice) -> CaveKind -> Dice
forall a b. (a -> b) -> a -> b
$ ContentData CaveKind -> ContentId CaveKind -> CaveKind
forall a. ContentData a -> ContentId a -> a
okind ContentData CaveKind
cocave ContentId CaveKind
lkind)
let alPos :: [Point]
alPos = EnumMap FactionId Point -> [Point]
forall k a. EnumMap k a -> [a]
EM.elems (EnumMap FactionId Point -> [Point])
-> EnumMap FactionId Point -> [Point]
forall a b. (a -> b) -> a -> b
$ EnumMap FactionId Point
-> LevelId
-> EnumMap LevelId (EnumMap FactionId Point)
-> EnumMap FactionId Point
forall k a. Enum k => a -> k -> EnumMap k a -> a
EM.findWithDefault EnumMap FactionId Point
forall k a. EnumMap k a
EM.empty LevelId
lid EnumMap LevelId (EnumMap FactionId Point)
factionPositions
placeItems :: Int -> m ()
placeItems :: Int -> m ()
placeItems n :: Int
n | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
litemNum = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
placeItems !Int
n = do
Level{EnumMap Point ItemBag
lfloor :: EnumMap Point ItemBag
lfloor :: Level -> EnumMap Point ItemBag
lfloor} <- LevelId -> m Level
forall (m :: * -> *). MonadStateRead m => LevelId -> m Level
getLevel LevelId
lid
let distAndNotFloor :: Point -> ContentId TileKind -> Bool
distAndNotFloor !Point
p _ =
let f :: Point -> Bool -> Bool
f !Point
k b :: Bool
b = Point -> Point -> Int
chessDist Point
p Point
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 4 Bool -> Bool -> Bool
&& Bool
b
in Point
p Point -> EnumMap Point ItemBag -> Bool
forall k a. Enum k => k -> EnumMap k a -> Bool
`EM.notMember` EnumMap Point ItemBag
lfloor Bool -> Bool -> Bool
&& (Point -> Bool -> Bool) -> Bool -> [Point] -> Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Point -> Bool -> Bool
f Bool
True [Point]
alPos
Maybe Point
mpos <- Rnd (Maybe Point) -> m (Maybe Point)
forall (m :: * -> *) a. MonadServer m => Rnd a -> m a
rndToAction (Rnd (Maybe Point) -> m (Maybe Point))
-> Rnd (Maybe Point) -> m (Maybe Point)
forall a b. (a -> b) -> a -> b
$ Int
-> Level
-> (Point -> ContentId TileKind -> Bool)
-> [Point -> ContentId TileKind -> Bool]
-> (Point -> ContentId TileKind -> Bool)
-> [Point -> ContentId TileKind -> Bool]
-> Rnd (Maybe Point)
findPosTry2 20 Level
lvl
(\_ !ContentId TileKind
t -> TileSpeedup -> ContentId TileKind -> Bool
Tile.isWalkable TileSpeedup
coTileSpeedup ContentId TileKind
t
Bool -> Bool -> Bool
&& Bool -> Bool
not (TileSpeedup -> ContentId TileKind -> Bool
Tile.isNoItem TileSpeedup
coTileSpeedup ContentId TileKind
t))
[ \_ !ContentId TileKind
t -> TileSpeedup -> ContentId TileKind -> Bool
Tile.isVeryOftenItem TileSpeedup
coTileSpeedup ContentId TileKind
t
, \_ !ContentId TileKind
t -> TileSpeedup -> ContentId TileKind -> Bool
Tile.isCommonItem TileSpeedup
coTileSpeedup ContentId TileKind
t ]
Point -> ContentId TileKind -> Bool
distAndNotFloor
[Point -> ContentId TileKind -> Bool
distAndNotFloor, Point -> ContentId TileKind -> Bool
distAndNotFloor]
case Maybe Point
mpos of
Just pos :: Point
pos -> do
Point -> LevelId -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
Point -> LevelId -> m ()
createCaveItem Point
pos LevelId
lid
Int -> m ()
placeItems (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1)
Nothing -> Text -> m ()
forall (m :: * -> *). MonadServer m => Text -> m ()
debugPossiblyPrint
"Server: placeItemsInDungeon: failed to find positions"
Int -> m ()
placeItems 0
Dungeon
dungeon <- (State -> Dungeon) -> m Dungeon
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> Dungeon
sdungeon
let fromEasyToHard :: [(LevelId, Level)]
fromEasyToHard = ((LevelId, Level) -> (LevelId, Level) -> Ordering)
-> [(LevelId, Level)] -> [(LevelId, Level)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((LevelId, Level) -> AbsDepth)
-> (LevelId, Level) -> (LevelId, Level) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (Level -> AbsDepth
ldepth (Level -> AbsDepth)
-> ((LevelId, Level) -> Level) -> (LevelId, Level) -> AbsDepth
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LevelId, Level) -> Level
forall a b. (a, b) -> b
snd)) ([(LevelId, Level)] -> [(LevelId, Level)])
-> [(LevelId, Level)] -> [(LevelId, Level)]
forall a b. (a -> b) -> a -> b
$ Dungeon -> [(LevelId, Level)]
forall k a. Enum k => EnumMap k a -> [(k, a)]
EM.assocs Dungeon
dungeon
((LevelId, Level) -> m ()) -> [(LevelId, Level)] -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
(a -> m ()) -> t a -> m ()
mapM_ (LevelId, Level) -> m ()
initialItems [(LevelId, Level)]
fromEasyToHard
embedItemsInDungeon :: MonadServerAtomic m => m ()
embedItemsInDungeon :: m ()
embedItemsInDungeon = do
let embedItemsOnLevel :: (LevelId, Level) -> m ()
embedItemsOnLevel (lid :: LevelId
lid, Level{TileMap
ltile :: Level -> TileMap
ltile :: TileMap
ltile}) =
(Point -> ContentId TileKind -> m ()) -> TileMap -> m ()
forall (m :: * -> *) c.
(Monad m, UnboxRepClass c) =>
(Point -> c -> m ()) -> Array c -> m ()
PointArray.imapMA_ (LevelId -> Point -> ContentId TileKind -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
LevelId -> Point -> ContentId TileKind -> m ()
embedItemOnPos LevelId
lid) TileMap
ltile
Dungeon
dungeon <- (State -> Dungeon) -> m Dungeon
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState State -> Dungeon
sdungeon
let fromEasyToHard :: [(LevelId, Level)]
fromEasyToHard = ((LevelId, Level) -> (LevelId, Level) -> Ordering)
-> [(LevelId, Level)] -> [(LevelId, Level)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (((LevelId, Level) -> AbsDepth)
-> (LevelId, Level) -> (LevelId, Level) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (Level -> AbsDepth
ldepth (Level -> AbsDepth)
-> ((LevelId, Level) -> Level) -> (LevelId, Level) -> AbsDepth
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LevelId, Level) -> Level
forall a b. (a, b) -> b
snd)) ([(LevelId, Level)] -> [(LevelId, Level)])
-> [(LevelId, Level)] -> [(LevelId, Level)]
forall a b. (a -> b) -> a -> b
$ Dungeon -> [(LevelId, Level)]
forall k a. Enum k => EnumMap k a -> [(k, a)]
EM.assocs Dungeon
dungeon
((LevelId, Level) -> m ()) -> [(LevelId, Level)] -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
(a -> m ()) -> t a -> m ()
mapM_ (LevelId, Level) -> m ()
forall (m :: * -> *).
MonadServerAtomic m =>
(LevelId, Level) -> m ()
embedItemsOnLevel [(LevelId, Level)]
fromEasyToHard
mapActorCStore_ :: MonadServer m
=> CStore -> (ItemId -> ItemQuant -> m ()) -> Actor -> m ()
mapActorCStore_ :: CStore -> (ItemId -> ItemQuant -> m ()) -> Actor -> m ()
mapActorCStore_ cstore :: CStore
cstore f :: ItemId -> ItemQuant -> m ()
f b :: Actor
b = do
ItemBag
bag <- (State -> ItemBag) -> m ItemBag
forall (m :: * -> *) a. MonadStateRead m => (State -> a) -> m a
getsState ((State -> ItemBag) -> m ItemBag)
-> (State -> ItemBag) -> m ItemBag
forall a b. (a -> b) -> a -> b
$ Actor -> CStore -> State -> ItemBag
getBodyStoreBag Actor
b CStore
cstore
((ItemId, ItemQuant) -> m ()) -> [(ItemId, ItemQuant)] -> m ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
(a -> m ()) -> t a -> m ()
mapM_ ((ItemId -> ItemQuant -> m ()) -> (ItemId, ItemQuant) -> m ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ItemId -> ItemQuant -> m ()
f) ([(ItemId, ItemQuant)] -> m ()) -> [(ItemId, ItemQuant)] -> m ()
forall a b. (a -> b) -> a -> b
$ ItemBag -> [(ItemId, ItemQuant)]
forall k a. Enum k => EnumMap k a -> [(k, a)]
EM.assocs ItemBag
bag