X-Git-Url: https://git.danieliu.xyz/?p=ekitaihs.git;a=blobdiff_plain;f=Sim.hs;fp=Sim.hs;h=26fc5f16b029ab54f55618d0f33d3b3543375ff7;hp=c48dbe4d2194456d72af2cce1db1f8ae925c9df9;hb=699b8ddd72aca8569a63b1e37a0265428c534bc9;hpb=c68579ccc2b03996eec6ac5c1526d6e5d20c3c78 diff --git a/Sim.hs b/Sim.hs index c48dbe4..26fc5f1 100644 --- a/Sim.hs +++ b/Sim.hs @@ -1,4 +1,8 @@ -module Sim ( initSimSpace, testSim, physStep, updateWaterChunk, validDirects ) where +module Sim ( + Simulation, simSpace, + ChunkType, ChunkData, chunkType, + initSimSpace, testSim, physStep, updateWaterChunk, validDirects, simToString +) where import Debug.Trace import Data.Vector ((!), (//)) @@ -66,3 +70,21 @@ 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)] +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 -> '.' + _ -> '?'