summaryrefslogtreecommitdiff
blob: 8ffeafcfd34843c127f4ad1418b6ddb00b4c1748 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
module BattleMap.Struct.TileInstance exposing
   (
      Type,
      Border,
      clone,
      get_location,
      get_class_id,
      get_family,
      get_cost,
      default,
      set_borders,
      get_borders,
      new_border,
      get_variant_id,
      get_border_variant_id,
      get_border_class_id,
      get_local_variant_ix,
--      remove_status_indicator,
--      add_status_indicator,
--      get_status_indicators,
      remove_tag,
      add_tag,
      get_tags,
      error,
      solve,
      set_location_from_index,
      add_extra_display_effect,
      remove_extra_display_effect,
      get_extra_display_effects,
      get_extra_display_effects_list,
      reset_extra_display_effects,
      decoder,
      encode
   )

-- Elm -------------------------------------------------------------------------
import Dict

import Set

import Json.Encode

import Json.Decode
import Json.Decode.Pipeline

-- Shared ----------------------------------------------------------------------
import Shared.Util.Set

-- Battle Map ------------------------------------------------------------------
import BattleMap.Struct.DataSet
import BattleMap.Struct.Tile
import BattleMap.Struct.Location

-- Local -----------------------------------------------------------------------
import Constants.UI
import Constants.Movement

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Type =
   {
      location : BattleMap.Struct.Location.Type,
      crossing_cost : Int,
      family : BattleMap.Struct.Tile.FamilyID,
      class_id : BattleMap.Struct.Tile.Ref,
      variant_id : BattleMap.Struct.Tile.VariantID,
      tags : (Set.Set String),
      extra_display_effects : (Set.Set String),
      borders : (List Border)
   }

type alias Border =
   {
      class_id : BattleMap.Struct.Tile.Ref,
      variant_id : BattleMap.Struct.Tile.VariantID
   }

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
noise_function : Int -> Int -> Int -> Int
noise_function a b c =
   (round (radians (toFloat ((a + 1) * 2 + (b + 1) * 3 + c))))

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
clone : BattleMap.Struct.Location.Type -> Type -> Type
clone loc inst = {inst | location = loc}

new_border : (
      BattleMap.Struct.Tile.Ref ->
      BattleMap.Struct.Tile.VariantID ->
      Border
   )
new_border class_id variant_id =
   {
      class_id = class_id,
      variant_id = variant_id
   }

default : BattleMap.Struct.Tile.Type -> Type
default tile =
   {
      location = {x = 0, y = 0},
      class_id = (BattleMap.Struct.Tile.get_id tile),
      variant_id = "0",
      crossing_cost = (BattleMap.Struct.Tile.get_cost tile),
      family = (BattleMap.Struct.Tile.get_family tile),
      tags = (Set.empty),
      extra_display_effects = (Set.empty),
      borders = []
   }

error : Int -> Int -> Type
error x y =
   {
      location = {x = x, y = y},
      class_id = "0",
      variant_id = "0",
      family = "0",
      crossing_cost = Constants.Movement.cost_when_out_of_bounds,
      tags = (Set.empty),
      extra_display_effects = (Set.empty),
      borders = []
   }

get_class_id : Type -> BattleMap.Struct.Tile.Ref
get_class_id inst = inst.class_id

get_cost : Type -> Int
get_cost inst = inst.crossing_cost

get_location : Type -> BattleMap.Struct.Location.Type
get_location inst = inst.location

get_family : Type -> BattleMap.Struct.Tile.FamilyID
get_family inst = inst.family

set_borders : (List Border) -> Type -> Type
set_borders borders tile_inst = {tile_inst | borders = borders}

get_borders : Type -> (List Border)
get_borders tile_inst = tile_inst.borders

get_variant_id : Type -> BattleMap.Struct.Tile.VariantID
get_variant_id tile_inst = tile_inst.variant_id

get_border_variant_id : Border -> BattleMap.Struct.Tile.VariantID
get_border_variant_id tile_border = tile_border.variant_id

get_local_variant_ix : Type -> Int
get_local_variant_ix tile_inst =
   (modBy
      Constants.UI.local_variants_per_tile
      (noise_function
         tile_inst.location.x
         tile_inst.location.y
         tile_inst.crossing_cost
      )
   )

solve : BattleMap.Struct.DataSet.Type -> Type -> Type
solve dataset tile_inst =
   let tile = (BattleMap.Struct.DataSet.get_tile tile_inst.class_id dataset) in
      {tile_inst |
         crossing_cost = (BattleMap.Struct.Tile.get_cost tile),
         family = (BattleMap.Struct.Tile.get_family tile)
      }

list_to_borders : (
      (List String) ->
      (List Border) ->
      (List Border)
   )
list_to_borders list borders =
   case list of
      (a :: (b :: c)) ->
         (list_to_borders
            c
            ({ class_id = a, variant_id = b } :: borders)
         )
      _ -> (List.reverse borders)

decoder : (Json.Decode.Decoder Type)
decoder =
   (Json.Decode.andThen
      (\tile_data ->
         case tile_data of
            (tile_id :: (variant_id :: borders)) ->
               (Json.Decode.succeed
                  Type
                  |> (Json.Decode.Pipeline.hardcoded {x = 0, y = 0}) -- Location
                  |> (Json.Decode.Pipeline.hardcoded 0) -- Crossing Cost
                  |> (Json.Decode.Pipeline.hardcoded "") -- Family
                  |> (Json.Decode.Pipeline.hardcoded tile_id)
                  |> (Json.Decode.Pipeline.hardcoded variant_id)
                  |> (Json.Decode.Pipeline.hardcoded (Set.empty)) -- tags
                  |> (Json.Decode.Pipeline.hardcoded (Set.empty)) -- display_effects
                  |>
                     (Json.Decode.Pipeline.hardcoded
                        (list_to_borders borders [])
                     )
               )
            _ -> (Json.Decode.succeed (error 0 0))
      )
      (Json.Decode.field "b" (Json.Decode.list (Json.Decode.string)))
   )

get_border_class_id : Border -> BattleMap.Struct.Tile.Ref
get_border_class_id tile_border = tile_border.class_id

set_location_from_index : Int -> Int -> Type -> Type
set_location_from_index map_width index tile_inst =
   {tile_inst |
      location =
            {
               x = (modBy map_width index),
               y = (index // map_width)
            }
   }

encode : Type -> Json.Encode.Value
encode tile_inst =
   (Json.Encode.object
      [
         (
            "b",
            (Json.Encode.list
               (Json.Encode.string)
               (
                  tile_inst.class_id
                  ::
                  (
                     tile_inst.variant_id
                     ::
                     (List.concatMap
                        (\border ->
                           [
                              border.class_id,
                              border.variant_id
                           ]
                        )
                        tile_inst.borders
                     )
                  )
               )
            )
         ),
         (
            "t",
            (Json.Encode.list
               (Json.Encode.string)
               (Set.toList tile_inst.tags)
            )
         )
      ]
   )

get_tags : Type -> (Set.Set String)
get_tags tile_inst = tile_inst.tags

add_tag : String -> Type -> Type
add_tag tag tile_inst =
   {tile_inst |
      tags = (Set.insert tag tile_inst.tags)
   }

remove_tag : String -> Type -> Type
remove_tag tag tile_inst =
   {tile_inst |
      tags = (Set.remove tag tile_inst.tags)
   }

add_extra_display_effect : String -> Type -> Type
add_extra_display_effect effect_name tile =
   {tile |
      extra_display_effects =
         (Set.insert effect_name tile.extra_display_effects)
   }

toggle_extra_display_effect : String -> Type -> Type
toggle_extra_display_effect effect_name tile =
   {tile |
      extra_display_effects =
         (Shared.Util.Set.toggle effect_name tile.extra_display_effects)
   }

remove_extra_display_effect : String -> Type -> Type
remove_extra_display_effect effect_name tile =
   {tile |
      extra_display_effects =
         (Set.remove effect_name tile.extra_display_effects)
   }

get_extra_display_effects : Type -> (Set.Set String)
get_extra_display_effects tile = tile.extra_display_effects

get_extra_display_effects_list : Type -> (List String)
get_extra_display_effects_list tile = (Set.toList tile.extra_display_effects)

reset_extra_display_effects : Type -> Type
reset_extra_display_effects tile = {tile | extra_display_effects = (Set.empty)}