aboutsummaryrefslogtreecommitdiff
blob: aaf3dfa0593fcc59472f7625cbc94092c4bf9add (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
module Struct.TurnResult exposing
   (
      Type(..),
      Attack,
      Movement,
      WeaponSwitch,
      PlayerVictory,
      PlayerDefeat,
      PlayerTurnStart,
      get_next_movement_dir,
      get_actor_index,
      get_attack_defender_index,
      maybe_get_attack_next_step,
      apply_inverse_step,
      apply_step,
      maybe_remove_step,
      decoder
   )

-- Elm -------------------------------------------------------------------------
import Array

import Json.Decode

-- Shared ----------------------------------------------------------------------
import Util.Array

-- Battle ----------------------------------------------------------------------
import Battle.Struct.Omnimods

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

-- Battle Characters -----------------------------------------------------------
import BattleCharacters.Struct.Character

-- Local Module ----------------------------------------------------------------
import Struct.Attack
import Struct.Character
import Struct.Player

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------
type alias Movement =
   {
      character_index : Int,
      path : (List BattleMap.Struct.Direction.Type),
      destination : BattleMap.Struct.Location.Type
   }

type alias Attack =
   {
      attacker_index : Int,
      defender_index : Int,
      sequence : (List Struct.Attack.Type),
      attacker_luck : Int,
      defender_luck : Int
   }

type alias WeaponSwitch =
   {
      character_index : Int
   }

type alias PlayerVictory =
   {
      player_index : Int
   }

type alias PlayerDefeat =
   {
      player_index : Int
   }

type alias PlayerTurnStart =
   {
      player_index : Int
   }

type Type =
   Moved Movement
   | Attacked Attack
   | SwitchedWeapon WeaponSwitch
   | PlayerWon PlayerVictory
   | PlayerLost PlayerDefeat
   | PlayerTurnStarted PlayerTurnStart

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_movement_step : (
      (BattleMap.Struct.Location.Type -> Battle.Struct.Omnimods.Type) ->
      Movement ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_movement_step tile_omnimods movement characters players =
   (
      (Util.Array.update_unsafe
         movement.character_index
         (\char ->
            case (List.head movement.path) of
               (Just dir) ->
                  (Struct.Character.dirty_set_location
                     (BattleMap.Struct.Location.neighbor
                        dir
                        (Struct.Character.get_location char)
                     )
                     char
                  )

               Nothing ->
                  let current_location = (Struct.Character.get_location char) in
                     (Struct.Character.set_location
                        current_location
                        (tile_omnimods current_location)
                        char
                     )
         )
         characters
      ),
      players
   )

apply_inverse_movement_step : (
      (BattleMap.Struct.Location.Type -> Battle.Struct.Omnimods.Type) ->
      Movement ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_inverse_movement_step tile_omnimods movement characters players =
   (
      (Util.Array.update_unsafe
         movement.character_index
         (\char ->
            (
               let
                  location =
                     (List.foldr
                        (BattleMap.Struct.Location.neighbor)
                        (Struct.Character.get_location char)
                        --(movement.destination)
                        (List.map
                           (BattleMap.Struct.Direction.opposite_of)
                           movement.path
                        )
                     )
               in
                  (Struct.Character.set_location
                     location
                     (tile_omnimods location)
                     char
                  )
            )
         )
         characters
      ),
      players
   )

apply_switched_weapon : (
      WeaponSwitch ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_switched_weapon weapon_switch characters players =
   (
      (Util.Array.update_unsafe
         weapon_switch.character_index
         (\char ->
            (Struct.Character.set_base_character
               (BattleCharacters.Struct.Character.switch_weapons
                 (Struct.Character.get_base_character char)
               )
               char
            )
         )
         characters
      ),
      players
   )

apply_player_defeat : (
      PlayerDefeat ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_player_defeat pdefeat characters players =
   (
      (Array.map
         (\c ->
            if ((Struct.Character.get_player_index c) == pdefeat.player_index)
            then (Struct.Character.set_defeated True c)
            else c
         )
         characters
      ),
      players
   )

apply_inverse_player_defeat : (
      PlayerDefeat ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_inverse_player_defeat pdefeat characters players =
   (
      (Array.map
         (\c ->
            (
               if
               (
                  (Struct.Character.get_player_index c)
                  == pdefeat.player_index
               )
               then (Struct.Character.set_defeated False c)
               else c
            )
         )
         characters
      ),
      players
   )

apply_attack_step : (
      Attack ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_attack_step attack characters players =
   case (List.head attack.sequence) of
      (Just attack_step) ->
         (
            (Struct.Attack.apply_to_characters
               attack.attacker_index
               attack.defender_index
               attack_step
               characters
            ),
            players
         )

      Nothing ->
         (
            characters,
            (Util.Array.update_unsafe
               attack.attacker_index
               (Struct.Player.set_luck attack.attacker_luck)
               (Util.Array.update_unsafe
                  attack.defender_index
                  (Struct.Player.set_luck attack.defender_luck)
                  players
               )
            )
         )

apply_inverse_attack : (
      Attack ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_inverse_attack attack characters players =
   (
      (List.foldr
         (Struct.Attack.apply_inverse_to_characters
            attack.attacker_index
            attack.defender_index
         )
         characters
         attack.sequence
      ),
      players
   )

movement_decoder : (Json.Decode.Decoder Movement)
movement_decoder =
   (Json.Decode.map3
      Movement
      (Json.Decode.field "ix" Json.Decode.int)
      (Json.Decode.field "p" (Json.Decode.list (BattleMap.Struct.Direction.decoder)))
      (Json.Decode.field "nlc" (BattleMap.Struct.Location.decoder))
   )

attack_decoder : (Json.Decode.Decoder Attack)
attack_decoder =
   (Json.Decode.map5
      Attack
      (Json.Decode.field "aix" Json.Decode.int)
      (Json.Decode.field "dix" Json.Decode.int)
      (Json.Decode.field "seq" (Json.Decode.list (Struct.Attack.decoder)))
      (Json.Decode.field "alk" Json.Decode.int)
      (Json.Decode.field "dlk" Json.Decode.int)
   )

weapon_switch_decoder : (Json.Decode.Decoder WeaponSwitch)
weapon_switch_decoder =
   (Json.Decode.map
      WeaponSwitch
      (Json.Decode.field "ix" Json.Decode.int)
   )

player_won_decoder : (Json.Decode.Decoder PlayerVictory)
player_won_decoder =
   (Json.Decode.map
      PlayerVictory
      (Json.Decode.field "ix" Json.Decode.int)
   )

player_lost_decoder : (Json.Decode.Decoder PlayerDefeat)
player_lost_decoder =
   (Json.Decode.map
      PlayerDefeat
      (Json.Decode.field "ix" Json.Decode.int)
   )

player_turn_started_decoder : (Json.Decode.Decoder PlayerTurnStart)
player_turn_started_decoder =
   (Json.Decode.map
      PlayerTurnStart
      (Json.Decode.field "ix" Json.Decode.int)
   )

internal_decoder : String -> (Json.Decode.Decoder Type)
internal_decoder kind =
   case kind of
      "swp" ->
         (Json.Decode.map
            (\x -> (SwitchedWeapon x))
            (weapon_switch_decoder)
         )

      "mv" ->
         (Json.Decode.map
            (\x -> (Moved x))
            (movement_decoder)
         )

      "atk" ->
         (Json.Decode.map
            (\x -> (Attacked x))
            (attack_decoder)
         )

      "pwo" ->
         (Json.Decode.map
            (\x -> (PlayerWon x))
            (player_won_decoder)
         )

      "plo" ->
         (Json.Decode.map
            (\x -> (PlayerLost x))
            (player_lost_decoder)
         )

      "pts" ->
         (Json.Decode.map
            (\x -> (PlayerTurnStarted x))
            (player_turn_started_decoder)
         )

      other ->
         (Json.Decode.fail
            (
               "Unknown kind of turn result: \""
               ++ other
               ++ "\"."
            )
         )

maybe_remove_movement_step : Movement -> (Maybe Type)
maybe_remove_movement_step movement =
   case (List.tail movement.path) of
      Nothing -> Nothing
      (Just path_tail) ->
         (Just
            (Moved
               {movement |
                  path = path_tail
               }
            )
         )

maybe_remove_attack_step : Attack -> (Maybe Type)
maybe_remove_attack_step attack =
   case (List.tail attack.sequence) of
      Nothing -> Nothing
      (Just sequence_tail) ->
         (Just
            (Attacked
               {attack |
                  sequence = sequence_tail
               }
            )
         )

apply_player_victory : (
      PlayerVictory ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_player_victory player_victory characters players =
   (
      characters,
      players
   )

apply_player_turn_started : (
      PlayerTurnStart ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_player_turn_started player_defeat characters players =
   (
      characters,
      players
   )

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_step : (
      (BattleMap.Struct.Location.Type -> Battle.Struct.Omnimods.Type) ->
      Type ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_step tile_omnimods turn_result characters players =
   case turn_result of
      (Moved movement) ->
         (apply_movement_step (tile_omnimods) movement characters players)

      (SwitchedWeapon weapon_switch) ->
         (apply_switched_weapon weapon_switch characters players)

      (Attacked attack) ->
         (apply_attack_step attack characters players)

      (PlayerWon pvict) ->
         (apply_player_victory pvict characters players)

      (PlayerLost pdefeat) ->
         (apply_player_defeat pdefeat characters players)

      (PlayerTurnStarted pturns) ->
         (apply_player_turn_started pturns characters players)

apply_inverse_step : (
      (BattleMap.Struct.Location.Type -> Battle.Struct.Omnimods.Type) ->
      Type ->
      (Array.Array Struct.Character.Type) ->
      (Array.Array Struct.Player.Type) ->
      (
         (Array.Array Struct.Character.Type),
         (Array.Array Struct.Player.Type)
      )
   )
apply_inverse_step tile_omnimods turn_result characters players =
   case turn_result of
      (Moved movement) ->
         (apply_inverse_movement_step
            (tile_omnimods)
            movement
            characters
            players
         )

      (SwitchedWeapon weapon_switch) ->
         (apply_switched_weapon weapon_switch characters players)

      (Attacked attack) ->
         (apply_inverse_attack attack characters players)

      (PlayerWon pvict) -> (characters, players)

      (PlayerLost pdefeat) ->
         (apply_inverse_player_defeat pdefeat characters players)

      (PlayerTurnStarted pturns) -> (characters, players)

decoder : (Json.Decode.Decoder Type)
decoder =
   (Json.Decode.field "t" Json.Decode.string)
   |> (Json.Decode.andThen internal_decoder)

maybe_remove_step : Type -> (Maybe Type)
maybe_remove_step turn_result =
   case turn_result of
      (Moved movement) -> (maybe_remove_movement_step movement)
      (SwitchedWeapon _) -> Nothing
      (Attacked attack) -> (maybe_remove_attack_step attack)
      (PlayerWon pvict) -> Nothing
      (PlayerLost pdefeat) -> Nothing
      (PlayerTurnStarted pturns) -> Nothing

get_next_movement_dir : Movement -> BattleMap.Struct.Direction.Type
get_next_movement_dir movement =
   case (List.head movement.path) of
      (Just dir) -> dir
      Nothing -> BattleMap.Struct.Direction.None

get_attack_defender_index : Attack -> Int
get_attack_defender_index attack = attack.defender_index

maybe_get_attack_next_step : Attack -> (Maybe Struct.Attack.Type)
maybe_get_attack_next_step attack = (List.head attack.sequence)

get_actor_index : Type -> Int
get_actor_index turn_result =
   case turn_result of
      (Moved movement) -> movement.character_index
      (SwitchedWeapon weapon_switch) -> weapon_switch.character_index
      (Attacked attack) -> attack.attacker_index
      (PlayerWon pvict) -> pvict.player_index
      (PlayerLost pdefeat) -> pdefeat.player_index
      (PlayerTurnStarted pturns) -> pturns.player_index