aboutsummaryrefslogtreecommitdiff
blob: 5ce2d4cca321d3377c28ee342b02d7193738f2b2 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module Battlemap.Navigator.Path exposing
   (
      Type,
      new,
      get_current_location,
      get_remaining_points,
      follow_directions
   )

import Set

import Battlemap.Direction
import Battlemap.Location
import Battlemap.Tile

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Type =
   {
      current_location : Battlemap.Location.Type,
      visited_locations : (Set.Set Battlemap.Location.Ref),
      previous_directions : (List Battlemap.Direction.Type),
      remaining_points : Int
   }

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
has_not_been_to : (
      Type ->
      Battlemap.Location.Type ->
      Bool
   )
has_not_been_to path location =
   (
      (path.current_location /= location)
      &&
      (not
         (Set.member
            (Battlemap.Location.get_ref location)
            path.visited_locations
         )
      )
   )

move_to : (
      Type ->
      Battlemap.Direction.Type ->
      Battlemap.Location.Type ->
      Int ->
      Type
   )
move_to path dir next_loc cost =
   {path |
      current_location = next_loc,
      visited_locations =
         (Set.insert
            (Battlemap.Location.get_ref path.current_location)
            path.visited_locations
         ),
      previous_directions = (dir :: path.previous_directions),
      remaining_points = (path.remaining_points - cost)
   }

battlemap_backtrack : (
      Battlemap.Type ->
      Battlemap.Location.Type ->
      Battlemap.Type
   )
battlemap_backtrack battlemap current_loc =
   (Battlemap.apply_to_tile_unsafe
      battlemap
      current_loc
      (Battlemap.Tile.set_direction
         Battlemap.Direction.None
      )
   )

navigator_backtrack : (
      Battlemap.Navigator.Type ->
      Battlemap.Location.Type ->
      (List Battlemap.Direction.Type) ->
      Battlemap.Navigator.Type
   )
try_backtracking_to path location dir =
               case (Util.List.pop nav.previous_directions) of
                     (Just (head, tail)) ->
                        if (head == (Battlemap.Direction.opposite_of dir))
                        then
                           (backtrack_to
                              nav
                              next_location
                              tail
                           )
                           )
                        else
                           (battlemap, nav)
                     Nothing -> (battlemap, nav)
               move_to path next_location
               if (can_move_to_new_tile path next_location)
               then
               else
   {nav |
      current_location = next_loc,
      visited_locations =
         (Set.remove
            (Battlemap.Location.get_ref next_loc)
            nav.visited_locations
         ),
      previous_directions = prev_dir_tail,
      remaining_points = (nav.remaining_points + 1)
   }


to : (
      Type ->
      Battlemap.Direction.Type ->
      (Battlemap.Type, Battlemap.Navigator.Type)
   )
to battlemap nav dir char_list =

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------

new : Battlemap.Location.Type -> Int -> Type
new start points =
   {
      current_location = start,
      visited_locations = Set.empty,
      previous_directions = [],
      remaining_points = points
   }

get_current_location : Type -> Battlemap.Location.Type
get_current_location path = path.current_location

get_remaining_points : Type -> Int
get_remaining_points path = path.remaining_points

follow_direction : (
      (Battlemap.Location.Type -> Bool) ->
      (Maybe Type) ->
      Battlemap.Direction.Type ->
      (Maybe Type)
   )
follow_direction can_cross cost_fun maybe_path dir =
   case maybe_path of
      (Just path) ->
         let
            next_location =
               (Battlemap.Location.neighbor
                  nav.current_location
                  dir
               )
         in
            if (can_cross path next_location)
            then
               if (has_not_been_to path next_location)
               then
                  (Just (move_to path next_location dir))
               else
                  (try_backtracking_to path next_location dir)
            else
               Nothing
            else
               (battlemap, nav)

      Nothing -> Nothing