about summary refs log tree commit diff stats
path: root/Fallen/Map.hs
blob: 96c6467db7062fe70b0a5c1183d2a73a643178a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Fallen.Map
( Map,
  emptyMap,
  dimension,
  inBounds,
  getTileAtPos,
  findTileInMap,
  updateMap,
  legalMoves,
  fillMapRect
) where
  import Fallen.Tiles
  import Fallen.Point
  import Data.List
  import Fallen.Util
  import Data.Maybe
  
  data Map = Map {
    dimension :: (Int, Int),
    mapdata :: [(Point, Tile)],
    background :: Tile
    }
  
  -- emptyMap :: Int -> Int -> Tile -> Map
  emptyMap w h t = Map { dimension=(w,h), mapdata=[], background=t }
  
  -- inBounds :: Map -> Point -> Bool
  inBounds m (x,y) = let (w,h) = dimension m in (x >= 0) && (x < w) && (y >= 0) && (y < h)
  
  -- getTileAtPos :: Map -> Point -> Tile
  --getTileAtPos m p = if (inBounds m p)
  --  then mapdata m p
  --  else background m
  getTileAtPos m p = fromMaybe (background m) $ lookup p $ mapdata m
  
  -- findTileInMap :: Map -> Tile -> [Point]
--  findTileInMap m t = filter (\p -> t == mapdata m p) rawPoints where
--    (w,h) = dimension m
--    rawPoints = [(x,y) | x <- [0..w-1], y <- [0..h-1]]
  findTileInMap m t = map fst $ filter ((== t) . snd) $ mapdata m
  
  -- updateMap updates the tiles at the given position in the map
  -- if the passed point already exists in the internal array, it is overwritten with the new point
  -- if the passed tile is the background tile, it removes the tile pair from the internal array
  -- updateMap :: Point -> Tile -> Map -> Map
  updateMap p t (Map d xs bg) = if t == bg
    then Map d (filter ((/= p) . fst) xs) bg
    else case lookup p xs of
      Just _ -> Map d (map (\(p1,t1) -> if p1 == p then (p,t) else (p1,t1)) xs) bg
      Nothing -> Map d ((p,t):xs) bg
--  updateMap p t (Map d xs bg) = Map d (redirect p t xs) bg where
--    redirect p t xs = (\p2 -> if p == p2 then t else xs p2)
  
  -- legalMoves :: Map -> Point -> [Tile] -> [Direction]
  legalMoves m p ts = map fst $ filter legal $ map tileDir directions where
    tileDir d = (d, getTileAtPos m $ stepInDirection p d)
    legal (_,t) = t `elem` ts
   
  -- fillMapRect :: Int -> Int -> Int -> Int -> Tile -> Map -> Map
  fillMapRect x y w h t (Map d xs bg) = if bg == t
	  then Map d (filter outBounds xs) bg 
	  else Map d (filter outBounds xs ++ [((px,py), t) | px <- [x..x+w-1], py <- [y..y+h-1]]) bg where
		  outBounds ((px,py),_) = (px < x) || (px >= (x+w)) || (py < y) || (py >= (y+h))
--  fillMapRect x y w h t (Map d xs bg) = Map d redirectInBounds bg where
--    redirectInBounds = (\p -> if (inBounds p) then t else xs p)
--    inBounds (px,py) = (px >= x) && (px < (x+w)) && (py >= y) && (py < (y+h))