1module WorldSetup where
2
3import Control.Monad.ST
4import Control.Monad.State.Strict
5import qualified Data.Vector as V
6import qualified Data.Set as S
7import Data.Vector ((!))
8import Data.Default
9import Control.Applicative
10import Prelude
11
12import Types
13import Level
14import Level.Border
15import Player
16import Spell
17import Poison
18import Rand
19
20-- Creates a new game world from the specified level content.
21makeWorld :: (Level, Level) -> Integer -> Rand -> ST RealWorld S
22makeWorld (frontl, backl) screenheight r = do
23	(front, frontbuf) <- setup frontv
24	(back, backbuf) <- setup backv
25	addCap front
26	addCap back
27	let s = S
28		{ world = front
29		, flipSide = back
30		, player = def { playerSpells = startingspells }
31		, bottomBuffer = (frontbuf, backbuf)
32		, topBuffer = 0
33		, peruser = def
34		, randSource = r
35		, helpShown = False
36		, messages = []
37		, spells = allSpells
38		, poisons = allPoisons
39		, windows = []
40		}
41	execStateT startingPosition s
42  where
43	(frontv, backv) = levelVectors (frontl, backl)
44
45	-- 2 for each end of scroll
46	scrollsize = fromIntegral $ screenheight - 4
47
48	setup :: V.Vector (V.Vector Char) -> ST RealWorld (World, Vec2 Char)
49	setup v = do
50		mv <- V.mapM V.thaw v
51		let (w, b) = V.splitAt (scrollsize - 1) mv
52		-- grow for scroll cap
53		let p = V.singleton (w ! 0)
54		let w' = V.concat [p,p,w,p,p]
55		(,)
56			<$> V.thaw w'
57			<*> V.thaw b
58
59	startingspells = flip S.insert startingSpells $
60		optspells !! fst (randomR (0, length optspells - 1) (randGen r))
61	optspells = S.toList maybeStartingSpells
62