summaryrefslogtreecommitdiff
path: root/engine/games/picross_minigame.asm
blob: e40e9920ffb436442f14fc7d46bbd67ef5a325ee (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
INCLUDE "constants.asm"

SECTION "engine/games/picross_minigame.asm", ROMX

; PicrossSprites constants
	const_def
	const PATTERN_NORMAL
	const PATTERN_TILESET

; PicrossMinigame.Jumptable constants
	const_def
	const PICROSS_INIT_MODE
	const PICROSS_RUN_MODE
	const PICROSS_EXIT_MODE
PICROSS_END_LOOP_F EQU 7

; wPicrossCurrentCellType constants
	const_def
	const PICROSS_BLANK_CELL
	const PICROSS_COLORED_CELL
	const PICROSS_MARKED_CELL

PICROSS_GFX_BGTILE EQU $80
PICROSS_GFX_BUSHTILE EQU $81
PICROSS_GFX_BUSHGROUNDTILE EQU $82
PICROSS_GFX_GROUNDTILE EQU $83

PICROSS_GFX_COLUMNS EQU $84
PICROSS_GFX_ROWS EQU $b4
PICROSS_GFX_TABLESTART equ $f0

; The Picross game area is referred as the "table" here.
; The table consists of 256 cells, divided into 4x4 "grids" of 16 cells each.

PicrossMinigame:
	call .Init
	call DelayFrame
.loop
	call .GameLoop
	jr nc, .loop
	call ClearJoypad
	ret

.Init:
	call DisableLCD
	callba InitEffectObject
	call .InitGFX
	call .PlacePlayerBG
	call .InitRAM

	depixel 8, 8, 0, 0
	ld a, SPRITE_ANIM_INDEX_MINIGAME_PICROSS_CURSOR
	call InitSpriteAnimStruct

	ld a, c
	ld [wPicrossCursorSpritePointer], a
	ld a, b
	ld [wPicrossCursorSpritePointer+1], a

	depixel 5, 4, 4, 4
	ld a, SPRITE_ANIM_INDEX_MINIGAME_PICROSS_GOLD
	call InitSpriteAnimStruct

	ld a, -1
	ld [wPicrossCurrentCellType], a
	call Picross_InitPicrossTable
	call Picross_InitPicrossDigits

	xor a
	ldh [hSCY], a
	ldh [hSCX], a
	ld [rWY], a
	ld [wJumptableIndex], a

	ld a, 1
	ldh [hBGMapMode], a

	ld a, %11100011
	ld [rLCDC], a
	ld a, %11100100
	ld [rBGP], a
	ld a, %11010000
	ld [rOBP0], a

	xor a ; PICROSS_BLANK_CELL
	ld [wPicrossCurrentCellType], a
	ret

.InitGFX:
	ld hl, PicrossBackgroundGFX
	ld de, vPicrossBackground
	ld bc, 4 tiles
	ld a, BANK(PicrossBackgroundGFX)
	call FarCopyData

; load column GFX
	ld de, vPicrossBackground tile 4
	ld b, 4
.column_outer_loop
	push bc
	ld b, 4
.column_inner_loop
	push bc
	ld hl, PicrossGridHighlightsGFX
	ld bc, 3 tiles
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .column_inner_loop
	pop bc
	dec b
	jr nz, .column_outer_loop

; load row GFX
	ld de, vPicrossBackground tile $34
	ld b, 4
.row_outer_loop
	push bc
	ld b, 3
.row1
	push bc
	ld hl, PicrossGridHighlightsGFX tile 3
	ld bc, 1 tiles
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row1
	ld b, 3
.row2
	push bc
	ld hl, PicrossGridHighlightsGFX tile 4
	ld bc, 1 tiles
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row2
	ld b, 3
.row3
	push bc
	ld hl, PicrossGridHighlightsGFX tile 5
	ld bc, LEN_2BPP_TILE
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row3
	ld b, 2
.row4
	push bc
	ld hl, PicrossGridHighlightsGFX tile 3
	ld bc, LEN_2BPP_TILE
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row4
	ld b, 2
.row5
	push bc
	ld hl, PicrossGridHighlightsGFX tile 4
	ld bc, LEN_2BPP_TILE
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row5
	ld b, 2
.row6
	push bc
	ld hl, PicrossGridHighlightsGFX tile 5
	ld bc, LEN_2BPP_TILE
	ld a, BANK(PicrossGridHighlightsGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .row6
	pop bc
	dec b
	jr nz, .row_outer_loop

; load Picross table
	ld de, vPicrossPlayArea
	ld b, 16
.load_grid
	push bc
	ld hl, PicrossGridGFX
	ld bc, 9 tiles
	ld a, BANK(PicrossGridGFX)
	call FarCopyData
	pop bc
	dec b
	jr nz, .load_grid

; load cursor GFX
	ld de, vSprites
	ld hl, PicrossCursorGFX
	ld bc, 9 tiles
	ld a, BANK(PicrossCursorGFX)
	call FarCopyData

; load Gold sprites
	ld de, vSprites + $100
	ld hl, GoldSpriteGFX + $40
	ld bc, 4 tiles
	ld a, BANK(GoldSpriteGFX)
	call FarCopyData

	ld de, vSprites + $140
	ld hl, GoldSpriteGFX + $100
	ld bc, 4 tiles
	ld a, BANK(GoldSpriteGFX)
	call FarCopyData

	ld a, SPRITE_ANIM_DICT_25
	ld hl, wSpriteAnimDict
	ld [hli], a
	ld [hl], SPRITE_ANIM_DICT_DEFAULT
	inc hl
	ld a, SPRITE_ANIM_DICT_22
	ld [hli], a
	ld [hl], SPRITE_ANIM_DICT_10
	ret

.PlacePlayerBG:
	hlcoord 0, 0
	ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
	ld a, PICROSS_GFX_BGTILE
	call ByteFill

	hlcoord 1, 1
	ld bc, 5
	ld a, PICROSS_GFX_BUSHTILE
	call ByteFill

	hlcoord 1, 2
	ld bc, 5
	ld a, PICROSS_GFX_BUSHTILE
	call ByteFill

	hlcoord 1, 3
	ld bc, 5
	ld a, PICROSS_GFX_BUSHGROUNDTILE
	call ByteFill

	hlcoord 1, 4
	ld bc, 5
	ld a, PICROSS_GFX_GROUNDTILE
	call ByteFill
	ret

.InitRAM:
	ld hl, wPicrossMarkedCells
	ld bc, $514
	xor a
	call ByteFill
	ret

.GameLoop:
	call .GetJoypadState
	ld a, [wJumptableIndex]
	bit PICROSS_END_LOOP_F, a
	jr nz, .quit

	call .ExecuteJumptable
	callba EffectObjectJumpNoDelay
	call DelayFrame
	and a
	ret

.quit
	scf
	ret

.GetJoypadState:
	call GetJoypadDebounced
	ldh a, [hJoyState]
	ld [wc606], a
	ld hl, wc607
	ld a, [hl]
	and a
	ret z
	dec [hl]
	xor a
	ld [wc606], a
	ret

.ExecuteJumptable:
	jumptable .Jumptable, wJumptableIndex

.Jumptable:
	dw .InitMode
	dw .RunMode
	dw .ExitMode

.InitMode:
	call .PlaceBGMapTiles
	ld hl, wJumptableIndex
	inc [hl]
	ret

.RunMode:
	call Picross_CheckPuzzleSolved
	jr c, .solved
	call Picross_ProcessJoypad
	ret

.solved
; Deallocate the cursor sprite
	ld hl, wPicrossCursorSpritePointer
	ld c, [hl]
	inc hl
	ld b, [hl]
	ld hl, SPRITEANIMSTRUCT_INDEX
	add hl, bc
	ld [hl], 0

; Exit Picross minigame
	ld hl, wJumptableIndex
	inc [hl]

.ExitMode:
	ldh a, [hJoyDown]
	and START
	ret z

; Game will truly exit once the Start button is pressed
	ld hl, wJumptableIndex
	set PICROSS_END_LOOP_F, [hl]
	ret

.PlaceBGMapTiles:
	xor a
	ldh [hBGMapMode], a
	call .PlacePlayerBG
	call .PlaceColumnNumbers
	call .PlaceRowNumbers
	call .PlaceTable
	call .HandleError
	ld a, 1
	ldh [hBGMapMode], a
	ret

.PlaceTable:
	ld hl, .TableCoords
	ld c, 4 * 4
	ld a, PICROSS_GFX_TABLESTART
.place_table_loop
	push bc
	push hl
	ld e, [hl]
	inc hl
	ld h, [hl]
	ld l, e
	call .PlaceTableGrid
	pop hl
	inc hl
	inc hl
	pop bc
	dec c
	jr nz, .place_table_loop
	ret

.PlaceTableGrid:
	ld de, SCREEN_WIDTH
	ld b, 3
.placegrid_outerloop
	push hl
	ld c, 3
.placegrid_innerloop
	ld [hli], a
	inc a
	dec c
	jr nz, .placegrid_innerloop
	pop hl
	add hl, de
	dec b
	jr nz, .placegrid_outerloop
	ret

.TableCoords:
; row 1
	dwcoord 7, 6
	dwcoord 10, 6
	dwcoord 13, 6
	dwcoord 16, 6

; row 2
	dwcoord 7, 9
	dwcoord 10, 9
	dwcoord 13, 9
	dwcoord 16, 9

; row 3
	dwcoord 7, 12
	dwcoord 10, 12
	dwcoord 13, 12
	dwcoord 16, 12

; row 4
	dwcoord 7, 15
	dwcoord 10, 15
	dwcoord 13, 15
	dwcoord 16, 15

.PlaceColumnNumbers:
; Set up the tile map for the column numbers.
	hlcoord 7, 1
	ld b, 4
	ld a, PICROSS_GFX_COLUMNS
.column_numbers_outer_loop
	push hl
	ld de, SCREEN_HEIGHT
	ld c, 4
.column_numbers_inner_loop
	ld [hli], a
	inc a
	ld [hli], a
	inc a
	ld [hl], a
	inc a
	add hl, de
	dec c
	jr nz, .column_numbers_inner_loop
	pop hl
	inc hl
	inc hl
	inc hl
	dec b
	jr nz, .column_numbers_outer_loop
	ret

.PlaceRowNumbers:
; Set up the tile map for the row numbers.
	hlcoord 1, 6
	ld b, 4
	ld a, PICROSS_GFX_ROWS
.row_numbers_loop1
	push hl
	push hl
	ld de, SCREEN_HEIGHT
	ld c, 3
.row_numbers_loop2
	ld [hli], a
	inc a
	ld [hli], a
	inc a
	ld [hl], a
	inc a
	add hl, de
	dec c
	jr nz, .row_numbers_loop2
	pop hl
	inc hl
	inc hl
	inc hl
	ld de, SCREEN_HEIGHT + 1
	ld c, 3
.row_numbers_loop3
	ld [hli], a
	inc a
	ld [hl], a
	inc a
	add hl, de
	dec c
	jr nz, .row_numbers_loop3
	pop hl
	ld de, $3c
	add hl, de
	dec b
	jr nz, .row_numbers_loop1
	ret

.HandleError:
	ld a, [wPicrossErrorCheck]
	and a
	ret z

; Print "ERROR" in the middle of the game display.
	ld hl, .ErrorBitmap
	decoord 0, 9
	ld c, .ErrorBitmapEnd - .ErrorBitmap

.print_message:
	ld a, [hli]
	add a, PICROSS_GFX_BGTILE
	ld [de], a
	inc de
	dec c
	jr nz, .print_message
	ret

.ErrorBitmap:
	pushc
	charmap " ", 0
	charmap "█", 1
	db " ██ ██  ██  ███ ██  "
	db " █  █ █ █ █ █ █ █ █ "
	db " ██ ██  ██  █ █ ██  "
	db " █  █ █ █ █ █ █ █ █ "
	db " ██ █ █ █ █ ███ █ █ "
	popc
.ErrorBitmapEnd:

Picross_CheckPuzzleSolved:
	ld de, wPicrossBitmap
	ld hl, wPicrossMarkedCells
	ld c, 0
.loop
	ld a, [de]
	and a
	jr z, .skip
	ld a, [hl]
	cp 1
	jr nz, .unsolved
	jr .continue
.skip
	ld a, [hl]
	cp 1
	jr z, .unsolved
.continue
	inc hl
	inc de
	dec c
	jr nz, .loop

; Puzzle is solved
	scf
	ret

.unsolved
	and a
	ret

Picross_ProcessJoypad:
	ld hl, hJoySum
	ld a, [hl]
	and A_BUTTON
	jr nz, .a_pressed

	ld a, [hl]
	and B_BUTTON
	jr nz, .b_pressed
	ret

.b_pressed
	ld a, 1
	jr .mark_cell

.a_pressed
	xor a

.mark_cell
	ld [wPicrossJoypadAction], a
	ld a, 1
	ld [wca59], a
	call Picross_DetermineGridCoord
	call Picross_MarkCell
	call Picross_InitDustObject
	ret

Picross_InitDustObject:
	ld hl, wPicrossCursorSpritePointer
	ld c, [hl]
	inc hl
	ld b, [hl]
	ld hl, SPRITEANIMSTRUCT_XCOORD
	add hl, bc
	ld e, [hl]
	inc e
	inc e
	inc e
	ld hl, SPRITEANIMSTRUCT_YCOORD
	add hl, bc
	ld d, [hl]
	inc d
	inc d
	ld a, [wPicrossCurrentCellType]
	cp PICROSS_COLORED_CELL
	ret nz

; Make dust object only if we are marking a cell
	ld a, SPRITE_ANIM_INDEX_MINIGAME_PICROSS_DUST
	call InitSpriteAnimStruct
	ret

Picross_DetermineGridCoord:
; Determines wPicrossCurrentGridNumber and wPicrossCurrentCellNumber
; from the cursor's current X and Y position.
	ld hl, wPicrossCursorSpritePointer
	ld c, [hl]
	inc hl
	ld b, [hl]

	ld hl, SPRITEANIMSTRUCT_XCOORD
	add hl, bc
	ld a, [hl]
	sub $40
	call .WriteXGridCoords

	ld hl, SPRITEANIMSTRUCT_YCOORD
	add hl, bc
	ld a, [hl]
	sub $40
	call .WriteYGridCoords
	ret

.WriteXGridCoords:
	cp $48
	jr nc, .fourth_grid_column
	cp $30
	jr nc, .third_grid_column
	cp $18
	jr nc, .second_grid_column

; first grid column
	call .WriteXCellCoords
	ld a, 0
	jr .grid_column_okay

.second_grid_column
	sub $18
	call .WriteXCellCoords
	ld a, 1
	jr .grid_column_okay

.third_grid_column
	sub $30
	call .WriteXCellCoords
	ld a, 2
	jr .grid_column_okay

.fourth_grid_column
	sub $48
	call .WriteXCellCoords
	ld a, 3

.grid_column_okay
	ld [wPicrossCurrentGridNumber], a
	ret

.WriteYGridCoords:
	cp $48
	jr nc, .fourth_grid_row
	cp $30
	jr nc, .third_grid_row
	cp $18
	jr nc, .second_grid_row

; first grid row
	call .WriteYCellCoords
	ld a, [wPicrossCurrentGridNumber]
	add a, 0
	jr .grid_row_okay

.second_grid_row
	sub $18
	call .WriteYCellCoords
	ld a, [wPicrossCurrentGridNumber]
	add a, 4
	jr .grid_row_okay

.third_grid_row
	sub $30
	call .WriteYCellCoords
	ld a, [wPicrossCurrentGridNumber]
	add a, 8
	jr .grid_row_okay

.fourth_grid_row
	sub $48
	call .WriteYCellCoords
	ld a, [wPicrossCurrentGridNumber]
	add a, 12

.grid_row_okay
	ld [wPicrossCurrentGridNumber], a
	ret

.WriteXCellCoords:
	cp $12
	jr z, .fourth_cell_column
	cp $c
	jr z, .third_cell_column
	cp $6
	jr z, .second_cell_column

; first cell column
	ld a, 0
	jr .cell_column_okay

.second_cell_column
	ld a, 1
	jr .cell_column_okay

.third_cell_column
	ld a, 2
	jr .cell_column_okay

.fourth_cell_column
	ld a, 3

.cell_column_okay
	ld [wPicrossCurrentCellNumber], a
	ret

.WriteYCellCoords:
	cp $12
	jr z, .fourth_cell_row
	cp $c
	jr z, .third_cell_row
	cp $6
	jr z, .second_cell_row

; first cell row
	ld a, [wPicrossCurrentCellNumber]
	add a, 0
	jr .cell_row_okay

.second_cell_row
	ld a, [wPicrossCurrentCellNumber]
	add a, 4
	jr .cell_row_okay

.third_cell_row
	ld a, [wPicrossCurrentCellNumber]
	add a, 8
	jr .cell_row_okay

.fourth_cell_row
	ld a, [wPicrossCurrentCellNumber]
	add a, 12

.cell_row_okay
	ld [wPicrossCurrentCellNumber], a
	ret

Picross_MarkCell:
	call Picross_SetTargetCellType
	ld hl, vPicrossPlayArea

; space between each grid in VRAM
	ld de, 9 tiles
	ld a, [wPicrossCurrentGridNumber]

.find_tile
	and a
	jr z, .update_tile
	dec a
	add hl, de
	jr .find_tile

.update_tile
	push hl
	ld e, l
	ld d, h
	ld hl, wPicrossRowGFX2bppBuffer
	ld c, 9
	ld b, BANK(@)
	call Request2bpp
	call Picross_SetMarkedCellGFX
	pop hl

	ld de, wPicrossRowGFX2bppBuffer
	ld c, 9
	ld b, BANK(@)
	call Request2bpp
	ret

Picross_SetTargetCellType:
	ld a, [wPicrossCurrentGridNumber]
	ld d, a
	and 12
	swap a
	ld e, a
	ld a, d
	and 3
	sla a
	sla a
	or e
	ld e, a

	ld a, [wPicrossCurrentCellNumber]
	ld d, a
	and 12
	sla a
	sla a
	or e
	ld e, a
	ld a, d
	and 3
	or e
	ld e, a
	ld d, 0

	ld hl, wPicrossMarkedCells
	add hl, de
	ldh a, [hJoypadState]
	and %11111100
	jr z, .check_b_pressed

	ld a, [wPicrossCurrentCellType]
	ld [hl], a
	ret

.check_b_pressed
	ld a, [wPicrossJoypadAction]
	and a
	jr z, .check_a_pressed
	ld a, [hl]
	cp B_BUTTON
	jr z, .done

	ld a, PICROSS_MARKED_CELL
	ld [wPicrossCurrentCellType], a
	ld [hl], a
	ret

.check_a_pressed
	ld a, [hl]
	cp A_BUTTON
	jr z, .done

	ld a, PICROSS_COLORED_CELL
	ld [wPicrossCurrentCellType], a
	ld [hl], a
	ret

.done
	xor a ; PICROSS_BLANK_CELL
	ld [wPicrossCurrentCellType], a
	ld [hl], a
	ret

Picross_SetMarkedCellGFX:
	ld a, [wPicrossCurrentCellNumber]
	ld e, a
	ld d, 0
	ld hl, .BufferRoutineTable
	add hl, de
	add hl, de
	add hl, de
	add hl, de
	ld a, [hli]
	ld [wPicrossBase2bppPointer], a
	ld a, [hli]
	ld [wPicrossBase2bppPointer + 1], a
	ld a, [hli]
	ld h, [hl]
	ld l, a
	jp hl

.BufferRoutineTable:
	dw wPicrossRowGFX2bppBuffer,       Picross_SetCellGFX_1
	dw wPicrossRowGFX2bppBuffer,       Picross_SetCellGFX_2
	dw wPicrossRowGFX2bppBuffer + $10, Picross_SetCellGFX_3
	dw wPicrossRowGFX2bppBuffer + $20, Picross_SetCellGFX_4

	dw wPicrossRowGFX2bppBuffer,       Picross_SetCellGFX_5
	dw wPicrossRowGFX2bppBuffer,       Picross_SetCellGFX_6
	dw wPicrossRowGFX2bppBuffer + $10, Picross_SetCellGFX_7
	dw wPicrossRowGFX2bppBuffer + $20, Picross_SetCellGFX_8

	dw wPicrossRowGFX2bppBuffer + $30, Picross_SetCellGFX_9
	dw wPicrossRowGFX2bppBuffer + $30, Picross_SetCellGFX_10
	dw wPicrossRowGFX2bppBuffer + $40, Picross_SetCellGFX_11
	dw wPicrossRowGFX2bppBuffer + $50, Picross_SetCellGFX_12

	dw wPicrossRowGFX2bppBuffer + $60, Picross_SetCellGFX_13
	dw wPicrossRowGFX2bppBuffer + $60, Picross_SetCellGFX_14
	dw wPicrossRowGFX2bppBuffer + $70, Picross_SetCellGFX_15
	dw wPicrossRowGFX2bppBuffer + $80, Picross_SetCellGFX_16

Picross_InitPicrossTable:
; Pick between 4 available layouts at random.
	call Random
	and 3

	call Picross_LoadLayout
	call Picross_GoThroughLayout
	call Picross_PopulateRAMBitmap
	ret

Picross_PopulateRAMBitmap:
	ld de, wPicrossBitmap

; Populate top half
	ld hl, wPicrossLayoutBuffer
	ld c, 8
.tiles
	push bc
	push hl
	call .PopulateOneRow
	ld bc, LEN_2BPP_TILE
	add hl, bc
	call .PopulateOneRow
	pop hl
	inc hl
	inc hl
	pop bc
	dec c
	jr nz, .tiles

; Populate bottom half
	ld hl, wPicrossLayoutBuffer2
	ld c, 8
.tiles2
	push bc
	push hl
	call .PopulateOneRow
	ld bc, LEN_2BPP_TILE
	add hl, bc
	call .PopulateOneRow
	pop hl
	inc hl
	inc hl
	pop bc
	dec c
	jr nz, .tiles2
	ret

.PopulateOneRow:
	ld b, [hl]
	ld c, 8
.loop
	ld a, 0
	sla b
	rl a
	ld [de], a
	inc de
	dec c
	jr nz, .loop
	ret

Picross_GoThroughLayout:
	ld hl, wPicrossLayoutBuffer - 1
	ld c, $20
.loop
	ld a, [hli]
	and [hl]
	ld [hli], a
	dec c
	jr nz, .loop
	ret

Picross_InitPicrossDigits:
	call Picross_InitDigitBuffer
	call Picross_CountColumnDigits
	jr c, .errored

	call Picross_ApplyColumnDigits
	call Picross_InitDigitBuffer
	call Picross_CountRowDigits
	jr c, .errored

	call Picross_ApplyRowDigits
	xor a
	ld [wPicrossErrorCheck], a
	ret

.errored
	ld a, 1
	ld [wPicrossErrorCheck], a
	ret

Picross_InitDigitBuffer:
	ld hl, wPicrossNumbersBuffer
	ld c, 0
	ld a, $ff
.loop
	ld [hli], a
	dec c
	jr nz, .loop
	ret

Picross_CountColumnDigits:
	ld hl, wPicrossBitmap
	ld de, wPicrossNumbersBuffer
	ld c, $10
.loop
	push bc
	push hl
	push de
	call .CountDigits
	jr nc, .error
	pop de
	ld hl, 5
	add hl, de
	ld e, l
	ld d, h
	pop hl
	inc hl
	pop bc
	dec c
	jr nz, .loop
	and a
	ret

.error
	pop de
	pop hl
	pop bc
	scf
	ret

.CountDigits:
	xor a
	ld [de], a
	ld b, 0
	ld c, $11
.loop1
	call .determine
	ret c
	and a
	jr z, .loop1
	xor a
	ld [de], a
.loop2
	ld a, [de]
	inc a
	ld [de], a
	call .determine
	ret c
	and a
	jr nz, .loop2
	inc de
	inc b
	ld a, b
	cp 5
	jr c, .loop1
	and a
	ret

.determine
	push de
	ld a, [hl]
	ld de, $10
	add hl, de
	pop de
	dec c
	jr z, .skip
	and a
	ret

.skip
	scf
	ret

Picross_CountRowDigits:
	ld hl, wPicrossBitmap
	ld de, wPicrossNumbersBuffer
	ld c, $10
.loop
	push bc
	push hl
	push de
	call .CountDigits
	jr nc, .error
	pop de
	ld hl, 6
	add hl, de
	ld e, l
	ld d, h
	pop hl
	ld bc, $10
	add hl, bc
	pop bc
	dec c
	jr nz, .loop
	and a
	ret

.error
	pop de
	pop hl
	pop bc
	scf
	ret

.CountDigits:
	xor a
	ld [de], a
	ld b, 0
	ld c, 17
.loop1
	call .determine
	ret c
	and a
	jr z, .loop1
	xor a
	ld [de], a
.loop2
	ld a, [de]
	inc a
	ld [de], a
	call .determine
	ret c
	and a
	jr nz, .loop2
	inc de
	inc b
	ld a, b
	cp 6
	jr c, .loop1
	and a
	ret
.determine
	ld a, [hli]
	dec c
	jr z, .skip
	and a
	ret

.skip
	scf
	ret

Picross_ApplyColumnDigits:
	ld c, 80
	ld hl, Picross_VRAMAndDigitRowPointers
	call Picross_ApplyDigits
	ret

Picross_VRAMAndDigitRowPointers:
; A huge table of VRAM locations and the subroutine to apply to it.
; This is used for printing the row digits in the proper positions.

; row 0
	dw vPicrossBackground tile  4, Picross_SetCellGFX_1
	dw vPicrossBackground tile  4, Picross_SetCellGFX_5
	dw vPicrossBackground tile  7, Picross_SetCellGFX_9
	dw vPicrossBackground tile 10, Picross_SetCellGFX_13
	dw vPicrossBackground tile 13, Picross_SetCellGFX_1
; row 1
	dw vPicrossBackground tile  4, Picross_SetCellGFX_2
	dw vPicrossBackground tile  4, Picross_SetCellGFX_6
	dw vPicrossBackground tile  7, Picross_SetCellGFX_10
	dw vPicrossBackground tile 10, Picross_SetCellGFX_14
	dw vPicrossBackground tile 13, Picross_SetCellGFX_2
; row 2
	dw vPicrossBackground tile  5, Picross_SetCellGFX_3
	dw vPicrossBackground tile  5, Picross_SetCellGFX_7
	dw vPicrossBackground tile  8, Picross_SetCellGFX_11
	dw vPicrossBackground tile 11, Picross_SetCellGFX_15
	dw vPicrossBackground tile 14, Picross_SetCellGFX_3
; row 3
	dw vPicrossBackground tile  6, Picross_SetCellGFX_4
	dw vPicrossBackground tile  6, Picross_SetCellGFX_8
	dw vPicrossBackground tile  9, Picross_SetCellGFX_12
	dw vPicrossBackground tile 12, Picross_SetCellGFX_16
	dw vPicrossBackground tile 15, Picross_SetCellGFX_4
; row 4
	dw vPicrossBackground tile 16, Picross_SetCellGFX_1
	dw vPicrossBackground tile 16, Picross_SetCellGFX_5
	dw vPicrossBackground tile 19, Picross_SetCellGFX_9
	dw vPicrossBackground tile 22, Picross_SetCellGFX_13
	dw vPicrossBackground tile 25, Picross_SetCellGFX_1
; row 5
	dw vPicrossBackground tile 16, Picross_SetCellGFX_2
	dw vPicrossBackground tile 16, Picross_SetCellGFX_6
	dw vPicrossBackground tile 19, Picross_SetCellGFX_10
	dw vPicrossBackground tile 22, Picross_SetCellGFX_14
	dw vPicrossBackground tile 25, Picross_SetCellGFX_2
	dw vPicrossBackground tile 17, Picross_SetCellGFX_3
; row 6
	dw vPicrossBackground tile 17, Picross_SetCellGFX_7
	dw vPicrossBackground tile 20, Picross_SetCellGFX_11
	dw vPicrossBackground tile 23, Picross_SetCellGFX_15
	dw vPicrossBackground tile 26, Picross_SetCellGFX_3
	dw vPicrossBackground tile 18, Picross_SetCellGFX_4
; row 7
	dw vPicrossBackground tile 18, Picross_SetCellGFX_8
	dw vPicrossBackground tile 21, Picross_SetCellGFX_12
	dw vPicrossBackground tile 24, Picross_SetCellGFX_16
	dw vPicrossBackground tile 27, Picross_SetCellGFX_4
	dw vPicrossBackground tile 28, Picross_SetCellGFX_1
; row 8
	dw vPicrossBackground tile 28, Picross_SetCellGFX_5
	dw vPicrossBackground tile 31, Picross_SetCellGFX_9
	dw vPicrossBackground tile 34, Picross_SetCellGFX_13
	dw vPicrossBackground tile 37, Picross_SetCellGFX_1
	dw vPicrossBackground tile 28, Picross_SetCellGFX_2
; row 9
	dw vPicrossBackground tile 28, Picross_SetCellGFX_6
	dw vPicrossBackground tile 31, Picross_SetCellGFX_10
	dw vPicrossBackground tile 34, Picross_SetCellGFX_14
; row 10
	dw vPicrossBackground tile 37, Picross_SetCellGFX_2
	dw vPicrossBackground tile 29, Picross_SetCellGFX_3
	dw vPicrossBackground tile 29, Picross_SetCellGFX_7
	dw vPicrossBackground tile 32, Picross_SetCellGFX_11
	dw vPicrossBackground tile 35, Picross_SetCellGFX_15
	dw vPicrossBackground tile 38, Picross_SetCellGFX_3
; row 11
	dw vPicrossBackground tile 30, Picross_SetCellGFX_4
	dw vPicrossBackground tile 30, Picross_SetCellGFX_8
	dw vPicrossBackground tile 33, Picross_SetCellGFX_12
	dw vPicrossBackground tile 36, Picross_SetCellGFX_16
	dw vPicrossBackground tile 39, Picross_SetCellGFX_4
; row 12
	dw vPicrossBackground tile 40, Picross_SetCellGFX_1
	dw vPicrossBackground tile 40, Picross_SetCellGFX_5
	dw vPicrossBackground tile 43, Picross_SetCellGFX_9
	dw vPicrossBackground tile 46, Picross_SetCellGFX_13
	dw vPicrossBackground tile 49, Picross_SetCellGFX_1
; row 13
	dw vPicrossBackground tile 40, Picross_SetCellGFX_2
	dw vPicrossBackground tile 40, Picross_SetCellGFX_6
	dw vPicrossBackground tile 43, Picross_SetCellGFX_10
	dw vPicrossBackground tile 46, Picross_SetCellGFX_14
	dw vPicrossBackground tile 49, Picross_SetCellGFX_2
; row 14
	dw vPicrossBackground tile 41, Picross_SetCellGFX_3
	dw vPicrossBackground tile 41, Picross_SetCellGFX_7
	dw vPicrossBackground tile 44, Picross_SetCellGFX_11
	dw vPicrossBackground tile 47, Picross_SetCellGFX_15
	dw vPicrossBackground tile 50, Picross_SetCellGFX_3
; row 15
	dw vPicrossBackground tile 42, Picross_SetCellGFX_4
	dw vPicrossBackground tile 42, Picross_SetCellGFX_8
	dw vPicrossBackground tile 45, Picross_SetCellGFX_12
	dw vPicrossBackground tile 48, Picross_SetCellGFX_16
	dw vPicrossBackground tile 51, Picross_SetCellGFX_4

Picross_ApplyRowDigits:
	ld c, 96
	ld hl, Picross_VRAMAndDigitColumnPointers
	call Picross_ApplyDigits
	ret

Picross_VRAMAndDigitColumnPointers:
; Another huge table of VRAM locations and the subroutine to apply to it.
; Used for printing the column digits in the proper positions.

; column 0
	dw vPicrossBackground tile  52, Picross_SetCellGFX_1
	dw vPicrossBackground tile  52, Picross_SetCellGFX_2
	dw vPicrossBackground tile  53, Picross_SetCellGFX_3
	dw vPicrossBackground tile  54, Picross_SetCellGFX_4
	dw vPicrossBackground tile  61, Picross_SetCellGFX_1
	dw vPicrossBackground tile  61, Picross_SetCellGFX_2
; column 1
	dw vPicrossBackground tile  52, Picross_SetCellGFX_5
	dw vPicrossBackground tile  52, Picross_SetCellGFX_6
	dw vPicrossBackground tile  53, Picross_SetCellGFX_7
	dw vPicrossBackground tile  54, Picross_SetCellGFX_8
	dw vPicrossBackground tile  61, Picross_ApplyDigit_17
	dw vPicrossBackground tile  61, Picross_ApplyDigit_18
; column 2
	dw vPicrossBackground tile  55, Picross_SetCellGFX_9
	dw vPicrossBackground tile  55, Picross_SetCellGFX_10
	dw vPicrossBackground tile  56, Picross_SetCellGFX_11
	dw vPicrossBackground tile  57, Picross_SetCellGFX_12
	dw vPicrossBackground tile  63, Picross_ApplyDigit_19
	dw vPicrossBackground tile  63, Picross_ApplyDigit_20
; column 3
	dw vPicrossBackground tile  58, Picross_SetCellGFX_13
	dw vPicrossBackground tile  58, Picross_SetCellGFX_14
	dw vPicrossBackground tile  59, Picross_SetCellGFX_15
	dw vPicrossBackground tile  60, Picross_SetCellGFX_16
	dw vPicrossBackground tile  65, Picross_SetCellGFX_13
	dw vPicrossBackground tile  65, Picross_SetCellGFX_14
; column 4
	dw vPicrossBackground tile  67, Picross_SetCellGFX_1
	dw vPicrossBackground tile  67, Picross_SetCellGFX_2
	dw vPicrossBackground tile  68, Picross_SetCellGFX_3
	dw vPicrossBackground tile  69, Picross_SetCellGFX_4
	dw vPicrossBackground tile  76, Picross_SetCellGFX_1
	dw vPicrossBackground tile  76, Picross_SetCellGFX_2
; column 5
	dw vPicrossBackground tile  67, Picross_SetCellGFX_5
	dw vPicrossBackground tile  67, Picross_SetCellGFX_6
	dw vPicrossBackground tile  68, Picross_SetCellGFX_7
	dw vPicrossBackground tile  69, Picross_SetCellGFX_8
	dw vPicrossBackground tile  76, Picross_ApplyDigit_17
	dw vPicrossBackground tile  76, Picross_ApplyDigit_18
; column 6
	dw vPicrossBackground tile  70, Picross_SetCellGFX_9
	dw vPicrossBackground tile  70, Picross_SetCellGFX_10
	dw vPicrossBackground tile  71, Picross_SetCellGFX_11
	dw vPicrossBackground tile  72, Picross_SetCellGFX_12
	dw vPicrossBackground tile  78, Picross_ApplyDigit_19
	dw vPicrossBackground tile  78, Picross_ApplyDigit_20
; column 7
	dw vPicrossBackground tile  73, Picross_SetCellGFX_13
	dw vPicrossBackground tile  73, Picross_SetCellGFX_14
	dw vPicrossBackground tile  74, Picross_SetCellGFX_15
	dw vPicrossBackground tile  75, Picross_SetCellGFX_16
	dw vPicrossBackground tile  80, Picross_SetCellGFX_13
	dw vPicrossBackground tile  80, Picross_SetCellGFX_14
; column 8
	dw vPicrossBackground tile  82, Picross_SetCellGFX_1
	dw vPicrossBackground tile  82, Picross_SetCellGFX_2
	dw vPicrossBackground tile  83, Picross_SetCellGFX_3
	dw vPicrossBackground tile  84, Picross_SetCellGFX_4
	dw vPicrossBackground tile  91, Picross_SetCellGFX_1
	dw vPicrossBackground tile  91, Picross_SetCellGFX_2
; column 9
	dw vPicrossBackground tile  82, Picross_SetCellGFX_5
	dw vPicrossBackground tile  82, Picross_SetCellGFX_6
	dw vPicrossBackground tile  83, Picross_SetCellGFX_7
	dw vPicrossBackground tile  84, Picross_SetCellGFX_8
	dw vPicrossBackground tile  91, Picross_ApplyDigit_17
	dw vPicrossBackground tile  91, Picross_ApplyDigit_18
; column 10
	dw vPicrossBackground tile  85, Picross_SetCellGFX_9
	dw vPicrossBackground tile  85, Picross_SetCellGFX_10
	dw vPicrossBackground tile  86, Picross_SetCellGFX_11
	dw vPicrossBackground tile  87, Picross_SetCellGFX_12
	dw vPicrossBackground tile  93, Picross_ApplyDigit_19
	dw vPicrossBackground tile  93, Picross_ApplyDigit_20
; column 11
	dw vPicrossBackground tile  88, Picross_SetCellGFX_13
	dw vPicrossBackground tile  88, Picross_SetCellGFX_14
	dw vPicrossBackground tile  89, Picross_SetCellGFX_15
	dw vPicrossBackground tile  90, Picross_SetCellGFX_16
	dw vPicrossBackground tile  95, Picross_SetCellGFX_13
	dw vPicrossBackground tile  95, Picross_SetCellGFX_14
; column 12
	dw vPicrossBackground tile  97, Picross_SetCellGFX_1
	dw vPicrossBackground tile  97, Picross_SetCellGFX_2
	dw vPicrossBackground tile  98, Picross_SetCellGFX_3
	dw vPicrossBackground tile  99, Picross_SetCellGFX_4
	dw vPicrossBackground tile 106, Picross_SetCellGFX_1
	dw vPicrossBackground tile 106, Picross_SetCellGFX_2
; column 13
	dw vPicrossBackground tile  97, Picross_SetCellGFX_5
	dw vPicrossBackground tile  97, Picross_SetCellGFX_6
	dw vPicrossBackground tile  98, Picross_SetCellGFX_7
	dw vPicrossBackground tile  99, Picross_SetCellGFX_8
	dw vPicrossBackground tile 106, Picross_ApplyDigit_17
	dw vPicrossBackground tile 106, Picross_ApplyDigit_18
; column 14
	dw vPicrossBackground tile 100, Picross_SetCellGFX_9
	dw vPicrossBackground tile 100, Picross_SetCellGFX_10
	dw vPicrossBackground tile 101, Picross_SetCellGFX_11
	dw vPicrossBackground tile 102, Picross_SetCellGFX_12
	dw vPicrossBackground tile 108, Picross_ApplyDigit_19
	dw vPicrossBackground tile 108, Picross_ApplyDigit_20
; column 15
	dw vPicrossBackground tile 103, Picross_SetCellGFX_13
	dw vPicrossBackground tile 103, Picross_SetCellGFX_14
	dw vPicrossBackground tile 104, Picross_SetCellGFX_15
	dw vPicrossBackground tile 105, Picross_SetCellGFX_16
	dw vPicrossBackground tile 110, Picross_SetCellGFX_13
	dw vPicrossBackground tile 110, Picross_SetCellGFX_14

Picross_ApplyDigits:
	xor a
	ld [wPicrossDrawingRoutineCounter], a
.loop
	push bc
	push hl
	call Picross_ApplyDigit
	pop hl
	inc hl
	inc hl
	inc hl
	inc hl
	pop bc
	dec c
	jr nz, .loop
	ret

Picross_ApplyDigit:
	push hl
	ld hl, wPicrossDrawingRoutineCounter
	ld e, [hl]
	inc [hl]
	ld d, 0
	ld hl, wPicrossNumbersBuffer
	add hl, de
	ld a, [hl]
	cp $ff
	jr z, .done_applying_digit

	swap a
	and $f0
	ld e, a
	ld a, [hl]
	swap a
	and $f
	ld d, a

	ld hl, PicrossNumbersGFX
	add hl, de
	ld a, l
	ld [wPicrossBaseGFXPointer], a
	ld a, h
	ld [wPicrossBaseGFXPointer+1], a
	pop hl

	ld a, [hli]
	ld [wPicrossBase2bppPointer], a
	ld a, [hli]
	ld [wPicrossBase2bppPointer+1], a
	ld a, [hli]
	ld h, [hl]
	ld l, a
	jp hl

.done_applying_digit
	pop hl
	ret

Picross_SetCellGFX_1:
	call Picross_SetTileGFXPointerDE
	ld hl, 0
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_SetCellGFX_2:
	call Picross_SetTileGFXPointerDE
	ld hl, 0
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopRight1

	call Picross_SetTileGFXPointerDE
	ld hl, $10
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopLeft1

	ret

Picross_SetCellGFX_3:
	call Picross_SetTileGFXPointerDE
	ld hl, 0
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopRight2

	call Picross_SetTileGFXPointerDE
	ld hl, $10
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopLeft2

	ret

Picross_SetCellGFX_4:
	call Picross_SetTileGFXPointerDE
	ld hl, 0
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopRightFull
	ret

Picross_SetCellGFX_5:
	call Picross_SetTileGFXPointerDE
	ld hl, 12
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopLeftFull
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_SetCellGFX_6:
	call Picross_SetTileGFXPointerDE
	ld hl, 12
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopRight1
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopRight1
	call Picross_SetTileGFXPointerDE
	ld hl, $1c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopLeft1
	ld hl, $40
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopLeft1
	ret

Picross_SetCellGFX_7:
	call Picross_SetTileGFXPointerDE
	ld hl, $c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopRight2
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopRight2
	call Picross_SetTileGFXPointerDE
	ld hl, $1c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopLeft2
	ld hl, $40
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopLeft2
	ret

Picross_SetCellGFX_8:
	call Picross_SetTileGFXPointerDE
	ld hl, $c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopRightFull
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopRightFull
	ret

Picross_SetCellGFX_9:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopLeftFull
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_SetCellGFX_10:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopRight1
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopRight1
	call Picross_SetTileGFXPointerDE
	ld hl, $18
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopLeft1
	ld hl, $40
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopLeft1
	ret

Picross_SetCellGFX_11:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopRight2
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopRight2
	call Picross_SetTileGFXPointerDE
	ld hl, $18
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopLeft2
	ld hl, $40
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopLeft2
	ret

Picross_SetCellGFX_12:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopRightFull
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopRightFull
	ret

Picross_SetCellGFX_13:
	call Picross_SetTileGFXPointerDE
	ld hl, 4
	call Picross_AddToHL2bppPointer
	ld c, $a
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_SetCellGFX_14:
	call Picross_SetTileGFXPointerDE
	ld hl, 4
	call Picross_AddToHL2bppPointer
	ld c, $a
	call Picross_ApplyTileGFXTopRight1
	call Picross_SetTileGFXPointerDE
	ld hl, $14
	call Picross_AddToHL2bppPointer
	ld c, $a
	call Picross_ApplyTileGFXTopLeft1
	ret

Picross_SetCellGFX_15:
	call Picross_SetTileGFXPointerDE
	ld hl, 4
	call Picross_AddToHL2bppPointer
	ld c, $a
	call Picross_ApplyTileGFXTopRight2
	call Picross_SetTileGFXPointerDE
	ld hl, $14
	call Picross_AddToHL2bppPointer
	ld c, $a
	call Picross_ApplyTileGFXTopLeft2
	ret

Picross_SetCellGFX_16:
	call Picross_SetTileGFXPointerDE
	ld hl, 4
	call Picross_AddToHL2bppPointer
	ld c, 10
	call Picross_ApplyTileGFXTopRightFull
	ret

Picross_ApplyDigit_17:
	call Picross_SetTileGFXPointerDE
	ld hl, $c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopLeftFull
	ld hl, $20
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_ApplyDigit_18:
	call Picross_SetTileGFXPointerDE
	ld hl, $c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopRight1
	ld hl, $20
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopRight1
	call Picross_SetTileGFXPointerDE
	ld hl, $1c
	call Picross_AddToHL2bppPointer
	ld c, 4
	call Picross_ApplyTileGFXTopLeft1
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 6
	call Picross_ApplyTileGFXTopLeft1
	ret

Picross_ApplyDigit_19:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopLeftFull
	ld hl, $20
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopLeftFull
	ret

Picross_ApplyDigit_20:
	call Picross_SetTileGFXPointerDE
	ld hl, 8
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopRight1
	ld hl, $20
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopRight1
	call Picross_SetTileGFXPointerDE
	ld hl, $18
	call Picross_AddToHL2bppPointer
	ld c, 8
	call Picross_ApplyTileGFXTopLeft1
	ld hl, $30
	call Picross_AddToHL2bppPointer
	ld c, 2
	call Picross_ApplyTileGFXTopLeft1
	ret

Picross_AddToHL2bppPointer:
	push de
	ld a, [wPicrossBase2bppPointer]
	ld e, a
	ld a, [wPicrossBase2bppPointer+1]
	ld d, a
	add hl, de
	pop de
	ret

Picross_ApplyTileGFXTopLeftFull:
	ld a, [de]
	inc de
	ld b, $f8
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopLeftFull
	ret

Picross_ApplyTileGFXTopRight1:
	ld a, [de]
	inc de
	rlca
	rlca
	ld b, 3
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopRight1
	ret

Picross_ApplyTileGFXTopLeft1:
	ld a, [de]
	inc de
	sla a
	sla a
	ld b, $e0
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopLeft1
	ret

Picross_ApplyTileGFXTopRight2:
	ld a, [de]
	inc de
	swap a
	ld b, $f
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopRight2
	ret

Picross_ApplyTileGFXTopLeft2:
	ld a, [de]
	inc de
	swap a
	ld b, $80
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopLeft2
	ret

Picross_ApplyTileGFXTopRightFull:
	ld a, [de]
	inc de
	srl a
	srl a
	ld b, 62
	call Picross_ApplyTileGFXCommon
	dec c
	jr nz, Picross_ApplyTileGFXTopRightFull
	ret

Picross_ApplyTileGFXCommon:
	push de
	push af
	ld a, [wPicrossCurrentCellType]
	cp -1
	jr nz, .skip
	pop af
	and b
	ld e, a
	ld a, [hl]
	or e
	ld [hl], a
	jr .finish

.skip
	ld a, b
	xor $ff
	ld e, a
	ld a, [hl]
	and e
	ld [hl], a
	pop af
	and b
	ld e, a
	ld a, [hl]
	or e
	ld [hl], a
	jr .finish

.asm_e3516 ; unreferenced
	ld a, b
	xor $ff
	ld e, a
	ld a, [hl]
	and e
	ld [hl], a
	pop af

.finish
	pop de
	inc hl
	ret

Picross_SetTileGFXPointerDE:
	ld a, [wPicrossCurrentCellType]
	cp -1
	jr z, .skip
	and 3
	ld e, a
	ld d, 0
	ld hl, .CellGFX
	add hl, de
	add hl, de
	ld e, [hl]
	inc hl
	ld d, [hl]
	ret

.skip:
	ld a, [wPicrossBaseGFXPointer]
	ld e, a
	ld a, [wPicrossBaseGFXPointer+1]
	ld d, a
	ret

.CellGFX:
; A "tileset" of 5x5 dots to place over the grid GFX.
	dw .empty
	dw .colored
	dw .marked
	dw .empty

pusho
opt g.oX# ; . = 0, o = 1, X = 2, # = 3
.empty:
	dw `........
	dw `.oo.o...
	dw `.o..o...
	dw `....o...
	dw `.oooo...

.colored:
	dw `#####...
	dw `#XXXX...
	dw `#XXXX...
	dw `#XXXX...
	dw `#XXXX...

.marked:
	dw `........
	dw `.XoXo...
	dw `.oX.o...
	dw `.X.Xo...
	dw `.oooo...
popo

Picross_LoadLayout:
	ld l, a
	ld h, 0
	add hl, hl
	add hl, hl
	ld de, PicrossSprites
	add hl, de
	ld a, [hli]
	and a
	jr z, .normal_pattern

.tileset_pattern
; Pattern originates from a tileset.
; Its bottom half GFX is exactly $100 apart from the top half.
	ld b, [hl]
	inc hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld de, wPicrossLayoutBuffer - 1
	ld a, b
	ld bc, 2 tiles
	push bc
	push af
	push hl
	call FarCopyData
	pop hl

; apply offset of bottom half GFX
	ld bc, $100
	add hl, bc
	pop af
	pop bc
	call FarCopyData
	ret

.normal_pattern
; Pattern originates from regular 2bpp data.
; Its bottom half GFX directly follows the top half.
	ld b, [hl]
	inc hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld de, wPicrossLayoutBuffer - 1
	ld a, b
	ld bc, 4 tiles
	call FarCopyData
	ret

picross_pattern: MACRO
if _NARG > 2
	db \1
	dbw \2, \3
else
	db \1
	dba \2
endc
ENDM

PicrossSprites:
	picross_pattern PATTERN_NORMAL, DiglettIcon
	picross_pattern PATTERN_NORMAL, GengarIcon
	picross_pattern PATTERN_NORMAL, AnnonIcon
	picross_pattern PATTERN_NORMAL, SnorlaxIcon
; TODO: Setting fixed bank $C for now before Tileset_0c_GFX is ripped
	picross_pattern PATTERN_TILESET, $c, Tileset_0c_GFX + $200
	picross_pattern PATTERN_NORMAL, PoliwrathSpriteGFX