File: world.cpp

package info (click to toggle)
xevil 2.02r2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,888 kB
  • sloc: cpp: 47,789; makefile: 189; csh: 4
file content (3271 lines) | stat: -rw-r--r-- 87,482 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
/* 
 * XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
 * http://www.xevil.com
 * satan@xevil.com
 *
 * 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, the file "gpl.txt"; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA, or visit http://www.gnu.org.
 */

// "world.cpp"

#if X11
#ifndef NO_PRAGMAS
#pragma implementation "world.cpp"
#endif
#endif

// Include Files.
#include "stdafx.h"

#if WIN32
#include "resource.h"
#endif

// Include Files.
#include <iostream>

#include "utils.h"
#include "coord.h"
#include "xdata.h"
#include "area.h"

#include "world.h"
#include "bitmaps/world/world.bitmaps"

// For PhysMover.
#include "actual.h"

using namespace std;

// Defines.
#define HANGING_PERCENT 0.40 // The % of the edge hanging off the corner.
#define STRAND_PERCENT .50 // Gives number of boundaries between rooms.
#define STRAND_LENGTH 8 // Length of strands in Blueprints::fill_random.
#define EMPTY_TRIES_MAX 5000
#define WALL_LENGTH 30
#define WALLS_PERCENT .01
#define WALLS_HORIZ_CHANCE 4 // 2 gives equal chance
#define LADDER_CHANCE 10
#define MAP_PRINT_DEFAULT False
#define UP_DOWN_CHANCE 4
#define OPEN_ITERATIONS 2


#define POSTERS_ACTUAL_PERCENT .0003  // Percentage of dim.
#define DOORS_ACTUAL_PERCENT .1  // Pairs of doors as percentage of rooms.
// Percentage of rooms that can possibly have movers.

// both are multipliers on number of rooms.
#define MOVERS_PERCENT .4
// Tries to find a space for the mover.
#define MOVER_TRIES 6

// Min number of movers across a horizontal track must be.
#define MOVERS_HORIZ_MIN_TRACK 6 // moverwidths
#define MOVERS_HORIZ_TRACK_LENGTH 25 // wsquares

// Used to determine how much space to leave between walls.
// Only the default, actual variable is objectMinDim.
#define OBJECT_COL_MAX 2
#define OBJECT_ROW_MAX 2
#define OBJECT_BIG_COL_MAX 2
#define OBJECT_BIG_ROW_MAX 3



// Blueprints class.  A helper for World.
class Blueprints {
 public:
  Blueprints(WorldP world,char map[W_ROW_MAX_MAX][W_COL_MAX_MAX],
	           const Dim &objectDimMax,int specialMap);
  /* EFFECTS: Create a blueprints object with room boundaries defined. */

  int get_middle_row(int down);
  int get_middle_col(int across);
  /* NOTES: Offset from edge of room, in WSQUARES.  Floor at middleRow.
     Ladder at [middleCol ... middleCol + objectDimMax.colMax). */


  // NOTE: The following three methods replace Blueprints::fill_map.
  void room_boundries();
  /* MODIFIES: map */
  /* EFFECTS: Using the defined room boundaries, create a map of the world. */

  void extra_walls();
  /* MODIFIES: map */

  void up_downs();
  /* MODIFIES: map */
  /* EFFECTS: Change all walls on edge of ladders to up_downs. */


  void print();
  /* EFFECTS: Print the room boundaries to cout. */

  Boolean blocked_right(const RoomIndex &ri);
  Boolean blocked_bottom(const RoomIndex &ri);
  Boolean blocked_left(const RoomIndex &ri);
  Boolean blocked_top(const RoomIndex &ri);
  /* EFFECTS: Returns whether ri has a blocking wall on the edge of the
     room. */


 private:
  void fill_random();
  /* EFFECTS: Snake algorithm to define boundries between rooms. */

  void special_map_adjust();
  /* EFFECTS: Adjust map for any requirements imposed by the
     special map specification. */

  Boolean open(const RoomIndex &ri);
  void has_right(const RoomIndex &ri,const Loc &rl);
  void missing_right(const RoomIndex &ri,const Loc &rl);
  void has_bottom(const RoomIndex &ri,const Loc &rl);
  void missing_bottom(const RoomIndex &ri,const Loc &rl);
  void has_top(const RoomIndex &ri,const Loc &rl);
  void missing_top(const RoomIndex &ri,const Loc &rl);
  void has_left(const RoomIndex &ri,const Loc &rl);
  void missing_left(const RoomIndex &ri,const Loc &rl);

  void ladder(const Loc &loc);
  /* EFFECTS: Draw a ladder going up and down from loc until a wall is hit. */

  Boolean wall_below(int rTop,int cTop,int num);
  /* EFFECTS: Return whether there is a Wwall or WupDown in the vertical
     column of num blocks whose top is (rTop,cTop). */

  WorldP world;
  Rooms worldRooms;
  Dim worldDim;
  char (* map)[W_COL_MAX_MAX];
  int middleRows[W_DOWN_MAX_MAX];
  int middleCols[W_ACROSS_MAX_MAX];
  Boolean horiz[W_DOWN_MAX_MAX + 1][W_ACROSS_MAX_MAX];
  Boolean vert[W_DOWN_MAX_MAX][W_ACROSS_MAX_MAX + 1];
  WSpecialMap specialMap;
  Dim objectDimMax;
};



Blueprints::Blueprints(WorldP w,char m[W_ROW_MAX_MAX][W_COL_MAX_MAX],
		                   const Dim &o_dim_max,WSpecialMap special) {
  objectDimMax = o_dim_max;
  world = w;
  worldRooms = world->get_rooms();
  worldDim = world->get_dim();
  map = m;
  specialMap = special;

  RoomIndex ri;

  // Boundaries only at the edges of the world.
  for (ri.across = 0; ri.across < worldRooms.acrossMax + 1; ri.across++) {
    for (ri.down = 0; ri.down < worldRooms.downMax + 1; ri.down++) {
      if (ri.across < worldRooms.acrossMax) {
	      if ((ri.down == 0) || (ri.down == worldRooms.downMax)) {
	        horiz[ri.down][ri.across] = True;
        }
	      else {
	        horiz[ri.down][ri.across] = False;
        }
    	}
      if (ri.down < worldRooms.downMax) {
//#if 0
	      if ((ri.across == 0) || (ri.across == worldRooms.acrossMax)) {
	        vert[ri.down][ri.across] = True;
        }
	      else {
//#endif
	        vert[ri.down][ri.across] = False;
        }
    	}
    }
  }

  // The maze algorithm.
  if (specialMap != World::ZIG_ZAG) {
    // ZIG_ZAG takes care of itself in special_map_adjust.
    fill_random();
  }

  // Any special adjustment for special world maps.
  special_map_adjust();


  assert(W_ROOM_ROW_MAX - 2 * (objectDimMax.rowMax + 1) > 0);
  assert(W_ROOM_COL_MAX - 2 * (objectDimMax.colMax + 1) > 0);

  // Compute middles
  for (int riR = 0; riR < worldRooms.downMax; riR++) {
    middleRows[riR] = objectDimMax.rowMax + 1 +
      Utils::choose(W_ROOM_ROW_MAX - 2 * (objectDimMax.rowMax + 1));
  }

  for (int riC = 0; riC < worldRooms.acrossMax; riC++) {
    middleCols[riC] = objectDimMax.colMax + 1 +
      Utils::choose(W_ROOM_COL_MAX - 2 * (objectDimMax.colMax + 1));
  }
}



int Blueprints::get_middle_row(int down) {
  assert (0 <= down && down < worldRooms.downMax);
  return middleRows[down];
}



int Blueprints::get_middle_col(int across) {
  assert (0 <= across && across < worldRooms.acrossMax);
  return middleCols[across];
}



void Blueprints::room_boundries() {
  RoomIndex ri;
  Loc rl;

  // Walls between rooms.
  for (ri.across = 0, rl.c = 0;
       ri.across < worldRooms.acrossMax;
       ri.across++, rl.c += W_ROOM_COL_MAX) {
    for (ri.down = 0, rl.r = 0;
	       ri.down < worldRooms.downMax;
	       ri.down++, rl.r += W_ROOM_ROW_MAX) {
      if (! horiz[ri.down][ri.across]) {
	      missing_top(ri,rl);
      }
      else {
	      has_top(ri,rl);
      }
      if (! horiz[ri.down + 1][ri.across]) {
	      missing_bottom(ri,rl);
      }
      else {
	      has_bottom(ri,rl);
      }
      if (! vert[ri.down][ri.across]) {
	      missing_left(ri,rl);
      }
      else {
	      has_left(ri,rl);
      }
      if (! vert[ri.down][ri.across + 1]) {
	      missing_right(ri,rl);
      }
      else {
	      has_right(ri,rl);
      }
    }
  }
}



void Blueprints::extra_walls() {
  for (int walls = 0;
       walls < worldDim.rowMax * worldDim.colMax * WALLS_PERCENT;
       walls ++) {
    Boolean ok = True;
    Loc loc;
    loc.r = Utils::choose(worldDim.rowMax);
    loc.c = Utils::choose(worldDim.colMax);
    int delta = Utils::coin_flip() ? 1 : -1;
    int horiz = Utils::choose(WALLS_HORIZ_CHANCE);

    // Special case for SEALS special world, no vertical walls.
    if (specialMap == World::SEALS) {
      horiz = 1;
    }

    Loc check;
    for (check.c = loc.c - objectDimMax.colMax;
	       check.c <= loc.c + objectDimMax.colMax;
	       check.c++) {
	    for (check.r = loc.r - objectDimMax.rowMax;
	         check.r <= loc.r + objectDimMax.rowMax;
	         check.r++) {
	      if (!world->open(check,True)) {
	        ok = False;
        }
      }
    }

    // Horizontal extra wall.
    if (horiz) {
	    while (ok && Utils::choose(WALL_LENGTH)) {
	      for (check.c = loc.c;
		         check.c != loc.c + delta * (objectDimMax.colMax + 1);
		         check.c += delta) {
	        for (check.r = loc.r - objectDimMax.rowMax;
                check.r <= loc.r + objectDimMax.rowMax;
                check.r++) {
		        if (!world->open(check,True)) {
		          ok = False;
            }
          }
        }

	      if (ok) {
		      if (! Utils::choose(LADDER_CHANCE)) {
		        ladder(loc);
          }
		      else {
		        map[loc.r][loc.c] = Wwall;
          }

    		  loc.c += delta;

		      if (specialMap != World::SEALS && 
              Utils::choose(UP_DOWN_CHANCE) == 0) {
		        loc.r += (Utils::coin_flip() ? 1 : -1);
          }
        }
      }
    }
    // Vertical wall, horiz == 0
    else {
	    while (ok && Utils::choose(WALL_LENGTH)) {
        for (check.r = loc.r;
             check.r != loc.r + delta * (objectDimMax.rowMax + 1);
             check.r += delta) {
          for (check.c = loc.c - objectDimMax.colMax;
               check.c <= loc.c + objectDimMax.colMax;
               check.c++) {
            if (!world->open(check,True)) {
	            ok = False;
            }
          }
        }
        if (ok) {
          map[loc.r][loc.c] = Wwall;
          loc.r += delta;
        }
      }
    }
  } // for walls.
}



void Blueprints::up_downs() {
  Loc loc;

  // Change Wwall to WupDown if a ladder is immediately to left or right.
  for (loc.c = 0; loc.c < worldDim.colMax; loc.c++) {
    for (loc.r = 0; loc.r < worldDim.rowMax; loc.r++) {
      if ((map[loc.r][loc.c] == Wwall) &&
          (((loc.c + 1 < worldDim.colMax) &&
	          (map[loc.r][loc.c + 1] == Wladder)) ||
           ((loc.c - 1 >= 0) &&
	          (map[loc.r][loc.c - 1] == Wladder)))) {
	      map[loc.r][loc.c] = WupDown;
      }
    }
  }


  // Change Wwall to WupDown if doesn't have a wall to right AND left.
  for (loc.c = 0; loc.c < worldDim.colMax; loc.c++) {
    for (loc.r = 0; loc.r < worldDim.rowMax; loc.r++) {
      if (map[loc.r][loc.c] == Wwall &&
          !((wall_below(loc.r - 1,loc.c - 1,3) &&
	           wall_below(loc.r - 1,loc.c + 1,3)) ||
	          wall_below(loc.r - 1,loc.c,1) ||
	          wall_below(loc.r + 1,loc.c,1))) {
        map[loc.r][loc.c] = WupDown;
      }
    }
  }
}



void Blueprints::print() {
  RoomIndex ri;

  for (ri.down = 0; ri.down <= worldRooms.downMax; ri.down++) {
    // Horizontal boundaries.
    for (ri.across = 0; ri.across < worldRooms.acrossMax; ri.across++) {
      cout << ((horiz[ri.down][ri.across]) ? "**" : "*-");
      cout << "*" << endl;

      // Vertical boundaries.
      if (ri.down < worldRooms.downMax) {
        for (ri.across = 0; ri.across <= worldRooms.acrossMax; ri.across++) {
          cout << ((vert[ri.down][ri.across]) ? "* " : "| ");
        }
      }

      cout << endl;
    }
  }
}



Boolean Blueprints::blocked_right(const RoomIndex &ri) {
  assert(ri.down < worldRooms.downMax && ri.down >= 0);
  assert(ri.across < worldRooms.acrossMax && ri.across >= 0);

  return vert[ri.down][ri.across + 1];
}



Boolean Blueprints::blocked_bottom(const RoomIndex &ri) {
  assert(ri.down < worldRooms.downMax && ri.down >= 0);
  assert(ri.across < worldRooms.acrossMax && ri.across >= 0);

  return horiz[ri.down + 1][ri.across];
}



Boolean Blueprints::blocked_left(const RoomIndex &ri) {
  assert(ri.down < worldRooms.downMax && ri.down >= 0);
  assert(ri.across < worldRooms.acrossMax && ri.across >= 0);

  return vert[ri.down][ri.across];
}



Boolean Blueprints::blocked_top(const RoomIndex &ri) {
  assert(ri.down < worldRooms.downMax && ri.down >= 0);
  assert(ri.across < worldRooms.acrossMax && ri.across >= 0);

  return horiz[ri.down][ri.across];
}



void Blueprints::fill_random() {
  // Don't do maze algorithm if the world is too small.
  if ((worldRooms.acrossMax < 2) || (worldRooms.downMax < 2)) {
    return;
  }

  int strandsNum = 
    (int)(worldRooms.acrossMax * worldRooms.downMax * STRAND_PERCENT);
  for (int n = 0; n < strandsNum; n++) {
    // Starting position.
    RoomIndex ri;
    ri.across = Utils::choose(worldRooms.acrossMax - 1) + 1;
    ri.down = Utils::choose(worldRooms.downMax - 1) + 1;

    Boolean ok = open(ri);
    // open implies that ri must not be on outer boundaries.
    while (Utils::choose(STRAND_LENGTH) && ok) {
      // Move horiz.
	    if (Utils::coin_flip()) {
        // Increase.
        if (Utils::coin_flip()) {
          horiz[ri.down][ri.across] = True;
          ri.across++;
          ok = open(ri);
        }
        // Decrease.
        else {
          horiz[ri.down][ri.across - 1] = True;
          ri.across--;
          ok = open(ri);
        }
  	  }
      // Move vert.
      else {
        // Increase.
        if (Utils::coin_flip()) {
          vert[ri.down][ri.across] = True;
          ri.down++;
          ok = open(ri);
        }
        // Decrease.
        else {
          vert[ri.down - 1][ri.across] = True;
          ri.down--;
          ok = open(ri);
        }
      }
		}	  
  }
}



void Blueprints::special_map_adjust() {
  switch (specialMap) {
    case World::SEALS: {
      // remove all interior vertical walls.

      RoomIndex ri;
      for (ri.down = 0; ri.down < worldRooms.downMax; ri.down++) {
        for (ri.across = 1; ri.across < worldRooms.acrossMax; ri.across++) {
          vert[ri.down][ri.across] = False;
        }
      }
    }
    break;

    case World::ZIG_ZAG: {
      RoomIndex ri;

      // Loop over all but top and bottom rows.
      for (ri.down = worldRooms.downMax - 1; ri.down > 0; ri.down--) {
        for (ri.across = 0; ri.across < worldRooms.acrossMax; ri.across++) {
          // Every row alternates having right and left side open
          // First row on bottom should have right open.
          Boolean rightOpen = ((worldRooms.downMax - ri.down) % 2 == 1);

          if ((rightOpen &&
	             ri.across != worldRooms.acrossMax - 1) ||
	            (!rightOpen &&
	             ri.across != 0)) {
	          horiz[ri.down][ri.across] = True;
          }
        }
      }

    }
    break;
  }
}



Boolean Blueprints::open(const RoomIndex &ri) {
  assert((ri.across >= 0) && 
         (ri.across <= worldRooms.acrossMax) &&
	       (ri.down >= 0) && 
         (ri.down <= worldRooms.downMax));

  if ((ri.across > 0) && horiz[ri.down][ri.across - 1]) {
    return False;
  }

  if ((ri.across < worldRooms.acrossMax) && horiz[ri.down][ri.across]) {
    return False;
  }

  if ((ri.down > 0) && vert[ri.down - 1][ri.across]) {
    return False;
  }

  if ((ri.down < worldRooms.downMax) && vert[ri.down][ri.across]) {
    return False;
  }

  return True;
}



void Blueprints::has_top(const RoomIndex &,const Loc &roomLoc) {
  for (int c = roomLoc.c; c < roomLoc.c + W_ROOM_COL_MAX; c++)
    map[roomLoc.r][c] = Wwall;
}



void Blueprints::missing_top(const RoomIndex &roomIndex,
				                     const Loc &roomLoc) {
  for (int c = roomLoc.c + middleCols[roomIndex.across];
       c < roomLoc.c + middleCols[roomIndex.across] + objectDimMax.colMax;
       c++) {
    for (int r = roomLoc.r; r < roomLoc.r + W_ROOM_ROW_MAX - 1; r++) {
      map[r][c] = Wladder;
    }
  }
}



void Blueprints::has_bottom(const RoomIndex &,
			                      const Loc &roomLoc) {
  for (int c = roomLoc.c; c < roomLoc.c + W_ROOM_COL_MAX; c++) {
    map[roomLoc.r + W_ROOM_ROW_MAX - 1][c] = Wwall;
  }
}



void Blueprints::missing_bottom(const RoomIndex &roomIndex,
				                        const Loc &roomLoc) {
  for (int c = roomLoc.c + middleCols[roomIndex.across];
       c < roomLoc.c + middleCols[roomIndex.across] + objectDimMax.colMax;
       c++) {
    for (int r = roomLoc.r + middleRows[roomIndex.down] - objectDimMax.rowMax;
         r < roomLoc.r + W_ROOM_ROW_MAX;
         r++) {
      map[r][c] = Wladder;
    }
  }
}



void Blueprints::has_left(const RoomIndex &,const Loc &roomLoc) {
  for (int r = roomLoc.r; r < roomLoc.r + W_ROOM_ROW_MAX; r++) {
    map[r][roomLoc.c] = Wwall;
  }
}



void Blueprints::missing_left(const RoomIndex &roomIndex,
                              const Loc &roomLoc) {
  for (int c = roomLoc.c; c < roomLoc.c + middleCols[roomIndex.across]; c++) {
    map[roomLoc.r + middleRows[roomIndex.down]][c] = Wwall;
  }
}



void Blueprints::has_right(const RoomIndex &,const Loc &roomLoc) {
  for (int r = roomLoc.r; r < roomLoc.r + W_ROOM_ROW_MAX; r++) {
    map[r][roomLoc.c + W_ROOM_COL_MAX - 1] = Wwall;
  }
}



void Blueprints::missing_right(const RoomIndex &roomIndex,
                               const Loc &roomLoc) {
  for (int c = roomLoc.c + middleCols[roomIndex.across] + objectDimMax.colMax;
       c < roomLoc.c + W_ROOM_COL_MAX; 
       c++) {
    map[roomLoc.r + middleRows[roomIndex.down]][c] = Wwall;
  }
}



void Blueprints::ladder(const Loc &init) {
  // delta is only -1 and 1.
  for (int delta = -1; delta <= 1; delta += 2) {
    Loc loc = init;

    // To avoid hitting init twice.
    if (delta == 1) {
	    loc.r++;
    }

    // Running directly into wall or ladder.
    while (world->open(loc,True)) {
      map[loc.r][loc.c] = Wladder;

      // Stop when there is a wall to the left or right.
      Loc left, right;
      right.r = left.r = loc.r;
      left.c = loc.c - 1;
      right.c = loc.c + 1;
      if ((!world->open(left) || !world->open(right))) {
        // Extra extension at top.
        if (delta == -1) {
          for (int extra = 1; extra <= objectDimMax.rowMax; extra++) {
		        Loc extraLoc;
		        extraLoc.c = loc.c;
		        extraLoc.r = loc.r - extra;
		        if (world->open(extraLoc,True)) {
		          map[extraLoc.r][extraLoc.c] = Wladder;
            }
		        else {
		          break;
            }
	        }
        }
	      break;
	    }

      loc.r += delta;
    } // while
  } // for delta
}



Boolean Blueprints::wall_below(int rTop,int cTop,int num) {
  Loc loc;
  loc.c = cTop;

  for (loc.r = rTop; loc.r < rTop + num; loc.r++) {
    if (world->inside(loc) &&
        (map[loc.r][loc.c] == Wwall || map[loc.r][loc.c] == WupDown)) {
      return True;
    }
  }

  return False;
}



// Functions for Mover class.
Mover::Mover(InStreamP in,WorldP w,LocatorP l) {
  world = w;
  locator = l;
  areaSet = True;
  timerSet = False;

  area.read(in);
  moverId.read(in);
  physMoverId.read(in);
}



void Mover::update_from_stream(const Area &a) {
  assert(areaSet);
  area = a;
}



int Mover::get_write_length() {
  return 
    Area::get_write_length() +        // area
    Identifier::get_write_length() + // moverId
    Identifier::get_write_length();  // physMoverId
}



void Mover::write(OutStreamP out) {
  area.write(out);
  moverId.write(out);
  physMoverId.write(out);
}



void Mover::init(WorldP w,LocatorP l,const Area &a,const Size &v,
                 MoverId mId) {
  world = w;
  locator = l;
  area = a;
  areaSet = True;
  vel = v;
  moverId = mId;
  timerSet = False;
  assert(check_area(area));
}



void Mover::init_area(const Area &a) {
  area = a;
  areaSet = True;
}



void Mover::init_not_area(WorldP w,LocatorP l,const Size &v,MoverId mId) {
  world = w;
  locator = l;
  vel = v;
  moverId = mId;
  timerSet = False;
  assert(check_area(area));
}



void Mover::clock() {
  // Vel is always valid and gives what the PREVIOUS velocity was.

  // Should never be off the track.
  assert(check_area(area));

  // not pausing at top or bottom.
  if (!timerSet) {
    // Make sure areaNext is still on the track of moverSquares.
    Area areaNext = area + vel;

    // Just move along.
    if (check_area(areaNext)) {
      area = areaNext;
    }
    // change direction.
    else if (area.wsquare_alligned()) {
      // Set timer.
      timer.set(W_MOVER_PAUSE_TIME);
      timerSet = True;
      // remember velocity
      velStored.width = -vel.width;
      velStored.height = -vel.height;
      vel.set_zero();
      // area doesn't change.
    }
    else {
      areaNext = area.wsquare_allign(vel);
      // After wsquare_allign(), area shouldn't be touching wsquares it
      // wasn't already touching.
      assert(check_area(areaNext) && !(area == areaNext));
      assert(areaNext.wsquare_alligned());
      vel = areaNext - area;
      area = areaNext;
    }
  } // timerSet

  // pausing at top or bottom.
  else {
    // Reverse direction.
    if (timer.ready() ) {
      // Can't use areaNext.
      // vel = velStored;
      // hack
      vel.width = (velStored.width > 0) ? W_MOVER_SPEED : ((velStored.width < 0) ? -W_MOVER_SPEED : 0);
      vel.height = (velStored.height > 0) ? W_MOVER_SPEED : ((velStored.height < 0) ? -W_MOVER_SPEED : 0);

      Area areaNext = area + vel;
      if (check_area(areaNext)) {
        area = areaNext;
      }
      // mover is stuck on a two square wide spot and isn't going anywhere.
      else {
        vel.set_zero();
      }

      timerSet = False;
    }
  }

  timer.clock();

  // Tell PhysMover that we've been clocked, so can update the Area it
  // presents to the world.
  PhysMoverP pM = (PhysMoverP)locator->lookup(physMoverId);
  if (pM) {
    pM->mover_clock();
  }
}



// True iff all squares covering area are moverSquares for this Mover.
Boolean Mover::check_area(const Area &area) {
  Loc list[AR_WSQUARES_MAX];
  int nItems;
  area.wsquares(list,nItems);
  // go through all wsquares on area.
  for (int m = 0; m < nItems; m++) {
    const Loc &loc = list[m];
    // wsquare outside the world.
    if (!world->inside(loc)) {
      return False;
    }
    // Mover is friend of World. (might change)
    UnionSquare *uSquare = world->unionSquares[loc.r][loc.c];
    if (!uSquare ||
        uSquare->type != UN_MOVER ||
        uSquare->mSquare.mover != this)
      return False;
  } // for
  return True;
}



UnionSquare *UnionSquare::read(InStreamP in,WorldP world) {
  UnionSquare *ret = new UnionSquare;
  assert(ret);
  
  ret->type = (int)in->read_char();

  switch (ret->type) {
  case UN_POSTER:
    ret->pSquare.poster = in->read_short();
    ret->pSquare.loc.read(in);
    in->read_short(); // padding
  break;

  case UN_DOOR:
    ret->dSquare.topBottom = in->read_short();
    ret->dSquare.dest.read(in);
    in->read_short(); // padding
  break;

  case UN_MOVER: {
    ret->mSquare.orientation = in->read_short();
    MoverId mId(in);
    ret->mSquare.mover = world->lookup(mId);
  }
  break;
    
  default:
    cerr << "Received invalid type of UnionSquare." << endl;
  }
  return ret;
}



int UnionSquare::get_write_length() {
  // Add padding to make Loc the size of Identifier.
  assert(Identifier::get_write_length() == Loc::get_write_length() + 2);

  return 
    sizeof(char) +              // type
    sizeof(short)  +              // poster/topBottom/orientation
    Identifier::get_write_length();    // loc/dest/moverId
}



void UnionSquare::write(OutStreamP out) {
  out->write_char((u_char)type);

  switch (type) {
  case UN_POSTER:
    assert(pSquare.poster <= USHRT_MAX);
    out->write_short((u_short)pSquare.poster);
    pSquare.loc.write(out);
    out->write_short(666); // padding
  break;

  case UN_DOOR:
    assert(dSquare.topBottom <= USHRT_MAX);
    out->write_short((u_short)dSquare.topBottom);
    dSquare.dest.write(out);
    out->write_short(666); // padding
  break;

  case UN_MOVER: {
    assert(mSquare.orientation <= USHRT_MAX);  
    out->write_short((u_short)mSquare.orientation);
    assert(mSquare.mover);
    MoverId mId = mSquare.mover->get_mover_id();
    mId.write(out);
  }
  break;
    
  default:
    assert(0);
  }
}



// Functions for World class.
World::World() {
  // Set by World::set_locator();
  locator = NULL;

  // Get pointer to static list of themes.
  themes = World_themes;

  objectDimMax.colMax = OBJECT_COL_MAX;
  objectDimMax.rowMax = OBJECT_ROW_MAX;

  mapPrint = MAP_PRINT_DEFAULT;
  rooms.acrossMax = 1;
  rooms.downMax = 1;
  roomsNext = rooms;
  rooms_dim_size_update();
  blueprints = NULL;
  moversNum = 0;
  uniqueGen = 0;
  justReset = True;

  specialMap = specialMapNext = MAP_NONE;

  worldFile = NULL;

  xValid = XVARS_VALID_INIT;

  Loc loc;
  for (loc.r = 0; loc.r < W_ROW_MAX_MAX; loc.r++) {
    for (loc.c = 0; loc.c < W_COL_MAX_MAX; loc.c++) {
      unionSquares[loc.r][loc.c] = NULL;
      map[loc.r][loc.c] = Wempty;
    }
  }

  
  // Initialize posterDims
  for (int n = 0; n < W_ALL_POSTERS_NUM; n++) {
    assert(posters[n].size.width % WSQUARE_WIDTH == 0 &&
           posters[n].size.width % WSQUARE_HEIGHT == 0);
    // The scaling factor of 2 is always true, regardless of whether we
    // are dealing with stretched coordinates or not.
    posterDims[n].colMax = posters[n].size.width / (WSQUARE_WIDTH * 2);
    posterDims[n].rowMax = posters[n].size.height / (WSQUARE_HEIGHT * 2);
  }
  
  choose_theme();

  // Caller now must explicitly ask for the title_map().
}



World::~World() {
  clear_everything();

  Utils::freeif(worldFile);
  
	// Generate new map.
  if (blueprints) {
    delete blueprints;
  }
}



void World::title_map(IViewportInfo* vInfoProvider) {
  ViewportInfo vInfo = vInfoProvider->get_info();
  Dim vDim = vInfo.get_viewport_dim();

  // Set to the dimensions of the viewport.
  clear_everything();
  rooms_dim_size_update(&vDim);


  // Put wall on top and bottom.
  Loc loc;
  for (loc.r = 0; loc.r < dim.rowMax; loc.r++) {
    for (loc.c = 0; loc.c < dim.colMax; loc.c++) {
      // Top and bottom.
      if ((loc.r == (dim.rowMax - 1)) || (loc.r == 0)) {
        map[loc.r][loc.c]= Wwall;
      }
    }
  }
        

  // Put title poster in center of screen.
  //
  // Title poster is always the last poster in the list.
  Loc start;
  start.c = dim.colMax / 2 - posterDims[W_ALL_POSTERS_NUM - 1].colMax / 2;
  start.r = dim.rowMax / 2 - posterDims[W_ALL_POSTERS_NUM - 1].rowMax / 2;

  for (loc.c = start.c; 
       loc.c <  start.c + posterDims[W_ALL_POSTERS_NUM - 1].colMax; 
       loc.c++) {
    for (loc.r = start.r; 
         loc.r < start.r + posterDims[W_ALL_POSTERS_NUM - 1].rowMax; 
         loc.r++) {
      if (! unionSquares[loc.r][loc.c]) {
        unionSquares[loc.r][loc.c] = new UnionSquare;
      }
      assert(unionSquares[loc.r][loc.c]);
      unionSquares[loc.r][loc.c]->type = UN_POSTER;
      unionSquares[loc.r][loc.c]->pSquare.poster = W_ALL_POSTERS_NUM - 1;
      unionSquares[loc.r][loc.c]->pSquare.loc.c = loc.c - start.c;
      unionSquares[loc.r][loc.c]->pSquare.loc.r = loc.r - start.r;
    }
  }
}



Dim World::get_room_dim() {
  Dim ret(W_ROOM_ROW_MAX,W_ROOM_COL_MAX);
  return ret;
}



Size World::get_room_size() {
  Size ret;
  ret.width = W_ROOM_COL_MAX * WSQUARE_WIDTH;
  ret.height = W_ROOM_ROW_MAX * WSQUARE_HEIGHT;
  return ret;
}



Boolean World::check_door(const Loc &loc,Loc &dest) {
  if (unionSquares[loc.r][loc.c] &&
      unionSquares[loc.r][loc.c]->type == UN_DOOR) {
    dest = unionSquares[loc.r][loc.c]->dSquare.dest;
    return True;
  }
  return False;
}



void World::set_rooms_next(const Rooms &r) {
  Utils::freeif(worldFile);

  roomsNext.acrossMax = Utils::minimum(W_ACROSS_MAX_MAX,r.acrossMax);
  roomsNext.downMax = Utils::minimum(W_DOWN_MAX_MAX,r.downMax);
}



void World::set_special_map_next(WSpecialMap map) {
  Utils::freeif(worldFile);
  specialMapNext = map;

  switch (map) {
  case SEALS:
    roomsNext.acrossMax = 4;
    roomsNext.downMax = 2;
    break;

  case ZIG_ZAG:
    roomsNext.acrossMax =  W_ACROSS_MAX_MAX;
    roomsNext.downMax = 3; // W_DOWN_MAX_MAX;
    break;

  case FLAG:
    roomsNext.acrossMax = 6;
    roomsNext.downMax = 3;
    break;

  case MAP_NONE:
    break;

  default:
    assert(0);
  }
}



Boolean World::overlap(const Box &box) {
  return ((box.loc.c < dim.colMax) &&
	  (box.loc.c + box.dim.colMax > 0) &&
	  (box.loc.r < dim.rowMax) &&
	  (box.loc.r + box.dim.rowMax > 0));
}



void World::draw(CMN_DRAWABLE buffer,Xvars &xvars,int dpyNum,
                 const Area &area,
                 Boolean reduceDraw,Boolean background3D) {
  if (!xvars.is_valid(xValid)) {
    init_x(xvars,IX_INIT,NULL);
  }

  if (!reduceDraw) {
    draw_background(buffer,xvars,dpyNum,area,background3D);
  }

  Pos aPos;
  Size aSize;
  area.get_rect(aPos,aSize);

  int moversFoundNum = 0;
  MoverP moversFound[MOVERS_MAX];

  // Draw squares and create list of movers encountered.
  Loc loc;
  for (loc.c = (int)floor((float)aPos.x / (float)WSQUARE_WIDTH);
       loc.c < (int)ceil((float)(aPos.x + aSize.width) / (float)WSQUARE_WIDTH);
       loc.c++) {
    for (loc.r = (int)floor((float)aPos.y / (float)WSQUARE_HEIGHT);
         loc.r < ceil((float)(aPos.y + aSize.height)/(float)WSQUARE_HEIGHT);
         loc.r++) {
      draw_square(buffer,xvars,dpyNum,loc,
                  loc.c * WSQUARE_WIDTH - aPos.x,
                  loc.r * WSQUARE_HEIGHT - aPos.y,reduceDraw);

      mover_list_add(moversFound,moversFoundNum,loc);
    }
  }

  // Draw movers once.
  for (int n = 0; n < moversFoundNum; n++) {
    Pos movPos;
    Size movSize;
    const Area &area = moversFound[n]->get_area();
    area.get_rect(movPos,movSize);
    draw_mover(buffer,xvars,dpyNum,moversFound[n],
               movPos.x - aPos.x,movPos.y - aPos.y);
  }

  if (!reduceDraw) {
    draw_outside(buffer,xvars,dpyNum,area);
  }
}



void World::draw_outside(CMN_DRAWABLE buffer,Xvars &xvars,int dpyNum,
                         Area area) {
  if (!xvars.is_valid(xValid)) {
    init_x(xvars,IX_INIT,NULL);
  }
  
  // In window coords. 
  area = xvars.stretch_area(area);
  Pos pos = area.get_pos();
  Size size = area.get_size();
  Size worldSize = xvars.stretch_size(get_size());
  
  // Yuck, divide by two and stretch.
  // World::outsides, backgrounds, and posters should
  // really be in unstretched coordinates.
  Size imgSize;
  imgSize.set(xvars.stretch_x(outsides[outsideIndex].size.width >> 1),
              xvars.stretch_y(outsides[outsideIndex].size.height >> 1));
  
  // Top side, stick out on right and left
  if (pos.y < 0) {
    for (int across = Utils::div(pos.x,imgSize.width);
         across * imgSize.width < pos.x + size.width;
         across++) {
      for (int down = Utils::div(pos.y,imgSize.height);
           down < 0;
           down++) {
        // area in window coords
        Area imgArea(Pos(across * imgSize.width 
                         - pos.x,down * imgSize.height - pos.y),
                     imgSize);
        // Clip to window (0,0,size.width,size.height)
        Size srcOffset = imgArea.clip(size);
        
        draw_outside_offset(buffer,xvars,
                            dpyNum,srcOffset,imgArea);
      }
    }
  }
  
  // Left side, stick out on bottom
  if (pos.x < 0) {
    for (int down = Utils::maximum(0,Utils::div(pos.y,imgSize.height));
         down * imgSize.height < pos.y + size.height;
         down++) {
      for (int across = Utils::div(pos.x,imgSize.width);
           (across * imgSize.width < pos.x + size.width) && (across < 0);
           across++) {
        // area in window coords
        Area imgArea(Pos(across * imgSize.width - pos.x,
                         down * imgSize.height - pos.y),imgSize);
        // Clip to window (0,0,size.width,size.height)
        Size srcOffset = imgArea.clip(size);
        
        draw_outside_offset(buffer,xvars,
                            dpyNum,srcOffset,imgArea);
      }
    }
  }
  
  
  // Bottom side, stick out on right
  if (pos.y + size.height > worldSize.height) {
    for (int across = Utils::maximum(0,Utils::div(pos.x,imgSize.width));
         across * imgSize.width < pos.x + size.width;
         across++) {
      for (int down = Utils::maximum(Utils::div(pos.y,imgSize.height),
                                     worldSize.height / imgSize.height);
           down * imgSize.height < pos.y + size.height;
           down++) {
        // area in window coords
        Area imgArea(Pos(across * imgSize.width 
                         - pos.x,down * imgSize.height - pos.y),
                     imgSize);
        // Clip to window (0,0,size.width,size.height)
        Size srcOffset = imgArea.clip(size);
        // Clip to world.
        srcOffset += imgArea.clip_top(worldSize.height - pos.y);
        
        draw_outside_offset(buffer,xvars,
                            dpyNum,srcOffset,imgArea);
      }
    }
  }
  
  // Right side, 
  if (pos.x + size.width > worldSize.width) {
    for (int down = Utils::maximum(0,Utils::div(pos.y,imgSize.height));
         (down * imgSize.height < pos.y + size.height) 
           && (down * imgSize.height < worldSize.height);
         down++) {
      for (int across = Utils::maximum(Utils::div(pos.x,imgSize.width),
                                       worldSize.width / imgSize.width);
           across * imgSize.width < pos.x + size.width;
           across++) {
        // area in window coords
        Area imgArea(Pos(across * imgSize.width 
                         - pos.x,down * imgSize.height - pos.y),
                     imgSize);
        // Clip to window (0,0,size.width,size.height)
        Size srcOffset = imgArea.clip(size);
        // Clip to world.
        srcOffset += imgArea.clip_left(worldSize.width - pos.x);
        
        draw_outside_offset(buffer,xvars,
                            dpyNum,srcOffset,imgArea);
      }
    }
  }
}



Boolean World::open(const Loc &loc,Boolean laddersClosed,Boolean postersClosed,
		                Boolean doorsClosed,Boolean outsideClosed) {
  if (!inside(loc)) {
    return !outsideClosed;
  }

  // a poster isn't closed at loc
  Boolean posterOpen =
  (!postersClosed ||
   !(unionSquares[loc.r][loc.c] &&
     unionSquares[loc.r][loc.c]->type == UN_POSTER));

  // a door isn't "closed" at loc
  Boolean doorOpen =
	  (!doorsClosed ||
	   !(unionSquares[loc.r][loc.c] &&
	     unionSquares[loc.r][loc.c]->type == UN_DOOR));

  // a moverSquare isn't "closed" at loc  (uses laddersClosed for now)
  Boolean moverSquareOpen =
	  (!laddersClosed ||
	   !(unionSquares[loc.r][loc.c] &&
	     unionSquares[loc.r][loc.c]->type == UN_MOVER));

  Boolean ret =
	  // map contains an open square at loc
	  (map[loc.r][loc.c] == Wempty ||
	   map[loc.r][loc.c] == WtextSquare ||
	   map[loc.r][loc.c] == Wsquanch ||
	   (map[loc.r][loc.c] == Wladder && !laddersClosed)) &&
    posterOpen &&
    doorOpen &&
    moverSquareOpen;

  // quick sanity check.
  if (unionSquares[loc.r][loc.c]) {
    assert(map[loc.r][loc.c] == Wempty || map[loc.r][loc.c] == Wwall);
  }

  return ret;
}



// The only World::open() that takes movers into account.
Boolean World::open(const Area &area,Boolean laddersClosed,
                    Boolean postersClosed,Boolean doorsClosed,
                    Boolean outsideClosed) {
  /* Avoid a certain type of crash when area is far out of the world.
     I.e. when area.pos is near overflow. */
  if (!inside(area.middle_wsquare())) {
    // Fix this to deal with outsideClosed.
    return False;
  }
  

  // Try all wsquares on top of area.
  Loc list[AR_WSQUARES_MAX];
  int nsquares;
  area.wsquares(list,nsquares);
  for (int n = 0; n < nsquares; n++) {
    if (!open(list[n],laddersClosed,postersClosed,
              doorsClosed,outsideClosed)) {
      return False;
    }

    // Have to add this inside() check because if outsideClosed is False, 
    // we can still get here.
    if (inside(list[n])) {
      // Look at movers corresponding to moverSquares that are covered by area.
      UnionSquare *uSquare = unionSquares[list[n].r][list[n].c];
      if (uSquare && uSquare->type == UN_MOVER) {
        const Area &moverArea = uSquare->mSquare.mover->get_area();
        if (moverArea.overlap(area)) {
          return False;
        }
      }
    }
  } // for
  
  return True;
}



Boolean World::open(const Box &box,Boolean laddersClosed,Boolean postersClosed,
		                Boolean doorsClosed) {
  Loc loc;
  for (loc.c = box.loc.c; loc.c < box.loc.c + box.dim.colMax; loc.c++) {
    for (loc.r = box.loc.r; loc.r < box.loc.r + box.dim.rowMax; loc.r++) {
      if (!open(loc,laddersClosed,postersClosed,doorsClosed)) {
        return False;
      }
    }
  }
  return True;
}



Wsig World::open_offset(Size &offset,const Area &areaNew,const Vel &vel) {
  if (open(areaNew)) {
    return W_NO_SIG;
  }

  // wrap-around disabled.
#if 0
  // Check for player wrapping around the world.
  if (check_wrap_around(offset,areaNew)) {
    return W_CLOSE;
  }
#endif

  Vel opp = -1.0f * vel;

  Dir best[4];
  Dir others[4];
  int bestNum,othersNum;
  opp.get_dirs_4(best,others,bestNum,othersNum);

  if (open_try_dirs(offset,areaNew,best,bestNum)) {
    return W_CLOSE;
  }

  if (open_try_dirs(offset,areaNew,others,othersNum)) {
    return W_CLOSE;
  }

	// Special "Hero stuck in corner" case.
	if (open_try_corners(offset,areaNew)) {
		return W_CLOSE;
  }

	// Big jump.
  if (open_try_diagonals(offset,areaNew)) {
    return W_CLOSE_BAD;
  }

  return W_FAILURE;
}



void World::compute_touching_hanging(Touching &touching, Hanging &hanging,
                                     MoverP &touchingMover,const Area &area) {
  Pos apos;
  Size asize;
  area.get_rect(apos,asize);

  touching = CO_air;
  hanging.corner = CO_air;

  TouchingList touchingList;
  area.touching_wsquares(touchingList);

  TouchingList edgeList;
  area.edge_wsquares(edgeList);

  // Set to NULL if not touching a Mover.
  touchingMover = NULL;

  // Last one takes priority.  Down is highest priority, then
  // Up, then right, left.
  th_helper(touching,hanging,touchingMover,touchingList.l,edgeList.l,
            True,area,apos.y,asize.height,
            WSQUARE_HEIGHT,CO_l,CO_l_UP,CO_l_DN);
  th_helper(touching,hanging,touchingMover,touchingList.r,edgeList.r,
            True,area,apos.y,asize.height,
            WSQUARE_HEIGHT,CO_r,CO_r_UP,CO_r_DN);
  th_helper(touching,hanging,touchingMover,touchingList.up,edgeList.up,
            False,area,apos.x,asize.width,
            WSQUARE_WIDTH,CO_up,CO_up_L,CO_up_R);
  th_helper(touching,hanging,touchingMover,touchingList.dn,edgeList.dn,
            False,area,apos.x,asize.width,
            WSQUARE_WIDTH,CO_dn,CO_dn_L,CO_dn_R);
}



void World::demo_reset() {
  // Don't reuse the nonce because may have old packets
  // uniqueGen = 0;

  justReset = True;
  choose_theme();
}



void World::clear_everything() {
  // Delete old unionSquares.
  Loc loc;
  for (loc.r = 0; loc.r < dim.rowMax; loc.r++) {
    for (loc.c = 0; loc.c < dim.colMax; loc.c++) {
      if (unionSquares[loc.r][loc.c]) {
        delete unionSquares[loc.r][loc.c];
        unionSquares[loc.r][loc.c] = NULL;
      }
    }
  }

  // Change rooms,dim,size of world.
  rooms = roomsNext;
  specialMap = specialMapNext;
  rooms_dim_size_update();

  // Clear map.
  for (loc.r = 0; loc.r < dim.rowMax; loc.r++) {
    for (loc.c = 0; loc.c < dim.colMax; loc.c++) {
      map[loc.r][loc.c] = Wempty;
    }
  }

  delete_movers();
}



void World::close_horiz_mover(const Loc &loc,Boolean &inHorizMover) {
  if (inHorizMover) {
    // initialize the mover.
    MoverId moverId;
    moverId.index = moversNum;
    moverId.unique = uniqueGen++;

    Size vel;
    vel.set(Utils::coin_flip() ? W_MOVER_SPEED : -W_MOVER_SPEED,0);

    // Use specified initial position
    if (movers[moversNum]->is_area_set()) {    
      movers[moversNum]->init_not_area(this,locator,vel,moverId);
    }
    // Start at right end of the track.
    else {
      // Doesn't check that we have at least two wsquares to the right.
      Pos pos((loc.c - 2) * WSQUARE_WIDTH,loc.r * WSQUARE_HEIGHT);
      Area area(AR_RECT,pos,moverSize);
      movers[moversNum]->init(this,locator,area,vel,moverId);
    }

    // Dummy mover object, so mover gets redrawn.
    PhysMoverP p = new PhysMover(this,locator,movers[moversNum]);
    assert(p);
    locator->add(p);
    movers[moversNum]->set_phys_mover_id(p->get_id());

    moversNum++;
    inHorizMover = False;
  } 
}



void World::close_vert_movers(const Dim &dim) {
  Loc l;
  // Traverse all squares.
  for (l.c = 0; l.c < (dim.colMax - 1); l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      Loc loc = l;
      Boolean inVertMover = False;
      // Go down, filling out mover.
      // (MoverP)-1 hack is described in read_from_file().
      while (loc.r < dim.rowMax &&
             unionSquares[loc.r][loc.c] && 
             unionSquares[loc.r][loc.c]->type == UN_MOVER &&
             (unionSquares[loc.r][loc.c]->mSquare.mover == NULL ||
              unionSquares[loc.r][loc.c]->mSquare.mover == (MoverP)-1) &&
             unionSquares[loc.r][loc.c]->mSquare.orientation == OR_VERT &&
             unionSquares[loc.r][loc.c+1] && 
             unionSquares[loc.r][loc.c+1]->type == UN_MOVER &&
             (unionSquares[loc.r][loc.c+1]->mSquare.mover == NULL ||
              unionSquares[loc.r][loc.c+1]->mSquare.mover == (MoverP)-1) &&
             unionSquares[loc.r][loc.c+1]->mSquare.orientation == OR_VERT) {
        if (!inVertMover) {
          // create the mover.
          inVertMover = True;
          assert(moversNum < MOVERS_MAX);
          movers[moversNum] = new Mover;
          assert(movers[moversNum]);
        }

        // If starting position of Mover is given by user.
        if (unionSquares[loc.r][loc.c]->mSquare.mover == (MoverP)-1 && 
            !movers[moversNum]->is_area_set()) {
          Pos pos(loc.c * WSQUARE_WIDTH,loc.r * WSQUARE_HEIGHT);
          Area area(AR_RECT,pos,moverSize);
          movers[moversNum]->init_area(area);
        }

        unionSquares[loc.r][loc.c]->mSquare.mover = movers[moversNum];        
        unionSquares[loc.r][loc.c+1]->mSquare.mover = movers[moversNum];        
        loc.r++;
      }  
      // Close out mover.
      if (inVertMover) {
        // initialize the mover.
        MoverId moverId;
        moverId.index = moversNum;
        moverId.unique = uniqueGen++;

        Size vel;
        vel.set(0,Utils::coin_flip() ? W_MOVER_SPEED : -W_MOVER_SPEED);

        // Use specified initial position
        if (movers[moversNum]->is_area_set()) {    
          movers[moversNum]->init_not_area(this,locator,vel,moverId);
        }
        // Start at top.
        else {
          Pos pos(l.c * WSQUARE_WIDTH,l.r * WSQUARE_HEIGHT);
          Area area(AR_RECT,pos,moverSize);
          movers[moversNum]->init(this,locator,area,vel,moverId);
        }

        // Dummy mover object, so mover gets redrawn.
        PhysMoverP p = new PhysMover(this,locator,movers[moversNum]);
        assert(p);
        locator->add(p);
        movers[moversNum]->set_phys_mover_id(p->get_id());

        moversNum++;
      }
    } // for
  } // for
}



Boolean World::read_from_file(char *filename) {
  FILE *fp = fopen(filename,"r");
  if (!fp) {
    cerr << "Could not open " << filename << endl;
    return False;
  }

  if (EOF == fscanf(fp,"XEvil World 1.0\n")) {
    cerr << "Invalid header in world file." << endl;
    fclose(fp);
    return False;
  }

  Dim dim;  
  if (2 != fscanf(fp,"%dx%d\n",&dim.colMax,&dim.rowMax)) {
    cerr << "Could not read size of world." << endl;
    fclose(fp);
    return False;
  }

  if (dim.colMax > W_COL_MAX_MAX) {
    cerr << "Across size is larger than maximum. Truncating to " << W_COL_MAX_MAX << endl;
    dim.colMax = W_COL_MAX_MAX;
  }

  if (dim.rowMax > W_ROW_MAX_MAX) {
    cerr << "Down size is larger than maximum. Truncating to " << W_ROW_MAX_MAX << endl;
    dim.rowMax = W_ROW_MAX_MAX;
  }

  Dim halfRoom(W_ROOM_ROW_MAX / 2,W_ROOM_COL_MAX / 2);
  if (dim.rowMax % halfRoom.rowMax != 0 || 
      dim.colMax % halfRoom.colMax != 0) {
    cerr << "Dimensions of world must be multiples of " << halfRoom.colMax << 
      " across and " << halfRoom.rowMax << " down." << endl;
    fclose(fp);
    return False;
  }

  if (dim.rowMax <= 0 || dim.colMax <= 0) {
    cerr << "Invalid dimension, negative or zero." << endl;
    fclose(fp);
    return False;
  }

  // No longer need to pad with extra Wwall.
  this->dim = dim;
  rooms_dim_size_update(&dim);

  char buffer[W_COL_MAX_MAX];
  Loc loc;
  for (loc.r = 0; loc.r < this->dim.rowMax; loc.r++) {
    // dim is the size of the data on file, this.dim is 
    // the size alligned to room boundries, fill rest with Wwall
    if (loc.r < dim.rowMax) {
      Boolean badRow = False;
      if (!fgets(buffer,W_COL_MAX_MAX,fp)) {
        badRow = True;
      }
      if (buffer[0] == ';') {
        // Hit a comment, ignore the line.
        loc.r--;
        continue;
      }
      if ((int)strlen(buffer) < dim.colMax) {
        badRow = True;
      }
      if (badRow) {
        cerr << "Could not read World data." << endl;
        fclose(fp);
        return False;   
      }      

      Boolean inHorizMover = False;
      for (loc.c = 0; loc.c < this->dim.colMax; loc.c++) {
        if (loc.c < dim.colMax) {
          char val;
          switch (buffer[loc.c]) {
            case '.':
              val = Wempty;
              close_horiz_mover(loc,inHorizMover);
              break;
            case '#':
              val = Wwall;
              close_horiz_mover(loc,inHorizMover);
              break;
            case 'H':
              val = Wladder;
              close_horiz_mover(loc,inHorizMover);
              break;
            case '*':
              val = WupDown;
              close_horiz_mover(loc,inHorizMover);
              break;
            case '^':
            case '|':
            case '&': // sticking in floor.
              val = Wempty;
              close_horiz_mover(loc,inHorizMover);
              if (loc.c + 1 < dim.colMax &&
                  (buffer[loc.c+1] == '^' ||
                   buffer[loc.c+1] == '|' ||
                   buffer[loc.c+1] == '&')) {
                Boolean markStartPos = (buffer[loc.c] == '^' && buffer[loc.c + 1] == '^');
                Boolean stickInFloor = (buffer[loc.c] == '&' && buffer[loc.c + 1] == '&');

                // Do two squares.
                for (int x = 0; x < 2; x++) {
                  unionSquares[loc.r][loc.c] = new UnionSquare;
                  assert(unionSquares[loc.r][loc.c]);
                  unionSquares[loc.r][loc.c]->type = UN_MOVER;

                  // Big hack here.  We don't create mover here since we don't know which
                  // mover goes with which wsquare, but we still need to remember where
                  // the initial position is.  Set mover to be (MoverP)(-1) for wsquares
                  // that the user specified as initial positions.
                  if (markStartPos) {
                    unionSquares[loc.r][loc.c]->mSquare.mover = (MoverP)-1;
                  }
                  else {
                    unionSquares[loc.r][loc.c]->mSquare.mover = NULL;
                  }
                  unionSquares[loc.r][loc.c]->mSquare.orientation = OR_VERT;
                  if (x == 0) {
                    map[loc.r][loc.c] = stickInFloor ? Wwall : Wempty;
                    loc.c++;
                  }
                  else {
                    assert(x == 1);
                    val = stickInFloor ? Wwall : Wempty;
                  } 
                }                
              }
              break;
            case '~':
            case '-':
            case '%':
              if (buffer[loc.c] == '%') {
                val = Wwall;
              }
              else {
                val = Wempty;
              }
              // Make sure at least two next to each other.
              if ((loc.c + 1 < dim.colMax && (buffer[loc.c+1] == '~' || buffer[loc.c+1] == '-' || buffer[loc.c+1] == '%')) ||
                  (loc.c - 1 >= 0 && (buffer[loc.c-1] == '~' || buffer[loc.c-1] == '-' || buffer[loc.c-1] == '%'))) {                    
                if (!inHorizMover) {
                  assert(moversNum < MOVERS_MAX);
                  movers[moversNum] = new Mover;
                  assert(movers[moversNum]);
                }

                inHorizMover = True;
                unionSquares[loc.r][loc.c] = new UnionSquare;
                assert(unionSquares[loc.r][loc.c]);
                unionSquares[loc.r][loc.c]->type = UN_MOVER;
                unionSquares[loc.r][loc.c]->mSquare.mover = movers[moversNum];
                unionSquares[loc.r][loc.c]->mSquare.orientation = OR_HORIZ;

                // If starting position of Mover is given by user.
                if (buffer[loc.c] == '~' && 
                    // Need two '~' in a row.
                    loc.c + 1 < dim.colMax && buffer[loc.c + 1] == '~' &&
                    // pos not already set.
                    !movers[moversNum]->is_area_set()) {
                  Pos pos(loc.c * WSQUARE_WIDTH,loc.r * WSQUARE_HEIGHT);
                  Area area(AR_RECT,pos,moverSize);
                  movers[moversNum]->init_area(area);
                }
              }
              break;
            default:
              cerr << "Error reading map, unknown character " << buffer[loc.c] << endl;
              val = Wempty;
              close_horiz_mover(loc,inHorizMover);
          }
          map[loc.r][loc.c] = val;
        }
        else {
          // Close out mover.
          close_horiz_mover(loc,inHorizMover);
          // Fill rest of row with Wwall
          map[loc.r][loc.c] = Wwall;              
        }
      } // for loc.c

    }
    else {
      // Fill remaining rows with Wwall
      for (loc.c = 0; loc.c < this->dim.colMax; loc.c++) {
        map[loc.r][loc.c] = Wwall;
      }                    
    }
  }

  close_vert_movers(dim);

  fclose(fp);
  return True;
}



void World::read_from_stream(InStreamP in) {
  Dim dim(in);

  themeIndex = in->read_int();
  themeIndex = Utils::minimum(W_THEME_NUM - 1,themeIndex);
  backgroundIndex = in->read_int();
#if WIN32
  backgroundIndex = Utils::minimum(W_ALL_BACKGROUNDS_NUM - 1,backgroundIndex);
#endif
  outsideIndex = in->read_int();
#if WIN32
  outsideIndex = Utils::minimum(W_ALL_OUTSIDES_NUM - 1,outsideIndex);
#endif
  // Take minimum with the max number, just in case the server gives an invalid
  // number.  Only for windows, UNIX client doesn't give a shit about these numbers.
  
  if (!in->alive()) {
    // failure.
    return;
  }
  rooms_dim_size_update(&dim);
  Loc l;
  for (l.c = 0; l.c < dim.colMax; l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      map[l.r][l.c] = in->read_char();
    }
  }

  assert(moversNum == 0);
  moversNum = in->read_int();
  int n;
  for (n = 0; n < moversNum; n++) {
    movers[n] = new Mover(in,this,locator);
    assert(movers[n]);
  }
  
  int uCount = in->read_int();
  for (n = 0; n < uCount; n++) {
    Loc loc;
    loc.read(in);
    if (unionSquares[loc.r][loc.c]) {
      delete unionSquares[loc.r][loc.c];
    }
    unionSquares[loc.r][loc.c] = UnionSquare::read(in,this);
    assert(unionSquares[loc.r][loc.c]);

    // Hack to deal with different poster sizes on UNIX vs. Windows.
    if (unionSquares[loc.r][loc.c]->type == UN_POSTER) {
      // Weed out posters that aren't there
      if (unionSquares[loc.r][loc.c]->pSquare.poster >= 
          W_ALL_POSTERS_NUM) {
        delete unionSquares[loc.r][loc.c];
        unionSquares[loc.r][loc.c] = NULL;
      }
      else {
        // Special check to weed out posters that are too big.      
        Loc &pLoc = unionSquares[loc.r][loc.c]->pSquare.loc;
        int p = unionSquares[loc.r][loc.c]->pSquare.poster;
        if (pLoc.c >= posterDims[p].colMax ||
            pLoc.r >= posterDims[p].rowMax) {
          delete unionSquares[loc.r][loc.c];
          unionSquares[loc.r][loc.c] = NULL;
        }
      }
    }
  }
}



int World::get_write_length() {
  // Count number of union squares.
  int uCount = 0;
  Loc l;
  for (l.c = 0; l.c < dim.colMax; l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      if (unionSquares[l.r][l.c]) {
        uCount++;
      }
    }
  }
  
  return 
    Dim::get_write_length() +                   // dim
    sizeof(int) +                               // themeIndex
    sizeof(int) +                               // backgroundIndex
    sizeof(int) +                               // outsideIndex
    dim.rowMax * dim.colMax * sizeof(char) +    // wsquares
    sizeof(int) +                               // moversNum
    moversNum * Mover::get_write_length() +     // movers[]
    sizeof(int) +                               // uCount
    uCount * (Loc::get_write_length() + 
              UnionSquare::get_write_length()); // unionSquares[]
}



void World::write(OutStreamP out) {
  dim.write(out);
  out->write_int(themeIndex);
  out->write_int(backgroundIndex);
  out->write_int(outsideIndex);
  if (!out->alive()) {
    return;
  }
  Loc l;
  for (l.c = 0; l.c < dim.colMax; l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      out->write_char(map[l.r][l.c]);
    }
  }

  // Write movers before union squares so that MoverSquares can refer to
  // the movers.
  out->write_int(moversNum);
  int n;
  for (n = 0; n < moversNum; n++) {
    movers[n]->write(out);
  }

  // Count number of union squares.
  int uCount = 0;
  for (l.c = 0; l.c < dim.colMax; l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      if (unionSquares[l.r][l.c]) {
        uCount++;
      }
    }
  }
  out->write_int(uCount);
  for (l.c = 0; l.c < dim.colMax; l.c++) {
    for (l.r = 0; l.r < dim.rowMax; l.r++) {
      if (unionSquares[l.r][l.c]) {
        l.write(out);
        unionSquares[l.r][l.c]->write(out);
      }
    }
  }
}



// For one room.
int World::get_write_length(const RoomIndex &) {
  return W_ROOM_ROW_MAX * W_ROOM_COL_MAX * sizeof(char);
}



// Write one room.
void World::write(OutStreamP outStream,const RoomIndex &idx) {
  if (!outStream->alive()) {
    return;
  }
  Loc l;
  for (l.r = idx.down * W_ROOM_ROW_MAX; 
       l.r < (idx.down + 1) * W_ROOM_ROW_MAX;
       l.r++) {
    for (l.c = idx.across * W_ROOM_COL_MAX; 
         l.c < (idx.across + 1) * W_ROOM_COL_MAX;
         l.c++) {
      if (inside(l)) {
        outStream->write_char(map[l.r][l.c]);
      }
      else {
        // There's not really any point in writing this.
        outStream->write_char(Woutside);
      }
    }
  }
}



// Read one room
void World::read(InStreamP in,const RoomIndex &idx) {
  if (!in->alive()) {
    return;
  }
  Loc l;
  for (l.r = idx.down * W_ROOM_ROW_MAX; 
       l.r < (idx.down + 1) * W_ROOM_ROW_MAX;
       l.r++) {
    for (l.c = idx.across * W_ROOM_COL_MAX; 
         l.c < (idx.across + 1) * W_ROOM_COL_MAX;
         l.c++) {
      if (inside(l)) {
        map[l.r][l.c] = in->read_char();
      }
      else {
        // just throw it away.
        in->read_char();
      }
    }
  }
}



void World::reset(const Dim *clearDim,InStreamP inStream) {
  // Don't reuse the nonce because may have old packets
  // uniqueGen = 0;

  justReset = True;

  assert(locator);

  clear_everything();

  // Create empty world of given dimensions.
  if (clearDim) {
    rooms_dim_size_update(clearDim);
    return;
  }

  // Read from stream if supplied
  if (inStream) {
    read_from_stream(inStream);
    return;
  }

  // Read from file if set.
  if (specialMap == MAP_NONE && worldFile) {
    if (!read_from_file(worldFile)) {
      // Failed to read, go back to default.
      clear_everything();
      // Don't bother to try again.
      Utils::freeif(worldFile);
    }
  }

  // Create regular world if worldFile is NULL or if world can't be read
  // in.
  if (!worldFile) {
    // Before blueprints
    if (specialMap == FLAG) {
      // hack so FireDemon can get flags.
      objectDimMax.colMax = OBJECT_BIG_COL_MAX;
      objectDimMax.rowMax = OBJECT_BIG_ROW_MAX;
    }
    else {
      // back to default.
      objectDimMax.colMax = OBJECT_COL_MAX;
      objectDimMax.rowMax = OBJECT_ROW_MAX;
    }

    // Generate new map.
    if (blueprints) {
      delete blueprints;
    }
    blueprints = new Blueprints(this,map,objectDimMax,specialMap);
    assert(blueprints);
    if (mapPrint) {
      blueprints->print();
    }
    blueprints->room_boundries();

    if (useMovers) {
      add_movers();
    }

    blueprints->extra_walls();
    blueprints->up_downs();

    if (specialMap != ZIG_ZAG) {// Can't have people teleporting around, now.
      add_doors();
    }

    add_posters();

    // add_vert_movers();
  }

  choose_theme();
}



void World::choose_theme() {
  // Weigh choice of themes by number of backgrounds in each theme.
  int themeWeights[W_THEME_NUM];
  for (int n = 0; n < W_THEME_NUM; n++) {
    themeWeights[n] = themes[n].backgroundsNum;
  }

  // X11 has no themes, but still maintains these indices if it is acting as 
  // a server.
  themeIndex = Utils::weighted_choose(W_THEME_NUM,themeWeights);

  backgroundIndex = Utils::choose(themes[themeIndex].backgroundsNum);
  // Want index into xdata.backgroundSurfaces
  backgroundIndex = themes[themeIndex].backgroundIndices[backgroundIndex];

  outsideIndex = Utils::choose(themes[themeIndex].outsidesNum);
  outsideIndex = themes[themeIndex].outsideIndices[outsideIndex];
}



MoverP World::lookup(const MoverId &id) {
  if (id.index != MoverId::INVALID &&
      id.index >= 0 &&
      id.index < moversNum &&
      movers[id.index]->get_mover_id() == id) {
    return movers[id.index];
  }
  return NULL;
}



void World::clock() {
  justReset = False;

  for (int n = 0; n < moversNum; n++) {
    movers[n]->clock();
  } // for n
}



Pos World::empty_rect(const Size &s) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    // Bail out, semi-gracefully.
    if (count >= EMPTY_TRIES_MAX) {
      cerr << "World::empty_rect failed after " 
           << EMPTY_TRIES_MAX << " iterations" << endl;
      Pos ret; // (0,0)
      return ret;
    }
    count++;

    Pos ret;
    ret.x = Utils::choose(size.width);
    ret.y = Utils::choose(size.height);

    Area area(AR_RECT,ret,s);
    if (open(area,True,False,True)) {
      return ret;
    }
  }
}



Pos World::empty_touching_rect(const Size &s) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    // Bail out, semi-gracefully.
    if (count >= EMPTY_TRIES_MAX) {
      cerr << "World::empty_touching_rect failed after " 
           << EMPTY_TRIES_MAX << " iterations" << endl;
      Pos ret; // (0,0)
      return ret;
    }
    count++;

    Pos ret;
    ret.x = Utils::choose(size.width);
    ret.y = WSQUARE_HEIGHT * Utils::choose(dim.rowMax) - s.height;

    Area area(AR_RECT,ret,s);
    Touching touching;
    Hanging hanging;
    MoverP dummy;
    compute_touching_hanging(touching,hanging,dummy,area);
    if (open(area,True,False,True) && (touching == CO_dn)) {
	    return ret;
    }
  }
}



Pos World::empty_accessible_rect(const Size &s) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    // Bail out, semi-gracefully.
    if (count >= EMPTY_TRIES_MAX) {
      cerr << "World::empty_accessible_rect failed after " 
           << EMPTY_TRIES_MAX << " iterations" << endl;
      Pos ret; // (0,0)
      return ret;
    }
    count++;

    // Choose the room.
    RoomIndex r(Utils::choose(rooms.downMax),
    Utils::choose(rooms.acrossMax));

    Pos ret;
    if (empty_accessible_rect_one(ret,s,r)) {
      return ret;
    }
  }
}



Pos World::empty_accessible_rect(const Size &s,const RoomIndex &r) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    // Bail out, semi-gracefully.
    if (count >= EMPTY_TRIES_MAX) {
      cerr << "World::empty_accessible_rect<2> failed after " 
           << EMPTY_TRIES_MAX << " iterations" << endl;
      Pos ret; // (0,0)
      return ret;
    }
    count++;

    Pos ret;
    // Use supplied room.
    if (empty_accessible_rect_one(ret,s,r)) {
      return ret;
    }
  }
}



Boolean World::empty_accessible_rect_one(Pos &ret,const Size &s,
					                               const RoomIndex &r) {
  Blueprints *bl = blueprints;
  assert(bl);

  // Start at right edge of room.
  ret.x = r.across * W_ROOM_COL_MAX * WSQUARE_WIDTH;

  // Choose x coord so that on right or left side of ladder, whereever
  // there is a floor.
  if (bl->blocked_right(r)) {
    if (bl->blocked_left(r)) {
      // Bail out, no suitable rect.
      return False;
    }
    else {
      // Left half of room.
      ret.x += Utils::choose(WSQUARE_WIDTH *
			           bl->get_middle_col(r.across));
    }
  }
  else {
    if (bl->blocked_left(r)) {
      // Right half of room.
      ret.x +=
	      WSQUARE_WIDTH *
	      (bl->get_middle_col(r.across) + objectDimMax.colMax) +
	      Utils::choose(WSQUARE_WIDTH *
		      (W_ROOM_COL_MAX -
		       bl->get_middle_col(r.across) -
		       objectDimMax.colMax));
    }
    else {
      if (Utils::coin_flip()) {
	      // Left half of room.
	      ret.x += Utils::choose(WSQUARE_WIDTH *
			       bl->get_middle_col(r.across));
      }
      else {
	      // Right half of room.
	      ret.x +=
	        WSQUARE_WIDTH *
	        (bl->get_middle_col(r.across) + objectDimMax.colMax) +
	        Utils::choose(WSQUARE_WIDTH *
			      (W_ROOM_COL_MAX -
			       bl->get_middle_col(r.across) -
			       objectDimMax.colMax));
      }
    }
  }

  // y coord so that area is just touching middle floor.
  ret.y = (r.down * W_ROOM_ROW_MAX + bl->get_middle_row(r.down))
    * WSQUARE_HEIGHT - s.height;

  Area area(AR_RECT,ret,s);
  if (open(area,True,False,True)) {
    return True;
  }
  else {
    return False;
  }
}



Boolean World::empty_box(Loc &loc,const Dim &d,Boolean laddersClosed,
			                   Boolean postersClosed,Boolean doorsClosed) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    if (count >= EMPTY_TRIES_MAX) {
      return False;
    }
    count++;

    loc.c = Utils::choose(dim.colMax);
    loc.r = Utils::choose(dim.rowMax);

    Box box(loc,d);
    if (open(box,laddersClosed,postersClosed,doorsClosed)) {
      return True;
    }
  }
}



Boolean World::empty_touching_box(Loc &loc,const Dim &d,
				                          Boolean laddersClosed,
				                          Boolean postersClosed,
				                          Boolean doorsClosed) {
  int count = 0; // Guard against infinite loop.

  while (True) {
    if (count >= EMPTY_TRIES_MAX) {
      return False;
    }
    count++;

    loc.c = Utils::choose(dim.colMax);
    loc.r = Utils::choose(dim.rowMax);

    Area area(AR_RECT,loc,d);
    Touching touching;
    Hanging hanging;
    MoverP dummy;
    compute_touching_hanging(touching,hanging,dummy,area);
    if (open(area,laddersClosed,postersClosed,doorsClosed) &&
	      touching == CO_dn) {
	    return True;
    }
  }
}



void World::rooms_dim_size_update(const Dim *newDim) {
  if (newDim) {
    // Use newDim to set dim, rooms, and size.
    dim = *newDim;
    // Round rooms up.
    rooms.downMax = (int)ceil((float)dim.rowMax / (float)W_ROOM_ROW_MAX);
    rooms.acrossMax = (int)ceil((float)dim.colMax / (float)W_ROOM_COL_MAX);
  }  
  else {
    // Use this.rooms to set dim and size.
    dim.colMax = rooms.acrossMax * W_ROOM_COL_MAX;
    dim.rowMax = rooms.downMax * W_ROOM_ROW_MAX;
  }
  size.width = dim.colMax * WSQUARE_WIDTH;
  size.height = dim.rowMax * WSQUARE_HEIGHT;
}



void World::th_helper(
    // Return values.
    Touching &touching,
    Hanging &hanging,
    MoverP &touchingMover,

    const TouchingListItem &tItem,
    const TouchingListItem &eItem,  // For getting potential Movers.
    Boolean r_c, // row/column
    const Area &area,
    int coord,int length,
    int wsquare_length,
    Touching iftouching,
    Corner ifsmall, Corner iflarge) {

  // Compute touching.
  Boolean touchingOk = False;

  // Get list of all movers touching area on the given side.
  MoverP touchingMovers[MOVERS_MAX];
  int touchingMoversNum = 0;
  int n;
  for (n = 0; n < eItem.num; n++) {
    const Loc &loc = eItem.list[n];
    if (inside(loc) && unionSquares[loc.r][loc.c] &&
        unionSquares[loc.r][loc.c]->type == UN_MOVER &&
        area.touches(unionSquares[loc.r][loc.c]->mSquare.mover->get_area(),
                     iftouching)) {

      Boolean alreadyFound = False;
      for (int m = 0; m < touchingMoversNum; m++) {
        if (touchingMovers[m] == unionSquares[loc.r][loc.c]->mSquare.mover) {
          alreadyFound = True;
          continue;
        }
      }
      if (!alreadyFound) {
        touchingMovers[touchingMoversNum] = 
          unionSquares[loc.r][loc.c]->mSquare.mover;
        touching = iftouching;
        // May be set again in hanging.
        touchingMover = touchingMovers[touchingMoversNum]; 
        touchingOk = True;
        touchingMoversNum++;
      }
    }
  }


  // Is a mover touching.
  if (!touchingOk) {
    // Check all Locs.
    for (n = 0; n < tItem.num; n++) {
      if (!open(tItem.list[n])) {
        touching = iftouching;
        touchingMover = NULL;
        touchingOk = True;
        continue;
      }
    }
  }
  
  
  // Only compute hanging if touching.
  if (touchingOk) {
    int hangCutoff = (int)floor(HANGING_PERCENT * length);
    
    int n;
    for (n = 0; n < tItem.num; n++) {
      if (!open(tItem.list[n])) {
        break;
      }
    }

    // tItem.list[n] is first blocked wsquare.
    if ((n < tItem.num) &&
	    ((r_c ? tItem.list[n].r:tItem.list[n].c) * wsquare_length - coord
	     >= hangCutoff)) {
      hanging.corner = ifsmall;
      hanging.loc = tItem.list[n];
      hanging.type = Hanging::LOC;
    }
    
    for (n = tItem.num - 1; n >= 0; n--) {
      if (! open(tItem.list[n])) {
        break;
      }
    }
    
    // tItem.list[n] is last blocked wsquare.
    if ((n >= 0) &&
	    (coord + length -
	     (r_c ? tItem.list[n].r + 1: tItem.list[n].c + 1) * wsquare_length
	     >= hangCutoff)) {
      hanging.corner = iflarge;
      hanging.loc = tItem.list[n];
      hanging.type = Hanging::LOC;
    }
    
    
    // Really should sort movers by their areas and then treat them similar
    // to the Locs, but fuck it.
    for (n = 0; n < touchingMoversNum; n++) {
      Pos moverPos;
      Size moverSize;
      const Area &moverArea = touchingMovers[n]->get_area();
      moverArea.get_rect(moverPos,moverSize);
      if ((r_c ? moverPos.y : moverPos.x) - coord
          >= hangCutoff) {
        hanging.corner = ifsmall;
        hanging.moverId = touchingMovers[n]->get_mover_id();
        hanging.type = Hanging::MOVER;
        // touchingMover = touchingMovers[n];
      }
      
      if (coord + length -
          (r_c ? moverPos.y + moverSize.height : moverPos.x + moverSize.width)
          >= hangCutoff) {
        hanging.corner = iflarge;
        hanging.moverId = touchingMovers[n]->get_mover_id();
        hanging.type = Hanging::MOVER;
        // touchingMover = touchingMovers[n];
      }
    }
  } // touchingOk
}



Boolean World::check_wrap_around(Size &offset,const Area &area) {
  // Only consider wrapping-around if the slot is completely open
  // except for being partially outside the world.  So, pass in False
  // for the outsideClosed argument of World::open(). */
  if (!open(area,False,False,False,False)) {
    return False;
  }

  Pos arPos;
  Size arSize;
  area.get_rect(arPos,arSize);

  // Can't have any objects that are as big as the world.
  assert(arSize.width < size.width);

  // Go off left side of screen, appear flush against right wall.
  if (arPos.x < 0) {
    offset.width = size.width - arSize.width - arPos.x;
    offset.height = 0;

    // Only do wrap-around if the new position is open.
    Area newArea = area + offset;
    return open(newArea);
  }

  // Go off right side of screen, appear flush against left wall.
  if (arPos.x + arSize.width > size.width) {
    offset.width = -arPos.x;
    offset.height = 0;

    // Only do wrap-around if the new position is open.
    Area newArea = area + offset;
    return open(newArea);
  }

  return False;
}



Boolean World::open_iter(Area &area,int &changed,Dir dir) {
  int offOne = 0;

  Loc list[AR_WSQUARES_MAX];
  int nsquares;
  area.wsquares(list,nsquares);
  for (int n = 0; n < nsquares; n++) {
    // Support for up_downs.
    if (inside(list[n]) && (map[list[n].r][list[n].c] == WupDown) &&
	      ((dir == CO_R) || (dir == CO_L))) {
	    return False;
    }

    // Avoid closed block.
    if (! open(list[n])) {
	    int temp = area.avoid_wsquare_dir(list[n],dir);
	    offOne = Utils::maximum(offOne,temp);
    }

    // Look at mover corresponding to moverSquare at list[n].
    if (inside(list[n])) {
      UnionSquare *uSquare = unionSquares[list[n].r][list[n].c];
      if (uSquare && uSquare->type == UN_MOVER) {
				const Area &moverArea = uSquare->mSquare.mover->get_area();
				if (moverArea.overlap(area)) {
            // Movers always shift you up.
            if (dir != CO_UP) {
              return False;
            }

  	        int temp = area.avoid_area_dir(uSquare->mSquare.mover->get_area(),dir);
	          offOne = max(offOne,temp);
				}
      }
    }
  }
  if (offOne == 0) {
    return True;
  }

  changed += offOne;
  area.shift(open_size(offOne,dir));
  return False;
}



Size World::open_size(int offset,Dir dir) {
  Size ret;
  ret.width = ret.height = 0;

  switch (dir) {
  case CO_R:
    ret.width = offset;
    break;
  case CO_DN:
    ret.height = offset;
    break;
  case CO_L:
    ret.width = -offset;
    break;
  case CO_UP:
    ret.height = -offset;
    break;
  };

  return ret;
}



Size World::open_size(int offset1,Dir dir1,int offset2,Dir dir2) {
  Size ret;
  ret.width = ret.height = 0;

  switch (dir1) {
  case CO_R:
    ret.width = offset1;
    break;
  case CO_DN:
    ret.height = offset1;
    break;
  case CO_L:
    ret.width = -offset1;
    break;
  case CO_UP:
    ret.height = -offset1;
    break;
  };

  switch (dir2) {
  case CO_R:
    ret.width = offset2;
    break;
  case CO_DN:
    ret.height = offset2;
    break;
  case CO_L:
    ret.width = -offset2;
    break;
  case CO_UP:
    ret.height = -offset2;
    break;
  };

  return ret;
}



Boolean World::open_try_dirs(Size &offset,const Area &area,const Dir dirs[4],
                             int dirsNum) {
  if (!dirsNum) {
    return False;
  }

  // Trying several directions simultaneously.
  Area areas[4];
  int offsets[4];
  for (int n = 0; n < dirsNum; n++) {
    areas[n] = area;
    offsets[n] = 0;
  }

  for (int iter = 0; iter < OPEN_ITERATIONS; iter++) {
    Boolean oks[4];
    int n;
    for (n = 0; n < dirsNum; n++) {
	    oks[n] = open_iter(areas[n],offsets[n],dirs[n]);
    }

    int offsetOkMin = Utils::minimum(offsets,oks,dirsNum);

    // Return after first iteration that gives an open area in one of the directions.
    for (n = 0; n < dirsNum; n++) {
	    if (oks[n] && (offsets[n] == offsetOkMin)) {
	      offset = open_size(offsets[n],dirs[n]);
	      return True;
	    }
    }
  }

  return False;
}



Boolean World::open_try_corners(Size &offset,const Area &area) {
	Dir corners[4];
	corners[0] = CO_DN_R;
	corners[1] = CO_DN_L;
	corners[2] = CO_UP_L;
	corners[3] = CO_UP_R;

	// Try all four corners.
	for (int cNum = 0; cNum < 4; cNum++) {
		Loc outer[3];
		Loc inner;
		if (area.corner_offset(offset,outer,inner,corners[cNum])) {
			// Check that all 3 outer wsquares are closed.
			Boolean validCorner = True;
			for (int n = 0; n < 3; n++) {
				if (open(outer[n])) {
					validCorner = False;
					break;
				}
			}
			// Check that the inner wsquare of the corner is open.
			if (!open(inner)) {
				validCorner = False;
      }

			if (validCorner) {
				// offset is already set.
				return True;
      }
		}
	}

	return False;
}



Boolean World::open_try_diagonals(Size &offset,const Area &area) {
  Dir dirs[4];
  dirs[0] = CO_R;
  dirs[1] = CO_DN;
  dirs[2] = CO_L;
  dirs[3] = CO_UP;

  int offsets[4];
  int n;
  for (n = 0; n < 4; n++) {
    offsets[n] = 0;
  }

  Loc list[AR_WSQUARES_MAX];
  int nsquares;
  area.wsquares(list,nsquares);
  for (n = 0; n < nsquares; n++) {
    if (! open(list[n])) {
      for (int dIndex = 0; dIndex < 4; dIndex++) {
        int dOffset = area.avoid_wsquare_dir(list[n],dirs[dIndex]);
        offsets[dIndex] = Utils::maximum(dOffset,offsets[dIndex]);
      }
    }

    // Look at mover corresponding to moverSquare at list[n].
    if (inside(list[n])) {
      UnionSquare *uSquare = unionSquares[list[n].r][list[n].c];
      if (uSquare && uSquare->type == UN_MOVER) {
        const Area &moverArea = uSquare->mSquare.mover->get_area();
        if (moverArea.overlap(area)) {
          for (int dIndex = 0; dIndex < 4; dIndex++) {
            int dOffset = 
              area.avoid_area_dir(uSquare->mSquare.mover->get_area(),
                                  dirs[dIndex]);
            offsets[dIndex] = Utils::maximum(dOffset,offsets[dIndex]);
          }
        }
      }
    }
  } // if
  
  for (int dIndex = 0; dIndex < 4; dIndex++) {
    Area areaTest = area;
    offset = open_size(offsets[dIndex],dirs[dIndex],
                       offsets[(dIndex + 1) % 4],dirs[(dIndex + 1) % 4]);
    areaTest.shift(offset);
    if (open(areaTest)) {
      return True;
    }
  }
  
  return False;
}



void World::add_posters() {
  // Put in posters.
  for (int n = 0;
       n < POSTERS_ACTUAL_PERCENT * dim.rowMax * dim.colMax;
       n++) {
    // Choose poster appropriate for the current theme.
    int p = Utils::choose(themes[themeIndex].postersNum);
    p = themes[themeIndex].posterIndices[p];

    Loc pLoc;
    if (!empty_box(pLoc,posterDims[p],True,True,True)) {
	    break;
    }

    Loc loc;
    for (loc.c = pLoc.c; loc.c < pLoc.c + posterDims[p].colMax; loc.c++) {
      for (loc.r = pLoc.r; loc.r < pLoc.r + posterDims[p].rowMax; loc.r++) {
        if (! unionSquares[loc.r][loc.c]) {
          unionSquares[loc.r][loc.c] = new UnionSquare;
        }
        assert(unionSquares[loc.r][loc.c]);
        unionSquares[loc.r][loc.c]->type = UN_POSTER;
        unionSquares[loc.r][loc.c]->pSquare.poster = p;
        unionSquares[loc.r][loc.c]->pSquare.loc.c = loc.c - pLoc.c;
        unionSquares[loc.r][loc.c]->pSquare.loc.r = loc.r - pLoc.r;
  	  }
    }
  }
}



void World::add_doors() {
  // Put in doors.
  for (int n = 0;
       n < DOORS_ACTUAL_PERCENT * rooms.downMax * rooms.acrossMax;
       n++) {
    Loc loc1;
    Loc loc2;
    Dim doorDim(2,1);  // Hardwired size of doors.
    if (!empty_touching_box(loc1,doorDim,True,True,True)) {
      break;
    }
    if (!empty_touching_box(loc2,doorDim,True,True,True)) {
      break;
    }

    // Don't want doors on top of each other.
    Area area1(AR_RECT,loc1,doorDim);
    Area area2(AR_RECT,loc2,doorDim);
    if (area1.overlap(area2)) {
      break;
    }

    // Lower half of doors.
    Loc loc1d = loc1.move(1,0);
    Loc loc2d = loc2.move(1,0);

    // We actually create 4 doors, two pairs.  Each apparent door is
    // actually two DoorSquares.

    // Cross connect two new doors at loc1 and loc2.
    unionSquares[loc1.r][loc1.c] = new UnionSquare;
    assert(unionSquares[loc1.r][loc1.c]);
    unionSquares[loc1.r][loc1.c]->type = UN_DOOR;
    unionSquares[loc1.r][loc1.c]->dSquare.dest = loc2;
    unionSquares[loc1.r][loc1.c]->dSquare.topBottom = W_DOOR_TOP;

    unionSquares[loc2.r][loc2.c] = new UnionSquare;
    assert(unionSquares[loc2.r][loc2.c]);
    unionSquares[loc2.r][loc2.c]->type = UN_DOOR;
    unionSquares[loc2.r][loc2.c]->dSquare.dest = loc1;
    unionSquares[loc2.r][loc2.c]->dSquare.topBottom = W_DOOR_TOP;

    // Cross connect two new doors at loc1d and loc2d.
    unionSquares[loc1d.r][loc1d.c] = new UnionSquare;
    assert(unionSquares[loc1d.r][loc1d.c]);
    unionSquares[loc1d.r][loc1d.c]->type = UN_DOOR;
    unionSquares[loc1d.r][loc1d.c]->dSquare.dest = loc2d;
    unionSquares[loc1d.r][loc1d.c]->dSquare.topBottom = W_DOOR_BOTTOM;

    unionSquares[loc2d.r][loc2d.c] = new UnionSquare;
    assert(unionSquares[loc2d.r][loc2d.c]);
    unionSquares[loc2d.r][loc2d.c]->type = UN_DOOR;
    unionSquares[loc2d.r][loc2d.c]->dSquare.dest = loc1d;
    unionSquares[loc2d.r][loc2d.c]->dSquare.topBottom = W_DOOR_BOTTOM;
  } // for
}



void World::add_movers() {
  assert(moversNum <= MOVERS_MAX && blueprints);
  // Movers shouldn't be too big.
  assert(objectDimMax.colMax * WSQUARE_WIDTH >= moverSize.width);

  // Number of movers to create.
  const int moversActual = Utils::choose((int)(rooms.acrossMax * rooms.downMax
			                                   * MOVERS_PERCENT) + 1);

  for (int n = 0; n < moversActual && moversNum < MOVERS_MAX; n++) {
    Boolean which = Utils::coin_flip();
    Boolean ok = False;
    int tries = 0;
    while (!ok && tries < MOVER_TRIES) {
      // Decide between horizontal and vertical mover.
      if (which) {
        ok = add_vert_mover();
      }
      else {
        ok = add_horiz_mover();
      }

      tries++;
    }
  }
}



Boolean World::add_horiz_mover() {
  assert(moversNum <= MOVERS_MAX && blueprints);
  // Movers shouldn't be too big.
  assert(objectDimMax.colMax * WSQUARE_WIDTH >= moverSize.width);


  // loc will walk right or left, filling in track.
  Loc loc;
  loc.r = Utils::choose(dim.rowMax);
  loc.c = Utils::choose(dim.colMax);

  // Check space surrounding initial choice for minimum track length.
  int delta = Utils::coin_flip() ? 1 : -1;  // right or left.
  Loc check;
  for (check.c = loc.c - (delta == -1 ? MOVERS_HORIZ_MIN_TRACK : 1) * objectDimMax.colMax;
	 check.c < loc.c + (delta == 1 ? MOVERS_HORIZ_MIN_TRACK + 1 : 2) * objectDimMax.colMax; // mover track plus blank space
	 check.c++) {
	  for (check.r = loc.r - objectDimMax.rowMax;
	       check.r <= loc.r + objectDimMax.rowMax;
	       check.r++) {
	    if (!open(check,True,True,True)) {
        return False;
      }
    }
  }


  // Create mover
  movers[moversNum] = new Mover;
  assert(movers[moversNum]);

  // Start at the right edge of the mover and go left
  if (delta == -1) {
    loc.c += objectDimMax.colMax - 1;
  }
  // else we are at the left edge going right.


  // minimum and maximum values of left side of mover, inclusive
  int mLeft = loc.c;
  int mRight = loc.c;

  // Go right/left until hit a wall or decide to stop.
  // Must do at least MOVERS_HORIZ_MIN_TRACK multiples of the mover width.
  int n = 0;
  Boolean ok = True;
  while (ok && (n <= MOVERS_HORIZ_MIN_TRACK * objectDimMax.colMax || 
                Utils::choose(MOVERS_HORIZ_TRACK_LENGTH))) {
    // Check if we can put a mover at loc.
	  for (check.c = loc.c;
        check.c != loc.c + delta * objectDimMax.colMax;
        check.c += delta) {
      for (check.r = loc.r - objectDimMax.rowMax;
       check.r <= loc.r + objectDimMax.rowMax;
       check.r++) {
	      if (!open(check,True,True,True)) {
		      ok = False;
          // Our above check of the surrounding space was not correct.
          assert(n >= MOVERS_HORIZ_MIN_TRACK * objectDimMax.colMax);
        }
        if (!ok) {
          break;
        }
      }
      if (!ok) {
        break;
      }
    }
                
    // Add mover track at loc.
    if (ok) {
      // Add a mover square.
      assert(inside(loc) && 
             (map[loc.r][loc.c] == Wempty ) &&
             !unionSquares[loc.r][loc.c]);
      unionSquares[loc.r][loc.c] = new UnionSquare;
      assert(unionSquares[loc.r][loc.c]);
      unionSquares[loc.r][loc.c]->type = UN_MOVER;
      unionSquares[loc.r][loc.c]->mSquare.mover = movers[moversNum];
      unionSquares[loc.r][loc.c]->mSquare.orientation = OR_HORIZ;

      mLeft = Utils::minimum(mLeft,loc.c);
      mRight = Utils::maximum(mRight,loc.c);
      n++;

      loc.c += delta;
    }
  } // while ok

  // Check again that we got enough track.
  assert(mRight - mLeft + 1 >= MOVERS_HORIZ_MIN_TRACK * objectDimMax.colMax);


  // initialize mover.
  // Choose X position somewhere in range of mover squares.
  int rangeX = (mRight - mLeft + 1 - objectDimMax.colMax) * WSQUARE_WIDTH + 1;
  assert(rangeX > 0); // Or not enough room.
  int startX = Utils::choose(rangeX) + mLeft * WSQUARE_WIDTH; 

  Pos pos(startX,loc.r * WSQUARE_HEIGHT);
  Area area(AR_RECT,pos,moverSize);

  Size vel;
  vel.set(Utils::coin_flip() ? W_MOVER_SPEED : -W_MOVER_SPEED,0);
  // initialize the mover.
  MoverId moverId;
  moverId.index = moversNum;
  moverId.unique = uniqueGen++;

  movers[moversNum]->init(this,locator,area,vel,moverId);

  // Dummy mover object, so mover gets redrawn.
  PhysMoverP p = new PhysMover(this,locator,movers[moversNum]);
  assert(p);
  locator->add(p);

  movers[moversNum]->set_phys_mover_id(p->get_id());

  moversNum++;
  return True;
}



// What an ugly function.  I should redo this.
Boolean World::add_vert_mover() {
  assert(moversNum <= MOVERS_MAX && blueprints);
  // Movers shouldn't be too big.
  assert(objectDimMax.colMax * WSQUARE_WIDTH >= moverSize.width);


  // Choose a random room, check if its middle is a ladder, if so, change
  // the ladder to a bunch of mover squares and create a new mover.
  // Else continue.
  Loc init;
  int m; // holder for the down/across value of the room chosen.
  m = Utils::choose(rooms.acrossMax);
  init.c =  m * W_ROOM_COL_MAX + blueprints->get_middle_col(m);
  m = Utils::choose(rooms.downMax);
  init.r =  m * W_ROOM_ROW_MAX + blueprints->get_middle_row(m);


  // Found a ladder, create a new mover and set all the ladder wsquares to be
  // MoverSquares pointing to the new mover.
  if (!(map[init.r][init.c] == Wladder && 
        map[init.r][init.c+1] == Wladder)) {
    return False;
  } 

  assert(!unionSquares[init.r][init.c]);

  // All wsquares between mTop and mBottom, exclusive, are part of the
  // mover.
  int mTop = init.r;
  int mBottom = init.r;

  movers[moversNum] = new Mover;
  assert(movers[moversNum]);

  // delta is -1, then 1.  Go up, then down..
  for (int delta = -1; delta <= 1; delta += 2) {
    Loc loc = init;

    // To avoid hitting init twice, skip it when going down.
    if (delta == 1) {
      loc.r++;
    }

    // Scan up/down depending on value of delta.
    Boolean bothInside = inside(loc) && inside(loc.r,loc.c+1);
    Boolean bothLadders = bothInside && 
      (map[loc.r][loc.c] == Wladder && map[loc.r][loc.c+1] == Wladder);
    Boolean aboveInside = inside(loc.r-1,loc.c) && inside(loc.r-1,loc.c+1);
    Boolean aboveMoverSq = aboveInside && 
      map[loc.r-1][loc.c] == Wempty && 
      map[loc.r-1][loc.c+1] == Wempty &&
      unionSquares[loc.r-1][loc.c] && 
      unionSquares[loc.r-1][loc.c+1] &&
      unionSquares[loc.r-1][loc.c]->type == UN_MOVER && 
      unionSquares[loc.r-1][loc.c+1]->type == UN_MOVER;
    Boolean bothWalls = bothInside && 
      (map[loc.r][loc.c] == Wwall && map[loc.r][loc.c+1] == Wwall);
    // This shit is all so that movers can stick down one square into floor.
    // CAREFUL!! Crappy code duplicated below.

    while (bothLadders ||  // normal case 
           (delta == 1 && aboveMoverSq && bothWalls) ) { // sticking down one
      
      // Scan to right.
      for (; loc.c < init.c + objectDimMax.colMax; loc.c++) {
        // Add a mover square.
        assert(inside(loc) && 
               (map[loc.r][loc.c] == Wladder || map[loc.r][loc.c] == Wwall) &&
               !unionSquares[loc.r][loc.c]);
        unionSquares[loc.r][loc.c] = new UnionSquare;
        assert(unionSquares[loc.r][loc.c]);
        unionSquares[loc.r][loc.c]->type = UN_MOVER;
        unionSquares[loc.r][loc.c]->mSquare.mover = movers[moversNum];
        unionSquares[loc.r][loc.c]->mSquare.orientation = OR_VERT;

        // Still leave overlap of wall and moversquare.
        if (map[loc.r][loc.c] != Wwall) {
          map[loc.r][loc.c] = Wempty;
        }

        mTop = Utils::minimum(mTop,loc.r);
        mBottom = Utils::maximum(mBottom,loc.r + 1);
    	}
	    loc.c = init.c;

      // When up at the top.
      // Remove annoying ladder sticking up.
      if (delta ==  -1 &&
          map[loc.r - objectDimMax.rowMax - 1][loc.c] != Wladder &&
          // Just to be sure we don't kill of some unionSquares.
          !unionSquares[loc.r - objectDimMax.rowMax - 1][loc.c]) {
        int locTop = loc.r - objectDimMax.rowMax;
        for (loc.r--; loc.r >= locTop; loc.r--) {
          for (loc.c = init.c; loc.c < init.c + objectDimMax.colMax; 
               loc.c++) {
            map[loc.r][loc.c] = Wempty;
          }
        }
        
        break;
      } // annoying ladder sticking up.
      
      loc.c = init.c;
      loc.r += delta;

      
      bothInside = inside(loc) && inside(loc.r,loc.c+1);
      bothLadders = bothInside && 
        (map[loc.r][loc.c] == Wladder && map[loc.r][loc.c+1] == Wladder);
      aboveInside = inside(loc.r-1,loc.c) && inside(loc.r-1,loc.c+1);
      aboveMoverSq = aboveInside && 
        map[loc.r-1][loc.c] == Wempty && 
        map[loc.r-1][loc.c+1] == Wempty &&
        unionSquares[loc.r-1][loc.c] && 
        unionSquares[loc.r-1][loc.c+1] &&
        unionSquares[loc.r-1][loc.c]->type == UN_MOVER && 
        unionSquares[loc.r-1][loc.c+1]->type == UN_MOVER;
      bothWalls = bothInside && 
        (map[loc.r][loc.c] == Wwall && map[loc.r][loc.c+1] == Wwall);
      // This shit is all so that movers can stick down one square into 
      // the floor.

    } // while
  } // for delta
  // Note: loc is now at the left side of the former ladder,
  // one square beneath it.

  assert(mTop < mBottom);  // Or no squares were added.

  // Choose Y position somewhere in range of mover squares.
  int rangeY = (mBottom - mTop) * WSQUARE_HEIGHT - moverSize.height + 1;
  assert(rangeY > 0); // or movers are more than one square high.
  int startY = Utils::choose(rangeY) + mTop * WSQUARE_HEIGHT;

  Pos pos(init.c * WSQUARE_WIDTH,startY);
  Area area(AR_RECT,pos,moverSize);

  Size vel;
  vel.set(0,Utils::coin_flip() ? W_MOVER_SPEED : -W_MOVER_SPEED);
  // initialize the mover.
  MoverId moverId;
  moverId.index = moversNum;
  moverId.unique = uniqueGen++;

  movers[moversNum]->init(this,locator,area,vel,moverId);

  // Dummy mover object, so mover gets redrawn.
  PhysMoverP p = new PhysMover(this,locator,movers[moversNum]);
  assert(p);
  locator->add(p);

  movers[moversNum]->set_phys_mover_id(p->get_id());

  moversNum++;
  return True;
}



void World::delete_movers() {
  // The locator should take care of killing off the PhysMovers
  // in Locator::reset().

  int n;
  for (n = 0; n < moversNum; n++) {
    delete movers[n];
    movers[n] = NULL;
  }
  moversNum = 0;
}



void World::mover_list_add(MoverP list[],int &nItems,const Loc &loc) {
  if (!inside(loc)) {
    return;
  }

  UnionSquare *uSquare = unionSquares[loc.r][loc.c];
  if (uSquare && uSquare->type == UN_MOVER) {
    MoverP mover = uSquare->mSquare.mover;

    // Only add movers that overlap loc.
    const Area &area = mover->get_area();
    if (!area.overlap(loc)) {
      return;
    }

    // Check for duplicates.
    for (int n = 0; n < nItems; n++) {
      if (list[n] == mover) {
        return;
      }
    }

    // Add to list.
    assert(nItems < MOVERS_MAX);
    list[nItems] = mover;
    nItems++;
  }
}



Boolean World::useMovers = True; // On by default.