summaryrefslogtreecommitdiff
blob: 41593c5c93622dd7be47210b254b9c112907664c (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
module Battlemap.Navigator exposing
   (
      Type,
      new_navigator,
      reset_navigation,
      go
   )

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

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

import Character

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

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


reset_navigation : Battlemap.Tile.Type -> Battlemap.Tile.Type
reset_navigation t =
   {t |
      nav_level = Battlemap.Direction.None
   }

go : (
      Battlemap.Type ->
      Type ->
      Battlemap.Direction.Type ->
      (List Character.Type) ->
      (Battlemap.Type, Type)
   )
go 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)
         && (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
            )
         )
      )
      then
         (
            (case
               (Battlemap.apply_to_tile
                  battlemap
                  nav.current_location
                  (Battlemap.Tile.set_direction dir)
               )
               of
                  Nothing -> battlemap
                  (Just bmap0) ->
                     (case
                        (Battlemap.apply_to_tile
                           bmap0
                           next_location
                           (Battlemap.Tile.set_direction dir)
                        )
                     of
                        Nothing -> battlemap
                        (Just bmap1) -> bmap1
                     )
            ),
            {nav |
               current_location = next_location,
               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)
            }
         )
      else if (not is_occupied)
      then
         case
            (
               (List.head nav.previous_directions),
               (List.tail nav.previous_directions)
            )
         of
            (Nothing, _) -> (battlemap, nav)
            (_ , Nothing) -> (battlemap, nav)
            ((Just prev_dir), (Just prev_dir_list)) ->
               if (dir == (Battlemap.Direction.opposite_of prev_dir))
               then
                  (
                     (case
                        (Battlemap.apply_to_tile
                           battlemap
                           nav.current_location
                           (Battlemap.Tile.set_direction
                              Battlemap.Direction.None
                           )
                        )
                        of
                           Nothing -> battlemap
                           (Just bmap) -> bmap
                     ),
                     {nav |
                        current_location = next_location,
                        visited_locations =
                           (Set.remove
                              (Battlemap.Location.get_ref next_location)
                              nav.visited_locations
                           ),
                        previous_directions = prev_dir_list,
                        remaining_points = (nav.remaining_points + 1)
                     }
                  )
               else
                  (battlemap, nav)
      else
         (battlemap, nav)