summaryrefslogtreecommitdiff
blob: d26a56ab3f2f479cac18ba6cbfc051957885d35d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module Battlemap.Navigator exposing
   (
      Navigator,
      new_navigator,
      reset_navigation,
      go
   )

import Set exposing (Set, member, empty, insert, remove)
import List as Lt exposing (head, tail)

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

import Battlemap.Location exposing
   (
      Location,
      LocationRef,
      neighbor,
      to_comparable
   )

type alias Navigator =
   {
      current_location : Location,
      visited_locations : (Set LocationRef),
      previous_directions : (List Direction)
   }

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


reset_navigation : Tile -> Tile
reset_navigation t =
   {t |
      nav_level = None
   }

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
            ),
            {nav |
               current_location = next_location,
               visited_locations =
                  (insert
                     (to_comparable nav.current_location)
                     nav.visited_locations
                  ),
               previous_directions = (dir :: nav.previous_directions)
            }
         )
      else
         case
            (
               (Lt.head nav.previous_directions),
               (Lt.tail nav.previous_directions)
            )
         of
            (Nothing, _) -> (battlemap, nav)
            (_ , Nothing) -> (battlemap, nav)
            ((Just prev_dir), (Just prev_dir_list)) ->
               if (dir == (opposite_of prev_dir))
               then
                  (
                     (case
                        (apply_to_tile
                           battlemap
                           next_location
                           (set_direction None)
                        )
                        of
                           Nothing -> battlemap
                           (Just bmap) -> bmap
                     ),
                     {nav |
                        current_location = next_location,
                        visited_locations =
                           (remove
                              (to_comparable next_location)
                              nav.visited_locations
                           ),
                        previous_directions = prev_dir_list
                     }
                  )
               else
                  (battlemap, nav)