initialize people to 0
[unix-history] / usr / src / games / sail / dr_1.c
CommitLineData
b5f0675e 1/*
1e008c14
DF
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
b5f0675e
EW
5 */
6
41506c08 7#ifndef lint
1e008c14
DF
8static char sccsid[] = "@(#)dr_1.c 5.1 (Berkeley) %G%";
9#endif not lint
7fc06086 10
b3a57661 11#include "driver.h"
41506c08
CL
12
13unfoul()
14{
b3a57661
EW
15 register struct ship *sp;
16 struct ship *to;
17 register int nat;
6ca45914 18 register i;
41506c08 19
b3a57661
EW
20 foreachship(sp) {
21 if (sp->file->captain[0])
22 continue;
23 nat = capship(sp)->nationality;
6ca45914 24 foreachship(to) {
ba28d9ed
EW
25 if (nat != capship(to)->nationality &&
26 !toughmelee(sp, to, 0, 0))
b3a57661 27 continue;
6ca45914
EW
28 for (i = fouled2(sp, to); --i >= 0;)
29 if (die() <= 2)
30 cleanfoul(sp, to, 0);
41506c08
CL
31 }
32 }
33}
34
41506c08
CL
35boardcomp()
36{
7fc06086 37 int crew[3];
b3a57661 38 register struct ship *sp, *sq;
41506c08 39
b3a57661
EW
40 foreachship(sp) {
41 if (*sp->file->captain)
42 continue;
b3a57661
EW
43 if (sp->file->dir == 0)
44 continue;
45 if (sp->file->struck || sp->file->captured != 0)
46 continue;
6ca45914
EW
47 if (!snagged(sp))
48 continue;
b3a57661
EW
49 crew[0] = sp->specs->crew1 != 0;
50 crew[1] = sp->specs->crew2 != 0;
51 crew[2] = sp->specs->crew3 != 0;
52 foreachship(sq) {
53 if (!Xsnagged2(sp, sq))
54 continue;
55 if (meleeing(sp, sq))
56 continue;
57 if (!sq->file->dir
58 || sp->nationality == capship(sq)->nationality)
59 continue;
60 switch (sp->specs->class - sq->specs->class) {
61 case -3: case -4: case -5:
62 if (crew[0]) {
63 /* OBP */
64 sendbp(sp, sq, crew[0]*100, 0);
65 crew[0] = 0;
66 } else if (crew[1]){
67 /* OBP */
68 sendbp(sp, sq, crew[1]*10, 0);
69 crew[1] = 0;
70 }
71 break;
72 case -2:
73 if (crew[0] || crew[1]) {
74 /* OBP */
75 sendbp(sp, sq, crew[0]*100+crew[1]*10,
76 0);
77 crew[0] = crew[1] = 0;
78 }
79 break;
80 case -1: case 0: case 1:
81 if (crew[0]) {
82 /* OBP */
83 sendbp(sp, sq, crew[0]*100+crew[1]*10,
84 0);
85 crew[0] = crew[1] = 0;
41506c08 86 }
b3a57661
EW
87 break;
88 case 2: case 3: case 4: case 5:
89 /* OBP */
90 sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2],
91 0);
92 crew[0] = crew[1] = crew[2] = 0;
93 break;
41506c08 94 }
b3a57661 95 }
41506c08
CL
96 }
97}
98
41506c08 99fightitout(from, to, key)
b3a57661
EW
100struct ship *from, *to;
101int key;
41506c08 102{
b3a57661
EW
103 struct ship *fromcap, *tocap;
104 int crewfrom[3], crewto[3], menfrom, mento;
41506c08 105 int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured;
b3a57661
EW
106 int topoints;
107 int index, totalfrom = 0, totalto = 0;
108 int count;
41506c08
CL
109 char message[60];
110
b3a57661
EW
111 menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key);
112 mento = mensent(to, from, crewto, &tocap, &pcto, 0);
113 if (fromcap == 0)
41506c08 114 fromcap = from;
b3a57661 115 if (tocap == 0)
41506c08 116 tocap = to;
6ca45914 117 if (key) {
22cfa172
EW
118 if (!menfrom) { /* if crew surprised */
119 if (fromcap == from)
120 menfrom = from->specs->crew1
121 + from->specs->crew2
122 + from->specs->crew3;
123 else
124 menfrom = from->file->pcrew;
125 } else {
126 menfrom *= 2; /* DBP's fight at an advantage */
127 }
128 }
b3a57661
EW
129 fromstrength = menfrom * fromcap->specs->qual;
130 strengthto = mento * tocap->specs->qual;
b3a57661
EW
131 for (count = 0;
132 (fromstrength < strengthto * 3 && strengthto < fromstrength * 3
133 || fromstrength == -1) && count < 4;
134 count++) {
41506c08
CL
135 index = fromstrength/10;
136 if (index > 8)
137 index = 8;
138 toinjured = MT[index][2 - die() / 3];
139 totalto += toinjured;
140 index = strengthto/10;
141 if (index > 8)
142 index = 8;
143 frominjured = MT[index][2 - die() / 3];
144 totalfrom += frominjured;
145 menfrom -= frominjured;
146 mento -= toinjured;
b3a57661
EW
147 fromstrength = menfrom * fromcap->specs->qual;
148 strengthto = mento * tocap->specs->qual;
41506c08 149 }
b3a57661 150 if (fromstrength >= strengthto * 3 || count == 4) {
41506c08
CL
151 unboard(to, from, 0);
152 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
153 subtract(to, totalto, crewto, tocap, pcto);
b3a57661
EW
154 makesignal(from, "boarders from %s repelled", to);
155 (void) sprintf(message, "killed in melee: %d. %s: %d",
156 totalto, from->shipname, totalfrom);
157 Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0);
41506c08 158 if (key)
b3a57661
EW
159 return 1;
160 } else if (strengthto >= fromstrength * 3) {
41506c08
CL
161 unboard(from, to, 0);
162 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
163 subtract(to, totalto, crewto, tocap, pcto);
b3a57661 164 if (key) {
41506c08 165 if (fromcap != from)
b3a57661
EW
166 Write(W_POINTS, fromcap, 0,
167 fromcap->file->points -
168 from->file->struck
169 ? from->specs->pts
170 : 2 * from->specs->pts,
171 0, 0, 0);
41506c08
CL
172
173/* ptr1 points to the shipspec for the ship that was just unboarded.
174 I guess that what is going on here is that the pointer is multiplied
175 or something. */
176
6ca45914 177 Write(W_CAPTURED, from, 0, to->file->index, 0, 0, 0);
b3a57661
EW
178 topoints = 2 * from->specs->pts + to->file->points;
179 if (from->file->struck)
180 topoints -= from->specs->pts;
181 Write(W_POINTS, to, 0, topoints, 0, 0, 0);
41506c08 182 mento = crewto[0] ? crewto[0] : crewto[1];
b3a57661 183 if (mento) {
41506c08 184 subtract(to, mento, crewto, tocap, pcto);
b3a57661 185 subtract(from, - mento, crewfrom, to, 0);
41506c08 186 }
b3a57661
EW
187 (void) sprintf(message, "captured by the %s!",
188 to->shipname);
189 Write(W_SIGNAL, from, 1, (int) message, 0, 0, 0);
190 (void) sprintf(message, "killed in melee: %d. %s: %d",
191 totalto, from->shipname, totalfrom);
192 Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0);
41506c08 193 mento = 0;
b3a57661 194 return 0;
41506c08
CL
195 }
196 }
b3a57661
EW
197 return 0;
198}
41506c08
CL
199
200resolve()
201{
b3a57661
EW
202 int thwart;
203 register struct ship *sp, *sq;
41506c08 204
b3a57661 205 foreachship(sp) {
b3a57661
EW
206 if (sp->file->dir == 0)
207 continue;
22cfa172 208 for (sq = sp + 1; sq < ls; sq++)
b3a57661
EW
209 if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
210 (void) fightitout(sp, sq, 0);
265d8c6a 211 thwart = 2;
b3a57661
EW
212 foreachship(sq) {
213 if (sq->file->dir && meleeing(sq, sp))
214 thwart = fightitout(sp, sq, 1);
215 if (!thwart)
216 break;
217 }
265d8c6a
EW
218 if (!thwart) {
219 foreachship(sq) {
220 if (sq->file->dir && meleeing(sq, sp))
221 unboard(sq, sp, 0);
222 unboard(sp, sq, 0);
223 }
224 unboard(sp, sp, 1);
225 } else if (thwart == 2)
b3a57661 226 unboard(sp, sp, 1);
41506c08
CL
227 }
228}
229
41506c08
CL
230compcombat()
231{
b3a57661
EW
232 register n;
233 register struct ship *sp;
234 struct ship *closest;
41506c08 235 int crew[3], men = 0, target, temp;
3ee7f12c
EW
236 int r, guns, ready, load, car;
237 int index, rakehim, sternrake;
b3a57661 238 int shootat, hit;
41506c08 239
b3a57661
EW
240 foreachship(sp) {
241 if (sp->file->captain[0] || sp->file->dir == 0)
242 continue;
243 crew[0] = sp->specs->crew1;
244 crew[1] = sp->specs->crew2;
245 crew[2] = sp->specs->crew3;
246 for (n = 0; n < 3; n++) {
247 if (sp->file->OBP[n].turnsent)
248 men += sp->file->OBP[n].mensent;
249 }
250 for (n = 0; n < 3; n++) {
251 if (sp->file->DBP[n].turnsent)
252 men += sp->file->DBP[n].mensent;
253 }
254 if (men){
255 crew[0] = men/100 ? 0 : crew[0] != 0;
256 crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
257 crew[2] = men%10 ? 0 : crew[2] != 0;
258 }
259 for (r = 0; r < 2; r++) {
260 if (!crew[2])
261 continue;
262 if (sp->file->struck)
263 continue;
264 if (r) {
265 ready = sp->file->readyR;
266 guns = sp->specs->gunR;
267 car = sp->specs->carR;
268 } else {
269 ready = sp->file->readyL;
270 guns = sp->specs->gunL;
271 car = sp->specs->carL;
41506c08 272 }
b3a57661
EW
273 if (!guns && !car)
274 continue;
275 if ((ready & R_LOADED) == 0)
276 continue;
277 closest = closestenemy(sp, r ? 'r' : 'l', 0);
278 if (closest == 0)
279 continue;
280 if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1)))
281 continue;
282 if (closest->file->struck)
283 continue;
284 target = range(sp, closest);
285 if (target > 10)
286 continue;
287 if (!guns && target >= 3)
288 continue;
289 load = L_ROUND;
290 if (target == 1 && sp->file->loadwith == L_GRAPE)
291 load = L_GRAPE;
292 if (target <= 3 && closest->file->FS)
293 load = L_CHAIN;
294 if (target == 1 && load != L_GRAPE)
295 load = L_DOUBLE;
296 if (load > L_CHAIN && target < 6)
297 shootat = HULL;
298 else
299 shootat = RIGGING;
300 rakehim = gunsbear(sp, closest)
301 && !gunsbear(closest, sp);
302 temp = portside(closest, sp, 1)
303 - closest->file->dir + 1;
304 if (temp < 1)
305 temp += 8;
306 if (temp > 8)
307 temp -= 8;
308 sternrake = temp > 4 && temp < 6;
309 index = guns;
310 if (target < 3)
311 index += car;
312 index = (index - 1) / 3;
313 index = index > 8 ? 8 : index;
314 if (!rakehim)
315 hit = HDT[index][target-1];
316 else
317 hit = HDTrake[index][target-1];
318 if (rakehim && sternrake)
319 hit++;
320 hit += QUAL[index][capship(sp)->specs->qual - 1];
321 for (n = 0; n < 3 && sp->file->captured == 0; n++)
322 if (!crew[n])
323 if (index <= 5)
324 hit--;
325 else
326 hit -= 2;
3ee7f12c
EW
327 if (ready & R_INITIAL) {
328 if (!r)
329 sp->file->readyL &= ~R_INITIAL;
330 else
331 sp->file->readyR &= ~R_INITIAL;
b3a57661
EW
332 if (index <= 3)
333 hit++;
41506c08 334 else
b3a57661 335 hit += 2;
3ee7f12c 336 }
b3a57661
EW
337 if (sp->file->captured != 0)
338 if (index <= 1)
339 hit--;
340 else
341 hit -= 2;
342 hit += AMMO[index][load - 1];
343 temp = sp->specs->class;
344 if ((temp >= 5 || temp == 1) && windspeed == 5)
345 hit--;
346 if (windspeed == 6 && temp == 4)
347 hit -= 2;
348 if (windspeed == 6 && temp <= 3)
349 hit--;
350 if (hit >= 0) {
351 if (load != L_GRAPE)
352 hit = hit > 10 ? 10 : hit;
353 table(shootat, load, hit, closest, sp, die());
41506c08
CL
354 }
355 }
356 }
357}
358
359next()
360{
b3a57661 361 if (++turn % 55 == 0)
6c17b19a
EW
362 if (alive)
363 alive = 0;
41506c08 364 else
6c17b19a
EW
365 people = 0;
366 if (people <= 0 || windspeed == 7) {
9329b33f
EW
367 register struct ship *s;
368 struct ship *bestship;
369 float net, best = 0.0;
370 foreachship(s) {
371 if (*s->file->captain)
372 continue;
373 net = (float)s->file->points / s->specs->pts;
374 if (net > best) {
375 best = net;
376 bestship = s;
377 }
378 }
379 if (best > 0.0) {
380 char *p = getenv("WOTD");
381 if (p == 0)
382 p = "Driver";
1f7f69b9
EW
383 if (islower(*p))
384 *p = toupper(*p);
7f1cb789 385 (void) strncpy(bestship->file->captain, p,
aaa1c87f
EW
386 sizeof bestship->file->captain);
387 bestship->file->captain
388 [sizeof bestship->file->captain - 1] = 0;
9329b33f
EW
389 log(bestship);
390 }
a3a7a20d 391 return -1;
41506c08 392 }
b3a57661 393 Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0);
a3a7a20d
EW
394 if (turn % 7 == 0 && (die() >= cc->windchange || !windspeed)) {
395 switch (die()) {
396 case 1:
397 winddir = 1;
398 break;
399 case 2:
400 break;
401 case 3:
402 winddir++;
403 break;
404 case 4:
405 winddir--;
406 break;
407 case 5:
408 winddir += 2;
409 break;
410 case 6:
411 winddir -= 2;
412 break;
413 }
414 if (winddir > 8)
415 winddir -= 8;
416 if (winddir < 1)
417 winddir += 8;
418 if (windspeed)
b3a57661
EW
419 switch (die()) {
420 case 1:
b3a57661 421 case 2:
a3a7a20d 422 windspeed--;
b3a57661
EW
423 break;
424 case 5:
b3a57661 425 case 6:
a3a7a20d 426 windspeed++;
b3a57661 427 break;
41506c08 428 }
a3a7a20d
EW
429 else
430 windspeed++;
431 Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0);
41506c08 432 }
a3a7a20d 433 return 0;
41506c08 434}