X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=Sim.hs;h=7f1688f93001d210acf594a7e2ab28ba7529e46c;hb=d6f7f328bce90b3298241052828a35e5b9b75ad4;hp=bb96fe387c6a89a8099afb7beb79e4a8c7977b3f;hpb=19625d0c9e1d31e1966815bfefba8af794b3dba2;p=ekitaihs.git diff --git a/Sim.hs b/Sim.hs index bb96fe3..7f1688f 100644 --- a/Sim.hs +++ b/Sim.hs @@ -1,18 +1,35 @@ -module Sim ( ChunkData, initSimSpace, defaultChunkData ) where +module Sim ( initSimSpace, testSim, physStep, validDirects ) where +import Data.Vector ((!), (//)) import qualified Data.Vector as V -data ChunkData = ChunkData - { velocity :: (Float, Float) - , density :: Float - } deriving (Show) +-- Simulation simSpace simW simH +data Simulation = Simulation (V.Vector ChunkData) Int Int deriving (Show) -defaultChunkData = ChunkData - { velocity = (0,0) - , density = 0 - } +data ChunkType = Empty + | Water + | Wall deriving (Show) -initSimSpace :: Int -> Int -> V.Vector ChunkData -initSimSpace x y = V.replicate (y*x) defaultChunkData +data ChunkData = ChunkData ChunkType deriving (Show) + +-- vec accessors +simGet (Simulation s w _) x y = s ! (y*w+x) +simSet (Simulation s w h) c x y = Simulation (s // [(y*w+x,c)]) w h + +initSimSpace x y = Simulation (V.replicate (y*x) (ChunkData Empty)) x y + +testSim = simSet (initSimSpace 10 10) (ChunkData Water) 5 0 + +physStep sim@(Simulation _ w h) = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) sim +_physStep grid acc sim@(Simulation s w h) = + if null grid then acc + else _physStep (tail grid) next sim + where x = fst $ head grid + y = snd $ head grid + next = sim + +-- 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,b) | a <- [x-1..x+1], b <- [y-1..y+1], not (a==x && b==y)] --- gaussSeidel