]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/dr_1.c
Warns fixes:
[bsdgames-darwin.git] / sail / dr_1.c
1 /* $NetBSD: dr_1.c,v 1.5 1997/10/13 19:43:14 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)dr_1.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: dr_1.c,v 1.5 1997/10/13 19:43:14 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "driver.h"
46 #include <stdlib.h>
47
48 void
49 unfoul()
50 {
51 struct ship *sp;
52 struct ship *to;
53 int nat;
54 int i;
55
56 foreachship(sp) {
57 if (sp->file->captain[0])
58 continue;
59 nat = capship(sp)->nationality;
60 foreachship(to) {
61 if (nat != capship(to)->nationality &&
62 !toughmelee(sp, to, 0, 0))
63 continue;
64 for (i = fouled2(sp, to); --i >= 0;)
65 if (die() <= 2)
66 cleanfoul(sp, to, 0);
67 }
68 }
69 }
70
71 void
72 boardcomp()
73 {
74 int crew[3];
75 struct ship *sp, *sq;
76
77 foreachship(sp) {
78 if (*sp->file->captain)
79 continue;
80 if (sp->file->dir == 0)
81 continue;
82 if (sp->file->struck || sp->file->captured != 0)
83 continue;
84 if (!snagged(sp))
85 continue;
86 crew[0] = sp->specs->crew1 != 0;
87 crew[1] = sp->specs->crew2 != 0;
88 crew[2] = sp->specs->crew3 != 0;
89 foreachship(sq) {
90 if (!Xsnagged2(sp, sq))
91 continue;
92 if (meleeing(sp, sq))
93 continue;
94 if (!sq->file->dir
95 || sp->nationality == capship(sq)->nationality)
96 continue;
97 switch (sp->specs->class - sq->specs->class) {
98 case -3: case -4: case -5:
99 if (crew[0]) {
100 /* OBP */
101 sendbp(sp, sq, crew[0]*100, 0);
102 crew[0] = 0;
103 } else if (crew[1]){
104 /* OBP */
105 sendbp(sp, sq, crew[1]*10, 0);
106 crew[1] = 0;
107 }
108 break;
109 case -2:
110 if (crew[0] || crew[1]) {
111 /* OBP */
112 sendbp(sp, sq, crew[0]*100+crew[1]*10,
113 0);
114 crew[0] = crew[1] = 0;
115 }
116 break;
117 case -1: case 0: case 1:
118 if (crew[0]) {
119 /* OBP */
120 sendbp(sp, sq, crew[0]*100+crew[1]*10,
121 0);
122 crew[0] = crew[1] = 0;
123 }
124 break;
125 case 2: case 3: case 4: case 5:
126 /* OBP */
127 sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2],
128 0);
129 crew[0] = crew[1] = crew[2] = 0;
130 break;
131 }
132 }
133 }
134 }
135
136 int
137 fightitout(from, to, key)
138 struct ship *from, *to;
139 int key;
140 {
141 struct ship *fromcap, *tocap;
142 int crewfrom[3], crewto[3], menfrom, mento;
143 int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured;
144 int topoints;
145 int index, totalfrom = 0, totalto = 0;
146 int count;
147 char message[60];
148
149 menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key);
150 mento = mensent(to, from, crewto, &tocap, &pcto, 0);
151 if (fromcap == 0)
152 fromcap = from;
153 if (tocap == 0)
154 tocap = to;
155 if (key) {
156 if (!menfrom) { /* if crew surprised */
157 if (fromcap == from)
158 menfrom = from->specs->crew1
159 + from->specs->crew2
160 + from->specs->crew3;
161 else
162 menfrom = from->file->pcrew;
163 } else {
164 menfrom *= 2; /* DBP's fight at an advantage */
165 }
166 }
167 fromstrength = menfrom * fromcap->specs->qual;
168 strengthto = mento * tocap->specs->qual;
169 for (count = 0;
170 ((fromstrength < strengthto * 3 && strengthto < fromstrength * 3)
171 || fromstrength == -1) && count < 4;
172 count++) {
173 index = fromstrength/10;
174 if (index > 8)
175 index = 8;
176 toinjured = MT[index][2 - die() / 3];
177 totalto += toinjured;
178 index = strengthto/10;
179 if (index > 8)
180 index = 8;
181 frominjured = MT[index][2 - die() / 3];
182 totalfrom += frominjured;
183 menfrom -= frominjured;
184 mento -= toinjured;
185 fromstrength = menfrom * fromcap->specs->qual;
186 strengthto = mento * tocap->specs->qual;
187 }
188 if (fromstrength >= strengthto * 3 || count == 4) {
189 unboard(to, from, 0);
190 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
191 subtract(to, totalto, crewto, tocap, pcto);
192 makesignal(from, "boarders from %s repelled", to);
193 (void) sprintf(message, "killed in melee: %d. %s: %d",
194 totalto, from->shipname, totalfrom);
195 Write(W_SIGNAL, to, 1, (long) message, 0, 0, 0);
196 if (key)
197 return 1;
198 } else if (strengthto >= fromstrength * 3) {
199 unboard(from, to, 0);
200 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
201 subtract(to, totalto, crewto, tocap, pcto);
202 if (key) {
203 if (fromcap != from)
204 Write(W_POINTS, fromcap, 0,
205 fromcap->file->points -
206 from->file->struck
207 ? from->specs->pts
208 : 2 * from->specs->pts,
209 0, 0, 0);
210
211 /* ptr1 points to the shipspec for the ship that was just unboarded.
212 I guess that what is going on here is that the pointer is multiplied
213 or something. */
214
215 Write(W_CAPTURED, from, 0, to->file->index, 0, 0, 0);
216 topoints = 2 * from->specs->pts + to->file->points;
217 if (from->file->struck)
218 topoints -= from->specs->pts;
219 Write(W_POINTS, to, 0, topoints, 0, 0, 0);
220 mento = crewto[0] ? crewto[0] : crewto[1];
221 if (mento) {
222 subtract(to, mento, crewto, tocap, pcto);
223 subtract(from, - mento, crewfrom, to, 0);
224 }
225 (void) sprintf(message, "captured by the %s!",
226 to->shipname);
227 Write(W_SIGNAL, from, 1, (long) message, 0, 0, 0);
228 (void) sprintf(message, "killed in melee: %d. %s: %d",
229 totalto, from->shipname, totalfrom);
230 Write(W_SIGNAL, to, 1, (long) message, 0, 0, 0);
231 mento = 0;
232 return 0;
233 }
234 }
235 return 0;
236 }
237
238 void
239 resolve()
240 {
241 int thwart;
242 struct ship *sp, *sq;
243
244 foreachship(sp) {
245 if (sp->file->dir == 0)
246 continue;
247 for (sq = sp + 1; sq < ls; sq++)
248 if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
249 (void) fightitout(sp, sq, 0);
250 thwart = 2;
251 foreachship(sq) {
252 if (sq->file->dir && meleeing(sq, sp))
253 thwart = fightitout(sp, sq, 1);
254 if (!thwart)
255 break;
256 }
257 if (!thwart) {
258 foreachship(sq) {
259 if (sq->file->dir && meleeing(sq, sp))
260 unboard(sq, sp, 0);
261 unboard(sp, sq, 0);
262 }
263 unboard(sp, sp, 1);
264 } else if (thwart == 2)
265 unboard(sp, sp, 1);
266 }
267 }
268
269 void
270 compcombat()
271 {
272 int n;
273 struct ship *sp;
274 struct ship *closest;
275 int crew[3], men = 0, target, temp;
276 int r, guns, ready, load, car;
277 int index, rakehim, sternrake;
278 int shootat, hit;
279
280 foreachship(sp) {
281 if (sp->file->captain[0] || sp->file->dir == 0)
282 continue;
283 crew[0] = sp->specs->crew1;
284 crew[1] = sp->specs->crew2;
285 crew[2] = sp->specs->crew3;
286 for (n = 0; n < 3; n++) {
287 if (sp->file->OBP[n].turnsent)
288 men += sp->file->OBP[n].mensent;
289 }
290 for (n = 0; n < 3; n++) {
291 if (sp->file->DBP[n].turnsent)
292 men += sp->file->DBP[n].mensent;
293 }
294 if (men){
295 crew[0] = men/100 ? 0 : crew[0] != 0;
296 crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
297 crew[2] = men%10 ? 0 : crew[2] != 0;
298 }
299 for (r = 0; r < 2; r++) {
300 if (!crew[2])
301 continue;
302 if (sp->file->struck)
303 continue;
304 if (r) {
305 ready = sp->file->readyR;
306 guns = sp->specs->gunR;
307 car = sp->specs->carR;
308 } else {
309 ready = sp->file->readyL;
310 guns = sp->specs->gunL;
311 car = sp->specs->carL;
312 }
313 if (!guns && !car)
314 continue;
315 if ((ready & R_LOADED) == 0)
316 continue;
317 closest = closestenemy(sp, r ? 'r' : 'l', 0);
318 if (closest == 0)
319 continue;
320 if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1)))
321 continue;
322 if (closest->file->struck)
323 continue;
324 target = range(sp, closest);
325 if (target > 10)
326 continue;
327 if (!guns && target >= 3)
328 continue;
329 load = L_ROUND;
330 if (target == 1 && sp->file->loadwith == L_GRAPE)
331 load = L_GRAPE;
332 if (target <= 3 && closest->file->FS)
333 load = L_CHAIN;
334 if (target == 1 && load != L_GRAPE)
335 load = L_DOUBLE;
336 if (load > L_CHAIN && target < 6)
337 shootat = HULL;
338 else
339 shootat = RIGGING;
340 rakehim = gunsbear(sp, closest)
341 && !gunsbear(closest, sp);
342 temp = portside(closest, sp, 1)
343 - closest->file->dir + 1;
344 if (temp < 1)
345 temp += 8;
346 if (temp > 8)
347 temp -= 8;
348 sternrake = temp > 4 && temp < 6;
349 index = guns;
350 if (target < 3)
351 index += car;
352 index = (index - 1) / 3;
353 index = index > 8 ? 8 : index;
354 if (!rakehim)
355 hit = HDT[index][target-1];
356 else
357 hit = HDTrake[index][target-1];
358 if (rakehim && sternrake)
359 hit++;
360 hit += QUAL[index][capship(sp)->specs->qual - 1];
361 for (n = 0; n < 3 && sp->file->captured == 0; n++)
362 if (!crew[n])
363 if (index <= 5)
364 hit--;
365 else
366 hit -= 2;
367 if (ready & R_INITIAL) {
368 if (!r)
369 sp->file->readyL &= ~R_INITIAL;
370 else
371 sp->file->readyR &= ~R_INITIAL;
372 if (index <= 3)
373 hit++;
374 else
375 hit += 2;
376 }
377 if (sp->file->captured != 0)
378 if (index <= 1)
379 hit--;
380 else
381 hit -= 2;
382 hit += AMMO[index][load - 1];
383 temp = sp->specs->class;
384 if ((temp >= 5 || temp == 1) && windspeed == 5)
385 hit--;
386 if (windspeed == 6 && temp == 4)
387 hit -= 2;
388 if (windspeed == 6 && temp <= 3)
389 hit--;
390 if (hit >= 0) {
391 if (load != L_GRAPE)
392 hit = hit > 10 ? 10 : hit;
393 table(shootat, load, hit, closest, sp, die());
394 }
395 }
396 }
397 }
398
399 int
400 next()
401 {
402 if (++turn % 55 == 0)
403 if (alive)
404 alive = 0;
405 else
406 people = 0;
407 if (people <= 0 || windspeed == 7) {
408 struct ship *s;
409 struct ship *bestship = NULL;
410 float net, best = 0.0;
411 foreachship(s) {
412 if (*s->file->captain)
413 continue;
414 net = (float)s->file->points / s->specs->pts;
415 if (net > best) {
416 best = net;
417 bestship = s;
418 }
419 }
420 if (best > 0.0) {
421 char *p = getenv("WOTD");
422 if (p == 0)
423 p = "Driver";
424 if (islower(*p))
425 *p = toupper(*p);
426 (void) strncpy(bestship->file->captain, p,
427 sizeof bestship->file->captain);
428 bestship->file->captain
429 [sizeof bestship->file->captain - 1] = 0;
430 logger(bestship);
431 }
432 return -1;
433 }
434 Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0);
435 if (turn % 7 == 0 && (die() >= cc->windchange || !windspeed)) {
436 switch (die()) {
437 case 1:
438 winddir = 1;
439 break;
440 case 2:
441 break;
442 case 3:
443 winddir++;
444 break;
445 case 4:
446 winddir--;
447 break;
448 case 5:
449 winddir += 2;
450 break;
451 case 6:
452 winddir -= 2;
453 break;
454 }
455 if (winddir > 8)
456 winddir -= 8;
457 if (winddir < 1)
458 winddir += 8;
459 if (windspeed)
460 switch (die()) {
461 case 1:
462 case 2:
463 windspeed--;
464 break;
465 case 5:
466 case 6:
467 windspeed++;
468 break;
469 }
470 else
471 windspeed++;
472 Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0);
473 }
474 return 0;
475 }