summaryrefslogtreecommitdiff
blob: 550169b97b11e0de119258b1b774da1863f8830b (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
module Struct.Tile exposing
   (
      Ref,
      Type,
      Instance,
      new,
      new_instance,
      error_tile_instance,
      get_id,
      get_name,
      get_range_minimum,
      get_range_maximum,
      get_cost,
      get_instance_cost,
      get_location,
      get_icon_id,
      get_type_id,
      get_variant_id,
      solve_tile_instance,
      decoder
   )

-- Elm -------------------------------------------------------------------------
import List

import Json.Decode
import Json.Decode.Pipeline

-- Map -------------------------------------------------------------------
import Constants.UI
import Constants.Movement

import Struct.Location

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Ref = Int

type alias PartiallyDecoded =
   {
      id : Int,
      nam : String,
      ct : Int,
      rmi : Int,
      rma : Int
   }

type alias Type =
   {
      id : Int,
      name : String,
      crossing_cost : Int,
      range_minimum : Int,
      range_maximum : Int
   }

type alias Instance =
   {
      location : Struct.Location.Type,
      icon_id : Int,
      crossing_cost : Int,
      type_id : Int
   }

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

finish_decoding : PartiallyDecoded -> Type
finish_decoding add_tile =
   {
      id = add_tile.id,
      name = add_tile.nam,
      crossing_cost = add_tile.ct,
      range_minimum = add_tile.rmi,
      range_maximum = add_tile.rma
   }

seek_tile_instance_type : Instance -> Type -> (Maybe Type) -> (Maybe Type)
seek_tile_instance_type instance candidate current_sol =
   if (current_sol == Nothing)
   then
      let
         icon_id = instance.icon_id
      in
         if
         (
            (icon_id >= candidate.range_minimum)
            && (icon_id <= candidate.range_maximum)
         )
         then
            (Just candidate)
         else
            current_sol
   else
      current_sol

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
new : Int -> String -> Int -> Int -> Int -> Type
new id name crossing_cost range_minimum range_maximum =
   {
      id = id,
      name = name,
      crossing_cost = crossing_cost,
      range_minimum = range_minimum,
      range_maximum = range_maximum
   }

new_instance : Int -> Int -> Int -> Int -> Int -> Instance
new_instance x y icon_id crossing_cost type_id =
   {
      location = {x = x, y = y},
      icon_id = icon_id,
      crossing_cost = crossing_cost,
      type_id = type_id
   }

error_tile_instance : Int -> Int -> Instance
error_tile_instance x y =
   {
      location = {x = x, y = y},
      icon_id = -1,
      type_id = -1,
      crossing_cost = Constants.Movement.cost_when_out_of_bounds
   }


get_id : Type -> Int
get_id tile = tile.id

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

get_instance_cost : Instance -> Int
get_instance_cost tile_inst = tile_inst.crossing_cost

get_name : Type -> String
get_name tile = tile.name

get_range_minimum : Type -> Int
get_range_minimum tile = tile.range_minimum

get_range_maximum : Type -> Int
get_range_maximum tile = tile.range_maximum

get_location : Instance -> Struct.Location.Type
get_location tile_inst = tile_inst.location

get_icon_id : Instance -> String
get_icon_id tile_inst = (toString tile_inst.icon_id)

get_type_id: Instance -> Int
get_type_id tile_inst = tile_inst.type_id

get_variant_id : Instance -> Int
get_variant_id tile_inst =
   (
      (noise_function
         tile_inst.location.x
         tile_inst.location.y
         tile_inst.crossing_cost
      )
      % Constants.UI.variants_per_tile
   )

solve_tile_instance : (List Type) -> Instance -> Instance
solve_tile_instance tiles tile_instance =
   let
      maybe_type =
         (List.foldr (seek_tile_instance_type tile_instance) Nothing tiles)
   in
      case maybe_type of
         (Just tile) ->
            {tile_instance |
               type_id = tile.id,
               crossing_cost = tile.crossing_cost
            }

         Nothing ->
            (error_tile_instance
               tile_instance.location.x
               tile_instance.location.y
            )

decoder : (Json.Decode.Decoder Type)
decoder =
   (Json.Decode.map
      (finish_decoding)
      (Json.Decode.Pipeline.decode
         PartiallyDecoded
         |> (Json.Decode.Pipeline.required "id" Json.Decode.int)
         |> (Json.Decode.Pipeline.required "nam" Json.Decode.string)
         |> (Json.Decode.Pipeline.required "ct" Json.Decode.int)
         |> (Json.Decode.Pipeline.required "rmi" Json.Decode.int)
         |> (Json.Decode.Pipeline.required "rma" Json.Decode.int)
      )
   )