3 ChunkType, ChunkData, chunkType,
4 initSimSpace, testSim, physStep, validDirects,
5 simToString, stringToSim
9 import Data.Vector ((!), (//))
10 import qualified Data.Vector as V
12 data Simulation = Simulation
13 { simSpace :: V.Vector ChunkData
22 | Drain deriving (Show, Enum)
24 data ChunkData = ChunkData
25 { chunkType :: ChunkType
29 simGet Simulation{simSpace=s,simW=w} x y = s ! (y*w+x)
30 simSet sim@Simulation{simSpace=s,simW=w,simH=h} c x y = sim { simSpace = (s // [(y*w+x,c)]) }
31 simGetChunkType sim x y = chunkType $ simGet sim x y
33 initSimSpace x y = Simulation
34 { simSpace = V.replicate (y*x) ChunkData { chunkType=Air }
39 testSim = let a = simSet (initSimSpace 10 10) (ChunkData Water) 5 5
40 in simSet a (ChunkData Wall) 5 6
42 physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] sim sim
43 -- _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} | trace ("" ++ show h) False = undefined
44 _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} =
46 else _physStep (tail grid) next sim
47 where x = fst $ head grid
49 valid = validDirects x y w h
50 next = case simGetChunkType sim x y of
51 Water -> updateWaterChunk x y valid acc
52 Pump -> updatePumpChunk x y valid acc
53 Drain -> updateDrainChunk x y valid acc
56 -- template for update functions
57 updateScaffolder x y valid sim directs cond exec =
58 if null directs then sim
59 else if cond (head directs) then exec (head directs)
60 else updateScaffolder x y valid sim (tail directs) cond exec
62 updateWaterChunk x y valid sim = updateScaffolder x y valid sim
63 [(0,1),(-1,1),(1,1),(-1,0),(1,0)]
66 fromEnum (simGetChunkType sim ((+) x $ fst qd) ((+) y $ snd qd)) == fromEnum Air
69 (simSet sim ChunkData { chunkType=Air } x y)
70 ChunkData { chunkType=Water }
75 updatePumpChunk x y valid sim = updateScaffolder x y valid sim
79 fromEnum (simGetChunkType sim ((+) x $ fst qd) ((+) y $ snd qd)) == fromEnum Air
81 (\qd -> simSet sim ChunkData { chunkType=Water } ((+) x $ fst qd) ((+) y $ snd qd))
83 updateDrainChunk x y valid sim = updateScaffolder x y valid sim
84 [(0,1),(0,-1),(-1,0),(1,0)]
87 fromEnum (simGetChunkType sim ((+) x $ fst qd) ((+) y $ snd qd)) == fromEnum Water
89 (\qd -> simSet sim ChunkData { chunkType=Air } ((+) x $ fst qd) ((+) y $ snd qd))
91 -- gets chunks around a given chunk that are inside grid
92 -- validDirects x y w h | trace ("w:"++ show w ++ "h:" ++ show h) False = undefined
93 validDirects x y w h = filter
94 (\q -> 0 <= (fst q)+x && (fst q)+x < w && 0 <= (snd q)+y && (snd q)+y < h)
95 [(a,b) | a <- [-1..1], b <- [-1..1], not (a==0 && b==0)]
97 simToString :: Simulation -> [Char]
98 simToString sim@Simulation{simW=w} =
99 let simStr = V.toList $ V.map chunkToChar $ simSpace sim
100 in insert w '\n' simStr
102 -- from https://stackoverflow.com/questions/12659562/insert-specific-element-y-after-every-n-elements-in-a-list
103 insert :: Int -> a -> [a] -> [a]
104 insert n y xs = countdown n xs where
105 countdown 0 xs = y:countdown n xs
107 countdown m (x:xs) = x:countdown (m-1) xs
109 -- stringToSim :: [String] -> Simulation
110 stringToSim strings =
111 _stringToSim st grid (initSimSpace w h)
112 where stripped = strings
113 w = maximum $ [(length s) | s <- stripped]
115 grid = [(a,b) | a <- [0..(length stripped)-1], b <- [0..(length $ stripped !! a)-1]]
118 _stringToSim st grid acc =
119 if null grid || null st then acc
120 else _stringToSim (tail st) (tail grid) next
121 where y = fst $ head grid -- not exactly sure why y and x got switched here
123 next = simSet acc (charToChunk $ head st) x y
125 -- maps each chunktype to an ascii character
126 chunkToChar :: ChunkData -> Char
136 -- this is redundant, fix this somehow
137 charToChunk :: Char -> ChunkData
139 ChunkData { chunkType=ctype }
140 where ctype = if c=='~' then Water
141 else if c=='#' then Wall
142 else if c=='@' then Pump
143 else if c=='O' then Drain