-
Notifications
You must be signed in to change notification settings - Fork 74
/
fleet.c
310 lines (270 loc) · 5.8 KB
/
fleet.c
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
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "mtwist.h"
#include "quat.h"
#include "fleet.h"
#define FLEET_SPACING 200.0f
#define MAXSHIPSPERFLEET 10
#define MAXFLEETS 10
struct fleet {
int nships;
int fleet_shape;
int32_t id[MAXSHIPSPERFLEET];
};
static struct fleet f[MAXFLEETS];
static int nfleets;
static union vec3 zero = { { 0, 0, 0 } };
static union vec3 fleet_line_position(int position)
{
union vec3 v = { { -1.0f, 0.0f, 0.0f } };
if (position == 0)
return zero;
vec3_mul_self(&v, (float) position * FLEET_SPACING);
return v;
}
static union vec3 fleet_triangle_position(int position)
{
int i, row, rowcount, rowpos;
static int r[256], rp[256], rc[256], R, RP, RC;
static int initialized = 0;
int limit;
float x, z;
union vec3 v;
/* FIXME: there is probably a closed form analytical solution to this */
/* FIXME: make rows alternate directions so that filling in holes works nicely */
if (position == 0)
return zero;
row = 0;
rowpos = 0;
rowcount = 1;
rc[0] = 1;
limit = position > 256 ? position : 256;
if (!initialized || position > 256) {
for (i = 0; i < limit; i++) {
if (!initialized && i < 256) {
r[i] = row;
rp[i] = rowpos;
rc[i] = rowcount;
}
if (rowpos >= rowcount - 1) {
row++;
rowcount++;
rowpos = 0;
} else {
rowpos++;
}
R = row;
RP = rowpos;
RC = rowcount;
}
initialized = 1;
}
if (position >= 256) {
row = R;
rowpos = RP;
rowcount = RC;
} else {
row = r[position];
rowpos = rp[position];
rowcount = rc[position];
}
x = FLEET_SPACING * -row;
//zzz z = (((float) -rowcount / 2.0f) + (float) rowcount * (float) rowpos) * FLEET_SPACING;
z = ((float) -((rowcount - 1) / 2.0f) + (float) rowpos) * FLEET_SPACING;
v.v.x = x;
v.v.y = 0.0f;
v.v.z = z;
return v;
}
static union vec3 fleet_square_position(int position)
{
union vec3 v = { { 0.0f, 0.0f, 0.0f } };
int i, r, c, cdir, rdir, squaresize;
static int row[MAXSHIPSPERFLEET];
static int column[MAXSHIPSPERFLEET];
static int initialized = 0;
/* 0 1 8 9 */
/* 3 2 7 10 */
/* 4 5 6 11 */
/* 15 14 13 12 */
/* etc. */
if (position == 0)
return zero;
r = 0;
c = -1;
squaresize = 1;
cdir = 1;
rdir = 0;
if (!initialized) {
for (i = 0; i < MAXSHIPSPERFLEET; i++) {
c += cdir;
r += rdir;
row[i] = r;
column[i] = c;
if (cdir == 1 && c == squaresize) {
cdir = 0;
if (r == 0)
rdir = 1;
else
rdir = -1;
} else if (cdir == -1 && c == 0) {
cdir = 0;
rdir = 1;
squaresize++;
} else if (rdir == -1 && r == 0) {
cdir = 1;
rdir = 0;
squaresize++;
} else if (rdir == 1 && r == squaresize) {
rdir = 0;
if (c == 0)
cdir = 1;
else if (c == squaresize)
cdir = -1;
}
}
initialized = 1;
}
v.v.x = (float) row[position] * FLEET_SPACING;
v.v.y = 0.0f;
v.v.z = (float) column[position] * FLEET_SPACING;
return v;
}
union vec3 fleet_position(int fleet_number, int position, union quat *fleet_orientation)
{
union vec3 v;
struct fleet *fleet = &f[fleet_number];
switch (fleet->fleet_shape) {
case FLEET_LINE:
v = fleet_line_position(position);
break;
case FLEET_TRIANGLE:
v = fleet_triangle_position(position);
break;
case FLEET_SQUARE:
v = fleet_square_position(position);
break;
default:
v = fleet_triangle_position(position);
break;
}
quat_rot_vec_self(&v, fleet_orientation);
return v;
}
int fleet_new(int fleet_shape, int32_t leader)
{
int i;
for (i = 0; i < nfleets; i++) {
if (f[i].nships == 0) {
f[i].nships = 1;
f[i].id[0] = leader;
f[i].fleet_shape = fleet_shape;
return i;
}
}
if (nfleets >= MAXFLEETS)
return -1;
i = nfleets;
nfleets++;
f[i].nships = 1;
f[i].id[0] = leader;
f[i].fleet_shape = fleet_shape;
return i;
}
int fleet_count(void)
{
return nfleets;
}
int max_fleets(void)
{
return MAXFLEETS;
}
int fleet_members(int fleet_number)
{
return f[fleet_number].nships;
}
uint32_t fleet_member_get_id(int fleet_number, int position)
{
assert(fleet_number >= 0 && fleet_number < nfleets);
if (position < 0 || position >= f[fleet_number].nships)
return (uint32_t) -1;
return f[fleet_number].id[position];
}
void fleet_leave(int32_t id)
{
int i, j;
for (i = 0; i < nfleets; i++) {
for (j = 0; j < f[i].nships;) {
if (f[i].id[j] == id) {
if (f[i].nships == 1) {
f[i].nships = 0;
return;
}
memmove(&f[i].id[j], &f[i].id[j + 1],
sizeof(f[i].id[j]) * (f[i].nships - 1 - j));
f[i].nships--;
return;
} else {
j++;
}
}
}
}
int fleet_join(int fleet_number, int32_t id)
{
int i, j;
if (f[fleet_number].nships >= MAXSHIPSPERFLEET)
return -1;
for (i = 0; i < nfleets; i++) {
for (j = 0; j < f[i].nships;) {
if (f[i].id[j] == id) {
if (i == fleet_number) {
return j;
} else {
memmove(&f[i].id[j], &f[i].id[j + 1],
sizeof(f[i].id[j]) * (f[i].nships - 1 - j));
f[i].nships--;
}
} else {
j++;
}
}
}
f[fleet_number].id[f[fleet_number].nships] = id;
f[fleet_number].nships++;
return f[fleet_number].nships - 1;
}
int fleet_position_number(int fleet_number, int32_t id)
{
int i;
for (i = 0; i < f[fleet_number].nships; i++)
if (f[fleet_number].id[i] == id)
return i;
return -1;
}
int32_t fleet_get_leader_id(int fleet_number)
{
return f[fleet_number].id[0];
}
#ifdef TESTFLEET
int main(int argc, char *argv[])
{
int n, n2, i;
union vec3 v;
union quat q = identity_quat;
n = fleet_new(FLEET_TRIANGLE, 1);
for (i = 0; i < 10; i++) {
v = fleet_position(n, i, &q);
printf("%d, %f,%f,%f\n", i, v.v.x, v.v.y, v.v.z);
}
n2 = fleet_new(FLEET_SQUARE, 1);
for (i = 0; i < 10; i++) {
v = fleet_position(n2, i, &q);
printf("%d, %f,%f,%f\n", i, v.v.x, v.v.y, v.v.z);
}
return 0;
}
#endif