File: init.c

package info (click to toggle)
xconq 7.2.2-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,296 kB
  • ctags: 9,199
  • sloc: ansic: 107,849; sh: 2,108; perl: 2,057; makefile: 1,177; sed: 161; csh: 50; awk: 49; lisp: 39
file content (1570 lines) | stat: -rw-r--r-- 43,996 bytes parent folder | download | duplicates (2)
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
/* General Xconq initialization.
   Copyright (C) 1987-1989, 1991-1997 Stanley T. Shebs.

Xconq 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, or (at your option)
any later version.  See the file COPYING.  */

/* Initialization is complicated, because Xconq needs lots of setup
   for maps, units, sides, and the like.  The data must also be able
   to come from saved games, scenarios, bare maps in files, type
   definitions, or be synthesized if necessary. */

/* The general theory is that everything gets set to a known empty
   state, then all files are read, then all the synthesis methods get
   run.  Files/readers and synth methods are each responsible for
   preventing fatal conflicts. */

#include "conq.h"
extern char *curdatestr;
#include "kernel.h"
#include "version.h"

static void cache_possible_types PARAMS ((void));
static void kill_excess_units PARAMS ((void));
static void trial_assign_player PARAMS ((int i, Player *player));
static void init_view_2 PARAMS ((Side *side, int terrainset));
static void init_view_cell PARAMS ((int x, int y));
static void maybe_init_view_cell PARAMS ((int x, int y));
static int adj_seen_terrain PARAMS ((int x, int y, Side *side));
static void init_ranged_views PARAMS ((void));

/* This is true when the set of types has been defined.  Subsequently
   only scenarios based on those types can be loaded. */

int typesdefined = FALSE;

/* This is true after the game is totally synthesized, but players may
   not all be in the game yet. */

int gamedefined = FALSE;

/* These are handy tmp vars, usually used with function args. */

short tmputype;
short tmpmtype;
short tmpttype;

/* The array of player/side matchups. */

Assign *assignments = NULL;

/* True when no AIs should be set up at game start. */

short initially_no_ai;

/* The list of pathnames to library directories. */

LibraryPath *xconq_libs;

LibraryPath *last_user_xconq_lib;

/* The name of the default game module. */

char *standard_game_name;

/* This is the main string printing buffer.  It is statically allocated
   because it is used even before the main data structures are inited. */

char spbuf[BUFSIZE];

/* This is an auxiliary buffer.  Also needs to be statically allocated. */

char tmpbuf[BUFSIZE];

/* This is true if the interface was requested to put up a new game
   dialog (kind of a random file for this, but *is* init-related). */

int option_popup_new_game_dialog;

#ifdef DESIGNERS

/* This is true if all sides with displays should become designers
   automatically. */

int allbedesigners = FALSE;

/* This is the current count of how many sides are designers. */

int numdesigners = 0;

#endif /* DESIGNERS */

int max_zoc_range;

short *u_possible;

/* The table of all available synthesis methods. */

struct synthtype {
    int key;
    int (*fn) PARAMS ((int calls, int runs));
    int dflt;
    int calls;
    int runs;
} synthmethods[] = {
    { K_MAKE_FRACTAL_PTILE_TERRAIN, make_fractal_terrain, TRUE, 0, 0 },
    { K_MAKE_RANDOM_TERRAIN, make_random_terrain, FALSE, 0, 0 },
    { K_MAKE_EARTHLIKE_TERRAIN, make_earthlike_terrain, FALSE, 0, 0 },
    { K_MAKE_MAZE_TERRAIN, make_maze_terrain, FALSE, 0, 0 },
    { K_MAKE_RIVERS, make_rivers, TRUE, 0, 0 },
    { K_MAKE_COUNTRIES, make_countries, TRUE, 0, 0 },
    { K_MAKE_INDEPENDENT_UNITS, make_independent_units, TRUE, 0, 0 },
    { K_MAKE_INITIAL_MATERIALS, make_initial_materials, TRUE, 0, 0 },
    { K_NAME_UNITS_RANDOMLY, name_units_randomly, TRUE, 0, 0 },
    { K_NAME_GEOGRAPHICAL_FEATURES, name_geographical_features, TRUE, 0, 0 },
    { K_MAKE_ROADS, make_roads, TRUE, 0, 0 },
    { K_MAKE_WEATHER, make_weather, TRUE, 0, 0 },
    { K_MAKE_RANDOM_DATE, make_random_date, FALSE, 0, 0 },
    { -1, NULL, 0, 0, 0 }
};

/* Allow environment vars to override compiled-in library and game. */

void
init_library_path(path)
char *path;
{
    char *xconqlib;

    xconqlib = getenv("XCONQLIB");
    if (empty_string(xconqlib)) {
	if (!empty_string(path)) {
	    xconqlib = path;
	} else {
	    xconqlib = default_library_filename();
	}
    }
    xconq_libs = (LibraryPath *) xmalloc(sizeof(LibraryPath));
    xconq_libs->path = copy_string(xconqlib);
    last_user_xconq_lib = NULL;
    standard_game_name = getenv("XCONQSTANDARDGAME");
    if (empty_string(standard_game_name))
      standard_game_name = STANDARD_GAME;
}

void
add_library_path(path)
char *path;
{
    LibraryPath *lib;

    /* A NULL path indicates that all the existing paths should go away. */
    if (path == NULL) {
	xconq_libs = last_user_xconq_lib = NULL;
	return;
    }
    lib = (LibraryPath *) xmalloc(sizeof(LibraryPath));
    lib->path = copy_string(path);
    /* Insert this after the previous user-specified library, if there
       was any, but before the default library places. */
    if (last_user_xconq_lib != NULL) {
	lib->next = last_user_xconq_lib->next;
	last_user_xconq_lib->next = lib;
    } else {
	lib->next = xconq_libs;
	xconq_libs = lib;
    }
    last_user_xconq_lib = lib;
}

/* This is a general init that prepares data structures to be filled in
   by a game definition.  It should run *before* command line parsing. */

void
init_data_structures()
{
    init_xrandom(-1);
    init_lisp();
    init_types();
    init_globals();
    init_namers();
    init_world();
    init_history();
    init_sides();
    init_agreements();
    init_units();
    init_nlang();
    init_help();
    init_scorekeepers();
    init_actions();
    init_tasks();
    init_ai_types();
    init_write();
    curdatestr = xmalloc(BUFSIZE);
}

/* Build the default list of synthesis methods. */

void
set_g_synth_methods_default()
{
    int i;
    Obj *synthlist = lispnil, *synthlistend = lispnil, *tmp;

    for (i = 0; synthmethods[i].key >= 0; ++i) {
	if (synthmethods[i].dflt) {
	    tmp = cons(intern_symbol(keyword_name(synthmethods[i].key)),
		       lispnil);
	    if (synthlist == lispnil) {
		synthlist = synthlistend = tmp;
	    } else {
		set_cdr(synthlistend, tmp);
		synthlistend = tmp;
	    }
	}
    }
    /* Now alter the global variable to contain this list. */
    set_g_synth_methods(synthlist);
}

/* Run a doublecheck on plausibility of game parameters.  Additional
   checks are performed elsewhere as needed, for instance during random
   generation.  Serious mistakes exit now, since they can cause all sorts
   of strange behavior and core dumps.  It's a little more friendly to only
   exit at the end of the tests, so all the mistakes can be found at once. */

/* In theory, if a game passes these tests, then Xconq will never crash. */

void
check_game_validity()
{
    int failed = FALSE, movers = FALSE, actors = FALSE;
    int u1, u2, m1, t1, t2, i, methkey, found;
    Obj *synthlist = g_synth_methods(), *methods, *method;

    /* We must have at least one kind of unit. */
    if (numutypes < 1) {
	init_error("no unit types have been defined");
	failed = TRUE;
    }
    /* OK not to have any types of materials. */
    /* We must have at least one kind of terrain. */
    if (numttypes < 1) {
	init_error("no terrain types have been defined");
	failed = TRUE;
    }
    /* Make sure inter-country distances relate correctly. */
    if (g_separation_min() >= 0
	&& g_separation_max() >= 0
	&& !(g_separation_min() <= g_separation_max())) {
	init_warning("country separations %d to %d screwed up",
		     g_separation_min(), g_separation_max());
    }
    /* Check all method names in the synthesis list. */
    for_all_list(synthlist, methods) {
	method = car(methods);
	found = FALSE;
	if (symbolp(method)) {
	    methkey = keyword_code(c_string(method));
	    for (i = 0; synthmethods[i].key >= 0; ++i) {
		if (methkey == synthmethods[i].key) {
		    found = TRUE;
		    break;
		}
	    }
	}
	if (!found) {
	    sprintlisp(spbuf, method, BUFSIZE);
	    init_warning("bad synthesis method `%s', ignoring", spbuf);
	}
    }
    /* (Need more general game checks.) */
    max_zoc_range = -1;
    /* Check that all the unit names and chars are distinct. */
    for_all_unit_types(u1) {
	for_all_unit_types(u2) {
	    /* Only do "upper triangle" of utype x utype matrix. */
	    if (u1 < u2) {
		if (strcmp(u_type_name(u1), u_type_name(u2)) == 0) {
		    init_warning(
		     "unit types %d and %d are both named \"%s\"",
				 u1, u2, u_type_name(u1));
		    /* This is bad but not disastrous, so don't fail. */
		}
	    }
	    if (uu_zoc_range(u1, u2) > max_zoc_range) {
		max_zoc_range = uu_zoc_range(u1, u2);
	    }
	}
    }
    /* (Eventually check material types also.) */
    /* Check that all terrain names and chars are distinct. */
    for_all_terrain_types(t1) {
	for_all_terrain_types(t2) {
	    /* Only do "upper triangle" of ttype x ttype matrix. */
	    if (t1 < t2) {
		if (strcmp(t_type_name(t1), t_type_name(t2)) == 0) {
		    init_warning(
		     "terrain types %d and %d are both named \"%s\"",
		     t1, t2, t_type_name(t1));
		    /* This is bad but not disastrous, so don't fail. */
		}
		/* Should check that colors are different from each other
		   and from builtin colors? */
	    }
	}
    }
    /* Check various unit type properties. */
    for_all_unit_types(u1) {
	/* Can't make use of this yet, so error out if anybody tries. */
	if (u_available(u1) != 1) {
	    init_error("unit type %d must always be available", u1);
	    failed = TRUE;
	}
	/* Can't make use of this yet, so error out if anybody tries. */
	if (u_action_priority(u1) != 0) {
	    init_error("unit type %d cannot have a nonzero action priority", u1);
	    failed = TRUE;
	}
	/* should be part of general bounds check */
	if (u_cp(u1) <= 0) {
	    init_error("unit type %d has nonpositive cp", u1);
	    failed = TRUE;
	}
	if (u_hp(u1) <= 0) {
	    init_error("unit type %d has nonpositive hp", u1);
	    failed = TRUE;
	}
	if (u_parts(u1) <= 0) {
	    init_error("unit type %d has nonpositive part count.", u1);
	    failed = TRUE;
	}
	if (u_parts(u1) > 0 && u_hp(u1) % u_parts(u1) != 0) {
	    init_error("unit type %d hp not a multiple of its parts.", u1);
	    failed = TRUE;
	}
	if (u_speed(u1) > 0) {
	    movers = TRUE;
	}
	if (u_acp(u1) > 0) {
	    actors = TRUE;
	}
    }
    /* Check various material type properties. */
    for_all_material_types(m1) {
	/* Can't make use of this yet, so error out if anybody tries. */
	if (m_available(m1) != 1) {
	    init_error("material type %d not always available", m1);
	    failed = TRUE;
	}
    }
    /* Check various terrain type properties. */
    for_all_terrain_types(t1) {
	/* Can't make use of this yet, so error out if anybody tries. */
	if (t_available(t1) != 1) {
	    init_error("terrain type %d not always available", t1);
	    failed = TRUE;
	}
    }
    /* If nothing can move and nothing can build, this will probably be
       a really dull game, but there may be such games, do don't say
       anything normally. */
    if (numutypes > 0 && !actors) {
	Dprintf("No actors have been defined.\n");
    }
    if (numutypes > 0 && !movers) {
	Dprintf("No movers have been defined.\n");
    }
    if (numttypes > 0 && numcelltypes == 0) {
	init_error("no terrain type has been allowed for cells");
	failed = TRUE;
    }
    /* This is a really bad game definition, leave before we crash.  This would
       only be executed on systems where init_error doesn't exit immediately. */
    if (failed) {
	exit(0);
    }
    Dprintf("Finished checking game design.\n");
    Dprintf("It defines %d unit types, %d material types, %d terrain types.\n",
	    numutypes, nummtypes, numttypes);
}

/* Calculate the values of global variables that are used everywhere. */

void
calculate_globals()
{
    /* The game is now completely defined; no further user-specified changes can
       occur. */
    gamedefined = TRUE;
    calculate_world_globals();
    /* This needs to be precalculated instead of as-needed, since range can be
       validly both negative and positive, so no way to distinguish uninitialized. */
    {
	int u1, u2, range;
    	extern int max_detonate_on_approach_range;

	max_detonate_on_approach_range = -1;
	for_all_unit_types(u1) {
	    for_all_unit_types(u2) {
		range = uu_detonate_approach_range(u1, u2);
		max_detonate_on_approach_range = max(range, max_detonate_on_approach_range);
	    }
	}
    }	
}

/* Clean up all the objects and cross-references. NOTE: this may be
   called multiple times!  It should not have any additional effects
   if called more than once. */

void
patch_object_references()
{
    int numhere, numoccs, t, id;
    Unit *unit, *transport, *unit2;
    Side *side;
    Obj *utref, *utorig;

    /* This cannot be called as part of prealloc_debug(), since that
       can occur before any types are defined.  This should happen
       after all unit types have been defined but before a first save;
       this is as good a place as any. */
    shortest_unique_name(0);
    /* Use read-in ids to fill in side slots that point to other objects. */
    for_all_sides(side) {
	if (side->playerid > 0) {
	    side->player = find_player(side->playerid);
	}
	if (side->controlled_by_id > 0) {
	    side->controlled_by = side_n(side->controlled_by_id);
	}
	if (side->self_unit_id > 0) {
	    side->self_unit = find_unit(side->self_unit_id);
	}
    }
    for_all_units(unit) {
	/* It's possible that dead units got read in, so check. */
	if (alive(unit)) {
	    if (unit->transport_id != lispnil) {
		transport = NULL;
		utref = utorig = unit->transport_id;
		/* We have a Lisp object; use it to identify a particular
		   unit as the transport. */
		if (symbolp(utref) && boundp(utref)) {
		    utref = symbol_value(utref);
		}
		if (numberp(utref)) {
		    transport = find_unit(c_number(utref));
		    if (transport == NULL)
		      init_warning("could not find a transport id %d for %s",
				   c_number(utref), unit_desig(unit));
		} else if (stringp(utref)) {
		    transport = find_unit_by_name(c_string(utref));
		    if (transport == NULL)
		      init_warning("could not find a transport named \"%s\" for %s",
				   c_string(utref), unit_desig(unit));
		} else if (symbolp(utref)) {
		    transport = find_unit_by_symbol(utref);
		    if (transport == NULL)
		      init_warning("could not find a transport with sym \"%s\" for %s",
				   c_string(utref), unit_desig(unit));
		} else {
		    /* not a recognized way to refer to a unit */
		    sprintlisp(tmpbuf, utorig, BUFSIZE);
		    init_warning("could not find transport %s for %s",
				 tmpbuf, unit_desig(unit));
		}
		/* It is important to make sure that unit->x, and
		   unit->y are negative at this point.  Otherwise, the
		   coverage will be messed up for units put into
		   transports that have not yet been placed.  They
		   will be covered for entering the cell, and again
		   when the transport enters the cell. */
		if (transport != NULL) {
		    if (transport != unit->transport) {
			if (unit->transport != NULL) {
			    leave_transport(unit);
			}
			/* (also check that this is a valid transport type?) */
			enter_transport(unit, transport);
		    }
		} else {
		    /* (could let the unit enter the cell, or could
		       make it infinitely postponed) */
		}
	    } else {
		/* Check that the unit's location is meaningful. */
		if (!inside_area(unit->x, unit->y)) {
		    if (inside_area(unit->prevx, unit->prevy)) {
			t = terrain_at(unit->prevx, unit->prevy);
			if ((can_occupy_cell(unit, unit->prevx, unit->prevy)
			     && !ut_vanishes_on(unit->type, t)
			     && !ut_wrecks_on(unit->type, t))
			    || can_occupy_conn(unit, unit->prevx, unit->prevy, unit->z)) {
			    enter_cell(unit, unit->prevx, unit->prevy);
			} else {
			    numhere = numoccs = 0;
			    /* Search this cell for units to enter. */
			    for_all_stack(unit->prevx, unit->prevy, transport) {
				++numhere;
				if (unit->side == transport->side
				    && can_occupy(unit, transport)) {
				    enter_transport(unit, transport);
				    break;
				}
				if (unit->side == transport->side
				    && can_occupy(transport, unit))
				  ++numoccs;
			    }
#if 0
			    /* Try having all the existing units enter
                               the transport. */
			    /* (but doesn't work if only some units
                               should go into transport) */
			    if (!inside_area(unit->x, unit->y)
				&& numoccs == numhere) {
				for_all_stack(unit->prevx, unit->prevy, unit2) {
				    enter_transport(unit2, unit);
				}
				enter_cell(unit, unit->prevx, unit->prevy);
			    }
#endif
			    if (!inside_area(unit->x, unit->y)) {
				init_warning("Can't put %s at %d,%d (%s)",
					     unit_desig(unit),
					     unit->prevx, unit->prevy,
					     (can_occupy_cell(unit, unit->prevx, unit->prevy)
					      ? "deadly terrain" : "no room"));
			    }
			}
			/* This prevents attempts to do a second enter_cell
			   during initialization. */
			unit->prevx = unit->prevy = -1;
		    } else if (unit->prevx == -1 && unit->prevy == -1) {
		    	/* This will be left alone - should have pos
                           filled in later. */
		    } else if (unit->cp >= 0) {
			/* Warn, but only if there's no good reason
			   for the unit to have an offworld
			   position. */
			if (area.fullwidth == 0) {
			    init_warning("%s is at offworld location, left there",
					 unit_desig(unit));
			}
			/* This will make it be a reinforcement. */
			unit->cp = -1;
		    }
		}
	    }
	    /* Make sure that side numbering will use only new numbers. */
	    if (unit->side != NULL) {
		if (unit->number > 0) {
		    (unit->side->counts)[unit->type] =
		      max((unit->side->counts)[unit->type], 1 + unit->number);
		}
	    } else {
		/* Trash the numbers on indep units. */
		unit->number = 0;
	    }
	    if (completed(unit)) {
		if (unit->act == NULL)
		  init_unit_actorstate(unit, TRUE);
		/* Restore acp that wasn't written out because it was
		   the most normal value. */
		if (unit->act && unit->act->acp < u_acp_min(unit->type))
		  unit->act->acp = u_acp(unit->type);
		/* Restore initacp that wasn't written out because it
		   was the normal value. */
		if (unit->act
		    && unit->act->acp > 0
		    && unit->act->initacp == 0)
		  unit->act->initacp = u_acp(unit->type);
		/* Might already have a plan, leave alone if so. */
		if (unit->plan == NULL) {
		    init_unit_plan(unit);
		}
	    }
	    /* Maybe fill in a formation's leader with a unit. */
	    if (unit->plan != NULL
		&& unit->plan->formation != NULL) {
		id = unit->plan->formation->args[0];
		if (id > 0) {
		    unit->plan->funit = find_unit(id);
		    if (unit->plan->funit == NULL) {
			init_warning("%s missing leader #%d, cancelling formation",
				     unit_desig(unit), id);
			unit->plan->formation = NULL;
		    }
		}
	    }
	} else {
	    /* Dead units need to be disentangled from anything that
	       might have been done to them.  For instance, a module
	       might include a standard collection of units, but then
	       follow up by removing some of those units, and can do
	       it by setting hp == 0.  We want this to work
	       consistently and reliably. */
	    /* Null this out, any possible unit reference is useless. */
	    unit->transport = NULL;
	    if (inside_area(unit->x, unit->y)) {
		leave_cell(unit);
	    }
	}
    }
}

/* Make up a proposed side/player assignment, creating sides and players
   as necessary.  Lock down any assignments that should not be changed,
   but leave everything to be changed as desired. */

/* (when does locking get done?) */

void
make_trial_assignments()
{
    int i = 0;
    Side *side;
    Player *player;

    /* Fill in the side's predefined default and range of initial advantage. */
    for_all_sides(side) {
    	init_side_advantage(side);
    }
    /* Ensure we have as many sides as will be required. */
    while (numsides < g_sides_min()) {
	make_up_a_side();
    }
    while (numsides < min(numplayers, g_sides_max())) {
	make_up_a_side();
    }
    /* Put in all the sides. */
    for_all_sides(side) {
	assignments[i++].side = side;
    }
    /* If no players have been created, make one that is human-run,
       presumably by the person who started up this program. */
    if (numplayers == 0) {
	add_default_player();
    }
    if (numsides < numplayers) {
	/* We have too many players. */
	init_warning("too many players (%d) for %d sides, ignoring extra",
		     numplayers, numsides);
    }
    /* Make any prespecified assignments. */
    for (i = 0; i < numsides; ++i) {
	if (assignments[i].side != NULL
	    && assignments[i].side->player != NULL
	    && assignments[i].player == NULL) {
	    trial_assign_player(i, assignments[i].side->player);
	    /* (should this assignment be locked?) */
	}
    }
    /* Assign any remaining players. */
    player = playerlist;
    for (i = 0; i < numsides; ++i) {
	if (assignments[i].side != NULL
	    && assignments[i].side->ingame
	    && assignments[i].player == NULL) {
	    for (; player != NULL; player = player->next) {
		if (player->side == NULL) {
		    trial_assign_player(i, player);
		    break;
		}
	    }
	}
    }
    /* Add default players for sides with none (in separate loop so player
       creation doesn't confuse player list traversal above).  Don't
       add for any sides not actually participating in the game. */
    for (i = 0; i < numsides; ++i) {
	if (assignments[i].side != NULL
	    && assignments[i].side->ingame
	    && assignments[i].player == NULL) {
	    player = add_player();
	    /* Default players are always AIs (at least for now). */
	    if (!initially_no_ai)
	      player->aitypename = "mplayer";
	    trial_assign_player(i, player);
	}
    }
    /* This warning can happen if there are more players than active
       sides. */
    for_all_players(player) {
	if (player->side == NULL) {
	    init_warning("Player %s not given a side", player_desig(player));
	}
    }
    /* At this point, we have matching sides and players, ready to be
       rearranged if desired. */
}

static void
trial_assign_player(i, player)
int i;
Player *player;
{
    assignments[i].player = player;
    player->side = assignments[i].side;
    (assignments[i].side)->player = player;
    /* Set the player's advantage to be the side's advantage, if defined. */
    if (assignments[i].player != NULL
	&& assignments[i].player->advantage == 0) {
	assignments[i].player->advantage = assignments[i].side->advantage;
    }
    Dprintf("Tentatively assigned %s to %s%s\n",
	    side_desig(assignments[i].side),
	    player_desig(assignments[i].player),
	    (assignments[i].locked ? " (locked)" : ""));
}

/* Create a random side with default characteristics. */

Side *
make_up_a_side()
{
    extern Obj *side_defaults;

    Side *side = create_side();

    if (side == NULL) {
	run_error("could not create a side");
	return NULL;
    }
    fill_in_side(side, side_defaults, FALSE);
    make_up_side_name(side);
    init_side_advantage(side);
    /* A newly-created side starts out in the game, can drop out later. */
    side->ingame = TRUE;
    Dprintf("Made up a side %s\n", side_desig(side));
    return side;
}

/* If undefined, seed a side's advantage and allowable range from the
   global values. */

void
init_side_advantage(side)
Side *side;
{
    /* Set up the default and range of initial advantages. */
    if (side->advantage == 0)
      side->advantage = g_advantage_default();
    if (side->minadvantage == 0)
      side->minadvantage = g_advantage_min();
    if (side->maxadvantage == 0)
      side->maxadvantage = g_advantage_max();
    if (side->maxadvantage == 0) {
	int i, methkey, found = FALSE;
	Obj *synthlist = g_synth_methods(), *methods, *method;

	for_all_list(synthlist, methods) {
	    method = car(methods);
	    if (symbolp(method)) {
		methkey = keyword_code(c_string(method));
		for (i = 0; synthmethods[i].key >= 0; ++i) {
		    if (methkey == synthmethods[i].key) {
			found = TRUE;
			break;
		    }
		}
	    }
	    if (found) {
		if (synthmethods[i].key == K_MAKE_COUNTRIES) {
		    int u, var = FALSE;

		    for_all_unit_types(u) {
			if (u_start_with(u) > 0) {
			    var = TRUE;
			    break;
			}
		    }
		    if (var)
		      side->maxadvantage = 10;
		}
	    }
	}
    }
    /* If we're not getting any guidance from global defaults, just set to 1. */
    if (side->minadvantage == 0)
      side->minadvantage = 1;
    if (side->maxadvantage == 0)
      side->maxadvantage = side->minadvantage;
}

/* Add a side and a player to go with it (used by interfaces). */

int
add_side_and_player()
{
    int n;
    Side *side;
    Player *player;

    side = make_up_a_side();
    if (side == NULL)
      return FALSE;
    n = numsides - 1;
    assignments[n].side = side;
    player = add_player();
    assignments[n].player = player;
    player->side = assignments[n].side;
    (assignments[n].side)->player = player;
    /* Set the player's advantage to be the side's advantage, if not
       already set. */
    if (player->advantage == 0) {
	player->advantage = side->advantage;
    }
    return TRUE;
}

/* This can be used by interfaces to exchange players between one side and
   another. */

int
exchange_players(n, n2)
int n, n2;
{
    int i;
    Player *tmpplayer = assignments[n].player;

    if (n < 0)
      n = 0;
    if (n2 < 0) {
	for (i = n + 1; i <= numsides + n; ++i) {
	    n2 = i % numsides;
	    if (assignments[n2].side && (assignments[n2].side)->ingame)
	      break;
	}
	/* No sides to exchange with, return. */
    	if (i == numsides + n)
	  return -1;
    }
    assignments[n].player = assignments[n2].player;
    assignments[n2].player = tmpplayer;
    /* Doesn't seem like these should be needed, but they are. */
    assignments[n].player->side = assignments[n].side;
    assignments[n].side->player = assignments[n].player;
    assignments[n2].player->side = assignments[n2].side;
    assignments[n2].side->player = assignments[n2].player;
    return n2;
}

int
remove_side_and_player()
{
    /* (how to do this?) */
    /* (would need to renumber sides etc) */
    return FALSE;
}

/* Synthesis methods fill in whatever is not fixed by game modules or by
   the player(s). */

void
run_synth_methods()
{
    int i, methkey, found = FALSE, rslt;
    Obj *synthlist, *methods, *method, *parms;

    /* Make sure no dead units get saved. */
    flush_dead_units();
    /* Make a consistent ordering of units. */
    sort_units(TRUE);
    synthlist = g_synth_methods();
    Dprintf("Will run syntheses ");
    Dprintlisp(synthlist);
    Dprintf("\n");
    for_all_list(synthlist, methods) {
	method = car(methods);
	if (symbolp(method)) {
	    methkey = keyword_code(c_string(method));
	    for (i = 0; synthmethods[i].key >= 0; ++i) {
		if (methkey == synthmethods[i].key) {
		    found = TRUE;
		    rslt = (*synthmethods[i].fn)(synthmethods[i].calls, synthmethods[i].runs);
		    ++(synthmethods[i].calls);
		    if (rslt)
		      ++(synthmethods[i].runs);
		    break;
		}
	    }
	} else if (consp(method)) {
	    parms = cdr(method);
	    if (stringp(car(method))) {
		/* External program. */
		/* (should format parms, compose outputfile redirection,
		    call program with os.c function, check return code,
		    open and read output file) */
		run_error("No external synth programs yet");
	    }
	}
	if (!found) {
	    sprintlisp(spbuf, method, BUFSIZE);
	    init_warning("bad synthesis method %s, ignoring", spbuf);
	}
    }
}

int
get_synth_method_uses(methkey, calls, runs)
int methkey, *calls, *runs;
{
    int i;

    for (i = 0; synthmethods[i].key >= 0; ++i) {
	if (methkey == synthmethods[i].key) {
	    *calls = synthmethods[i].calls;
	    *runs = synthmethods[i].runs;
	    return TRUE;
	}
    }
    return FALSE;
}

int
make_weather(calls, runs)
int calls, runs;
{
    int x, y, winddir, t, var;
    extern int maxclouds;	

    if (maxclouds > 0) {
	if (!clouds_defined()) {
	    allocate_area_clouds();
	    for_all_cells(x, y) {
		t = terrain_at(x, y);
		/* Vary the cloud cover randomly across the range of possibilities. */
		var = t_clouds_max(t) - t_clouds_min(t);
		if (var > 0)
		  var = xrandom(var + 1);
		set_raw_cloud_at(x, y, t_clouds_min(t) + var);
	    }
	}
    }
    if (maxwindforce > 0) {
	if (!winds_defined()) {
	    allocate_area_winds();
	    if (g_wind_mix_range() > 0) {
		/* Make all winds start in the same direction. */
		/* Initial variation will randomize. */
		winddir = random_dir();
		for_all_cells(x, y) {
		    set_wind_at(x, y, winddir,
				t_wind_force_avg(terrain_at(x, y)));
		}
	    } else {
		for_all_cells(x, y) {
		    set_wind_at(x, y, random_dir(),
				t_wind_force_avg(terrain_at(x, y)));
		}
	    }
	}
    }
    return TRUE;
}

/* Set the starting date/time to a random value within a given range. */

int
make_random_date(calls, runs)
int calls, runs;
{
    int n;

    /* Don't bother if the initial date has been set explicitly. */
    if (!empty_string(g_initial_date()))
      return FALSE;
    if (empty_string(g_initial_date_min()))
      return FALSE;
    if (empty_string(g_initial_date_max()))
      return FALSE;
    /* Use the low end of the date range as a starting place for date
       calculations. */
    set_initial_date(g_initial_date_min());
    n = turns_between(g_initial_date_min(), g_initial_date_max());
    /* Now bump the starting date to its random place. */
    set_initial_date(absolute_date_string(xrandom(n) + 1));
    return TRUE;
}

/* The final init cleans up various stuff. */

void
final_init()
{
    Side *side;

    /* Fill in any empty doctrines. */
    for_all_sides_plus_indep(side) {
	init_doctrine(side);
    }
    /* Make sure each side has a self-unit if it needs one. */
    for_all_sides(side) {
	init_self_unit(side);
    }
    /* At this point we should be ready to roll.  Any inconsistencies
       hereafter will be fatal. */
    check_consistency();
    cache_possible_types();
    kill_excess_units();
    /* Count the units initially present. */
    init_side_balance();
    /* Fix up garbled view data. */
    init_all_views();
    configure_sides();
    /* Check again, just to be sure. */
    check_consistency();
    create_game_help_nodes();
    /* Set up the scores to be attached to each side. */
    init_scores();
    final_init_world();
    init_run();
    /* Start the recording of history. */
    start_history();
    /* Compute and cache AI/planning data. */
    ai_init_shared();
#ifdef DEBUGGING
    /* Make sure that any debugging-related allocation is done. */
    if (Debug || DebugG || DebugM)
      prealloc_debug();
    /* Report on memory consumption. */
    Dprintf("One side is %d bytes.\n", sizeof(Side));
    Dprintf("One unit is %d bytes, one plan is %d bytes.\n",
	    sizeof(Unit), sizeof(Plan));
    if (Debug)
      report_malloc();
#endif /* DEBUGGING */
}

static void
kill_excess_units()
{
    Unit *unit;

    for_all_units(unit) {
	/* Offworld units with no scheduled appearance should evaporate. */
	if (!inside_area(unit->x, unit->y)
	    && unit_appear_turn(unit) < 0) {
	    kill_unit(unit, -1);
	}
    }
    /* Make the units' storage available. */
    flush_dead_units();
}

/* Load up any player-specified configuration data. */

void
configure_sides()
{
    Side *side;

    for_all_sides(side) {
	load_side_config(side);
    }
}

/* Calculate what each side knows about the world. */

void
init_all_views()
{
    int x, y, i = 0, todo = max(1, numsides * area.numcells);
    int terrainset;
    Side *side;

    /* Snapshot the value here, so is permanently on. */
    all_see_all = g_see_all();
    /* Set up the basic view structures for all sides first. */
    for_all_sides(side) {
	if (all_see_all) {
	    side->see_all = TRUE;
	} else {
	    side->see_all = FALSE;
	    terrainset = init_view(side);
	    init_view_2(side, terrainset);
	}
	/* We're not normally allowed to choose whether to see all or not. */
	side->may_set_see_all = FALSE;
	/* Can't think of any other place to put this... */
	calc_start_xy(side);
    }
    /* Nothing more to do here. */
    if (all_see_all)
      return;
    announce_lengthy_process("Computing current view at each location");
    /* (coverage is also done at this point) */
    for_all_sides(side) {
	for_all_cells(x, y) {
	    ++i;
    	    if (i % 100 == 0)
    	      announce_progress((100 * i) / todo);
	    see_cell(side, x, y);
	}
    }
    finish_lengthy_process();
    init_ranged_views();
}

static void
init_view_2(side, terrainset)
Side *side;
int terrainset;
{
    int x, y;

    calc_coverage(side);
    tmpside = side;
    if (g_terrain_seen()) {
	for_all_cells(x, y) {
	    init_view_cell(x, y);
	}
    } else {
	for_all_cells(x, y) {
	    if (!terrainset)
	      set_terrain_view(side, x, y, UNSEEN);
	    set_unit_view(side, x, y, EMPTY);
	    /* View date is 0, which is what we want. */
	}
    }
}

/* This is a helper for the following routine.  The helper is applied to
   each cell, decides what is visible in that cell. */

/* This only works from already_seen, does not account for coverage. */

/* (should only be run once/cell/side, use a scratch layer to keep track?) */

static void
init_view_cell(x, y)
int x, y;
{
    int u, chance;
    Unit *unit;

    /* Guaranteed to see the terrain accurately. */
    set_terrain_view(tmpside, x, y, buildtview(terrain_at(x, y)));
    /* If this cell is under observation, don't need to do anything special. */
    if (cover(tmpside, x, y) > 0)
      return;
    /* Scan all the units here to see if any are visible. */
    for_all_stack(x, y, unit) {
    	if (in_play(unit)) {
    	    u = unit->type;
	    if (u_see_always(u)) {
	    	see_exact(tmpside, x, y);
	    	/* or flag unit as spotted? */
		set_cover(tmpside, x, y, 1);
	    	return;
	    }
	    chance = already_seen_chance(tmpside, unit);
	    if (probability(chance)) {
	    	see_exact(tmpside, x, y);
	    	/* This view might be overwritten by a view of
	    	   another might-be-seen unit at this location,
	    	   so don't return just yet. */
	    }
	}
    }
    /* We get to see the weather conditions here. */
    set_wind_view(tmpside, x, y, raw_wind_at(x, y));
}

int
already_seen_chance(side, unit)
Side *side;
Unit *unit;
{
    int u = unit->type, val;

    if (indep(unit)) {
	if (side != NULL
	    && side->already_seen_indep != NULL) {
	    val = side->already_seen_indep[u];
	    if (val >= 0)
	      return val;
	}
	return u_already_seen_indep(u);
    } else {
	if (side != NULL
	    && side->already_seen != NULL) {
	    val = side->already_seen[u];
	    if (val >= 0)
	      return val;
	}
	return u_already_seen(u);
    }
}

static void
maybe_init_view_cell(x, y)
int x, y;
{
    int dir, x1, y1;

    if (adj_seen_terrain(x, y, tmpside) && flip_coin()) {
	init_view_cell(x, y);
	for_all_directions(dir) {
	    if (point_in_dir(x, y, dir, &x1, &y1)) {
		init_view_cell(x1, y1);
	    }
	}
    }
}

static int
adj_seen_terrain(x, y, side)
int x, y;
Side *side;
{
    int dir, x1, y1;

    if (!inside_area(x, y) || side == NULL)
      return FALSE;
    for_all_directions(dir) {
	if (point_in_dir(x, y, dir, &x1, &y1)) {
	    if (terrain_view(side, x1, y1) != UNSEEN)
	      return TRUE;
	}
    }
    return FALSE;
}

/* Do ranged initial views from units. */

static void
init_ranged_views()
{
    int rad, x, y, pop, dir, x1, y1, i = 0;
    Unit *unit;
    Side *side, *side2;

    /* Don't run if nothing exists to look at. */
    if (!terrain_defined())
      return;
    /* Skip if everything already known, side creation got these cases. */
    if (g_see_all() || g_terrain_seen())
      return;
    announce_lengthy_process("Computing ranged and people views");
    /* Compute the view for each side. */ 
    for_all_sides(side) {
	/* Set this so the helper fn has a side to use. */
	tmpside = side;
	/* View from our own and other units. */
	for_all_units(unit) {
	    if (trusted_side(unit->side, side)) {
		/* The unit always sees itself. */
		see_exact(side, unit->x, unit->y);
		/* It may also see things nearby. */
		rad = u_seen_radius(unit->type);
		if (rad >= area.maxdim) {
		    /* Special optimization - view the whole area. */
		    for_all_cells(x, y) {
			init_view_cell(x, y);
		    }
		    /* Note that we're not done; other units may be able to
		       supply more exact views of their vicinities than
		       would init_view_cell from a distant unit. */
		} else if (rad >= 0) {
		    apply_to_area(unit->x, unit->y, rad, init_view_cell);
		}
	    }
	}
	/* The people see everything in the cells that they are in, plus the
	   normally visible things in adjacent cells. */
	if (people_sides_defined()) {
	    for_all_interior_cells(x, y) {
		pop = people_side_at(x, y);
		side2 = side_n(pop);
		if (pop != NOBODY && trusted_side(side2, side)) {
		    see_exact(side, x, y);
		    for_all_directions(dir) {
			if (point_in_dir(x, y, dir, &x1, &y1)) {
			    init_view_cell(x1, y1);
			}
		    }
		}
	    }
	}
	if (side->finalradius > 0) {
	    /* (should also view terrain adj to each of these cells, since the
	       viewing represents exploration) */
	    apply_to_ring(side->startx, side->starty,
			  1, side->finalradius - 1,
			  init_view_cell);
	    apply_to_ring(side->startx, side->starty,
			  side->finalradius - 2, side->finalradius + 2,
			  maybe_init_view_cell);
	}
	announce_progress((100 * i++) / numsides);
    }
    finish_lengthy_process();
}

/* Method to give all units and terrain a basic stockpile of supply. */

int
make_initial_materials(calls, runs)
int calls, runs;
{
    int m, t, amts[MAXTTYPES], doany, x, y;

    /* Go over each material and terrain type, looking for nonzero
       material in terrain possibilities, then alloc and fill in layers
       as needed. */
    for_all_material_types(m) {
	doany = FALSE;
	for_all_terrain_types(t) {
	    amts[t] = min(tm_storage_x(t, m), tm_initial(t, m));
	    if (amts[t] > 0)
	      doany = TRUE;
	}
	if (doany) {
	    allocate_area_material(m);
	    for_all_cells(x, y) {
		t = terrain_at(x, y);
		set_material_at(x, y, m, amts[t]);
	    }
	}
    }
    return TRUE;
}

/* Give the unit what it is declared to have stockpiled
   at the start of a game. */

void
init_supply(unit)
Unit *unit;
{
    int m, u = unit->type;

    for_all_material_types(m) {
	unit->supply[m] = min(um_storage_x(u, m), um_initial(u, m));
    }
}

/* Quicky test needed in a couple places. */

int
saved_game()
{
    FILE *fp;

    fp = fopen(saved_game_filename(), "r");
    if (fp != NULL) {
	fclose(fp);
	return TRUE;
    } else {
	return FALSE;
    }
}

/* Count all the initial units in each side's balance sheet, but only
   if all the balance sheets are completely blank. */

void
init_side_balance()
{
    int u, anything = FALSE;
    Unit *unit;
    Side *side;

    for_all_sides(side) {
	for_all_unit_types(u) {
	    if (total_gain(side, u) > 0)
	      return;
	}
    }
    for_all_units(unit) {
	count_gain(unit->side, unit->type, initial_gain);
    }
}

/* This routine does a set of checks to make sure that Xconq's data
   is in a valid state.  This is particularly important after init,
   since the combination of files and synthesis methods may have
   caused some sort of disaster. */

void
check_consistency()
{
    int x, y;

    /* If no terrain, make a flat area of all ttype 0. */
    if (!terrain_defined()) {
	init_warning("No terrain defined, substituting type 0");
	allocate_area_terrain();
	for_all_cells(x, y) {
	    if (inside_area(x, y)) {
		set_terrain_at(x, y, 0);
	    }
	}
	add_edge_terrain();
    }
    if (numsides <= 0) {
	init_error("There are no player sides at all in this game");
    } else if (numsides < numplayers) {
	init_warning("Only made %d of the %d sides requested",
		     numsides, numplayers);
    }
    /* Need any kind of unit checks? */
}

static void
cache_possible_types()
{
    int u, u2, rescan;
    Unit *unit;

    u_possible = (short *) xmalloc(numutypes * sizeof(short));
    for_all_units(unit) {
	u_possible[unit->type] = TRUE;
    }
    rescan = TRUE;
    while (rescan) {
	rescan = FALSE;
	for_all_unit_types(u) {
	    if (u_possible[u]) {
		for_all_unit_types(u2) {
		    if (uu_acp_to_create(u, u2) > 0
			&& !u_possible[u2]) {
			u_possible[u2] = TRUE;
			rescan = TRUE;
		    }
		}
	    }
	}
    }
}

/* This does the actual assignment of players to sides, and initializes the
   side structures appropriately. */

void
assign_players_to_sides()
{
    int i, numdisplays = 0, numdisplayswanted = 0, numais = 0, n = 0;
    Side *side;
    Player *player;

    announce_lengthy_process("Assigning players to sides");
    for (i = 0; i < numsides; ++i) {
	announce_progress((100 * i) / numsides);
	side = assignments[i].side;
	player = assignments[i].player;
	if (player == NULL)
	  continue;
	canonicalize_player(player);
	/* Fix any mistaken advantages. */
	/* This is a warning here because properly-done interfaces shouldn't
	   allow any mistaken advantages to get this far. */
	if (player->advantage < side->minadvantage) {
	    init_warning("Requested advantage of %d for %s is too low, will be set to %d",
			 player->advantage, player_desig(player), side->minadvantage);
	    player->advantage = side->minadvantage;
	}
	if (player->advantage > side->maxadvantage) {
	    init_warning("Requested advantage of %d for %s is too high, will be set to %d",
			 player->advantage, player_desig(player), side->maxadvantage);
	    player->advantage = side->maxadvantage;
	}
	side->rid = player->rid;
	/* Call the interface code to initialize the side's display, if
	   it wants to use one (the interface has to decide). */
	if (side_wants_display(side)) {
	    ++numdisplayswanted;
	    if (side->rid == my_rid) {
		init_ui(side);
	    } else {
		init_remote_ui(side);
	    }
	}
	if (side_has_display(side)) {
	    ++numdisplays;
	}
	/* Count the desired AIs, for setup below. */
	if (side_wants_ai(side)) {
	    ++numais;
	}
	Dprintf("Assigned %s to %s\n",
		 side_desig(side), player_desig(player));
    }
    finish_lengthy_process();
    if (numdisplays < numdisplayswanted) {
	if (numdisplays < 1) {
	    init_warning("None of the %d requested displays opened",
			 numdisplayswanted);
	} else {
	    init_warning("Only %d of %d requested displays opened",
			 numdisplays, numdisplayswanted);
	}
    } else if (numdisplays == 0) {
	init_warning("Need at least one display to run");
    }
#ifdef DESIGNERS
    /* Make each displayed side into a designer if it was requested. */
    if (allbedesigners) {
    	for_all_sides(side) {
	    if (side_has_display(side)) {
		become_designer(side);
	    }
    	}
    }
#endif /* DESIGNERS */
    if (numais > 0) {
	announce_lengthy_process("Setting up AIs");
	for (i = 0; i < numsides; ++i) {
	    if (numais > 1)
	      announce_progress((100 * n++) / numais);
	    side = assignments[i].side;
	    if (side_wants_ai(side)) {
		init_ai(side);
	    }
	}
	finish_lengthy_process();
    }
}

/* Make sure any debugging I/O routines have allocated their space.
   Usually routines like side_desig allocate their working space on
   demand, but if the first demand occurs during a game being saved
   because allocation fails, then we're in big trouble.  So this
   routine, which need only be be called when debugging is turned
   on, calls allocation-needing things in such a way to cause them
   to allocate their working space. */

void
prealloc_debug()
{
    side_desig(NULL);
    player_desig(NULL);
    unit_desig(NULL);
}

/* Return the program version. */

char *
version_string()
{
    return VERSION;
}

/* Return the copyright notice. */

char *
copyright_string()
{
    return COPYRIGHT;
}

/* Return the license string. */

char *
license_string()
{
    return "\
Xconq is free software and you are welcome to distribute copies of it\n\
under certain conditions; type \"o copying\" to see the conditions.\n\
There is absolutely no warranty for Xconq; type \"o warranty\" for details.\n\
";
}

/* This comment is a fake reference to K_NO_X, which is a keyword used
   to clear the subtype-x property of terrain, but is not actually
   mentioned in the code - besides here anyway. :-) */