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
|
module Battle.Struct.Omnimods exposing
(
Type,
new,
merge,
none,
apply_to_attributes,
get_attack_damage,
get_damage_sum,
get_attribute_mods,
get_attack_mods,
get_defense_mods,
get_all_mods,
scale,
decoder
)
-- Elm -------------------------------------------------------------------------
import Dict
import Json.Decode
import Json.Decode.Pipeline
-- Battle ----------------------------------------------------------------------
import Battle.Struct.Attributes
import Battle.Struct.DamageType
--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Type =
{
attributes : (Dict.Dict String Int),
attack : (Dict.Dict String Int),
defense : (Dict.Dict String Int)
}
type alias GenericMod =
{
t : String,
v : Int
}
--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
generic_mods_decoder : (Json.Decode.Decoder (Dict.Dict String Int))
generic_mods_decoder =
(Json.Decode.map
((Dict.fromList) >> (Dict.remove "none"))
(Json.Decode.list
(Json.Decode.map
(\gm -> (gm.t, gm.v))
(Json.Decode.succeed
GenericMod
|> (Json.Decode.Pipeline.required "t" Json.Decode.string)
|> (Json.Decode.Pipeline.required "v" Json.Decode.int)
)
)
)
)
merge_mods : (
(Dict.Dict String Int) ->
(Dict.Dict String Int) ->
(Dict.Dict String Int)
)
merge_mods a_mods b_mods =
(Dict.merge
(Dict.insert)
(\t -> \v_a -> \v_b -> \r -> (Dict.insert t (v_a + v_b) r))
(Dict.insert)
a_mods
b_mods
(Dict.empty)
)
scale_dict_value : Float -> String -> Int -> Int
scale_dict_value modifier entry_name value =
(ceiling ((toFloat value) * modifier))
--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
decoder : (Json.Decode.Decoder Type)
decoder =
(Json.Decode.succeed
Type
|> (Json.Decode.Pipeline.required "attm" generic_mods_decoder)
|> (Json.Decode.Pipeline.required "atkm" generic_mods_decoder)
|> (Json.Decode.Pipeline.required "defm" generic_mods_decoder)
)
new : (
(List (String, Int)) ->
(List (String, Int)) ->
(List (String, Int)) ->
Type
)
new attribute_mods attack_mods defense_mods =
{
attributes = (Dict.fromList attribute_mods),
attack = (Dict.fromList attack_mods),
defense = (Dict.fromList defense_mods)
}
none : Type
none =
{
attributes = (Dict.empty),
attack = (Dict.empty),
defense = (Dict.empty)
}
merge : Type -> Type -> Type
merge omni_a omni_b =
{
attributes = (merge_mods omni_a.attributes omni_b.attributes),
attack = (merge_mods omni_a.attack omni_b.attack),
defense = (merge_mods omni_a.defense omni_b.defense)
}
apply_to_attributes : (
Type ->
Battle.Struct.Attributes.Type ->
Battle.Struct.Attributes.Type
)
apply_to_attributes omnimods attributes =
(Dict.foldl
(
(Battle.Struct.Attributes.decode_category)
>> (Battle.Struct.Attributes.mod)
)
attributes
omnimods.attributes
)
get_damage_sum : Type -> Int
get_damage_sum omni =
(Dict.foldl (\t -> \v -> \result -> (result + v)) 0 omni.attack)
get_attack_damage : Float -> Type -> Type -> Int
get_attack_damage dmg_modifier atk_omni def_omni =
let
base_def =
(
case
(Dict.get
(Battle.Struct.DamageType.encode
Battle.Struct.DamageType.Base
)
def_omni.defense
)
of
(Just v) -> v
Nothing -> 0
)
in
(Dict.foldl
(\t -> \v -> \result ->
let
actual_atk =
(max
0
(
(ceiling ((toFloat v) * dmg_modifier))
- base_def
)
)
in
case (Dict.get t def_omni.defense) of
(Just def_v) -> (result + (max 0 (actual_atk - def_v)))
Nothing -> (result + actual_atk)
)
0
atk_omni.attack
)
scale : Float -> Type -> Type
scale multiplier omnimods =
{omnimods |
attributes = (Dict.map (scale_dict_value multiplier) omnimods.attributes),
attack = (Dict.map (scale_dict_value multiplier) omnimods.attack),
defense =
(Dict.map (scale_dict_value multiplier) omnimods.defense)
}
get_attribute_mods : Type -> (List (String, Int))
get_attribute_mods omnimods = (Dict.toList omnimods.attributes)
get_attack_mods : Type -> (List (String, Int))
get_attack_mods omnimods = (Dict.toList omnimods.attack)
get_defense_mods : Type -> (List (String, Int))
get_defense_mods omnimods = (Dict.toList omnimods.defense)
get_all_mods : Type -> (List (String, Int))
get_all_mods omnimods =
(
(get_attribute_mods omnimods)
++ (get_attack_mods omnimods)
++ (get_defense_mods omnimods)
)
|