File: vision.c

package info (click to toggle)
glhack 1.2-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,748 kB
  • sloc: ansic: 208,571; cpp: 13,139; yacc: 2,005; makefile: 1,152; lex: 377; sh: 121; awk: 89; sed: 11
file content (2622 lines) | stat: -rw-r--r-- 75,155 bytes parent folder | download | duplicates (12)
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
/*	SCCS Id: @(#)vision.c	3.4	1999/02/18	*/
/* Copyright (c) Dean Luick, with acknowledgements to Dave Cohrs, 1990.	*/
/* NetHack may be freely redistributed.  See license for details.	*/

#include "hack.h"

/* Circles ==================================================================*/

/*
 * These numbers are limit offsets for one quadrant of a circle of a given
 * radius (the first number of each line) from the source.  The number in
 * the comment is the element number (so pointers can be set up).  Each
 * "circle" has as many elements as its radius+1.  The radius is the number
 * of points away from the source that the limit exists.  The radius of the
 * offset on the same row as the source *is* included so we don't have to
 * make an extra check.  For example, a circle of radius 4 has offsets:
 *
 *				XXX	+2
 *				...X	+3
 *				....X	+4
 *				....X	+4
 *				@...X   +4
 *
 */
char circle_data[] = {
/*  0*/	 1, 1,
/*  2*/	 2, 2, 1,
/*  5*/	 3, 3, 2, 1,
/*  9*/	 4, 4, 4, 3, 2,
/* 14*/	 5, 5, 5, 4, 3, 2,
/* 20*/	 6, 6, 6, 5, 5, 4, 2,
/* 27*/	 7, 7, 7, 6, 6, 5, 4, 2,
/* 35*/	 8, 8, 8, 7, 7, 6, 6, 4, 2,
/* 44*/	 9, 9, 9, 9, 8, 8, 7, 6, 5, 3,
/* 54*/	10,10,10,10, 9, 9, 8, 7, 6, 5, 3,
/* 65*/	11,11,11,11,10,10, 9, 9, 8, 7, 5, 3,
/* 77*/	12,12,12,12,11,11,10,10, 9, 8, 7, 5, 3,
/* 90*/	13,13,13,13,12,12,12,11,10,10, 9, 7, 6, 3,
/*104*/	14,14,14,14,13,13,13,12,12,11,10, 9, 8, 6, 3,
/*119*/	15,15,15,15,14,14,14,13,13,12,11,10, 9, 8, 6, 3,
/*135*/ 16 /* should be MAX_RADIUS+1; used to terminate range loops -dlc */
};

/*
 * These are the starting indexes into the circle_data[] array for a
 * circle of a given radius.
 */
char circle_start[] = {
/*  */	  0,	/* circles of radius zero are not used */
/* 1*/    0,
/* 2*/	  2,
/* 3*/	  5,
/* 4*/	  9,
/* 5*/	 14,
/* 6*/	 20,
/* 7*/	 27,
/* 8*/	 35,
/* 9*/	 44,
/*10*/	 54,
/*11*/	 65,
/*12*/	 77,
/*13*/	 90,
/*14*/	104,
/*15*/	119,
};


/*===========================================================================*/
/* Vision (arbitrary line of sight) =========================================*/

/*------ global variables ------*/

#if 0	/* (moved to decl.c) */
/* True if we need to run a full vision recalculation. */
boolean	vision_full_recalc = 0;

/* Pointers to the current vision array. */
char	**viz_array;
#endif
char	*viz_rmin, *viz_rmax;		/* current vision cs bounds */


/*------ local variables ------*/


static char could_see[2][ROWNO][COLNO];		/* vision work space */
static char *cs_rows0[ROWNO], *cs_rows1[ROWNO];
static char  cs_rmin0[ROWNO],  cs_rmax0[ROWNO];
static char  cs_rmin1[ROWNO],  cs_rmax1[ROWNO];

static char  viz_clear[ROWNO][COLNO];		/* vision clear/blocked map */
static char *viz_clear_rows[ROWNO];

static char  left_ptrs[ROWNO][COLNO];		/* LOS algorithm helpers */
static char right_ptrs[ROWNO][COLNO];

/* Forward declarations. */
STATIC_DCL void FDECL(fill_point, (int,int));
STATIC_DCL void FDECL(dig_point, (int,int));
STATIC_DCL void NDECL(view_init);
STATIC_DCL void FDECL(view_from,(int,int,char **,char *,char *,int,
			     void (*)(int,int,genericptr_t),genericptr_t));
STATIC_DCL void FDECL(get_unused_cs, (char ***,char **,char **));
#ifdef REINCARNATION
STATIC_DCL void FDECL(rogue_vision, (char **,char *,char *));
#endif

/* Macro definitions that I can't find anywhere. */
#define sign(z) ((z) < 0 ? -1 : ((z) ? 1 : 0 ))
#define v_abs(z)  ((z) < 0 ? -(z) : (z))	/* don't use abs -- it may exist */

/*
 * vision_init()
 *
 * The one-time vision initialization routine.
 *
 * This must be called before mklev() is called in newgame() [allmain.c],
 * or before a game restore.   Else we die a horrible death.
 */
void
vision_init()
{
    int i;

    /* Set up the pointers. */
    for (i = 0; i < ROWNO; i++) {
	cs_rows0[i] = could_see[0][i];
	cs_rows1[i] = could_see[1][i];
	viz_clear_rows[i] = viz_clear[i];
    }

    /* Start out with cs0 as our current array */
    viz_array = cs_rows0;
    viz_rmin  = cs_rmin0;
    viz_rmax  = cs_rmax0;

    vision_full_recalc = 0;
    (void) memset((genericptr_t) could_see, 0, sizeof(could_see));

    /* Initialize the vision algorithm (currently C or D). */
    view_init();

#ifdef VISION_TABLES
    /* Note:  this initializer doesn't do anything except guarantee that
	      we're linked properly.
    */
    vis_tab_init();
#endif
}

/*
 * does_block()
 *
 * Returns true if the level feature, object, or monster at (x,y) blocks
 * sight.
 */
int
does_block(x,y,lev)
    int x, y;
    register struct rm    *lev;
{
    struct obj   *obj;
    struct monst *mon;

    /* Features that block . . */
    if (IS_ROCK(lev->typ) || lev->typ == TREE || (IS_DOOR(lev->typ) &&
			    (lev->doormask & (D_CLOSED|D_LOCKED|D_TRAPPED) )))
	return 1;

    if (lev->typ == CLOUD || lev->typ == WATER ||
			(lev->typ == MOAT && Underwater))
	return 1;

    /* Boulders block light. */
    for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
	if (obj->otyp == BOULDER) return 1;

    /* Mimics mimicing a door or boulder block light. */
    if ((mon = m_at(x,y)) && (!mon->minvis || See_invisible) &&
	  ((mon->m_ap_type == M_AP_FURNITURE &&
	  (mon->mappearance == S_hcdoor || mon->mappearance == S_vcdoor)) ||
	  (mon->m_ap_type == M_AP_OBJECT && mon->mappearance == BOULDER)))
	return 1;

    return 0;
}

/*
 * vision_reset()
 *
 * This must be called *after* the levl[][] structure is set with the new
 * level and the level monsters and objects are in place.
 */
void
vision_reset()
{
    int y;
    register int x, i, dig_left, block;
    register struct rm    *lev;

    /* Start out with cs0 as our current array */
    viz_array = cs_rows0;
    viz_rmin  = cs_rmin0;
    viz_rmax  = cs_rmax0;

    (void) memset((genericptr_t) could_see, 0, sizeof(could_see));

    /* Reset the pointers and clear so that we have a "full" dungeon. */
    (void) memset((genericptr_t) viz_clear,        0, sizeof(viz_clear));

    /* Dig the level */
    for (y = 0; y < ROWNO; y++) {
	dig_left = 0;
	block = TRUE;	/* location (0,y) is always stone; it's !isok() */
	lev = &levl[1][y];
	for (x = 1; x < COLNO; x++, lev += ROWNO)
	    if (block != (IS_ROCK(lev->typ) || does_block(x,y,lev))) {
		if(block) {
		    for(i=dig_left; i<x; i++) {
			left_ptrs [y][i] = dig_left;
			right_ptrs[y][i] = x-1;
		    }
		} else {
		    i = dig_left;
		    if(dig_left) dig_left--; /* point at first blocked point */
		    for(; i<x; i++) {
			left_ptrs [y][i] = dig_left;
			right_ptrs[y][i] = x;
			viz_clear[y][i] = 1;
		    }
		}
		dig_left = x;
		block = !block;
	    }
	/* handle right boundary; almost identical for blocked/unblocked */
	i = dig_left;
	if(!block && dig_left) dig_left--; /* point at first blocked point */
	for(; i<COLNO; i++) {
	    left_ptrs [y][i] = dig_left;
	    right_ptrs[y][i] = (COLNO-1);
	    viz_clear[y][i] = !block;
	}
    }

    iflags.vision_inited = 1;	/* vision is ready */
    vision_full_recalc = 1;	/* we want to run vision_recalc() */
}


/*
 * get_unused_cs()
 *
 * Called from vision_recalc() and at least one light routine.  Get pointers
 * to the unused vision work area.
 */
STATIC_OVL void
get_unused_cs(rows, rmin, rmax)
    char ***rows;
    char **rmin, **rmax;
{
    register int  row;
    register char *nrmin, *nrmax;

    if (viz_array == cs_rows0) {
	*rows = cs_rows1;
	*rmin = cs_rmin1;
	*rmax = cs_rmax1;
    } else {
	*rows = cs_rows0;
	*rmin = cs_rmin0;
	*rmax = cs_rmax0;
    }

    /* return an initialized, unused work area */
    nrmin = *rmin;
    nrmax = *rmax;

    (void) memset((genericptr_t)**rows, 0, ROWNO*COLNO);  /* we see nothing */
    for (row = 0; row < ROWNO; row++) {		/* set row min & max */
	*nrmin++ = COLNO-1;
	*nrmax++ = 0;
    }
}


#ifdef REINCARNATION
/*
 * rogue_vision()
 *
 * Set the "could see" and in sight bits so vision acts just like the old
 * rogue game:
 *
 *	+ If in a room, the hero can see to the room boundaries.
 *	+ The hero can always see adjacent squares.
 *
 * We set the in_sight bit here as well to escape a bug that shows up
 * due to the one-sided lit wall hack.
 */
STATIC_OVL void
rogue_vision(next, rmin, rmax)
    char **next;	/* could_see array pointers */
    char *rmin, *rmax;
{
    int rnum = levl[u.ux][u.uy].roomno - ROOMOFFSET; /* no SHARED... */
    int start, stop, in_door, xhi, xlo, yhi, ylo;
    register int zx, zy;

    /* If in a lit room, we are able to see to its boundaries. */
    /* If dark, set COULD_SEE so various spells work -dlc */
    if (rnum >= 0) {
	for (zy = rooms[rnum].ly-1; zy <= rooms[rnum].hy+1; zy++) {
	    rmin[zy] = start = rooms[rnum].lx-1;
	    rmax[zy] = stop  = rooms[rnum].hx+1;

	    for (zx = start; zx <= stop; zx++) {
		if (rooms[rnum].rlit) {
		    next[zy][zx] = COULD_SEE | IN_SIGHT;
		    levl[zx][zy].seenv = SVALL;	/* see the walls */
		} else
		    next[zy][zx] = COULD_SEE;
	    }
	}
    }

    in_door = levl[u.ux][u.uy].typ == DOOR;

    /* Can always see adjacent. */
    ylo = max(u.uy - 1, 0);
    yhi = min(u.uy + 1, ROWNO - 1);
    xlo = max(u.ux - 1, 1);
    xhi = min(u.ux + 1, COLNO - 1);
    for (zy = ylo; zy <= yhi; zy++) {
	if (xlo < rmin[zy]) rmin[zy] = xlo;
	if (xhi > rmax[zy]) rmax[zy] = xhi;

	for (zx = xlo; zx <= xhi; zx++) {
	    next[zy][zx] = COULD_SEE | IN_SIGHT;
	    /*
	     * Yuck, update adjacent non-diagonal positions when in a doorway.
	     * We need to do this to catch the case when we first step into
	     * a room.  The room's walls were not seen from the outside, but
	     * now are seen (the seen bits are set just above).  However, the
	     * positions are not updated because they were already in sight.
	     * So, we have to do it here.
	     */
	    if (in_door && (zx == u.ux || zy == u.uy)) newsym(zx,zy);
	}
    }
}
#endif /* REINCARNATION */

/*#define EXTEND_SPINE*/	/* possibly better looking wall-angle */

#ifdef EXTEND_SPINE

STATIC_DCL int FDECL(new_angle, (struct rm *, unsigned char *, int, int));
/*
 * new_angle()
 *
 * Return the new angle seen by the hero for this location.  The angle
 * bit is given in the value pointed at by sv.
 *
 * For T walls and crosswall, just setting the angle bit, even though
 * it is technically correct, doesn't look good.  If we can see the
 * next position beyond the current one and it is a wall that we can
 * see, then we want to extend a spine of the T to connect with the wall
 * that is beyond.  Example:
 *
 *	 Correct, but ugly			   Extend T spine
 *
 *		| ...					| ...
 *		| ...	<-- wall beyond & floor -->	| ...
 *		| ...					| ...
 * Unseen   -->   ...					| ...
 * spine	+-...	<-- trwall & doorway	-->	+-...
 *		| ...					| ...
 *
 *
 *		   @	<-- hero		-->	   @
 *
 *
 * We fake the above check by only checking if the horizontal &
 * vertical positions adjacent to the crosswall and T wall are
 * unblocked.  Then, _in general_ we can see beyond.  Generally,
 * this is good enough.
 *
 *	+ When this function is called we don't have all of the seen
 *	  information (we're doing a top down scan in vision_recalc).
 *	  We would need to scan once to set all IN_SIGHT and COULD_SEE
 *	  bits, then again to correctly set the seenv bits.
 *	+ I'm trying to make this as cheap as possible.  The display &
 *	  vision eat up too much CPU time.
 *	
 *
 * Note:  Even as I write this, I'm still not convinced.  There are too
 *	  many exceptions.  I may have to bite the bullet and do more
 *	  checks.	- Dean 2/11/93
 */
STATIC_OVL int
new_angle(lev, sv, row, col)
    struct rm *lev;
    unsigned char *sv;
    int row, col;
{
    register int res = *sv;

    /*
     * Do extra checks for crosswalls and T walls if we see them from
     * an angle.
     */
    if (lev->typ >= CROSSWALL && lev->typ <= TRWALL) {
	switch (res) {
	    case SV0:
		if (col > 0	  && viz_clear[row][col-1]) res |= SV7;
		if (row > 0	  && viz_clear[row-1][col]) res |= SV1;
		break;
	    case SV2:
		if (row > 0	  && viz_clear[row-1][col]) res |= SV1;
		if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
		break;
	    case SV4:
		if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
		if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
		break;
	    case SV6:
		if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
		if (col > 0	  && viz_clear[row][col-1]) res |= SV7;
		break;
	}
    }
    return res;
}
#else
/*
 * new_angle()
 *
 * Return the new angle seen by the hero for this location.  The angle
 * bit is given in the value pointed at by sv.
 *
 * The other parameters are not used.
 */
#define new_angle(lev, sv, row, col) (*sv)

#endif


/*
 * vision_recalc()
 *
 * Do all of the heavy vision work.  Recalculate all locations that could
 * possibly be seen by the hero --- if the location were lit, etc.  Note
 * which locations are actually seen because of lighting.  Then add to
 * this all locations that be seen by hero due to night vision and x-ray
 * vision.  Finally, compare with what the hero was able to see previously.
 * Update the difference.
 *
 * This function is usually called only when the variable 'vision_full_recalc'
 * is set.  The following is a list of places where this function is called,
 * with three valid values for the control flag parameter:
 *
 * Control flag = 0.  A complete vision recalculation.  Generate the vision
 * tables from scratch.  This is necessary to correctly set what the hero
 * can see.  (1) and (2) call this routine for synchronization purposes, (3)
 * calls this routine so it can operate correctly.
 *
 *	+ After the monster move, before input from the player. [moveloop()]
 *	+ At end of moveloop. [moveloop() ??? not sure why this is here]
 *	+ Right before something is printed. [pline()]
 *	+ Right before we do a vision based operation. [do_clear_area()]
 *	+ screen redraw, so we can renew all positions in sight. [docrt()]
 *
 * Control flag = 1.  An adjacent vision recalculation.  The hero has moved
 * one square.  Knowing this, it might be possible to optimize the vision
 * recalculation using the current knowledge.  This is presently unimplemented
 * and is treated as a control = 0 call.
 *
 *	+ Right after the hero moves. [domove()]
 *
 * Control flag = 2.  Turn off the vision system.  Nothing new will be
 * displayed, since nothing is seen.  This is usually done when you need
 * a newsym() run on all locations in sight, or on some locations but you
 * don't know which ones.
 *
 *	+ Before a screen redraw, so all positions are renewed. [docrt()]
 *	+ Right before the hero arrives on a new level. [goto_level()]
 *	+ Right after a scroll of light is read. [litroom()]
 *	+ After an option has changed that affects vision [parseoptions()]
 *	+ Right after the hero is swallowed. [gulpmu()]
 *	+ Just before bubbles are moved. [movebubbles()]
 */
void
vision_recalc(control)
    int control;
{
    char **temp_array;	/* points to the old vision array */
    char **next_array;	/* points to the new vision array */
    char *next_row;	/* row pointer for the new array */
    char *old_row;	/* row pointer for the old array */
    char *next_rmin;	/* min pointer for the new array */
    char *next_rmax;	/* max pointer for the new array */
    char *ranges;	/* circle ranges -- used for xray & night vision */
    int row;		/* row counter (outer loop)  */
    int start, stop;	/* inner loop starting/stopping index */
    int dx, dy;		/* one step from a lit door or lit wall (see below) */
    register int col;	/* inner loop counter */
    register struct rm *lev;	/* pointer to current pos */
    struct rm *flev;	/* pointer to position in "front" of current pos */
    extern unsigned char seenv_matrix[3][3];	/* from display.c */
    static unsigned char colbump[COLNO+1];	/* cols to bump sv */
    unsigned char *sv;				/* ptr to seen angle bits */
    int oldseenv;				/* previous seenv value */

    vision_full_recalc = 0;			/* reset flag */
    if (in_mklev || !iflags.vision_inited) return;

#ifdef GCC_WARN
    row = 0;
#endif

    /*
     * Either the light sources have been taken care of, or we must
     * recalculate them here.
     */

    /* Get the unused could see, row min, and row max arrays. */
    get_unused_cs(&next_array, &next_rmin, &next_rmax);

    /* You see nothing, nothing can see you --- if swallowed or refreshing. */
    if (u.uswallow || control == 2) {
	/* do nothing -- get_unused_cs() nulls out the new work area */

    } else if (Blind) {
	/*
	 * Calculate the could_see array even when blind so that monsters
	 * can see you, even if you can't see them.  Note that the current
	 * setup allows:
	 *
	 *	+ Monsters to see with the "new" vision, even on the rogue
	 *	  level.
	 *
	 *	+ Monsters can see you even when you're in a pit.
	 */
	view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
		0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);

	/*
	 * Our own version of the update loop below.  We know we can't see
	 * anything, so we only need update positions we used to be able
	 * to see.
	 */
	temp_array = viz_array;	/* set viz_array so newsym() will work */
	viz_array = next_array;

	for (row = 0; row < ROWNO; row++) {
	    old_row = temp_array[row];

	    /* Find the min and max positions on the row. */
	    start = min(viz_rmin[row], next_rmin[row]);
	    stop  = max(viz_rmax[row], next_rmax[row]);

	    for (col = start; col <= stop; col++)
		if (old_row[col] & IN_SIGHT) newsym(col,row);
	}

	/* skip the normal update loop */
	goto skip;
    }
#ifdef REINCARNATION
    else if (Is_rogue_level(&u.uz)) {
	rogue_vision(next_array,next_rmin,next_rmax);
    }
#endif
    else {
	int has_night_vision = 1;	/* hero has night vision */

	if (Underwater && !Is_waterlevel(&u.uz)) {
	    /*
	     * The hero is under water.  Only see surrounding locations if
	     * they are also underwater.  This overrides night vision but
	     * does not override x-ray vision.
	     */
	    has_night_vision = 0;

	    for (row = u.uy-1; row <= u.uy+1; row++)
		for (col = u.ux-1; col <= u.ux+1; col++) {
		    if (!isok(col,row) || !is_pool(col,row)) continue;

		    next_rmin[row] = min(next_rmin[row], col);
		    next_rmax[row] = max(next_rmax[row], col);
		    next_array[row][col] = IN_SIGHT | COULD_SEE;
		}
	}

	/* if in a pit, just update for immediate locations */
	else if (u.utrap && u.utraptype == TT_PIT) {
	    for (row = u.uy-1; row <= u.uy+1; row++) {
		if (row < 0) continue;	if (row >= ROWNO) break;

		next_rmin[row] = max(      0, u.ux - 1);
		next_rmax[row] = min(COLNO-1, u.ux + 1);
		next_row = next_array[row];

		for(col=next_rmin[row]; col <= next_rmax[row]; col++)
		    next_row[col] = IN_SIGHT | COULD_SEE;
	    }
	} else
	    view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
		0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);

	/*
	 * Set the IN_SIGHT bit for xray and night vision.
	 */
	if (u.xray_range >= 0) {
	    if (u.xray_range) {
		ranges = circle_ptr(u.xray_range);

		for (row = u.uy-u.xray_range; row <= u.uy+u.xray_range; row++) {
		    if (row < 0) continue;	if (row >= ROWNO) break;
		    dy = v_abs(u.uy-row);	next_row = next_array[row];

		    start = max(      0, u.ux - ranges[dy]);
		    stop  = min(COLNO-1, u.ux + ranges[dy]);

		    for (col = start; col <= stop; col++) {
			char old_row_val = next_row[col];
			next_row[col] |= IN_SIGHT;
			oldseenv = levl[col][row].seenv;
			levl[col][row].seenv = SVALL;	/* see all! */
			/* Update if previously not in sight or new angle. */
			if (!(old_row_val & IN_SIGHT) || oldseenv != SVALL)
			    newsym(col,row);
		    }

		    next_rmin[row] = min(start, next_rmin[row]);
		    next_rmax[row] = max(stop, next_rmax[row]);
		}

	    } else {	/* range is 0 */
		next_array[u.uy][u.ux] |= IN_SIGHT;
		levl[u.ux][u.uy].seenv = SVALL;
		next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
		next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
	    }
	}

	if (has_night_vision && u.xray_range < u.nv_range) {
	    if (!u.nv_range) {	/* range is 0 */
		next_array[u.uy][u.ux] |= IN_SIGHT;
		levl[u.ux][u.uy].seenv = SVALL;
		next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
		next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
	    } else if (u.nv_range > 0) {
		ranges = circle_ptr(u.nv_range);

		for (row = u.uy-u.nv_range; row <= u.uy+u.nv_range; row++) {
		    if (row < 0) continue;	if (row >= ROWNO) break;
		    dy = v_abs(u.uy-row);	next_row = next_array[row];

		    start = max(      0, u.ux - ranges[dy]);
		    stop  = min(COLNO-1, u.ux + ranges[dy]);

		    for (col = start; col <= stop; col++)
			if (next_row[col]) next_row[col] |= IN_SIGHT;

		    next_rmin[row] = min(start, next_rmin[row]);
		    next_rmax[row] = max(stop, next_rmax[row]);
		}
	    }
	}
    }

    /* Set the correct bits for all light sources. */
    do_light_sources(next_array);


    /*
     * Make the viz_array the new array so that cansee() will work correctly.
     */
    temp_array = viz_array;
    viz_array = next_array;

    /*
     * The main update loop.  Here we do two things:
     *
     *	    + Set the IN_SIGHT bit for places that we could see and are lit.
     *	    + Reset changed places.
     *
     * There is one thing that make deciding what the hero can see
     * difficult:
     *
     *  1.  Directional lighting.  Items that block light create problems.
     *      The worst offenders are doors.  Suppose a door to a lit room
     *      is closed.  It is lit on one side, but not on the other.  How
     *      do you know?  You have to check the closest adjacent position.
     *	    Even so, that is not entirely correct.  But it seems close
     *	    enough for now.
     */
    colbump[u.ux] = colbump[u.ux+1] = 1;
    for (row = 0; row < ROWNO; row++) {
	dy = u.uy - row;                dy = sign(dy);
	next_row = next_array[row];     old_row = temp_array[row];

	/* Find the min and max positions on the row. */
	start = min(viz_rmin[row], next_rmin[row]);
	stop  = max(viz_rmax[row], next_rmax[row]);
	lev = &levl[start][row];

	sv = &seenv_matrix[dy+1][start < u.ux ? 0 : (start > u.ux ? 2:1)];

	for (col = start; col <= stop;
				lev += ROWNO, sv += (int) colbump[++col]) {
	    if (next_row[col] & IN_SIGHT) {
		/*
		 * We see this position because of night- or xray-vision.
		 */
		oldseenv = lev->seenv;
		lev->seenv |= new_angle(lev,sv,row,col); /* update seen angle */

		/* Update pos if previously not in sight or new angle. */
		if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
		    newsym(col,row);
	    }

	    else if ((next_row[col] & COULD_SEE)
				&& (lev->lit || (next_row[col] & TEMP_LIT))) {
		/*
		 * We see this position because it is lit.
		 */
		if ((IS_DOOR(lev->typ) || lev->typ == SDOOR ||
		     IS_WALL(lev->typ)) && !viz_clear[row][col]) {
		    /*
		     * Make sure doors, walls, boulders or mimics don't show up
		     * at the end of dark hallways.  We do this by checking
		     * the adjacent position.  If it is lit, then we can see
		     * the door or wall, otherwise we can't.
		     */
		    dx = u.ux - col;	dx = sign(dx);
		    flev = &(levl[col+dx][row+dy]);
		    if (flev->lit || next_array[row+dy][col+dx] & TEMP_LIT) {
			next_row[col] |= IN_SIGHT;	/* we see it */

			oldseenv = lev->seenv;
			lev->seenv |= new_angle(lev,sv,row,col);

			/* Update pos if previously not in sight or new angle.*/
			if (!(old_row[col] & IN_SIGHT) || oldseenv!=lev->seenv)
			    newsym(col,row);
		    } else
			goto not_in_sight;	/* we don't see it */

		} else {
		    next_row[col] |= IN_SIGHT;	/* we see it */

		    oldseenv = lev->seenv;
		    lev->seenv |= new_angle(lev,sv,row,col);

		    /* Update pos if previously not in sight or new angle. */
		    if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
			newsym(col,row);
		}
	    } else if ((next_row[col] & COULD_SEE) && lev->waslit) {
		/*
		 * If we make it here, the hero _could see_ the location,
		 * but doesn't see it (location is not lit).
		 * However, the hero _remembers_ it as lit (waslit is true).
		 * The hero can now see that it is not lit, so change waslit
		 * and update the location.
		 */
		lev->waslit = 0; /* remember lit condition */
		newsym(col,row);
	    }
	    /*
	     * At this point we know that the row position is *not* in normal
	     * sight.  That is, the position is could be seen, but is dark
	     * or LOS is just plain blocked.
	     *
	     * Update the position if:
	     * o If the old one *was* in sight.  We may need to clean up
	     *   the glyph -- E.g. darken room spot, etc.
	     * o If we now could see the location (yet the location is not
	     *   lit), but previously we couldn't see the location, or vice
	     *   versa.  Update the spot because there there may be an infared
	     *   monster there.
	     */
	    else {
not_in_sight:
		if ((old_row[col] & IN_SIGHT)
			|| ((next_row[col] & COULD_SEE)
				^ (old_row[col] & COULD_SEE)))
		    newsym(col,row);
	    }

	} /* end for col . . */
    }	/* end for row . .  */
    colbump[u.ux] = colbump[u.ux+1] = 0;

skip:
    /* This newsym() caused a crash delivering msg about failure to open
     * dungeon file init_dungeons() -> panic() -> done(11) ->
     * vision_recalc(2) -> newsym() -> crash!  u.ux and u.uy are 0 and
     * program_state.panicking == 1 under those circumstances
     */
    if (!program_state.panicking)
	newsym(u.ux, u.uy);		/* Make sure the hero shows up! */

    /* Set the new min and max pointers. */
    viz_rmin  = next_rmin;
    viz_rmax = next_rmax;
}


/*
 * block_point()
 *
 * Make the location opaque to light.
 */
void
block_point(x,y)
    int x, y;
{
    fill_point(y,x);

    /* recalc light sources here? */

    /*
     * We have to do a full vision recalculation if we "could see" the
     * location.  Why? Suppose some monster opened a way so that the
     * hero could see a lit room.  However, the position of the opening
     * was out of night-vision range of the hero.  Suddenly the hero should
     * see the lit room.
     */
    if (viz_array[y][x]) vision_full_recalc = 1;
}

/*
 * unblock_point()
 *
 * Make the location transparent to light.
 */
void
unblock_point(x,y)
    int x, y;
{
    dig_point(y,x);

    /* recalc light sources here? */

    if (viz_array[y][x]) vision_full_recalc = 1;
}


/*===========================================================================*\
 |									     |
 |	Everything below this line uses (y,x) instead of (x,y) --- the	     |
 |	algorithms are faster if they are less recursive and can scan	     |
 |	on a row longer.						     |
 |									     |
\*===========================================================================*/


/* ========================================================================= *\
			Left and Right Pointer Updates
\* ========================================================================= */

/*
 *			LEFT and RIGHT pointer rules
 *
 *
 * **NOTE**  The rules changed on 4/4/90.  This comment reflects the
 * new rules.  The change was so that the stone-wall optimization
 * would work.
 *
 * OK, now the tough stuff.  We must maintain our left and right
 * row pointers.  The rules are as follows:
 *
 * Left Pointers:
 * ______________
 *
 * + If you are a clear spot, your left will point to the first
 *   stone to your left.  If there is none, then point the first
 *   legal position in the row (0).
 *
 * + If you are a blocked spot, then your left will point to the
 *   left-most blocked spot to your left that is connected to you.
 *   This means that a left-edge (a blocked spot that has an open
 *   spot on its left) will point to itself.
 *
 *
 * Right Pointers:
 * ---------------
 * + If you are a clear spot, your right will point to the first
 *   stone to your right.  If there is none, then point the last
 *   legal position in the row (COLNO-1).
 *
 * + If you are a blocked spot, then your right will point to the
 *   right-most blocked spot to your right that is connected to you.
 *   This means that a right-edge (a blocked spot that has an open
 *    spot on its right) will point to itself.
 */
STATIC_OVL void
dig_point(row,col)
    int row,col;
{
    int i;

    if (viz_clear[row][col]) return;		/* already done */

    viz_clear[row][col] = 1;

    /*
     * Boundary cases first.
     */
    if (col == 0) {				/* left edge */
	if (viz_clear[row][1]) {
	    right_ptrs[row][0] = right_ptrs[row][1];
	} else {
	    right_ptrs[row][0] = 1;
	    for (i = 1; i <= right_ptrs[row][1]; i++)
		left_ptrs[row][i] = 1;
	}
    } else if (col == (COLNO-1)) {		/* right edge */

	if (viz_clear[row][COLNO-2]) {
	    left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
	} else {
	    left_ptrs[row][COLNO-1] = COLNO-2;
	    for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
		right_ptrs[row][i] = COLNO-2;
	}
    }

    /*
     * At this point, we know we aren't on the boundaries.
     */
    else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
	/* Both sides clear */
	for (i = left_ptrs[row][col-1]; i <= col; i++) {
	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
	    right_ptrs[row][i] = right_ptrs[row][col+1];
	}
	for (i = col; i <= right_ptrs[row][col+1]; i++) {
	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
	    left_ptrs[row][i] = left_ptrs[row][col-1];
	}

    } else if (viz_clear[row][col-1]) {
	/* Left side clear, right side blocked. */
	for (i = col+1; i <= right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = col+1;

	for (i = left_ptrs[row][col-1]; i <= col; i++) {
	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
	    right_ptrs[row][i] = col+1;
	}
	left_ptrs[row][col] = left_ptrs[row][col-1];

    } else if (viz_clear[row][col+1]) {
	/* Right side clear, left side blocked. */
	for (i = left_ptrs[row][col-1]; i < col; i++)
	    right_ptrs[row][i] = col-1;

	for (i = col; i <= right_ptrs[row][col+1]; i++) {
	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
	    left_ptrs[row][i] = col-1;
	}
	right_ptrs[row][col] = right_ptrs[row][col+1];

    } else {
	/* Both sides blocked */
	for (i = left_ptrs[row][col-1]; i < col; i++)
	    right_ptrs[row][i] = col-1;

	for (i = col+1; i <= right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = col+1;

	left_ptrs[row][col]  = col-1;
	right_ptrs[row][col] = col+1;
    }
}

STATIC_OVL void
fill_point(row,col)
    int row, col;
{
    int i;

    if (!viz_clear[row][col]) return;

    viz_clear[row][col] = 0;

    if (col == 0) {
	if (viz_clear[row][1]) {			/* adjacent is clear */
	    right_ptrs[row][0] = 0;
	} else {
	    right_ptrs[row][0] = right_ptrs[row][1];
	    for (i = 1; i <= right_ptrs[row][1]; i++)
		left_ptrs[row][i] = 0;
	}
    } else if (col == COLNO-1) {
	if (viz_clear[row][COLNO-2]) {		/* adjacent is clear */
	    left_ptrs[row][COLNO-1] = COLNO-1;
	} else {
	    left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
	    for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
		right_ptrs[row][i] = COLNO-1;
	}
    }

    /*
     * Else we know that we are not on an edge.
     */
    else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
	/* Both sides clear */
	for (i = left_ptrs[row][col-1]+1; i <= col; i++)
	    right_ptrs[row][i] = col;

	if (!left_ptrs[row][col-1])		/* catch the end case */
	    right_ptrs[row][0] = col;

	for (i = col; i < right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = col;

	if (right_ptrs[row][col+1] == COLNO-1)	/* catch the end case */
	    left_ptrs[row][COLNO-1] = col;

    } else if (viz_clear[row][col-1]) {
	/* Left side clear, right side blocked. */
	for (i = col; i <= right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = col;

	for (i = left_ptrs[row][col-1]+1; i < col; i++)
	    right_ptrs[row][i] = col;

	if (!left_ptrs[row][col-1])		/* catch the end case */
	    right_ptrs[row][i] = col;

	right_ptrs[row][col] = right_ptrs[row][col+1];

    } else if (viz_clear[row][col+1]) {
	/* Right side clear, left side blocked. */
	for (i = left_ptrs[row][col-1]; i <= col; i++)
	    right_ptrs[row][i] = col;

	for (i = col+1; i < right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = col;

	if (right_ptrs[row][col+1] == COLNO-1)	/* catch the end case */
	    left_ptrs[row][i] = col;

	left_ptrs[row][col] = left_ptrs[row][col-1];

    } else {
	/* Both sides blocked */
	for (i = left_ptrs[row][col-1]; i <= col; i++)
	    right_ptrs[row][i] = right_ptrs[row][col+1];

	for (i = col; i <= right_ptrs[row][col+1]; i++)
	    left_ptrs[row][i] = left_ptrs[row][col-1];
    }
}


/*===========================================================================*/
/*===========================================================================*/
/* Use either algorithm C or D.  See the config.h for more details. =========*/

/*
 * Variables local to both Algorithms C and D.
 */
static int  start_row;
static int  start_col;
static int  step;
static char **cs_rows;
static char *cs_left;
static char *cs_right;

static void FDECL((*vis_func), (int,int,genericptr_t));
static genericptr_t varg;

/*
 * Both Algorithms C and D use the following macros.
 *
 *      good_row(z)	  - Return TRUE if the argument is a legal row.
 *      set_cs(rowp,col)  - Set the local could see array.
 *      set_min(z)	  - Save the min value of the argument and the current
 *			      row minimum.
 *      set_max(z)	  - Save the max value of the argument and the current
 *			      row maximum.
 *
 * The last three macros depend on having local pointers row_min, row_max,
 * and rowp being set correctly.
 */
#define set_cs(rowp,col) (rowp[col] = COULD_SEE)
#define good_row(z) ((z) >= 0 && (z) < ROWNO)
#define set_min(z) if (*row_min > (z)) *row_min = (z)
#define set_max(z) if (*row_max < (z)) *row_max = (z)
#define is_clear(row,col) viz_clear_rows[row][col]

/*
 * clear_path()		expanded into 4 macros/functions:
 *
 *	q1_path()
 *	q2_path()
 *	q3_path()
 *	q4_path()
 *
 * "Draw" a line from the start to the given location.  Stop if we hit
 * something that blocks light.  The start and finish points themselves are
 * not checked, just the points between them.  These routines do _not_
 * expect to be called with the same starting and stopping point.
 *
 * These routines use the generalized integer Bresenham's algorithm (fast
 * line drawing) for all quadrants.  The algorithm was taken from _Procedural
 * Elements for Computer Graphics_, by David F. Rogers.  McGraw-Hill, 1985.
 */
#ifdef MACRO_CPATH	/* quadrant calls are macros */

/*
 * When called, the result is in "result".
 * The first two arguments (srow,scol) are one end of the path.  The next
 * two arguments (row,col) are the destination.  The last argument is
 * used as a C language label.  This means that it must be different
 * in each pair of calls.
 */

/*
 *  Quadrant I (step < 0).
 */
#define q1_path(srow,scol,y2,x2,label)			\
{							\
    int dx, dy;						\
    register int k, err, x, y, dxs, dys;		\
							\
    x  = (scol);	y  = (srow);			\
    dx = (x2) - x;	dy = y - (y2);			\
							\
    result = 0;		 /* default to a blocked path */\
							\
    dxs = dx << 1;	   /* save the shifted values */\
    dys = dy << 1;					\
    if (dy > dx) {					\
	err = dxs - dy;					\
							\
	for (k = dy-1; k; k--) {			\
	    if (err >= 0) {				\
		x++;					\
		err -= dys;				\
	    }						\
	    y--;					\
	    err += dxs;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    } else {						\
	err = dys - dx;					\
							\
	for (k = dx-1; k; k--) {			\
	    if (err >= 0) {				\
		y--;					\
		err -= dxs;				\
	    }						\
	    x++;					\
	    err += dys;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    }							\
							\
    result = 1;						\
}

/*
 * Quadrant IV (step > 0).
 */
#define q4_path(srow,scol,y2,x2,label)			\
{							\
    int dx, dy;						\
    register int k, err, x, y, dxs, dys;		\
							\
    x  = (scol);	y  = (srow);			\
    dx = (x2) - x;	dy = (y2) - y;			\
							\
    result = 0;		 /* default to a blocked path */\
							\
    dxs = dx << 1;	   /* save the shifted values */\
    dys = dy << 1;					\
    if (dy > dx) {					\
	err = dxs - dy;					\
							\
	for (k = dy-1; k; k--) {			\
	    if (err >= 0) {				\
		x++;					\
		err -= dys;				\
	    }						\
	    y++;					\
	    err += dxs;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
							\
    } else {						\
	err = dys - dx;					\
							\
	for (k = dx-1; k; k--) {			\
	    if (err >= 0) {				\
		y++;					\
		err -= dxs;				\
	    }						\
	    x++;					\
	    err += dys;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    }							\
							\
    result = 1;						\
}

/*
 * Quadrant II (step < 0).
 */
#define q2_path(srow,scol,y2,x2,label)			\
{							\
    int dx, dy;						\
    register int k, err, x, y, dxs, dys;		\
							\
    x  = (scol);	y  = (srow);			\
    dx = x - (x2);	dy = y - (y2);			\
							\
    result = 0;		 /* default to a blocked path */\
							\
    dxs = dx << 1;	   /* save the shifted values */\
    dys = dy << 1;					\
    if (dy > dx) {					\
	err = dxs - dy;					\
							\
	for (k = dy-1; k; k--) {			\
	    if (err >= 0) {				\
		x--;					\
		err -= dys;				\
	    }						\
	    y--;					\
	    err += dxs;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    } else {						\
	err = dys - dx;					\
							\
	for (k = dx-1; k; k--) {			\
	    if (err >= 0) {				\
		y--;					\
		err -= dxs;				\
	    }						\
	    x--;					\
	    err += dys;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    }							\
							\
    result = 1;						\
}

/*
 * Quadrant III (step > 0).
 */
#define q3_path(srow,scol,y2,x2,label)			\
{							\
    int dx, dy;						\
    register int k, err, x, y, dxs, dys;		\
							\
    x  = (scol);	y  = (srow);			\
    dx = x - (x2);	dy = (y2) - y;			\
							\
    result = 0;		 /* default to a blocked path */\
							\
    dxs = dx << 1;	   /* save the shifted values */\
    dys = dy << 1;					\
    if (dy > dx) {					\
	err = dxs - dy;					\
							\
	for (k = dy-1; k; k--) {			\
	    if (err >= 0) {				\
		x--;					\
		err -= dys;				\
	    }						\
	    y++;					\
	    err += dxs;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
							\
    } else {						\
	err = dys - dx;					\
							\
	for (k = dx-1; k; k--) {			\
	    if (err >= 0) {				\
		y++;					\
		err -= dxs;				\
	    }						\
	    x--;					\
	    err += dys;					\
	    if (!is_clear(y,x)) goto label;/* blocked */\
	}						\
    }							\
							\
    result = 1;						\
}

#else   /* quadrants are really functions */

STATIC_DCL int FDECL(_q1_path, (int,int,int,int));
STATIC_DCL int FDECL(_q2_path, (int,int,int,int));
STATIC_DCL int FDECL(_q3_path, (int,int,int,int));
STATIC_DCL int FDECL(_q4_path, (int,int,int,int));

#define q1_path(sy,sx,y,x,dummy) result = _q1_path(sy,sx,y,x)
#define q2_path(sy,sx,y,x,dummy) result = _q2_path(sy,sx,y,x)
#define q3_path(sy,sx,y,x,dummy) result = _q3_path(sy,sx,y,x)
#define q4_path(sy,sx,y,x,dummy) result = _q4_path(sy,sx,y,x)

/*
 * Quadrant I (step < 0).
 */
STATIC_OVL int
_q1_path(srow,scol,y2,x2)
    int scol, srow, y2, x2;
{
    int dx, dy;
    register int k, err, x, y, dxs, dys;

    x  = scol;		y  = srow;
    dx = x2 - x;	dy = y - y2;

    dxs = dx << 1;	   /* save the shifted values */
    dys = dy << 1;
    if (dy > dx) {
	err = dxs - dy;

	for (k = dy-1; k; k--) {
	    if (err >= 0) {
		x++;
		err -= dys;
	    }
	    y--;
	    err += dxs;
	    if (!is_clear(y,x)) return 0; /* blocked */
	}
    } else {
	err = dys - dx;

	for (k = dx-1; k; k--) {
	    if (err >= 0) {
		y--;
		err -= dxs;
	    }
	    x++;
	    err += dys;
	    if (!is_clear(y,x)) return 0;/* blocked */
	}
    }

    return 1;
}

/*
 * Quadrant IV (step > 0).
 */
STATIC_OVL int
_q4_path(srow,scol,y2,x2)
    int scol, srow, y2, x2;
{
    int dx, dy;
    register int k, err, x, y, dxs, dys;

    x  = scol;		y  = srow;
    dx = x2 - x;	dy = y2 - y;

    dxs = dx << 1;	   /* save the shifted values */
    dys = dy << 1;
    if (dy > dx) {
	err = dxs - dy;

	for (k = dy-1; k; k--) {
	    if (err >= 0) {
		x++;
		err -= dys;
	    }
	    y++;
	    err += dxs;
	    if (!is_clear(y,x)) return 0; /* blocked */
	}
    } else {
	err = dys - dx;

	for (k = dx-1; k; k--) {
	    if (err >= 0) {
		y++;
		err -= dxs;
	    }
	    x++;
	    err += dys;
	    if (!is_clear(y,x)) return 0;/* blocked */
	}
    }

    return 1;
}

/*
 * Quadrant II (step < 0).
 */
STATIC_OVL int
_q2_path(srow,scol,y2,x2)
    int scol, srow, y2, x2;
{
    int dx, dy;
    register int k, err, x, y, dxs, dys;

    x  = scol;		y  = srow;
    dx = x - x2;	dy = y - y2;

    dxs = dx << 1;	   /* save the shifted values */
    dys = dy << 1;
    if (dy > dx) {
	err = dxs - dy;

	for (k = dy-1; k; k--) {
	    if (err >= 0) {
		x--;
		err -= dys;
	    }
	    y--;
	    err += dxs;
	    if (!is_clear(y,x)) return 0; /* blocked */
	}
    } else {
	err = dys - dx;

	for (k = dx-1; k; k--) {
	    if (err >= 0) {
		y--;
		err -= dxs;
	    }
	    x--;
	    err += dys;
	    if (!is_clear(y,x)) return 0;/* blocked */
	}
    }

    return 1;
}

/*
 * Quadrant III (step > 0).
 */
STATIC_OVL int
_q3_path(srow,scol,y2,x2)
    int scol, srow, y2, x2;
{
    int dx, dy;
    register int k, err, x, y, dxs, dys;

    x  = scol;		y  = srow;
    dx = x - x2;	dy = y2 - y;

    dxs = dx << 1;	   /* save the shifted values */
    dys = dy << 1;
    if (dy > dx) {
	err = dxs - dy;

	for (k = dy-1; k; k--) {
	    if (err >= 0) {
		x--;
		err -= dys;
	    }
	    y++;
	    err += dxs;
	    if (!is_clear(y,x)) return 0; /* blocked */
	}
    } else {
	err = dys - dx;

	for (k = dx-1; k; k--) {
	    if (err >= 0) {
		y++;
		err -= dxs;
	    }
	    x--;
	    err += dys;
	    if (!is_clear(y,x)) return 0;/* blocked */
	}
    }

    return 1;
}

#endif	/* quadrants are functions */

/*
 * Use vision tables to determine if there is a clear path from
 * (col1,row1) to (col2,row2).  This is used by:
 *		m_cansee()
 *		m_canseeu()
 *		do_light_sources()
 */
boolean
clear_path(col1,row1,col2,row2)
    int col1, row1, col2, row2;
{
    int result;

    if(col1 < col2) {
	if(row1 > row2) {
	    q1_path(row1,col1,row2,col2,cleardone);
	} else {
	    q4_path(row1,col1,row2,col2,cleardone);
	}
    } else {
	if(row1 > row2) {
	    q2_path(row1,col1,row2,col2,cleardone);
	} else if(row1 == row2 && col1 == col2) {
	    result = 1;
	} else {
	    q3_path(row1,col1,row2,col2,cleardone);
	}
    }
#ifdef MACRO_CPATH
cleardone:
#endif
    return((boolean)result);
}

#ifdef VISION_TABLES
/*===========================================================================*\
			    GENERAL LINE OF SIGHT
				Algorithm D
\*===========================================================================*/


/*
 * Indicate caller for the shadow routines.
 */
#define FROM_RIGHT 0
#define FROM_LEFT  1


/*
 * Include the table definitions.
 */
#include "vis_tab.h"


/* 3D table pointers. */
static close2d *close_dy[CLOSE_MAX_BC_DY];
static far2d   *far_dy[FAR_MAX_BC_DY];

STATIC_DCL void FDECL(right_side, (int,int,int,int,int,int,int,char*));
STATIC_DCL void FDECL(left_side, (int,int,int,int,int,int,int,char*));
STATIC_DCL int FDECL(close_shadow, (int,int,int,int));
STATIC_DCL int FDECL(far_shadow, (int,int,int,int));

/*
 * Initialize algorithm D's table pointers.  If we don't have these,
 * then we do 3D table lookups.  Verrrry slow.
 */
STATIC_OVL void
view_init()
{
    int i;

    for (i = 0; i < CLOSE_MAX_BC_DY; i++)
	close_dy[i] = &close_table[i];

    for (i = 0; i < FAR_MAX_BC_DY; i++)
	far_dy[i] = &far_table[i];
}


/*
 * If the far table has an entry of OFF_TABLE, then the far block prevents
 * us from seeing the location just above/below it.  I.e. the first visible
 * location is one *before* the block.
 */
#define OFF_TABLE 0xff

STATIC_OVL int
close_shadow(side,this_row,block_row,block_col)
    int side,this_row,block_row,block_col;
{
    register int sdy, sdx, pdy, offset;

    /*
     * If on the same column (block_row = -1), then we can see it.
     */
    if (block_row < 0) return block_col;

    /* Take explicit absolute values.  Adjust. */
    if ((sdy = (start_row-block_row)) < 0) sdy = -sdy; --sdy;	/* src   dy */
    if ((sdx = (start_col-block_col)) < 0) sdx = -sdx;		/* src   dx */
    if ((pdy = (block_row-this_row))  < 0) pdy = -pdy;		/* point dy */

    if (sdy < 0 || sdy >= CLOSE_MAX_SB_DY || sdx >= CLOSE_MAX_SB_DX ||
						    pdy >= CLOSE_MAX_BC_DY) {
	impossible("close_shadow:  bad value");
	return block_col;
    }
    offset = close_dy[sdy]->close[sdx][pdy];
    if (side == FROM_RIGHT)
	return block_col + offset;

    return block_col - offset;
}


STATIC_OVL int
far_shadow(side,this_row,block_row,block_col)
    int side,this_row,block_row,block_col;
{
    register int sdy, sdx, pdy, offset;

    /*
     * Take care of a bug that shows up only on the borders.
     *
     * If the block is beyond the border, then the row is negative.  Return
     * the block's column number (should be 0 or COLNO-1).
     *
     * Could easily have the column be -1, but then wouldn't know if it was
     * the left or right border.
     */
    if (block_row < 0) return block_col;

    /* Take explicit absolute values.  Adjust. */
    if ((sdy = (start_row-block_row)) < 0) sdy = -sdy;		/* src   dy */
    if ((sdx = (start_col-block_col)) < 0) sdx = -sdx; --sdx;	/* src   dx */
    if ((pdy = (block_row-this_row))  < 0) pdy = -pdy; --pdy;	/* point dy */

    if (sdy >= FAR_MAX_SB_DY || sdx < 0 || sdx >= FAR_MAX_SB_DX ||
					    pdy < 0 || pdy >= FAR_MAX_BC_DY) {
	impossible("far_shadow:  bad value");
	return block_col;
    }
    if ((offset = far_dy[sdy]->far_q[sdx][pdy]) == OFF_TABLE) offset = -1;
    if (side == FROM_RIGHT)
	return block_col + offset;

    return block_col - offset;
}


/*
 * right_side()
 *
 * Figure out what could be seen on the right side of the source.
 */
STATIC_OVL void
right_side(row, cb_row, cb_col, fb_row, fb_col, left, right_mark, limits)
    int row;		/* current row */
    int	cb_row, cb_col;	/* close block row and col */
    int	fb_row, fb_col;	/* far block row and col */
    int left;		/* left mark of the previous row */
    int	right_mark;	/* right mark of previous row */
    char *limits;	/* points at range limit for current row, or NULL */
{
    register int  i;
    register char *rowp;
    int  hit_stone = 0;
    int  left_shadow, right_shadow, loc_right;
    int  lblock_col;		/* local block column (current row) */
    int  nrow, deeper;
    char *row_min;		/* left most */
    char *row_max;		/* right most */
    int		  lim_max;	/* right most limit of circle */

#ifdef GCC_WARN
    rowp = 0;
#endif
    nrow    = row + step;
    deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
    if(!vis_func) {
	rowp    = cs_rows[row];
	row_min = &cs_left[row];
	row_max = &cs_right[row];
    }
    if(limits) {
	lim_max = start_col + *limits;
	if(lim_max > COLNO-1) lim_max = COLNO-1;
	if(right_mark > lim_max) right_mark = lim_max;
	limits++; /* prepare for next row */
    } else
	lim_max = COLNO-1;

    /*
     * Get the left shadow from the close block.  This value could be
     * illegal.
     */
    left_shadow = close_shadow(FROM_RIGHT,row,cb_row,cb_col);

    /*
     * Mark all stone walls as seen before the left shadow.  All this work
     * for a special case.
     *
     * NOTE.  With the addition of this code in here, it is now *required*
     * for the algorithm to work correctly.  If this is commented out,
     * change the above assignment so that left and not left_shadow is the
     * variable that gets the shadow.
     */
    while (left <= right_mark) {
	loc_right = right_ptrs[row][left];
	if(loc_right > lim_max) loc_right = lim_max;
	if (viz_clear_rows[row][left]) {
	    if (loc_right >= left_shadow) {
		left = left_shadow;	/* opening ends beyond shadow */
		break;
	    }
	    left = loc_right;
	    loc_right = right_ptrs[row][left];
	    if(loc_right > lim_max) loc_right = lim_max;
	    if (left == loc_right) return;	/* boundary */

	    /* Shadow covers opening, beyond right mark */
	    if (left == right_mark && left_shadow > right_mark) return;
	}

	if (loc_right > right_mark)	/* can't see stone beyond the mark */
	    loc_right = right_mark;

	if(vis_func) {
	    for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
	} else {
	    for (i = left; i <= loc_right; i++) set_cs(rowp,i);
	    set_min(left);	set_max(loc_right);
	}

	if (loc_right == right_mark) return;	/* all stone */
	if (loc_right >= left_shadow) hit_stone = 1;
	left = loc_right + 1;
    }

    /*
     * At this point we are at the first visible clear spot on or beyond
     * the left shadow, unless the left shadow is an illegal value.  If we
     * have "hit stone" then we have a stone wall just to our left.
     */

    /*
     * Get the right shadow.  Make sure that it is a legal value.
     */
    if ((right_shadow = far_shadow(FROM_RIGHT,row,fb_row,fb_col)) >= COLNO)
	right_shadow = COLNO-1;
    /*
     * Make vertical walls work the way we want them.  In this case, we
     * note when the close block blocks the column just above/beneath
     * it (right_shadow < fb_col [actually right_shadow == fb_col-1]).  If
     * the location is filled, then we want to see it, so we put the
     * right shadow back (same as fb_col).
     */
    if (right_shadow < fb_col && !viz_clear_rows[row][fb_col])
	right_shadow = fb_col;
    if(right_shadow > lim_max) right_shadow = lim_max;

    /*
     * Main loop.  Within the range of sight of the previous row, mark all
     * stone walls as seen.  Follow open areas recursively.
     */
    while (left <= right_mark) {
	/* Get the far right of the opening or wall */
	loc_right = right_ptrs[row][left];
	if(loc_right > lim_max) loc_right = lim_max;

	if (!viz_clear_rows[row][left]) {
	    hit_stone = 1;	/* use stone on this row as close block */
	    /*
	     * We can see all of the wall until the next open spot or the
	     * start of the shadow caused by the far block (right).
	     *
	     * Can't see stone beyond the right mark.
	     */
	    if (loc_right > right_mark) loc_right = right_mark;

	    if(vis_func) {
		for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left; i <= loc_right; i++) set_cs(rowp,i);
		set_min(left);	set_max(loc_right);
	    }

	    if (loc_right == right_mark) return;	/* hit the end */
	    left = loc_right + 1;
	    loc_right = right_ptrs[row][left];
	    if(loc_right > lim_max) loc_right = lim_max;
	    /* fall through... we know at least one position is visible */
	}

	/*
	 * We are in an opening.
	 *
	 * If this is the first open spot since the could see area  (this is
	 * true if we have hit stone), get the shadow generated by the wall
	 * just to our left.
	 */
	if (hit_stone) {
	    lblock_col = left-1;	/* local block column */
	    left = close_shadow(FROM_RIGHT,row,row,lblock_col);
	    if (left > lim_max) break;		/* off the end */
	}

	/*
	 * Check if the shadow covers the opening.  If it does, then
	 * move to end of the opening.  A shadow generated on from a
	 * wall on this row does *not* cover the wall on the right
	 * of the opening.
	 */
	if (left >= loc_right) {
	    if (loc_right == lim_max) {		/* boundary */
		if (left == lim_max) {
		    if(vis_func) (*vis_func)(lim_max, row, varg);
		    else {
			set_cs(rowp,lim_max);	/* last pos */
			set_max(lim_max);
		    }
		}
		return;					/* done */
	    }
	    left = loc_right;
	    continue;
	}

	/*
	 * If the far wall of the opening (loc_right) is closer than the
	 * shadow limit imposed by the far block (right) then use the far
	 * wall as our new far block when we recurse.
	 *
	 * If the limits are the the same, and the far block really exists
	 * (fb_row >= 0) then do the same as above.
	 *
	 * Normally, the check would be for the far wall being closer OR EQUAL
	 * to the shadow limit.  However, there is a bug that arises from the
	 * fact that the clear area pointers end in an open space (if it
	 * exists) on a boundary.  This then makes a far block exist where it
	 * shouldn't --- on a boundary.  To get around that, I had to
	 * introduce the concept of a non-existent far block (when the
	 * row < 0).  Next I have to check for it.  Here is where that check
	 * exists.
	 */
	if ((loc_right < right_shadow) ||
				(fb_row >= 0 && loc_right == right_shadow)) {
	    if(vis_func) {
		for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left; i <= loc_right; i++) set_cs(rowp,i);
		set_min(left);	set_max(loc_right);
	    }

	    if (deeper) {
		if (hit_stone)
		    right_side(nrow,row,lblock_col,row,loc_right,
							left,loc_right,limits);
		else
		    right_side(nrow,cb_row,cb_col,row,loc_right,
							left,loc_right,limits);
	    }

	    /*
	     * The following line, setting hit_stone, is needed for those
	     * walls that are only 1 wide.  If hit stone is *not* set and
	     * the stone is only one wide, then the close block is the old
	     * one instead one on the current row.  A way around having to
	     * set it here is to make left = loc_right (not loc_right+1) and
	     * let the outer loop take care of it.  However, if we do that
	     * then we then have to check for boundary conditions here as
	     * well.
	     */
	    hit_stone = 1;

	    left = loc_right+1;
	}
	/*
	 * The opening extends beyond the right mark.  This means that
	 * the next far block is the current far block.
	 */
	else {
	    if(vis_func) {
		for (i=left; i <= right_shadow; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left; i <= right_shadow; i++) set_cs(rowp,i);
		set_min(left);	set_max(right_shadow);
	    }

	    if (deeper) {
		if (hit_stone)
		    right_side(nrow,   row,lblock_col,fb_row,fb_col,
						     left,right_shadow,limits);
		else
		    right_side(nrow,cb_row,    cb_col,fb_row,fb_col,
						     left,right_shadow,limits);
	    }

	    return;	/* we're outta here */
	}
    }
}


/*
 * left_side()
 *
 * This routine is the mirror image of right_side().  Please see right_side()
 * for blow by blow comments.
 */
STATIC_OVL void
left_side(row, cb_row, cb_col, fb_row, fb_col, left_mark, right, limits)
    int row;		/* the current row */
    int	cb_row, cb_col;	/* close block row and col */
    int	fb_row, fb_col;	/* far block row and col */
    int	left_mark;	/* left mark of previous row */
    int right;		/* right mark of the previous row */
    char *limits;
{
    register int  i;
    register char *rowp;
    int  hit_stone = 0;
    int  left_shadow, right_shadow, loc_left;
    int  lblock_col;		/* local block column (current row) */
    int  nrow, deeper;
    char *row_min;		/* left most */
    char *row_max;		/* right most */
    int		  lim_min;

#ifdef GCC_WARN
    rowp = 0;
#endif
    nrow    = row + step;
    deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
    if(!vis_func) {
	rowp    = cs_rows[row];
	row_min = &cs_left[row];
	row_max = &cs_right[row];
    }
    if(limits) {
	lim_min = start_col - *limits;
	if(lim_min < 0) lim_min = 0;
	if(left_mark < lim_min) left_mark = lim_min;
	limits++; /* prepare for next row */
    } else
	lim_min = 0;

    /* This value could be illegal. */
    right_shadow = close_shadow(FROM_LEFT,row,cb_row,cb_col);

    while ( right >= left_mark ) {
	loc_left = left_ptrs[row][right];
	if(loc_left < lim_min) loc_left = lim_min;
	if (viz_clear_rows[row][right]) {
	    if (loc_left <= right_shadow) {
		right = right_shadow;	/* opening ends beyond shadow */
		break;
	    }
	    right = loc_left;
	    loc_left = left_ptrs[row][right];
	    if(loc_left < lim_min) loc_left = lim_min;
	    if (right == loc_left) return;	/* boundary */
	}

	if (loc_left < left_mark)	/* can't see beyond the left mark */
	    loc_left = left_mark;

	if(vis_func) {
	    for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
	} else {
	    for (i = loc_left; i <= right; i++) set_cs(rowp,i);
	    set_min(loc_left);	set_max(right);
	}

	if (loc_left == left_mark) return;	/* all stone */
	if (loc_left <= right_shadow) hit_stone = 1;
	right = loc_left - 1;
    }

    /* At first visible clear spot on or beyond the right shadow. */

    if ((left_shadow = far_shadow(FROM_LEFT,row,fb_row,fb_col)) < 0)
	left_shadow = 0;

    /* Do vertical walls as we want. */
    if (left_shadow > fb_col && !viz_clear_rows[row][fb_col])
	left_shadow = fb_col;
    if(left_shadow < lim_min) left_shadow = lim_min;

    while (right >= left_mark) {
	loc_left = left_ptrs[row][right];

	if (!viz_clear_rows[row][right]) {
	    hit_stone = 1;	/* use stone on this row as close block */

	    /* We can only see walls until the left mark */
	    if (loc_left < left_mark) loc_left = left_mark;

	    if(vis_func) {
		for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = loc_left; i <= right; i++) set_cs(rowp,i);
		set_min(loc_left);	set_max(right);
	    }

	    if (loc_left == left_mark) return;	/* hit end */
	    right = loc_left - 1;
	    loc_left = left_ptrs[row][right];
	    if (loc_left < lim_min) loc_left = lim_min;
	    /* fall through...*/
	}

	/* We are in an opening. */
	if (hit_stone) {
	    lblock_col = right+1;	/* stone block (local) */
	    right = close_shadow(FROM_LEFT,row,row,lblock_col);
	    if (right < lim_min) return;	/* off the end */
	}

	/*  Check if the shadow covers the opening. */
	if (right <= loc_left) {
	    /*  Make a boundary condition work. */
	    if (loc_left == lim_min) {	/* at boundary */
		if (right == lim_min) {
		    if(vis_func) (*vis_func)(lim_min, row, varg);
		    else {
			set_cs(rowp,lim_min);	/* caught the last pos */
			set_min(lim_min);
		    }
		}
		return;			/* and break out the loop */
	    }

	    right = loc_left;
	    continue;
	}

	/* If the far wall of the opening is closer than the shadow limit. */
	if ((loc_left > left_shadow) ||
				    (fb_row >= 0 && loc_left == left_shadow)) {
	    if(vis_func) {
		for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = loc_left; i <= right; i++) set_cs(rowp,i);
		set_min(loc_left);	set_max(right);
	    }

	    if (deeper) {
		if (hit_stone)
		    left_side(nrow,row,lblock_col,row,loc_left,
							loc_left,right,limits);
		else
		    left_side(nrow,cb_row,cb_col,row,loc_left,
							loc_left,right,limits);
	    }

	    hit_stone = 1;	/* needed for walls of width 1 */
	    right = loc_left-1;
	}
	/*  The opening extends beyond the left mark. */
	else {
	    if(vis_func) {
		for (i=left_shadow; i <= right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left_shadow; i <= right; i++) set_cs(rowp,i);
		set_min(left_shadow);	set_max(right);
	    }

	    if (deeper) {
		if (hit_stone)
		    left_side(nrow,row,lblock_col,fb_row,fb_col,
						     left_shadow,right,limits);
		else
		    left_side(nrow,cb_row,cb_col,fb_row,fb_col,
						     left_shadow,right,limits);
	    }

	    return;	/* we're outta here */
	}

    }
}

/*
 * view_from
 *
 * Calculate a view from the given location.  Initialize and fill a
 * ROWNOxCOLNO array (could_see) with all the locations that could be
 * seen from the source location.  Initialize and fill the left most
 * and right most boundaries of what could be seen.
 */
STATIC_OVL void
view_from(srow,scol,loc_cs_rows,left_most,right_most, range, func, arg)
    int  srow, scol;			/* source row and column */
    char **loc_cs_rows;			/* could_see array (row pointers) */
    char *left_most, *right_most;	/* limits of what could be seen */
    int range;		/* 0 if unlimited */
    void FDECL((*func), (int,int,genericptr_t));
    genericptr_t arg;
{
    register int i;
    char	 *rowp;
    int		 nrow, left, right, left_row, right_row;
    char	 *limits;

    /* Set globals for near_shadow(), far_shadow(), etc. to use. */
    start_col = scol;
    start_row = srow;
    cs_rows   = loc_cs_rows;
    cs_left   = left_most;
    cs_right  = right_most;
    vis_func = func;
    varg = arg;

    /*  Find the left and right limits of sight on the starting row. */
    if (viz_clear_rows[srow][scol]) {
	left  = left_ptrs[srow][scol];
	right = right_ptrs[srow][scol];
    } else {
	left  = (!scol) ? 0 :
	    (viz_clear_rows[srow][scol-1] ?  left_ptrs[srow][scol-1] : scol-1);
	right = (scol == COLNO-1) ? COLNO-1 :
	    (viz_clear_rows[srow][scol+1] ? right_ptrs[srow][scol+1] : scol+1);
    }

    if(range) {
	if(range > MAX_RADIUS || range < 1)
	    panic("view_from called with range %d", range);
	limits = circle_ptr(range) + 1; /* start at next row */
	if(left < scol - range) left = scol - range;
	if(right > scol + range) right = scol + range;
    } else
	limits = (char*) 0;

    if(func) {
	for (i = left; i <= right; i++) (*func)(i, srow, arg);
    } else {
	/* Row optimization */
	rowp = cs_rows[srow];

	/* We know that we can see our row. */
	for (i = left; i <= right; i++) set_cs(rowp,i);
	cs_left[srow]  = left;
	cs_right[srow] = right;
    }

    /* The far block has a row number of -1 if we are on an edge. */
    right_row = (right == COLNO-1) ? -1 : srow;
    left_row  = (!left)		   ? -1 : srow;

    /*
     *  Check what could be seen in quadrants.
     */
    if ( (nrow = srow+1) < ROWNO ) {
	step =  1;	/* move down */
	if (scol<COLNO-1)
	    right_side(nrow,-1,scol,right_row,right,scol,right,limits);
	if (scol)
	    left_side(nrow,-1,scol,left_row, left, left, scol,limits);
    }

    if ( (nrow = srow-1) >= 0 ) {
	step = -1;	/* move up */
	if (scol<COLNO-1)
	    right_side(nrow,-1,scol,right_row,right,scol,right,limits);
	if (scol)
	    left_side(nrow,-1,scol,left_row, left, left, scol,limits);
    }
}


#else	/*===== End of algorithm D =====*/


/*===========================================================================*\
			    GENERAL LINE OF SIGHT
				Algorithm C
\*===========================================================================*/

/*
 * Defines local to Algorithm C.
 */
STATIC_DCL void FDECL(right_side, (int,int,int,char*));
STATIC_DCL void FDECL(left_side, (int,int,int,char*));

/* Initialize algorithm C (nothing). */
STATIC_OVL void
view_init()
{
}

/*
 * Mark positions as visible on one quadrant of the right side.  The
 * quadrant is determined by the value of the global variable step.
 */
STATIC_OVL void
right_side(row, left, right_mark, limits)
    int row;		/* current row */
    int left;		/* first (left side) visible spot on prev row */
    int right_mark;	/* last (right side) visible spot on prev row */
    char *limits;	/* points at range limit for current row, or NULL */
{
    int		  right;	/* right limit of "could see" */
    int		  right_edge;	/* right edge of an opening */
    int		  nrow;		/* new row (calculate once) */
    int		  deeper;	/* if TRUE, call self as needed */
    int		  result;	/* set by q?_path() */
    register int  i;		/* loop counter */
    register char *rowp;	/* row optimization */
    char	  *row_min;	/* left most  [used by macro set_min()] */
    char	  *row_max;	/* right most [used by macro set_max()] */
    int		  lim_max;	/* right most limit of circle */

#ifdef GCC_WARN
    rowp = row_min = row_max = 0;
#endif
    nrow    = row + step;
    /*
     * Can go deeper if the row is in bounds and the next row is within
     * the circle's limit.  We tell the latter by checking to see if the next
     * limit value is the start of a new circle radius (meaning we depend
     * on the structure of circle_data[]).
     */
    deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
    if(!vis_func) {
	rowp    = cs_rows[row];	/* optimization */
	row_min = &cs_left[row];
	row_max = &cs_right[row];
    }
    if(limits) {
	lim_max = start_col + *limits;
	if(lim_max > COLNO-1) lim_max = COLNO-1;
	if(right_mark > lim_max) right_mark = lim_max;
	limits++; /* prepare for next row */
    } else
	lim_max = COLNO-1;

    while (left <= right_mark) {
	right_edge = right_ptrs[row][left];
	if(right_edge > lim_max) right_edge = lim_max;

	if (!is_clear(row,left)) {
	    /*
	     * Jump to the far side of a stone wall.  We can set all
	     * the points in between as seen.
	     *
	     * If the right edge goes beyond the right mark, check to see
	     * how much we can see.
	     */
	    if (right_edge > right_mark) {
		/*
		 * If the mark on the previous row was a clear position,
		 * the odds are that we can actually see part of the wall
		 * beyond the mark on this row.  If so, then see one beyond
		 * the mark.  Otherwise don't.  This is a kludge so corners
		 * with an adjacent doorway show up in nethack.
		 */
		right_edge = is_clear(row-step,right_mark) ?
						    right_mark+1 : right_mark;
	    }
	    if(vis_func) {
		for (i = left; i <= right_edge; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left; i <= right_edge; i++) set_cs(rowp,i);
		set_min(left);      set_max(right_edge);
	    }
	    left = right_edge + 1; /* no limit check necessary */
	    continue;
	}

	/* No checking needed if our left side is the start column. */
	if (left != start_col) {
	    /*
	     * Find the left side.  Move right until we can see it or we run
	     * into a wall.
	     */
	    for (; left <= right_edge; left++) {
		if (step < 0) {
		    q1_path(start_row,start_col,row,left,rside1);
		} else {
		    q4_path(start_row,start_col,row,left,rside1);
		}
rside1:					/* used if q?_path() is a macro */
		if (result) break;
	    }

	    /*
	     * Check for boundary conditions.  We *need* check (2) to break
	     * an infinite loop where:
	     *
	     *		left == right_edge == right_mark == lim_max.
	     *
	     */
	    if (left > lim_max) return;	/* check (1) */
	    if (left == lim_max) {	/* check (2) */
		if(vis_func) (*vis_func)(lim_max, row, varg);
		else {
		    set_cs(rowp,lim_max);
		    set_max(lim_max);
		}
		return;
	    }
	    /*
	     * Check if we can see any spots in the opening.  We might
	     * (left == right_edge) or might not (left == right_edge+1) have
	     * been able to see the far wall.  Make sure we *can* see the
	     * wall (remember, we can see the spot above/below this one)
	     * by backing up.
	     */
	    if (left >= right_edge) {
		left = right_edge;	/* for the case left == right_edge+1 */
		continue;
	    }
	}

	/*
	 * Find the right side.  If the marker from the previous row is
	 * closer than the edge on this row, then we have to check
	 * how far we can see around the corner (under the overhang).  Stop
	 * at the first non-visible spot or we actually hit the far wall.
	 *
	 * Otherwise, we know we can see the right edge of the current row.
	 *
	 * This must be a strict less than so that we can always see a
	 * horizontal wall, even if it is adjacent to us.
	 */
	if (right_mark < right_edge) {
	    for (right = right_mark; right <= right_edge; right++) {
		if (step < 0) {
		    q1_path(start_row,start_col,row,right,rside2);
		} else {
		    q4_path(start_row,start_col,row,right,rside2);
		}
rside2:					/* used if q?_path() is a macro */
		if (!result) break;
	    }
	    --right;	/* get rid of the last increment */
	}
	else
	    right = right_edge;

	/*
	 * We have the range that we want.  Set the bits.  Note that
	 * there is no else --- we no longer handle splinters.
	 */
	if (left <= right) {
	    /*
	     * An ugly special case.  If you are adjacent to a vertical wall
	     * and it has a break in it, then the right mark is set to be
	     * start_col.  We *want* to be able to see adjacent vertical
	     * walls, so we have to set it back.
	     */
	    if (left == right && left == start_col &&
			start_col < (COLNO-1) && !is_clear(row,start_col+1))
		right = start_col+1;

	    if(right > lim_max) right = lim_max;
	    /* set the bits */
	    if(vis_func)
		for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
	    else {
		for (i = left; i <= right; i++) set_cs(rowp,i);
		set_min(left);      set_max(right);
	    }

	    /* recursive call for next finger of light */
	    if (deeper) right_side(nrow,left,right,limits);
	    left = right + 1; /* no limit check necessary */
	}
    }
}


/*
 * This routine is the mirror image of right_side().  See right_side() for
 * extensive comments.
 */
STATIC_OVL void
left_side(row, left_mark, right, limits)
    int row, left_mark, right;
    char *limits;
{
    int		  left, left_edge, nrow, deeper, result;
    register int  i;
    register char *rowp;
    char	  *row_min, *row_max;
    int		  lim_min;

#ifdef GCC_WARN
    rowp = row_min = row_max = 0;
#endif
    nrow    = row+step;
    deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
    if(!vis_func) {
	rowp    = cs_rows[row];
	row_min = &cs_left[row];
	row_max = &cs_right[row];
    }
    if(limits) {
	lim_min = start_col - *limits;
	if(lim_min < 0) lim_min = 0;
	if(left_mark < lim_min) left_mark = lim_min;
	limits++; /* prepare for next row */
    } else
	lim_min = 0;

    while (right >= left_mark) {
	left_edge = left_ptrs[row][right];
	if(left_edge < lim_min) left_edge = lim_min;

	if (!is_clear(row,right)) {
	    /* Jump to the far side of a stone wall. */
	    if (left_edge < left_mark) {
		/* Maybe see more (kludge). */
		left_edge = is_clear(row-step,left_mark) ?
						    left_mark-1 : left_mark;
	    }
	    if(vis_func) {
		for (i = left_edge; i <= right; i++) (*vis_func)(i, row, varg);
	    } else {
		for (i = left_edge; i <= right; i++) set_cs(rowp,i);
		set_min(left_edge); set_max(right);
	    }
	    right = left_edge - 1; /* no limit check necessary */
	    continue;
	}

	if (right != start_col) {
	    /* Find the right side. */
	    for (; right >= left_edge; right--) {
		if (step < 0) {
		    q2_path(start_row,start_col,row,right,lside1);
		} else {
		    q3_path(start_row,start_col,row,right,lside1);
		}
lside1:					/* used if q?_path() is a macro */
		if (result) break;
	    }

	    /* Check for boundary conditions. */
	    if (right < lim_min) return;
	    if (right == lim_min) {
		if(vis_func) (*vis_func)(lim_min, row, varg);
		else {
		    set_cs(rowp,lim_min);
		    set_min(lim_min);
		}
		return;
	    }
	    /* Check if we can see any spots in the opening. */
	    if (right <= left_edge) {
		right = left_edge;
		continue;
	    }
	}

	/* Find the left side. */
	if (left_mark > left_edge) {
	    for (left = left_mark; left >= left_edge; --left) {
		if (step < 0) {
		    q2_path(start_row,start_col,row,left,lside2);
		} else {
		    q3_path(start_row,start_col,row,left,lside2);
		}
lside2:					/* used if q?_path() is a macro */
		if (!result) break;
	    }
	    left++;	/* get rid of the last decrement */
	}
	else
	    left = left_edge;

	if (left <= right) {
	    /* An ugly special case. */
	    if (left == right && right == start_col &&
			    start_col > 0 && !is_clear(row,start_col-1))
		left = start_col-1;

	    if(left < lim_min) left = lim_min;
	    if(vis_func)
		for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
	    else {
		for (i = left; i <= right; i++) set_cs(rowp,i);
		set_min(left);      set_max(right);
	    }

	    /* Recurse */
	    if (deeper) left_side(nrow,left,right,limits);
	    right = left - 1; /* no limit check necessary */
	}
    }
}


/*
 * Calculate all possible visible locations from the given location
 * (srow,scol).  NOTE this is (y,x)!  Mark the visible locations in the
 * array provided.
 */
STATIC_OVL void
view_from(srow, scol, loc_cs_rows, left_most, right_most, range, func, arg)
    int  srow, scol;	/* starting row and column */
    char **loc_cs_rows;	/* pointers to the rows of the could_see array */
    char *left_most;	/* min mark on each row */
    char *right_most;	/* max mark on each row */
    int range;		/* 0 if unlimited */
    void FDECL((*func), (int,int,genericptr_t));
    genericptr_t arg;
{
    register int i;		/* loop counter */
    char         *rowp;		/* optimization for setting could_see */
    int		 nrow;		/* the next row */
    int		 left;		/* the left-most visible column */
    int		 right;		/* the right-most visible column */
    char	 *limits;	/* range limit for next row */

    /* Set globals for q?_path(), left_side(), and right_side() to use. */
    start_col = scol;
    start_row = srow;
    cs_rows   = loc_cs_rows;	/* 'could see' rows */
    cs_left   = left_most;
    cs_right  = right_most;
    vis_func = func;
    varg = arg;

    /*
     * Determine extent of sight on the starting row.
     */
    if (is_clear(srow,scol)) {
	left =  left_ptrs[srow][scol];
	right = right_ptrs[srow][scol];
    } else {
	/*
	 * When in stone, you can only see your adjacent squares, unless
	 * you are on an array boundary or a stone/clear boundary.
	 */
	left  = (!scol) ? 0 :
		(is_clear(srow,scol-1) ? left_ptrs[srow][scol-1] : scol-1);
	right = (scol == COLNO-1) ? COLNO-1 :
		(is_clear(srow,scol+1) ? right_ptrs[srow][scol+1] : scol+1);
    }

    if(range) {
	if(range > MAX_RADIUS || range < 1)
	    panic("view_from called with range %d", range);
	limits = circle_ptr(range) + 1; /* start at next row */
	if(left < scol - range) left = scol - range;
	if(right > scol + range) right = scol + range;
    } else
	limits = (char*) 0;

    if(func) {
	for (i = left; i <= right; i++) (*func)(i, srow, arg);
    } else {
	/* Row pointer optimization. */
	rowp = cs_rows[srow];

	/* We know that we can see our row. */
	for (i = left; i <= right; i++) set_cs(rowp,i);
	cs_left[srow]  = left;
	cs_right[srow] = right;
    }

    /*
     * Check what could be seen in quadrants.  We need to check for valid
     * rows here, since we don't do it in the routines right_side() and
     * left_side() [ugliness to remove extra routine calls].
     */
    if ( (nrow = srow+1) < ROWNO ) {	/* move down */
	step =  1;
	if (scol < COLNO-1) right_side(nrow, scol, right, limits);
	if (scol)	    left_side (nrow, left,  scol, limits);
    }

    if ( (nrow = srow-1) >= 0 ) {	/* move up */
	step = -1;
	if (scol < COLNO-1) right_side(nrow, scol, right, limits);
	if (scol)	    left_side (nrow, left,  scol, limits);
    }
}

#endif	/*===== End of algorithm C =====*/

/*
 * AREA OF EFFECT "ENGINE"
 *
 * Calculate all possible visible locations as viewed from the given location
 * (srow,scol) within the range specified. Perform "func" with (x, y) args and
 * additional argument "arg" for each square.
 *
 * If not centered on the hero, just forward arguments to view_from(); it
 * will call "func" when necessary.  If the hero is the center, use the
 * vision matrix and reduce extra work.
 */
void
do_clear_area(scol,srow,range,func,arg)
    int scol, srow, range;
    void FDECL((*func), (int,int,genericptr_t));
    genericptr_t arg;
{
	/* If not centered on hero, do the hard work of figuring the area */
	if (scol != u.ux || srow != u.uy)
	    view_from(srow, scol, (char **)0, (char *)0, (char *)0,
							range, func, arg);
	else {
	    register int x;
	    int y, min_x, max_x, max_y, offset;
	    char *limits;

	    if (range > MAX_RADIUS || range < 1)
		panic("do_clear_area:  illegal range %d", range);
	    if(vision_full_recalc)
		vision_recalc(0);	/* recalc vision if dirty */
	    limits = circle_ptr(range);
	    if ((max_y = (srow + range)) >= ROWNO) max_y = ROWNO-1;
	    if ((y = (srow - range)) < 0) y = 0;
	    for (; y <= max_y; y++) {
		offset = limits[v_abs(y-srow)];
		if((min_x = (scol - offset)) < 0) min_x = 0;
		if((max_x = (scol + offset)) >= COLNO) max_x = COLNO-1;
		for (x = min_x; x <= max_x; x++)
		    if (couldsee(x, y))
			(*func)(x, y, arg);
	    }
	}
}

/*vision.c*/