-
Notifications
You must be signed in to change notification settings - Fork 207
/
judge_machine.asm
1171 lines (1053 loc) · 21.1 KB
/
judge_machine.asm
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
JUDGE_UP_DOWN_TILE EQU $00
JUDGE_UNDERLINE_TILE EQU $01
JUDGE_LINE_END_TILE EQU $02
JUDGE_MALE_TILE EQU $07
JUDGE_FEMALE_TILE EQU $08
JUDGE_STAR_TILE EQU $09
JUDGE_LEFT_RIGHT_TILE EQU $0a
JUDGE_BORDER_TILE EQU $13
JUDGE_BLANK_TILE EQU $64
JUDGE_WHITE_TILE EQU $6d
JudgeMachine:
; Check that the machine is activated
ld hl, wStatusFlags3
bit 0, [hl] ; ENGINE_JUDGE_MACHINE
ld hl, NewsMachineOffText
jr z, .done
; Introduce machine
ld hl, NewsMachineIntroText
.continue
call PrintText
call YesNoBox
jr c, .cancel
; Choose a party Pokémon
ld hl, NewsMachineWhichMonText
call PrintText
farcall SelectMonFromParty
jr c, .cancel
; Can't judge an Egg
ld a, MON_IS_EGG
call GetPartyParamLocation
bit MON_IS_EGG_F, [hl]
ld hl, NewsMachineEggText
jr nz, .done
; Show the EV and IV charts
ld hl, NewsMachinePrepText
call PrintText
call FadeToMenu
call JudgeSystem
call ExitAllMenus
; hLCDInterruptFunction gets overridden for drawing the radar chart.
; (It is not called as such because the LCD interrupt is disabled.)
; This restores it to the LCDGeneric function.
ld a, LOW(LCDGeneric)
ldh [hFunctionTargetLo], a
ld a, HIGH(LCDGeneric)
ldh [hFunctionTargetHi], a
ld hl, NewsMachineContinueText
jr .continue
.cancel
ld hl, NewsMachineCancelText
.done
jmp PrintText
NewsMachineOffText:
text "It's the #mon"
line "Judge Machine!"
para "It's not in"
line "operation yet…"
done
NewsMachineIntroText:
text "It's the #mon"
line "Judge Machine!"
para "Would you like to"
line "judge a #mon's"
cont "overall power?"
done
NewsMachineWhichMonText:
text "Please select"
line "a #mon."
prompt
NewsMachinePrepText:
text "Visualizing your"
line "#mon's power…"
prompt
NewsMachineContinueText:
text "Would you like"
line "to judge another"
cont "#mon?"
done
NewsMachineCancelText:
text "Goodbye!"
done
NewsMachineEggText:
text "An Egg doesn't"
line "have any power"
cont "yet to judge!"
done
JudgeSystem::
; Start with the EV chart
xor a
ldh [hChartScreen], a
.restart
; Clear the screen
call ClearBGPalettes
call ClearTileMap
call DisableLCD
xor a
ldh [hBGMapMode], a
; Load the party struct into wTempMon
ld a, [wCurPartyMon]
inc a
ld c, a
ld b, 1
farcall CopyBetweenPartyAndTemp
; Load the frontpic graphics
ld hl, wTempMonForm
predef GetVariant
call GetBaseData
ld de, vTiles2
predef GetFrontpic
; Load the blank chart graphics
ld a, $1
ldh [rVBK], a
ld hl, JudgeSystemGFX
ld de, vTiles5
lb bc, BANK(JudgeSystemGFX), 10 * 12
call DecompressRequest2bpp
xor a
ldh [rVBK], a
; Load the max stat sparkle and hyper trained bottle cap graphics
ld hl, MaxStatSparkleGFX
ld de, vTiles0
ld bc, 2 tiles
rst CopyBytes
; Place the up/down arrows and nickname
ld hl, wPartyMonNicknames
ld a, [wCurPartyMon]
call SkipNames
ld d, h
ld e, l
hlcoord 0, 0
ld a, JUDGE_UP_DOWN_TILE
ld [hli], a
rst PlaceString
; Place the level
hlcoord 14, 0
call PrintLevel
; Place the gender icon
farcall GetGender
ld a, JUDGE_WHITE_TILE
jr c, .got_gender
ld a, JUDGE_MALE_TILE
jr nz, .got_gender
ld a, JUDGE_FEMALE_TILE
.got_gender
hlcoord 18, 0
ld [hli], a
; Place the shiny icon
ld bc, wTempMonShiny
farcall CheckShininess
; a = carry ? JUDGE_STAR_TILE : JUDGE_WHITE_TILE
sbc a
and JUDGE_STAR_TILE - JUDGE_WHITE_TILE
add JUDGE_WHITE_TILE
ld [hli], a
; Place the top border
ld bc, SCREEN_WIDTH
ld a, JUDGE_BORDER_TILE
rst ByteFill
; Place the heading underline
hlcoord 0, 2
ld [hl], JUDGE_LEFT_RIGHT_TILE
hlcoord 0, 3
ld bc, 10
ld a, JUDGE_UNDERLINE_TILE
rst ByteFill
ld [hl], JUDGE_LINE_END_TILE
; Place the frontpic graphics
hlcoord 0, 6
farcall PlaceFrontpicAtHL
; Place the Pokédex number
ld a, [wCurPartySpecies]
ld [wTextDecimalByte], a
hlcoord 1, 13
ld a, "№"
ld [hli], a
ld a, "."
ld [hli], a
lb bc, PRINTNUM_LEADINGZEROS | 1, 3
ld de, wTextDecimalByte
call PrintNum
; Place the chart
hlcoord 8, 4
ld de, SCREEN_WIDTH
xor a
ld b, 12
.chart_row
ld c, 10
push hl
.chart_col
ld [hli], a
inc a
dec c
jr nz, .chart_col
pop hl
add hl, de
dec b
jr nz, .chart_row
; Clear some non-chart tiles
ld a, JUDGE_BLANK_TILE
hlcoord 9, 4
ld [hli], a
ld [hl], a
hlcoord 15, 4
ld [hli], a
ld [hl], a
hlcoord 9, 15
ld [hli], a
ld [hl], a
hlcoord 15, 15
ld [hli], a
ld [hl], a
; Place the stat names and values
hlcoord 12, 2
ld de, .HP
ld bc, wTempMonMaxHP
call .PrintTopStat
hlcoord 17, 4
ld de, .Atk
ld bc, wTempMonAttack
call .PrintTopStat
hlcoord 17, 15
ld de, .Def
ld bc, wTempMonDefense
call .PrintBottomStat
hlcoord 12, 17
ld de, .Spd
ld bc, wTempMonSpeed
call .PrintBottomStat
hlcoord 6, 15
ld de, .SDf
ld bc, wTempMonSpclDef
call .PrintBottomStat
hlcoord 6, 4
ld de, .SAt
ld bc, wTempMonSpclAtk
call .PrintTopStat
; Show the screen
call EnableLCD
ld a, CGB_JUDGE_SYSTEM
call GetCGBLayout
.render
call ClearSpriteAnims
; Display the current chart, EVs or IVs
ldh a, [hChartScreen]
and a
jr z, .evs
ld hl, IVChartPals
ld de, .IVHeading
ld bc, RenderIVChart
jr .got_config
.evs
ld hl, EVChartPals
ld de, .EVHeading
ld bc, RenderEVChart
.got_config
call .PrepareChart
; Load the rendered chart graphics
ld a, $1
ldh [rVBK], a
ld hl, vTiles5
ld de, wDecompressScratch
lb bc, BANK(wDecompressScratch), 10 * 12
call Request2bppInWRA6
xor a
ldh [rVBK], a
; Wait for input
.input_loop
farcall PlaySpriteAnimations
call DelayFrame
call JoyTextDelay
ldh a, [hJoyLast]
; B button quits
bit B_BUTTON_F, a
ret nz
; Up or down switches between party members
bit D_UP_F, a
jr nz, .prev_mon
bit D_DOWN_F, a
jr nz, .next_mon
; Left or right toggles between EVs and IVs
and D_LEFT | D_RIGHT
jr z, .input_loop
ldh a, [hChartScreen]
cpl
ldh [hChartScreen], a
jr .render
.prev_mon
ld a, [wCurPartyMon]
.more_prev
and a
jr z, .input_loop
dec a
ld hl, wPartyMon1IsEgg
push af
call GetPartyLocation
pop af
bit MON_IS_EGG_F, [hl]
jr nz, .more_prev
jr .switch_mon
.next_mon
ld a, [wPartyCount]
ld b, a
ld a, [wCurPartyMon]
.more_next
inc a
cp b
jr z, .input_loop
ld hl, wPartyMon1IsEgg
push af
call GetPartyLocation
pop af
bit MON_IS_EGG_F, [hl]
jr nz, .more_next
.switch_mon
ld [wCurPartyMon], a
ld c, a
ld b, 0
ld hl, wPartySpecies
add hl, bc
inc a
ld [wPartyMenuCursor], a
ld a, [hl]
ld [wCurPartySpecies], a
call ClearSpriteAnims
jmp .restart
.EVHeading:
db "Effort @"
.IVHeading:
db "Potential@"
.PrintTopStat:
; hl = coords, de = string, bc = stat
push bc
rst PlaceString
ld de, SCREEN_WIDTH
jr ._FinishPrintStat
.PrintBottomStat:
; hl = coords, de = string, bc = stat
push bc
rst PlaceString
ld de, -SCREEN_WIDTH
._FinishPrintStat:
add hl, de
pop de
lb bc, 2, 3
jmp PrintNum
.HP: db "HP@"
.Atk: db "Atk@"
.Def: db "Def@"
.Spd: db "Spd@"
.SDf: db "SDf@"
.SAt: db "SAt@"
.PrepareChart:
; hl = palettes, de = title string, bc = chart function
push bc
push de
; Load the palettes
ld de, wBGPals1
ld bc, 6 palettes
call FarCopyColorWRAM
; Place the title
pop de
hlcoord 1, 2
rst PlaceString
; Render the chart when this returns (bc is on the stack)
ld b, 2
jmp SafeCopyTilemapAtOnce
SparkleMaxStat:
; Show a sparkle sprite at (d, e) if a is 255
; Returns carry if the sprite is shown
inc a
ret nz
ld a, SPRITE_ANIM_INDEX_MAX_STAT_SPARKLE
jr _InitSpriteAnimStruct_PreserveHL
SparkleMaxStatOrShowBottleCap:
; Show a sparkle sprite at (d, e) if a is 255,
; or a bottle cap sprite if [hl] bit 7 is 1 (shifts [hl] regardless)
; Returns carry if either sprite is shown
rlc [hl] ; sets carry if hyper trained
inc a ; sets z if if max stat; does not affect carry
ld a, SPRITE_ANIM_INDEX_MAX_STAT_SPARKLE
jr z, _InitSpriteAnimStruct_PreserveHL
assert SPRITE_ANIM_INDEX_MAX_STAT_SPARKLE + 1 == SPRITE_ANIM_INDEX_HYPER_TRAINED_STAT
inc a ; does not affect carry
ret nc
_InitSpriteAnimStruct_PreserveHL:
push hl
call _InitSpriteAnimStruct
pop hl
scf
ret
RenderEVChart:
; Read the EVs and round them up to the nearest 4
; HP
ld a, [wTempMonHPEV]
or %11
ldh [hChartHP], a
depixel 2, 12
call SparkleMaxStat
; Atk
ld a, [wTempMonAtkEV]
or %11
ldh [hChartAtk], a
depixel 4, 17
call SparkleMaxStat
; Def
ld a, [wTempMonDefEV]
or %11
ldh [hChartDef], a
depixel 15, 17
call SparkleMaxStat
; Spd
ld a, [wTempMonSpdEV]
or %11
ldh [hChartSpd], a
depixel 17, 12
call SparkleMaxStat
; SAt
ld a, [wTempMonSatEV]
or %11
ldh [hChartSat], a
depixel 4, 6
call SparkleMaxStat
; SDf
ld a, [wTempMonSdfEV]
or %11
ldh [hChartSdf], a
depixel 15, 6
call SparkleMaxStat
jr RenderChart
RenderIVChart:
; Read the IVs and scale them to 255 instead of 31
ld hl, wBuffer1
ld a, [wTempMonHyperTraining]
ld [hl], a
; HP
ld a, [wTempMonHPAtkDV]
and $f0
ld b, a
swap a
or b
ldh [hChartHP], a
depixel 2, 12
call SparkleMaxStatOrShowBottleCap
; Atk
ld a, [wTempMonHPAtkDV]
and $0f
ld b, a
swap a
or b
ldh [hChartAtk], a
depixel 4, 17
call SparkleMaxStatOrShowBottleCap
; Def
ld a, [wTempMonDefSpdDV]
and $f0
ld b, a
swap a
or b
ldh [hChartDef], a
depixel 15, 17
call SparkleMaxStatOrShowBottleCap
; Spd
ld a, [wTempMonDefSpdDV]
and $0f
ld b, a
swap a
or b
ldh [hChartSpd], a
depixel 17, 12
call SparkleMaxStatOrShowBottleCap
; SAt
ld a, [wTempMonSatSdfDV]
and $f0
ld b, a
swap a
or b
ldh [hChartSat], a
depixel 4, 6
call SparkleMaxStatOrShowBottleCap
; SDf
ld a, [wTempMonSatSdfDV]
and $0f
ld b, a
swap a
or b
ldh [hChartSdf], a
depixel 15, 6
call SparkleMaxStatOrShowBottleCap
; fallthrough
RenderChart:
; Decompress blank chart graphics
ld hl, JudgeSystemGFX
ld b, BANK(JudgeSystemGFX)
call FarDecompressWRA6InB
; Render the radar chart onto the graphics
ld a, BANK(wDecompressScratch)
ldh [rSVBK], a
call OutlineRadarChart
ld a, $1
ldh [rSVBK], a
ret
CalcBTimesCOver256:
; a = b * c / 256
xor a
ldh [hMultiplicand + 0], a
ldh [hMultiplicand + 1], a
ld a, b
ldh [hMultiplicand + 2], a
ld a, c
ldh [hMultiplier], a
call Multiply
ldh a, [hProduct + 2]
ret
OutlineRadarChart:
; de = point for HP axis
ldh a, [hChartHP]
ld b, a
; x = 39
ld a, 39
ld d, a
; y = 46 - v * 47 / 256
ld c, 47
call CalcBTimesCOver256
cpl
add 46 + 1 ; a = 46 - a
ld e, a
; Store the HP point to close the polygon
push de
push de
; de = Atk point
ldh a, [hChartAtk]
ld b, a
; x = 41 + v * 39 / 256
ld c, 39
call CalcBTimesCOver256
add 41
ld d, a
; y = ForwardSlashAxisYCoords[x] (~= 46 - v * 23 / 256)
add LOW(ForwardSlashAxisYCoords)
ld c, a
adc HIGH(ForwardSlashAxisYCoords)
sub c
ld b, a
ld a, [bc]
ld e, a
; Draw a line from HP to Atk
pop bc
push de
ld a, LOW(FillRadarDown)
ldh [hFunctionTargetLo], a
ld a, HIGH(FillRadarDown)
ldh [hFunctionTargetHi], a
call DrawAndFillRadarEdge
; de = Def point
ldh a, [hChartDef]
ld b, a
; x = 41 + v * 39 / 256
ld c, 39
call CalcBTimesCOver256
add 41
ld d, a
; y = BackslashAxisYCoords[x] (~= 49 + v * 23 / 256)
add LOW(BackslashAxisYCoords)
ld c, a
adc HIGH(BackslashAxisYCoords)
sub c
ld b, a
ld a, [bc]
ld e, a
; Draw a line from Atk to Def
pop bc
push de
ld a, LOW(FillRadarLeft)
ldh [hFunctionTargetLo], a
ld a, HIGH(FillRadarLeft)
ldh [hFunctionTargetHi], a
call DrawAndFillRadarEdge
; de = Spd point
ldh a, [hChartSpd]
ld b, a
; x = 40
ld a, 40
ld d, a
; y = 49 + v * 47 / 256
ld c, 47
call CalcBTimesCOver256
add 49
ld e, a
; Draw a line from Def to Spd
pop bc
push de
ld a, LOW(FillRadarUp)
ldh [hFunctionTargetLo], a
ld a, HIGH(FillRadarUp)
ldh [hFunctionTargetHi], a
call DrawAndFillRadarEdge
; de = SDf point
ldh a, [hChartSdf]
ld b, a
; x = 38 - v * 39 / 256
ld c, 39
call CalcBTimesCOver256
cpl
add 38 + 1 ; a = 38 - a
ld d, a
; y = ForwardSlashAxisYCoords[x] (~= 49 + v * 23 / 256)
add LOW(ForwardSlashAxisYCoords)
ld c, a
adc HIGH(ForwardSlashAxisYCoords)
sub c
ld b, a
ld a, [bc]
ld e, a
; Draw a line from Spd to SDf
pop bc
push de
; hFunctionTarget is already FillRadarUp
call DrawAndFillRadarEdge
; de = SAt point
ldh a, [hChartSat]
ld b, a
; x = 38 - v * 39 / 256
ld c, 39
call CalcBTimesCOver256
cpl
add 38 + 1 ; a = 38 - a
ld d, a
; y = BackslashAxisYCoords[x] (~= 46 - v * 23 / 256)
add LOW(BackslashAxisYCoords)
ld c, a
adc HIGH(BackslashAxisYCoords)
sub c
ld b, a
ld a, [bc]
ld e, a
; Draw a line from SDf to SAt
pop bc
push de
ld a, LOW(FillRadarRight)
ldh [hFunctionTargetLo], a
ld a, HIGH(FillRadarRight)
ldh [hFunctionTargetHi], a
call DrawAndFillRadarEdge
; Draw a line from SAt to HP, closing the polygon
pop bc
pop de
ld a, LOW(FillRadarDown)
ldh [hFunctionTargetLo], a
ld a, HIGH(FillRadarDown)
ldh [hFunctionTargetHi], a
; fallthrough
DrawAndFillRadarEdge:
; Draw a line from (b, c) to (d, e) and fill it in with hFunction
; Calculate |x1 - x0|
ld a, d
sub b
jr nc, .x_sorted
cpl
inc a ; a = -a
.x_sorted
ldh [hDX], a
ld l, a
; Calculate |y1 - y0|
ld a, e
sub c
jr nc, .y_sorted
cpl
inc a ; a = -a
.y_sorted
ldh [hDY], a
; Branch based on slope
cp l
jr nc, DrawHighRadarLine ; dy (a) >= dx (l)
; fallthrough
DrawLowRadarLine:
; Draw a line from (b, c) to (d, e), left to right, where dx > dy
; Ensure that x0 < x1 (b < d)
ld a, b
cp d
jr c, .x_sorted
; swap bc and de
ld a, b
ld b, d
ld d, a
ld a, c
ld c, e
ld e, a
.x_sorted
; Shift up or down depending on dy
ld a, c
cp e
ld a, $0c ; inc c
jr c, .y_sorted
jr z, DrawHorizontalRadarLine ; c == e, so y is constant
inc a ; $0d ; dec c
.y_sorted
ldh [hSingleOpcode], a
; D = 2 * dy - dx
ldh a, [hDX]
ld l, a
ldh a, [hDY]
add a
sub l
ldh [hErr], a
; For x from b to d, draw a point at (x, c)
ld a, d
ldh [hChartLineCoord], a
.loop
call hLCDInterruptFunction ; FillRadarUp/Down/Left/Right
; Update D and y
ldh a, [hErr]
cp $80 ; high bit means negative
jr nc, .not_positive
call hSingleOperation
ldh a, [hDX]
ld l, a
ldh a, [hErr]
sub l
sub l
ldh [hErr], a
.not_positive
ld hl, hDY
add [hl]
add [hl]
ldh [hErr], a
inc b
ldh a, [hChartLineCoord]
cp b
jr nc, .loop
ret
DrawHighRadarLine:
; Draw a line from (b, c) to (d, e), top to bottom, where dx <= dy
; Ensure that y0 < y1 (c < e)
ld a, c
cp e
jr c, .y_sorted
; swap bc and de
ld a, b
ld b, d
ld d, a
ld a, c
ld c, e
ld e, a
.y_sorted
; Shift right or left depending on dx
ld a, b
cp d
ld a, $04 ; inc b
jr c, .x_sorted
jr z, DrawVerticalRadarLine ; b == d, so x is constant
inc a ; $05 ; dec b
.x_sorted
ldh [hSingleOpcode], a
; D = 2 * dx - dy
ldh a, [hDY]
ld l, a
ldh a, [hDX]
add a
sub l
ldh [hErr], a
; For y from c to e, draw a point at (b, y)
ld a, e
ldh [hChartLineCoord], a
.loop
call hLCDInterruptFunction ; FillRadarUp/Down/Left/Right
; Update D and x
ldh a, [hErr]
cp $80
jr nc, .not_positive
call hSingleOperation
ldh a, [hDY]
ld l, a
ldh a, [hErr]
sub l
sub l
ldh [hErr], a
.not_positive
ld hl, hDX
add [hl]
add [hl]
ldh [hErr], a
inc c
ldh a, [hChartLineCoord]
cp c
jr nc, .loop
ret
DrawHorizontalRadarLine:
; Draw from (b, c) to (d, e), where c == e and b < d
; Ensure that x0 < x1 (b < d)
ld a, b
cp d
jr c, .x_sorted
ld b, d
ld d, a
.x_sorted
; For x from b to d, draw a point at (x, c)
ld a, d
ldh [hChartLineCoord], a
.loop
call hLCDInterruptFunction ; FillRadarUp/Down/Left/Right
inc b
ldh a, [hChartLineCoord]
cp b
jr nc, .loop
ret
DrawVerticalRadarLine:
; Draw a line from (b, c) to (d, e), where b == d
; Ensure that y0 < y1 (c < e)
ld a, c
cp e
jr c, .y_sorted
ld c, e
ld e, a
.y_sorted
; For y from c to e, draw a point at (b, y)
ld a, e
ldh [hChartLineCoord], a
.loop
call hLCDInterruptFunction ; FillRadarUp/Down/Left/Right
inc c
ldh a, [hChartLineCoord]
cp c
jr nc, .loop
ret
FillRadarUp:
; Draw a vertical line up from (b, c) to the lower diagonal half-axes
ld hl, LowerSlashAxesYCoords
jr _FillRadarVertical
FillRadarDown:
; Draw a vertical line down from (b, c) to the upper diagonal half-axes
ld hl, UpperSlashAxesYCoords
; fallthrough
_FillRadarVertical:
; Draw a vertical line from (b, c) to (b, y), where y = hl[b]
push bc
; de = point on the diagonal axes
ld e, b
ld d, 0
add hl, de
ld a, [hl]
ld e, a
ld d, b
; Ensure that y0 < y1 (c < e)
ld a, c
cp e
jr c, .y_sorted
ld c, e
ld e, a
.y_sorted
; For y from c to e, draw a point at (b, y)
ld a, e
ldh [hChartFillCoord], a
.loop
call DrawRadarPointBC
inc c
ldh a, [hChartFillCoord]
cp c
jr nc, .loop
pop bc
ret
FillRadarLeft:
; Draw a horizontal line left from (b, c) to the right diagonal half-axes
ld hl, RightSlashAxesXCoords
jr _FillRadarHorizontal
FillRadarRight:
; Draw a horizontal line right from (b, c) to the left diagonal half-axes
ld hl, LeftSlashAxesXCoords
; fallthrough
_FillRadarHorizontal:
; Draw a horizontal line from (b, c) to the vertical axis
push bc
; de = point on the diagonal axes
ld a, c
sub 24
ld e, a
ld d, 0
add hl, de
ld a, [hl]
ld d, a
ld e, c
; Ensure that x0 < x1 (b < d)
ld a, b
cp d
jr c, .x_sorted
ld b, d
ld d, a
.x_sorted
; For x from b to d, draw a point at (x, c)
ld a, d
ldh [hChartFillCoord], a
.loop
call DrawRadarPointBC
inc b
ldh a, [hChartFillCoord]
cp b
jr nc, .loop
pop bc
ret