summaryrefslogtreecommitdiff
blob: b0406768077c352a2d00efd6c1f486fbbde454c9 (plain)
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
module Battlemap.Navigator exposing (Navigator, new_navigator, go)

import Set exposing (Set, member, empty, insert)

import Battlemap exposing (Battlemap, has_location, apply_to_tile)
import Battlemap.Location exposing (..)
import Battlemap.Direction exposing (..)
import Battlemap.Tile exposing (set_direction)

type alias Navigator =
   {
      current_location : Location,
      visited_locations : (Set LocationComparable)
   }

new_navigator : Location -> Navigator
new_navigator start =
   {
      current_location = start,
      visited_locations = empty
   }

go : Battlemap -> Navigator -> Direction -> (Battlemap, Navigator)
go battlemap nav dir =
   let
      next_location = (neighbor nav.current_location dir)
   in
      if
      (
         (has_location battlemap next_location)
         && (nav.current_location /= next_location)
         && (not (member (to_comparable next_location) nav.visited_locations))
      )
      then
         (
            (case
               (apply_to_tile
                  battlemap
                  nav.current_location
                  (set_direction dir)
               )
               of
                  Nothing -> battlemap
                  (Just bmap) -> bmap
            ),
            {
               current_location = next_location,
               visited_locations =
                  (insert
                     (to_comparable nav.current_location)
                     nav.visited_locations
                  )
            }
         )
      else
         (
            battlemap,
            nav
         )