summaryrefslogtreecommitdiff
blob: 924f71591a7fb4d0d773706bea5fb65e2d0c42a4 (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
module Battlemap.Navigator.Move exposing (to)

import Set
import List

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

import Character

import Util.List

can_move_to_new_tile : (
      Battlemap.Navigator.Type ->
      Battlemap.Type ->
      Battlemap.Location.Type ->
      Bool
   )
can_move_to_new_tile nav battlemap next_location =
   (
      (nav.remaining_points > 0)
      && (Battlemap.has_location battlemap next_location)
      && (nav.current_location /= next_location)
      &&
      (not
         (Set.member
            (Battlemap.Location.get_ref next_location)
            nav.visited_locations
         )
      )
   )

battlemap_move_to : (
      Battlemap.Type ->
      Battlemap.Location.Type ->
      Battlemap.Direction.Type ->
      Battlemap.Location.Type ->
      Battlemap.Type
   )
battlemap_move_to battlemap current_loc dir next_loc =
   (Battlemap.apply_to_tile_unsafe
      (Battlemap.apply_to_tile_unsafe
         battlemap
         current_loc
         (Battlemap.Tile.set_direction dir)
      )
      next_loc
      (Battlemap.Tile.set_direction dir)
   )

navigator_move_to : (
      Battlemap.Navigator.Type ->
      Battlemap.Direction.Type ->
      Battlemap.Location.Type ->
      Battlemap.Navigator.Type
   )
navigator_move_to nav dir next_loc =
   {nav |
      current_location = next_loc,
      visited_locations =
         (Set.insert
            (Battlemap.Location.get_ref nav.current_location)
            nav.visited_locations
         ),
      previous_directions = (dir :: nav.previous_directions),
      remaining_points = (nav.remaining_points - 1)
   }

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
   )
navigator_backtrack nav next_loc prev_dir_tail =
   {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 : (
      Battlemap.Type ->
      Battlemap.Navigator.Type ->
      Battlemap.Direction.Type ->
      (List Character.Type) ->
      (Battlemap.Type, Battlemap.Navigator.Type)
   )
to battlemap nav dir char_list =
   let
      next_location = (Battlemap.Location.neighbor nav.current_location dir)
      is_occupied = (List.any (\c -> (c.location == next_location)) char_list)
   in
      if (not is_occupied)
      then
         if (can_move_to_new_tile nav battlemap next_location)
         then
            (
               (battlemap_move_to
                  battlemap
                  nav.current_location
                  dir
                  next_location
               ),
               (navigator_move_to
                  nav
                  dir
                  next_location
               )
            )
         else
            case (Util.List.pop nav.previous_directions) of
               Nothing -> (battlemap, nav)
               (Just (head, tail)) ->
                  if (head == (Battlemap.Direction.opposite_of dir))
                  then
                     (
                        (battlemap_backtrack
                           battlemap
                           nav.current_location
                        ),
                        (navigator_backtrack
                           nav
                           next_location
                           tail
                        )
                     )
                  else
                     (battlemap, nav)
      else
         (battlemap, nav)