summaryrefslogtreecommitdiff
blob: d3243c2af7158148f00c62ae1918b758d684e2cd (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.Struct.Location exposing (..)

-- Elm -------------------------------------------------------------------------
import Json.Decode
import Json.Decode.Pipeline

import Json.Encode

import Set

-- Battle Map ------------------------------------------------------------------
import BattleMap.Struct.Direction

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Type =
   {
      x : Int,
      y : Int
   }

type alias Ref = (Int, Int)

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
new : Int -> Int -> Type
new x y =
   {
      x = x,
      y = y
   }

neighbor : BattleMap.Struct.Direction.Type -> Type -> Type
neighbor dir loc =
   case dir of
      BattleMap.Struct.Direction.Right -> {loc | x = (loc.x + 1)}
      BattleMap.Struct.Direction.Left -> {loc | x = (loc.x - 1)}
      BattleMap.Struct.Direction.Up -> {loc | y = (loc.y - 1)}
      BattleMap.Struct.Direction.Down -> {loc | y = (loc.y + 1)}
      BattleMap.Struct.Direction.None -> loc

get_ref : Type -> Ref
get_ref l =
   (l.x, l.y)

from_ref : Ref -> Type
from_ref (x, y) =
   {x = x, y = y}

dist : Type -> Type -> Int
dist loc_a loc_b =
   (
      (abs (loc_a.x - loc_b.x))
      +
      (abs (loc_a.y - loc_b.y))
   )

decoder : (Json.Decode.Decoder Type)
decoder =
   (Json.Decode.succeed
      Type
      |> (Json.Decode.Pipeline.required "x" Json.Decode.int)
      |> (Json.Decode.Pipeline.required "y" Json.Decode.int)
   )

encode : Type -> Json.Encode.Value
encode loc =
   (Json.Encode.object
      [
         ( "x", (Json.Encode.int loc.x) ),
         ( "y", (Json.Encode.int loc.y) )
      ]
   )

neighbors : Type -> (List Type)
neighbors loc =
   [
      {loc | x = (loc.x + 1)},
      {loc | x = (loc.x - 1)},
      {loc | y = (loc.y - 1)},
      {loc | y = (loc.y + 1)}
   ]

get_full_neighborhood : Type -> (List Type)
get_full_neighborhood loc =
   [
      {loc | x = (loc.x - 1), y = (loc.y - 1)},
      {loc | y = (loc.y - 1)},
      {loc | x = (loc.x + 1), y = (loc.y - 1)},
      {loc | x = (loc.x - 1)},
      {loc | x = (loc.x + 1)},
      {loc | x = (loc.x - 1), y = (loc.y + 1)},
      {loc | y = (loc.y + 1)},
      {loc | x = (loc.x + 1), y = (loc.y + 1)}
   ]

add_neighborhood_to_set : (
      Int ->
      Int ->
      Int ->
      Type ->
      (Set.Set Ref) ->
      (Set.Set Ref)
   )
add_neighborhood_to_set map_width map_height tdist loc set =
   (List.foldl
      (\height_mod current_width_result ->
         let
            abs_width_mod = (abs (tdist - (abs height_mod)))
            current_height = (loc.y + height_mod)
         in
            if ((current_height < 0) || (current_height >= map_height))
            then current_width_result
            else
               (List.foldl
                  (\width_mod current_result ->
                     let new_location_x = (loc.x + width_mod) in
                        if
                        (
                           (new_location_x < 0)
                           || (new_location_x >= map_width)
                        )
                        then current_result
                        else
                           (Set.insert
                              (new_location_x, current_height)
                              current_result
                           )
                  )
                  current_width_result
                  (List.range (-abs_width_mod) abs_width_mod)
               )
      )
      set
      (List.range (-tdist) tdist)
   )