module Sim (
Simulation, simSpace,
ChunkType, ChunkData, chunkType,
- initSimSpace, testSim, physStep, updateWaterChunk, validDirects, simToString
+ initSimSpace, testSim, physStep, validDirects,
+ simToString, stringToSim
) where
import Debug.Trace
, simH = y
}
-testSim = simSet (initSimSpace 3 3) (ChunkData Water) 1 1
+testSim = let a = simSet (initSimSpace 10 10) (ChunkData Water) 5 5
+ in simSet a (ChunkData Wall) 5 6
physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] sim sim
--- _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} | trace ("" ++ show acc) False = undefined
+-- _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} | trace ("" ++ show h) False = undefined
_physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} =
if null grid then acc
else _physStep (tail grid) next sim
next = case simGetChunkType sim x y of
Water -> updateWaterChunk x y valid acc
_ -> acc
+ -- spawn = simSet next (ChunkData Water) 5 0
-- takes in list of valid chunks, as well as the sim to modify
+-- updateWaterChunk x y valid sim | trace ("x:" ++ show x ++ " y:" ++ show y ++ " " ++ show valid) False = undefined
updateWaterChunk x y valid sim =
-- if the chunk below is free, fall straight down
- if elem (0,-1) valid && fromEnum (simGetChunkType sim x (y-1)) == fromEnum Air
+ if elem (0,1) valid && fromEnum (simGetChunkType sim x (y+1)) == fromEnum Air
then
let moved = simSet sim ChunkData { chunkType=Air } x y
- in simSet moved ChunkData { chunkType=Water } x (y-1)
- else if elem (-1,-1) valid && fromEnum (simGetChunkType sim (x-1) (y-1)) == fromEnum Air
+ in simSet moved ChunkData { chunkType=Water } x (y+1)
+ else if elem (-1,1) valid && fromEnum (simGetChunkType sim (x-1) (y+1)) == fromEnum Air
then
let moved = simSet sim ChunkData { chunkType=Air } x y
- in simSet moved ChunkData { chunkType=Water } (x-1) (y-1)
- else if elem (1,-1) valid && fromEnum (simGetChunkType sim (x+1) (y-1)) == fromEnum Air
+ in simSet moved ChunkData { chunkType=Water } (x-1) (y+1)
+ else if elem (1,1) valid && fromEnum (simGetChunkType sim (x+1) (y+1)) == fromEnum Air
then
let moved = simSet sim ChunkData { chunkType=Air } x y
- in simSet moved ChunkData { chunkType=Water } (x+1) (y-1)
+ in simSet moved ChunkData { chunkType=Water } (x+1) (y+1)
+ else if elem (-1,0) valid && fromEnum (simGetChunkType sim (x-1) y) == fromEnum Air
+ then
+ let moved = simSet sim ChunkData { chunkType=Air } x y
+ in simSet moved ChunkData { chunkType=Water } (x-1) y
+ else if elem (1,0) valid && fromEnum (simGetChunkType sim (x+1) y) == fromEnum Air
+ then
+ let moved = simSet sim ChunkData { chunkType=Air } x y
+ in simSet moved ChunkData { chunkType=Water } (x+1) y
-- stay put
else sim
-- gets chunks around a given chunk that are inside grid
+-- validDirects x y w h | trace ("w:"++ show w ++ "h:" ++ show h) False = undefined
validDirects x y w h = filter
- (\q -> 0 <= (fst q) && (fst q) < w && (snd q) <= 0 && (snd q) < h)
- [(a-x,b-y) | a <- [x-1..x+1], b <- [y-1..y+1], not (a==x && b==y)]
+ (\q -> 0 <= (fst q)+x && (fst q)+x < w && 0 <= (snd q)+y && (snd q)+y < h)
+ [(a,b) | a <- [-1..1], b <- [-1..1], not (a==0 && b==0)]
simToString :: Simulation -> [Char]
simToString sim@Simulation{simW=w} =
countdown _ [] = []
countdown m (x:xs) = x:countdown (m-1) xs
+-- stringToSim :: [String] -> Simulation
+stringToSim strings =
+ _stringToSim st grid (initSimSpace w h)
+ where stripped = strings
+ w = maximum $ [(length s) | s <- stripped]
+ h = length stripped
+ grid = [(a,b) | a <- [0..(length stripped)-1], b <- [0..(length $ stripped !! a)-1]]
+ st = concat stripped
+
+_stringToSim st grid acc =
+ if null grid || null st then acc
+ else _stringToSim (tail st) (tail grid) next
+ where y = fst $ head grid -- not exactly sure why y and x got switched here
+ x = snd $ head grid
+ next = simSet acc (charToChunk $ head st) x y
+
+-- maps each chunktype to an ascii character
chunkToChar :: ChunkData -> Char
chunkToChar c =
case chunkType c of
Water -> '~'
Air -> '.'
+ Wall -> '#'
_ -> '?'
+
+-- this is redundant, fix this somehow
+charToChunk :: Char -> ChunkData
+charToChunk c =
+ ChunkData { chunkType=ctype }
+ where ctype = if c=='~' then Water
+ else if c=='#' then Wall
+ else Air
+