summaryrefslogtreecommitdiff
path: root/pokemontools/crystal.py
blob: d08c15d833a51a13d19a2b429e069c03259ef9ba (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
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
# -*- coding: utf-8 -*-
"""
utilities to help disassemble pokémon crystal
"""
from __future__ import print_function
from __future__ import absolute_import

import os
import sys
import inspect
import json
from copy import copy, deepcopy
import subprocess
import logging

# for capwords
import string

# for python2.6
if not hasattr(json, "dumps"):
    json.dumps = json.write

spacing = "\t"

lousy_dragon_shrine_hack = [0x18d079, 0x18d0a9, 0x18d061, 0x18d091]

# table of pointers to map groups
# each map group contains some number of map headers
map_group_pointer_table = 0x94000
map_group_count = 26
map_group_offsets = []
map_header_byte_size = 9
second_map_header_byte_size = 12

# event segment sizes
warp_byte_size = 5
trigger_byte_size = 8
signpost_byte_size = 5
people_event_byte_size = 13

max_texts = 3
text_count = 0
texts = []

# these appear outside of quotes (see pokered/extras/pretty_map_headers.py)
# this doesn't do anything but is still used in TextScript
constant_abbreviation_bytes = {}

from . import helpers
from . import chars
from . import labels
from . import pksv
from . import romstr
from . import pointers
from . import interval_map
from . import trainers
from . import move_constants
from . import pokemon_constants
from . import item_constants
from . import wram
from . import exceptions

from . import addresses
is_valid_address = addresses.is_valid_address

from . import old_text_script
OldTextScript = old_text_script

from . import configuration
conf = configuration.Config()

data_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data/pokecrystal/")
conf.wram = os.path.join(data_path, "wram.asm")
conf.gbhw = os.path.join(data_path, "gbhw.asm")
conf.hram = os.path.join(data_path, "hram.asm")

from .map_names import map_names
from .song_names import song_names

# ---- script_parse_table explanation ----
# This is an IntervalMap that keeps track of previously parsed scripts, texts
# and other objects. Anything that has a location in the ROM should be mapped
# to an interval (a range of addresses) in this structure. Each object that is
# assigned to an interval should implement attributes or methods like:
#   ATTRIBUTE/METHOD            EXPLANATION
#   label                       what the heck to call the object
#   address                     where it begins
#   to_asm()                    spit out asm (not including label)
# keys are intervals "500..555" of byte addresses for each script
# last byte is not inclusive(?) really? according to who??
# this is how to make sure scripts are not recalculated
script_parse_table = interval_map.IntervalMap()

def is_script_already_parsed_at(address):
    """looks up whether or not a script is parsed at a certain address"""
    if script_parse_table[address] == None:
        return False
    return True

def script_parse_table_pretty_printer():
    """helpful debugging output"""
    for each in script_parse_table.items():
        logging.info("{0}".format(each))

def map_name_cleaner(input):
    """generate a valid asm label for a given map name"""
    return input.replace(":", "").\
                 replace("(", "").\
                 replace(")", "").\
                 replace("'", "").\
                 replace("/", "").\
                 replace(",", "").\
                 replace(".", "").\
                 replace("Pokémon Center", "PokeCenter").\
                 replace("é", "e").\
                 replace("-", "").\
                 replace("Hooh", "HoOh").\
                 replace("hooh", "HoOh").\
                 replace(" ", "")

rom = romstr.RomStr(None)

def direct_load_rom(filename=None):
    """loads bytes into memory"""
    if filename == None:
        filename = os.path.join(conf.path, "baserom.gbc")
    global rom
    file_handler = open(filename, "rb")
    rom = romstr.RomStr(file_handler.read())
    file_handler.close()
    return rom

def load_rom(filename=None):
    """checks that the loaded rom matches the path
    and then loads the rom if necessary."""
    if filename == None:
        filename = os.path.join(conf.path, "baserom.gbc")
    global rom
    if rom != romstr.RomStr(None) and rom != None:
        return rom
    if not isinstance(rom, romstr.RomStr):
        return direct_load_rom(filename=filename)
    elif os.lstat(filename).st_size != len(rom):
        return direct_load_rom(filename)

def direct_load_asm(filename=None):
    if filename == None:
        filename = os.path.join(conf.path, "main.asm")
    """returns asm source code (AsmList) from a file"""
    asm = open(filename, "r").read().split("\n")
    asm = romstr.AsmList(asm)
    return asm

def load_asm(filename=None):
    """returns asm source code (AsmList) from a file (uses a global)"""
    if filename == None:
        filename = os.path.join(conf.path, "main.asm")
    global asm
    asm = direct_load_asm(filename=filename)
    return asm

def load_asm2(filename="../main.asm"):
    """loads the asm source code into memory"""
    new_asm = Asm(filename=filename)
    return new_asm

def rom_interval(offset, length, rom=None, strings=True, debug=True):
    """returns hex values for the rom starting at offset until offset+length"""
    return rom.interval(offset, length, strings=strings, debug=debug)

def rom_until(offset, byte, rom=None, strings=True, debug=True):
    """returns hex values from rom starting at offset until the given byte"""
    return rom.until(offset, byte, strings=strings, debug=debug)

def how_many_until(byte, starting, rom):
    index = rom.find(byte, starting)
    return index - starting

def load_map_group_offsets(map_group_pointer_table, map_group_count, rom=None):
    """reads the map group table for the list of pointers"""
    map_group_offsets = [] # otherwise this method can only be used once
    data = rom.interval(map_group_pointer_table, map_group_count*2, strings=False)
    data = helpers.grouper(data)
    for pointer_parts in data:
        pointer = pointer_parts[0] + (pointer_parts[1] << 8)
        offset = pointer - 0x4000 + map_group_pointer_table
        map_group_offsets.append(offset)
    return map_group_offsets

def calculate_pointer_from_bytes_at(address, bank=False):
    """calculates a pointer from 2 bytes at a location
    or 3-byte pointer [bank][2-byte pointer] if bank=True"""
    if bank == True:
        bank = ord(rom[address])
        address += 1
    elif bank == False or bank == None:
        bank = pointers.calculate_bank(address)
    elif bank == "reverse" or bank == "reversed":
        bank = ord(rom[address+2])
    elif type(bank) == int:
        pass
    else:
        raise Exception("bad bank given to calculate_pointer_from_bytes_at")
    byte1 = ord(rom[address])
    byte2 = ord(rom[address+1])
    temp  = byte1 + (byte2 << 8)
    if temp == 0:
        return None
    return pointers.calculate_pointer(temp, bank)

def clean_up_long_info(long_info):
    """cleans up some data from parse_script_engine_script_at formatting issues"""
    long_info = str(long_info)
    # get rid of the first newline
    if long_info[0] == "\n":
        long_info = long_info[1:]
    # get rid of the last newline and any leftover space
    if long_info.count("\n") > 0:
        if long_info[long_info.rindex("\n")+1:].isspace():
            long_info = long_info[:long_info.rindex("\n")]
        # remove spaces+hash from the front of each line
        new_lines = []
        for line in long_info.split("\n"):
            line = line.strip()
            if line[0] == "#":
                line = line[1:]
            new_lines.append(line)
        long_info = "\n".join(new_lines)
    return long_info

def get_pokemon_constant_by_id(id):
    if id == 0:
        return None
    else:
        return pokemon_constants.pokemon_constants[id]

def command_debug_information(command_byte=None, map_group=None, map_id=None, address=0, info=None, long_info=None, pksv_name=None):
    "used to help debug in parse_script_engine_script_at"
    info1 = "parsing command byte " + hex(command_byte) + " for map " + \
          str(map_group) + "." + str(map_id) + " at " + hex(address)
    info1 += "    pksv: " + str(pksv_name)
    #info1 += "    info: " + str(info)
    #info1 += "    long_info: " + long_info
    return info1

all_texts = []
class TextScript(object):
    """
    A text is a sequence of bytes (and sometimes commands). It's not the same
    thing as a Script. The bytes are translated into characters based on the
    lookup table (see chars.py). The in-text commands are for including values
    from RAM, playing sound, etc.

    see: http://hax.iimarck.us/files/scriptingcodes_eng.htm#InText
    """
    base_label = "UnknownText_"
    def __init__(self, address, map_group=None, map_id=None, debug=False, label=None, force=False, show=None, script_parse_table=None, text_command_classes=None):
        self.text_command_classes = text_command_classes
        self.script_parse_table = script_parse_table

        self.address = address
        # $91, $84, $82, $54, $8c
        # 0x19768c is a a weird problem?
        if address in [0x26ef, 0x26f2, 0x6ee, 0x1071, 0x5ce33, 0x69523, 0x7ee98, 0x72176, 0x7a578, 0x19c09b, 0x19768c]:
            return None
        self.map_group, self.map_id, self.debug = map_group, map_id, debug
        self.dependencies = None
        self.commands = None
        self.force = force

        if is_script_already_parsed_at(address) and not force:
            raise Exception("TextScript already parsed at "+hex(address))

        if not label:
            label = self.base_label + hex(address)
        self.label = Label(name=label, address=address, object=self)

        self.parse()

    def is_valid(self):
        return not (self.address in [0x26ef, 0x26f2, 0x6ee, 0x1071, 0x5ce33, 0x69523, 0x7ee98, 0x72176, 0x7a578, 0x19c09b, 0x19768c])

    # hmm this looks exactly like Script.get_dependencies (which makes sense..)
    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.address in [0x26ef, 0x26f2, 0x6ee, 0x1071, 0x5ce33, 0x69523, 0x7ee98, 0x72176, 0x7a578, 0x19c09b, 0x19768c]:
            return []

        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies

        dependencies = []

        for command in self.commands:
            deps = command.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
            dependencies.extend(deps)

        self.dependencies = dependencies
        return self.dependencies

    # this is almost an exact copy of Script.parse
    # with the exception of using text_command_classes instead of command_classes
    def parse(self):
        if self.address in [0x26ef, 0x26f2, 0x6ee, 0x1071, 0x5ce33, 0x69523, 0x7ee98, 0x72176, 0x7a578, 0x19c09b, 0x19768c]:
            return None

        text_command_classes = self.text_command_classes
        script_parse_table = self.script_parse_table
        current_address = copy(self.address)
        start_address = copy(current_address)

        # don't clutter up my screen
        if self.debug:
            logging.debug(
                "NewTextScript.parse address={address} map_group={map_group} map_id={map_id}"
                .format(
                    address=hex(self.address),
                    map_group=str(self.map_group),
                    map_id=self.map_id,
                )
            )

        # load up the rom if it hasn't been loaded already
        rom = load_rom()

        # in the event that the script parsing fails.. it would be nice to leave evidence
        script_parse_table[start_address:start_address+1] = "incomplete NewTextScript.parse"

        # start with a blank script
        commands = []

        # use this to control the while loop
        end = False

        # for each command found...
        while not end:
            # get the current scripting byte
            cur_byte = ord(rom[current_address])

            # reset the command class (last command was probably different)
            scripting_command_class = None

            # match the command id byte to a scripting command class like MainText
            for class_ in text_command_classes:
                if class_[1].id == cur_byte:
                    scripting_command_class = class_[1]

            if self.address == 0x9c00e and self.debug:
                if current_address > 0x9c087:
                    logging.debug("self.commands is: {commands}".format(commands=commands))
                    for num in [0, 1]:
                        logging.debug(
                            "command {id} address={address} last_address={last}"
                            .format(
                                id=str(num),
                                address=hex(commands[num].address),
                                last=hex(commands[num].last_address),
                            )
                        )
                    raise Exception("going beyond the bounds for this text script")

            # no matching command found
            if scripting_command_class == None:
                raise Exception("unable to parse text command $%.2x in the text script at %s at %s" % (cur_byte, hex(start_address), hex(current_address)))

            # create an instance of the command class and let it parse its parameter bytes
            cls = scripting_command_class(address=current_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug, force=self.force)

            if self.debug:
                logging.debug(cls.to_asm())

            # store it in this script object
            commands.append(cls)

            # certain commands will end the scripting engine
            end = cls.end

            # skip past the command's parameter bytes to go to the next command
            #current_address += cls.size
            current_address = cls.last_address

        # last byte belonging to script is last byte of last command,
        # or the last byte of the last command's last parameter
        # (actually i think this might be the next byte after??)
        self.last_address = current_address

        if self.debug:
            logging.debug("cls.address is {0}".format(hex(cls.address)))
            logging.debug("cls.size is {0}".format(hex(cls.size)))
            logging.debug("cls.last_address is {0}".format(hex(cls.last_address)))
            logging.debug("self.last_address is {0}".format(hex(self.last_address)))

        if self.last_address != (cls.address + cls.size):
            raise exceptions.TextScriptException(
                "the last address should equal the last command's (address + size)"
            )

        if self.last_address != cls.last_address:
            raise exceptions.TextScriptException(
                "the last address of the TextScript should be the last_address of its last command"
            )

        # just some debugging..
        if self.debug:
            last_address = self.last_address
            logging.debug("TextScript last_address == {0}".format(hex(last_address)))
            #assert last_address != 0x5db06, "TextScript.parse somehow has a text with a last_address of 0x5db06 instead of 0x5db07"

        # store the script in the global table/map thing
        script_parse_table[start_address:current_address] = self
        all_texts.append(self)

        if self.debug:
            asm_output = "\n".join([command.to_asm() for command in commands])
            logging.debug("asm_output is:\n{0}".format(asm_output))

        # store the script
        self.commands = commands

        return commands

    def to_asm(self):
        if self.address in [0x26ef, 0x26f2, 0x6ee, 0x1071, 0x5ce33, 0x69523, 0x7ee98, 0x72176, 0x7a578, 0x19c09b, 0x19768c]:
            return None

        asm_output = "\n".join([command.to_asm() for command in self.commands])
        return asm_output

def parse_text_engine_script_at(address, map_group=None, map_id=None, debug=True, show=True, force=False):
    """parses a text-engine script ("in-text scripts")
    http://hax.iimarck.us/files/scriptingcodes_eng.htm#InText
    see parse_text_at2, parse_text_at, and process_00_subcommands
    """
    if is_script_already_parsed_at(address) and not force:
        return script_parse_table[address]
    return TextScript(address, map_group=map_group, map_id=map_id, debug=debug, show=show, force=force, script_parse_table=script_parse_table, text_command_classes=text_command_classes)

def find_text_addresses():
    """returns a list of text pointers
    useful for testing parse_text_engine_script_at"""
    return TextScript.find_addresses()

class EncodedText(object):
    """a sequence of bytes that, when decoded, represent readable text
    based on the chars table from preprocessor.py and other places"""
    base_label = "UnknownRawText_"

    def __init__(self, address, bank=None, map_group=None, map_id=None, debug=True, label=None):
        self.address = address
        if bank:
            self.bank = bank
        else:
            self.bank = pointers.calculate_bank(address)
        self.map_group, self.map_id, self.debug = map_group, map_id, debug
        if not label:
            label = self.base_label + hex(address)
        self.label = Label(name=label, address=address, object=self)
        self.dependencies = None
        self.parse()
        script_parse_table[self.address : self.last_address] = self

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

    def parse(self):
        offset = self.address

        # read until $57, $50 or $58
        jump57 = how_many_until(chr(0x57), offset, rom)
        jump50 = how_many_until(chr(0x50), offset, rom)
        jump58 = how_many_until(chr(0x58), offset, rom)

        # whichever command comes first
        jump = min([jump57, jump50, jump58])

        end_address = offset + jump # we want the address before $57

        text = parse_text_at2(offset, end_address-offset, debug=self.debug)

        if jump == jump50:
            text += "@"

        self.text = text

        self.last_address = self.end_address = end_address

    def to_asm(self):
        return "\""+self.text+"\""

    @staticmethod
    def process_00_subcommands(start_address, end_address, debug=True):
        """split this text up into multiple lines
        based on subcommands ending each line"""
        if debug:
            logging.debug(
                "process_00_subcommands({start}, {end})"
                .format(
                    start=hex(start_address),
                    end=hex(end_address),
                )
            )
        lines = {}
        subsection = rom[start_address:end_address]

        line_count = 0
        current_line = []
        for pbyte in subsection:
            byte = ord(pbyte)
            current_line.append(byte)
            if  byte == 0x4f or byte == 0x51 or byte == 0x55:
                lines[line_count] = current_line
                current_line = []
                line_count += 1

        # don't forget the last line
        lines[line_count] = current_line
        line_count += 1
        return lines

    @staticmethod
    def from_bytes(bytes, debug=True, japanese=False):
        """assembles a string based on bytes looked up in the chars table"""
        line = ""
        if japanese: charset = chars.jap_chars
        else: charset = chars.chars
        for byte in bytes:
            if type(byte) != int:
                byte = ord(byte)
            if byte in charset.keys():
                line += charset[byte]
            elif debug:
                logging.debug("byte not known: {0}".format(hex(byte)))
        return line

    @staticmethod
    def parse_text_at(address, count=10, debug=True, japanese=False):
        """returns a string of text from an address
        this does not handle text commands"""
        output = ""
        commands = process_00_subcommands(address, address+count, debug=debug)
        for (line_id, line) in commands.items():
            output += parse_text_from_bytes(line, debug=debug, japanese=japanese)
        texts.append([address, output])
        return output


def process_00_subcommands(start_address, end_address, debug=True):
    """split this text up into multiple lines
    based on subcommands ending each line"""
    return EncodedText.process_00_subcommands(start_address, end_address, debug=debug)

def parse_text_from_bytes(bytes, debug=True, japanese=False):
    """assembles a string based on bytes looked up in the chars table"""
    return EncodedText.from_bytes(bytes, debug=debug, japanese=japanese)

def parse_text_at(address, count=10, debug=True):
    """returns a list of bytes from an address
    see parse_text_at2 for pretty printing"""
    return parse_text_from_bytes(rom.interval(address, count, strings=False), debug=debug)

def parse_text_at2(address, count=10, debug=True, japanese=False):
    """returns a string of text from an address
    this does not handle text commands"""
    return EncodedText.parse_text_at(address, count, debug=debug, japanese=japanese)

def parse_text_at3(address, map_group=None, map_id=None, debug=False):
    deh = script_parse_table[address]
    if deh:
        return deh
    else:
        text = TextScript(address, map_group=map_group, map_id=map_id, debug=debug, script_parse_table=script_parse_table, text_command_classes=text_command_classes)
        if text.is_valid():
            return text
        else:
            return None

def rom_text_at(address, count=10):
    """prints out raw text from the ROM
    like for 0x112110"""
    return "".join([chr(x) for x in rom.interval(address, count, strings=False)])

def get_map_constant_label(map_group=None, map_id=None, map_internal_ids=None):
    """returns PALLET_TOWN for some map group/id pair"""
    if map_group == None:
        raise Exception("need map_group")
    if map_id == None:
        raise Exception("need map_id")

    for (id, each) in map_internal_ids.items():
        if each["map_group"] == map_group and each["map_id"] == map_id:
            return each["label"]
    return None

def get_map_constant_label_by_id(global_id, map_internal_ids):
    """returns a map constant label for a particular map id"""
    return map_internal_ids[global_id]["label"]

def get_id_for_map_constant_label(label):
    """returns some global id for a given map constant label
    PALLET_TOWN = 1, for instance."""
    global map_internal_ids
    for (id, each) in map_internal_ids.items():
        if each["label"] == label:
            return id
    return None

def generate_map_constant_labels():
    """generates the global for this script
    mapping ids to map groups/ids/labels"""
    global map_internal_ids
    map_internal_ids = {}
    i = 0
    for map_group in map_names.keys():
        for map_id in map_names[map_group].keys():
            if map_id == "offset": continue
            cmap = map_names[map_group][map_id]
            name = cmap["name"]
            name = name.replace("Pokémon Center", "PokeCenter").\
                        replace(" ", "_").\
                        replace("-", "_").\
                        replace("é", "e")
            constant_label = map_name_cleaner(name).upper()
            map_internal_ids[i] = {"label": constant_label,
                                   "map_id": map_id,
                                   "map_group": map_group}
            i += 1
    return map_internal_ids

# see generate_map_constant_labels() later
def generate_map_constants():
    """generates content for constants.asm
    this will generate two macros: GROUP and MAP"""
    global map_internal_ids
    if map_internal_ids == None or map_internal_ids == {}:
        generate_map_constant_labels()
    globals, groups, maps = "", "", ""
    for (id, each) in map_internal_ids.items():
        label = each["label"].replace("-", "_").replace("é", "e").upper()

        groups += "GROUP_"+ label + " EQU $%.2x" % (each["map_group"])
        groups += "\n"
        maps += "MAP_"+ label + " EQU $%.2x" % (each["map_id"])
        maps += "\n"
        globals +=  label + " EQU $%.2x" % (id)
        globals += "\n"
        #for multi-byte constants:
        #print each["label"] + " EQUS \"$%.2x,$%.2x\"" % (each["map_group"], each["map_id"])
    logging.debug("globals: {0}".format(globals))
    logging.debug("groups: {0}".format(groups))
    logging.debug("maps: {0}".format(maps))

def generate_map_constants_dimensions():
    """
    Generate _WIDTH and _HEIGHT properties.
    """
    global map_internal_ids
    output = ""
    if map_internal_ids == None or map_internal_ids == {}:
        generate_map_constant_labels()
    for (id, each) in map_internal_ids.items():
        map_group = each["map_group"]
        map_id    = each["map_id"]
        label = each["label"].replace("-", "_").replace("é", "e").upper()
        output += label + "_HEIGHT EQU %d\n" % (map_names[map_group][map_id]["header_new"].second_map_header.height.byte)
        output += label + "_WIDTH EQU %d\n" % (map_names[map_group][map_id]["header_new"].second_map_header.width.byte)
    return output

def transform_wildmons(asm, map_internal_ids):
    """
    Converts a wildmons section to use map constants.
    input: wildmons text.
    """
    asmlines = asm.split("\n")
    returnlines = []
    for line in asmlines:
        if "; " in line and not ("day" in line or "morn" in line or "nite" in line or "0x" in line or "encounter" in line) \
        and line != "" and line.split("; ")[0] != "":
            map_group = int(line.split("\tdb ")[1].split(",")[0].replace("$", "0x"), base=16)
            map_id    = int(line.split("\tdb ")[1].split(",")[1].replace("$", "0x").split("; ")[0], base=16)
            label     = get_map_constant_label(map_group=map_group, map_id=map_id, map_internal_ids=map_internal_ids)
            returnlines.append("\tdb GROUP_"+label+", MAP_"+label) #+" ; " + line.split(";")[1])
        else:
            returnlines.append(line)
    return "\n".join(returnlines)

def parse_script_asm_at(*args, **kwargs):
    # XXX TODO
    logging.debug("TODO: parse_script_asm_at")
    return None

def find_all_text_pointers_in_script_engine_script(script, bank=None, debug=False):
    """returns a list of text pointers
    based on each script-engine script command"""
    # TODO: recursively follow any jumps in the script
    if script == None:
        return []
    addresses = set()
    for (k, command) in enumerate(script.commands):
        if debug:
            logging.debug("command is: {0}".format(command))
        if   command.id == 0x4B:
            addresses.add(command.params[0].parsed_address)
        elif command.id == 0x4C:
            addresses.add(command.params[0].parsed_address)
        elif command.id == 0x51:
            addresses.add(command.params[0].parsed_address)
        elif command.id == 0x53:
            addresses.add(command.params[0].parsed_address)
        elif command.id == 0x64:
            addresses.add(command.params[0].parsed_address)
            addresses.add(command.params[1].parsed_address)
    return addresses

def translate_command_byte(crystal=None, gold=None):
    """takes a command byte from either crystal or gold
    returns the command byte in the other (non-given) game

    The new commands are values 0x52 and 0x9F. This means:
        Crystal's 0x00–0x51 correspond to Gold's 0x00–0x51
        Crystal's 0x53–0x9E correspond to Gold's 0x52–0x9D
        Crystal's 0xA0–0xA5 correspond to Gold's 0x9E–0xA3

    see: http://www.pokecommunity.com/showpost.php?p=4347261
    """
    if crystal != None: # convert to gold
        if crystal <= 0x51: return crystal
        if crystal == 0x52: return None
        if 0x53 <= crystal <= 0x9E: return crystal-1
        if crystal == 0x9F: return None
        if 0xA0 <= crystal <= 0xA5: return crystal-2
        if crystal > 0xA5:
            raise Exception("dunno yet if crystal has new insertions after crystal:0xA5 (gold:0xA3)")
    elif gold != None: # convert to crystal
        if gold <= 0x51: return gold
        if 0x52 <= gold <= 0x9D: return gold+1
        if 0x9E <= gold <= 0xA3: return gold+2
        if gold > 0xA3:
            raise Exception("dunno yet if crystal has new insertions after gold:0xA3 (crystal:0xA5)")
    else:
        raise Exception("translate_command_byte needs either a crystal or gold command")

class SingleByteParam():
    """or SingleByte(CommandParam)"""
    size = 1
    should_be_decimal = False
    byte_type = "db"

    def __init__(self, *args, **kwargs):
        for (key, value) in kwargs.items():
            setattr(self, key, value)
        # check address
        if not hasattr(self, "address"):
            raise Exception("an address is a requirement")
        elif self.address == None:
            raise Exception("address must not be None")
        elif not is_valid_address(self.address):
            raise Exception("address must be valid")
        # check size
        if not hasattr(self, "size") or self.size == None:
            raise Exception("size is probably 1?")
        # parse bytes from ROM
        self.parse()

    def parse(self): self.byte = ord(rom[self.address])

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

    def to_asm(self):
        if not self.should_be_decimal:
            return hex(self.byte).replace("0x", "$")
        else:
            return str(self.byte)

    @staticmethod
    def from_asm(value):
        return value

class DollarSignByte(SingleByteParam):
    def to_asm(self):
        return hex(self.byte).replace("0x", "$")
HexByte=DollarSignByte

class ItemLabelByte(DollarSignByte):
    def to_asm(self):
        label = item_constants.find_item_label_by_id(self.byte)
        if label:
            return label
        elif not label:
            return DollarSignByte.to_asm(self)


class DecimalParam(SingleByteParam):
    should_be_decimal = True


class MultiByteParam():
    """or MultiByte(CommandParam)"""
    size = 2
    should_be_decimal = False
    byte_type = "dw"

    def __init__(self, *args, **kwargs):
        self.prefix = "$" # default.. feel free to set 0x in kwargs
        for (key, value) in kwargs.items():
            setattr(self, key, value)
        # check address
        if not hasattr(self, "address") or self.address == None:
            raise Exception("an address is a requirement")
        elif not is_valid_address(self.address):
            raise Exception("address must be valid")
        # check size
        if not hasattr(self, "size") or self.size == None:
            raise Exception("don't know how many bytes to read (size)")
        self.parse()

    def parse(self):
        self.bytes = rom.interval(self.address, self.size, strings=False)
        self.parsed_number = self.bytes[0] + (self.bytes[1] << 8)
        if hasattr(self, "bank"):
            self.parsed_address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        else:
            self.parsed_address = calculate_pointer_from_bytes_at(self.address, bank=None)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

    # you won't actually use this to_asm because it's too generic
    #def to_asm(self): return ", ".join([(self.prefix+"%.2x")%x for x in self.bytes])
    def to_asm(self):
        if not self.should_be_decimal:
            return self.prefix+"".join([("%.2x")%x for x in reversed(self.bytes)])
        elif self.should_be_decimal:
            decimal = int("0x"+"".join([("%.2x")%x for x in reversed(self.bytes)]), 16)
            return str(decimal)

    @staticmethod
    def from_asm(value):
        return value


class PointerLabelParam(MultiByteParam):
    # default size is 2 bytes
    default_size = 2
    size = 2
    # default is to not parse out a bank
    bank = False
    force = False
    debug = False

    def __init__(self, *args, **kwargs):
        self.dependencies = None
        # bank can be overriden
        if "bank" in kwargs.keys():
            if kwargs["bank"] != False and kwargs["bank"] != None and kwargs["bank"] in [True, "reverse"]:
                self.given_bank = kwargs["bank"]
            #if kwargs["bank"] not in [None, False, True, "reverse"]:
            #    raise Exception("bank cannot be: " + str(kwargs["bank"]))
        if self.size > 3:
            raise Exception("param size is too large")
        # continue instantiation.. self.bank will be set down the road
        MultiByteParam.__init__(self, *args, **kwargs)

    def parse(self):
        self.parsed_address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        MultiByteParam.parse(self)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        dependencies = []
        if self.parsed_address == self.address:
            return dependencies
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        thing = script_parse_table[self.parsed_address]
        if thing and thing.address == self.parsed_address and not (thing is self):
            #if self.debug:
            #    print "parsed address is: " + hex(self.parsed_address) + " with label: " + thing.label.name + " of type: " + str(thing.__class__)
            dependencies.append(thing)
            if not thing in global_dependencies:
                global_dependencies.add(thing)
                more = thing.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
                dependencies.extend(more)
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        bank = self.bank
        # we pass bank= for whether or not to include a bank byte when reading
        #.. it's not related to caddress
        caddress = None
        if not (hasattr(self, "parsed_address") and self.parsed_address != None):
            caddress = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        else:
            caddress = self.parsed_address
        label = get_label_for(caddress)
        pointer_part = label # use the label, if it is found

        # check that the label actually points to the right place
        result = script_parse_table[caddress]
        if result != None and hasattr(result, "label"):
            if result.label.name != label:
                label = None
            elif result.address != caddress:
                label = None
        elif result != None:
            label = None

        # setup output bytes if the label was not found
        if not label:
            if bank == True:
                lo, hi = self.bytes[1:3]
            else:
                lo, hi = self.bytes[0:2]
            pointer_part = "{0}{1:02x}{2:02x}".format(self.prefix, hi, lo)

        # bank positioning matters!
        if bank == True or bank == "reverse": # bank, pointer
            # possibly use BANK(LABEL) if we know the bank
            if label:
                bank_part = "BANK({})".format(label)
            else:
                if "$" in pointer_part:
                    if 0x4000 <= caddress <= 0x7FFF:
                        #bank_part = "$%.2x" % (pointers.calculate_bank(self.parent.parent.address))
                        bank_part = "1"
                    else:
                        bank_part = "$%.2x" % (pointers.calculate_bank(caddress))
                else:
                    bank_part = ((self.prefix+"%.2x")%bank)
            # for labels, expand bank_part at build time
            if bank in ["reverse", True] and label:
                return label
            # return the asm based on the order the bytes were specified to be in
            elif bank == "reverse": # pointer, bank
                return pointer_part+", "+bank_part
            elif bank == True: # bank, pointer
                return bank_part+", "+pointer_part
            else:
                raise Exception("this should never happen")
            raise Exception("this should never happen")
        # this next one will either return the label or the raw bytes
        elif bank == False or bank == None: # pointer
            return pointer_part # this could be the same as label
        else:
            #raise Exception("this should never happen")
            return pointer_part # probably in the same bank ?
        raise Exception("this should never happen")

class PointerLabelBeforeBank(PointerLabelParam):
    size = 3
    bank = True # bank appears first, see calculate_pointer_from_bytes_at
    byte_type = 'db'

    @staticmethod
    def from_asm(value):
        return 'BANK({0})\n\tdw {0}'.format(value)

class PointerLabelAfterBank(PointerLabelParam):
    size = 3
    bank = "reverse" # bank appears last, see calculate_pointer_from_bytes_at
    byte_type = 'dw'

    @staticmethod
    def from_asm(value):
        return '{0}\n\tdb BANK({0})'.format(value)


class ScriptPointerLabelParam(PointerLabelParam): pass


class ScriptPointerLabelBeforeBank(PointerLabelBeforeBank): pass


class ScriptPointerLabelAfterBank(PointerLabelAfterBank): pass


def _parse_script_pointer_bytes(self, debug = False):
    PointerLabelParam.parse(self)
    if debug:
        logging.debug(
            "_parse_script_pointer_bytes - calculating the pointer located at {0}"
            .format(hex(self.address))
        )
    address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
    if address != None and address > 0x4000:
        if debug:
            logging.debug(
                "_parse_script_pointer_bytes - the pointer is: {0}"
                .format(hex(address))
            )
        self.script = parse_script_engine_script_at(address, debug=self.debug, force=self.force, map_group=self.map_group, map_id=self.map_id)
ScriptPointerLabelParam.parse = _parse_script_pointer_bytes
ScriptPointerLabelBeforeBank.parse = _parse_script_pointer_bytes
ScriptPointerLabelAfterBank.parse = _parse_script_pointer_bytes

class PointerLabelToScriptPointer(PointerLabelParam):
    def parse(self):
        PointerLabelParam.parse(self)
        address = calculate_pointer_from_bytes_at(self.parsed_address, bank=self.bank)
        address2 = calculate_pointer_from_bytes_at(address, bank=True)
        self.script = parse_script_engine_script_at(address2, origin=False, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug)


class AsmPointerParam(PointerLabelBeforeBank):
    def parse(self):
        PointerLabelBeforeBank.parse(self)
        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank) # 3-byte pointer
        self.asm = parse_script_asm_at(address, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug) # might end in some specific way?


class PointerToAsmPointerParam(PointerLabelParam):
    def parse(self):
        PointerLabelParam.parse(self)
        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank) # 2-byte pointer
        address2 = calculate_pointer_from_bytes_at(address, bank="reverse") # maybe not "reverse"?
        self.asm = parse_script_asm_at(address, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug) # might end in some specific way?


class RAMAddressParam(MultiByteParam):
    def to_asm(self):
        address = calculate_pointer_from_bytes_at(self.address, bank=False)
        label = get_ram_label(address)
        if label:
            return label
        else:
            return "$"+"".join(["%.2x"%x for x in reversed(self.bytes)])+""


class MoneyByteParam(MultiByteParam):
    size = 3
    byte_type = "db"
    max_value = 0x0F423F
    should_be_decimal = True
    def parse(self):
        MultiByteParam.parse(self)
        # in the rom as xxyyzz
        self.x = self.bytes[2]
        self.y = self.bytes[1]
        self.z = self.bytes[0]
    def to_asm(self):
        return str(self.x + (self.y << 8) + (self.z << 16))

    # this is used by the preprocessor
    @staticmethod
    def from_asm(value):
        # max is 0F423F
        # z = 0x0F ; y = 0x42 ; x = 0x3F
        # 999999 = x + (y << 8) + (z << 16)

        value = int(value)

        x = (value & 0x0000FF)
        y = (value & 0x00FF00) >> 8
        z = (value & 0xFF0000) >> 16

        return str(z) + "\ndb "+str(y)+"\ndb "+str(x)

def read_money(address, dohex=False):
    z = ord(rom[address])
    y = ord(rom[address+1])
    x = ord(rom[address+2])
    answer = x + (y << 8) + (z << 16)
    if not dohex:
        return answer
    else:
        return hex(answer)

def write_money(money):
    value = money
    x = (value & 0x0000FF)
    y = (value & 0x00FF00) >> 8
    z = (value & 0xFF0000) >> 16
    return "db "+str(z)+"\ndb "+str(y)+"\ndb "+str(x)

class CoinByteParam(MultiByteParam):
    size = 2
    max_value = 0x270F
    should_be_decimal = True


class MapGroupParam(SingleByteParam):
    def to_asm(self):
        map_id = ord(rom[self.address+1])
        map_constant_label = get_map_constant_label(map_id=map_id, map_group=self.byte, map_internal_ids=self.map_internal_ids) # like PALLET_TOWN
        if map_constant_label == None:
            return str(self.byte)
        #else: return "GROUP("+map_constant_label+")"
        else:
            return "GROUP_"+map_constant_label


class MapIdParam(SingleByteParam):
    def parse(self):
        SingleByteParam.parse(self)
        self.map_group = ord(rom[self.address-1])

    def to_asm(self):
        map_group = ord(rom[self.address-1])
        map_constant_label = get_map_constant_label(map_id=self.byte, map_group=map_group, map_internal_ids=self.map_internal_ids)
        if map_constant_label == None:
            return str(self.byte)
        #else: return "MAP("+map_constant_label+")"
        else:
            return "MAP_"+map_constant_label


class MapGroupIdParam(MultiByteParam):
    def parse(self):
        MultiByteParam.parse(self)
        self.map_group = self.bytes[0]
        self.map_id = self.bytes[1]

    def to_asm(self):
        map_group = self.map_group
        map_id = self.map_id
        label = get_map_constant_label(map_group=map_group, map_id=map_id, map_internal_ids=self.map_internal_ids)
        return label


class PokemonParam(SingleByteParam):
    def to_asm(self):
        pokemon_constant = get_pokemon_constant_by_id(self.byte)
        if pokemon_constant:
            return pokemon_constant
        else:
            return str(self.byte)

class PokemonWordParam(MultiByteParam):
    def to_asm(self):
        pokemon_constant = get_pokemon_constant_by_id(self.parsed_number)
        if pokemon_constant:
            return pokemon_constant
        else:
            return str(self.parsed_number)


class PointerParamToItemAndLetter(MultiByteParam):
    # [2F][2byte pointer to item no + 0x20 bytes letter text]
    pass


class TrainerIdParam(SingleByteParam):
    def to_asm(self):
        # find the group id by first finding the param type id
        i = 0
        foundit = None
        for (k, v) in self.parent.param_types.items():
            if v["class"] == TrainerGroupParam:
                foundit = i
                break
            i += 1

        if foundit == None:
            raise Exception("didn't find a TrainerGroupParam in this command??")

        # now get the trainer group id
        trainer_group_id = self.parent.params[foundit].byte

        # check the rule to see whether to use an id or not
        if ("uses_numeric_trainer_ids" in trainers.trainer_group_names[trainer_group_id].keys()) or \
           (not "trainer_names" in trainers.trainer_group_names[trainer_group_id].keys()):
            return str(self.byte)
        else:
            return trainers.trainer_group_names[trainer_group_id]["trainer_names"][self.byte-1]

class TrainerGroupParam(SingleByteParam):
    def to_asm(self):
        trainer_group_id = self.byte
        return trainers.trainer_group_names[trainer_group_id]["constant"]

class MoveParam(SingleByteParam):
    def to_asm(self):
        if self.byte in move_constants.moves.keys():
            return move_constants.moves[self.byte]
        else:
            # this happens for move=0 (no move) in trainer headers
            return str(self.byte)

class MenuDataPointerParam(PointerLabelParam):
    """read menu data at the target site"""


string_to_text_texts = []
class RawTextPointerLabelParam(PointerLabelParam):
    # not sure if these are always to a text script or raw text?
    def parse(self):
        PointerLabelParam.parse(self)
        #bank = pointers.calculate_bank(self.address)
        address = calculate_pointer_from_bytes_at(self.address, bank=False)
        self.calculated_address = address
        #self.text = parse_text_at3(address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        #self.text = TextScript(address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.text = parse_text_engine_script_at(address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        global_dependencies.add(self.text)
        return [self.text]

class EncodedTextLabelParam(PointerLabelParam):
    def parse(self):
        PointerLabelParam.parse(self)

        address = calculate_pointer_from_bytes_at(self.address, bank=False)
        self.parsed_address = address
        self.text = EncodedText(address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)

        if isinstance(self.text, EncodedText):
            string_to_text_texts.append(self.text)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        global_dependencies.add(self.text)
        return [self.text]

class TextPointerLabelParam(PointerLabelParam):
    """this is a pointer to a text script"""
    bank = False
    text = None
    def parse(self):
        PointerLabelParam.parse(self)
        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        if address != None and address != 0:
            self.text = parse_text_engine_script_at(address, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug)
            if not self.text:
                self.text = script_parse_table[address]

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.text:
            global_dependencies.add(self.text)
            return [self.text]
        else:
            return []

class TextPointerLabelAfterBankParam(PointerLabelAfterBank):
    text = None
    def parse(self):
        PointerLabelAfterBank.parse(self)
        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        if address != None and address != 0:
            self.text = parse_text_engine_script_at(address, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug)
            if not self.text:
                self.text = script_parse_table[address]

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.text:
            global_dependencies.add(self.text)
            return [self.text]
        else:
            return []

class MovementPointerLabelParam(PointerLabelParam):
    def parse(self):
        PointerLabelParam.parse(self)
        if is_script_already_parsed_at(self.parsed_address):
            self.movement = script_parse_table[self.parsed_address]
        else:
            self.movement = ApplyMovementData(self.parsed_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if hasattr(self, "movement") and self.movement:
            global_dependencies.add(self.movement)
            return [self.movement] + self.movement.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
        else:
            raise Exception("MovementPointerLabelParam hasn't been parsed yet")

class MapDataPointerParam(PointerLabelParam):
    pass

class Command(object):
    """
    Note: when dumping to asm, anything in script_parse_table that directly
    inherits Command should not be .to_asm()'d.
    """
    # use this when the "byte id" doesn't matter
    # .. for example, a non-script command doesn't use the "byte id"
    override_byte_check = False
    is_rgbasm_macro = False
    base_label = "UnseenLabel_"

    def __init__(self, address=None, *pargs, **kwargs):
        """params:
        address     - where the command starts
        force       - whether or not to force the script to be parsed (default False)
        debug       - are we in debug mode? default False
        map_group
        map_id
        """
        defaults = {"force": False, "debug": False, "map_group": None, "map_id": None}
        if not is_valid_address(address):
            raise Exception("address is invalid")
        # set up some variables
        self.address = address
        self.last_address = None
        # setup the label based on base_label if available
        label = self.base_label + hex(self.address)
        self.label = Label(name=label, address=address, object=self)
        # params are where this command's byte parameters are stored
        self.params = {}
        self.dependencies = None
        # override default settings
        defaults.update(kwargs)
        # set everything
        for (key, value) in defaults.items():
            setattr(self, key, value)
        # but also store these kwargs
        self.args = defaults
        # start parsing this command's parameter bytes
        self.parse()

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        dependencies = []
        #if self.dependencies != None and not recompute:
        #    global_dependencies.update(self.dependencies)
        #    return self.dependencies
        for (key, param) in self.params.items():
            if hasattr(param, "get_dependencies") and param != self:
                deps = param.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
                if deps != None and not self in deps:
                    dependencies.extend(deps)
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        # start with the rgbasm macro name for this command
        output = ""
        #if len(self.macro_name) > 0 and self.macro_name[0].isdigit():
        #    output += "_"
        output += self.macro_name
        # return if there are no params
        if len(self.param_types.keys()) == 0:
            return output
        # first one will have no prefixing comma
        first = True
        # start reading the bytes after the command byte
        if not self.override_byte_check:
            current_address = self.address+1
        else:
            current_address = self.address
        #output = self.macro_name + ", ".join([param.to_asm() for (key, param) in self.params.items()])
        # add each param
        for (key, param) in self.params.items():
            name = param.name
            # the first param shouldn't have ", " prefixed
            if first:
                output += " "
                first = False
            # but all other params should
            else: output += ", "
            # now add the asm-compatible param string
            output += param.to_asm()
            current_address += param.size
        #for param_type in self.param_types:
        #    name = param_type["name"]
        #    klass = param_type["klass"]
        #    # create an instance of this type
        #    # tell it to begin parsing at this latest byte
        #    obj = klass(address=current_address)
        #    # the first param shouldn't have ", " prefixed
        #    if first: first = False
        #    # but all other params should
        #    else: output += ", "
        #    # now add the asm-compatible param string
        #    output += obj.to_asm()
        #    current_address += obj.size
        return output

    def parse(self):
        # id, size (inclusive), param_types
        #param_type = {"name": each[1], "class": each[0]}
        if not self.override_byte_check:
            current_address = self.address+1
        else:
            current_address = self.address
        byte = ord(rom[self.address])
        if not self.override_byte_check and (not byte == self.id):
            raise Exception("byte ("+hex(byte)+") != self.id ("+hex(self.id)+")")
        i = 0
        for (key, param_type) in self.param_types.items():
            name = param_type["name"]
            klass = param_type["class"]
            # make an instance of this class, like SingleByteParam()
            # or ItemLabelByte.. by making an instance, obj.parse() is called
            obj = klass(address=current_address, name=name, parent=self, **dict([(k,v) for (k, v) in self.args.items() if k not in ["parent"]]))
            # save this for later
            self.params[i] = obj
            # increment our counters
            current_address += obj.size
            i += 1
        self.last_address = current_address
        return True


class GivePoke(Command):
    id = 0x2D
    macro_name = "givepoke"
    size = 4 # minimum
    end = False
    param_types = {
                  0: {"name": "pokemon", "class": PokemonParam},
                  1: {"name": "level", "class": DecimalParam},
                  2: {"name": "item", "class": ItemLabelByte},
                  3: {"name": "trainer", "class": DecimalParam},
                  4: {"name": "trainer_name_pointer", "class": MultiByteParam}, # should probably use TextLabelParam
                  5: {"name": "pkmn_nickname", "class": MultiByteParam}, # XXX TextLabelParam ?
                  }
    allowed_lengths = [4, 6]

    def parse(self):
        self.params = {}
        byte = ord(rom[self.address])
        if not byte == self.id:
            raise Exception("this should never happen")
        current_address = self.address+1
        i = 0
        self.size = 1
        for (key, param_type) in self.param_types.items():
            # stop executing after the 4th byte unless it == 0x1
            if i == 4:
                logging.debug("self.params[3].byte is: {0}".format(str(self.params[3].byte)))
            if i == 4 and self.params[3].byte != 1: break
            name = param_type["name"]
            klass = param_type["class"]
            # make an instance of this class, like SingleByteParam()
            # or ItemLabelByte.. by making an instance, obj.parse() is called
            obj = klass(address=current_address, name=name)
            # save this for later
            self.params[i] = obj
            # increment our counters
            current_address += obj.size
            self.size += obj.size
            i += 1
        self.last_address = current_address
        return True

class DataByteWordMacro(Command):
    """
    Only used by the preprocessor.
    """

    id = None
    macro_name = "dbw"
    size = 3
    override_byte_check = True

    param_types = {
        0: {"name": "db value", "class": DecimalParam},
        1: {"name": "dw value", "class": PointerLabelParam},
    }

    def __init__(self): pass
    def parse(self): pass
    def to_asm(self): pass


event_flags = None
def read_event_flags():
    global event_flags
    constants = wram.read_constants(os.path.join(conf.path, 'constants.asm'))
    event_flags = dict(filter(lambda key_value: key_value[1].startswith('EVENT_'), constants.items()))

engine_flags = None
def read_engine_flags():
    global engine_flags
    constants = wram.read_constants(os.path.join(conf.path, 'constants.asm'))
    engine_flags = dict(filter(lambda key_value1: key_value1[1].startswith('ENGINE_'), constants.items()))

class EventFlagParam(MultiByteParam):
    def to_asm(self):
        if event_flags is None: read_event_flags()
        return event_flags.get(self.parsed_number) or MultiByteParam.to_asm(self)

class EngineFlagParam(MultiByteParam):
    def to_asm(self):
        if engine_flags is None: read_engine_flags()
        return engine_flags.get(self.parsed_number) or MultiByteParam.to_asm(self)


class MovementCommand(Command):
    # the vast majority of movement commands do not end the movement script
    end = False

    # this is only used for e.g. macros that don't appear as a byte in the ROM
    # don't use the override because all movements are specified with a byte
    override_byte_check = False

    # most commands have size=1 but one or two have a single parameter (gasp)
    size = 1

    param_types = {}
    params = []

    # most movement commands won't have any dependencies
    # get_dependencies on Command will look at the values of params
    # so this doesn't need to be specified by MovementCommand as long as it extends Command
    #def get_dependencies(self, recompute=False, global_dependencies=set()):
    #    return []

    def parse(self):
        if ord(rom[self.address]) < 0x45:
            # this is mostly handled in to_asm
            pass
        else:
            Command.parse(self)

    def to_asm(self):
        # return "db $%.2x"%(self.byte)
        return Command.to_asm(self)

class MovementDBCommand(Command):
    end = False
    macro_name = "db"
    override_byte_check = True
    id = None
    byte = None
    size = 1
    param_types = {
        0: {"name": "db value", "class": SingleByteParam},
    }
    params = []

    def to_asm(self):
        asm = Command.to_asm(self)
        return asm + " ; movement"

# down, up, left, right
movement_command_bases = {
    0x00: "turn_head",
    0x04: "half_step",
    0x08: "slow_step", # small_step?
    0x0C: "step",
    0x10: "big_step", # fast_step?
    0x14: "slow_slide_step",
    0x18: "slide_step",
    0x1C: "fast_slide_step",
    0x20: "turn_away",
    0x24: "turn_in", # towards?
    0x28: "turn_waterfall", # what??
    0x2C: "slow_jump_step",
    0x30: "jump_step",
    0x34: "fast_jump_step",

    # tauwasser says the pattern stops at $45 but $38 looks more realistic?
    0x3A: "remove_fixed_facing",
    0x3B: "fix_facing",
    0x3D: "hide_person",
    0x3E: "show_person",
    0x45: "accelerate_last",
    0x46: ["step_sleep", ["duration", DecimalParam]],
    0x47: "step_end",
    0x49: "remove_person",

    # do these next two have any params ??
    0x4C: "teleport_from",
    0x4D: "teleport_to",

    0x4E: "skyfall",
    0x4F: "step_wait5",
    0x53: "hide_emote",
    0x54: "show_emote",
    0x55: ["step_shake", ["displacement", DecimalParam]],
}

# create MovementCommands from movement_command_bases
def create_movement_commands(debug=False):
    """
    Creates MovementCommands from movement_command_bases. This is just a cheap
    trick instead of manually defining all of those classes.
    """
    #movement_command_classes = inspect.getmembers(sys.modules[__name__], \
    #                       lambda obj: inspect.isclass(obj) and \
    #                       issubclass(obj, MovementCommand) and \
    #                       not (obj is MovementCommand))
    movement_command_classes2 = []
    for (byte, cmd) in movement_command_bases.items():
        if type(cmd) == str:
            cmd = [cmd]
        cmd_name = cmd[0].replace(" ", "_")
        params = {"id": byte, "size": 1, "end": byte is 0x47, "macro_name": cmd_name}
        params["param_types"] = {}
        if len(cmd) > 1:
            param_types = cmd[1:]
            for (i, each) in enumerate(param_types):
                thing = {"name": each[0], "class": each[1]}
                params["param_types"][i] = thing
                if debug:
                    logging.debug(
                        "each is {0} and thing[class] is {1}"
                        .format(each, str(thing["class"]))
                    )
                params["size"] += thing["class"].size

        if byte <= 0x34:
            for x in range(0, 4):

                direction = None
                if x == 0:
                    direction = "down"
                elif x == 1:
                    direction = "up"
                elif x == 2:
                    direction = "left"
                elif x == 3:
                    direction = "right"
                else:
                    raise Exception("this should never happen")

                cmd_name = cmd[0].replace(" ", "_") + "_" + direction
                klass_name = cmd_name+"Command"
                params["id"] = copy(byte)
                params["macro_name"] = cmd_name
                klass = type(copy(klass_name), (MovementCommand,), deepcopy(params))
                globals()[klass_name] = klass
                movement_command_classes2.append(klass)

                byte += 1
            del cmd_name
            del params
            del klass_name
        else:
            klass_name = cmd_name+"Command"
            klass = type(klass_name, (MovementCommand,), params)
            globals()[klass_name] = klass
            movement_command_classes2.append(klass)
    # later an individual klass will be instantiated to handle something
    return movement_command_classes2

movement_command_classes = create_movement_commands()

all_movements = []
class ApplyMovementData(object):
    base_label = "MovementData_"

    def __init__(self, address, map_group=None, map_id=None, debug=False, label=None, force=False):
        self.address   = address
        self.map_group = map_group
        self.map_id    = map_id
        self.debug     = debug
        self.force     = force

        if not label:
            label = self.base_label + hex(address)
        self.label     = Label(name=label, address=address, object=self)

        self.dependencies = []
        self.commands     = []

        self.parse()

    # this is almost an exact copy of Script.parse
    # with the exception of using text_command_classes instead of command_classes
    def parse(self):
        global movement_command_classes, script_parse_table
        address = self.address

        # i feel like checking myself
        if not is_valid_address(address):
            raise exceptions.AddressException(
                "ApplyMovementData.parse must be given a valid address (but got {0})."
                .format(hex(address))
            )

        current_address = copy(self.address)
        start_address = copy(current_address)

        # don't clutter up my screen
        if self.debug:
            logging.debug(
                "ApplyMovementData.parse address={address} map_group={map_group} map_id={map_id}"
                .format(
                    address=hex(self.address),
                    map_group=str(self.map_group),
                    map_id=str(self.map_id),
                )
            )

        # load up the rom if it hasn't been loaded already
        rom = load_rom()

        # in the event that the script parsing fails.. it would be nice to leave evidence
        script_parse_table[start_address:start_address+1] = "incomplete ApplyMovementData.parse"

        # start with a blank script
        commands = []

        # use this to control the while loop
        end = False

        # for each command found...
        while not end:
            # get the current scripting byte
            cur_byte = ord(rom[current_address])

            # reset the command class (last command was probably different)
            scripting_command_class = None

            # match the command id byte to a scripting command class like "step half"
            for class_ in movement_command_classes:
                # allow lists of ids
                if (type(class_.id) == list and cur_byte in class_.id) \
                   or class_.id == cur_byte:
                    scripting_command_class = class_

            # temporary fix for applymovement scripts
            if ord(rom[current_address]) == 0x47:
                end = True

            # no matching command found
            xyz = None
            if scripting_command_class == None:
                scripting_command_class = MovementDBCommand
                #scripting_command_class = deepcopy(MovementCommand)
                #scripting_command_class.id = scripting_command_class.byte = ord(rom[current_address])
                #scripting_command_class.macro_name = "db"
                #scripting_command_class.size = 1
                #scripting_command_class.override_byte_check = True
                #scripting_command_class.id = None
                #scripting_command_class.param_types = {0: {"name": "db value", "class": DecimalParam}}

                xyz = True

            # create an instance of the command class and let it parse its parameter bytes
            cls = scripting_command_class(address=current_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug, force=self.force)

            if self.debug:
                logging.debug("cls.to_asm() is: {0}".format(cls.to_asm()))

            # store it in this script object
            commands.append(cls)

            # certain commands will end the movement engine
            end = cls.end

            # skip past the command's parameter bytes to go to the next command
            current_address += cls.size

        # last byte belonging to script is last byte of last command,
        # or the last byte of the last command's last parameter
        # (actually i think this might be the next byte after??)
        self.last_address = current_address

        # store the script in the global table/map thing
        all_movements.append(self)
        script_parse_table[start_address:current_address] = self

        if self.debug:
            asm_output = "\n".join([command.to_asm() for command in commands])
            logging.debug("asm_output: {0}".format(asm_output))

        # store the script
        self.commands = commands
        return commands

    def to_asm(self):
        asm_output = "\n".join([command.to_asm() for command in self.commands])
        return asm_output

    # TODO: get_dependencies doesn't work if ApplyMovementData uses labels in the future
    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

def print_all_movements():
    for each in all_movements:
        print(each.to_asm())
        print("------------------")
    print("done")

class TextCommand(Command):
    # an individual text command will not end it
    end = False

    # this is only used for e.g. macros that don't appear as a byte in the ROM
    # don't use the override because all text commands are specified with a byte
    override_byte_check = False

    # in the case of text/asm commands, size is unknown until after parsing
    # some text commands can specify this upfront but not $0
    size = None

    param_types = {}
    params = []

    # most text commands won't have any dependencies
    # .. except for that one that points to another location for text
    # get_dependencies on Command will look at the values of params
    # so this doesn't need to be specified by TextCommand as long as it extends Command
    #def get_dependencies(self, recompute=False, global_dependencies=set()):
    #    return []


# this is a regular command in a TextScript for writing text
# but unlike other macros that preprocessor.py handles,
# the preprocessor-parser is custom and MainText is not
# used as a macro by main.asm - however, MainText is
# treated as a macro for the sake of parsing the ROM because
# it is called with $0. This is very similar to how Script
# is parsed and handled. But again, script command macros
# are quite different.. preprocessor.py allows some of them
# to handle how they should be parsed from main.asm, in
# addition to their regular "parse()" method.
class MainText(TextCommand):
    "Write text. Structure: [00][Text][0x50 (ends code)]"
    id = 0x0
    macro_name = "do_text"
    use_zero = True

    def parse(self):
        offset = self.address

        # the code below assumes we're jumping past a $0 byte
        if self.use_zero == False:
            offset = offset
        else:
            offset = offset + 1

        # read until $50, $57 or $58 (not sure about $58...)
        jump57 = how_many_until(chr(0x57), offset, rom)
        jump50 = how_many_until(chr(0x50), offset, rom)
        jump58 = how_many_until(chr(0x58), offset, rom)

        # pick whichever one comes first
        jump = min([jump57, jump50, jump58])

        # if $57 appears first then this command is the last in this text script
        if jump == jump57 or jump == jump58:
            self.end = True

        jump += 1

        # we want the address after the $57
        # ("last_address" is misnamed everywhere)
        end_address = offset + jump
        self.last_address = self.end_address = end_address

        # read the text bytes into a structure
        # skip the first offset byte because that's the command byte
        self.bytes = rom.interval(offset, jump, strings=False)

        # include the original command in the size calculation
        self.size = jump

        if self.use_zero:
            self.last_address = self.address + jump + 1
            self.size = self.last_address - self.address

            if self.address == 0x9c00e and self.debug:
                if self.last_address != 0x9c086:
                    argparams = {
                        "self.address": hex(self.address),
                        "jump": str(jump),
                        "bytes": str(self.bytes),
                        "self.size": str(self.size),
                        "self.last_address": hex(self.last_address),
                    }

                    logging.debug(str(argparams))

                    raise Exception("last_address is wrong for 0x9c00e")

    def to_asm(self):
        if self.size < 2 or len(self.bytes) < 1:
            raise Exception("$0 text command can't end itself with no follow-on bytes")

        if self.use_zero:
            output = "db $0"
        else:
            output = ""

        # db $0, $57 or db $0, $50 or w/e
        if self.size == 2 and len(self.bytes) == 1:
            output += ", $%.2x" % (self.bytes[0])
            return output

        # whether or not quotes are open
        in_quotes = False

        # whether or not to print "db " next
        new_line = False

        # whether or not there was a ", " last..
        # this is useful outside of quotes
        was_comma = False

        # has a $50 or $57 been passed yet?
        end = False

        if not self.use_zero:
            new_line = True
            was_comma = False

        for byte in self.bytes:
            if end:
                raise Exception("the text ended due to a $50 or $57 but there are more bytes?")

            if new_line:
                if in_quotes:
                    raise Exception("can't be in_quotes on a newline")
                elif was_comma:
                    raise Exception("last line's last character can't be a comma")

                output += "db "

            # $4f, $51 and $55 can end a line
            if byte in [0x4f, 0x51, 0x55]:
                if new_line:
                    raise exceptions.TextScriptException("can't have $4f, $51, $55 as the first character on a newline")

                if in_quotes:
                    output += "\", $%.2x\n" % (byte)
                elif not in_quotes:
                    if not was_comma:
                        output += ", "
                    output += "$%.2x\n" % (byte)

                # reset everything
                in_quotes = False
                new_line  = True
                was_comma = False
            elif byte == 0x50:
                # technically you could have this i guess... db "@"
                # but in most situations it will be added to the end of the previous line
                #assert not new_line, "can't have $50 or '@' as the first character on a newline in the text at "+hex(self.address)

                if in_quotes:
                    output += "@\"\n"
                    new_line = True
                elif not in_quotes:
                    if not was_comma and not new_line:
                        output += ", "
                    output += "\"@\"\n"

                # reset everything
                in_quotes = False
                new_line  = True
                was_comma = False
                end       = True

                # self.end should be set in parse or constructor
                # so this is very useless here.. but it's a truism i guess
                self.end  = True
            elif byte == 0x57 or byte == 0x58:
                # close any quotes
                if in_quotes:
                    output += "\""
                    was_comma = False

                if not was_comma and not new_line:
                    output += ", "

                output += "$%.2x\n" % (byte)

                in_quotes = False
                new_line  = True
                was_comma = False
                end       = True

                # dunno if $58 should end a text script or not
                # also! self.end should be set in parse not in to_asm
                # so this is pretty useless overall...
                if byte == 0x58:
                    self.end = True
            elif byte in chars.chars.keys():
                # figure out what the character actually is
                char = chars.chars[byte]

                # oh wait.. quotes isn't a valid character in the first place :(
                if char == "\"":
                    if in_quotes:
                        output += "\""
                        in_quotes = False
                    elif not in_quotes:
                        if new_line:
                            output += "\""
                        elif not new_line:
                            if not was_comma:
                                output += ", "
                            output += "\""
                        in_quotes = True

                # the above if statement is probably never called
                else:
                    if not in_quotes:
                        if not new_line and not was_comma:
                            output += ", "
                        output += "\""
                        in_quotes = True

                    output += char

                new_line  = False
                was_comma = False
                end       = False
            else:
                # raise Exception("unknown byte in text script ($%.2x)" % (byte))
                # just add an unknown byte directly to the text.. what's the worse that can happen?

                if in_quotes:
                    output += "\", $%.2x" % (byte)

                    in_quotes = False
                    was_comma = False
                    new_line = False
                elif not in_quotes:
                    if not was_comma and not new_line:
                        output += ", "
                    output += "$%.2x" % (byte)

                # reset things
                in_quotes = False
                new_line  = False
                was_comma = False

        # this shouldn't happen because of the rom_until calls in the parse method
        if not end:
            raise Exception("ran out of bytes without the script ending? starts at "+hex(self.address))

        # last character may or may not be allowed to be a newline?
        # Script.to_asm() has command.to_asm()+"\n"
        if output[-1] == "\n":
            output = output[:-1]

        return output

class PokedexText(MainText):
    use_zero = False

class WriteTextFromRAM(TextCommand):
    """
    Write text from ram. Structure: [01][Ram address (2byte)]
    For valid ram addresses see Glossary. This enables use of variable text strings.
    """
    id = 0x1
    macro_name = "text_from_ram"
    size = 3
    param_types = {
        0: {"name": "pointer", "class": MultiByteParam},
    }
class WriteNumberFromRAM(TextCommand):
    """
    02 = Write number from ram. Structure: [02][Ram address (2byte)][Byte]

    Byte:

    Bit5:Bit6:Bit7
       1:   1:   1 = PokéDollar| Don’t write zeros
       0:   1:   1 = Don’t write zeros
       0:   0:   1 = Spaces instead of zeros
       0:   0:   0 = Write zeros
       0:   1:   0 = Write zeros
       1:   0:   0 = PokéDollar
       1:   1:   0 = PokéDollar
       1:   0:   1 = Spaces instead of zeros| PokéDollar

    Number of figures = Byte AND 0x1F *2
    No Hex --> Dec Conversio
    """
    id = 0x2
    macro_name = "number_from_ram"
    size = 4
    param_types = {
        0: {"name": "pointer", "class": PointerLabelParam},
        1: {"name": "config", "class": HexByte},
    }
class SetWriteRAMLocation(TextCommand):
    "Define new ram address to write to. Structure: [03][Ram address (2byte)]"
    id = 0x3
    macro_name = "store_at"
    size = 3
    param_types = {
        0: {"name": "ram address", "class": PointerLabelParam},
    }
class ShowBoxWithValueAt(TextCommand):
    "04 = Write a box. Structure: [04][Ram address (2byte)][Y][X]"
    id = 0x4
    macro_name = "text_box"
    size = 5
    param_types = {
        0: {"name": "ram address", "class": PointerLabelParam},
        1: {"name": "y", "class": DecimalParam},
        2: {"name": "x", "class": DecimalParam},
    }
class Populate2ndLineOfTextBoxWithRAMContents(TextCommand):
    "05 = New ram address to write to becomes 2nd line of a text box. Structure: [05]"
    id = 0x5
    macro_name = "text_dunno1"
    size = 1
class ShowArrowsAndButtonWait(TextCommand):
    "06 = Wait for key down + show arrows. Structure: [06]"
    id = 0x6
    macro_name = "text_waitbutton"
    size = 1
class Populate2ndLine(TextCommand):
    """
    07 = New ram address to write to becomes 2nd line of a text box
    Textbox + show arrows. Structure: [07]
    """
    id = 0x7
    macro_name = "text_dunno2"
    size = 1
class TextInlineAsm(TextCommand):
    "08 = After the code an ASM script starts. Structure: [08][Script]"
    id = 0x8
    macro_name = "start_asm"
    end = True
    size = 1
    # TODO: parse the following asm with gbz80disasm
class WriteDecimalNumberFromRAM(TextCommand):
    """
    09 = Write number from rom/ram in decimal. Structure: [09][Ram address/Pointer (2byte)][Byte]
      Byte:

      Is split: 1. 4 bits = Number of bytes to load. 0 = 3, 1 = 1, 2 = 2
                2. 4 bits = Number of figures of displayed number
                                                     0 = Don’t care
                                                     1 = Don’t care
                                                     >=2 = Number
    """
    id = 0x9
    macro_name = "deciram"
    size = 4
    param_types = {
        0: {"name": "pointer?", "class": PointerLabelParam},
        1: {"name": "config", "class": HexByte},
    }
class InterpretDataStream(TextCommand):
    """
    0A = Interpret Data stream. Structure: [0A]
    see: http://hax.iimarck.us/files/scriptingcodes_eng.htm#Marke88
    """
    id = 0xA
    macro_name = "interpret_data"
    size = 1
class Play0thSound(TextCommand):
    "0B = Play sound 0x0000. Structure: [0B]"
    id = 0xB
    sound_num = 0
    macro_name = "sound0"
    size = 1
class LimitedIntrepretDataStream(TextCommand):
    """
    0C = Interpret Data stream. Structure: [0C][Number of codes to interpret]
    For every interpretation there is a“…“ written
    """
    id = 0xC
    macro_name = "limited_interpret_data"
    size = 2
    param_types = {
        0: {"name": "number of codes to interpret", "class": DecimalParam},
    }
class WaitForKeyDownDisplayArrow(ShowArrowsAndButtonWait):
    """
    0D = Wait for key down  display arrow. Structure: [0D]
    """
    id = 0xD
    macro_name = "waitbutton2"
    size = 1
class Play9thSound(Play0thSound):
    id = 0xE
    sound_num = 9
    macro_name = "sound0x09"
    size = 1
class Play1stSound(Play0thSound):
    id = 0xF
    sound_num = 1
    macro_name = "sound0x0F"
    size = 1
class Play2ndSound(Play0thSound):
    id = 0x10
    sound_num = 2
    macro_name = "sound0x02"
    size = 1
class Play10thSound(Play0thSound):
    id = 0x11
    sound_num = 10
    macro_name = "sound0x0A"
    size = 1
class Play45thSound(Play0thSound):
    id = 0x12
    sound_num = 0x2D
    macro_name = "sound0x2D"
    size = 1
class Play44thSound(Play0thSound):
    id = 0x13
    sound_num = 0x2C
    macro_name = "sound0x2C"
    size = 1
class DisplayByteFromRAMAt(TextCommand):
    """
    14 = Display MEMORY. Structure: [14][Byte]

    Byte:

      00 = MEMORY1
      01 = MEMORY2
      02 = MEMORY
      04 = TEMPMEMORY2
      05 = TEMPMEMORY1
    """
    id = 0x14
    macro_name = "show_byte_at"
    size = 2
    param_types = {
        1: {"name": "memory byte id", "class": DecimalParam},
    }
class WriteCurrentDay(TextCommand):
    "15 = Write current day. Structure: [15]"
    id = 0x15
    macro_name = "current_day"
    size = 1
class TextJump(TextCommand):
    "16 = 3byte pointer to new text follows. Structure: [16][2byte pointer][bank]"
    id = 0x16
    macro_name = "text_jump"
    size = 4
    param_types = {
        0: {"name": "text", "class": TextPointerLabelAfterBankParam},
    }
# this is needed because sometimes a script ends with $50 $50
class TextEndingCommand(TextCommand):
    id = 0x50
    macro_name = "db"
    override_byte_check = False
    size = 1
    end = True
    def to_asm(self):
        return "db $50"

text_command_classes = inspect.getmembers(sys.modules[__name__], \
                       lambda obj: inspect.isclass(obj) and \
                       issubclass(obj, TextCommand) and \
                       obj != TextCommand and obj != PokedexText)

# byte: [name, [param1 name, param1 type], [param2 name, param2 type], ...]
# 0x9E: ["verbosegiveitem", ["item", ItemLabelByte], ["quantity", SingleByteParam]],
pksv_crystal_more = {
    0x00: ["2call", ["pointer", ScriptPointerLabelParam]],
    0x01: ["3call", ["pointer", ScriptPointerLabelBeforeBank]],
    0x02: ["2ptcall", ["pointer", RAMAddressParam]],
    0x03: ["2jump", ["pointer", ScriptPointerLabelParam]],
    0x04: ["3jump", ["pointer", ScriptPointerLabelBeforeBank]],
    0x05: ["2ptjump", ["pointer", RAMAddressParam]],
    0x06: ["if equal", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
    0x07: ["if not equal", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
    0x08: ["iffalse", ["pointer", ScriptPointerLabelParam]],
    0x09: ["iftrue", ["pointer", ScriptPointerLabelParam]],
    0x0A: ["if greater than", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
    0x0B: ["if less than", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
    0x0C: ["jumpstd", ["predefined_script", MultiByteParam]],
    0x0D: ["callstd", ["predefined_script", MultiByteParam]],
    0x0E: ["3callasm", ["asm", AsmPointerParam]],
    0x0F: ["special", ["predefined_script", MultiByteParam]],
    0x10: ["2ptcallasm", ["asm", RAMAddressParam]],
    # should map_group/map_id be dealt with in some special way in the asm?
    0x11: ["checkmaptriggers", ["map_group", SingleByteParam], ["map_id", SingleByteParam]],
    0x12: ["domaptrigger", ["map_group", MapGroupParam], ["map_id", MapIdParam], ["trigger_id", SingleByteParam]],
    0x13: ["checktriggers"],
    0x14: ["dotrigger", ["trigger_id", SingleByteParam]],
    0x15: ["writebyte", ["value", SingleByteParam]],
    0x16: ["addvar", ["value", SingleByteParam]],
    0x17: ["random", ["input", SingleByteParam]],
    0x18: ["checkver"],
    0x19: ["copybytetovar", ["address", RAMAddressParam]],
    0x1A: ["copyvartobyte", ["address", RAMAddressParam]],
    0x1B: ["loadvar", ["address", RAMAddressParam], ["value", SingleByteParam]],
    0x1C: ["checkcode", ["variable_id", SingleByteParam]],
    0x1D: ["writevarcode", ["variable_id", SingleByteParam]],
    0x1E: ["writecode", ["variable_id", SingleByteParam], ["value", SingleByteParam]],
    0x1F: ["giveitem", ["item", ItemLabelByte], ["quantity", SingleByteParam]],
    0x20: ["takeitem", ["item", ItemLabelByte], ["quantity", DecimalParam]],
    0x21: ["checkitem", ["item", ItemLabelByte]],
    0x22: ["givemoney", ["account", SingleByteParam], ["money", MoneyByteParam]],
    0x23: ["takemoney", ["account", SingleByteParam], ["money", MoneyByteParam]],
    0x24: ["checkmoney", ["account", SingleByteParam], ["money", MoneyByteParam]],
    0x25: ["givecoins", ["coins", CoinByteParam]],
    0x26: ["takecoins", ["coins", CoinByteParam]],
    0x27: ["checkcoins", ["coins", CoinByteParam]],
    # 0x28-0x2A not from pksv
    0x28: ["addcellnum", ["person", SingleByteParam]],
    0x29: ["delcellnum", ["person", SingleByteParam]],
    0x2A: ["checkcellnum", ["person", SingleByteParam]],
    # back on track...
    0x2B: ["checktime", ["time", SingleByteParam]],
    0x2C: ["checkpoke", ["pkmn", PokemonParam]],
#0x2D: ["givepoke", ], .... see GivePoke class
    0x2E: ["giveegg", ["pkmn", PokemonParam], ["level", DecimalParam]],
    0x2F: ["givepokeitem", ["pointer", PointerParamToItemAndLetter]],
    0x30: ["checkpokeitem", ["pointer", PointerParamToItemAndLetter]], # not pksv
    0x31: ["checkevent", ["event_flag", EventFlagParam]],
    0x32: ["clearevent", ["event_flag", EventFlagParam]],
    0x33: ["setevent", ["event_flag", EventFlagParam]],
    0x34: ["checkflag", ["engine_flag", EngineFlagParam]],
    0x35: ["clearflag", ["engine_flag", EngineFlagParam]],
    0x36: ["setflag", ["engine_flag", EngineFlagParam]],
    0x37: ["wildon"],
    0x38: ["wildoff"],
    0x39: ["xycompare", ["pointer", MultiByteParam]],
    0x3A: ["warpmod", ["warp_id", SingleByteParam], ["map_group", MapGroupParam], ["map_id", MapIdParam]],
    0x3B: ["blackoutmod", ["map_group", MapGroupParam], ["map_id", MapIdParam]],
    0x3C: ["warp", ["map_group", MapGroupParam], ["map_id", MapIdParam], ["x", SingleByteParam], ["y", SingleByteParam]],
    0x3D: ["readmoney", ["account", SingleByteParam], ["memory", SingleByteParam]], # not pksv
    0x3E: ["readcoins", ["memory", SingleByteParam]], # not pksv
    0x3F: ["RAM2MEM", ["memory", SingleByteParam]], # not pksv
    0x40: ["pokenamemem", ["pokemon", PokemonParam], ["memory", SingleByteParam]], # not pksv
    0x41: ["itemtotext", ["item", ItemLabelByte], ["memory", SingleByteParam]],
    0x42: ["mapnametotext", ["memory", SingleByteParam]], # not pksv
    0x43: ["trainertotext", ["trainer_id", TrainerGroupParam], ["trainer_group", TrainerIdParam], ["memory", SingleByteParam]],
    0x44: ["stringtotext", ["text_pointer", EncodedTextLabelParam], ["memory", SingleByteParam]],
    0x45: ["itemnotify"],
    0x46: ["pocketisfull"],
    0x47: ["loadfont"],
    0x48: ["refreshscreen", ["dummy", SingleByteParam]],
    0x49: ["loadmovesprites"],
    0x4A: ["loadbytec1ce", ["byte", SingleByteParam]], # not pksv
    0x4B: ["3writetext", ["text_pointer", PointerLabelBeforeBank]],
    0x4C: ["2writetext", ["text_pointer", RawTextPointerLabelParam]],
    0x4D: ["repeattext", ["byte", SingleByteParam], ["byte", SingleByteParam]], # not pksv
    0x4E: ["yesorno"],
    0x4F: ["loadmenudata", ["data", MenuDataPointerParam]],
    0x50: ["writebackup"],
    0x51: ["jumptextfaceplayer", ["text_pointer", RawTextPointerLabelParam]],
    0x52: ["3jumptext", ["text_pointer", PointerLabelBeforeBank]],
    0x53: ["jumptext", ["text_pointer", RawTextPointerLabelParam]],
    0x54: ["closetext"],
    0x55: ["keeptextopen"],
    0x56: ["pokepic", ["pokemon", PokemonParam]],
    0x57: ["pokepicyesorno"],
    0x58: ["interpretmenu"],
    0x59: ["interpretmenu2"],
# not pksv
    0x5A: ["loadpikachudata"],
    0x5B: ["battlecheck"],
    0x5C: ["loadtrainerdata"],
# back to pksv..
    0x5D: ["loadpokedata", ["pokemon", PokemonParam], ["level", DecimalParam]],
    0x5E: ["loadtrainer", ["trainer_group", TrainerGroupParam], ["trainer_id", TrainerIdParam]],
    0x5F: ["startbattle"],
    0x60: ["returnafterbattle"],
    0x61: ["catchtutorial", ["byte", SingleByteParam]],
# not pksv
    0x62: ["trainertext", ["which_text", SingleByteParam]],
    0x63: ["trainerstatus", ["action", SingleByteParam]],
# back to pksv..
    0x64: ["winlosstext", ["win_text_pointer", TextPointerLabelParam], ["loss_text_pointer", TextPointerLabelParam]],
    0x65: ["scripttalkafter"], # not pksv
    0x66: ["talkaftercancel"],
    0x67: ["talkaftercheck"],
    0x68: ["setlasttalked", ["person", SingleByteParam]],
    0x69: ["applymovement", ["person", SingleByteParam], ["data", MovementPointerLabelParam]],
    0x6A: ["applymovement2", ["data", MovementPointerLabelParam]], # not pksv
    0x6B: ["faceplayer"],
    0x6C: ["faceperson", ["person1", SingleByteParam], ["person2", SingleByteParam]],
    0x6D: ["variablesprite", ["byte", SingleByteParam], ["sprite", SingleByteParam]],
    0x6E: ["disappear", ["person", SingleByteParam]], # hideperson
    0x6F: ["appear", ["person", SingleByteParam]], # showperson
    0x70: ["follow", ["person2", SingleByteParam], ["person1", SingleByteParam]],
    0x71: ["stopfollow"],
    0x72: ["moveperson", ["person", SingleByteParam], ["x", SingleByteParam], ["y", SingleByteParam]],
    0x73: ["writepersonxy", ["person", SingleByteParam]], # not pksv
    0x74: ["loademote", ["bubble", SingleByteParam]],
    0x75: ["showemote", ["bubble", SingleByteParam], ["person", SingleByteParam], ["time", DecimalParam]],
    0x76: ["spriteface", ["person", SingleByteParam], ["facing", SingleByteParam]],
    0x77: ["follownotexact", ["person2", SingleByteParam], ["person1", SingleByteParam]],
    0x78: ["earthquake", ["param", DecimalParam]],
    0x79: ["changemap", ["map_data_pointer", MapDataPointerParam]],
    0x7A: ["changeblock", ["x", SingleByteParam], ["y", SingleByteParam], ["block", SingleByteParam]],
    0x7B: ["reloadmap"],
    0x7C: ["reloadmappart"],
    0x7D: ["writecmdqueue", ["queue_pointer", MultiByteParam]],
    0x7E: ["delcmdqueue", ["byte", SingleByteParam]],
    0x7F: ["playmusic", ["music_pointer", MultiByteParam]],
    0x80: ["playrammusic"],
    0x81: ["musicfadeout", ["music", MultiByteParam], ["fadetime", SingleByteParam]],
    0x82: ["playmapmusic"],
    0x83: ["reloadmapmusic"],
    0x84: ["cry", ["cry_id", PokemonWordParam]],
    0x85: ["playsound", ["sound_pointer", MultiByteParam]],
    0x86: ["waitbutton"],
    0x87: ["warpsound"],
    0x88: ["specialsound"],
    0x89: ["passtoengine", ["data_pointer", PointerLabelBeforeBank]],
    0x8A: ["newloadmap", ["which_method", SingleByteParam]],
    0x8B: ["pause", ["length", DecimalParam]],
    0x8C: ["deactivatefacing", ["time", SingleByteParam]],
    0x8D: ["priorityjump", ["pointer", ScriptPointerLabelParam]],
    0x8E: ["warpcheck"],
    0x8F: ["ptpriorityjump", ["pointer", ScriptPointerLabelParam]],
    0x90: ["return"],
    0x91: ["end"],
    0x92: ["reloadandreturn", ["which_method", SingleByteParam]],
    0x93: ["resetfuncs"],
    0x94: ["pokemart", ["dialog_id", SingleByteParam], ["mart_id", MultiByteParam]], # maybe it should be a pokemark constant id/label?
    0x95: ["elevator", ["floor_list_pointer", PointerLabelParam]],
    0x96: ["trade", ["trade_id", SingleByteParam]],
    0x97: ["askforphonenumber", ["number", SingleByteParam]],
    0x98: ["phonecall", ["caller_name", RawTextPointerLabelParam]],
    0x99: ["hangup"],
    0x9A: ["describedecoration", ["byte", SingleByteParam]],
    0x9B: ["fruittree", ["tree_id", SingleByteParam]],
    0x9C: ["specialphonecall", ["call_id", MultiByteParam]],
    0x9D: ["checkphonecall"],
    0x9E: ["verbosegiveitem", ["item", ItemLabelByte], ["quantity", DecimalParam]],
    0x9F: ["verbosegiveitem2", ["item", ItemLabelByte], ["var", SingleByteParam]],
    0xA0: ["loadwilddata", ["flag", SingleByteParam], ["map_group", MapGroupParam], ["map_id", MapIdParam]],
    0xA1: ["halloffame"],
    0xA2: ["credits"],
    0xA3: ["warpfacing", ["facing", SingleByteParam], ["map_group", MapGroupParam], ["map_id", MapIdParam], ["x", SingleByteParam], ["y", SingleByteParam]],
    0xA4: ["storetext", ["memory", SingleByteParam]],
    0xA5: ["displaylocation", ["id", SingleByteParam], ["memory", SingleByteParam]],
    0xA6: ["trainerclassname", ["id", SingleByteParam], ["memory", SingleByteParam]],
    0xA7: ["name", ["type", SingleByteParam], ["id", SingleByteParam], ["mempry", SingleByteParam]],
    0xA8: ["wait", ["duration", DecimalParam]],
    0xA9: ["unknown0xa9"],
}
def create_command_classes(debug=False):
    """creates some classes for each command byte"""
    # don't forget to add any manually created script command classes
    # .. except for Warp, Signpost and some others that aren't found in scripts
    klasses = [GivePoke]
    for (byte, cmd) in pksv_crystal_more.items():
        cmd_name = cmd[0].replace(" ", "_")
        params = {"id": byte, "size": 1, "end": byte in pksv.pksv_crystal_more_enders, "macro_name": cmd_name}
        params["param_types"] = {}
        if len(cmd) > 1:
            param_types = cmd[1:]
            for (i, each) in enumerate(param_types):
                thing = {"name": each[0], "class": each[1]}
                params["param_types"][i] = thing
                if debug:
                    logging.debug("each is {0} and thing[class] is {1}".format(each, thing["class"]))
                params["size"] += thing["class"].size
        klass_name = cmd_name+"Command"
        klass = type(klass_name, (Command,), params)
        globals()[klass_name] = klass
        klasses.append(klass)
    # later an individual klass will be instantiated to handle something
    return klasses
command_classes = create_command_classes()


class BigEndianParam(object):
    """big-endian word"""
    size = 2
    should_be_decimal = False
    byte_type = "bigdw"

    def __init__(self, *args, **kwargs):
        self.prefix = '$'
        for (key, value) in kwargs.items():
            setattr(self, key, value)
        self.parse()

    def parse(self):
        self.bytes = rom.interval(self.address, 2, strings=False)
        self.parsed_number = self.bytes[0] * 0x100 + self.bytes[1]

    def to_asm(self):
        if not self.should_be_decimal:
            return self.prefix+"".join([("%.2x")%x for x in self.bytes])
        elif self.should_be_decimal:
            decimal = int("0x"+"".join([("%.2x")%x for x in self.bytes]), 16)
            return str(decimal)

    @staticmethod
    def from_asm(value):
        return value

class DecimalBigEndianParam(BigEndianParam):
    should_be_decimal = True

music_commands = {
    0xD0: ["octave 8"],
    0xD1: ["octave 7"],
    0xD2: ["octave 6"],
    0xD3: ["octave 5"],
    0xD4: ["octave 4"],
    0xD5: ["octave 3"],
    0xD6: ["octave 2"],
    0xD7: ["octave 1"],
    0xD8: ["notetype", ["note_length", SingleByteParam], ["intensity", SingleByteParam]], # no intensity on channel 4/8
    0xD9: ["forceoctave", ["octave", SingleByteParam]],
    0xDA: ["tempo", ["tempo", DecimalBigEndianParam]],
    0xDB: ["dutycycle", ["duty_cycle", SingleByteParam]],
    0xDC: ["intensity", ["intensity", SingleByteParam]],
    0xDD: ["soundinput", ["input", SingleByteParam]],
    0xDE: ["unknownmusic0xde", ["unknown", SingleByteParam]],
    0xDF: ["togglesfx"],
    0xE0: ["unknownmusic0xe0", ["unknown", SingleByteParam], ["unknown", SingleByteParam]],
    0xE1: ["vibrato", ["delay", SingleByteParam], ["extent", SingleByteParam]],
    0xE2: ["unknownmusic0xe2", ["unknown", SingleByteParam]],
    0xE3: ["togglenoise", ["id", SingleByteParam]], # no parameters on toggle off
    0xE4: ["panning", ["tracks", SingleByteParam]],
    0xE5: ["volume", ["volume", SingleByteParam]],
    0xE6: ["tone", ["tone", BigEndianParam]],
    0xE7: ["unknownmusic0xe7", ["unknown", SingleByteParam]],
    0xE8: ["unknownmusic0xe8", ["unknown", SingleByteParam]],
    0xE9: ["globaltempo", ["value", DecimalBigEndianParam]],
    0xEA: ["restartchannel", ["address", PointerLabelParam]],
    0xEB: ["newsong", ["id", DecimalBigEndianParam]],
    0xEC: ["sfxpriorityon"],
    0xED: ["sfxpriorityoff"],
    0xEE: ["unknownmusic0xee", ["address", PointerLabelParam]],
    0xEF: ["stereopanning", ["tracks", SingleByteParam]],
    0xF0: ["sfxtogglenoise", ["id", SingleByteParam]], # no parameters on toggle off
    0xF1: ["music0xf1"], # nothing
    0xF2: ["music0xf2"], # nothing
    0xF3: ["music0xf3"], # nothing
    0xF4: ["music0xf4"], # nothing
    0xF5: ["music0xf5"], # nothing
    0xF6: ["music0xf6"], # nothing
    0xF7: ["music0xf7"], # nothing
    0xF8: ["music0xf8"], # nothing
    0xF9: ["unknownmusic0xf9"],
    0xFA: ["setcondition", ["condition", SingleByteParam]],
    0xFB: ["jumpif", ["condition", SingleByteParam], ["address", PointerLabelParam]],
    0xFC: ["jumpchannel", ["address", PointerLabelParam]],
    0xFD: ["loopchannel", ["count", DecimalParam], ["address", PointerLabelParam]],
    0xFE: ["callchannel", ["address", PointerLabelParam]],
    0xFF: ["endchannel"],
}

music_command_enders = [
    "restartchannel",
    "newsong",
    "unknownmusic0xee",
    "jumpchannel",
    "endchannel",
]

def create_music_command_classes(debug=False):
    klasses = []
    for (byte, cmd) in music_commands.items():
        cmd_name = cmd[0].replace(" ", "_")
        params = {
            "id": byte,
            "size": 1,
            "end": cmd[0] in music_command_enders,
            "macro_name": cmd[0]
        }
        params["param_types"] = {}
        if len(cmd) > 1:
            param_types = cmd[1:]
            for (i, each) in enumerate(param_types):
                thing = {"name": each[0], "class": each[1]}
                params["param_types"][i] = thing
                if debug:
                    logging.debug("each is {0} and thing[class] is {1}".format(each, thing["class"]))
                params["size"] += thing["class"].size
        klass_name = cmd_name+"Command"
        klass = type(klass_name, (Command,), params)
        globals()[klass_name] = klass
        if klass.macro_name == "notetype":
            klass.allowed_lengths = [1, 2]
        elif klass.macro_name in ["togglenoise", "sfxtogglenoise"]:
            klass.allowed_lengths = [0, 1]
        klasses.append(klass)
    # later an individual klass will be instantiated to handle something
    return klasses

music_classes = create_music_command_classes()

class OctaveParam(DecimalParam):
    @staticmethod
    def from_asm(value):
        value = int(value)
        return hex(0xd8 - value).replace("0x", "$")

class OctaveCommand(Command):
    macro_name = "octave"
    size = 0
    end = False
    param_types = {
        0: {"name": "octave", "class": OctaveParam},
    }
    allowed_lengths = [1]
    override_byte_check = True

class ChannelCommand(Command):
    macro_name = "channel"
    size = 3
    override_byte_check = True
    param_types = {
        0: {"name": "id", "class": DecimalParam},
        1: {"name": "address", "class": PointerLabelParam},
    }


# pokered

class callchannel(Command):
    id = 0xFD
    macro_name = "callchannel"
    size = 3
    param_types = {
        0: {"name": "address", "class": PointerLabelParam},
    }

class loopchannel(Command):
    id = 0xFE
    macro_name = "loopchannel"
    size = 4
    param_types = {
        0: {"name": "count", "class": SingleByteParam},
        1: {"name": "address", "class": PointerLabelParam},
    }


effect_commands = {
    0x1: ['checkturn'],
    0x2: ['checkobedience'],
    0x3: ['usedmovetext'],
    0x4: ['doturn'],
    0x5: ['critical'],
    0x6: ['damagestats'],
    0x7: ['stab'],
    0x8: ['damagevariation'],
    0x9: ['checkhit'],
    0xa: ['effect0x0a'],
    0xb: ['effect0x0b'],
    0xc: ['effect0x0c'],
    0xd: ['resulttext'],
    0xe: ['checkfaint'],
    0xf: ['criticaltext'],
    0x10: ['supereffectivetext'],
    0x11: ['checkdestinybond'],
    0x12: ['buildopponentrage'],
    0x13: ['poisontarget'],
    0x14: ['sleeptarget'],
    0x15: ['draintarget'],
    0x16: ['eatdream'],
    0x17: ['burntarget'],
    0x18: ['freezetarget'],
    0x19: ['paralyzetarget'],
    0x1a: ['selfdestruct'],
    0x1b: ['mirrormove'],
    0x1c: ['statup'],
    0x1d: ['statdown'],
    0x1e: ['payday'],
    0x1f: ['conversion'],
    0x20: ['resetstats'],
    0x21: ['storeenergy'],
    0x22: ['unleashenergy'],
    0x23: ['forceswitch'],
    0x24: ['endloop'],
    0x25: ['flinchtarget'],
    0x26: ['ohko'],
    0x27: ['recoil'],
    0x28: ['mist'],
    0x29: ['focusenergy'],
    0x2a: ['confuse'],
    0x2b: ['confusetarget'],
    0x2c: ['heal'],
    0x2d: ['transform'],
    0x2e: ['screen'],
    0x2f: ['poison'],
    0x30: ['paralyze'],
    0x31: ['substitute'],
    0x32: ['rechargenextturn'],
    0x33: ['mimic'],
    0x34: ['metronome'],
    0x35: ['leechseed'],
    0x36: ['splash'],
    0x37: ['disable'],
    0x38: ['cleartext'],
    0x39: ['charge'],
    0x3a: ['checkcharge'],
    0x3b: ['traptarget'],
    0x3c: ['effect0x3c'],
    0x3d: ['rampage'],
    0x3e: ['checkrampage'],
    0x3f: ['constantdamage'],
    0x40: ['counter'],
    0x41: ['encore'],
    0x42: ['painsplit'],
    0x43: ['snore'],
    0x44: ['conversion2'],
    0x45: ['lockon'],
    0x46: ['sketch'],
    0x47: ['defrostopponent'],
    0x48: ['sleeptalk'],
    0x49: ['destinybond'],
    0x4a: ['spite'],
    0x4b: ['falseswipe'],
    0x4c: ['healbell'],
    0x4d: ['kingsrock'],
    0x4e: ['triplekick'],
    0x4f: ['kickcounter'],
    0x50: ['thief'],
    0x51: ['arenatrap'],
    0x52: ['nightmare'],
    0x53: ['defrost'],
    0x54: ['curse'],
    0x55: ['protect'],
    0x56: ['spikes'],
    0x57: ['foresight'],
    0x58: ['perishsong'],
    0x59: ['startsandstorm'],
    0x5a: ['endure'],
    0x5b: ['checkcurl'],
    0x5c: ['rolloutpower'],
    0x5d: ['effect0x5d'],
    0x5e: ['furycutter'],
    0x5f: ['attract'],
    0x60: ['happinesspower'],
    0x61: ['present'],
    0x62: ['damagecalc'],
    0x63: ['frustrationpower'],
    0x64: ['safeguard'],
    0x65: ['checksafeguard'],
    0x66: ['getmagnitude'],
    0x67: ['batonpass'],
    0x68: ['pursuit'],
    0x69: ['clearhazards'],
    0x6a: ['healmorn'],
    0x6b: ['healday'],
    0x6c: ['healnite'],
    0x6d: ['hiddenpower'],
    0x6e: ['startrain'],
    0x6f: ['startsun'],
    0x70: ['attackup'],
    0x71: ['defenseup'],
    0x72: ['speedup'],
    0x73: ['specialattackup'],
    0x74: ['specialdefenseup'],
    0x75: ['accuracyup'],
    0x76: ['evasionup'],
    0x77: ['attackup2'],
    0x78: ['defenseup2'],
    0x79: ['speedup2'],
    0x7a: ['specialattackup2'],
    0x7b: ['specialdefenseup2'],
    0x7c: ['accuracyup2'],
    0x7d: ['evasionup2'],
    0x7e: ['attackdown'],
    0x7f: ['defensedown'],
    0x80: ['speeddown'],
    0x81: ['specialattackdown'],
    0x82: ['specialdefensedown'],
    0x83: ['accuracydown'],
    0x84: ['evasiondown'],
    0x85: ['attackdown2'],
    0x86: ['defensedown2'],
    0x87: ['speeddown2'],
    0x88: ['specialattackdown2'],
    0x89: ['specialdefensedown2'],
    0x8a: ['accuracydown2'],
    0x8b: ['evasiondown2'],
    0x8c: ['statmessageuser'],
    0x8d: ['statmessagetarget'],
    0x8e: ['statupfailtext'],
    0x8f: ['statdownfailtext'],
    0x90: ['effectchance'],
    0x91: ['effect0x91'],
    0x92: ['effect0x92'],
    0x93: ['switchturn'],
    0x94: ['fakeout'],
    0x95: ['bellydrum'],
    0x96: ['psychup'],
    0x97: ['rage'],
    0x98: ['doubleflyingdamage'],
    0x99: ['doubleundergrounddamage'],
    0x9a: ['mirrorcoat'],
    0x9b: ['checkfuturesight'],
    0x9c: ['futuresight'],
    0x9d: ['doubleminimizedamage'],
    0x9e: ['skipsuncharge'],
    0x9f: ['thunderaccuracy'],
    0xa0: ['teleport'],
    0xa1: ['beatup'],
    0xa2: ['ragedamage'],
    0xa3: ['effect0xa3'],
    0xa4: ['allstatsup'],
    0xa5: ['effect0xa5'],
    0xa6: ['effect0xa6'],
    0xa7: ['effect0xa7'],
    0xa8: ['effect0xa8'],
    0xa9: ['clearmissdamage'],
    0xaa: ['movedelay'],
    0xab: ['hittarget'],
    0xac: ['tristatuschance'],
    0xad: ['supereffectivelooptext'],
    0xae: ['startloop'],
    0xaf: ['curl'],
    0xfe: ['endturn'],
    0xff: ['endmove'],
}

effect_command_enders = [0xFF,]

def create_effect_command_classes(debug=False):
    klasses = []
    for (byte, cmd) in effect_commands.items():
        cmd_name = cmd[0].replace(" ", "_")
        params = {
            "id": byte,
            "size": 1,
            "end": byte in effect_command_enders,
            "macro_name": cmd_name
        }
        params["param_types"] = {}
        if len(cmd) > 1:
            param_types = cmd[1:]
            for (i, each) in enumerate(param_types):
                thing = {"name": each[0], "class": each[1]}
                params["param_types"][i] = thing
                if debug:
                    logging.debug("each is {0} and thing[class] is {1}".format(each, thing["class"]))
                params["size"] += thing["class"].size
        klass_name = cmd_name+"Command"
        klass = type(klass_name, (Command,), params)
        globals()[klass_name] = klass
        klasses.append(klass)
    # later an individual klass will be instantiated to handle something
    return klasses

effect_classes = create_effect_command_classes()



def generate_macros(filename=None):
    if filename == None:
        filename = os.path.join(conf.path, "script_macros.asm")
    """generates all macros based on commands
    this is dumped into script_macros.asm"""
    output  = "; This file is generated by generate_macros.\n"
    for command in command_classes:
        output += "\n"
        #if command.macro_name[0].isdigit():
        #    output += "_"
        output += command.macro_name + ": MACRO\n"
        output += spacing + "db $%.2x\n"%(command.id)
        current_param = 1
        for (index, each) in command.param_types.items():
            if issubclass(each["class"], SingleByteParam):
                output += spacing + "db \\" + str(current_param) + "\n"
            elif issubclass(each["class"], MultiByteParam):
                output += spacing + "dw \\" + str(current_param) + "\n"
            current_param += 1
        output += spacing + "ENDM\n"

    fh = open(filename, "w")
    fh.write(output)
    fh.close()

    return output

# use this to keep track of commands without pksv names
pksv_no_names = {}
def pretty_print_pksv_no_names():
    """just some nice debugging output
    use this to keep track of commands without pksv names
    pksv_no_names is created in parse_script_engine_script_at"""
    for (command_byte, addresses) in pksv_no_names.items():
        if command_byte in pksv.pksv_crystal_unknowns:
            continue
        print(hex(command_byte) + " appearing in these scripts: ")
        for address in addresses:
            print("    " + hex(address))

recursive_scripts = set([])
def rec_parse_script_engine_script_at(address, origin=None, debug=True):
    """this is called in parse_script_engine_script_at for recursion
    when this works it should be flipped back to using the regular
    parser."""
    recursive_scripts.add((address, origin))
    return parse_script_engine_script_at(address, origin=origin, debug=debug)

def find_broken_recursive_scripts(output=False, debug=True):
    """well.. these at least have a chance of maybe being broken?"""
    for r in list(recursive_scripts):
        script = {}
        length = "not counted here"
        if is_script_already_parsed_at(r[0]):
            script = script_parse_table[r[0]]
            length = str(len(script))
        if len(script) > 20 or script == {}:
            logging.debug(
                "script at {address} from main script {scr} with length {length}"
                .format(
                    address=hex(r[0]),
                    scr=hex(r[1]),
                    length=length,
                )
            )
            if output:
                parse_script_engine_script_at(r[0], force=True, debug=True)


stop_points = [0x1aafa2,
               0x9f58f, # battle tower
               0x9f62f, # battle tower
              ]
class Script(object):
    base_label = "UnknownScript_"
    def __init__(self, *args, **kwargs):
        self.address = None
        self.commands = None
        if len(kwargs) == 0 and len(args) == 0:
            raise Exception("Script.__init__ must be given some arguments")
        # first positional argument is address
        if len(args) == 1:
            address = args[0]
            if type(address) == str:
                address = int(address, 16)
            elif type(address) != int:
                raise Exception("address must be an integer or string")
            self.address = address
        elif len(args) > 1:
            raise Exception("don't know what to do with second (or later) positional arguments")
        self.dependencies = None
        if "label" in kwargs.keys():
            label = kwargs["label"]
        else:
            label = None
        if not label:
            label = self.base_label + hex(self.address)
        self.label = Label(name=label, address=address, object=self)
        if "map_group" in kwargs.keys():
            self.map_group = kwargs["map_group"]
        if "map_id" in kwargs.keys():
            self.map_id = kwargs["map_id"]
        if "parent" in kwargs.keys():
            self.parent = kwargs["parent"]
        # parse the script at the address
        if "use_old_parse" in kwargs.keys() and kwargs["use_old_parse"] == True:
            self.old_parse(**kwargs)
        else:
            self.parse(self.address, **kwargs)

    def pksv_list(self):
        """shows a list of pksv names for each command in the script"""
        items = []
        if type(self.commands) == dict:
            for (id, command) in self.commands.items():
                if command["type"] in pksv.pksv_crystal:
                    items.append(pksv.pksv_crystal[command["type"]])
                else:
                    items.append(hex(command["type"]))
        else:
            for command in self.commands:
                items.append(command.macro_name)
        return items


    def to_pksv(self):
        """returns a string of pksv command names"""
        pksv = self.pksv_list()
        output = "script starting at: "+hex(self.address)+" .. "
        first = True
        for item in pksv:
            item = str(item)
            if first:
                output += item
                first = False
            else:
                output += ", "+item
        return output

    def show_pksv(self):
        """prints a list of pksv command names in this script"""
        logging.debug("to_pksv(): {0}".format(self.to_pksv()))

    def parse(self, start_address, force=False, map_group=None, map_id=None, force_top=True, origin=True, debug=False):
        """parses a script using the Command classes
        as an alternative to the old method using hard-coded commands

        force_top just means 'force the main script to get parsed, but not any subscripts'
        """
        global command_classes, rom, script_parse_table
        current_address = start_address
        if debug:
            logging.debug(
                "Script.parse address={address} map_group={map_group} map_id={map_id}"
                .format(
                    address=hex(self.address),
                    map_group=str(map_group),
                    map_id=str(map_id),
                )
            )
        if start_address in stop_points and force == False:
            if debug:
                logging.debug(
                    "script parsing is stopping at stop_point={stop_point} at map_group={map_group} map_id={map_id}"
                    .format(
                        stop_point=hex(start_address),
                        map_group=str(map_group),
                        map_id=str(map_id),
                    )
                )
            return None
        if start_address < 0x4000 and start_address not in [0x26ef, 0x114, 0x1108]:
            if debug:
                logging.debug("address is less than 0x4000.. address is {0}".format(hex(start_address)))
            sys.exit(1)
        if is_script_already_parsed_at(start_address) and not force and not force_top:
            raise Exception("this script has already been parsed before, please use that instance ("+hex(start_address)+")")

        # load up the rom if it hasn't been loaded already
        rom = load_rom()

        # in the event that the script parsing fails.. it would be nice to leave evidence
        script_parse_table[start_address:start_address+1] = "incomplete parse_script_with_command_classes"

        # start with a blank script
        commands = []

        # use this to control the while loop
        end = False

        # for each command found..
        while not end:
            # get the current scripting byte
            cur_byte = ord(rom[current_address])

            # reset the command class (last command was probably different)
            scripting_command_class = None

            # match the command id byte to a scripting command class like GivePoke
            for class_ in command_classes:
                if class_.id == cur_byte:
                    scripting_command_class = class_

            # no matching command found (not implemented yet)- just end this script
            # NOTE: might be better to raise an exception and end the program?
            if scripting_command_class == None:
                if debug:
                    logging.debug("parsing script; current_address is: {0}".format(hex(current_address)))
                current_address += 1
                asm_output = "\n".join([command.to_asm() for command in commands])
                end = True
                continue
                # maybe the program should exit with failure instead?
                #raise Exception("no command found? id: " + hex(cur_byte) + " at " + hex(current_address) + " asm is:\n" + asm_output)

            # create an instance of the command class and let it parse its parameter bytes
            #print "about to parse command(script@"+hex(start_address)+"): " + str(scripting_command_class.macro_name)
            cls = scripting_command_class(address=current_address, force=force, map_group=map_group, map_id=map_id, parent=self)

            #if self.debug:
            #    print cls.to_asm()

            # store it in this script object
            commands.append(cls)

            # certain commands will end the scripting engine
            end = cls.end

            # skip past the command's parameter bytes to go to the next command
            #current_address = cls.last_address + 1
            current_address += cls.size

        # last byte belonging to script is last byte of last command,
        # or the last byte of the last command's last parameter
        self.last_address = current_address

        # store the script in the global table/map thing
        script_parse_table[start_address:current_address] = self

        asm_output = "\n".join([command.to_asm() for command in commands])
        if debug:
            logging.debug("asm_output is: {0}".format(asm_output))

        # store the script
        self.commands = commands

        return commands

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        dependencies = []
        for command in self.commands:
            deps = command.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
            dependencies.extend(deps)
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        asm_output = "".join([command.to_asm()+"\n" for command in self.commands])
        if asm_output[-1] == "\n":
            asm_output = asm_output[:-1]
        return asm_output

    def old_parse(self, *args, **kwargs):
        """included from old_parse_scripts"""

from . import old_parse_scripts
Script.old_parse = old_parse_scripts.old_parse

def parse_script_engine_script_at(address, map_group=None, map_id=None, force=False, debug=True, origin=True):
    if is_script_already_parsed_at(address) and not force:
        return script_parse_table[address]
    return Script(address, map_group=map_group, map_id=map_id, force=force, debug=debug, origin=origin)

def compare_script_parsing_methods(address):
    """
    compares the parsed scripts using the new method and the old method
    The new method is Script.parse, the old method is Script.old_parse.

    There are likely to be problems with the new script parser, the one
    that uses the command classes to parse bytes. To look for these
    problems, you can compare the output of one parsing method to the
    output of the other. When there's a difference, there is something
    worth correcting. Probably by each command's "macro_name" attribute.
    """
    rom = load_rom()
    separator = "################ compare_script_parsing_methods"
    # first do it the old way
    logging.debug(separator)
    logging.debug("parsing the script at {address} using the old method".format(address=hex(address)))
    oldscript = Script(address, debug=True, force=True, origin=True, use_old_parse=True)
    # and now the old way
    logging.debug(separator)
    logging.debug("parsing the script at {address} using the new method".format(address=hex(address)))
    newscript = Script(address, debug=True, force=True, origin=True)
    # let the comparison begin..
    errors = 0
    logging.debug("{0} COMPARISON RESULTS".format(separator))
    if not len(oldscript.commands.keys()) == len(newscript.commands):
        logging.debug("the two scripts don't have the same number of commands")
        errors += 1
    for (id, oldcommand) in oldscript.commands.items():
        newcommand = newscript.commands[id]
        oldcommand_pksv_name = pksv.pksv_crystal[oldcommand["type"]].replace(" ", "_")
        if oldcommand["start_address"] != newcommand.address:
            logging.debug(
                "The two address (command id={id}) do not match old={old} new={new}"
                .format(
                    id=id,
                    old=hex(oldcommand["start_address"]),
                    new=hex(newcommand.address),
                )
            )
            errors += 1
        if oldcommand_pksv_name != newcommand.macro_name:
            logging.debug(
                "The two commands (id={id}) do not have the same name old={old} new={new}"
                .format(
                    id=id,
                    old=oldcommand_pksv_name,
                    new=newcommand.macro_name,
                )
            )
            errors += 1
    logging.info("total comparison errors: {0}".format(errors))
    return oldscript, newscript


class Warp(Command):
    """only used outside of scripts"""
    size = warp_byte_size
    macro_name = "warp_def"
    param_types = {
        0: {"name": "y", "class": HexByte},
        1: {"name": "x", "class": HexByte},
        2: {"name": "warp_to", "class": DecimalParam},
        3: {"name": "map_bank", "class": MapGroupParam},
        4: {"name": "map_id", "class": MapIdParam},
    }
    override_byte_check = True

    def __init__(self, *args, **kwargs):
        self.id = kwargs["id"]
        script_parse_table[kwargs["address"] : kwargs["address"] + self.size] = self
        Command.__init__(self, *args, **kwargs)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

all_warps = []
def parse_warps(address, warp_count, bank=None, map_group=None, map_id=None, debug=True):
    warps = []
    current_address = address
    for each in range(warp_count):
        warp = Warp(address=current_address, id=each, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        current_address += warp_byte_size
        warps.append(warp)
    all_warps.extend(warps)
    return warps

class XYTrigger(Command):
    size = trigger_byte_size
    macro_name = "xy_trigger"
    param_types = {
        0: {"name": "number", "class": DecimalParam},
        1: {"name": "y", "class": HexByte},
        2: {"name": "x", "class": HexByte},
        3: {"name": "unknown1", "class": SingleByteParam},
        4: {"name": "script", "class": ScriptPointerLabelParam},
        5: {"name": "unknown2", "class": SingleByteParam},
        6: {"name": "unknown3", "class": SingleByteParam},
    }
    override_byte_check = True

    def __init__(self, *args, **kwargs):
        self.id = kwargs["id"]
        self.dependencies = None
        Command.__init__(self, *args, **kwargs)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        dependencies = []
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        thing = script_parse_table[self.params[4].parsed_address]
        if thing and thing != self.params[4]:
            dependencies.append(thing)
            global_dependencies.add(thing)
        self.dependencies = dependencies
        return dependencies

all_xy_triggers = []
def parse_xy_triggers(address, trigger_count, bank=None, map_group=None, map_id=None, debug=True):
    xy_triggers = []
    current_address = address
    for each in range(trigger_count):
        xy_trigger = XYTrigger(address=current_address, id=each, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        current_address += trigger_byte_size
        xy_triggers.append(xy_trigger)
    all_xy_triggers.extend(xy_triggers)
    return xy_triggers

def old_parse_xy_trigger_bytes(some_bytes, bank=None, map_group=None, map_id=None, debug=True):
    """parse some number of triggers from the data"""
    assert len(some_bytes) % trigger_byte_size == 0, "wrong number of bytes"
    triggers = []
    for bytes in helpers.grouper(some_bytes, count=trigger_byte_size):
        trigger_number = int(bytes[0], 16)
        y = int(bytes[1], 16)
        x = int(bytes[2], 16)
        unknown1 = int(bytes[3], 16) # XXX probably 00?
        script_ptr_byte1 = int(bytes[4], 16)
        script_ptr_byte2 = int(bytes[5], 16)
        script_ptr = script_ptr_byte1 + (script_ptr_byte2 << 8)
        script_address = None
        script = None
        if bank:
            script_address = pointers.calculate_pointer(script_ptr, bank)
            logging.debug(
                "parsing xy trigger byte scripts.. x={x} y={y}"
                .format(x=x, y=y)
            )
            script = parse_script_engine_script_at(script_address, map_group=map_group, map_id=map_id)

        triggers.append({
            "trigger_number": trigger_number,
            "y": y,
            "x": x,
            "unknown1": unknown1, # probably 00
            "script_ptr": script_ptr,
            "script_pointer": {"1": script_ptr_byte1, "2": script_ptr_byte2},
            "script_address": script_address,
            "script": script,
        })
    return triggers


class ItemFragment(Command):
    """used by ItemFragmentParam and PeopleEvent
    (for items placed on a map)"""
    size = 2
    macro_name = "db"
    base_label = "ItemFragment_"
    override_byte_check = True
    param_types = {
        0: {"name": "item", "class": ItemLabelByte},
        1: {"name": "quantity", "class": DecimalParam},
    }

    def __init__(self, address=None, bank=None, map_group=None, map_id=None, debug=False, label=None):
        if not is_valid_address(address):
            raise exceptions.AddressException(
                "ItemFragment must be given a valid address (but got {0})."
                .format(hex(address))
            )
        self.address = address
        self.last_address = address + self.size
        self.bank = bank
        if not label:
            label = self.base_label + hex(address)
        self.label = Label(name=label, address=address, object=self)
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.params = {}
        self.dependencies = None
        self.args = {"debug": debug, "map_group": map_group, "map_id": map_id, "bank": bank}
        script_parse_table[self.address : self.last_address] = self
        self.parse()


class ItemFragmentParam(PointerLabelParam):
    """used by PeopleEvent"""

    def parse(self):
        PointerLabelParam.parse(self)

        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        self.calculated_address = address

        itemfrag = ItemFragment(address=address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.itemfrag = itemfrag

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        self.dependencies = [self.itemfrag].extend(self.itemfrag.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        global_dependencies.add(self.itemfrag)
        return self.dependencies

class TrainerFragment(Command):
    """used by TrainerFragmentParam and PeopleEvent for trainer data

    Maybe this shouldn't be a Command. The output might sprawl
    over multiple lines, and maybe it should be commented in to_asm?

    [Event flag (2byte)][Trainer group][Trainer]
    [2byte pointer to Text when seen]
    [2byte pointer to text when trainer beaten]
    [2byte pointer to script when lost (0000=Blackout)]
    [2byte pointer to script if won/talked to again]

    The event flag tells the game later on if the trainer has been
    beaten already (set) or not (reset).

    03 = Nothing
    04 = Nothing
    05 = Nothing
    06 = Nothing
    """
    size = 12
    macro_name = "trainer_def"
    base_label = "Trainer_"
    override_byte_check = True
    param_types = {
        0: {"name": "event_flag", "class": EventFlagParam},
        1: {"name": "trainer_group", "class": TrainerGroupParam},
        2: {"name": "trainer_id", "class": TrainerIdParam},
        3: {"name": "text_when_seen", "class": TextPointerLabelParam},
        4: {"name": "text_when_trainer_beaten", "class": TextPointerLabelParam},
        5: {"name": "script_when_lost", "class": ScriptPointerLabelParam},
        6: {"name": "script_talk_again", "class": ScriptPointerLabelParam},
    }

    def __init__(self, *args, **kwargs):
        address = kwargs["address"]
        logging.debug("TrainerFragment address={0}".format(hex(address)))
        self.address = address
        self.last_address = self.address + self.size
        if not is_valid_address(address) or address in [0x26ef]:
            self.include_in_asm = False
            return
        script_parse_table[self.address : self.last_address] = self
        self.dependencies = None
        Command.__init__(self, *args, **kwargs)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        deps = []
        if not is_valid_address(self.address):
            return deps
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        #deps.append(self.params[3])
        deps.extend(self.params[3].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        #deps.append(self.params[4])
        deps.extend(self.params[4].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        #deps.append(self.params[5])
        deps.extend(self.params[5].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        #deps.append(self.params[6])
        deps.extend(self.params[6].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = deps
        return deps

    def parse(self):
        Command.parse(self)

        # get the trainer group id
        trainer_group = self.params[1].byte

        # get the trainer id
        trainer_id = self.params[2].byte

        if not trainer_group in self.trainer_group_maximums.keys():
            self.trainer_group_maximums[trainer_group] = set([trainer_id])
        else:
            self.trainer_group_maximums[trainer_group].add(trainer_id)

        # give this object a possibly better label
        label = "Trainer"
        if ("uses_numeric_trainer_ids" in trainers.trainer_group_names[trainer_group].keys()) \
           or ("trainer_names" not in trainers.trainer_group_names[trainer_group].keys()):
            label += string.capwords(trainers.trainer_group_names[trainer_group]["constant"])
            if "trainer_names" in trainers.trainer_group_names[trainer_group].keys() \
               and len(trainers.trainer_group_names[trainer_group]["trainer_names"]) > 1:
                label += str(trainer_id)
        else:
            label += string.capwords(trainers.trainer_group_names[trainer_group]["constant"]) + \
                     string.capwords(trainers.trainer_group_names[trainer_group]["trainer_names"][trainer_id-1])

        label = label.replace("Gruntm", "GruntM").replace("Gruntf", "GruntF").replace("Lt_surge", "LtSurge")

        self.label = Label(name=label, address=self.address, object=self)

        # ---- give better labels to the objects created by TrainerFragment ----

        text_when_seen_text = script_parse_table[self.params[3].parsed_address]
        if text_when_seen_text != None:
            text_when_seen_label = Label(name=label + "WhenSeenText", address=text_when_seen_text.address, object=text_when_seen_text)
            text_when_seen_text.label = text_when_seen_label

        text_when_beaten_text = script_parse_table[self.params[4].parsed_address]
        if text_when_beaten_text != None:
            text_when_beaten_label = Label(name=label + "WhenBeatenText", address=text_when_beaten_text.address, object=text_when_beaten_text)
            text_when_beaten_text.label = text_when_beaten_label

        script_when_lost = script_parse_table[self.params[5].parsed_address]
        if script_when_lost != None:
            script_when_lost_label = Label(name=label + "WhenLostScript", address=script_when_lost.address, object=script_when_lost)
            script_when_lost.label = script_when_lost_label

        script_talk_again = script_parse_table[self.params[6].parsed_address]
        if script_talk_again != None:
            script_talk_again_label = Label(name=label + "WhenTalkScript", address=script_talk_again.address, object=script_talk_again)
            script_talk_again.label = script_talk_again_label

    def to_asm(self):
        xspacing = ""
        output = ""
        output += xspacing + "; event flag\n"
        output += xspacing + "dw $%.2x"%(self.params[0].parsed_number)
        output += "\n\n"+xspacing+"; trainer group && trainer id\n"
        output += xspacing + "db %s, %s" % (self.params[1].to_asm(), self.params[2].to_asm())
        output += "\n\n"+xspacing+"; text when seen\n"
        output += xspacing + "dw " + self.params[3].to_asm()
        output += "\n\n"+xspacing+"; text when trainer beaten\n"
        output += xspacing + "dw " + self.params[4].to_asm()
        output += "\n\n"+xspacing+"; script when lost\n"
        output += xspacing + "dw " + self.params[5].to_asm()
        output += "\n\n"+xspacing+"; script when talk again\n"
        output += xspacing + "dw " + self.params[6].to_asm()
        return output

class TrainerFragmentParam(PointerLabelParam):
    """used by PeopleEvent to point to trainer data"""
    def parse(self):
        address = calculate_pointer_from_bytes_at(self.address, bank=self.bank)
        self.calculated_address = address
        if address == 0x26ef:
            self.trainerfrag = None
        else:
            trainerfrag = TrainerFragment(address=address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
            self.trainerfrag = trainerfrag
        PointerLabelParam.parse(self)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        deps = []
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        if self.trainerfrag:
            global_dependencies.add(self.trainerfrag)
            deps.append(self.trainerfrag)
            deps.extend(self.trainerfrag.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = deps
        return deps

trainer_group_table = None
class TrainerGroupTable(object):
    """
    A list of pointers.

    This should probably be called TrainerGroupPointerTable.
    """

    def __init__(self, trainer_group_maximums=None, trainers=None, script_parse_table=None):
        self.trainer_group_maximums = trainer_group_maximums
        self.trainers = trainers
        self.script_parse_table = script_parse_table
        assert 0x43 in self.trainer_group_maximums.keys(), "TrainerGroupTable should only be created after all the trainers have been found"
        self.address = self.trainers.trainer_group_pointer_table_address
        self.bank = pointers.calculate_bank(self.trainers.trainer_group_pointer_table_address)
        self.label = Label(name="TrainerGroupPointerTable", address=self.address, object=self)
        self.size = None
        self.last_address = None
        self.dependencies = None
        self.headers = []
        self.parse()

        self.script_parse_table[self.address : self.last_address] = self

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        global_dependencies.update(self.headers)
        if recompute == True and self.dependencies != None and self.dependencies != []:
            return self.dependencies
        dependencies = copy(self.headers)
        for header in self.headers:
            dependencies.extend(header.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        return dependencies

    def parse(self):
        size = 0
        for (key, kvalue) in self.trainers.trainer_group_names.items():
            # calculate the location of this trainer group header from its pointer
            pointer_bytes_location = kvalue["pointer_address"]
            parsed_address = calculate_pointer_from_bytes_at(pointer_bytes_location, bank=self.bank)
            self.trainers.trainer_group_names[key]["parsed_address"] = parsed_address

            # parse the trainer group header at this location
            name = kvalue["name"]
            trainer_group_header = TrainerGroupHeader(address=parsed_address, group_id=key, group_name=name, trainer_group_maximums=self.trainer_group_maximums)
            self.trainers.trainer_group_names[key]["header"] = trainer_group_header
            self.headers.append(trainer_group_header)

            # keep track of the size of this pointer table
            size += 2
        self.size = size
        self.last_address = self.address + self.size

    def to_asm(self):
        output = "".join([str("dw "+get_label_for(header.address)+"\n") for header in self.headers])
        return output

class TrainerGroupHeader(object):
    """
    A trainer group header is a repeating list of individual trainer headers.

    <Trainer Name> <0x50> <Data type> <Pokémon Data>+ <0xFF>

    Data type <0x00>: Pokémon Data is <Level> <Species>. Used by most trainers.
    Data type <0x01>: Pokémon Data is <Level> <Pokémon> <Move1> <Move2> <Move3> <Move4>. Used often for Gym Leaders.
    Data type <0x02>: Pokémon Data is <Level> <Pokémon> <Held Item>. Used mainly by Pokéfans.
    Data type <0x03>: Pokémon Data is <Level> <Pokémon> <Held Item> <Move1> <Move2> <Move3> <Move4>. Used by a few Cooltrainers.
    """

    def __init__(self, address=None, group_id=None, group_name=None, trainer_group_maximums=None):
        assert address!=None, "TrainerGroupHeader requires an address"
        assert group_id!=None, "TrainerGroupHeader requires a group_id"
        assert group_name!=None, "TrainerGroupHeader requires a group_name"

        self.trainer_group_maximums = trainer_group_maximums

        self.address = address
        self.group_id = group_id
        self.group_name = group_name
        self.dependencies = None
        self.individual_trainer_headers = []
        self.label = Label(name=group_name+"TrainerGroupHeader", address=self.address, object=self)
        self.parse()

        script_parse_table[address : self.last_address] = self

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        """
        TrainerGroupHeader has no dependencies.
        """
        # TODO: possibly include self.individual_trainer_headers
        if recompute or self.dependencies == None:
            self.dependencies = []
        return self.dependencies

    def parse(self):
        """
        how do i know when there's no more data for this header?
         do a global analysis of the rom and figure out the max ids
         this wont work for rom hacks of course
        see find_trainer_ids_from_scripts
        """
        size = 0
        current_address = self.address

        if self.group_id not in self.trainer_group_maximums.keys():
            self.size = 0
            self.last_address = current_address
            return

        # create an IndividualTrainerHeader for each id in range(min id, max id + 1)
        min_id = min(self.trainer_group_maximums[self.group_id])
        max_id = max(self.trainer_group_maximums[self.group_id])

        if self.group_id == 0x0C:
            # CAL appears a third time with third-stage evos (meganium, typhlosion, feraligatr)
            max_id += 1
        elif self.group_id == 0x29:
            # there's a missing supernerd :(
            max_id += 1
        elif self.group_id == 0x2D:
            # missing bikers
            max_id += 2
        elif self.group_id == 0x31:
            # missing jugglers
            max_id += 3
        elif self.group_id == 0x32:
            # blackbelt wai
            max_id += 1
        elif self.group_id == 0x3C:
            # kimono girl miki
            max_id += 1
        elif self.group_id == 0x3D:
            # twins lea & pia
            max_id += 1

        for trainer_id in range(min_id, max_id+1):
            trainer_header = TrainerHeader(address=current_address, trainer_group_id=self.group_id, trainer_id=trainer_id, parent=self)
            self.individual_trainer_headers.append(trainer_header)
            # current_address += trainer_header.size
            current_address = trainer_header.last_address
            size += trainer_header.size

        self.last_address = current_address
        self.size = size

    def to_asm(self):
        output = "\n\n".join(["; "+header.make_constant_name()+" ("+str(header.trainer_id)+") at "+hex(header.address)+"\n"+header.to_asm() for header in self.individual_trainer_headers])
        return output

class TrainerHeader(object):
    """
    <Trainer Name> <0x50> <Data type> <Pokémon Data>+ <0xFF>

    Data type <0x00>: Pokémon Data is <Level> <Species>. Used by most trainers.
    Data type <0x01>: Pokémon Data is <Level> <Pokémon> <Move1> <Move2> <Move3> <Move4>. Used often for Gym Leaders.
    Data type <0x02>: Pokémon Data is <Level> <Pokémon> <Held Item>. Used mainly by Pokéfans.
    Data type <0x03>: Pokémon Data is <Level> <Pokémon> <Held Item> <Move1> <Move2> <Move3> <Move4>. Used by a few Cooltrainers.
    """

    def __init__(self, address=None, trainer_group_id=None, trainer_id=None, parent=None):
        self.parent = parent
        self.address = address
        self.trainer_group_id = trainer_group_id
        self.trainer_id = trainer_id
        self.dependencies = []
        self.size = None
        self.last_address = None
        self.parse()
        self.label = Label(name=self.make_name(), address=self.address, object=self)
        # this shouldn't be added to script_parse_table because
        # TrainerGroupHeader covers its address range

    def make_name(self):
        """
        Must occur after parse() is called.
        Constructs a name based on self.parent.group_name and self.name.
        """
        if self.trainer_group_id in [0x14, 0x16, 0x17, 0x18, 0x19, 0x1B, 0x1C, 0x1D, 0x1E, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2B, 0x2C, 0x2D, 0x2F, 0x30, 0x31, 0x32, 0x34, 0x35, 0x36, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x41]:
            return self.parent.group_name.upper() + "_" + self.name[:-1]
        else:
            return self.parent.group_name + "_" + str(self.trainer_id)

    def make_constant_name(self):
        if hasattr(self, "seed_constant_name"):
            seed = self.seed_constant_name
        else:
            seed = self.name

        if "?" in seed:
            if seed[-2].isdigit():
                x = 2
            else:
                x = 1
            seed = trainers.trainer_group_names[self.trainer_group_id]["name"]+"_"+seed[-x:]
        elif self.trainer_group_id == 0x1f and "EXECUTIVE" in seed:
            seed = "GRUNT_"+seed
        elif self.trainer_group_id == 0x2d and "BENNY" in seed.upper():
            seed = "BIKER_BENNY"
        elif self.trainer_group_id == 0x24 and "BENNY" in seed.upper():
            seed = "BUG_CATCHER_BENNY"

        return string.capwords(seed).\
               replace("@", "").\
               replace(" & ", "AND").\
               replace(" ", "").\
               replace(".", "_").\
               upper()

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if recompute or self.dependencies == None:
            self.dependencies = []
        return self.dependencies

    def parse(self):
        address = self.address

        # figure out how many bytes until 0x50 "@"
        jump = how_many_until(chr(0x50), address, rom)

        # parse the "@" into the name
        self.name = parse_text_at(address, jump+1)

        # where is the next byte?
        current_address = address + jump + 1

        # figure out the pokemon data type
        self.data_type = ord(rom[current_address])

        current_address += 1

        # figure out which partymon parser to use for this trainer header
        party_mon_parser = None
        for monparser in trainer_party_mon_parsers:
            if monparser.id == self.data_type:
                party_mon_parser = monparser
                break

        if party_mon_parser == None:
            raise Exception("no trainer party mon parser found to parse data type " + hex(self.data_type))

        self.party_mons = party_mon_parser(address=current_address, group_id=self.trainer_group_id, trainer_id=self.trainer_id, parent=self)

        # let's have everything in trainer_party_mon_parsers handle the last $FF
        #self.size = self.party_mons.size + 1 + len(self.name)
        self.size = self.party_mons.last_address - self.address
        self.last_address = self.party_mons.last_address

    def to_asm(self):
        output = "db \""+self.name+"\"\n"
        output += "db $%.2x ; data type\n" % (self.data_type)
        output += self.party_mons.to_asm()
        output += "\n; last_address="+hex(self.last_address)+" size="+str(self.size)
        return output

class TrainerPartyMonParser(object):
    """
    Just a generic trainer party mon parser.
    Don't use this directly. Only use the child classes.
    """
    id = None
    dependencies = None
    param_types = None

    # could go either way on this one.. TrainerGroupHeader.parse would need to be changed
    # so as to not increase current_address by one after reading "data_type"
    override_byte_check = True

    def __init__(self, address=None, group_id=None, trainer_id=None, parent=None):
        self.address = address
        self.group_id = group_id
        self.trainer_id = trainer_id
        self.parent = parent
        self.args = {}
        self.mons = {}
        self.parse()

        # pick up the $FF at the end
        self.last_address += 1

    def parse(self):
        current_address = self.address
        pkmn = 0
        continuer = True
        while continuer:
            self.mons[pkmn] = {}
            i = 0
            for (key, param_type) in self.param_types.items():
                name = param_type["name"]
                klass = param_type["class"]
                # make an instance of this class, like SingleByteParam()
                # or ItemLabelByte.. by making an instance, obj.parse() is called
                obj = klass(address=current_address, name=name, parent=self, **dict([(k,v) for (k, v) in self.args.items() if k not in ["parent"]]))
                # save this for later
                self.mons[pkmn][i] = obj
                # increment our counters
                current_address += obj.size
                i += 1
            pkmn += 1
            if ord(rom[current_address]) == 0xFF:
                break
        self.last_address = current_address
        return True

    def to_asm(self):
        output = ""
        #output = "; " + ", ".join([param_type["name"] for (key, param_type) in self.param_types.items()]) + "\n"
        for mon in self.mons:
            output += "db " + ", ".join([param.to_asm() for (name, param) in self.mons[mon].items()])
            output += "\n"
        output += "db $ff ; end trainer party mons"
        return output

class TrainerPartyMonParser0(TrainerPartyMonParser):
    """
    Data type <0x00>: Pokémon Data is <Level> <Species>. Used by most trainers.
    """
    id = 0
    size = 2 + 1
    param_types = {
        0: {"name": "level", "class": DecimalParam},
        1: {"name": "species", "class": PokemonParam},
    }
class TrainerPartyMonParser1(TrainerPartyMonParser):
    """
    Data type <0x01>: Pokémon Data is <Level> <Pokémon> <Move1> <Move2> <Move3>
    <Move4>. Used often for Gym Leaders.
    """
    id = 1
    size = 6 + 1
    param_types = {
        0: {"name": "level", "class": DecimalParam},
        1: {"name": "species", "class": PokemonParam},
        2: {"name": "move1", "class": MoveParam},
        3: {"name": "move2", "class": MoveParam},
        4: {"name": "move3", "class": MoveParam},
        5: {"name": "move4", "class": MoveParam},
    }
class TrainerPartyMonParser2(TrainerPartyMonParser):
    """
    Data type <0x02>: Pokémon Data is <Level> <Pokémon> <Held Item>. Used mainly by Pokéfans.
    """
    id = 2
    size = 3 + 1
    param_types = {
        0: {"name": "level", "class": DecimalParam},
        1: {"name": "species", "class": PokemonParam},
        2: {"name": "item", "class": ItemLabelByte},
    }
class TrainerPartyMonParser3(TrainerPartyMonParser):
    """
    Data type <0x03>: Pokémon Data is <Level> <Pokémon> <Held Item> <Move1>
    <Move2> <Move3> <Move4>.
    Used by a few Cooltrainers.
    """
    id = 3
    size = 7 + 1
    param_types = {
        0: {"name": "level", "class": DecimalParam},
        1: {"name": "species", "class": PokemonParam},
        2: {"name": "item", "class": ItemLabelByte},
        3: {"name": "move1", "class": MoveParam},
        4: {"name": "move2", "class": MoveParam},
        5: {"name": "move3", "class": MoveParam},
        6: {"name": "move4", "class": MoveParam},
    }

trainer_party_mon_parsers = [TrainerPartyMonParser0, TrainerPartyMonParser1, TrainerPartyMonParser2, TrainerPartyMonParser3]

def find_trainer_ids_from_scripts(script_parse_table=None, trainer_group_maximums=None):
    """
    Looks through all scripts to find trainer group numbers and trainer numbers.

    This can be used with trainer_group_maximums to figure out the current number of
    trainers in each of the originating trainer groups.
    """
    total_unreferenced_trainers = 0

    # look at each possibly relevant script
    for item in script_parse_table.items():
        object = item[1]
        if isinstance(object, Script):
            check_script_has_trainer_data(object, trainer_group_maximums=trainer_group_maximums)

    # make a set of each list of trainer ids to avoid dupes
    # this will be used later in TrainerGroupTable
    for item in trainer_group_maximums.items():
        key = item[0]
        value = set(item[1])
        trainer_group_maximums[key] = value

def report_unreferenced_trainer_ids(trainer_group_maximums):
    """
    Reports on the number of unreferenced trainer ids in each group.

    This should be called after find_trainer_ids_from_scripts.

    These are trainer groups with "unused" trainer ids. The
    "find_trainer_ids_from_scripts" function analyzes each script in the game,
    and each map header in the game (because of code in TrainerFragment), and
    finds all references to trainers. But, if there are any trainers that are
    referenced in raw ASM, this method does not detect them. Each instance of a
    trainer reference is added to a global table called
    "trainer_group_maximums". Next, "find_trainer_ids_from_scripts" looks at
    the trainer IDs referenced for each group and takes the minimum number and
    the maximum number. To find whether or not there are any unused trainers,
    it takes the minimum and maximum ids and then sees which intermediate
    numbers are missing from the list of "referenced" trainer ids.
    """
    for item in trainer_group_maximums.items():
        key = item[0]
        value = item[1]

        # i'm curious: are there any missing trainer ids in this group?
        min_id = min(value)
        max_id = max(value)
        expectables = range(min_id, max_id+1)

        unreferenced = set()

        for expectable in expectables:
            if not expectable in value:
                unreferenced.add(expectable)

        if len(unreferenced) > 0:
            total_unreferenced_trainers += len(unreferenced)
            output = "trainer group "+hex(key)+" (\""+trainers.trainer_group_names[key]["name"]+"\")"
            output += " (min="+str(min_id)+", max="+str(max_id)+")"
            output += " has "+str(len(unreferenced))+" unreferenced trainer ids"
            output += ": " + str(unreferenced)
            logging.info(output)
    logging.info("total unreferenced trainers: {0}".format(total_unreferenced_trainers))

def trainer_group_report(trainer_group_maximums):
    """
    Reports how many trainer ids are used in each trainer group.
    """
    output = ""
    total = 0
    for trainer_group_id in trainer_group_maximums.keys():
        group_name = trainers.trainer_group_names[trainer_group_id]["name"]
        first_name = trainer_name_from_group(trainer_group_id).replace("\n", "")
        trainers = len(trainer_group_maximums[trainer_group_id])
        total += trainers
        output += "group "+hex(trainer_group_id)+":\n"
        output += "\tname: "+group_name+"\n"
        output += "\tfirst: "+first_name+"\n"
        output += "\ttrainer count:\t"+str(trainers)+"\n\n"
    output += "total trainers: " + str(total)
    return output

def check_script_has_trainer_data(script, trainer_group_maximums=None):
    """
    see find_trainer_ids_from_scripts
    """
    for command in script.commands:
        trainer_group = None
        trainer_id = None

        if command.id == 0x43:
            trainer_group = command.params[0].byte
            trainer_id = command.params[1].byte
        elif command.id == 0x5E:
            trainer_group = command.params[0].byte
            trainer_id = command.params[1].byte

        if trainer_group != None and trainer_id != None:
            if trainer_group in trainer_group_maximums.keys():
                trainer_group_maximums[trainer_group].add(trainer_id)
            else:
                trainer_group_maximums[trainer_group] = set([trainer_id])

def trainer_name_from_group(group_id, trainer_id=0):
    """This doesn't actually work for trainer_id > 0."""
    bank = pointers.calculate_bank(0x39999)
    ptr_address = 0x39999 + ((group_id - 1)*2)
    address = calculate_pointer_from_bytes_at(ptr_address, bank=bank)
    text = parse_text_at2(address, how_many_until(chr(0x50), address, rom))
    return text

def make_trainer_group_name_trainer_ids(trainer_group_table, debug=True):
    """
    Edits trainers.trainer_group_names and sets the trainer names.
    For instance, "AMY & MAY" becomes "AMY_AND_MAY1" and "AMY_AND_MAY2"

    This should only be used after TrainerGroupTable.parse has been called.
    """
    assert trainer_group_table != None, "TrainerGroupTable must be called before setting the trainer names"

    if debug:
        logging.info("starting to make trainer names and give ids to repeated trainer names")

    i = 1
    for header in trainer_group_table.headers:
        trainer_names = [] # (name, trainer_header)
        dupes = set()
        group_id = i
        group_name = header.group_name
        for trainer_header in header.individual_trainer_headers:
            if trainer_header.name in [x[0] for x in trainer_names]:
                dupes.add(trainer_header.name)
            trainer_names.append([trainer_header.name, trainer_header])

        # now fix trainers with duplicate names by appending an id
        if len(dupes) > 0:
            for dupe in dupes:
                culprits = [trainer_header for trainer_header in header.individual_trainer_headers if trainer_header.name == dupe]
                for (id, culprit) in enumerate(culprits):
                    culprit.seed_constant_name = culprit.name.replace("@", "") + str(id+1)
                    culprit.constant_name = culprit.make_constant_name()

        # now add the trainer names to trainers.trainer_group_names
        trainers.trainer_group_names[i]["trainer_names"] = [theader.make_constant_name() for theader in header.individual_trainer_headers]

        i += 1

    if debug:
        logging.info("done improving trainer names")


class SpriteParam(SingleByteParam):
    sprites = {
        0x1: 'SPRITE_CHRIS',
        0x2: 'SPRITE_CHRIS_BIKE',
        0x3: 'SPRITE_GAMEBOY_KID',
        0x4: 'SPRITE_SILVER',
        0x5: 'SPRITE_OAK',
        0x6: 'SPRITE_RED',
        0x7: 'SPRITE_BLUE',
        0x8: 'SPRITE_BILL',
        0x9: 'SPRITE_ELDER',
        0xa: 'SPRITE_JANINE',
        0xb: 'SPRITE_KURT',
        0xc: 'SPRITE_MOM',
        0xd: 'SPRITE_BLAINE',
        0xe: 'SPRITE_REDS_MOM',
        0xf: 'SPRITE_DAISY',
        0x10: 'SPRITE_ELM',
        0x11: 'SPRITE_WILL',
        0x12: 'SPRITE_FALKNER',
        0x13: 'SPRITE_WHITNEY',
        0x14: 'SPRITE_BUGSY',
        0x15: 'SPRITE_MORTY',
        0x16: 'SPRITE_CHUCK',
        0x17: 'SPRITE_JASMINE',
        0x18: 'SPRITE_PRYCE',
        0x19: 'SPRITE_CLAIR',
        0x1a: 'SPRITE_BROCK',
        0x1b: 'SPRITE_KAREN',
        0x1c: 'SPRITE_BRUNO',
        0x1d: 'SPRITE_MISTY',
        0x1e: 'SPRITE_LANCE',
        0x1f: 'SPRITE_SURGE',
        0x20: 'SPRITE_ERIKA',
        0x21: 'SPRITE_KOGA',
        0x22: 'SPRITE_SABRINA',
        0x23: 'SPRITE_COOLTRAINER_M',
        0x24: 'SPRITE_COOLTRAINER_F',
        0x25: 'SPRITE_BUG_CATCHER',
        0x26: 'SPRITE_TWIN',
        0x27: 'SPRITE_YOUNGSTER',
        0x28: 'SPRITE_LASS',
        0x29: 'SPRITE_TEACHER',
        0x2a: 'SPRITE_BUENA',
        0x2b: 'SPRITE_SUPER_NERD',
        0x2c: 'SPRITE_ROCKER',
        0x2d: 'SPRITE_POKEFAN_M',
        0x2e: 'SPRITE_POKEFAN_F',
        0x2f: 'SPRITE_GRAMPS',
        0x30: 'SPRITE_GRANNY',
        0x31: 'SPRITE_SWIMMER_GUY',
        0x32: 'SPRITE_SWIMMER_GIRL',
        0x33: 'SPRITE_BIG_SNORLAX',
        0x34: 'SPRITE_SURFING_PIKACHU',
        0x35: 'SPRITE_ROCKET',
        0x36: 'SPRITE_ROCKET_GIRL',
        0x37: 'SPRITE_NURSE',
        0x38: 'SPRITE_LINK_RECEPTIONIST',
        0x39: 'SPRITE_CLERK',
        0x3a: 'SPRITE_FISHER',
        0x3b: 'SPRITE_FISHING_GURU',
        0x3c: 'SPRITE_SCIENTIST',
        0x3d: 'SPRITE_KIMONO_GIRL',
        0x3e: 'SPRITE_SAGE',
        0x3f: 'SPRITE_UNUSED_GUY',
        0x40: 'SPRITE_GENTLEMAN',
        0x41: 'SPRITE_BLACK_BELT',
        0x42: 'SPRITE_RECEPTIONIST',
        0x43: 'SPRITE_OFFICER',
        0x44: 'SPRITE_CAL',
        0x45: 'SPRITE_SLOWPOKE',
        0x46: 'SPRITE_CAPTAIN',
        0x47: 'SPRITE_BIG_LAPRAS',
        0x48: 'SPRITE_GYM_GUY',
        0x49: 'SPRITE_SAILOR',
        0x4a: 'SPRITE_BIKER',
        0x4b: 'SPRITE_PHARMACIST',
        0x4c: 'SPRITE_MONSTER',
        0x4d: 'SPRITE_FAIRY',
        0x4e: 'SPRITE_BIRD',
        0x4f: 'SPRITE_DRAGON',
        0x50: 'SPRITE_BIG_ONIX',
        0x51: 'SPRITE_N64',
        0x52: 'SPRITE_SUDOWOODO',
        0x53: 'SPRITE_SURF',
        0x54: 'SPRITE_POKE_BALL',
        0x55: 'SPRITE_POKEDEX',
        0x56: 'SPRITE_PAPER',
        0x57: 'SPRITE_VIRTUAL_BOY',
        0x58: 'SPRITE_OLD_LINK_RECEPTIONIST',
        0x59: 'SPRITE_ROCK',
        0x5a: 'SPRITE_BOULDER',
        0x5b: 'SPRITE_SNES',
        0x5c: 'SPRITE_FAMICOM',
        0x5d: 'SPRITE_FRUIT_TREE',
        0x5e: 'SPRITE_GOLD_TROPHY',
        0x5f: 'SPRITE_SILVER_TROPHY',
        0x60: 'SPRITE_KRIS',
        0x61: 'SPRITE_KRIS_BIKE',
        0x62: 'SPRITE_KURT_OUTSIDE',
        0x63: 'SPRITE_SUICUNE',
        0x64: 'SPRITE_ENTEI',
        0x65: 'SPRITE_RAIKOU',
        0x66: 'SPRITE_STANDING_YOUNGSTER',
    }

    pokemon_sprites = {
        0x80: 'SPRITE_UNOWN',
        0x81: 'SPRITE_GEODUDE',
        0x82: 'SPRITE_GROWLITHE',
        0x83: 'SPRITE_WEEDLE',
        0x84: 'SPRITE_SHELLDER',
        0x85: 'SPRITE_ODDISH',
        0x86: 'SPRITE_GENGAR',
        0x87: 'SPRITE_ZUBAT',
        0x88: 'SPRITE_MAGIKARP',
        0x89: 'SPRITE_SQUIRTLE',
        0x8a: 'SPRITE_TOGEPI',
        0x8b: 'SPRITE_BUTTERFREE',
        0x8c: 'SPRITE_DIGLETT',
        0x8d: 'SPRITE_POLIWAG',
        0x8e: 'SPRITE_PIKACHU',
        0x8f: 'SPRITE_CLEFAIRY',
        0x90: 'SPRITE_CHARMANDER',
        0x91: 'SPRITE_JYNX',
        0x92: 'SPRITE_STARMIE',
        0x93: 'SPRITE_BULBASAUR',
        0x94: 'SPRITE_JIGGLYPUFF',
        0x95: 'SPRITE_GRIMER',
        0x96: 'SPRITE_EKANS',
        0x97: 'SPRITE_PARAS',
        0x98: 'SPRITE_TENTACOOL',
        0x99: 'SPRITE_TAUROS',
        0x9a: 'SPRITE_MACHOP',
        0x9b: 'SPRITE_VOLTORB',
        0x9c: 'SPRITE_LAPRAS',
        0x9d: 'SPRITE_RHYDON',
        0x9e: 'SPRITE_MOLTRES',
        0x9f: 'SPRITE_SNORLAX',
        0xa0: 'SPRITE_GYARADOS',
        0xa1: 'SPRITE_LUGIA',
        0xa2: 'SPRITE_HO_OH',
    }

    variable_sprites = {
        0xe0: 'SPRITE_DAYCARE_MON_1',
        0xe1: 'SPRITE_DAYCARE_MON_2',
        0xf0: 'SPRITE_VARS',
        0xf0: 'SPRITE_CONSOLE',
        0xf1: 'SPRITE_DOLL_1',
        0xf2: 'SPRITE_DOLL_2',
        0xf3: 'SPRITE_BIG_DOLL',
        0xf4: 'SPRITE_WEIRD_TREE',
        0xf5: 'SPRITE_OLIVINE_RIVAL',
        0xf6: 'SPRITE_AZALEA_ROCKET',
        0xf7: 'SPRITE_FUSCHIA_GYM_1',
        0xf8: 'SPRITE_FUSCHIA_GYM_2',
        0xf9: 'SPRITE_FUSCHIA_GYM_3',
        0xfa: 'SPRITE_FUSCHIA_GYM_4',
        0xfb: 'SPRITE_COPYCAT',
        0xfc: 'SPRITE_JANINE_IMPERSONATOR',
    }

    def to_asm(self):
        if self.byte in self.sprites.keys():
            return self.sprites[self.byte]
        if self.byte in self.pokemon_sprites.keys():
            return self.sprites[self.byte]
        if self.byte in self.variable_sprites.keys():
            return self.sprites[self.byte]
        return SingleByteParam.to_asm(self)



class PeopleEvent(Command):
    size = people_event_byte_size
    macro_name = "person_event"
    base_label = "PeopleEvent_"
    override_byte_check = True
    param_types = {
        0: {"name": "sprite", "class": SpriteParam},
        1: {"name": "y from top+4", "class": DecimalParam},
        2: {"name": "x from top+4", "class": DecimalParam},
        3: {"name": "facing", "class": HexByte},
        4: {"name": "movement", "class": HexByte},
        5: {"name": "clock_hour", "class": DecimalParam},
        6: {"name": "clock_daytime", "class": DecimalParam},
        7: {"name": "color_function", "class": HexByte},
        8: {"name": "sight_range", "class": DecimalParam},
        9: {"name": "pointer", "class": PointerLabelParam}, # or ScriptPointerLabelParam or ItemLabelParam
        10: {"name": "event flag", "class": EventFlagParam},
    }

    def xto_asm(self):
        output = "\n; person-event\n; picture, y, x, facing, movement, clock_hour, clock_daytime, color_function, sight_range\n"
        output += "db $%.2x, %d, %d, $%.2x, $%.2x, %d, %d, $%.2x, %d\n" % (self.params[0].byte, self.params[1].byte, self.params[2].byte, self.params[3].byte, self.params[4].byte, self.params[5].byte, self.params[6].byte, self.params[7].byte, self.params[8].byte)
        output += "; pointer\ndw %s\n" % (self.params[9].to_asm())
        output += "; event flag\ndw %s" % (self.params[10].to_asm())
        return output

    def __init__(self, address, id, bank=None, map_group=None, map_id=None, debug=False, label=None, force=False):
        if not is_valid_address(address):
            raise exceptions.AddressException(
                "PeopleEvent must be given a valid address (but got {0})."
                .format(hex(address))
            )
        self.address = address
        self.last_address = address + people_event_byte_size
        self.id = id
        self.bank = bank
        if not label:
            label = self.base_label + hex(address)
        self.label = Label(name=label, address=address, object=self)
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.force = force
        self.params = {}
        self.dependencies = None
        # PeopleEvent should probably not be in the global script_parse_table
        #script_parse_table[self.address : self.last_address] = self
        self.parse()

    def parse(self):
        address = self.address
        bank = self.bank

        color_function_byte = None
        lower_bits = None
        higher_bits = None
        is_regular_script = None
        is_give_item = None
        is_trainer = None

        self.params = {}
        current_address = self.address
        i = 0
        self.size = 1
        color_function_byte = None
        for (key, param_type) in self.param_types.items():
            if i == 9:
                if is_give_item:
                    name = "item_fragment_pointer"
                    klass = ItemFragmentParam
                elif is_regular_script:
                    name = "script_pointer"
                    klass = ScriptPointerLabelParam
                elif is_trainer:
                    name = "trainer"
                    #klass = MultiByteParam
                    klass = TrainerFragmentParam
                else:
                    name = "unknown"
                    klass = MultiByteParam
            else:
                name = param_type["name"]
                klass = param_type["class"]
            obj = klass(address=current_address, name=name, debug=self.debug, force=self.force, map_group=self.map_group, map_id=self.map_id, bank=self.bank)
            self.params[i] = obj
            if i == 7:
                color_function_byte = ord(rom[current_address])
                lower_bits = color_function_byte & 0xF
                higher_bits = color_function_byte >> 4
                is_regular_script = lower_bits == 00
                is_give_item = lower_bits == 0o1
                is_trainer = lower_bits == 0o2
            current_address += obj.size
            self.size += obj.size
            i += 1
        self.last_address = current_address
        self.is_trainer = is_trainer
        self.is_give_item = is_give_item
        self.is_regular_script = is_regular_script
        self.y = self.params[1].byte
        self.x = self.params[2].byte
        self.facing = self.params[3].byte
        self.movement = self.params[4].byte
        self.clock_hour = self.params[5].byte
        self.clock_daytime = self.params[6].byte
        self.color_function = self.params[7].byte
        self.sight_range = self.params[8].byte
        self.pointer = self.params[9].bytes
        self.event_flag = self.params[10].bytes
        return True


all_people_events = []
def parse_people_events(address, people_event_count, bank=None, map_group=None, map_id=None, debug=False, force=False):
    # people_event_byte_size
    people_events = []
    current_address = address
    id = 0
    for each in range(people_event_count):
        pevent = PeopleEvent(address=current_address, id=id, bank=bank, map_group=map_group, map_id=map_id, debug=debug, force=force)
        current_address += people_event_byte_size
        people_events.append(pevent)
        id += 1
    all_people_events.extend(people_events)
    return people_events

def old_parse_people_event_bytes(some_bytes, address=None, map_group=None, map_id=None, debug=True):
    """parse some number of people-events from the data
    see http://hax.iimarck.us/files/scriptingcodes_eng.htm#Scripthdr

    For example, map 1.1 (group 1 map 1) has four person-events.

        37 05 07 06 00 FF FF 00 00 02 40 FF FF
        3B 08 0C 05 01 FF FF 00 00 05 40 FF FF
        3A 07 06 06 00 FF FF A0 00 08 40 FF FF
        29 05 0B 06 00 FF FF 00 00 0B 40 FF FF
    """
    assert len(some_bytes) % people_event_byte_size == 0, "wrong number of bytes"

    # address is not actually required for this function to work...
    bank = None
    if address:
        bank = pointers.calculate_bank(address)

    people_events = []
    for bytes in helpers.grouper(some_bytes, count=people_event_byte_size):
        pict = int(bytes[0], 16)
        y = int(bytes[1], 16)    # y from top + 4
        x = int(bytes[2], 16)    # x from left + 4
        face = int(bytes[3], 16) # 0-4 for regular, 6-9 for static facing
        move = int(bytes[4], 16)
        clock_time_byte1 = int(bytes[5], 16)
        clock_time_byte2 = int(bytes[6], 16)
        color_function_byte = int(bytes[7], 16) # Color|Function
        trainer_sight_range = int(bytes[8], 16)

        lower_bits = color_function_byte & 0xF
        #lower_bits_high = lower_bits >> 2
        #lower_bits_low = lower_bits & 3
        higher_bits = color_function_byte >> 4
        #higher_bits_high = higher_bits >> 2
        #higher_bits_low = higher_bits & 3

        is_regular_script = lower_bits == 00
        # pointer points to script
        is_give_item = lower_bits == 0o1
        # pointer points to [Item no.][Amount]
        is_trainer = lower_bits == 0o2
        # pointer points to trainer header

        # goldmap called these next two bytes "text_block" and "text_bank"?
        script_pointer_byte1 = int(bytes[9], 16)
        script_pointer_byte2 = int(bytes[10], 16)
        script_pointer = script_pointer_byte1 + (script_pointer_byte2 << 8)
        # calculate the full address by assuming it's in the current bank
        # but what if it's not in the same bank?
        extra_portion = {}
        if bank:
            ptr_address = pointers.calculate_pointer(script_pointer, bank)
            if is_regular_script:
                logging.debug(
                    "parsing a person-script at x={x} y={y} address={address}"
                    .format(
                        x=(x-4),
                        y=(y-4),
                        address=hex(ptr_address),
                    )
                )
                script = parse_script_engine_script_at(ptr_address, map_group=map_group, map_id=map_id)
                extra_portion = {
                    "script_address": ptr_address,
                    "script": script,
                    "event_type": "script",
                }
            if is_give_item:
                logging.debug("not parsing give item event.. [item id][quantity]")
                extra_portion = {
                    "event_type": "give_item",
                    "give_item_data_address": ptr_address,
                    "item_id": ord(rom[ptr_address]),
                    "item_qty": ord(rom[ptr_address+1]),
                }
            if is_trainer:
                logging.debug(
                    "parsing a trainer (person-event) at x={x} y={y}"
                    .format(x=x, y=y)
                )
                parsed_trainer = parse_trainer_header_at(ptr_address, map_group=map_group, map_id=map_id)
                extra_portion = {
                    "event_type": "trainer",
                    "trainer_data_address": ptr_address,
                    "trainer_data": parsed_trainer,
                }

        # XXX not sure what's going on here
        # event flag (hidden if set)
        # note: FFFF for none
        when_byte = int(bytes[11], 16)
        hide = int(bytes[12], 16)

        event_flag_byte2 = int(bytes[11], 16)
        event_flag_byte1 = int(bytes[12], 16)
        event_flag = event_flag_byte1 + (event_flag_byte2 << 8)

        people_event = {
            "pict": pict,
            "y": y,                      # y from top + 4
            "x": x,                      # x from left + 4
            "face": face,                # 0-4 for regular, 6-9 for static facing
            "move": move,
            "clock_time": {"1": clock_time_byte1,
                           "2": clock_time_byte2},       # clock/time setting byte 1
            "color_function_byte": color_function_byte,  # Color|Function
            "trainer_sight_range": trainer_sight_range,  # trainer range of sight
            "script_pointer": {"1": script_pointer_byte1,
                               "2": script_pointer_byte2},

            #"text_block": text_block,   # script pointer byte 1
            #"text_bank": text_bank,     # script pointer byte 2
            "when_byte": when_byte,      # event flag (hidden if set)
            "hide": hide,                # note: FFFF for none

            "is_trainer": is_trainer,
            "is_regular_script": is_regular_script,
            "is_give_item": is_give_item,
        }
        people_event.update(extra_portion)
        people_events.append(people_event)
    return people_events


class SignpostRemoteBase(object):
    def __init__(self, address, bank=None, map_group=None, map_id=None, signpost=None, debug=False, label=None):
        self.address = address
        self.last_address = address + self.size
        script_parse_table[self.address : self.last_address] = self
        self.bank = bank
        self.map_group = map_group
        self.map_id = map_id
        self.signpost = signpost
        self.debug = debug
        self.params = []
        if not label:
            label = self.base_label + hex(address)
        self.label = Label(name=label, address=address, object=self)
        self.dependencies = None
        self.parse()

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        dependencies = []
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        for p in self.params:
            deps = p.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
            dependencies.extend(deps)
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        """very similar to Command.to_asm"""
        if len(self.params) == 0:
            return ""
        #output = ", ".join([p.to_asm() for p in self.params])
        output = ""
        for param in self.params:
            if issubclass(param.__class__, SingleByteParam):
                output += "db "
            else:
                output += "dw "
            output += param.to_asm() + "\n"
        return output


class SignpostRemoteScriptChunk(SignpostRemoteBase):
    """
    a signpost might point to [Event flag (2byte)][2byte pointer to script]
    """
    base_label = "SignpostRemoteScript_"
    size = 4

    def parse(self):
        address = self.address
        bank = self.bank

        #event_flag_byte1 = ord(rom[address])
        #event_flag_byte2 = ord(rom[address+1])
        event_flag = MultiByteParam(address=address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.params.append(event_flag)

        #script_address = calculate_pointer_from_bytes_at(address+2, bank=bank)
        #script = parse_script_engine_script_at(script_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        script_param = ScriptPointerLabelParam(address=address+2, map_group=self.map_group, map_id=self.map_id, debug=self.debug, force=False)
        self.params.append(script_param)
        self.script = script_param.script
        self.signpost.remote_script = self.script

        #self.event_flag_bytes = [event_flag_byte1, event_flag_byte2]
        #self.script_address = script_address
        #self.script = script


class SignpostRemoteItemChunk(SignpostRemoteBase):
    """
    a signpost might point to [Event flag (2byte)][Item no.]
    """
    base_label = "SignpostRemoteItem_"
    size = 3

    def parse(self):
        address = self.address
        bank = self.bank

        event_flag = MultiByteParam(address=address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.params.append(event_flag)

        item = ItemLabelByte(address=address+2)
        self.params.append(item)
        self.item = item


class SignpostRemoteUnknownChunk(SignpostRemoteBase):
    """
    a signpost might point to [Event flag (2byte)][??]
    """
    base_label = "SignpostRemoteUnknown_"
    size = 3

    def parse(self):
        address = self.address
        bank = self.bank

        event_flag = MultiByteParam(address=address, bank=self.bank, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.params.append(event_flag)

        byte = SingleByteParam(address=address+2)
        self.params.append(byte)


# this could potentially extend Command
# see how class Warp does this
class Signpost(Command):
    """parse some number of signposts from the data

    [Y position][X position][Function][Script pointer (2byte)]

    functions:
        00      Sign can be read from all directions
                script pointer to: script
        01      Sign can only be read from below
                script pointer to: script
        02      Sign can only be read from above
                script pointer to: script
        03      Sign can only be read from right
                script pointer to: script
        04      Sign can only be read from left
                script pointer to: script
        05      If event flag is set then pointer is interpreted
                script pointer to: [event flag (2byte)][2byte pointer to script]
        06      If event flag is not set then pointer is interpreted
                script pointer to: [event flag (2byte)][2byte pointer to script]
        07      If event flag is set then item is given
                script pointer to: [event flag (2byte)][Item no.]
        08      No Action
                script pointer to: [event flag (2byte)][??]
    """
    size = 5
    macro_name = "signpost"
    override_byte_check = True

    # preprocessor uses this
    param_types = {
        0: {"name": "y", "class": DecimalParam},
        1: {"name": "x", "class": DecimalParam},
        2: {"name": "function", "class": HexByte},
        3: {"name": "pointer", "class": PointerLabelParam},
    }

    def __init__(self, address, id, bank=None, map_group=None, map_id=None, debug=True, label=None):
        self.address = address
        self.id = id
        if label == None:
            label = "UnknownSignpost_"+str(map_group)+"Map"+str(map_id)+"_"+hex(address)
        self.label = Label(name=label, address=address, object=self)
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.bank = bank
        self.last_address = self.address + self.size
        self.y, self.x, self.func = None, None, None
        # Signpost should probably not be in the globals
        #script_parse_table[self.address : self.last_address] = self
        self.remotes = []
        self.params = []
        self.dependencies = None
        self.parse()

    def parse(self):
        """parse just one signpost"""
        address = self.address
        bank = self.bank
        self.last_address = self.address + self.size
        bytes = rom.interval(self.address, self.size) #, signpost_byte_size)

        self.y = int(bytes[0], 16)
        self.x = int(bytes[1], 16)
        self.func = int(bytes[2], 16)
        y, x, func = self.y, self.x, self.func

        # y
        self.params.append(DecimalParam(address=address, bank=self.bank, map_group=self.map_group, map_id=self.map_id, debug=self.debug))
        # x
        self.params.append(DecimalParam(address=address+1, bank=self.bank, map_group=self.map_group, map_id=self.map_id, debug=self.debug))
        # func
        self.params.append(HexByte(address=address+2, bank=self.bank, map_group=self.map_group, map_id=self.map_id, debug=self.debug))

        output = "******* parsing signpost "+str(self.id)+" at: "
        output += "x="+str(x)+" y="+str(y)+" on map_group="
        output += str(self.map_group)+" map_id="+str(self.map_id)

        if func in [0, 1, 2, 3, 4]:
            # signpost's script pointer points to a script
            script_ptr_byte1 = int(bytes[3], 16)
            script_ptr_byte2 = int(bytes[4], 16)
            script_pointer = script_ptr_byte1 + (script_ptr_byte2 << 8)

            script_address = pointers.calculate_pointer(script_pointer, bank)
            output += " script@"+hex(script_address)
            logging.debug(output)

            param = ScriptPointerLabelParam(address=self.address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug, force=False)
            self.params.append(param)
            param = script_parse_table[param.parsed_address]
            param.label = Label(address=param.address, object=param, name="Map"+map_names[self.map_group][self.map_id]["label"]+"Signpost"+str(self.id)+"Script")

            #self.script_address = script_address
            #self.script = script
        elif func in [5, 6]:
            # signpost's script pointer points to [event flag (2byte)][2byte pointer to script]
            ptr_byte1 = int(bytes[3], 16)
            ptr_byte2 = int(bytes[4], 16)
            pointer = ptr_byte1 + (ptr_byte2 << 8)
            address = pointers.calculate_pointer(pointer, bank)

            event_flag_byte1 = ord(rom[address])
            event_flag_byte2 = ord(rom[address+1])
            script_ptr_byte1 = ord(rom[address+2])
            script_ptr_byte2 = ord(rom[address+3])
            script_address = calculate_pointer_from_bytes_at(address+2, bank=bank)

            output += " remote_chunk@"+hex(address)+" remote_script@"+hex(script_address)
            logging.debug(output)

            r1 = SignpostRemoteScriptChunk(address, signpost=self, \
                   bank=self.bank, map_group=self.map_group, map_id=self.map_id, \
                   debug=self.debug)
            self.remotes.append(r1)

            # give a better label to the SignpostRemoteScriptChunk
            r1.label = Label(address=r1.address, object=r1, name="Map"+map_names[self.map_group][self.map_id]["label"]+"SignpostPtr"+str(self.id))

            mb = PointerLabelParam(address=self.address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
            self.params.append(mb)

            # update the remote script address
            param = script_parse_table[script_address]
            param.label = Label(address=param.address, object=param, name="Map"+map_names[self.map_group][self.map_id]["label"]+"Signpost"+str(self.id)+"Script")

        elif func == 7:
            # signpost's script pointer points to [event flag (2byte)][Item no.]
            ptr_byte1 = int(bytes[3], 16)
            ptr_byte2 = int(bytes[4], 16)
            pointer = ptr_byte1 + (ptr_byte2 << 8)
            address = pointers.calculate_pointer(pointer, bank)

            item_id         = ord(rom[address+2])
            output += " item_id="+str(item_id)
            logging.debug(output)

            r1 = SignpostRemoteItemChunk(address, signpost=self, \
                   bank=self.bank, map_group=self.map_group, map_id=self.map_id, \
                   debug=self.debug)
            self.remotes.append(r1)
            r1.label = Label(address=r1.address, object=r1, name="Map"+map_names[self.map_group][self.map_id]["label"]+"SignpostItem"+str(self.id))

            mb = PointerLabelParam(address=self.address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
            self.params.append(mb)

            #event_flag_byte1 = ord(rom[address])
            #event_flag_byte2 = ord(rom[address+1])
            #self.event_flag_bytes = [event_flag_byte1, event_flag_byte2]
            #self.item_id = item_id
        elif func == 8:
            # signpost's script pointer points to [event flag (2byte)][??]
            ptr_byte1 = int(bytes[3], 16)
            ptr_byte2 = int(bytes[4], 16)
            pointer = ptr_byte1 + (ptr_byte2 << 8)
            address = pointers.calculate_pointer(pointer, bank)

            output += " remote unknown chunk at="+hex(address)
            logging.debug(output)

            r1 = SignpostRemoteUnknownChunk(address, signpost=self, \
                   bank=self.bank, map_group=self.map_group, map_id=self.map_id, \
                   debug=self.debug)
            self.remotes.append(r1)

            mb = PointerLabelParam(address=self.address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
            self.params.append(mb)
        else:
            raise Exception("unknown signpost type byte="+hex(func) + " signpost@"+hex(self.address))

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        dependencies = []
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        for p in self.params:
            dependencies.extend(p.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        output = self.macro_name + " "
        if self.params == []:
            raise Exception("signpost has no params?")
        output += ", ".join([p.to_asm() for p in self.params])
        return output

all_signposts = []
def parse_signposts(address, signpost_count, bank=None, map_group=None, map_id=None, debug=True):
    if bank == None:
        raise Exception("signposts need to know their bank")
    signposts = []
    current_address = address
    id = 0
    for each in range(signpost_count):
        signpost = Signpost(current_address, id, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        current_address += signpost_byte_size # i think ??
        signposts.append(signpost)
        id += 1
    all_signposts.extend(signposts)
    return signposts


class LandmarkParam(SingleByteParam):
    landmarks = [
        'SPECIAL_MAP',

        # johto
        'NEW_BARK_TOWN',
        'ROUTE_29',
        'CHERRYGROVE_CITY',
        'ROUTE_30',
        'ROUTE_31',
        'VIOLET_CITY',
        'SPROUT_TOWER',
        'ROUTE_32',
        'RUINS_OF_ALPH',
        'UNION_CAVE',
        'ROUTE_33',
        'AZALEA_TOWN',
        'SLOWPOKE_WELL',
        'ILEX_FOREST',
        'ROUTE_34',
        'GOLDENROD_CITY',
        'RADIO_TOWER',
        'ROUTE_35',
        'NATIONAL_PARK',
        'ROUTE_36',
        'ROUTE_37',
        'ECRUTEAK_CITY',
        'TIN_TOWER',
        'BURNED_TOWER',
        'ROUTE_38',
        'ROUTE_39',
        'OLIVINE_CITY',
        'LIGHTHOUSE',
        'BATTLE_TOWER',
        'ROUTE_40',
        'WHIRL_ISLANDS',
        'ROUTE_41',
        'CIANWOOD_CITY',
        'ROUTE_42',
        'MT_MORTAR',
        'MAHOGANY_TOWN',
        'ROUTE_43',
        'LAKE_OF_RAGE',
        'ROUTE_44',
        'ICE_PATH',
        'BLACKTHORN_CITY',
        'DRAGONS_DEN',
        'ROUTE_45',
        'DARK_CAVE',
        'ROUTE_46',
        'SILVER_CAVE',

        # kanto
        'PALLET_TOWN',
        'ROUTE_1',
        'VIRIDIAN_CITY',
        'ROUTE_2',
        'PEWTER_CITY',
        'ROUTE_3',
        'MT_MOON',
        'ROUTE_4',
        'CERULEAN_CITY',
        'ROUTE_24',
        'ROUTE_25',
        'ROUTE_5',
        'UNDERGROUND',
        'ROUTE_6',
        'VERMILION_CITY',
        'DIGLETTS_CAVE',
        'ROUTE_7',
        'ROUTE_8',
        'ROUTE_9',
        'ROCK_TUNNEL',
        'ROUTE_10',
        'POWER_PLANT',
        'LAVENDER_TOWN',
        'LAV_RADIO_TOWER',
        'CELADON_CITY',
        'SAFFRON_CITY',
        'ROUTE_11',
        'ROUTE_12',
        'ROUTE_13',
        'ROUTE_14',
        'ROUTE_15',
        'ROUTE_16',
        'ROUTE_17',
        'ROUTE_18',
        'FUCHSIA_CITY',
        'ROUTE_19',
        'ROUTE_20',
        'SEAFOAM_ISLANDS',
        'CINNABAR_ISLAND',
        'ROUTE_21',
        'ROUTE_22',
        'VICTORY_ROAD',
        'ROUTE_23',
        'INDIGO_PLATEAU',
        'ROUTE_26',
        'ROUTE_27',
        'TOHJO_FALLS',
        'ROUTE_28',
        'FAST_SHIP',
    ]

    def to_asm(self):
        if self.byte < len(self.landmarks):
            return self.landmarks[self.byte]
        return SingleByteParam.to_asm(self)


class SongParam(SingleByteParam):
    def to_asm(self):
        if self.byte < len(song_names):
            return 'MUSIC_' + song_names[self.byte].upper().replace(' ','_')
        return SingleByteParam.to_asm(self)


class TimeOfDayParam(DecimalParam):
    times = ['MORN', 'DAY', 'NITE', 'DARKNESS']
    def to_asm(self):
        if self.byte < len(self.times):
            return self.times[self.byte]
        return DecimalParam.to_asm(self)


class MapHeader(object):
    base_label = "MapHeader_"

    def __init__(self, address, map_group=None, map_id=None, debug=True, label=None, bank=0x25):
        logging.debug(
            "creating a MapHeader at {address} map_group={map_group} map_id={map_id}"
            .format(address=hex(address), map_group=map_group, map_id=map_id)
        )
        self.address = address
        self.map_group = map_group
        self.map_id = map_id
        self.bank = bank
        self.debug = debug
        self.dependencies = None
        label = self.make_label()
        self.label = Label(name=label, address=address, object=self)
        self.last_address = address + 9
        script_parse_table[address : self.last_address] = self
        self.parse()

    def make_label(self):
        return map_names[self.map_group][self.map_id]["label"] + "_MapHeader"

    def parse(self):
        address = self.address
        logging.debug("parsing a MapHeader at {0}".format(hex(address)))
        self.bank = HexByte(address=address)
        self.tileset = HexByte(address=address+1)
        self.permission = DecimalParam(address=address+2)
        self.second_map_header_address = pointers.calculate_pointer(ord(rom[address+3])+(ord(rom[address+4])<<8), self.bank.byte)
        # TODO: is the bank really supposed to be 0x25 all the time ??
        self.second_map_header = SecondMapHeader(self.second_map_header_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        all_second_map_headers.append(self.second_map_header)
        self.location_on_world_map = LandmarkParam(address=address+5)
        self.music = SongParam(address=address+6)
        self.time_of_day = TimeOfDayParam(address=address+7)
        self.fishing_group = DecimalParam(address=address+8)

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        dependencies = [self.second_map_header]
        global_dependencies.add(self.second_map_header)
        dependencies.append(self.second_map_header.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        output  = "; bank, tileset, permission\n"
        output += "db " + ", ".join(["BANK(" + self.second_map_header.label.name + ")", self.tileset.to_asm(), self.permission.to_asm()])
        output += "\n\n; second map header\n"
        output += "dw " + PointerLabelParam(address=self.address+3).to_asm() # TODO: should we include bank=self.bank.byte ??
        output += "\n\n; location on world map, music, time of day, fishing group\n"
        output += "db " + ", ".join([self.location_on_world_map.to_asm(), self.music.to_asm(), self.time_of_day.to_asm(), self.fishing_group.to_asm()])
        return output

def parse_map_header_at(address, map_group=None, map_id=None, all_map_headers=None, debug=True):
    """parses an arbitrary map header at some address"""
    logging.debug("parsing a map header at {0}".format(hex(address)))
    map_header = MapHeader(address, map_group=map_group, map_id=map_id, debug=debug)
    all_map_headers.append(map_header)
    return map_header

def get_direction(connection_byte, connection_id):
    """
    Given a connection byte and a connection id, which direction is this
    connection?

    example:
      The 0th connection of $5 is SOUTH and the 1st connection is
      EAST.
    """
    connection_options = [0b1000, 0b0100, 0b0010, 0b0001]
    results = ["NORTH", "SOUTH", "WEST", "EAST"]

    for option in connection_options:
        if (option & connection_byte) == 0:
            results[connection_options.index(option)] = ""

    # prune results
    while "" in results:
        results.remove("")

    return results[connection_id]

class SecondMapHeader(object):
    base_label = "SecondMapHeader_"

    def __init__(self, address, map_group=None, map_id=None, debug=True, bank=None, label=None):
        logging.debug("creating a SecondMapHeader at {0}".format(hex(address)))
        self.address = address
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.bank = bank
        self.dependencies = None
        label = self.make_label()
        self.label = Label(name=label, address=address, object=self)

        # the minimum number of bytes is 12
        self.last_address = address+12
        self.size = 12

        script_parse_table[address : self.last_address] = self
        self.parse()

    def make_label(self):
        return map_names[self.map_group][self.map_id]["label"] + "_SecondMapHeader"

    def parse(self):
        address = self.address
        bytes = rom.interval(address, second_map_header_byte_size, strings=False)
        size = second_map_header_byte_size

        # for later
        self.connections = []

        self.border_block = HexByte(address=address)
        self.height = DecimalParam(address=address+1)
        self.width  = DecimalParam(address=address+2)

        # bank appears first
        ###self.blockdata_address = PointerLabelBeforeBank(address+3)
        self.blockdata_address = calculate_pointer_from_bytes_at(address+3, bank=True)
        xyz = script_parse_table[self.blockdata_address]
        if xyz == None:
            self.blockdata = MapBlockData(self.blockdata_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug, width=self.width, height=self.height)
        else:
            self.blockdata = xyz

        # bank appears first
        ###self.script_address = PointerLabelBeforeBank(address+6)
        self.script_header_address = calculate_pointer_from_bytes_at(address+6, bank=True)
        self.script_header = MapScriptHeader(self.script_header_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        all_map_script_headers.append(self.script_header)

        self.event_bank = ord(rom[address+6])
        self.event_header_address = calculate_pointer_from_bytes_at(address+9, bank=ord(rom[address+6]))
        self.event_header = MapEventHeader(self.event_header_address, map_group=self.map_group, map_id=self.map_id, debug=self.debug)
        self.connection_byte = DecimalParam(address=address+11)
        all_map_event_headers.append(self.event_header)

        self.size = size

        if self.connection_byte == 0:
            return True

        current_address = address+12

        # short alias
        cb    = self.connection_byte.byte

        # east = 1, west = 2, south = 4, north = 8 (or'd together)
        east  = ((cb & 0x1) != 0)
        west  = ((cb & 0x2) != 0)
        south = ((cb & 0x4) != 0)
        north = ((cb & 0x8) != 0)
        directions = [east, west, south, north]
        connection_count = directions.count(True)

        for connection in range(0, connection_count):
            direction = get_direction(self.connection_byte.byte, connection)
            connection = Connection(current_address, direction=direction, map_group=self.map_group, map_id=self.map_id, debug=self.debug, smh=self)
            self.connections.append(connection)

            # 12 bytes each?
            current_address += connection.size

        self.last_address = current_address

        return True

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        dependencies = [self.script_header, self.event_header, self.blockdata]
        global_dependencies.update(dependencies)
        dependencies.append(self.script_header.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        dependencies.append(self.event_header.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        self_constant_label = get_map_constant_label(map_group=self.map_group, map_id=self.map_id, map_internal_ids=self.map_internal_ids)
        output = "; border block\n"
        output += "db " + self.border_block.to_asm() + "\n\n"
        output += "; height, width\n"
        output += "db " + self_constant_label + "_HEIGHT, " + self_constant_label + "_WIDTH\n\n"
        output += "; blockdata (bank-then-pointer)\n"
        thing = ScriptPointerLabelBeforeBank(address=self.address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug).to_asm()
        output += "dbw " + thing.split(", ")[0] + ", "+thing.split(", ")[1] + "\n\n"
        output += "; script header (bank-then-pointer)\n"
        thing = ScriptPointerLabelBeforeBank(address=self.address+6, map_group=self.map_group, map_id=self.map_id, debug=self.debug).to_asm()
        output += "dbw " + thing.split(", ")[0] + ", " + thing.split(", ")[1] + "\n\n"
        output += "; map event header (bank-then-pointer)\n"
        output += "dw " + PointerLabelParam(address=self.address+9, bank=self.event_bank, map_group=self.map_group, map_id=self.map_id, debug=self.debug).to_asm() + "\n\n"

        output += "; connections\n"
        dir_results = []
        connection_options = [0b1000, 0b0100, 0b0010, 0b0001]
        dirs = ["NORTH", "SOUTH", "WEST", "EAST"]
        for (id, each) in enumerate(dirs):
            if ((connection_options[id] & self.connection_byte.byte) != 0):
                dir_results.append(each)
        output += "db " + " | ".join(dir_results)
        if len(dir_results) == 0:
            output += "0"

        if self.connection_byte.byte == 0 or len(dir_results) == 0:
            return output
        else:
            output += "\n\n"

        connections = "\n\n".join([connection.to_asm() for connection in self.connections])
        output += connections

        return output

strip_pointer_data = []
strip_destination_data = []
connections = []
wrong_norths = []
wrong_easts = []
wrong_souths = []
wrong_wests = []

class Connection(object):
    size = 12

    def __init__(self, address, direction=None, map_group=None, map_id=None, debug=True, smh=None):
        self.address = address
        self.direction = direction.lower()
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.smh = smh
        self.last_address = address + self.size
        connections.append(self)

        self.parse()

    def parse(self):
        current_address = self.address

        is_vertical   = ((self.direction == "north") or (self.direction == "south"))
        is_horizontal = ((self.direction == "east") or (self.direction == "west"))

        connected_map_group_id = ord(rom[current_address])
        self.connected_map_group_id = connected_map_group_id
        current_address += 1

        connected_map_id = ord(rom[current_address])
        self.connected_map_id = connected_map_id
        current_address += 1

        # window (use JohtoMap's calculation, not this)
        #   up: C701h + height_of_connected_map * (width_of_connected_map + 6)
        #   left: C706h + 2 * width_of_connected_map
        #   down/right: C707h + width_of_connected_map
        #
        # 2 bytes (flipped) - X position of starting point for intermediate
        # tiles (scrolls through connected map line-by-line. this way you can
        # change Y position also)
        #
        # According to JohtoMap, the calculation for tile data pointer is:
        #   int p = otherMap.tileDataLocation;
        #   int h = (otherMap.width - otherMap.height)
        #   if (h > 0)
        #       p += (h * otherMap.height) + (otherMap.height * 3) + (otherMap.height + 3)
        #   else
        #       p += (otherMap.height * otherMap.width) - (otherMap.width * 3);
        #   c.tileDataPointer = gb.Get2BytePointer(p);
        #
        # tauwasser calls this "connection strip pointer"
        tile_data_pointer = ord(rom[current_address]) + (ord(rom[current_address+1]) << 8)
        strip_pointer = tile_data_pointer
        self.strip_pointer = tile_data_pointer
        current_address += 2

        # 10:19 <comet> memoryotherpointer is <comet> what johtomap calls OMDL (in ram where the tiles start getting pulled from the other map)
        memory_other_pointer = ord(rom[current_address]) + (ord(rom[current_address+1]) << 8)
        # 10:42 <comet> it would be a good idea to rename otherpointer strippointer or striploc
        # 10:42 <comet> since thats more accurate
        # 11:05 <comet> Above: C803h + xoffset
        # 11:05 <comet> Below: C803h + (m.height + 3) * (m.width + 6) + xoffset
        # 11:05 <comet> Left: C800h + (m.width + 6) * (yoffset + 3)
        # 11:05 <comet> Right: C7FDh + (m.width + 6) * (yoffset + 4)
        #
        # tauwasser calls this "connection strip destination" and lin calls this "memoryOtherPointer"
        #   Points to the upper left block of the connection strip
        #   (The bank the Blockdata is in, is loaded out of the Mapheader of the connected Map.)
        #   The connection strip is always 3 Blocks high resp. wide
        #   (depending on the connection's direction)
        strip_destination = memory_other_pointer
        self.strip_destination = memory_other_pointer
        current_address += 2

        # length of the connection strip in blocks
        connection_strip_length = ord(rom[current_address])
        current_address += 1
        connected_map_width     = ord(rom[current_address])
        current_address += 1

        self.connection_strip_length = connection_strip_length
        self.connected_map_width     = connected_map_width

        y_position_after_map_change = ord(rom[current_address])
        yoffset = y_position_after_map_change
        current_address += 1

        x_position_after_map_change = ord(rom[current_address])
        xoffset = x_position_after_map_change
        current_address += 1

        # in pokered these were called alignments? same thing?
        self.yoffset = y_position_after_map_change
        self.xoffset = x_position_after_map_change

        # tauwasser calls this "window" and lin calls this "memoryCurrentPointer"
        # Position of the upper left block after entering the Map
        #
        # tauwasser's formula for windows:
        #   Above: C701h + Height_of_connected_map * (Width_of_connected_map + 6)
        #   Left: C706h + 2 * Width_of_connected_map
        #   Below/Right: C707h + Width_of_connected_map
        window = ord(rom[current_address]) + (ord(rom[current_address+1]) << 8)
        current_address += 2

        self.window = window

        current_map_height = self.smh.height.byte
        current_map_width  = self.smh.width.byte

        if "header_new" in map_names[connected_map_group_id][connected_map_id].keys():
            # the below code ensures that there's an equation to handle strip_pointer

            ldirection = self.direction.lower()
            connected_map_header        = map_names[connected_map_group_id][connected_map_id]["header_new"]
            connected_second_map_header = connected_map_header.second_map_header
            connected_map_height        = connected_second_map_header.height.byte
            connected_map_width         = connected_second_map_header.width.byte
            p = connected_second_map_header.blockdata.address
            h = None
            method = "default"

            if ldirection == "north":
                h = connected_map_width - self.smh.width.byte
                if ((p + ((connected_map_height * connected_map_width) - (connected_map_width * 3)))%0x4000)+0x4000 == strip_pointer:
                    # lin's equation:
                    #   p += (otherMap.height * otherMap.width) - (otherMap.width * 3)
                    p += (connected_map_height * connected_map_width) - (connected_map_width * 3)
                    method = "north1"
                elif ((p + connected_map_width + xoffset + (16 * connected_map_height) - 16)%0x4000)+0x4000 == strip_pointer:
                    p += connected_map_width + xoffset + (16 * connected_map_height) - 16
                    method = "north2"
                elif p != strip_pointer:
                    # worst case scenario: we don't know how to calculate p, so we'll just set it as a constant
                    # example: Route10North north to Route9 (strip_pointer=0x7eae, connected map's blockdata=0x7de9)
                    p = strip_pointer
                    method = "north3"
                else:
                    # this doesn't seem to ever happen
                    # or just do nothing (value is already ok)
                    method = "north4"
            elif ldirection == "west":
                h = connected_map_height - self.smh.height.byte
                if ((p + (h * connected_map_width) - (connected_map_width * 3) + (connected_map_width - 1) - 2)%0x4000)+0x4000 == strip_pointer:
                    # lin's method:
                    #   p += (h * otherMap.width) - (otherMap.width * 3) + (otherMap.width - 3)
                    p += (h * connected_map_width) - (connected_map_width * 3) + (connected_map_width - 1) - 2
                    method = "west1"
                elif ((p + connected_map_width - 3)%0x4000)+0x4000 == strip_pointer:
                    logging.debug("west h <= 0")
                    # lin's method:
                    #   p += otherMap.width - 3
                    p += connected_map_width - 3
                    method = "west2"
                elif ((p + xoffset + (current_map_height * 2))%0x4000 + 0x4000) == strip_pointer:
                    method = "west3"
                    p += xoffset + (current_map_height * 2)
                elif (p%0x4000)+0x4000 != strip_pointer:
                    # worst case scenario: dunno what to do
                    method = "west4"
                    p = strip_pointer
                else:
                    # this doesn't seem to ever happen
                    # do nothing
                    method = "west5"
            elif ldirection == "south":
                logging.debug("south.. dunno what to do?")

                if (p%0x4000)+0x4000 == strip_pointer:
                    # do nothing
                    method = "south1"
                elif ((p + (xoffset - connection_strip_length + self.smh.width.byte) / 2)%0x4000)+0x4000 == strip_pointer:
                    # comet's method
                    method = "south2"
                    p += (xoffset - connection_strip_length + self.smh.width.byte) / 2
                elif ((p + ((xoffset - connection_strip_length + self.smh.width.byte) / 2) - 1)%0x4000)+0x4000 == strip_pointer:
                    method = "south3"
                    p += ((xoffset - connection_strip_length + self.smh.width.byte) / 2) - 1
            elif ldirection == "east":
                if (p%0x4000)+0x4000 == strip_pointer:
                    # do nothing
                    method = "east1"
                elif ((p + (connected_map_height - connection_strip_length) * connected_map_width)%0x4000)+0x4000 == strip_pointer:
                    p += (connected_map_height - connection_strip_length) * connected_map_width
                    method = "east2"
                elif ((p + 100 - 4 * connected_map_width)%0x4000) + 0x4000 == strip_pointer:
                    method = "east3"
                    p += 100 - 4 * connected_map_width
                elif ((p + 2 * (100 - 4 * connected_map_width))%0x4000) + 0x4000 == strip_pointer:
                    method = "east4"
                    # the "2" is possibly ( connected_map_height / current_map_height )
                    # or current_map_width/yoffset or connected_map_width/yoffset
                    p += 2 * (100 - 4 * connected_map_width)

            # convert the address to a 2-byte pointer
            intermediate_p = p
            p = (p % 0x4000) + 0x4000

            data = {
                "strip_pointer": strip_pointer,
                "strip_length": connection_strip_length,
                "other_blockdata_address": connected_second_map_header.blockdata.address,
                "other_blockdata_pointer": (connected_second_map_header.blockdata.address%0x4000)+0x4000,

                "xoffset": xoffset,
                "yoffset": yoffset,

                "connected_map_height": connected_map_height,
                "connected_map_width": connected_map_width,
                "connected_map_group_id": connected_map_group_id,
                "connected_map_id": connected_map_id,
                "connected_map_label": map_names[connected_map_group_id][connected_map_id]["label"],

                "current_map_width": self.smh.width.byte,
                "current_map_height": self.smh.height.byte,
                "current_map_label": map_names[self.smh.map_group][self.smh.map_id]["label"],
                "current_map_group_id": self.smh.map_group,
                "current_map_id": self.smh.map_id,

                "difference": strip_pointer - ((connected_second_map_header.blockdata.address%0x4000)+0x4000),
                "direction": ldirection,
                "method": method,
            }
            strip_pointer_data.append(data)

            if p != strip_pointer:
                wowparams = {
                    "method": method,
                    "direction": ldirection,
                    "other map blockdata address": hex(connected_second_map_header.blockdata.address),
                    "h": h,
                    "initial p": hex(connected_second_map_header.blockdata.address),
                    "intermediate p": hex(intermediate_p),
                    "final p": hex(p),
                    "connection length": connection_strip_length,
                    "strip_pointer": hex(strip_pointer),
                    "other map height": connected_map_height,
                    "other map width": connected_map_width,
                }

                logging.debug(wowparams)

                whatparams = {
                    "other map group_id": hex(connected_map_group_id),
                    "other map map_id": hex(connected_map_id),
                    "other map name": +map_names[connected_map_group_id][connected_map_id]["label"],
                    "smh": hex(connected_second_map_header.address),
                    "width": connected_second_map_header.width.byte,
                    "height": connected_second_map_header.height.byte,
                }

                logging.debug(whatparams)

                curparams = {
                    "current map group_id": hex(self.map_group),
                    "current map map_id": hex(self.map_id),
                    "current map name": map_names[self.map_group][self.map_id]["label"],
                    "smh": hex(self.smh.address),
                    "width": self.smh.width.byte,
                    "height": self.smh.height.byte,
                }

                logging.debug(curparams)

                if ldirection == "east":
                    wrong_easts.append(data)
                elif ldirection == "west":
                    wrong_wests.append(data)
                elif ldirection == "south":
                    wrong_souths.append(data)
                elif ldirection == "north":
                    wrong_norths.append(data)

                # this will only happen if there's a bad formula
                raise Exception("tauwasser strip_pointer calculation was wrong? strip_pointer="+hex(strip_pointer) + " p="+hex(p))

            calculated_destination = None
            method = "strip_destination_default"
            x_movement_of_the_connection_strip_in_blocks = None
            y_movement_of_the_connection_strip_in_blocks = None

            # the below code makes sure there's an equation to calculate strip_destination
            # 11:05 <comet> Above: C803h + xoffset
            # 11:05 <comet> Below: C803h + (m.height + 3) * (m.width + 6) + xoffset
            # 11:05 <comet> Left: C800h + (m.width + 6) * (yoffset + 3)
            # 11:05 <comet> Right: C7FDh + (m.width + 6) * (yoffset + 4)
            #
            # tauwasser calls this "connection strip destination" and lin calls this "memoryOtherPointer"
            #   Points to the upper left block of the connection strip
            #   (The bank the Blockdata is in, is loaded out of the Mapheader of the connected Map.)
            #   The connection strip is always 3 Blocks high resp. wide
            #   (depending on the connection's direction)
            if ldirection == "north":
                x_movement_of_the_connection_strip_in_blocks = strip_destination - 0xC703
                logging.debug(
                    "(north) x_movement_of_the_connection_strip_in_blocks is: {0}"
                    .format(x_movement_of_the_connection_strip_in_blocks)
                )
                if x_movement_of_the_connection_strip_in_blocks < 0:
                    raise Exception("x_movement_of_the_connection_strip_in_blocks is wrong? " + str(x_movement_of_the_connection_strip_in_blocks))
            elif ldirection == "south":
                # strip_destination =
                # 0xc703 + (current_map_height + 3) * (current_map_width + 6) + x_movement_of_the_connection_strip_in_blocks
                x_movement_of_the_connection_strip_in_blocks = strip_destination - (0xc703 + (current_map_height + 3) * (current_map_width + 6))
                logging.debug(
                    "(south) x_movement_of_the_connection_strip_in_blocks is: {0}"
                    .format(x_movement_of_the_connection_strip_in_blocks)
                )
            elif ldirection == "east":
                # strip_destination =
                #   0xc700 + (current_map_width + 6) * (y_movement_of_the_connection_strip_in_blocks + 3)
                y_movement_of_the_connection_strip_in_blocks = (strip_destination - 0xc700) / (current_map_width + 6) - 3
                logging.debug(
                    "(east) y_movement_of_the_connection_strip_in_blocks is {0}"
                    .format(y_movement_of_the_connection_strip_in_blocks)
                )
            elif ldirection == "west":
                # strip_destination =
                #   0xc6fd + (current_map_width + 6) * (y_movement_of_the_connection_strip_in_blocks + 4)
                y_movement_of_the_connection_strip_in_blocks = (strip_destination - 0xc6fd) / (current_map_width + 6) - 4
                logging.debug(
                    "(west) y_movement_of_the_connection_strip_in_blocks is {0}"
                    .format(y_movement_of_the_connection_strip_in_blocks)
                )

            # let's also check the window equations
            # tauwasser calls this "window" and lin calls this "memoryCurrentPointer"
            # Position of the upper left block after entering the Map
            #
            # tauwasser's formula for windows:
            #   Above: C701h + Height_of_connected_map * (Width_of_connected_map + 6)
            #   Left: C706h + 2 * Width_of_connected_map
            #   Below/Right: C707h + Width_of_connected_map
            window_worked = False
            if ldirection == "north":
                # tauwasser's formula: 0xc701 + connected_map_height * (connected_map_width + 6)
                window_start = 0xc801
                if window == window_start + (connected_map_height * 6) + (connected_map_height * connected_map_width):
                    window_worked = True
            elif ldirection == "east":
                window_start = 0xc807
                if window == (window_start + connected_map_width):
                    window_worked = True
            elif ldirection == "south":
                window_start = 0xc807
                if window == (window_start + connected_map_width):
                    window_worked = True
            elif ldirection == "west":
                window_start = 0xc807
                if window == (window_start + xoffset):
                    window_worked = True

            data = {
                "window": window,
                "window_start": window_start,
                "window_diff": window - window_start,
                "window_worked": window_worked,
                "strip_destination": strip_destination,
                "strip_length": connection_strip_length,
                "other_blockdata_address": connected_second_map_header.blockdata.address,
                "other_blockdata_pointer": (connected_second_map_header.blockdata.address%0x4000)+0x4000,

                "xoffset": xoffset,
                "yoffset": yoffset,

                "connected_map_height": connected_map_height,
                "connected_map_width": connected_map_width,
                "connected_map_group_id": connected_map_group_id,
                "connected_map_id": connected_map_id,
                "connected_map_label": map_names[connected_map_group_id][connected_map_id]["label"],

                "current_map_width": self.smh.width.byte,
                "current_map_height": self.smh.height.byte,
                "current_map_label": map_names[self.smh.map_group][self.smh.map_id]["label"],
                "current_map_group_id": self.smh.map_group,
                "current_map_id": self.smh.map_id,

                "y_movement_of_the_connection_strip_in_blocks": y_movement_of_the_connection_strip_in_blocks,
                "x_movement_of_the_connection_strip_in_blocks": x_movement_of_the_connection_strip_in_blocks,

                "direction": ldirection,
                "method": method,
            }
            strip_destination_data.append(data)

    def to_asm(self):
        output     = ""
        ldirection = self.direction.lower()

        connected_map_group_id      = self.connected_map_group_id
        connected_map_id            = self.connected_map_id

        connected_map_header        = map_names[connected_map_group_id][connected_map_id]["header_new"]
        connected_second_map_header = connected_map_header.second_map_header
        connected_map_height        = connected_second_map_header.height.byte
        connected_map_width         = connected_second_map_header.width.byte

        connection_strip_length = self.connection_strip_length
        connected_map_width     = self.connected_map_width

        current_map_height      = self.smh.height.byte
        current_map_width       = self.smh.width.byte

        map_constant_label          = get_map_constant_label(map_group=connected_map_group_id, map_id=connected_map_id, map_internal_ids=self.map_internal_ids)
        self_constant_label         = get_map_constant_label(map_group=self.smh.map_group, map_id=self.smh.map_id, map_internal_ids=self.map_internal_ids)
        if map_constant_label != None:
            map_group_label = "GROUP_" + map_constant_label
            map_label       = "MAP_"   + map_constant_label
        else:
            map_group_label = str(connected_map_group_id)
            map_label       = str(connected_map_id)

        output += "; " + self.direction.upper() + " to " \
                  + map_names[connected_map_group_id][connected_map_id]["name"] \
                  + "\n"

        output += "db %s, %s ; connected map (group, id)\n" % (map_group_label, map_label)

        yoffset = self.yoffset
        xoffset = self.xoffset

        # According to JohtoMap, the calculation for tile data pointer is:
        #   int p = otherMap.tileDataLocation;
        #   int h = (otherMap.width - otherMap.height)
        #   if (h > 0)
        #       p += (h * otherMap.height) + (otherMap.height * 3) + (otherMap.height + 3)
        #   else
        #       p += (otherMap.height * otherMap.width) - (otherMap.width * 3);
        #   c.tileDataPointer = gb.Get2BytePointer(p);
        strip_pointer     = self.strip_pointer

        p = connected_second_map_header.blockdata.address

        output += "dw "

        if ldirection == "north":
            h = connected_map_width - self.smh.width.byte
            if ((p + ((connected_map_height * connected_map_width) - (connected_map_width * 3)))%0x4000)+0x4000 == strip_pointer:
                # lin's equation:
                #   p += (otherMap.height * otherMap.width) - (otherMap.width * 3)
                p += (connected_map_height * connected_map_width) - (connected_map_width * 3)
                method = "north1"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + (" + map_constant_label + "_HEIGHT * " + map_constant_label + "_WIDTH) - (" + map_constant_label + "_WIDTH * 3))"
            elif ((p + connected_map_width + xoffset + (16 * connected_map_height) - 16)%0x4000)+0x4000 == strip_pointer:
                p += connected_map_width + xoffset + (16 * connected_map_height) - 16
                method = "north2"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + map_constant_label + "_WIDTH + " + str(xoffset) + " + (16 * " + map_constant_label + "_HEIGHT) - 16)"
            elif p != strip_pointer:
                # worst case scenario: we don't know how to calculate p, so we'll just set it as a constant
                # example: Route10North north to Route9 (strip_pointer=0x7eae, connected map's blockdata=0x7de9)
                p = strip_pointer
                method = "north3"
                output += "$%.2x" % (p)
            else:
                # this doesn't seem to ever happen
                # or just do nothing (value is already ok)
                method = "north4"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + ")"
        elif ldirection == "west":
            h = connected_map_height - self.smh.height.byte
            h_out = "(" + map_constant_label +"_HEIGHT - " + self_constant_label +"_HEIGHT)"
            if ((p + (h * connected_map_width) - (connected_map_width * 3) + (connected_map_width - 1) - 2)%0x4000)+0x4000 == strip_pointer:
                # lin's method:
                #   p += (h * otherMap.width) - (otherMap.width * 3) + (otherMap.width - 3)
                p += (h * connected_map_width) - (connected_map_width * 3) + (connected_map_width - 1) - 2
                method = "west1"
                this_part = "((" + h_out + " * " + map_constant_label + "_WIDTH) - (" + map_constant_label + "_WIDTH * 3) + (" + map_constant_label + "_WIDTH - 1) - 2)"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + this_part + ")"
            elif ((p + connected_map_width - 3)%0x4000)+0x4000 == strip_pointer:
                logging.debug("west h <= 0")
                # lin's method:
                #   p += otherMap.width - 3
                p += connected_map_width - 3
                method = "west2"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + map_constant_label + "_WIDTH - 3)"
            elif ((p + xoffset + (current_map_height * 2))%0x4000 + 0x4000) == strip_pointer:
                method = "west3"
                p += xoffset + (current_map_height * 2)
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + str(xoffset) + " + (" + map_constant_label + "_HEIGHT * 2))"
            elif (p%0x4000)+0x4000 != strip_pointer:
                # worst case scenario: dunno what to do
                method = "west4"
                p = strip_pointer
                output += "$%.2x" % ((p%0x4000)+0x4000)
            else:
                # this doesn't seem to ever happen
                # do nothing
                method = "west5"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + ")"
        elif ldirection == "south":
            if (p%0x4000)+0x4000 == strip_pointer:
                # do nothing
                method = "south1"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + ")"
            elif ((p + (xoffset - connection_strip_length + self.smh.width.byte) / 2)%0x4000)+0x4000 == strip_pointer:
                # comet's method
                method = "south2"
                p += (xoffset - connection_strip_length + self.smh.width.byte) / 2
                this_part = "((" + str(xoffset) + " - " + str(connection_strip_length) + " + " + self_constant_label + "_WIDTH) / 2)"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + this_part + ")"
            elif ((p + ((xoffset - connection_strip_length + self.smh.width.byte) / 2) - 1)%0x4000)+0x4000 == strip_pointer:
                method = "south3"
                p += ((xoffset - connection_strip_length + self.smh.width.byte) / 2) - 1
                this_part = "(((" + str(xoffset) + " - " + str(connection_strip_length) + " + " + self_constant_label + "_WIDTH) / 2) - 1)"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + this_part + ")"
        elif ldirection == "east":
            if (p%0x4000)+0x4000 == strip_pointer:
                # do nothing
                method = "east1"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + ")"
            elif ((p + (connected_map_height - connection_strip_length) * connected_map_width)%0x4000)+0x4000 == strip_pointer:
                p += (connected_map_height - connection_strip_length) * connected_map_width
                method = "east2"
                this_part = "((" + map_constant_label + "_HEIGHT - " + str(connection_strip_length) + ") * " + map_constant_label + "_WIDTH)"
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + " + this_part +  ")"
            elif ((p + 100 - 4 * connected_map_width)%0x4000) + 0x4000 == strip_pointer:
                method = "east3"
                p += 100 - 4 * connected_map_width
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + 100 - (" + map_constant_label + "_WIDTH * 4))"
            elif ((p + 2 * (100 - 4 * connected_map_width))%0x4000) + 0x4000 == strip_pointer:
                method = "east4"
                # the "2" is possibly ( connected_map_height / current_map_height )
                # or current_map_width/yoffset or connected_map_width/yoffset
                p += 2 * (100 - 4 * connected_map_width)
                output += "(" + get_label_for(connected_second_map_header.blockdata.address) + " + ((100 - (" + map_constant_label + "_WIDTH * 4)) * 2))"

        output += " ; strip pointer\n"

        # tauwasser calls this "connection strip destination" and lin calls this "memoryOtherPointer"
        #   Points to the upper left block of the connection strip
        #   (The bank the Blockdata is in, is loaded out of the Mapheader of the connected Map.)
        #   The connection strip is always 3 Blocks high resp. wide
        #   (depending on the connection's direction)
        strip_destination = self.strip_destination

        output += "dw "

        # i am not convinced about these calculations
        if ldirection == "north":
            x_movement_of_the_connection_strip_in_blocks = strip_destination - 0xC703
            xmov = x_movement_of_the_connection_strip_in_blocks
            output += "($C703 + " + str(xmov) + ")"
        elif ldirection == "south":
            # strip_destination =
            # 0xc703 + (current_map_height + 3) * (current_map_width + 6) + x_movement_of_the_connection_strip_in_blocks
            x_movement_of_the_connection_strip_in_blocks = strip_destination - (0xc703 + (current_map_height + 3) * (current_map_width + 6))
            xmov = x_movement_of_the_connection_strip_in_blocks
            #output += "($C703 + (((" + self_constant_label + "_HEIGHT + 3) * (" + self_constant_label + "_WIDTH + 6)) + " + str(xmov) + "))"

            # xmov = strip_destination - (0xc703 + (current_map_height + 3) * (current_map_width + 6))
            #difference = 0xC715 + xmov + 6*current_map_height + 3*current_map_width + current_map_width*current_map_height
            #difference = 50965 + ymov + 6*current_map_height + 3*current_map_width + current_map_width*current_map_height

            output += "($C703 + " + str(xmov) + " + ((" + self_constant_label + "_HEIGHT + 3) * (" + self_constant_label + "_WIDTH + 6)))"
        elif ldirection == "east":
            # strip_destination =
            #   0xc700 + (current_map_width + 6) * (y_movement_of_the_connection_strip_in_blocks + 3)
            y_movement_of_the_connection_strip_in_blocks = (strip_destination - 0xc700) / (current_map_width + 6) - 3
            ymov = y_movement_of_the_connection_strip_in_blocks
            #output += "($C700 + ((" + self_constant_label + "_WIDTH + 6) * (" + str(ymov) + " + 3)) + "+str(ymov)+")"
            output += "$%.2x" % (strip_destination)
        elif ldirection == "west":
            # strip_destination =
            # 0xc6fd + (current_map_width + 6) * (y_movement_of_the_connection_strip_in_blocks + 4)
            y_movement_of_the_connection_strip_in_blocks = (strip_destination - 0xc700) / (current_map_width + 6) - 3
            ymov = y_movement_of_the_connection_strip_in_blocks
            #output += "($C700 + ((" + self_constant_label + "_WIDTH + 6) * (" + str(ymov) + " + 4)) - 4)"
            output += "$%.2x" % (strip_destination)
        output += " ; strip destination\n"

        output += "db " + str(connection_strip_length,) + ", " + map_constant_label + "_WIDTH ; (connection strip length, connected map width)\n"

        #if ldirection in ["east", "west"]:
        #    Y_movement_of_connection_strip_in_blocks =
        #elif direction in ["north", "south"]:
        #    X_movement_of_connection_strip_in_blocks =

        # Above: (Height_of_connected_map * 2) - 1
        # Below: 0
        # Left/Right: (Y_movement_of_connection_strip_in_blocks * -2)
        yoffset = self.yoffset # y_position_after_map_change

        if ldirection == "south" and yoffset != 0:
            raise Exception("tauwasser was wrong about yoffset=0 for south? it's: " + str(yoffset))
        elif ldirection == "north" and yoffset != ((connected_map_height * 2) - 1):
            raise Exception("tauwasser was wrong about yoffset for north? it's: " + str(yoffset))
        #elif not ((yoffset % -2) == 0):
        #    raise Exception("tauwasser was wrong about yoffset for west/east? it's not divisible by -2: " + str(yoffset))

        # Left: (Width_of_connected_map * 2) - 1
        # Right: 0
        # Above/Below: (X_movement_of_connection_strip_in_blocks * -2)
        xoffset = self.xoffset # x_position_after_map_change

        if ldirection == "east" and xoffset != 0:
            raise Exception("tauwasser was wrong about xoffset=0 for east? it's: " + str(xoffset))
        elif ldirection == "west" and xoffset != ((connected_map_width * 2) - 1):
            raise Exception("tauwasser was wrong about xoffset for west? it's: " + str(xoffset))
        #elif not ((xoffset % -2) == 0):
        #    raise Exception("tauwasser was wrong about xoffset for north/south? it's not divisible by -2: " + str(xoffset))

        output += "db "

        if ldirection == "south":
            output += "0"
        elif ldirection == "north":
            output += "((" + map_constant_label + "_HEIGHT * 2) - 1)"
        else:
            output += str(yoffset)

        output += ", "

        if ldirection == "east":
            output += "0"
        elif ldirection == "west":
            output += "((" + map_constant_label + "_WIDTH * 2) - 1)"
        else:
            output += str(xoffset)

        output += " ; yoffset, xoffset\n"

        window = self.window

        output += "dw "

        # let's also check the window equations
        # tauwasser calls this "window" and lin calls this "memoryCurrentPointer"
        # Position of the upper left block after entering the Map
        #
        # tauwasser's formula for windows:
        #   Above: C701h + Height_of_connected_map * (Width_of_connected_map + 6)
        #   Left: C706h + 2 * Width_of_connected_map
        #   Below/Right: C707h + Width_of_connected_map
        window_worked = False
        if ldirection == "north":
            # tauwasser's formula: 0xc701 + connected_map_height * (connected_map_width + 6)
            window_start = 0xc801
            if window == window_start + (connected_map_height * 6) + (connected_map_height * connected_map_width):
                window_worked = True
                output += "($C801 + ((" + map_constant_label + "_HEIGHT * 6) + (" + map_constant_label + "_HEIGHT * " + map_constant_label + "_WIDTH)))"
        elif ldirection == "east":
            window_start = 0xc807
            if window == (window_start + connected_map_width):
                window_worked = True
                output += "($C807 + " + map_constant_label + "_WIDTH)"
        elif ldirection == "south":
            window_start = 0xc807
            if window == (window_start + connected_map_width):
                window_worked = True
                output += "($C807 + " + map_constant_label + "_WIDTH)"
        elif ldirection == "west":
            window_start = 0xc807
            if window == (window_start + xoffset):
                window_worked = True
                output += "($C807 + " + str(xoffset) + ")"

        output += " ; window"

        return output

all_second_map_headers = []
def parse_second_map_header_at(address, map_group=None, map_id=None, debug=True):
    """each map has a second map header"""
    smh = SecondMapHeader(address, map_group=map_group, map_id=map_id, debug=debug)
    all_second_map_headers.append(smh)
    return smh

class MapBlockData(object):
    base_label = "MapBlockData_"
    maps_path = os.path.realpath(os.path.join(conf.path, "maps"))

    def __init__(self, address, map_group=None, map_id=None, debug=True, bank=None, label=None, width=None, height=None):
        self.address = address
        self.map_group = map_group
        self.map_id = map_id
        self.map_name = map_names[map_group][map_id]["label"]
        self.map_path = os.path.join(self.maps_path, self.map_name + ".blk")
        self.debug = debug
        self.bank = bank
        if width and height:
            self.width = width
            self.height = height
        else:
            raise Exception("MapBlockData needs to know the width/height of its map")
        label = self.make_label()
        self.label = Label(name=label, address=address, object=self)
        self.last_address = self.address + (self.width.byte * self.height.byte)
        script_parse_table[address : self.last_address] = self
        self.parse()

    def make_label(self):
        return map_names[self.map_group][self.map_id]["label"] + "_BlockData"

    def save_to_file(self):
        # check if the file exists already
        map_path = self.map_path
        if not os.path.exists(self.maps_path):
            os.mkdir(self.maps_path)
        if not os.path.exists(map_path):
            # dump to file
            #bytes = rom.interval(self.address, self.width.byte*self.height.byte, strings=True)
            bytes = rom[self.address : self.address + self.width.byte*self.height.byte]
            file_handler = open(map_path, "w")
            file_handler.write(bytes)
            file_handler.close()

    def parse(self):
        self.save_to_file()

    def to_asm(self):
        return "INCBIN \"maps/"+self.map_name+".blk\""


class MapEventHeader(object):
    base_label = "MapEventHeader_"

    def __init__(self, address, map_group=None, map_id=None, debug=True, bank=None, label=None):
        logging.debug(
            "making a MapEventHeader at {address} map_group={map_group} map_id={map_id}"
            .format(
                address=hex(address),
                map_group=map_group,
                map_id=map_id,
            )
        )
        self.address = address
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.bank = bank
        self.dependencies = None
        label = self.make_label()
        self.label = Label(name=label, address=address, object=self)
        self.parse()
        script_parse_table[address : self.last_address] = self

    def make_label(self):
        return map_names[self.map_group][self.map_id]["label"] + "_MapEventHeader"

    def parse(self):
        map_group, map_id, debug = self.map_group, self.map_id, self.debug
        address = self.address
        bank = pointers.calculate_bank(self.address) # or use self.bank
        logging.debug("event header address is {0}".format(hex(address)))

        filler1 = ord(rom[address])
        filler2 = ord(rom[address+1])
        self.fillers = [filler1, filler2]

        # warps
        warp_count = ord(rom[address+2])
        warp_byte_count = warp_byte_size * warp_count
        after_warps = address + 3 + warp_byte_count
        warps = parse_warps(address+3, warp_count, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        self.warp_count = warp_count
        self.warps = warps

        # triggers (based on xy location)
        xy_trigger_count = ord(rom[after_warps])
        trigger_byte_count = trigger_byte_size * xy_trigger_count
        xy_triggers = parse_xy_triggers(after_warps+1, xy_trigger_count, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        after_triggers = after_warps + 1 + trigger_byte_count
        self.xy_trigger_count = xy_trigger_count
        self.xy_triggers = xy_triggers

        # signposts
        signpost_count = ord(rom[after_triggers])
        signpost_byte_count = signpost_byte_size * signpost_count
        # signposts = rom.interval(after_triggers+1, signpost_byte_count)
        signposts = parse_signposts(after_triggers+1, signpost_count, bank=bank, map_group=map_group, map_id=map_id, debug=debug)
        after_signposts = after_triggers + 1 + signpost_byte_count
        self.signpost_count = signpost_count
        self.signposts = signposts

        # people events
        people_event_count = ord(rom[after_signposts])
        people_event_byte_count = people_event_byte_size * people_event_count
        # people_events_bytes = rom.interval(after_signposts+1, people_event_byte_count)
        # people_events = parse_people_event_bytes(people_events_bytes, address=after_signposts+1, map_group=map_group, map_id=map_id)
        people_events = parse_people_events(after_signposts+1, people_event_count, bank=pointers.calculate_bank(after_signposts+2), map_group=map_group, map_id=map_id, debug=debug)
        self.people_event_count = people_event_count
        self.people_events = people_events

        if people_event_count > 0:
            self.last_address = people_events[-1].last_address
        else:
            self.last_address = after_signposts+1
        return True

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        bases = []
        bases += self.people_events
        bases += self.signposts
        bases += self.xy_triggers
        bases += self.warps

        dependencies = []
        for p in bases:
            dependencies.extend(p.get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        xspacing = "" # was =spacing
        output = "; filler\n"
        output += "db %d, %d\n\n" % (self.fillers[0], self.fillers[1])

        output += xspacing + "; warps\n"
        output += xspacing + "db %d"%(self.warp_count)
        if len(self.warps) > 0:
            output += "\n"
        output += "\n".join([xspacing+warp.to_asm() for warp in self.warps])

        output += "\n\n"
        output += xspacing + "; xy triggers\n"
        output += xspacing + "db %d"%(self.xy_trigger_count)
        if len(self.xy_triggers) > 0:
            output += "\n"
        output += "\n".join([xspacing+xy_trigger.to_asm() for xy_trigger in self.xy_triggers])

        output += "\n\n"
        output += xspacing + "; signposts\n"
        output += xspacing + "db %d"%(self.signpost_count)
        if len(self.signposts) > 0:
            output += "\n"
        output += "\n".join([xspacing+signpost.to_asm() for signpost in self.signposts])

        output += "\n\n"
        output += xspacing + "; people-events\n"
        output += xspacing + "db %d"%(self.people_event_count)
        if len(self.people_events) > 0:
            output += "\n"

        for people_event in self.people_events:
            output += xspacing
            output += people_event.to_asm()
            output += "\n"

        if output[-1] == "\n":
            output = output[:-1]
        return output

all_map_event_headers = []
def parse_map_event_header_at(address, map_group=None, map_id=None, debug=True, bank=None):
    """parse crystal map event header byte structure thing"""
    ev = MapEventHeader(address, map_group=map_group, map_id=map_id, debug=debug, bank=bank)
    all_map_event_headers.append(ev)
    return ev

class MapScriptHeader(object):
    """parses a script header

    This structure allows the game to have e.g. one-time only events on a map
    or first enter events or permanent changes to the map or permanent script
    calls.

    This header a combination of a trigger script section and a callback script
    section. I don't know if these 'trigger scripts' are the same as the others
    referenced in the map event header, so this might need to be renamed very
    soon. The scripts in MapEventHeader are called XYTrigger.

    trigger scripts:
    [[Number1 of pointers] Number1 * [2byte pointer to script][00][00]]

    callback scripts:
    [[Number2 of pointers] Number2 * [hook number][2byte pointer to script]]

    hook byte choices:
        01 - map data has already been loaded to ram, tileset and sprites still missing
            map change (3rd step)
            loading (2nd step)
            map connection (3rd step)
            after battle (1st step)
        02 - map data, tileset and sprites are all loaded
            map change (5th step)
        03 - neither map data not tilesets nor sprites are loaded
            map change (2nd step)
            loading (1st step)
            map connection (2nd step)
        04 - map data and tileset loaded, sprites still missing
            map change (4th step)
            loading (3rd step)
            sprite reload (1st step)
            map connection (4th step)
            after battle (2nd step)
        05 - neither map data not tilesets nor sprites are loaded
            map change (1st step)
            map connection (1st step)

    When certain events occur, the call backs will be called in this order (same info as above):
        map change:
            05, 03, 01, 04, 02
        loading:
            03, 01, 04
        sprite reload:
            04
        map connection:
            05, 03, 01, 04 note that #2 is not called (unlike "map change")
        after battle:
            01, 04
    """
    base_label = "MapScriptHeader_"

    def __init__(self, address, map_group=None, map_id=None, debug=True, bank=None, label=None):
        logging.debug(
            "creating a MapScriptHeader at {address} map_group={map_group} map_id={map_id}"
            .format(
                address=hex(address),
                map_group=map_group,
                map_id=map_id,
            )
        )
        self.address = address
        self.map_group = map_group
        self.map_id = map_id
        self.debug = debug
        self.bank = bank
        self.dependencies = None
        label = self.make_label()
        self.label = Label(name=label, address=address, object=self)
        self.parse()
        script_parse_table[address : self.last_address] = self

    def make_label(self):
        return map_names[self.map_group][self.map_id]["label"] + "_MapScriptHeader"

    def parse(self):
        address = self.address
        map_group = self.map_group
        map_id = self.map_id
        debug = self.debug
        #[[Number1 of pointers] Number1 * [2byte pointer to script][00][00]]
        self.trigger_count = ord(rom[address])
        self.triggers = []
        ptr_line_size = 4
        groups = helpers.grouper(rom.interval(address+1, self.trigger_count * ptr_line_size, strings=False), count=ptr_line_size)
        current_address = address+1
        for (index, trigger_bytes) in enumerate(groups):
            logging.debug(
                "parsing a map trigger script at {address} map_group={map_group} map_id={map_id}"
                .format(
                    address=hex(current_address),
                    map_group=map_group,
                    map_id=map_id,
                )
            )
            script = ScriptPointerLabelParam(address=current_address, map_group=map_group, map_id=map_id, debug=debug)
            extra_bytes = MultiByteParam(address=current_address+2, map_group=map_group, map_id=map_id, debug=debug)
            self.triggers.append([script, extra_bytes])
            current_address += ptr_line_size
        current_address = address + (self.trigger_count * ptr_line_size) + 1
        #[[Number2 of pointers] Number2 * [hook number][2byte pointer to script]]
        callback_ptr_line_size = 3
        self.callback_count = DecimalParam(address=current_address)
        self.callback_count = self.callback_count.byte
        current_address += 1
        self.callbacks = []
        for index in range(self.callback_count):
            logging.debug(
                "parsing a callback script at {address} map_group={map_group} map_id={map_id}"
                .format(
                    address=hex(current_address),
                    map_group=map_group,
                    map_id=map_id,
                )
            )
            hook_byte = HexByte(address=current_address)
            callback = ScriptPointerLabelParam(address=current_address+1, map_group=map_group, map_id=map_id, debug=debug)
            self.callbacks.append({"hook": hook_byte, "callback": callback})
            current_address += 3 # i think?
        self.last_address = current_address
        logging.debug(
            "done parsing a MapScriptHeader map_group={map_group} map_id={map_id}"
            .format(map_group=map_group, map_id=map_id)
        )
        return True

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        if self.dependencies != None and not recompute:
            global_dependencies.update(self.dependencies)
            return self.dependencies
        dependencies = []
        for p in list(self.triggers):
            # dependencies.append(p[0])
            dependencies.extend(p[0].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        for callback in self.callbacks:
            dependencies.append(callback["callback"])
            global_dependencies.add(callback["callback"])
            dependencies.extend(callback["callback"].get_dependencies(recompute=recompute, global_dependencies=global_dependencies))
        self.dependencies = dependencies
        return dependencies

    def to_asm(self):
        output = ""
        output += "; trigger count\n"
        output += "db %d\n"%self.trigger_count
        if len(self.triggers) > 0:
            output += "\n; triggers\n"
            output += "\n".join([str("dw "+p[0].to_asm()+", "+p[1].to_asm()) for p in self.triggers])
            output += "\n"
        output += "\n; callback count\n"
        output += "db %d"%self.callback_count
        if len(self.callbacks) > 0:
            output += "\n\n; callbacks\n\n"
            output += "\n\n".join(["dbw "+str(p["hook"].byte)+", "+p["callback"].to_asm() for p in self.callbacks])
        return output

all_map_script_headers = []
def parse_map_script_header_at(address, map_group=None, map_id=None, debug=True):
    evv = MapScriptHeader(address, map_group=map_group, map_id=map_id, debug=debug)
    all_map_script_headers.append(evv)
    return evv

def parse_map_header_by_id(*args, **kwargs):
    """convenience function to parse a specific map"""
    map_group, map_id = None, None
    all_map_headers = kwargs["all_map_headers"]
    if "map_group" in kwargs.keys():
        map_group = kwargs["map_group"]
    if "map_id" in kwargs.keys():
        map_id = kwargs["map_id"]
    if (map_group == None and map_id != None) or \
       (map_group != None and map_id == None):
        raise Exception("map_group and map_id must both be provided")
    elif map_group == None and map_id == None and len(args) == 0:
        raise Exception("must be given an argument")
    elif len(args) == 1 and type(args[0]) == str:
        map_group = int(args[0].split(".")[0])
        map_id = int(args[0].split(".")[1])
    elif map_group == None and map_id == None:
        raise Exception("dunno what to do with input")
    offset = map_names[map_group]["offset"]
    map_header_offset = offset + ((map_id - 1) * map_header_byte_size)
    return parse_map_header_at(map_header_offset, all_map_headers=all_map_headers, map_group=map_group, map_id=map_id)

def parse_all_map_headers(map_names, all_map_headers=None, debug=True):
    """
    Calls parse_map_header_at for each map in each map group. Updates the
    map_names structure.
    """
    if "offset" not in map_names[1]:
        raise Exception("dunno what to do - map_names should have groups with pre-calculated offsets by now")
    for (group_id, group_data) in map_names.items():
        offset = group_data["offset"]
        # we only care about the maps
        #del group_data["offset"]
        for (map_id, map_data) in group_data.items():
            if map_id == "offset": continue # skip the "offset" address for this map group
            if debug:
                logging.debug(
                    "map_group={group_id} map_id={map_id}"
                    .format(group_id=group_id, map_id=map_id)
                )
            map_header_offset = offset + ((map_id - 1) * map_header_byte_size)
            map_names[group_id][map_id]["header_offset"] = map_header_offset

            new_parsed_map = parse_map_header_at(map_header_offset, map_group=group_id, map_id=map_id, all_map_headers=all_map_headers, debug=debug)
            map_names[group_id][map_id]["header_new"] = new_parsed_map

class PokedexEntryPointerTable(object):
    """
    A list of pointers.
    """

    def __init__(self):
        self.address = 0x44378
        self.target_bank = pointers.calculate_bank(0x181695)
        self.label = Label(name="PokedexDataPointerTable", address=self.address, object=self)
        self.size = None
        self.last_address = None
        self.dependencies = None
        self.entries = []
        self.parse()

        script_parse_table[self.address : self.last_address] = self

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        global_dependencies.update(self.entries)
        dependencies = []
        [dependencies.extend(entry.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)) for entry in self.entries]
        return dependencies

    def parse(self):
        size = 0
        lastpointer = 0
        for i in range(251):
            # Those are consecutive in GS!
            if i == 0x40:
                self.target_bank = 0x6e
            elif i == 0x80:
                self.target_bank = 0x73
            elif i == 0xc0:
                self.target_bank = 0x74
            loc = self.address+(i*2)
            pointer = calculate_pointer_from_bytes_at(loc, bank=self.target_bank)
            #print(hex(pointer))
            #if pointer < lastpointer:
            #    self.target_bank += 1
            #    pointer += 0x4000
            self.entries.append(PokedexEntry(pointer, i+1))

            size += 2
        self.size = size
        self.last_address = self.address + self.size

    def to_asm(self):
        output = "".join([str("dw "+get_label_for(entry.address)+"\n") for entry in self.entries])
        return output

class PokedexEntry(object):
    def __init__(self, address, pokemon_id):
        self.address = address
        self.dependencies = None
        #label = self.make_label()
        if pokemon_id in pokemon_constants.pokemon_constants:
            pokename = string.capwords(pokemon_constants.pokemon_constants[pokemon_id].replace("__", " ").replace("_", " ")).replace(" ", "")
        else:
            pokename = "Pokemon{0}".format(pokemon_id)
        self.label = Label(name=pokename+"PokedexEntry", address=self.address, object=self)
        self.parse()
        script_parse_table[address : self.last_address] = self

    def get_dependencies(self, recompute=False, global_dependencies=set()):
        return []

    def parse(self):
        # eww.
        address = self.address
        jump = how_many_until(chr(0x50), address, rom)
        self.species = parse_text_at(address, jump+1)
        address = address + jump + 1

        self.weight = ord(rom[address  ]) + (ord(rom[address+1]) << 8)
        self.height = ord(rom[address+2]) + (ord(rom[address+3]) << 8)
        address += 4

        jump = how_many_until(chr(0x50), address, rom)
        self.page1 = PokedexText(address)
        address = address + jump + 1
        jump = how_many_until(chr(0x50), address, rom)
        self.page2 = PokedexText(address)

        self.last_address = address + jump + 1
        #print(self.to_asm())
        return True

    def to_asm(self):
        output = """\
    db "{0}" ; species name
    dw {1}, {2} ; height, weight

    {3}
    {4}""".format(self.species, self.weight, self.height, self.page1.to_asm(), self.page2.to_asm())
        return output

# map names with no labels will be generated
# generate labels for each map name
for map_group_id in map_names.keys():
    map_group = map_names[map_group_id]
    for map_id in map_group.keys():
        # skip if we maybe already have the 'offset' label set in this map group
        if map_id == "offset": continue
        # skip if we provided a pre-set value for the map's label
        if "label" in map_group[map_id]: continue
        # convience alias
        map_data = map_group[map_id]
        # clean up the map name to be an asm label
        cleaned_name = map_name_cleaner(map_data["name"])
        # set the value in the original dictionary
        map_names[map_group_id][map_id]["label"] = cleaned_name
# generate map constants (like 1=PALLET_TOWN)
generate_map_constant_labels()

#### asm utilities ####
# these are pulled in from pokered/extras/analyze_incbins.py

# store each line of source code here
asm = None

# store each incbin line separately
incbin_lines = []

# storage for processed incbin lines
processed_incbins = {}

def to_asm(some_object, use_asm_rules=False):
    """shows an object's asm with a label and an ending comment
    showing the next byte address"""
    if isinstance(some_object, int):
        some_object = script_parse_table[some_object]
    # add one to the last_address to show where the next byte is in the file
    last_address = some_object.last_address
    # create a line like "label: ; 0x10101"
    asm = some_object.label.name + ": ; " + hex(some_object.address) + "\n"
    # now add the inner/actual asm
    #asm += spacing + some_object.to_asm().replace("\n", "\n"+spacing).replace("\n"+spacing+"\n"+spacing, "\n\n"+spacing)
    asmr = some_object.to_asm()
    asmr = asmr.replace("\n", "\n"+spacing)
    asmr = asmr.replace("\n"+spacing+"\n", "\n\n"+spacing)
    asmr = asmr.replace("\n\n"+spacing+spacing, "\n\n"+spacing)
    asm += spacing + asmr
    if use_asm_rules:
        asm = asm.replace("\n" + spacing + "; ", "\n; ")
        asm = asm.replace("\n" + spacing + ".asm_", "\n.asm_")
    # show the address of the next byte below this
    asm += "\n; " + hex(last_address)
    return asm

def get_dependencies_for(some_object, recompute=False, global_dependencies=set()):
    """
    calculates which labels need to be satisfied for an object
    to be inserted into the asm and compile successfully.

    You could also choose to not insert labels into the asm, but
    then you're losing out on the main value of having asm in the
    first place.
    """
    try:
        if isinstance(some_object, int):
            some_object = script_parse_table[some_object]
        if some_object.dependencies != None and not recompute:
            global_dependencies.update(some_object.dependencies)
        else:
            some_object.get_dependencies(recompute=recompute, global_dependencies=global_dependencies)
        return global_dependencies
    except RuntimeError as e:
        # 1552, 1291, 2075, 1552, 1291...

        errorargs = {
            "some_object": some_object,
            "class type": some_object.__class__,
            "label name": some_object.label.name,
            "address": some_object.address,
            "asm": to_asm(some_object),
        }

        logging.debug(str(errorargs))

        raise e

def isolate_incbins(asm=None):
    "find each incbin line"
    global incbin_lines
    if asm == None:
        asm = globals()["asm"]
    incbin_lines = []
    for line in asm:
        if line == "": continue
        if line.count(" ") == len(line): continue

        # clean up whitespace at beginning of line
        while line[0] == " ":
            line = line[1:]

        if line[0:6] == "INCBIN" and "baserom.gbc" in line:
            incbin_lines.append(line)
    return incbin_lines

def process_incbins():
    "parse incbin lines into memory"
    global asm, incbin_lines, processed_incbins
    # load asm if it isn't ready yet
    if asm == [] or asm == None:
        load_asm()
    # get a list of incbins if that hasn't happened yet
    if incbin_lines == [] or incbin_lines == None:
        isolate_incbins(asm=asm)
    # reset the global that this function creates
    processed_incbins = {}
    # for each incbin..
    for incbin in incbin_lines:
        # reset this entry
        processed_incbin = {}
        # get the line number from the global asm line list
        line_number = asm.index(incbin)
        # forget about all the leading characters
        partial_start = incbin[21:]
        start = partial_start.split(",")[0].replace("$", "0x")
        start = eval(start)
        start_hex = hex(start).replace("0x", "$")

        partial_interval = incbin[21:].split(",")[1]
        partial_interval = partial_interval.replace(";", "#")
        partial_interval = partial_interval.replace("$", "0x").replace("0xx", "0x")
        interval = eval(partial_interval)
        interval_hex = hex(interval).replace("0x", "$").replace("x", "")

        end = start + interval
        end_hex = hex(end).replace("0x", "$")

        processed_incbin = {"line_number": line_number,
                            "line": incbin,
                            "start": start,
                            "interval": interval,
                            "end": end, }
        # don't add this incbin if the interval is 0
        if interval != 0:
            processed_incbins[line_number] = processed_incbin
    return processed_incbins

def reset_incbins():
    "reset asm before inserting another diff"
    global asm, incbin_lines, processed_incbins
    asm = None
    incbin_lines = []
    processed_incbins = {}
    load_asm()
    isolate_incbins(asm=asm)
    process_incbins()

def find_incbin_to_replace_for(address, debug=False, rom_file=None):
    """returns a line number for which incbin to edit
    if you were to insert bytes into main.asm"""
    if rom_file == None:
        rom_file = os.path.join(conf.path, "baserom.gbc")
    if type(address) == str: address = int(address, 16)
    if not (0 <= address <= os.lstat(rom_file).st_size):
        raise IndexError("address is out of bounds")
    for incbin_key in processed_incbins.keys():
        incbin = processed_incbins[incbin_key]
        start = incbin["start"]
        end = incbin["end"]
        if debug:
            argstuff = {
                "start": start,
                "end": end,
                "address": str(type(address)),
            }

            logging.debug(str(argstuff))
            logging.debug(
                "checking... {start} <= {address} <= {end}"
                .format(
                    start=hex(start),
                    address=hex(address),
                    end=hex(end),
                )
            )
        if start <= address <= end:
            return incbin_key
    return None

def split_incbin_line_into_three(line, start_address, byte_count, rom_file=None):
    """
    splits an incbin line into three pieces.
    you can replace the middle one with the new content of length bytecount

    start_address: where you want to start inserting bytes
    byte_count: how many bytes you will be inserting
    """
    if rom_file == None:
        rom_file = os.path.join(conf.path, "baserom.gbc")
    if type(start_address) == str: start_address = int(start_address, 16)
    if not (0 <= start_address <= os.lstat(rom_file).st_size):
        raise IndexError("start_address is out of bounds")
    if len(processed_incbins) == 0:
        raise Exception("processed_incbins must be populated")

    original_incbin = processed_incbins[line]
    start = original_incbin["start"]
    end = original_incbin["end"]

    # start, end1, end2 (to be printed as start, end1 - end2)
    if start_address - start > 0:
        first = (start, start_address, start)
    else:
        first = (None) # skip this one because we're not including anything

    # this is the one you will replace with whatever content
    second = (start_address, byte_count)

    third = (start_address + byte_count, end - (start_address + byte_count))

    output = ""

    if first:
        output += "INCBIN \"baserom.gbc\",$" + hex(first[0])[2:] + ",$" + hex(first[1])[2:] + " - $" + hex(first[2])[2:] + "\n"
    output += "INCBIN \"baserom.gbc\",$" + hex(second[0])[2:] + "," + str(byte_count) + "\n"
    output += "INCBIN \"baserom.gbc\",$" + hex(third[0])[2:] + ",$" + hex(third[1])[2:] # no newline
    return output

def generate_diff_insert(line_number, newline, debug=False):
    """generates a diff between the old main.asm and the new main.asm
    note: requires python2.7 i think? b/c of subprocess.check_output"""
    global asm
    original = "\n".join(line for line in asm)
    newfile = deepcopy(asm)
    newfile[line_number] = newline # possibly inserting multiple lines
    newfile = "\n".join(line for line in newfile)

    # make sure there's a newline at the end of the file
    if newfile[-1] != "\n":
        newfile += "\n"

    original_filename = "ejroqjfoad.temp"
    newfile_filename = "fjiqefo.temp"

    original_fh = open(original_filename, "w")
    original_fh.write(original)
    original_fh.close()

    newfile_fh = open(newfile_filename, "w")
    newfile_fh.write(newfile)
    newfile_fh.close()

    try:
        from subprocess import CalledProcessError
    except ImportError:
        CalledProcessError = None

    try:
        diffcontent = subprocess.check_output("diff -u " + os.path.join(conf.path, "main.asm") + " " + newfile_filename, shell=True)
    except (AttributeError, CalledProcessError):
        p = subprocess.Popen(["diff", "-u", os.path.join(conf.path, "main.asm"), newfile_filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        diffcontent = out

    os.system("rm " + original_filename)
    os.system("rm " + newfile_filename)

    if debug:
        logging.debug("diffcontent is {0}".format(diffcontent))
    return diffcontent

def apply_diff(diff, try_fixing=True, do_compile=True):
    logging.info("Applying diff.")

    # write the diff to a file
    fh = open("temp.patch", "w")
    fh.write(diff)
    fh.close()

    # apply the patch
    os.system("cp " + os.path.join(conf.path, "main.asm") + " " + os.path.join(conf.path, "main1.asm"))
    os.system("patch " + os.path.join(conf.path, "main.asm") + " " + "temp.patch")

    # remove the patch
    os.system("rm temp.patch")

    # confirm it's working
    if do_compile:
        try:
            subprocess.check_call("cd " + conf.path + "; make clean; make", shell=True)
            return True
        except Exception as exc:
            if try_fixing:
                os.system("mv " + os.path.join(conf.path, "main1.asm") + " " + os.path.join(conf.path, "main.asm"))
            return False

from .crystalparts.asmline import AsmLine

class Incbin(object):
    def __init__(self, line, bank=None, debug=False):
        self.line = line
        self.bank = bank
        self.replace_me = False
        self.debug = debug
        self.parse()

    def parse(self):
        incbin = self.line
        partial_start = incbin[21:]
        start = partial_start.split(",")[0].replace("$", "0x")

        if self.debug:
            logging.debug("Incbin.parse line is {0}".format(self.line))
            logging.debug("Incbin.parse partial_start is {0}".format(partial_start))
            logging.debug("Incbin.parse start is {0}".format(start))
        try:
            start = eval(start)
        except Exception as e:
            logging.debug("start is {0}".format(start))
            raise Exception("problem with evaluating interval range: " + str(e))

        start_hex = hex(start).replace("0x", "$")

        partial_interval = incbin[21:].split(",")[1]
        partial_interval = partial_interval.replace(";", "#")
        partial_interval = partial_interval.replace("$", "0x").replace("0xx", "0x")
        interval = eval(partial_interval)
        interval_hex = hex(interval).replace("0x", "$").replace("x", "")

        end = start + interval
        end_hex = hex(end).replace("0x", "$")

        self.address = start
        self.start_address = start
        self.end_address = end
        self.last_address = end
        self.interval = interval

    def to_asm(self):
        if self.interval > 0:
            return self.line
        else:
            return ""

    def split(self, start_address, byte_count):
        """splits this incbin into three separate incbins"""
        if start_address < self.start_address or start_address > self.end_address:
            raise Exception("this incbin doesn't handle this address")
        incbins = []

        if self.debug:
            logging.debug(
                "splitting an incbin (\"{line}\") into three at {start} for {count} bytes"
                .format(
                    line=self.line,
                    start=hex(start_address),
                    count=byte_count,
                )
            )

        # start, end1, end2 (to be printed as start, end1 - end2)
        if (start_address - self.start_address) > 0:
            first = (self.start_address, start_address, self.start_address)
            incbins.append(Incbin("INCBIN \"baserom.gbc\",$%.2x,$%.2x - $%.2x" % (first[0], first[1], first[2])))
            if self.debug:
                logging.debug(incbins[0].line)
        else:
            # skip this one because we're not including anything
            first = None

        # this is the one you will replace with whatever content
        second = (start_address, byte_count)
        incbins.append(Incbin("INCBIN \"baserom.gbc\",$%.2x,$%.2x" % (start_address, byte_count)))
        incbins[-1].replace_me = True
        if self.debug:
            logging.debug(incbins[-1].line)

        if (self.last_address - (start_address + byte_count)) > 0:
            third = (start_address + byte_count, self.last_address - (start_address + byte_count))
            incbins.append(Incbin("INCBIN \"baserom.gbc\",$%.2x,$%.2x" % (third[0], third[1])))
            if self.debug:
                logging.debug(incbins[-1].line)

        return incbins

class AsmSection(object):
    def __init__(self, line):
        self.bank_id = None
        self.line = line
        self.parse()

    def parse(self):
        line    = self.line

        if not "bank" in line:
            self.bank_id      = -1
            self.address      = -1
            self.last_address = None
            self.end_address  = None
            return

        bank_id = int(line.split("\"")[1].split("bank")[1], 16)
        self.bank_id  = bank_id
        start_address = bank_id * 0x4000
        end_address   = (bank_id * 0x4000) + 0x4000 - 1

        self.address = self.start_address = start_address
        self.last_address = None
        self.end_address = None
        # this entity doesn't actually take up this space..
        # although it could be argued that lines should exist under this object
        #self.address  = self.start_address = start_address
        #self.last_address = self.end_address = end_address

    def to_asm(self):
        return self.line

new_asm = None
def load_asm2(filename=None, force=False):
    """loads the asm source code into memory"""
    if filename == None:
        filename = os.path.join(conf.path, "main.asm")
    global new_asm
    if new_asm == None or force:
        new_asm = Asm(filename=filename)
    return new_asm

class Asm(object):
    """controls the overall asm output"""
    def __init__(self, filename=None, debug=True):
        if filename == None:
            filename = os.path.join(conf.path, "main.asm")
        self.parts = []
        self.labels = []
        self.filename = filename
        self.debug = debug
        self.load_and_parse()

    def load_and_parse(self):
        self.parts = []
        asm = open(self.filename, "r").read().split("\n")
        asm_list = romstr.AsmList(asm)
        bank = 0
        for line in asm_list:
            if (line[0:6] == "INCBIN" or line[1:6] == "INCBIN") and not any([contaminant+"\"" in line for contaminant in [".2bpp", ".1bpp", ".asm", ".lz"]]):
                thing = Incbin(line, bank=bank)
            elif line[0:7] == "SECTION":
                thing = AsmSection(line)
                bank = thing.bank_id
            else:
                thing = AsmLine(line, bank=bank)
                label = labels.get_label_from_line(line)
                if label:
                    laddress = labels.get_address_from_line_comment(line)
                    thing.label = Label(name=label, address=laddress, object=thing, add_to_globals=False)
                    self.labels.append(thing.label)
            self.parts.append(thing)

    def is_label_name_in_file(self, label_name):
        for llabel in self.labels:
            if llabel.name == label_name:
                return llabel
        return False

    def does_address_have_label(self, address):
        """
        Checks if an address has a label.
        """
        # either something will directly have the address
        # or- it's possibel that no label was given
        # or there will be an Incbin that covers the range
        for part in self.parts:
            if isinstance(part, Incbin) and part.start_address <= address <= part.end_address:
                return False
            elif hasattr(part, "address") and part.address == address and hasattr(part, "label"):
                return part.label

        return None

    def insert(self, new_object):
        if isinstance(new_object, ScriptPointerLabelParam):
            # its' probably being injected in some get_dependencies() somewhere
            logging.debug("don't know why ScriptPointerLabelParam is getting to this point?")
            return

        # first some validation
        if not hasattr(new_object, "address"):
            logging.debug("object needs to have an address property: {0}".format(new_object))
            return

        start_address = new_object.address

        # skip this dragon shrine script calling itself
        # what about other scripts that call themselves ?
        if start_address in lousy_dragon_shrine_hack:
            logging.debug("skipping 0x18d079 in dragon shrine for a lousy hack")
            return

        if not hasattr(new_object, "label") and hasattr(new_object, "is_valid") and not new_object.is_valid():
            return

        debugmsg  = "object is " + new_object.label.name + " type="+str(new_object.__class__)+" new_object="+str(new_object)
        debugmsg += " label = " + new_object.label.name
        debugmsg += " start_address="+hex(start_address)#+" end_address="+hex(end_address)

        if not hasattr(new_object, "last_address"):
            logging.debug(debugmsg)
            raise Exception("object needs to have a last_address property")
        end_address = new_object.last_address
        debugmsg += " last_address="+hex(end_address)

        # check if the object is already inserted
        if new_object in self.parts:
            logging.debug(
                "object was previously inserted ({new_object}; {address})"
                .format(
                    new_object=new_object,
                    address=hex(new_object.address),
                )
            )
            return
        # check by label
        other_obj = self.is_label_name_in_file(new_object.label.name)
        if other_obj:
            other_obj = other_obj.object
            logging.debug(
                "object was previously inserted ({name} at {address}) by {othername} at {otheraddress}"
                .format(
                    name=new_object.label.name,
                    address=hex(new_object.address),
                    othername=other_obj.label.name,
                    otheraddress=other_obj.address,
                )
            )
            return
        # check by address
        #if self.does_address_have_label(new_object.address):
        #    print "object's address is already used ("+str(new_object)+") at "+hex(new_object.address)+" label="+new_object.label.name
        #    return

        if self.debug:
            logging.debug(debugmsg)
        del debugmsg
        if (end_address < start_address) or ((end_address - start_address) < 0):
            if not self.debug:
                logging.debug("object is new_object={0}".format(new_object))
                logging.deubg(
                    "start_address={start} end_address={end}"
                    .format(start=hex(start_address), end=hex(end_address))
                )
            if hasattr(new_object, "to_asm"):
                logging.debug(to_asm(new_object))
            raise Exception("Asm.insert was given an object with a bad address range")

        # 1) find which object needs to be replaced
        # or
        # 2) find which object goes after it
        found = False
        for object in list(self.parts):
            # skip objects without a defined interval (like a comment line)
            if not hasattr(object, "address") or not hasattr(object, "last_address"):
                continue
            # skip an AsmSection
            if isinstance(object, AsmSection):
                continue
            # replace an incbin with three incbins, replace middle incbin with whatever
            elif isinstance(object, Incbin) and (object.address <= start_address < object.last_address):
                # split up the incbin into three segments
                incbins = object.split(start_address, end_address - start_address)
                # figure out which incbin to replace with the new object
                if incbins[0].replace_me:
                    index = 0
                else: # assume incbins[1].replace_me (the middle one)
                    index = 1
                # replace that index with the new_object
                incbins[index] = new_object
                # insert these incbins into self.parts
                gindex = self.parts.index(object)
                self.parts = self.parts[:gindex] + incbins + self.parts[gindex:]
                self.parts.remove(object)
                found = True
                break
            elif object.address <= start_address < object.last_address:
                logging.debug("this is probably a script that is looping back on itself?")
                found = True
                break
            # insert before the current object
            elif object.address > end_address:
                #insert_before = index of object
                index = self.parts.index(object)
                self.parts.insert(index, new_object)
                found = True
                break
        if not found:
            raise Exception("unable to insert object into Asm")
        self.labels.append(new_object.label)
        return True

    def insert_with_dependencies(self, input):
        if type(input) == list:
            input_objects = input
        else:
            input_objects = [input]

        for object0 in input_objects:
            global_dependencies = set([object0])
            poopbutt = get_dependencies_for(object0, global_dependencies=global_dependencies, recompute=False)
            objects = global_dependencies
            objects.update(poopbutt)
            new_objects = copy(objects)
            for object in objects:
                if hasattr(object, "dependencies") and object.dependencies == None:
                    new_objects.update(object.get_dependencies())
            for object in new_objects:
                if isinstance(object, ScriptPointerLabelParam):
                    continue
                #if object in self.parts:
                #    if self.debug:
                #        print "already inserted -- object.__class__="+str(object.__class__)+" object is: "+str(object)+\
                #              " for object.__class__="+str(object0.__class__)+" object="+str(object0)
                #    continue
                if self.debug:
                    logging.debug("object is: {0}".format(object))
                self.insert(object)

                # just some old debugging
                #if object.label.name == "UnknownText_0x60128":
                #    raise Exception("debugging...")
                #elif object.label.name == "UnknownScript_0x60011":
                #    raise Exception("debugging.. dependencies are: " + str(object.dependencies) + " versus: " + str(object.get_dependencies()))

    def insert_single_with_dependencies(self, object):
        self.insert_with_dependencies(object)

    def insert_multiple_with_dependencies(self, objects):
        self.insert_with_dependencies(objects)

    def insert_all(self, limit=100):
        count = 0
        for each in script_parse_table.items():
            if count == limit: break
            object = each[1]
            if type(object) == str: continue
            self.insert_single_with_dependencies(object)
            count += 1

    def insert_and_dump(self, limit=100, filename="output.txt"):
        self.insert_all(limit=limit)
        self.dump(filename=filename)

    def dump(self, filename="output.txt"):
        fh = open(filename, "w")
        newlines_before_next_obj_requested = 0
        newlines_before_next_obj_given     = 0

        current_requested_newlines_before  = 0
        current_requested_newlines_after   = 0
        previous_requested_newlines_before = 0
        previous_requested_newlines_after  = 0

        written_newlines          = 0
        write_something           = False
        first = True
        last  = None
        for each in self.parts:
            asm = ""
            previous_requested_newlines_after = current_requested_newlines_after
            current_requested_newlines_before = current_requested_newlines_after

            write_something = True
            if (isinstance(each, str) and each == "") or (isinstance(each, AsmLine) and each.line == ""):
                current_requested_newlines_before = 0
                if current_requested_newlines_after < 2:
                    current_requested_newlines_after += 1
                write_something = False
            elif (isinstance(each, str) and each != "") or (isinstance(each, AsmLine) and each.line != ""):
                if isinstance(each, AsmLine):
                    asm = each.to_asm()
                elif isinstance(each, str):
                    asm = each
                current_requested_newlines_before = 0
                current_requested_newlines_after  = 1
            elif isinstance(each, AsmSection) or isinstance(each, Incbin) or hasattr(each, "to_asm"):
                if isinstance(each, AsmSection) or isinstance(each, Incbin):
                    asm = each.to_asm()
                else:
                    asm = to_asm(each)
                current_requested_newlines_before = 2
                current_requested_newlines_after  = 2
            else:
                raise Exception("dunno what to do with("+str(each)+") in Asm.parts")

            if write_something:
                if not first:
                    newlines_before = max([current_requested_newlines_before, previous_requested_newlines_after])
                    while written_newlines < newlines_before:
                        fh.write("\n")
                        written_newlines += 1
                else:
                    first = False
                fh.write(asm)
                written_newlines = 0
            last = each

        # make sure the file ends with a newline
        fh.write("\n")

def list_things_in_bank(bank):
    objects = []
    for blah in script_parse_table.items():
        object = blah[1]
        if hasattr(object, "address") and pointers.calculate_bank(object.address) == bank:
            objects.append(object)
    return objects

def list_texts_in_bank(bank):
    """
    Narrows down the list of objects that you will be inserting into Asm.
    """
    if len(all_texts) == 0:
        raise Exception("all_texts is blank.. parse_rom() will populate it")

    assert bank != None, "list_texts_in_banks must be given a particular bank"

    assert 0 <= bank < 0x80, "bank doesn't exist in the ROM"

    texts = []
    for text in all_texts:
        if pointers.calculate_bank(text.address) == bank:
            texts.append(text)

    return texts

def list_movements_in_bank(bank, all_movements):
    """
    Narrows down the list of objects to speed up Asm insertion.
    """
    if len(all_movements) == 0:
        raise Exception("all_movements is blank.. parse_rom() will populate it")

    assert bank != None, "list_movements_in_bank must be given a particular bank"
    assert 0 <= bank < 0x80, "bank doesn't exist in the ROM (out of bounds)"

    movements = []
    for movement in all_movements:
        if pointers.calculate_bank(movement.address) == bank:
            movements.append(movement)
    return movements

def dump_asm_for_texts_in_bank(bank, start=50, end=100, rom=None):
    """
    Simple utility to help with dumping texts into a particular bank. This is
    helpful for figuring out which text is breaking that bank.
    """
    # load and parse the ROM if necessary
    if rom == None or len(rom) <= 4:
        rom = load_rom()
        parse_rom()

    # get all texts
    # first 100 look okay?
    texts = list_texts_in_bank(bank)[start:end]

    # create a new dump
    asm = Asm()

    # start the insertion process
    asm.insert_multiple_with_dependencies(texts)

    # start dumping
    asm.dump()

    logging.info("done dumping texts for bank {banked}".format(banked="$%.2x" % bank))

def dump_asm_for_movements_in_bank(bank, start=0, end=100, all_movements=None):
    if rom == None or len(rom) <= 4:
        rom = load_rom()
        parse_rom()

    movements = list_movements_in_bank(bank, all_movements)[start:end]

    asm = Asm()
    asm.insert_with_dependencies(movements)
    asm.dump()
    logging.info("done dumping movements for bank {banked}".format(banked="$%.2x" % bank))

def dump_things_in_bank(bank, start=50, end=100):
    """
    is helpful for figuring out which object is breaking that bank.
    """
    # load and parse the ROM if necessary
    if rom == None or len(rom) <= 4:
        rom = load_rom()
        parse_rom()

    things = list_things_in_bank(bank)[start:end]

    # create a new dump
    asm = Asm()

    # start the insertion process
    asm.insert_with_dependencies(things)

    # start dumping
    asm.dump()

    logging.info("done dumping things for bank {banked}".format(banked="$%.2x" % bank))

def analyze_intervals():
    """find the largest baserom.gbc intervals"""
    global asm, processed_incbins
    if asm == None:
        load_asm()
    if processed_incbins == {}:
        isolate_incbins(asm=asm)
        process_incbins()
    results = []
    ordered_keys = sorted(processed_incbins, key=lambda entry: processed_incbins[entry]["interval"])
    ordered_keys.reverse()
    for key in ordered_keys:
        results.append(processed_incbins[key])
    return results

all_labels = []
def write_all_labels(all_labels, filename="labels.json"):
    fh = open(filename, "w")
    fh.write(json.dumps(all_labels))
    fh.close()
    return True

def setup_wram_labels(config=conf):
    """
    Get all wram labels and store it on the module.
    """
    wramproc = wram.WRAMProcessor(config=config)
    wramproc.initialize()
    wram.wram_labels = wramproc.wram_labels

def get_ram_label(address):
    """
    returns a label assigned to a particular ram address
    """
    if not hasattr(wram, "wram_labels"):
        setup_wram_labels()
    if address in wram.wram_labels.keys():
        return wram.wram_labels[address][-1]
    return None

def get_label_for(address):
    """
    returns a label assigned to a particular address
    """
    global all_labels

    if address == None:
        return None
    if type(address) != int:
        raise Exception("get_label_for requires an integer address, got: " + str(type(address)))

    # lousy hack to get around recursive scripts in dragon shrine
    if address in lousy_dragon_shrine_hack:
        return None

    # the old way
    for thing in all_labels:
        if thing["address"] == address:
            return thing["label"]

    # the new way
    obj = script_parse_table[address]
    if obj:
        if hasattr(obj, "label"):
            return obj.label.name
        else:
            return "AlreadyParsedNoDefaultUnknownLabel_" + hex(address)

    return None

# all_new_labels is a temporary replacement for all_labels,
# at least until the two approaches are merged in the code base.
all_new_labels = []

class Label(object):
    """
    Every object in script_parse_table is given a label.

    This label is simply a way to keep track of what objects have
    been previously written to file.
    """
    def __init__(self, name=None, address=None, line_number=None, object=None, is_in_file=None, address_is_in_file=None, add_to_globals=True):
        assert address != None, "need an address"
        assert is_valid_address(address), "address must be valid"
        assert object != None, "need an object to relate with"

        self.address = address
        self.object = object

        # label might not be in the file yet
        self.line_number = line_number

        # -- These were some old attempts to check whether the label
        # -- was already in use. They work, but the other method is
        # -- better.
        #
        # check if the label is in the file already
        # check if the address of this label is already in use

        self.is_in_file = is_in_file

        self.address_is_in_file = address_is_in_file

        if name == None:
            name = object.base_label + "_" + hex(object.address)

        self.name = name

        if add_to_globals:
            all_new_labels.append(self)

    def check_is_in_file(self):
        """
        This method checks if the label appears in the file based on the
        entries to the Asm.parts list.
        """
        # assert new_asm != None, "new_asm should be an instance of Asm"
        new_asm = load_asm2()
        is_in_file = new_asm.is_label_name_in_file(self.name)
        self.is_in_file = is_in_file
        return is_in_file

    def check_address_is_in_file(self):
        """
        Checks if the address is in use by another label.
        """
        new_asm = load_asm2()
        self.address_is_in_file = new_asm.does_address_have_label(self.address)
        return self.address_is_in_file

    def old_check_address_is_in_file(self):
        """
        Checks whether or not the address of the object is already in the file.
        This might happen if the label name is different but the address is the
        same. Another scenario is that the label is already used, but at a
        different address.

        This method works by looking at the INCBINs. When there is
        an INCBIN that covers this address in the file, then there
        is no label at this address yet (or there is, but we can
        easily add another label in front of the incbin or something),
        and when there is no INCBIN that has this address, then we
        know that something is already using this address.
        """
        if processed_incbins == {}:
            process_incbins()

        incbin = find_incbin_to_replace_for(self.address)

        if incbin == None:
            return True
        else:
            return False

    def make_label(self):
        """
        Generates a label name based on parents and self.object.
        """
        obj = self.object
        name = obj.make_label()
        return name

label_errors = ""
def get_labels_between(start_line_id, end_line_id, bank):
    foundlabels = []
    #label = {
    #   "line_number": 15,
    #   "bank": 32,
    #   "label": "PalletTownText1",
    #   "offset": 0x5315,
    #   "address": 0x75315,
    #}
    if asm == None:
        load_asm()
    sublines = asm[start_line_id : end_line_id + 1]
    for (current_line_offset, line) in enumerate(sublines):
        # skip lines without labels
        if not labels.line_has_label(line): continue
        # reset some variables
        line_id = start_line_id + current_line_offset
        line_label = labels.get_label_from_line(line)
        address = None
        offset = None
        # setup a place to store return values from line_has_comment_address
        returnable = {}
        # get the address from the comment
        has_comment = labels.line_has_comment_address(line, returnable=returnable, bank=bank)
        # skip this line if it has no address in the comment
        if not has_comment: continue
        # parse data from line_has_comment_address
        address = returnable["address"]
        bank = returnable["bank"]
        offset = returnable["offset"]
        # dump all this info into a single structure
        label = {
            "line_number": line_id,
            "bank": bank,
            "label": line_label,
            "offset": offset,
            "address": address,
        }
        # store this structure
        foundlabels.append(label)
    return foundlabels

def scan_for_predefined_labels(debug=False):
    """looks through the asm file for labels at specific addresses,
    this relies on the label having its address after. ex:

    ViridianCity_h: ; 0x18357 to 0x18384 (45 bytes) (bank=6) (id=1)
    PalletTownText1: ; 4F96 0x18f96
    ViridianCityText1: ; 0x19102

    It would be more productive to use rgbasm to spit out all label
    addresses, but faster to write this script. rgbasm would be able
    to grab all label addresses better than this script..
    """
    global all_labels
    all_labels = []
    bank_intervals = {}

    if asm == None:
        load_asm()

    # figure out line numbers for each bank
    for bank_id in range(0x7F+1):
        abbreviation = ("%.x" % (bank_id)).upper()
        abbreviation_next = ("%.x" % (bank_id+1)).upper()
        if bank_id == 0:
            abbreviation = "0"
            abbreviation_next = "1"

        # calculate the start/stop line numbers for this bank
        start_line_id = helpers.index(asm, lambda line: "\"bank" + abbreviation.lower() + "\"" in line.lower())
        if bank_id != 0x7F:
            end_line_id = helpers.index(asm, lambda line: "\"bank" + abbreviation_next.lower() + "\"" in line.lower())
            end_line_id += 1
        else:
            end_line_id = len(asm) - 1

        if debug:
            output = "bank" + abbreviation + " starts at "
            output += str(start_line_id)
            output += " to "
            output += str(end_line_id)
            logging.debug(output)

        # store the start/stop line number for this bank
        bank_intervals[bank_id] = {"start": start_line_id,
                                   "end": end_line_id,}
    # for each bank..
    for bank_id in bank_intervals.keys():
        # get the start/stop line number
        bank_data = bank_intervals[bank_id]
        start_line_id = bank_data["start"]
        end_line_id   = bank_data["end"]
        # get all labels between these two lines
        labels = get_labels_between(start_line_id, end_line_id, bank_id)
        # bank_intervals[bank_id]["labels"] = labels
        all_labels.extend(labels)
    write_all_labels(all_labels)
    return all_labels

all_map_headers = []

trainer_group_maximums = {}

# Some of the commands need a reference to this data. This is a hacky way to
# get around having a global, and it should be fixed eventually.
Command.trainer_group_maximums = trainer_group_maximums

SingleByteParam.map_internal_ids = map_internal_ids
MultiByteParam.map_internal_ids = map_internal_ids

def add_map_offsets_into_map_names(map_group_offsets, map_names=None):
    """
    Add the offsets for each map into the map_names variable.
    """
    # add the offsets into our map structure, why not (johto maps only)
    return [map_names[map_group_id+1].update({"offset": offset}) for map_group_id, offset in enumerate(map_group_offsets)]

rom_parsed = False

def parse_rom(rom=None):
    if not rom:
        # read the rom and figure out the offsets for maps
        rom = direct_load_rom()

    # make wram.wram_labels available
    setup_wram_labels()

    # figure out the map offsets
    map_group_offsets = load_map_group_offsets(map_group_pointer_table=map_group_pointer_table, map_group_count=map_group_count, rom=rom)

    # populate the map_names structure with the offsets
    add_map_offsets_into_map_names(map_group_offsets, map_names=map_names)

    # parse map header bytes for each map
    parse_all_map_headers(map_names, all_map_headers=all_map_headers)

    # find trainers based on scripts and map headers
    # this can only happen after parsing the entire map and map scripts
    find_trainer_ids_from_scripts(script_parse_table=script_parse_table, trainer_group_maximums=trainer_group_maximums)

    # and parse the main TrainerGroupTable once we know the max number of trainers
    #global trainer_group_table
    trainer_group_table = TrainerGroupTable(trainer_group_maximums=trainer_group_maximums, trainers=trainers, script_parse_table=script_parse_table)

    # improve duplicate trainer names
    make_trainer_group_name_trainer_ids(trainer_group_table)

    global rom_parsed
    rom_parsed = True

    return map_names

def cachably_parse_rom(rom=None):
    """
    Calls parse_rom if it hasn't been called and completed yet.
    """
    global rom_parsed
    if not rom_parsed:
        return parse_rom(rom=rom)
    else:
        return map_names

if __name__ == "crystal":
    pass