/
init.d
126 lines (104 loc) · 2.38 KB
/
init.d
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
/*
* Empire, the Wargame of the Century (tm)
* Copyright (C) 1978-2004 by Walter Bright
* All Rights Reserved
*
* You may use this source for personal use only. To use it commercially
* or to distribute source or binaries of Empire, please contact
* www.digitalmars.com.
*
* Written by Walter Bright.
* This source is written in the D Programming Language.
* See www.digitalmars.com/d/ for the D specification and compiler.
*
* Use entirely at your own risk. There is no warranty, expressed or implied.
*/
import std.string;
import empire;
import mapdata;
import var;
/*****************************
* Initialize city variables.
*/
void citini()
{ int loc,i,j,k;
for (i = CITMAX; i--;)
{ memset(&city[i],0,City.sizeof);
city[i].loc = city[i].own = 0;
city[i].phs = -1; // no phase
}
for (i = 0, loc = MAPSIZE; loc--;)
if (typ[map[loc]] == X)
city[i++].loc = loc;
//printf("%d cities\n",i);
assert(i <= CITMAX);
/* shuffle cities around
*/
for (i = CITMAX / 2; i--;)
{ j = empire.random(CITMAX);
k = empire.random(CITMAX);
loc = city[j].loc;
city[j].loc = city[k].loc;
city[k].loc = loc; // swap city locs
}
}
/*****************************
* Select a map.
* Returns:
* 0 success
* !=0 failure
*/
int selmap()
{
// Use internal maps
int j;
ubyte *d;
int i,a,c,n;
j = empire.random(5);
d = cast(ubyte *)(*mapdata.mapdata[j]);
i = MAPSIZE - 1;
while ((c = *d) != 0) // 0 marks end of data
{ n = (c >> 2) & 63; // count of map values - 1
a = c & 3; // bottom 2 bits
if (a == 0 || // a must be 1,2,3
c == -1 || // error reading file
i - n < 0) // too much data
{
assert(0);
}
while (n-- >= 0)
map[i--] = a;
d++;
}
if (ranq() & 4) flip();
if (ranq() & 4) klip(); // random map rotations
return 0;
}
/***********************
* Flip map corner to corner.
*/
void flip()
{ int i,j,c;
i = j = MAPSIZE / 2;
while (i--)
{ c = map[j];
map[j++] = map[i];
map[i] = c;
}
}
/************************
* Flip map end to end.
*/
void klip()
{ int row,i,j,c;
row = 0;
while (row < MAPSIZE)
{ i = j = (Mcolmx + 1) / 2;
while (i--)
{ c = map[row + j];
map[row + j++] = map[row + i];
map[row + i] = c;
}
row += Mcolmx + 1;
}
}