aboutsummaryrefslogtreecommitdiff
path: root/sword2/router.cpp
blob: 06229dc29bc67050091575eaad4a79a57052645b (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
/* Copyright (C) 1994-2003 Revolution Software Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

//--------------------------------------------------------------------------------------
// ROUTER.CPP by James

// A rehash of Jeremy's original jrouter.c, containing low-level system routines
// for calculating routes between points inside a walk-grid, and constructing
// walk animations from mega-sets.

// jrouter.c undwent 2 major reworks from the original:
// (1)	Restructured to allow more flexibility in the mega-sets, ie. more info taken from the walk-data
//		- the new George & Nico mega-sets & walk-data were then tested & tweaked in the Sword1 system
// (2)	Updated for the new Sword2 system, ie. new object structures
//		- now compatible with Sword2, the essential code already having been tested

//--------------------------------------------------------------------------------------


/****************************************************************************
 *    JROUTER.C				polygon router with modular walks
 *       					using a tree of modules
 *       					21 july 94  
 *  3  november 94  
 *  System currently works by scanning grid data and coming up with a	ROUTE
 *  as a series of way points(nodes), the smoothest eight directional PATH
 * 	through these nodes is then found, and a WALK created to fit the PATH.
 *
 *	Two funtions are called by the user, RouteFinder creates a route as a
 *	module list, HardWalk creates an animation list from the module list.
 *	The split is only provided to allow the possibility of turning the
 *	autorouter over two game cycles.     
 ****************************************************************************
 *    
 *	Routine timings on osborne 486
 *
 *	Read floor resource (file already loaded)	 112 pixels
 *
 *	Read mega resource (file already loaded)	 112 pixels
 *
 *
 *
 ****************************************************************************
 *    
 *  Modified 12 Oct 95
 *
 *	Target Points within 1 pixel of a line are ignored ???
 *
 *	Modules split into  Points within 1 pixel of a line are ignored ???
 *
 ****************************************************************************
 *
 *  TOTALLY REHASHED BY JAMES FOR NEW MEGAS USING OLD SYSTEM
 *  THEN REINCARNATED BY JAMES FOR NEW MEGAS USING NEW SYSTEM
 *
 ****************************************************************************
 ****************************************************************************/


//#define PLOT_PATHS 1

 
/*
 * Include Files
 */


#include "stdafx.h"
#include "driver/driver96.h"
#include "console.h"
#include "debug.h"
#include "defs.h"
#include "header.h"
#include "interpreter.h"
#include "memory.h"
#include "object.h"
#include "resman.h"
#include "router.h"

//#ifdef PLOT_PATHS
//#include "grengine.h"
//#endif

#define MAX_FRAMES_PER_CYCLE	16
#define NO_DIRECTIONS			8
#define MAX_FRAMES_PER_CHAR		(MAX_FRAMES_PER_CYCLE * NO_DIRECTIONS)
#define ROUTE_END_FLAG			255




//---------------------------------------
// TEMP!
int8 forceSlidy;	// 1 = force the use of slidy router (so solid path not used when ending walk in ANY direction)
//---------------------------------------

/*
 * Type Defines
 */

#define	O_WALKANIM_SIZE	600			// max number of nodes in router output
#define	O_GRID_SIZE		200			// max 200 lines & 200 points
#define	EXTRA_GRID_SIZE	20			// max 20 lines & 20 points
#define	O_ROUTE_SIZE	50			// max number of modules in a route


typedef struct
{
	int16   x1;
  	int16   y1;
  	int16   x2;
	int16   y2;
	int16   xmin;
	int16   ymin;
	int16   xmax;
	int16   ymax;
	int16   dx;	   // x2 - x1
	int16   dy;	   // y2 - y1
	int32   co;	   // co = (y1 *dx)- (x1*dy) from an equation for a line y*dx = x*dy + co
}_barData;

typedef struct
{
	int16   x;
	int16   y;
	int16	level;
	int16   prev;
	int16   dist;
}_nodeData;

typedef	struct
{
	int32		nbars;
	_barData	*bars;
	int32		nnodes;
	_nodeData	*node;
} _floorData;

typedef	struct
{
	int32	x;
	int32	y;
	int32	dirS;
	int32	dirD;
} _routeData;

typedef	struct
{
	int32	x;
	int32	y;
	int32	dir;
	int32	num;
} _pathData;


//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Function prototypes

static int32	GetRoute(void);
static void		ExtractRoute(void);
static void		LoadWalkGrid(void);
static void		SetUpWalkGrid(Object_mega *ob_mega, int32 x, int32 y, int32 dir);
static void		LoadWalkData(Object_walkdata *ob_walkdata);
static void		PlotCross(int16 x, int16 y, uint8 colour);

static int32	Scan(int32);     
static int32	NewCheck(int32, int32 , int32 , int32 , int32);
static int32	LineCheck(int32 , int32 , int32 , int32);
static int32	VertCheck(int32 , int32 , int32);
static int32	HorizCheck(int32 , int32 , int32);
static int32	Check(int32 , int32 , int32 , int32);
static int32	CheckTarget(int32 , int32);

static int32	SmoothestPath();
static int32	SlidyPath();
static int32	SolidPath();

static int32	SmoothCheck(int32 best, int32 p, int32 dirS, int32 dirD);

static int32	AddSlowInFrames(_walkData *walkAnim);
static void		AddSlowOutFrames(_walkData *walkAnim);
static void		SlidyWalkAnimator(_walkData *walkAnim);
static int32	SolidWalkAnimator(_walkData *walkAnim);
#ifdef PLOT_PATHS
static void		RouteLine(int32 x1,int32 y1,int32 x2,int32 y2 ,int32 colour);
#endif

//--------------------------------------------------------------------------------------
#define MAX_WALKGRIDS		10

static int32	walkGridList[MAX_WALKGRIDS];

//--------------------------------------------------------------------------------------
#define TOTAL_ROUTE_SLOTS	2				// because we only have 2 megas in the game!

static mem		*route_slots[TOTAL_ROUTE_SLOTS];	// stores pointers to mem blocks containing routes created & used by megas (NULL if slot not in use)

//--------------------------------------------------------------------------------------
// Local Variables

static	int32		nbars;
static	int32		nnodes;
static	_barData	bars[O_GRID_SIZE+EXTRA_GRID_SIZE];	// because extra bars will be copied into here afer walkgrid loaded
static	_nodeData	node[O_GRID_SIZE+EXTRA_GRID_SIZE];

// area for extra route data to block parts of floors and enable routing round mega charaters
static int32		nExtraBars	= 0;
static int32		nExtraNodes	= 0;
static _barData		extraBars[EXTRA_GRID_SIZE];
static _nodeData	extraNode[EXTRA_GRID_SIZE];

static	int32	startX;
static	int32	startY;
static	int32	startDir;
static	int32	targetX;
static	int32	targetY;
static	int32	targetDir;
static	int32	scaleA;
static	int32	scaleB;
static	_routeData	route[O_ROUTE_SIZE];
static	_pathData	smoothPath[O_ROUTE_SIZE];
static	_pathData	modularPath[O_ROUTE_SIZE];
static	int32   	routeLength;


int32	framesPerStep;
int32	framesPerChar;

uint8	nWalkFrames;				// no. of frames per walk cycle
uint8	usingStandingTurnFrames;	// any standing turn frames?
uint8	usingWalkingTurnFrames;		// any walking turn frames?
uint8	usingSlowInFrames;			// any slow-in frames?
uint8	usingSlowOutFrames;			// any slow-out frames?
int32 	dx[NO_DIRECTIONS + MAX_FRAMES_PER_CHAR];
int32	dy[NO_DIRECTIONS + MAX_FRAMES_PER_CHAR];
int8	modX[NO_DIRECTIONS];
int8	modY[NO_DIRECTIONS];
int32	diagonalx = 0;
int32	diagonaly = 0;

int32	firstStandFrame;

int32	firstStandingTurnLeftFrame;
int32	firstStandingTurnRightFrame;

int32	firstWalkingTurnLeftFrame;	// left walking turn
int32	firstWalkingTurnRightFrame; // right walking turn

uint32	firstSlowInFrame[NO_DIRECTIONS];
uint32	numberOfSlowInFrames[NO_DIRECTIONS];

uint32	leadingLeg[NO_DIRECTIONS];

int32	firstSlowOutFrame;
int32	numberOfSlowOutFrames;	// number of slow-out frames on for each leading-leg in each direction


int32	stepCount;

int32	moduleX;
int32	moduleY;
int32	currentDir;
int32	lastCount;
int32	frame;

// ie. total number of slow-out frames = (numberOfSlowOutFrames * 2 * NO_DIRECTIONS)

/*
 *    CODE
 */

// **************************************************************************

//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
uint8 CheckForCollision(void)
{
//	static uint32 player_pc;
//	static uint32 non_player_pc;


	uint8 collision=0;

	return (collision);
}
//--------------------------------------------------------------------------------------
uint8 ReturnSlotNo(uint32 megaId)
{
	if (ID==CUR_PLAYER_ID)	// George (8)
		return(0);
	else					// One of Nico's mega id's
		return(1);
}
//--------------------------------------------------------------------------------------
void AllocateRouteMem(void)
{
//	uint8 slotNo=0;
	uint8 slotNo;

	//------------------------------------------
	// removed (James23June96)
	/*
	while (route_slots[slotNo] > 0)
	{
		slotNo++;

	#ifdef _SWORD2_DEBUG
		if (slotNo == TOTAL_ROUTE_SLOTS)
			Con_fatal_error("ERROR: route_slots[] full in AllocateRouteMem() (%s line %u)",__FILE__,__LINE__);
	#endif
	}
	*/
	//------------------------------------------
	// added (James23June96)
	// Player character always always slot 0, while the other mega (normally Nico) always uses slot 1
	// Better this way, so that if mega object removed from memory while in middle of route,
	// the old route will be safely cleared from memory just before they create a new one

	slotNo = ReturnSlotNo(ID); 

	// if this slot is already used, then it can't be needed any more
	// because this id is creating a new route!
	if (route_slots[slotNo])
	{
		FreeRouteMem();
	}
	//------------------------------------------

	route_slots[slotNo] = Twalloc( sizeof(_walkData)*O_WALKANIM_SIZE, MEM_locked, UID_walk_anim );
	// 12000 bytes were used for this in Sword1 mega compacts, based on 20 bytes per '_walkData' frame
	// ie. allowing for 600 frames including end-marker
	// Now '_walkData' is 8 bytes, so 8*600 = 4800 bytes.
	// Note that a 600 frame walk lasts about 48 seconds! (600fps / 12.5s = 48s)

//	megaObject->route_slot_id = slotNo+1;	// mega keeps note of which slot contains the pointer to it's walk animation mem block
											// +1 so that '0' can mean "not walking"
}
//--------------------------------------------------------------------------------------
_walkData* LockRouteMem(void)
{
	uint8 slotNo = ReturnSlotNo(ID); 
	
	Lock_mem( route_slots[slotNo] );
	return (_walkData *)route_slots[slotNo]->ad;
}
//--------------------------------------------------------------------------------------
void FloatRouteMem(void)
{
	uint8 slotNo = ReturnSlotNo(ID); 

	Float_mem( route_slots[slotNo] );
}
//--------------------------------------------------------------------------------------
void FreeRouteMem(void)
{
	uint8 slotNo = ReturnSlotNo(ID); 

	Free_mem( route_slots[slotNo] );	// free the mem block pointed to from this entry of route_slots[]
	route_slots[slotNo] = NULL;			// clear this route_slots[] entry
}
//--------------------------------------------------------------------------------------
void FreeAllRouteMem(void)
{
	uint8 slotNo;

	for (slotNo=0; slotNo < TOTAL_ROUTE_SLOTS; slotNo++)
	{
		if (route_slots[slotNo])
		{
			Free_mem( route_slots[slotNo] );	// free the mem block pointed to from this entry of route_slots[]
			route_slots[slotNo] = NULL;
		}
	}
}
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------

// **************************************************************************
// **************************************************************************
// **************************************************************************
// **************************************************************************

int32 RouteFinder(Object_mega *ob_mega, Object_walkdata *ob_walkdata, int32 x, int32 y, int32 dir)
{
/****************************************************************************
 *    RouteFinder.C				polygon router with modular walks
 *    		   							21 august 94  
 *    3  november 94  
 *		RouteFinder creates a list of modules that enables HardWalk to create
 *		an animation list.
 *
 *    RouteFinder currently works by scanning grid data and coming up with a ROUTE
 *    as a series of way points(nodes), the smoothest eight directional PATH
 * 		through these nodes is then found, this information is made available to
 *		HardWalk for a WALK to be created to fit the PATH.
 *    
 *    30 november 94 return values modified
 *    
 *    return 	0 = failed to find a route
 *    
 *    				1 = found a route
 *
 *    				2 = mega already at target
 *    
 ****************************************************************************/

	int32		routeFlag = 0;
	int32		solidFlag = 0;
	_walkData *walkAnim;

//	megaId = id;


	SetUpWalkGrid(ob_mega, x, y, dir);
	LoadWalkData(ob_walkdata);

	walkAnim = LockRouteMem();	// lock the _walkData array (NB. AFTER loading walkgrid & walkdata!)

// **************************************************************************
// All route data now loaded start finding a route
// **************************************************************************
// **************************************************************************
// Check if we can get a route through the floor 		changed 12 Oct95 JPS 
// **************************************************************************

	routeFlag = GetRoute();


	if (routeFlag == 2)  //special case for zero length route
	{
		if (targetDir >7)// if target direction specified as any
		{
			targetDir = startDir;
		} 
		// just a turn on the spot is required set an end module for the route let the animator deal with it
		// modularPath is normally set by ExtractRoute
		modularPath[0].dir	= startDir;
 		modularPath[0].num	= 0;
 		modularPath[0].x	= startX;
 		modularPath[0].y	= startY;
		modularPath[1].dir	= targetDir;
 		modularPath[1].num	= 0;
 		modularPath[1].x	= startX;
 		modularPath[1].y	= startY;
 		modularPath[2].dir	= 9;
 		modularPath[2].num	= ROUTE_END_FLAG;

 		SlidyWalkAnimator(walkAnim);
 		routeFlag = 2;
	}
	else if (routeFlag == 1) // a normal route
	{
		SmoothestPath();//Converts the route to an exact path
																			// The Route had waypoints and direction options
																			// The Path is an exact set of lines in 8 directions that reach the target.
																			// The path is in module format, but steps taken in each direction are not accurate   
		// if target dir = 8 then the walk isn't linked to an anim so 
		// we can create a route without sliding and miss the exact target

		if (!forceSlidy)
		{
			if (targetDir == 8)	// can end facing ANY direction (ie. exact end position not vital) - so use SOLID walk to avoid sliding to exact position
			{
				SolidPath();
				solidFlag = SolidWalkAnimator(walkAnim);
			}
		}

		if(!solidFlag)	// if we failed to create a SOLID route, do a SLIDY one instead
		{
			SlidyPath();
			SlidyWalkAnimator(walkAnim);
		}
	}
	else // Route didn't reach target so assume point was off the floor
	{
//		routeFlag = 0;
	}


	#ifdef PLOT_PATHS
		#ifdef _WIN32
		RenderScreenGDK( screenDef.buffer, scroll_offset_x, scroll_offset_y, screenDef.width * XBLOCKSIZE );
		#else
		RenderOffScreenBuffer( scroll_offset_x, scroll_offset_y, SCREEN_WIDTH, SCREEN_DEPTH );
		#endif

		FlushMouseEvents();	// clear mouse buffer
		while (!TestForMouseEvent());	// wait for a button press or release
		FlushMouseEvents();	// clear mouse buffer again to prevent rapid fire!
	#endif


   	FloatRouteMem();	// float the _walkData array again

	return routeFlag;	// send back null route
}

/*******************************************************************************
 *******************************************************************************
 * 														GET A ROUTE
 *******************************************************************************
 *******************************************************************************/


int32 GetRoute(void)
{
 /****************************************************************************
  *    GetRoute.C				extract a path from walk grid
  *    		   							12 october 94  
  *
  *    GetRoute currently works by scanning grid data and coming up with a ROUTE
  *    as a series of way points(nodes).
	*		 static	_routeData			route[O_ROUTE_SIZE];
  *    
  *    return 	0 = failed to find a route
  *    
  *    				1 = found a route
  *
  *    				2 = mega already at target
  *    
  *    				3 = failed to find a route because target was on a line
  *    
  ****************************************************************************/
	int32		routeGot = 0;
	int32		level;
	int32		changed;

	if ((startX == targetX) && (startY == targetY)) 
		routeGot = 2;

	else	// 'else' added by JEL (23jan96) otherwise 'routeGot' affected even when already set to '2' above - causing some 'turns' to walk downwards on the spot
		routeGot = CheckTarget(targetX,targetY);// returns 3 if target on a line ( +- 1 pixel )


	if (routeGot == 0) //still looking for a route check if target is within a pixel of a line 
	{
		// scan through the nodes linking each node to its nearest neighbour until no more nodes change
		// This is the routine that finds a route using Scan()
		level = 1;
		do
		{
			changed = Scan(level);
			level =level + 1;
		}
		while(changed == 1);

		// Check to see if the route reached the target
		if (node[nnodes].dist < 9999)
		{
			routeGot = 1;
			ExtractRoute();	// it did so extract the route as nodes and the directions to go between each node
											// route.X,route.Y and route.Dir now hold all the route infomation with the target dir or route continuation
		}
	}

	return routeGot;
}


/*******************************************************************************
 *******************************************************************************
 * 														THE SLIDY PATH ROUTINES
 *******************************************************************************
 *******************************************************************************/


int32 SmoothestPath()
{
/*
 *	This is the second big part of the route finder and the the only bit that tries to be clever
 *	(the other bits are clever).
 *  This part of the autorouter creates a list of modules from a set of lines running across the screen
 *	The task is complicated by two things;
 *	Firstly in choosing a route through the maze of nodes	the routine tries to minimise	the amount of each
 *	individual turn avoiding 90 degree and greater turns (where possible) and reduces the total number of
 *	turns (subject to two 45 degree turns being better than one 90 degree turn).
 *  Secondly when walking in a given direction the number of steps required to reach the end of that run
 *	is not calculated accurately. This is because I was unable to derive a function to relate number of
 *	steps taken between two points to the shrunken step size   
 *
 */
	int32	p;     
	int32	dirS;
	int32	dirD;
	int32	dS;
	int32	dD;
	int32	dSS;
	int32	dSD;
	int32	dDS;
	int32	dDD;
	int32	SS;
	int32	SD;
	int32	DS;
	int32	DD;
	int32	i;
	int32	j;
	int32	temp;
	int32	steps;
	int32	option;
	int32	options;
	int32	lastDir;
	int32	nextDirS;
	int32	nextDirD;
	int32	tempturns[4];     
	int32	turns[4];     
	int32	turntable[NO_DIRECTIONS] = {0,1,3,5,7,5,3,1};

	// route.X route.Y and route.Dir start at far end
	smoothPath[0].x		= startX;
	smoothPath[0].y		= startY;
	smoothPath[0].dir	= startDir;
	smoothPath[0].num	= 0;
  p = 0;
  lastDir = startDir;
  // for each section of the route
  do
  {     

		dirS = route[p].dirS;
		dirD = route[p].dirD;
		nextDirS = route[p+1].dirS;
		nextDirD = route[p+1].dirD;

		// Check directions into and out of a pair of nodes
		// going in
		dS = dirS - lastDir;
		if ( dS < 0)
			dS = dS + NO_DIRECTIONS;

		dD = dirD - lastDir;
		if ( dD < 0)
			dD = dD + NO_DIRECTIONS;

		// coming out
		dSS = dirS - nextDirS;
		if ( dSS < 0)
			dSS = dSS + NO_DIRECTIONS;

		dDD = dirD - nextDirD;
		if ( dDD < 0)
			dDD = dDD + NO_DIRECTIONS;

		dSD = dirS - nextDirD;
		if ( dSD < 0)
			dSD = dSD + NO_DIRECTIONS;

		dDS = dirD - nextDirS;
		if ( dDS < 0)
			dDS = dDS + NO_DIRECTIONS;

		// Determine the amount of turning involved in each possible path 
		dS	= turntable[dS];
		dD	= turntable[dD];
		dSS = turntable[dSS];
		dDD = turntable[dDD];
		dSD	= turntable[dSD];
		dDS	= turntable[dDS];
		// get the best path out ie assume next section uses best direction 
		if (dSD < dSS)
		{
			dSS = dSD;
		}
		if (dDS < dDD)
		{
			dDD = dDS;
		}
		// rate each option
		SS = dS + dSS + 3;		// Split routes look crap so weight against them
		SD = dS + dDD;
		DS = dD + dSS;
		DD = dD + dDD + 3;
		// set up turns as a sorted	array of the turn values
		tempturns[0] = SS;
		turns[0] = 0;
		tempturns[1] = SD;
		turns[1] = 1;
		tempturns[2] = DS;
		turns[2] = 2;
		tempturns[3] = DD;
		turns[3] = 3;
		i = 0;
		do 
		{
			j = 0;
			do 
			{
				if (tempturns[j] > tempturns[j + 1])
				{
					temp			= turns[j];
					turns[j]		= turns[j+1];
					turns[j+1]		= temp;
					temp			= tempturns[j];
					tempturns[j]	= tempturns[j+1];
					tempturns[j+1]	= temp;
				}
				j = j + 1;
			}
			while (j < 3); 
			i = i + 1;
		}
		while (i < 3);

		// best option matched in order of the priority we would like to see on the screen
		// but each option must be checked to see if it can be walked

		options = NewCheck(1, route[p].x, route[p].y, route[p + 1].x, route[p + 1].y);

#ifdef _SWORD2_DEBUG
		if (options == 0)
		{
			Zdebug("BestTurns fail %d %d %d %d",route[p].x, route[p].y, route[p + 1].x, route[p + 1].y);
			Zdebug("BestTurns fail %d %d %d %d",turns[0],turns[1],turns[2],options);
			Con_fatal_error("BestTurns failed (%s line %u)",__FILE__,__LINE__);
		}
#endif

		i = 0; 
		steps = 0; 
		do
		{
			option = 1 << turns[i];
			if (option & options)
				steps = SmoothCheck(turns[i],p,dirS,dirD);
			i = i + 1;
		}
		while ((steps == 0) && (i < 4));

#ifdef PLOT_PATHS	// plot the best path
	if (steps != 0)
	{
		i = 0; 
		do
		{
			RouteLine(smoothPath[i].x, smoothPath[i].y, smoothPath[i+1].x, smoothPath[i+1].y, 228);
			i = i + 1;
		}
		while (i < steps);
	}
#endif


#ifdef _SWORD2_DEBUG
		if (steps == 0)
		{
			Zdebug("BestTurns failed %d %d %d %d",route[p].x, route[p].y, route[p + 1].x, route[p + 1].y);
			Zdebug("BestTurns failed %d %d %d %d",turns[0],turns[1],turns[2],options);
			Con_fatal_error("BestTurns failed (%s line %u)",__FILE__,__LINE__);
		}
#endif

		// route.X route.Y route.dir and bestTurns start at far end
		p = p + 1;


	}
	while (p < (routeLength));
	// best turns will end heading as near as possible to target dir rest is down to anim for now
	smoothPath[steps].dir = 9;
	smoothPath[steps].num = ROUTE_END_FLAG;
		return 1;				 
}




int32 SmoothCheck(int32 best, int32 p, int32 dirS, int32 dirD)
/****************************************************************************
 *	Slip sliding away
 *  This path checker checks to see if a walk that exactly follows the path
 *  would be valid. This should be inherently true for atleast one of the turn
 *	options.
 *  No longer checks the data it only creates the smoothPath array JPS
 ****************************************************************************/
{
	static	int32  k;   
	int32	tempK;   
	int32	x;   
	int32	y;   
	int32	x2;   
	int32	y2;   
	int32	ldx;   
	int32	ldy;   
	int32	dsx;
	int32	dsy;
	int32	ddx;
	int32	ddy;
	int32	dirX;
	int32	dirY;
	int32	ss0;
	int32	ss1;
	int32	ss2;
	int32	sd0;
	int32	sd1;
	int32	sd2;


	if (p == 0)
	{
		k = 1;
	}
	tempK = 0;
	x = route[p].x;
	y = route[p].y;
	x2 = route[p + 1].x;
	y2 = route[p + 1].y;
	ldx = x2 - x;
	ldy = y2 - y;
	dirX = 1;
	dirY = 1;
	if (ldx < 0)
	{
		ldx = -ldx;
		dirX = -1;
	}

	if (ldy < 0)
	{
		ldy = -ldy;
		dirY = -1;
	}

// set up sd0-ss2 to reflect possible movement in each direction
		if ((dirS == 0)	|| (dirS == 4))// vert and diag
		{
		  ddx = ldx;
			ddy = (ldx*diagonaly)/diagonalx;
			dsy = ldy - ddy;
			ddx = ddx * dirX;
			ddy = ddy * dirY;
			dsy = dsy * dirY;
			dsx = 0;

			sd0 = (ddx + modX[dirD]/2)/ modX[dirD];
			ss0 = (dsy + modY[dirS]/2) / modY[dirS];
			sd1 = sd0/2;
			ss1 = ss0/2;
			sd2 = sd0 - sd1;
			ss2 = ss0 - ss1;
		}
		else
		{
		  ddy = ldy;
			ddx = (ldy*diagonalx)/diagonaly;
			dsx = ldx - ddx;
			ddy = ddy * dirY;
			ddx = ddx * dirX;
			dsx = dsx * dirX;
			dsy = 0;

			sd0 = (ddy + modY[dirD]/2)/ modY[dirD];
			ss0 = (dsx + modX[dirS]/2)/ modX[dirS];
			sd1 = sd0/2;
			ss1 = ss0/2;
			sd2 = sd0 - sd1;
			ss2 = ss0 - ss1;
		}

			if (best == 0) //halfsquare, diagonal,	halfsquare
			{
							smoothPath[k].x		= x+dsx/2;
							smoothPath[k].y		= y+dsy/2;
							smoothPath[k].dir	= dirS;
							smoothPath[k].num	= ss1;
							k = k + 1;
							smoothPath[k].x		= x+dsx/2+ddx;
							smoothPath[k].y		= y+dsy/2+ddy;
							smoothPath[k].dir	= dirD;
							smoothPath[k].num	= sd0;
							k = k + 1;
							smoothPath[k].x		= x+dsx+ddx;
							smoothPath[k].y		= y+dsy+ddy;
							smoothPath[k].dir	= dirS;
							smoothPath[k].num	= ss2;
							k = k + 1;
							tempK = k;
			}
			else if (best == 1) //square, diagonal
			{
						smoothPath[k].x = x+dsx;
						smoothPath[k].y = y+dsy;
						smoothPath[k].dir = dirS;
						smoothPath[k].num = ss0;
						k = k + 1;
						smoothPath[k].x = x2;
						smoothPath[k].y = y2;
						smoothPath[k].dir = dirD;
						smoothPath[k].num = sd0;
						k = k + 1;
						tempK = k;
			}
			else if (best == 2) //diagonal square
			{
						smoothPath[k].x = x+ddx;
						smoothPath[k].y = y+ddy;
						smoothPath[k].dir = dirD;
						smoothPath[k].num = sd0;
						k = k + 1;
						smoothPath[k].x = x2;
						smoothPath[k].y = y2;
						smoothPath[k].dir = dirS;
						smoothPath[k].num = ss0;
						k = k + 1;
						tempK = k;
			}
			else //halfdiagonal, square, halfdiagonal
			{
							smoothPath[k].x = x+ddx/2;
							smoothPath[k].y = y+ddy/2;
							smoothPath[k].dir = dirD;
							smoothPath[k].num = sd1;
							k = k + 1;
							smoothPath[k].x = x+dsx+ddx/2;
							smoothPath[k].y = y+dsy+ddy/2;
							smoothPath[k].dir = dirS;
							smoothPath[k].num = ss0;
							k = k + 1;
							smoothPath[k].x = x2;
							smoothPath[k].y = y2;
							smoothPath[k].dir = dirD;
							smoothPath[k].num = sd2;
							k = k + 1;
							tempK = k;
			}

	return tempK;	
}

int32 SlidyPath()
{
/****************************************************************************
 *  SlidyPath creates a path based on part steps with no sliding to get
 *	as near as possible to the target without any sliding this routine is
 *	currently unused, but is intended for use when just clicking about.
 *
 *	produce a module list from the line data
 *
 ****************************************************************************/
int32 smooth;
int32 slidy;
int32 scale;
int32 stepX;
int32 stepY;
int32 deltaX;
int32 deltaY;

	// strip out the short sections
	slidy = 1;
	smooth = 1;
	modularPath[0].x = smoothPath[0].x;
	modularPath[0].y = smoothPath[0].y;
	modularPath[0].dir = smoothPath[0].dir;
	modularPath[0].num = 0;

	while (smoothPath[smooth].num < ROUTE_END_FLAG)
	{
		scale = scaleA * smoothPath[smooth].y + scaleB;
		deltaX = smoothPath[smooth].x - modularPath[slidy-1].x;
		deltaY = smoothPath[smooth].y - modularPath[slidy-1].y;
		stepX = modX[smoothPath[smooth].dir];
		stepY = modY[smoothPath[smooth].dir];
		stepX = stepX * scale;
		stepY = stepY * scale;
		stepX = stepX >> 19;// quarter a step minimum
		stepY = stepY >> 19;
		if ((abs(deltaX)>=abs(stepX)) &&	(abs(deltaY)>=abs(stepY)))
		{									
	 		modularPath[slidy].x = smoothPath[smooth].x;
			modularPath[slidy].y = smoothPath[smooth].y;
			modularPath[slidy].dir = smoothPath[smooth].dir;
			modularPath[slidy].num = 1;
			slidy += 1;
		}
		smooth += 1;
	}
	// in	case the last bit had no steps
	if (slidy > 1)
	{
		modularPath[slidy-1].x = smoothPath[smooth-1].x;
		modularPath[slidy-1].y = smoothPath[smooth-1].y;
	}
	// set up the end of the walk
	modularPath[slidy].x = smoothPath[smooth-1].x;
	modularPath[slidy].y = smoothPath[smooth-1].y;
	modularPath[slidy].dir = targetDir;
	modularPath[slidy].num = 0;
	slidy += 1;
	modularPath[slidy].x = smoothPath[smooth-1].x;
	modularPath[slidy].y = smoothPath[smooth-1].y;
	modularPath[slidy].dir = 9;
	modularPath[slidy].num = ROUTE_END_FLAG;
	return 1;
}

//****************************************************************************
// SLOW IN

int32 AddSlowInFrames(_walkData *walkAnim)
{
	uint32 slowInFrameNo;


	if ((usingSlowInFrames) && (modularPath[1].num > 0))
	{
		for (slowInFrameNo=0; slowInFrameNo<numberOfSlowInFrames[currentDir]; slowInFrameNo++)
		{
			walkAnim[stepCount].frame	= firstSlowInFrame[currentDir] + slowInFrameNo;
			walkAnim[stepCount].step	= 0;
			walkAnim[stepCount].dir		= currentDir;
			walkAnim[stepCount].x			= moduleX;
			walkAnim[stepCount].y			= moduleY;
			stepCount += 1;
		}
		return(1);
	}
	else
	{
		return(0);
	}
}
//----------------------------------------------------------------------------
void EarlySlowOut(Object_mega *ob_mega, Object_walkdata *ob_walkdata)
{
	int32 slowOutFrameNo;
	int32 walk_pc;
	_walkData *walkAnim;

	
	//Zdebug("\nEARLY SLOW-OUT");

	LoadWalkData(ob_walkdata);

	//Zdebug("********************************");
	//Zdebug("framesPerStep              =%d",framesPerStep);								// 6;
	//Zdebug("numberOfSlowOutFrames      =%d",numberOfSlowOutFrames);				// 7;
	//Zdebug("firstWalkingTurnLeftFrame  =%d",firstWalkingTurnLeftFrame);		// 120;
	//Zdebug("firstWalkingTurnRightFrame =%d",firstWalkingTurnRightFrame);	// 216;
	//Zdebug("firstSlowOutFrame          =%d",firstSlowOutFrame);						// 344;
	//Zdebug("********************************");


 	walk_pc  = ob_mega->walk_pc;

	walkAnim = LockRouteMem();	// lock the _walkData array (NB. AFTER loading walkgrid & walkdata!)


	if (usingSlowOutFrames)	// if this mega does actually have slow-out frames
	{
		do	// overwrite the next step (half a cycle) of the walk (ie .step - 0..5
		{
			//Zdebug("\nSTEP NUMBER:    walkAnim[%d].step  = %d",walk_pc,walkAnim[walk_pc].step);
			//Zdebug("ORIGINAL FRAME: walkAnim[%d].frame = %d",walk_pc,walkAnim[walk_pc].frame);
			// map from existing walk frame across to correct frame number of slow-out - remember, there may be more slow-out frames than walk-frames!

			if (walkAnim[walk_pc].frame >= firstWalkingTurnRightFrame)	// if it's a walking turn-right, rather than a normal step
			{
				walkAnim[walk_pc].frame -= firstWalkingTurnRightFrame;		// then map it to a normal step frame first
				//Zdebug("MAPPED TO WALK: walkAnim[%d].frame = %d  (walking turn-right frame --> walk frame)",walk_pc,walkAnim[walk_pc].frame);
			}

			else if (walkAnim[walk_pc].frame >= firstWalkingTurnLeftFrame)	// if it's a walking turn-left, rather than a normal step
			{
				walkAnim[walk_pc].frame -= firstWalkingTurnLeftFrame;		// then map it to a normal step frame first
				//Zdebug("MAPPED TO WALK: walkAnim[%d].frame = %d  (walking turn-left frame --> walk frame)",walk_pc,walkAnim[walk_pc].frame);
			}

			walkAnim[walk_pc].frame += firstSlowOutFrame + ((walkAnim[walk_pc].frame / framesPerStep) * (numberOfSlowOutFrames-framesPerStep));
			walkAnim[walk_pc].step = 0;
			//Zdebug("SLOW-OUT FRAME: walkAnim[%d].frame = %d",walk_pc,walkAnim[walk_pc].frame);
			walk_pc += 1;
		}
		while(walkAnim[walk_pc].step > 0 );

		//Zdebug("\n");

		for (slowOutFrameNo=framesPerStep; slowOutFrameNo < numberOfSlowOutFrames; slowOutFrameNo++)	// add stationary frame(s) (OPTIONAL)
		{
			walkAnim[walk_pc].frame = walkAnim[walk_pc-1].frame + 1;
			//Zdebug("EXTRA FRAME:    walkAnim[%d].frame = %d",walk_pc,walkAnim[walk_pc].frame);
			walkAnim[walk_pc].step = 0;
			walkAnim[walk_pc].dir = walkAnim[walk_pc-1].dir;
			walkAnim[walk_pc].x = walkAnim[walk_pc-1].x;
			walkAnim[walk_pc].y = walkAnim[walk_pc-1].y;
			walk_pc++;
		}
	}
	else	// this mega doesn't have slow-out frames
	{
		walkAnim[walk_pc].frame	= firstStandFrame + walkAnim[walk_pc-1].dir;	// stand in current direction
		walkAnim[walk_pc].step	= 0;
		walkAnim[walk_pc].dir		= walkAnim[walk_pc-1].dir;
		walkAnim[walk_pc].x			= walkAnim[walk_pc-1].x;
		walkAnim[walk_pc].y			= walkAnim[walk_pc-1].y;
		walk_pc++;
	}

	walkAnim[walk_pc].frame	= 512;	// end of sequence
	walkAnim[walk_pc].step	= 99;	// so that this doesn't happen again while 'george_walking' is still '2'
}
//----------------------------------------------------------------------------
// SLOW OUT

void AddSlowOutFrames(_walkData *walkAnim)
{
	int32 slowOutFrameNo;


 	if ((usingSlowOutFrames)&&(lastCount>=framesPerStep))	// if the mega did actually walk, we overwrite the last step (half a cycle) with slow-out frames + add any necessary stationary frames
	{
		// place stop frames here
		// slowdown at the end of the last walk
	
		slowOutFrameNo = lastCount - framesPerStep;


		//Zdebug("SLOW OUT: slowOutFrameNo(%d) = lastCount(%d) - framesPerStep(%d)",slowOutFrameNo,lastCount,framesPerStep);
	
		do	// overwrite the last step (half a cycle) of the walk
		{
			// map from existing walk frame across to correct frame number of slow-out - remember, there may be more slow-out frames than walk-frames!
			walkAnim[slowOutFrameNo].frame += firstSlowOutFrame + ((walkAnim[slowOutFrameNo].frame / framesPerStep) * (numberOfSlowOutFrames-framesPerStep));
			walkAnim[slowOutFrameNo].step = 0;	// because no longer a normal walk-step
			//Zdebug("walkAnim[%d].frame = %d",slowOutFrameNo,walkAnim[slowOutFrameNo].frame);
			slowOutFrameNo += 1;
		}
		while(slowOutFrameNo < lastCount );
	
		for (slowOutFrameNo=framesPerStep; slowOutFrameNo < numberOfSlowOutFrames; slowOutFrameNo++)	// add stationary frame(s) (OPTIONAL)
		{
			walkAnim[stepCount].frame = walkAnim[stepCount-1].frame + 1;
			//Zdebug("EXTRA FRAMES: walkAnim[%d].frame = %d",stepCount,walkAnim[stepCount].frame);
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir	= walkAnim[stepCount-1].dir;
			walkAnim[stepCount].x		= walkAnim[stepCount-1].x;
			walkAnim[stepCount].y		= walkAnim[stepCount-1].y;
			stepCount += 1;
		}
	}
}
//----------------------------------------------------------------------------

void SlidyWalkAnimator(_walkData *walkAnim)
/****************************************************************************
 *  Skidding every where HardWalk creates an animation that exactly fits the
 *	smoothPath and uses foot slipping to fit whole steps into the route
 *	Parameters: georgeg,mouseg
 *	Returns:		rout 
 *
 *	produce a module list from the line data
 *
 ****************************************************************************/
{
	static int32 left = 0;
	int32 p;
	int32 lastDir;
	int32 lastRealDir;
	int32 turnDir;
	int32 scale;
	int32 step;
	int32 module;
	int32 moduleEnd;
	int32 module16X;
	int32 module16Y;
	int32 stepX;
	int32 stepY;
	int32 errorX;
	int32 errorY;
	int32 lastErrorX;
	int32 lastErrorY;
	int32 frameCount;
	int32 frames;


	p = 0;
	lastDir = modularPath[0].dir;
	currentDir = modularPath[1].dir;
	if (currentDir == NO_DIRECTIONS)
	{
		currentDir = lastDir;
	}
	moduleX = startX;
	moduleY = startY;
	module16X = moduleX << 16;
	module16Y = moduleY << 16;
	stepCount = 0;

	//****************************************************************************
	// SLIDY
	// START THE WALK WITH THE FIRST STANDFRAME THIS MAY CAUSE A DELAY
	// BUT IT STOPS THE PLAYER MOVING FOR COLLISIONS ARE DETECTED
	//****************************************************************************
	//Zdebug("\nSLIDY: STARTING THE WALK");
	module =	framesPerChar + lastDir;
	walkAnim[stepCount].frame = module;
	walkAnim[stepCount].step = 0;
	walkAnim[stepCount].dir = lastDir;
	walkAnim[stepCount].x = moduleX;
	walkAnim[stepCount].y = moduleY;
	stepCount += 1;

	//****************************************************************************
	// SLIDY
	// TURN TO START THE WALK
	//****************************************************************************
	//Zdebug("\nSLIDY: TURNING TO START THE WALK");
	// rotate if we need to
	if (lastDir != currentDir)
	{
		// get the direction to turn
		turnDir = currentDir - lastDir;
		if ( turnDir < 0)
				turnDir += NO_DIRECTIONS;

		if (turnDir > 4)
			turnDir = -1;
		else if (turnDir > 0)
			turnDir = 1;

		// rotate to new walk direction
		// for george and nico put in a head turn at the start
		if (usingStandingTurnFrames)
		{
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				module =	firstStandingTurnLeftFrame + lastDir;
			}
			else
			{
				module =	firstStandingTurnRightFrame + lastDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}

		// rotate till were facing new dir then go back 45 degrees
		while (lastDir != currentDir)
		{
			lastDir += turnDir;
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				if ( lastDir < 0)
						lastDir += NO_DIRECTIONS;
				module =	firstStandingTurnLeftFrame + lastDir;
			}
			else
			{
				if ( lastDir > 7)
						lastDir -= NO_DIRECTIONS;
				module =	firstStandingTurnRightFrame + lastDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}
		// the back 45 degrees bit
		stepCount -= 1;// step back one because new head turn for george takes us past the new dir
	}
	// his head is in the right direction
	lastRealDir = currentDir;

	//****************************************************************************
	// SLIDY: THE SLOW IN

	AddSlowInFrames(walkAnim);
	//****************************************************************************

	//****************************************************************************
	// SLIDY
	// THE WALK
	//****************************************************************************

	//Zdebug("\nSLIDY: THE WALK");

	//---------------------------------------------------
	// start the walk on the left or right leg, depending on how the slow-in frames were drawn

	if (leadingLeg[currentDir]==0)	// (0=left; 1=right)
		left = 0;							//  start the walk on the left leg (ie. at beginning of the first step of the walk cycle)
	else
		left = framesPerStep;	//  start the walk on the right leg (ie. at beginning of the second step of the walk cycle)
	//---------------------------------------------------


	lastCount = stepCount;
	lastDir = 99;// this ensures that we don't put in turn frames for the start		
	currentDir = 99;// this ensures that we don't put in turn frames for the start		
	do
	{
		assert(stepCount < O_WALKANIM_SIZE);
		while (modularPath[p].num == 0)
		{
			p = p + 1;
			if (currentDir != 99)
				lastRealDir = currentDir;
			lastDir = currentDir;
			lastCount = stepCount;
		}
		//calculate	average amount to lose in each step on the way to the next node
		currentDir = modularPath[p].dir;
		if (currentDir < NO_DIRECTIONS)
		{
			module =	currentDir * framesPerStep * 2 + left;
			if (left == 0)
			 left = framesPerStep;
			else
			 left = 0;
			moduleEnd = module + framesPerStep;
			step = 0;
			scale = (scaleA * moduleY + scaleB);
			do
			{
				module16X += dx[module]*scale;
				module16Y += dy[module]*scale;
				moduleX = module16X >> 16;
				moduleY = module16Y >> 16;
				walkAnim[stepCount].frame = module;
				walkAnim[stepCount].step = step;	// normally 0,1,2,3,4,5,0,1,2,etc
				walkAnim[stepCount].dir = currentDir;
				walkAnim[stepCount].x = moduleX;
				walkAnim[stepCount].y = moduleY;
				stepCount += 1;
				step += 1;
				module += 1;
			}
			while( module < moduleEnd) ;
			stepX = modX[modularPath[p].dir];
			stepY = modY[modularPath[p].dir];
			errorX = modularPath[p].x -	moduleX;
			errorX = errorX * stepX;
			errorY = modularPath[p].y -	moduleY;
			errorY = errorY * stepY;
			if ((errorX < 0) || (errorY < 0))
			{
				modularPath[p].num = 0;	// the end of the path
				// okay those last steps took us past our target but do we want to scoot or moonwalk
				frames = stepCount - lastCount;
				errorX = modularPath[p].x - walkAnim[stepCount-1].x;
				errorY = modularPath[p].y - walkAnim[stepCount-1].y;

				if (frames > framesPerStep)
				{
					lastErrorX = modularPath[p].x - walkAnim[stepCount-7].x;
					lastErrorY = modularPath[p].y - walkAnim[stepCount-7].y;
					if (stepX==0)
					{
						if (3*abs(lastErrorY) < abs(errorY)) //the last stop was closest
						{
							stepCount -= framesPerStep;
							if (left == 0)
						 		left = framesPerStep;
							else
							 	left = 0;
						}
					}
					else
					{
						if (3*abs(lastErrorX) < abs(errorX)) //the last stop was closest
						{
							stepCount -= framesPerStep;
							if (left == 0)
						 		left = framesPerStep;
							else
							 	left = 0;
						}
					}
				}
				errorX = modularPath[p].x - walkAnim[stepCount-1].x;
				errorY = modularPath[p].y - walkAnim[stepCount-1].y;
				// okay we've reached the end but we still have an error
				if (errorX != 0)
				{
					frameCount = 0;
					frames = stepCount - lastCount;
					do
					{
						frameCount += 1;
						walkAnim[lastCount + frameCount - 1].x +=	errorX*frameCount/frames;
					}
					while(frameCount<frames);	
				}
				if (errorY != 0)
				{
					frameCount = 0;
					frames = stepCount - lastCount;
					do
					{
						frameCount += 1;
						walkAnim[lastCount + frameCount-1].y +=	errorY*frameCount/frames;
					}
					while(frameCount<frames);	
				}
				// Now is the time to put in the turn frames for the last turn
				if (frames < framesPerStep)
					currentDir = 99;// this ensures that we don't put in turn frames for this walk or the next		
				if (currentDir != 99)
					lastRealDir = currentDir;
				// check each turn condition in turn
				if (((lastDir != 99) && (currentDir != 99)) && (usingWalkingTurnFrames)) // only for george
				{	
				  lastDir = currentDir - lastDir;//1 and -7 going right -1 and 7 going left
					if (((lastDir == -1) || (lastDir == 7)) || ((lastDir == -2) || (lastDir == 6)))
					{
						// turn at the end of the last walk
						frame = lastCount - framesPerStep;
						do
						{
							walkAnim[frame].frame += firstWalkingTurnLeftFrame;	//was 104;	//turning left 
							frame += 1;
						}
						while(frame < lastCount );
					}
					if (((lastDir == 1) || (lastDir == -7)) || ((lastDir == 2) || (lastDir == -6)))
					{	
						// turn at the end of the current walk
						frame = lastCount - framesPerStep;
						do
						{
							walkAnim[frame].frame += firstWalkingTurnRightFrame;	// was 200;	// turning right
							frame += 1;
						}
						while(frame < lastCount );
					}
					lastDir = currentDir;
				}
				// all turns checked

				lastCount = stepCount;
				moduleX = walkAnim[stepCount-1].x;
				moduleY =	walkAnim[stepCount-1].y;
				module16X = moduleX << 16;
				module16Y = moduleY << 16;
			}
		}
	}
	while (modularPath[p].dir < NO_DIRECTIONS);



#ifdef _SWORD2_DEBUG
	if (lastRealDir == 99)
	{
		Con_fatal_error("SlidyWalkAnimatorlast direction error (%s line %u)",__FILE__,__LINE__);
	}
#endif

	//****************************************************************************
	// SLIDY: THE SLOW OUT
	AddSlowOutFrames(walkAnim);

	//****************************************************************************
	// SLIDY
	// TURNS TO END THE WALK ?
	//****************************************************************************

	// We've done the walk now put in any turns at the end


	if (targetDir == 8)	// ANY direction -> stand in the last direction
	{
		module =	firstStandFrame + lastRealDir;
		targetDir =	lastRealDir;
		walkAnim[stepCount].frame = module;
		walkAnim[stepCount].step = 0;
		walkAnim[stepCount].dir = lastRealDir;
		walkAnim[stepCount].x = moduleX;
		walkAnim[stepCount].y = moduleY;
		stepCount += 1;
	}
	if (targetDir == 9)	// 'stance' was non-zero
	{
		if (stepCount == 0)
		{
			module =	framesPerChar + lastRealDir;
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastRealDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}
	}
	else if (targetDir != lastRealDir) // rotate to targetDir
	{
		// rotate to target direction
		turnDir = targetDir - lastRealDir;
		if ( turnDir < 0)
				turnDir += NO_DIRECTIONS;

		if (turnDir > 4)
			turnDir = -1;
		else if (turnDir > 0)
			turnDir = 1;

		// rotate to target direction
		// for george and nico put in a head turn at the start
		if (usingStandingTurnFrames)
		{
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				module =	firstStandingTurnLeftFrame + lastDir;
			}
			else
			{
				module =	firstStandingTurnRightFrame + lastDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastRealDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}

		// rotate if we need to
		while (lastRealDir != targetDir)
		{
			lastRealDir += turnDir;
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				if ( lastRealDir < 0)
						lastRealDir += NO_DIRECTIONS;
				module =	firstStandingTurnLeftFrame + lastRealDir;
			}
			else
			{
				if ( lastRealDir > 7)
						lastRealDir -= NO_DIRECTIONS;
				module =	firstStandingTurnRightFrame + lastRealDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastRealDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}
		module =	firstStandFrame + lastRealDir;
		walkAnim[stepCount-1].frame = module;
	}
	else // just stand at the end
	{
		module =	firstStandFrame + lastRealDir;
		walkAnim[stepCount].frame = module;
		walkAnim[stepCount].step = 0;
		walkAnim[stepCount].dir = lastRealDir;
		walkAnim[stepCount].x = moduleX;
		walkAnim[stepCount].y = moduleY;
		stepCount += 1;
	}

	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;
	stepCount += 1;
	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;
	stepCount += 1;
	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;


	//-------------------------------------------
	// write all the frames to "debug.txt"

	//Zdebug("\nTHE WALKDATA:");
	for (frame=0; frame<=stepCount; frame++)
	{
		//Zdebug("walkAnim[%d].frame=%d",frame,walkAnim[frame].frame);
	}
	//-------------------------------------------


//	Zdebug("RouteFinder RouteSize is %d", stepCount);
	return;
}


/*******************************************************************************
 *******************************************************************************
 * 														THE SOLID PATH ROUTINES
 *******************************************************************************
 *******************************************************************************/

int32 SolidPath()
{
/****************************************************************************
 *  SolidPath creates a path based on whole steps with no sliding to get
 *	as near as possible to the target without any sliding this routine is
 *	currently unused, but is intended for use when just clicking about.
 *
 *	produce a module list from the line data
 *
 ****************************************************************************/
int32 smooth;
int32 solid;
int32 scale;
int32 stepX;
int32 stepY;
int32 deltaX;
int32 deltaY;

	// strip out the short sections
	solid = 1;
	smooth = 1;
	modularPath[0].x = smoothPath[0].x;
	modularPath[0].y = smoothPath[0].y;
	modularPath[0].dir = smoothPath[0].dir;
	modularPath[0].num = 0;

	do
	{
		scale = scaleA * smoothPath[smooth].y + scaleB;
		deltaX = smoothPath[smooth].x - modularPath[solid-1].x;
		deltaY = smoothPath[smooth].y - modularPath[solid-1].y;
		stepX = modX[smoothPath[smooth].dir];
		stepY = modY[smoothPath[smooth].dir];
		stepX = stepX * scale;
		stepY = stepY * scale;
		stepX = stepX >> 16;
		stepY = stepY >> 16;
		if ((abs(deltaX)>=abs(stepX)) &&	(abs(deltaY)>=abs(stepY)))
		{
			modularPath[solid].x = smoothPath[smooth].x;
			modularPath[solid].y = smoothPath[smooth].y;
			modularPath[solid].dir = smoothPath[smooth].dir;
			modularPath[solid].num = 1;
			solid += 1;
		}
		smooth += 1;
	}
	while (smoothPath[smooth].num < ROUTE_END_FLAG);
	// in	case the last bit had no steps
	if (solid == 1) //there were no paths so put in a dummy end
	{
			solid = 2;
			modularPath[1].dir = smoothPath[0].dir;
			modularPath[1].num = 0;
	}	
	modularPath[solid-1].x = smoothPath[smooth-1].x;
	modularPath[solid-1].y = smoothPath[smooth-1].y;
	// set up the end of the walk
	modularPath[solid].x = smoothPath[smooth-1].x;
	modularPath[solid].y = smoothPath[smooth-1].y;
	modularPath[solid].dir = 9;
	modularPath[solid].num = ROUTE_END_FLAG;
	return 1;

}

int32 SolidWalkAnimator(_walkData *walkAnim)
{
/****************************************************************************
 *  SolidWalk creates an animation based on whole steps with no sliding to get
 *	as near as possible to the target without any sliding this routine is
 *	is intended for use when just clicking about.
 *
 *	produce a module list from the line data
 *
 *	returns 0 if solid route not found
 ****************************************************************************/

	int32 p;
	int32 i;
	int32 left;
	int32 lastDir;
	int32 turnDir;
	int32 scale;
	int32 step;
	int32 module;
	int32 module16X;
	int32 module16Y;
	int32 errorX;
	int32 errorY;
	int32 moduleEnd;
	int32 slowStart=0;



	// start at the beginning for a change
	lastDir = modularPath[0].dir;
	p = 1;
	currentDir = modularPath[1].dir;
	module =	framesPerChar + lastDir;
	moduleX = startX;
	moduleY = startY;
	module16X = moduleX << 16;
	module16Y = moduleY << 16;
	stepCount = 0;

	//****************************************************************************
	// SOLID
	// START THE WALK WITH THE FIRST STANDFRAME THIS MAY CAUSE A DELAY
	// BUT IT STOPS THE PLAYER MOVING FOR COLLISIONS ARE DETECTED
	//****************************************************************************

	//Zdebug("\nSOLID: STARTING THE WALK");
	walkAnim[stepCount].frame = module;
	walkAnim[stepCount].step = 0;
	walkAnim[stepCount].dir = lastDir;
	walkAnim[stepCount].x = moduleX;
	walkAnim[stepCount].y = moduleY;
	stepCount += 1;

	//****************************************************************************
	// SOLID
	// TURN TO START THE WALK
	//****************************************************************************
	//Zdebug("\nSOLID: TURNING TO START THE WALK");
	// rotate if we need to
	if (lastDir != currentDir)
	{
		// get the direction to turn
		turnDir = currentDir - lastDir;
		if ( turnDir < 0)
				turnDir += NO_DIRECTIONS;

		if (turnDir > 4)
			turnDir = -1;
		else if (turnDir > 0)
			turnDir = 1;

		// rotate to new walk direction
		// for george and nico put in a head turn at the start
		if (usingStandingTurnFrames)
		{
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				module =	firstStandingTurnLeftFrame + lastDir;
			}
			else
			{
				module =	firstStandingTurnRightFrame + lastDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}

		// rotate till were facing new dir then go back 45 degrees
		while (lastDir != currentDir)
		{
			lastDir += turnDir;
			if ( turnDir < 0)	// new frames for turn frames	29oct95jps
			{
				if ( lastDir < 0)
						lastDir += NO_DIRECTIONS;
				module =	firstStandingTurnLeftFrame + lastDir;
			}
			else
			{
				if ( lastDir > 7)
						lastDir -= NO_DIRECTIONS;
				module =	firstStandingTurnRightFrame + lastDir;
			}
			walkAnim[stepCount].frame = module;
			walkAnim[stepCount].step = 0;
			walkAnim[stepCount].dir = lastDir;
			walkAnim[stepCount].x = moduleX;
			walkAnim[stepCount].y = moduleY;
			stepCount += 1;
		}
		// the back 45 degrees bit
		stepCount -= 1;// step back one because new head turn for george takes us past the new dir
	}

	//****************************************************************************
	// SOLID: THE SLOW IN

	slowStart = AddSlowInFrames(walkAnim);

	//****************************************************************************
	// SOLID
	// THE WALK
	//****************************************************************************

	//Zdebug("\nSOLID: THE WALK");

	//---------------------------------------------------
	// start the walk on the left or right leg, depending on how the slow-in frames were drawn

	if (leadingLeg[currentDir]==0)	// (0=left; 1=right)
		left = 0;							//  start the walk on the left leg (ie. at beginning of the first step of the walk cycle)
	else
		left = framesPerStep;	//  start the walk on the right leg (ie. at beginning of the second step of the walk cycle)
	//---------------------------------------------------


	lastCount = stepCount;
	lastDir = 99;// this ensures that we don't put in turn frames for the start		
	currentDir = 99;// this ensures that we don't put in turn frames for the start		

	do
	{
		while(modularPath[p].num > 0)
		{
			currentDir = modularPath[p].dir;
			if (currentDir< NO_DIRECTIONS)
			{

				module =	currentDir * framesPerStep * 2 + left;
				if (left == 0)
					left = framesPerStep;
				else
					left = 0;
				moduleEnd = module + framesPerStep;
				step = 0;
				scale = (scaleA * moduleY + scaleB);
				do
				{
					module16X += dx[module]*scale;
					module16Y += dy[module]*scale;
					moduleX = module16X >> 16;
					moduleY = module16Y >> 16;
					walkAnim[stepCount].frame = module;
					walkAnim[stepCount].step = step;	// normally 0,1,2,3,4,5,0,1,2,etc
					walkAnim[stepCount].dir = currentDir;
					walkAnim[stepCount].x = moduleX;
					walkAnim[stepCount].y = moduleY;
					stepCount += 1;
					module += 1;
					step += 1;
				}
				while( module < moduleEnd) ;
				errorX = modularPath[p].x -	moduleX;
				errorX = errorX * modX[modularPath[p].dir];
				errorY = modularPath[p].y -	moduleY;
				errorY = errorY * modY[modularPath[p].dir];
				if ((errorX < 0) || (errorY < 0))
				{
					modularPath[p].num = 0;
					stepCount -= framesPerStep;
					if (left == 0)
						left = framesPerStep;
					else
						left = 0;
					// Okay this is the end of a section
					moduleX = walkAnim[stepCount-1].x;
					moduleY =	walkAnim[stepCount-1].y;
					module16X = moduleX << 16;
					module16Y = moduleY << 16;
					modularPath[p].x =moduleX;
					modularPath[p].y =moduleY;
					// Now is the time to put in the turn frames for the last turn
					if ((stepCount - lastCount) < framesPerStep)// no step taken
					{
						if (slowStart == 1)// clean up if a slow in but no walk
						{
							//stepCount -= 3;
							stepCount -= numberOfSlowInFrames[currentDir];	// (James08sep97)
							//lastCount -= 3;
							lastCount -= numberOfSlowInFrames[currentDir];	// (James08sep97)
							slowStart = 0;
						}
						currentDir = 99;// this ensures that we don't put in turn frames for this walk or the next		
					}
					// check each turn condition in turn
					if (((lastDir != 99) && (currentDir != 99)) && (usingWalkingTurnFrames)) // only for george
					{	
					  lastDir = currentDir - lastDir;//1 and -7 going right -1 and 7 going left
						if (((lastDir == -1) || (lastDir == 7)) || ((lastDir == -2) || (lastDir == 6)))
						{
							// turn at the end of the last walk
							frame = lastCount - framesPerStep;
							do
							{
								walkAnim[frame].frame += firstWalkingTurnLeftFrame;	// was 104;	//turning left 
								frame += 1;
							}
							while(frame < lastCount );
						}
						if (((lastDir == 1) || (lastDir == -7)) || ((lastDir == 2) || (lastDir == -6)))
						{	
							// turn at the end of the current walk
							frame = lastCount - framesPerStep;
							do
							{
								walkAnim[frame].frame += firstWalkingTurnRightFrame;	// was 200;	// turning right
								frame += 1;
							}
							while(frame < lastCount );
						}
					}
					// all turns checked
					lastCount = stepCount;
				}
			}
		}
		p = p + 1;
		lastDir = currentDir;
		slowStart = 0; //can only be valid first time round 
	}
	while (modularPath[p].dir < NO_DIRECTIONS);


	//****************************************************************************
	// SOLID: THE SLOW OUT
	AddSlowOutFrames(walkAnim);

	//****************************************************************************

	module =	framesPerChar + modularPath[p-1].dir;
	walkAnim[stepCount].frame = module;
	walkAnim[stepCount].step = 0;
	walkAnim[stepCount].dir = modularPath[p-1].dir;
	walkAnim[stepCount].x = moduleX;
	walkAnim[stepCount].y = moduleY;
	stepCount += 1;

	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;
	stepCount += 1;
	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;
	stepCount += 1;
	walkAnim[stepCount].frame	= 512;
	walkAnim[stepCount].step	= 99;

	//-------------------------------------------
	// write all the frames to "debug.txt"

	//Zdebug("\nTHE WALKDATA:");
	for (frame=0; frame<=stepCount; frame++)
	{
		//Zdebug("walkAnim[%d].frame=%d",frame,walkAnim[frame].frame);
	}
	//-------------------------------------------

	//****************************************************************************
	// SOLID
	// NO END TURNS
	//****************************************************************************

//	Zdebug("RouteFinder RouteSize is %d", stepCount);
//	now check the route
		i = 0;
		do
		{
			if (!Check(modularPath[i].x, modularPath[i].y, modularPath[i+1].x, modularPath[i+1].y))
				p=0;
		#ifdef PLOT_PATHS
			RouteLine(modularPath[i].x, modularPath[i].y, modularPath[i+1].x, modularPath[i+1].y, 227);
    #endif   
			i += 1;
		}
		while(i<p-1);
		if (p != 0)
		{
			targetDir =	modularPath[p-1].dir;
		}
		if (p != 0)
		{
			if (CheckTarget(moduleX,moduleY) == 3)// new target on a line
			{
				p = 0;
				//Zdebug("Solid walk target was on a line %d %d", moduleX, moduleY);
			}
		}

	return p;
}


/*******************************************************************************
 *******************************************************************************
 * 														THE SCAN ROUTINES
 *******************************************************************************
 *******************************************************************************/

int32 Scan(int32 level)
/*******************************************************************************
 * Called successively from RouteFinder	until no more changes take place in the
 * grid array	ie he best path has been found
 *
 * Scans through every point in the node array and checks if there is a route
 * between each point and if this route gives a new route.
 *
 * This routine could probably halve its processing time if it doubled up on the
 * checks after	each route check
 *
 *******************************************************************************/
{
	int32	i;
	int32	k;
  int32 x1;
  int32 y1;
  int32 x2;
  int32 y2;
	int32	distance;
	int32	changed = 0;
 	// For all the nodes that have new values and a distance less than enddist
	// ie dont check for new routes from a point we checked before or from a point
	// that is already further away than the best route so far. 
	i = 0;
	do
	{
		if ((node[i].dist < node[nnodes].dist) && (node[i].level == level))
		{
			x1 = node[i].x;
			y1 = node[i].y;
			k=nnodes;
			do
			{
				if (node[k].dist > node[i].dist)
				{
					x2 = node[k].x;
					y2 = node[k].y;

					if (abs(x2-x1)>(4.5*abs(y2-y1)))
					{
						distance = (8*abs(x2-x1)+18*abs(y2-y1))/(54*8)+1;
					}
					else
					{
						distance = (6*abs(x2-x1)+36*abs(y2-y1))/(36*14)+1;
					}

					if ((distance + node[i].dist < node[nnodes].dist) && (distance + node[i].dist < node[k].dist))
					{
						if (NewCheck(0, x1,y1,x2,y2))
						{
							node[k].level = level + 1;
							node[k].dist  = distance + node[i].dist;
							node[k].prev  = i;
							changed = 1;
						}
					}
				}
				k-=1;
			}
			while(k > 0);	
		}
		i=i+1;
	}
	while(i < nnodes);	
	return changed;
}


int32 NewCheck(int32 status, int32 x1 , int32 y1 , int32 x2 ,int32 y2)
/*******************************************************************************
 * NewCheck routine checks if the route between two points can be achieved
 * without crossing any of the bars in the Bars array. 
 *
 * NewCheck differs from check in that that 4 route options are considered
 * corresponding to actual walked routes.
 *
 * Note distance doesnt take account of shrinking ??? 
 *
 * Note Bars array must be properly calculated ie min max dx dy co
 *******************************************************************************/
{
  int32   ldx;
  int32   ldy;
  int32   dlx;
  int32   dly;
  int32   dirX;
  int32   dirY;
  int32   step1;   
  int32   step2;   
  int32   step3;   
  int32   steps;   
  int32   options;   

	steps = 0;
	options = 0;
  ldx = x2 - x1;
  ldy = y2 - y1;
	dirX = 1;
	dirY = 1;
	if (ldx < 0)
	{
		ldx = -ldx;
		dirX = -1;
	}

	if (ldy < 0)
	{
		ldy = -ldy;
		dirY = -1;
	}

	//make the route options
	if ((diagonaly * ldx) > (diagonalx * ldy))	// dir  = 1,2 or 2,3 or 5,6 or 6,7
	{
	  dly = ldy;
		dlx = (ldy*diagonalx)/diagonaly;
		ldx = ldx - dlx;
		dlx = dlx * dirX;
		dly = dly * dirY;
		ldx = ldx * dirX;
		ldy = 0;

	 	//options are
		//square, diagonal a code 1 route
		step1 = Check(x1, y1, x1+ldx, y1);
		if (step1 != 0)
		{
			step2 = Check(x1+ldx, y1, x2, y2);
			if (step2 != 0)
			{
				steps = step1 + step2;	// yes
				options = options + 2;
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1+ldx, y1, 231);
			RouteLine(x1+ldx, y1, x2, y2, 231);
		}
    #endif   
			}
		}
		//diagonal, square a code 2 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x1+dlx,y1+dly);
			if (step1 != 0)
			{
				step2 = Check(x1+dlx, y2, x2, y2);
				if (step2 != 0)
				{
					steps = step1 + step2;	// yes
					options = options + 4;
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1+dlx,y1+dly, 231);
			RouteLine(x1+dlx, y2, x2, y2, 231);
		}
    #endif   
				}
			}
		}
		//halfsquare, diagonal, halfsquare a code 0 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x1+ldx/2, y1);
			if (step1 != 0)
			{
				step2 = Check(x1+ldx/2, y1, x1+ldx/2+dlx, y2);
				if (step2 != 0)
				{
					step3 = Check(x1+ldx/2+dlx, y2, x2, y2);
					if (step3 != 0)
					{
						steps = step1 + step2 + step3;	// yes
					options = options + 1;
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1+ldx/2, y1, 231);
			RouteLine(x1+ldx/2, y1, x1+ldx/2+dlx, y2, 231);
			RouteLine(x1+ldx/2+dlx, y2, x2, y2, 231);
		}
    #endif   
					}
				}
			}
		}
		//halfdiagonal, square, halfdiagonal a code 3 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x1+dlx/2, y1+dly/2);
			if (step1 != 0)
			{
				step2 = Check(x1+dlx/2, y1+dly/2, x1+ldx+dlx/2, y1+dly/2);
				if (step2 != 0)
				{
					step3 = Check(x1+ldx+dlx/2, y1+dly/2, x2, y2);
					if (step3 != 0)
					{
						steps = step1 + step2 + step3;	// yes
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1+dlx/2, y1+dly/2, 231);
			RouteLine(x1+dlx/2, y1+dly/2, x1+ldx+dlx/2, y1+dly/2, 231);
			RouteLine(x1+ldx+dlx/2, y1+dly/2, x2, y2, 231);
		}
    #endif   
						options = options + 8;
					}
				}
			}
		}
	}
	else // dir  = 7,0 or 0,1 or 3,4 or 4,5
	{
	  dlx = ldx;
		dly = (ldx*diagonaly)/diagonalx;
		ldy = ldy - dly;
		dlx = dlx * dirX;
		dly = dly * dirY;
		ldy = ldy * dirY;
		ldx = 0;

	 	//options are
		//square, diagonal a code 1 route
		step1 = Check(x1 ,y1 ,x1 ,y1+ldy );
		if (step1 != 0)
		{
			step2 = Check(x1 ,y1+ldy ,x2,y2);
			if (step2 != 0)
			{
				steps = step1 + step2;	// yes
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1 ,y1 ,x1 ,y1+ldy, 231);
			RouteLine(x1 ,y1+ldy ,x2, y2, 231);
		}
    #endif   
				options = options + 2;
			}
		}
		//diagonal, square a code 2 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x2, y1+dly);
			if (step1 != 0)
			{
				step2 = Check(x2, y1+dly, x2, y2);
				if (step2 != 0)
				{
					steps = step1 + step2;	// yes
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x2, y1+dly, 231);
			RouteLine(x2, y1+dly, x2, y2, 231);
		}
    #endif   
					options = options + 4;
				}
			}
		}
		//halfsquare, diagonal, halfsquare a code 0 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x1, y1+ldy/2);
			if (step1 != 0)
			{
				step2 = Check(x1, y1+ldy/2, x2, y1+ldy/2+dly);
				if (step2 != 0)
				{
					step3 = Check(x2, y1+ldy/2+dly, x2, y2);
					if (step3 != 0)
					{
						steps = step1 + step2 + step3;	// yes
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1, y1+ldy/2, 231);
			RouteLine(x1, y1+ldy/2, x2, y1+ldy/2+dly, 231);
			RouteLine(x2, y1+ldy/2+dly, x2, y2, 231);
		}
    #endif   
						options = options + 1;
					}
				}
			}
		}
		//halfdiagonal, square, halfdiagonal a code 3 route
		if ((steps == 0) || (status == 1))
		{
			step1 = Check(x1, y1, x1+dlx/2, y1+dly/2);
			if (step1 != 0)
			{
				step2 = Check(x1+dlx/2, y1+dly/2, x1+dlx/2, y1+ldy+dly/2);
				if (step2 != 0)
				{
					step3 = Check(x1+dlx/2, y1+ldy+dly/2, x2, y2);
					if (step3 != 0)
					{
						steps = step1 + step2 + step3;	// yes
						options = options + 8;
		#ifdef PLOT_PATHS
		if (status == 1)
		{
			RouteLine(x1, y1, x1+dlx/2, y1+dly/2, 231);
			RouteLine(x1+dlx/2, y1+dly/2, x1+dlx/2, y1+ldy+dly/2, 231);
			RouteLine(x1+dlx/2, y1+ldy+dly/2, x2, y2, 231);
		}
    #endif   
					}
				}
			}
		}
	}
	if (status == 0)
	{
		status = steps;
	}
	else
	{
		status = options;
	}
	return status;
}


/*******************************************************************************
 *******************************************************************************
 * 														CHECK ROUTINES
 *******************************************************************************
 *******************************************************************************/


int32 Check(int32 x1 , int32 y1 , int32 x2 ,int32 y2)
{
//call the fastest line check for the given line 
//returns 1 if line didn't cross any bars
  int32   steps;

	if ((x1 == x2) &&	(y1 == y2))
	{
		steps = 1;
	}
	else if (x1 == x2)
	{
			steps = VertCheck(x1, y1, y2);
	}
	else if (y1 == y2)
	{
			steps = HorizCheck(x1, y1, x2);
	}
	else
	{
			steps = LineCheck(x1, y1, x2, y2);
	}
	return steps;

}


int32 LineCheck(int32 x1 , int32 y1 , int32 x2 ,int32 y2)
{
  int32   dirx;
  int32   diry;
  int32   co;
  int32   slope;   
  int32   i;
  int32   xc;
  int32   yc;
  int32   xmin;
  int32   ymin;
  int32   xmax;
  int32   ymax;
  int32   linesCrossed = 1;   


  if (x1 > x2)
  {
  	xmin = x2;    
  	xmax = x1;
  }
  else
  {
  	xmin = x1;    
  	xmax = x2;    
	}
  if (y1 > y2)
  {
  	ymin = y2;    
  	ymax = y1;
  }
  else
  {
  	ymin = y1;    
  	ymax = y2;    
	}
  //line set to go one step in chosen direction
  //so ignore if it hits anything
  dirx = x2 - x1;
	diry = y2 - y1;
  co = (y1 *dirx)- (x1*diry);       //new line equation

  i = 0;          
  do
  {
		// this is the inner inner loop
	  if ((xmax >= bars[i].xmin) && ( xmin <= bars[i].xmax))  //skip if not on module 
    {
			if ((ymax >= bars[i].ymin) && ( ymin <= bars[i].ymax))  //skip if not on module 
	    {
				// okay its a valid line calculate an intersept
				// wow but all this arithmatic we must have loads of time
		    slope = (bars[i].dx * diry) - (bars[i].dy *dirx);// slope it he slope between the two lines
		    if (slope != 0)//assuming parallel lines don't cross
		    {
					//calculate x intercept and check its on both lines
			    xc = ((bars[i].co * dirx) - (co * bars[i].dx)) / slope;

			    if ((xc >= xmin-1) && (xc <= xmax+1))   //skip if not on module 
			    {
					  if ((xc >= bars[i].xmin-1) && (xc <= bars[i].xmax+1))   //skip if not on line 
				    {

					    yc = ((bars[i].co * diry) - (co * bars[i].dy)) / slope;

					    if ((yc >= ymin-1) && (yc <= ymax+1))   //skip if not on module 
					    {
						    if ((yc >= bars[i].ymin-1) && (yc <= bars[i].ymax+1))   //skip if not on line 
						    {
							 	  linesCrossed = 0;
								}
							}
						}
					}
				}
			}
		}
    i = i + 1;
  }
  while((i < nbars) && linesCrossed);

  return linesCrossed;
}


int32 HorizCheck(int32 x1 , int32 y , int32 x2)
{
  int32   ldy;
  int32   i;
  int32   xc;
  int32   xmin;
  int32   xmax;
  int32   linesCrossed = 1;   

  if (x1 > x2)
  {
  	xmin = x2;    
  	xmax = x1;
  }
  else
  {
  	xmin = x1;    
  	xmax = x2;    
	}
  //line set to go one step in chosen direction
  //so ignore if it hits anything

  i = 0;          
  do
  {
		// this is the inner inner loop
	  if ((xmax >= bars[i].xmin) && ( xmin <= bars[i].xmax))  //skip if not on module 
    {
			if ((y >= bars[i].ymin) && ( y <= bars[i].ymax))  //skip if not on module 
	    {
				// okay its a valid line calculate an intersept
				// wow but all this arithmatic we must have loads of time
		    if (bars[i].dy == 0)
		    {
			    linesCrossed = 0;          
		    }
				else
				{
					ldy = y-bars[i].y1;
					xc = bars[i].x1 + (bars[i].dx * ldy)/bars[i].dy;
		    	if ((xc >= xmin-1) && (xc <= xmax+1))   //skip if not on module 
		    	{
				    linesCrossed = 0;          
		    	}
				}
			}
		}
    i = i + 1;
  }
  while((i < nbars) && linesCrossed);

  return linesCrossed;
}


int32 VertCheck(int32 x, int32 y1, int32 y2)
{
  int32   ldx;
  int32   i;
  int32   yc;
  int32   ymin;
  int32   ymax;
  int32   linesCrossed = 1;   

  if (y1 > y2)
  {
  	ymin = y2;    
  	ymax = y1;
  }
  else
  {
  	ymin = y1;    
  	ymax = y2;    
	}
  //line set to go one step in chosen direction
  //so ignore if it hits anything
  i = 0;          
  do		// this is the inner inner loop
  {
	  if ((x >= bars[i].xmin) && ( x <= bars[i].xmax))  //overlapping
    {
			if ((ymax >= bars[i].ymin) && ( ymin <= bars[i].ymax))  //skip if not on module 
		  {
				// okay its a valid line calculate an intersept
				// wow but all this arithmatic we must have loads of time
		    if (bars[i].dx == 0)//both lines vertical and overlap in x and y so they cross
		    {
			    linesCrossed = 0;
		    }
				else
				{
			 		ldx = x-bars[i].x1;
					yc = bars[i].y1 + (bars[i].dy * ldx)/bars[i].dx;
			    if ((yc >= ymin-1) && (yc <= ymax+1))   //the intersept overlaps 
			    {
				    linesCrossed = 0;
			    }
				}
			}
    }
    i = i + 1;
  }
  while((i < nbars) && linesCrossed);

  return linesCrossed;
}

int32 CheckTarget(int32 x , int32 y)
/*******************************************************************************
 *******************************************************************************/
{
  int32   ldx;
  int32   ldy;
  int32   i;
  int32   xc;
  int32   yc;
  int32   xmin;
  int32   xmax;
  int32   ymin;
  int32   ymax;
  int32   onLine = 0;   

 	xmin = x - 1;    
 	xmax = x + 1;
 	ymin = y - 1;    
 	ymax = y + 1;

	// check if point +- 1 is on the line
  //so ignore if it hits anything

  i = 0;          
  do
  {

		// this is the inner inner loop

	  if ((xmax >= bars[i].xmin) && ( xmin <= bars[i].xmax))  //overlapping line 
    {
			if ((ymax >= bars[i].ymin) && ( ymin <= bars[i].ymax))  //overlapping line 
	    {

				// okay this line overlaps the target calculate an y intersept for x 

		    if (bars[i].dx == 0)// vertical line so we know it overlaps y
		    {
					yc = 0; 	
		    }
				else
				{
					ldx = x-bars[i].x1;
					yc = bars[i].y1 + (bars[i].dy * ldx)/bars[i].dx;
				}

		    if ((yc >= ymin) && (yc <= ymax))   //overlapping point for y 
		    {
			    onLine = 3;// target on a line so drop out
					//Zdebug("RouteFail due to target on a line %d %d",x,y);
				}
				else
				{
			    if (bars[i].dy == 0)// vertical line so we know it overlaps y
					{
						xc = 0;
					}
					else
					{
						ldy = y-bars[i].y1;
						xc = bars[i].x1 + (bars[i].dx * ldy)/bars[i].dy;
					}

				  if ((xc >= xmin) && (xc <= xmax))   //skip if not on module 
				  {
				    onLine = 3;// target on a line so drop out
						//Zdebug("RouteFail due to target on a line %d %d",x,y);
			    }
				}
	    }
    }
    i = i + 1;
  }
  while((i < nbars) && (onLine == 0));


  return onLine;
}

/*******************************************************************************
 *******************************************************************************
 * 														THE SETUP ROUTINES
 *******************************************************************************
 *******************************************************************************/
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

void LoadWalkData(Object_walkdata *ob_walkdata)
{
	uint8	direction;
	uint16	firstFrameOfDirection;
	uint16	walkFrameNo;
	uint32	frameCounter = 0;	// starts at frame 0 of mega set (16sep96 JEL)
      

	nWalkFrames				= ob_walkdata->nWalkFrames;
	usingStandingTurnFrames	= ob_walkdata->usingStandingTurnFrames;
	usingWalkingTurnFrames	= ob_walkdata->usingWalkingTurnFrames;
	usingSlowInFrames		= ob_walkdata->usingSlowInFrames;
	usingSlowOutFrames		= ob_walkdata->usingSlowOutFrames;
	numberOfSlowOutFrames	= usingSlowOutFrames;	// 0 = not using slow out frames; non-zero = using that many frames for each leading leg for each direction

 	memcpy(&numberOfSlowInFrames[0],ob_walkdata->nSlowInFrames,NO_DIRECTIONS*sizeof(numberOfSlowInFrames[0]));
 	memcpy(&leadingLeg[0],ob_walkdata->leadingLeg,NO_DIRECTIONS*sizeof(leadingLeg[0]));
 	memcpy(&dx[0],ob_walkdata->dx,NO_DIRECTIONS*(nWalkFrames+1)*sizeof(dx[0]));
 	memcpy(&dy[0],ob_walkdata->dy,NO_DIRECTIONS*(nWalkFrames+1)*sizeof(dy[0]));

	//---------------------------------------------------------

	for (direction=0; direction<NO_DIRECTIONS; direction++)
	{
		firstFrameOfDirection = direction * nWalkFrames;

		modX[direction]=0;
		modY[direction]=0;

		for (walkFrameNo=firstFrameOfDirection; walkFrameNo < (firstFrameOfDirection + (nWalkFrames/2)); walkFrameNo++ )
		{
			modX[direction] += dx[walkFrameNo];	// eg. modX[0] is the sum of the x-step sizes for the first half of the walk cycle for direction 0
			modY[direction] += dy[walkFrameNo];
		}
	}

	diagonalx =  modX[3];
	diagonaly =  modY[3];

	//----------------------------------------------------
	// interpret the walk data
	//----------------------------------------------------

	framesPerStep = nWalkFrames/2;
	framesPerChar = nWalkFrames * NO_DIRECTIONS;

	// offset pointers added Oct 30 95 JPS
	// mega id references removed 16sep96 by JEL

	//---------------------
	// WALK FRAMES
	// start on frame 0
	frameCounter += framesPerChar;

	//---------------------
	// STAND FRAMES
	firstStandFrame = frameCounter;	// stand frames come after the walk frames
	frameCounter += NO_DIRECTIONS;	// one stand frame for each direction

	//---------------------
	// STANDING TURN FRAMES - OPTIONAL!
	if (usingStandingTurnFrames)
	{
		firstStandingTurnLeftFrame	= frameCounter;	// standing turn-left frames come after the slow-out frames
		frameCounter += NO_DIRECTIONS;							// one for each direction

		firstStandingTurnRightFrame = frameCounter;	// standing turn-left frames come after the standing turn-right frames
		frameCounter += NO_DIRECTIONS;							// one for each direction
	}
	else
	{
		firstStandingTurnLeftFrame	= firstStandFrame;	// refer instead to the normal stand frames
		firstStandingTurnRightFrame	= firstStandFrame;	// -"-
	}
	//---------------------
	// WALKING TURN FRAMES - OPTIONAL!
	if (usingWalkingTurnFrames)
	{
		firstWalkingTurnLeftFrame		= frameCounter;	// walking left-turn frames come after the stand frames
		frameCounter += framesPerChar;

		firstWalkingTurnRightFrame	= frameCounter;	// walking right-turn frames come after the walking left-turn frames
		frameCounter += framesPerChar;
	}
	else
	{
		firstWalkingTurnLeftFrame		= 0;
		firstWalkingTurnRightFrame	= 0;
	}
	//---------------------
	// SLOW-IN FRAMES - OPTIONAL!

	if (usingSlowInFrames)	// slow-in frames come after the walking right-turn frames
	{
		for (direction=0; direction<NO_DIRECTIONS; direction++)
		{
			firstSlowInFrame[direction] = frameCounter;				// make note of frame number of first slow-in frame for each direction
			frameCounter += numberOfSlowInFrames[direction];	// can be a different number of slow-in frames in each direction
		}
	}
	//---------------------
	// SLOW-OUT FRAMES - OPTIONAL!

	if (usingSlowOutFrames)
	{
		firstSlowOutFrame	= frameCounter;	// slow-out frames come after the slow-in frames
	}
	//---------------------
}


/*******************************************************************************
 *******************************************************************************
 * 														THE ROUTE EXTRACTOR
 *******************************************************************************
 *******************************************************************************/

void	ExtractRoute()
/****************************************************************************
 *    ExtractRoute	gets route from the node data after a full scan, route is
 *		written with just the basic way points and direction options for heading
 *		to the next point. 
 ****************************************************************************/
{
	int32	prev;
	int32	prevx;
	int32	prevy;
	int32	last;
	int32	point;
  int32 p;     
	int32	dirx;
	int32	diry;
	int32	dir;
	int32	ldx;
	int32	ldy;


 	// extract the route from the node data
	prev = nnodes;
	last = prev;
	point = O_ROUTE_SIZE - 1;
	route[point].x = node[last].x;
	route[point].y = node[last].y;
	do
	{
		point = point -	1;
		prev = node[last].prev;
		prevx = node[prev].x;
		prevy = node[prev].y;
		route[point].x = prevx;
		route[point].y = prevy;
		last = prev;
	}
	while (prev > 0);

	// now shuffle route down in the buffer
	routeLength = 0;
	do
	{
		route[routeLength].x = route[point].x;
		route[routeLength].y = route[point].y;
		point = point + 1;
		routeLength = routeLength + 1;
	}
	while (point < O_ROUTE_SIZE);
	routeLength = routeLength - 1;

	// okay the route exists as a series point now put in some directions
  p = 0;
  do
  {
		#ifdef PLOT_PATHS
			BresenhamLine(route[p+1].x-128,route[p+1].y-128, route[p].x-128,route[p].y-128, (uint8*)screen_ad, true_pixel_size_x, pixel_size_y, ROUTE_END_FLAG);
    #endif   
		ldx = route[p+1].x - route[p].x;
		ldy = route[p+1].y - route[p].y;
		dirx = 1;
		diry = 1;
		if (ldx < 0)
		{
			ldx = -ldx;
			dirx = -1;
		}
		if (ldy < 0)
		{
			ldy = -ldy;
			diry = -1;
		}

		if ((diagonaly * ldx) > (diagonalx * ldy))	// dir  = 1,2 or 2,3 or 5,6 or 6,7
		{
			dir = 4 - 2 * dirx;	 // 2 or 6
			route[p].dirS = dir;
			dir = dir + diry * dirx; // 1,3,5 or 7
			route[p].dirD = dir;
		}
		else // dir  = 7,0 or 0,1 or 3,4 or 4,5
		{
			dir = 2 + 2 * diry;	// 0 or 4
			route[p].dirS = dir;
			dir = 4 - 2 * dirx;	 // 2 or 6
			dir = dir + diry * dirx; // 1,3,5 or 7
			route[p].dirD = dir;
		}
		p = p + 1;	
	}
	while (p < (routeLength));
	// set the last dir to continue previous route unless specified
	if (targetDir == 8)	// ANY direction
	{ 
		route[p].dirS = route[p-1].dirS;
		route[p].dirD = route[p-1].dirD;
	}
	else
	{ 
		route[p].dirS = targetDir;
		route[p].dirD = targetDir;
	}
	return;
}

//*******************************************************************************

void RouteLine(int32 x1,int32 y1,int32 x2,int32 y2 ,int32 colour)
{
	if (x1);
	if (x2);
	if (y1);
	if (y2);
	if (colour);
//	BresenhamLine(x1-128, y1-128, x2-128, y2-128, (uint8*)screen_ad, true_pixel_size_x, pixel_size_y, colour);
	return;
}

//*******************************************************************************

void SetUpWalkGrid(Object_mega *ob_mega, int32 x, int32 y, int32 dir)
{
	int32 i;


	LoadWalkGrid();	// get walk grid file + extra grid into 'bars' & 'node' arrays


	// copy the mega structure into the local variables for use in all subroutines

	startX		= ob_mega->feet_x;
	startY		= ob_mega->feet_y;
	startDir	= ob_mega->current_dir;
	targetX		= x;
	targetY		= y;
	targetDir	= dir;

	scaleA		= ob_mega->scale_a;
	scaleB		= ob_mega->scale_b;


	// mega's current position goes into first node
	node[0].x		= startX;
	node[0].y		= startY;
	node[0].level	= 1;
	node[0].prev	= 0;
	node[0].dist	= 0;

	// reset other nodes
	for (i=1; i<nnodes; i++)
	{
		node[i].level	= 0;
	  	node[i].prev	= 0;
	  	node[i].dist	= 9999;
	}

	// target position goes into final node
	node[nnodes].x		= targetX;
	node[nnodes].y		= targetY;
	node[nnodes].level	= 0;
	node[nnodes].prev	= 0;
	node[nnodes].dist	= 9999;
}

//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
void PlotWalkGrid(void)
{
	int32 j;
      

	LoadWalkGrid();	// get walk grid file + extra grid into 'bars' & 'node' arrays

	//-------------------------------
	// lines

	for (j=0; j<nbars; j++)
	{
		DrawLine(bars[j].x1,bars[j].y1, bars[j].x2,bars[j].y2, 254);
	}
	//-------------------------------
	// nodes

	for (j=1; j<nnodes; j++)	// leave node 0 for start node
	{
		PlotCross(node[j].x,node[j].y, 184);
	}
	//-------------------------------
}
//------------------------------------------------------------------------------------------
void PlotCross(int16 x, int16 y, uint8 colour)
{
	DrawLine(x-1, y-1, x+1, y+1, colour);
	DrawLine(x+1, y-1, x-1, y+1, colour);	
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

void LoadWalkGrid(void)
{
//	_standardHeader	header;
	_walkGridHeader	floorHeader;
	uint32 	j;
	uint8  *fPolygrid;
 	int entry;
	uint32 theseBars;
	uint32 theseNodes;


	nbars	= 0;	// reset counts
	nnodes	= 1;	// leave node 0 for start-node

	//-------------------------------
	// STATIC GRIDS (added/removed by object logics)

	for (entry=0; entry < MAX_WALKGRIDS; entry++)	// go through walkgrid list
	{
		if (walkGridList[entry])
		{
			fPolygrid = res_man.Res_open(walkGridList[entry]);	// open walk grid file

 //			memmove( (uint8*)&header, fPolygrid, sizeof(_standardHeader) );
 			fPolygrid += sizeof(_standardHeader);

 			memmove( (uint8*)&floorHeader, fPolygrid, sizeof(_walkGridHeader) );
 			fPolygrid += sizeof(_walkGridHeader);

			//-------------------------------
			// how many bars & nodes are we getting from this walkgrid file

			theseBars	= floorHeader.numBars;
			theseNodes	= floorHeader.numNodes;

			//-------------------------------
			// check that we're not going to exceed the max allowed in the complete walkgrid arrays

			#ifdef _SWORD2_DEBUG
			if ((nbars+theseBars) >= O_GRID_SIZE)
				Con_fatal_error("Adding walkgrid(%d): %d+%d bars exceeds max %d (%s line %u)", walkGridList[entry], nbars, theseBars, O_GRID_SIZE, __FILE__, __LINE__);

			if ((nnodes+theseNodes) >= O_GRID_SIZE)
				Con_fatal_error("Adding walkgrid(%d): %d+%d nodes exceeds max %d (%s line %u)", walkGridList[entry], nnodes, theseBars, O_GRID_SIZE, __FILE__, __LINE__);
			#endif

			//-------------------------------
			// lines

 			memmove( (uint8*)&bars[nbars], fPolygrid, theseBars*sizeof(_barData) );
			fPolygrid += theseBars*sizeof(_barData);//move pointer to start of node data

			//-------------------------------
			// nodes

			for (j=0; j<theseNodes; j++)	// leave node 0 for start node
			{
				memmove( (uint8*)&node[nnodes+j].x, fPolygrid, 2*sizeof(int16) );
				fPolygrid += 2*sizeof(int16);
			}

 			//-------------------------------

			res_man.Res_close(walkGridList[entry]);	// close walk grid file

			nbars	+= theseBars;	// increment counts of total bars & nodes in whole walkgrid
			nnodes	+= theseNodes;
		}
	}

	//-------------------------------
	// EXTRA GRIDS (moveable grids added by megas)

	// Note that these will be checked against allowed max at the time of creating them

	//-------------------------------
	// extra lines

 	memmove((uint8 *) &bars[nbars], (uint8 *) &extraBars[0], nExtraBars*sizeof(_barData));
	nbars += nExtraBars;

	//-------------------------------
	// extra nodes

 	memmove((uint8 *) &node[nnodes], (uint8 *) &extraNode[0], nExtraNodes*sizeof(_nodeData));
	nnodes += nExtraNodes;

	//-------------------------------
}

//------------------------------------------------------------------------------------------
void ClearWalkGridList(void)
{
	int entry;

	for (entry=0; entry < MAX_WALKGRIDS; entry++)
		walkGridList[entry] = 0;
}
//------------------------------------------------------------------------------------------
// called from FN_add_walkgrid
void AddWalkGrid(int32 gridResource)
{
	int entry;

	// first, scan list to see if this grid is already included
	entry=0;
	while ((entry < MAX_WALKGRIDS) && (walkGridList[entry] != gridResource))
		entry++;

	if (entry == MAX_WALKGRIDS)	// if this new resource isn't already in the list, then add it, (otherwise finish)
	{
		// scan the list for a free slot
		entry=0;
		while ((entry < MAX_WALKGRIDS) && (walkGridList[entry]))
			entry++;

		if (entry < MAX_WALKGRIDS)	// if we found a free slot
			walkGridList[entry] = gridResource;
		else
			Con_fatal_error("ERROR: walkGridList[] full in %s line %d",__FILE__,__LINE__);
	}
}
//--------------------------------------------------------------------------------------
// called from FN_remove_walkgrid
void RemoveWalkGrid(int32 gridResource)
{
	int entry;

	// first, scan list to see if this grid is actually there
	entry=0;
	while ((entry < MAX_WALKGRIDS) && (walkGridList[entry] != gridResource))
		entry++;

	if (entry < MAX_WALKGRIDS)	// if we've found it in the list, reset entry to zero (otherwise just ignore the request)
		walkGridList[entry] = 0;
}
//--------------------------------------------------------------------------------------