X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=Sim.hs;h=26fc5f16b029ab54f55618d0f33d3b3543375ff7;hb=699b8ddd72aca8569a63b1e37a0265428c534bc9;hp=e96816ed412b0bd102408c06d87bfca524eb86d8;hpb=bdbd06560f50a45aa12c15d4fefa0e3c303fbd7a;p=ekitaihs.git diff --git a/Sim.hs b/Sim.hs index e96816e..26fc5f1 100644 --- a/Sim.hs +++ b/Sim.hs @@ -1,40 +1,90 @@ -module Sim ( initSimSpace, testSim, physStep ) where +module Sim ( + Simulation, simSpace, + ChunkType, ChunkData, chunkType, + initSimSpace, testSim, physStep, updateWaterChunk, validDirects, simToString +) where +import Debug.Trace import Data.Vector ((!), (//)) import qualified Data.Vector as V --- Simulation simSpace simW simH -data Simulation = Simulation (V.Vector ChunkData) Int Int deriving (Show) +data Simulation = Simulation + { simSpace :: V.Vector ChunkData + , simW :: Int + , simH :: Int + } deriving (Show) -data ChunkType = Empty +data ChunkType = Air | Water - | Wall deriving (Show) + | Wall deriving (Show, Enum) -data ChunkData = ChunkData ChunkType deriving (Show) +data ChunkData = ChunkData + { chunkType :: ChunkType + } deriving (Show) -- vec accessors -veccGet v w i = v ! i -simGet (Simulation s w _) i = veccGet s w i +simGet Simulation{simSpace=s,simW=w} x y = s ! (y*w+x) +simSet sim@Simulation{simSpace=s,simW=w,simH=h} c x y = sim { simSpace = (s // [(y*w+x,c)]) } +simGetChunkType sim x y = chunkType $ simGet sim x y -veccSet v w c i = v // [(i,c)] -simSet (Simulation s w h) c i = Simulation (veccSet s w c i) w h +initSimSpace x y = Simulation + { simSpace = V.replicate (y*x) ChunkData { chunkType=Air } + , simW = x + , simH = y + } -initSimSpace x y = Simulation (V.replicate (y*x) (ChunkData Empty)) x y +testSim = simSet (initSimSpace 3 3) (ChunkData Water) 1 1 -testSim = simSet (initSimSpace 10 10) (ChunkData Water) 5 +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} = + if null grid then acc + else _physStep (tail grid) next sim + where x = fst $ head grid + y = snd $ head grid + valid = validDirects x y w h + next = case simGetChunkType sim x y of + Water -> updateWaterChunk x y valid acc + _ -> acc -physStep sim@(Simulation _ w h) = _physStep 0 (initSimSpace w h) sim -_physStep i acc sim@(Simulation s w h) = - if i >= w*h then acc - else _physStep (i+1) next sim - where - getChunkData (ChunkData c) = c - next = case getChunkData $ simGet sim i of - Water -> sim - _ -> sim +-- takes in list of valid chunks, as well as the sim to modify +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 + 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 + 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 + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } (x+1) (y-1) + -- stay put + else sim --- initGaussSeidel s = --- where h = length s --- w = length (s V.! 0) +-- gets chunks around a given chunk that are inside grid +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)] --- gaussSeidel +simToString :: Simulation -> [Char] +simToString sim@Simulation{simW=w} = + let simStr = V.toList $ V.map chunkToChar $ simSpace sim + in insert w '\n' simStr + +-- from https://stackoverflow.com/questions/12659562/insert-specific-element-y-after-every-n-elements-in-a-list +insert :: Int -> a -> [a] -> [a] +insert n y xs = countdown n xs where + countdown 0 xs = y:countdown n xs + countdown _ [] = [] + countdown m (x:xs) = x:countdown (m-1) xs + +chunkToChar :: ChunkData -> Char +chunkToChar c = + case chunkType c of + Water -> '~' + Air -> '.' + _ -> '?'