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
|
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,
error,
solve,
set_location_from_index,
decoder,
encode
)
-- Elm -------------------------------------------------------------------------
import Dict
import Json.Encode
import Json.Decode
import Json.Decode.Pipeline
-- Battle Map ------------------------------------------------------------------
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,
triggers : (List 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),
triggers = [],
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,
triggers = [],
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 : (
(Dict.Dict BattleMap.Struct.Tile.Ref BattleMap.Struct.Tile.Type) ->
Type ->
Type
)
solve tiles tile_inst =
case (Dict.get tile_inst.class_id tiles) of
(Just tile) ->
{tile_inst |
crossing_cost = (BattleMap.Struct.Tile.get_cost tile),
family = (BattleMap.Struct.Tile.get_family tile)
}
Nothing ->
{tile_inst |
crossing_cost = -1,
family = "-1"
}
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.required
"t"
(Json.Decode.list (Json.Decode.string))
)
|>
(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) tile_inst.triggers)
)
]
)
|