dr_1.c revision 1.16 1 /* $NetBSD: dr_1.c,v 1.16 2001/01/04 05:34:56 jwise 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.16 2001/01/04 05:34:56 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include "extern.h"
50 #include "driver.h"
51
52 void unfoul(void);
53 void boardcomp(void);
54 static int fightitout(struct ship *, struct ship *, int);
55 void resolve(void);
56 void compcombat(void);
57 int next(void);
58
59 void
60 unfoul(void)
61 {
62 struct ship *sp;
63 struct ship *to;
64 int nat;
65 int i;
66
67 foreachship(sp) {
68 if (sp->file->captain[0])
69 continue;
70 nat = capship(sp)->nationality;
71 foreachship(to) {
72 if (nat != capship(to)->nationality &&
73 !is_toughmelee(sp, to, 0, 0))
74 continue;
75 for (i = fouled2(sp, to); --i >= 0;)
76 if (dieroll() <= 2)
77 cleanfoul(sp, to, 0);
78 }
79 }
80 }
81
82 void
83 boardcomp(void)
84 {
85 int crew[3];
86 struct ship *sp, *sq;
87
88 foreachship(sp) {
89 if (*sp->file->captain)
90 continue;
91 if (sp->file->dir == 0)
92 continue;
93 if (sp->file->struck || sp->file->captured != 0)
94 continue;
95 if (!snagged(sp))
96 continue;
97 crew[0] = sp->specs->crew1 != 0;
98 crew[1] = sp->specs->crew2 != 0;
99 crew[2] = sp->specs->crew3 != 0;
100 foreachship(sq) {
101 if (!Xsnagged2(sp, sq))
102 continue;
103 if (meleeing(sp, sq))
104 continue;
105 if (!sq->file->dir
106 || sp->nationality == capship(sq)->nationality)
107 continue;
108 switch (sp->specs->class - sq->specs->class) {
109 case -3: case -4: case -5:
110 if (crew[0]) {
111 /* OBP */
112 sendbp(sp, sq, crew[0]*100, 0);
113 crew[0] = 0;
114 } else if (crew[1]){
115 /* OBP */
116 sendbp(sp, sq, crew[1]*10, 0);
117 crew[1] = 0;
118 }
119 break;
120 case -2:
121 if (crew[0] || crew[1]) {
122 /* OBP */
123 sendbp(sp, sq, crew[0]*100+crew[1]*10,
124 0);
125 crew[0] = crew[1] = 0;
126 }
127 break;
128 case -1: case 0: case 1:
129 if (crew[0]) {
130 /* OBP */
131 sendbp(sp, sq, crew[0]*100+crew[1]*10,
132 0);
133 crew[0] = crew[1] = 0;
134 }
135 break;
136 case 2: case 3: case 4: case 5:
137 /* OBP */
138 sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2],
139 0);
140 crew[0] = crew[1] = crew[2] = 0;
141 break;
142 }
143 }
144 }
145 }
146
147 static int
148 fightitout(struct ship *from, struct ship *to, int key)
149 {
150 struct ship *fromcap, *tocap;
151 int crewfrom[3], crewto[3], menfrom, mento;
152 int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured;
153 int topoints;
154 int index, totalfrom = 0, totalto = 0;
155 int count;
156 char message[60];
157
158 menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key);
159 mento = mensent(to, from, crewto, &tocap, &pcto, 0);
160 if (fromcap == 0)
161 fromcap = from;
162 if (tocap == 0)
163 tocap = to;
164 if (key) {
165 if (!menfrom) { /* if crew surprised */
166 if (fromcap == from)
167 menfrom = from->specs->crew1
168 + from->specs->crew2
169 + from->specs->crew3;
170 else
171 menfrom = from->file->pcrew;
172 } else {
173 menfrom *= 2; /* DBP's fight at an advantage */
174 }
175 }
176 fromstrength = menfrom * fromcap->specs->qual;
177 strengthto = mento * tocap->specs->qual;
178 for (count = 0;
179 ((fromstrength < strengthto * 3 && strengthto < fromstrength * 3)
180 || fromstrength == -1) && count < 4;
181 count++) {
182 index = fromstrength/10;
183 if (index > 8)
184 index = 8;
185 toinjured = MT[index][2 - dieroll() / 3];
186 totalto += toinjured;
187 index = strengthto/10;
188 if (index > 8)
189 index = 8;
190 frominjured = MT[index][2 - dieroll() / 3];
191 totalfrom += frominjured;
192 menfrom -= frominjured;
193 mento -= toinjured;
194 fromstrength = menfrom * fromcap->specs->qual;
195 strengthto = mento * tocap->specs->qual;
196 }
197 if (fromstrength >= strengthto * 3 || count == 4) {
198 unboard(to, from, 0);
199 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
200 subtract(to, totalto, crewto, tocap, pcto);
201 makemsg(from, "boarders from %s repelled", to->shipname);
202 sprintf(message, "killed in melee: %d. %s: %d",
203 totalto, from->shipname, totalfrom);
204 Writestr(W_SIGNAL, to, message);
205 if (key)
206 return 1;
207 } else if (strengthto >= fromstrength * 3) {
208 unboard(from, to, 0);
209 subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
210 subtract(to, totalto, crewto, tocap, pcto);
211 if (key) {
212 if (fromcap != from)
213 Write(W_POINTS, fromcap,
214 fromcap->file->points -
215 from->file->struck
216 ? from->specs->pts
217 : 2 * from->specs->pts,
218 0, 0, 0);
219
220 /* ptr1 points to the shipspec for the ship that was just unboarded.
221 I guess that what is going on here is that the pointer is multiplied
222 or something. */
223
224 Write(W_CAPTURED, from, to->file->index, 0, 0, 0);
225 topoints = 2 * from->specs->pts + to->file->points;
226 if (from->file->struck)
227 topoints -= from->specs->pts;
228 Write(W_POINTS, to, topoints, 0, 0, 0);
229 mento = crewto[0] ? crewto[0] : crewto[1];
230 if (mento) {
231 subtract(to, mento, crewto, tocap, pcto);
232 subtract(from, - mento, crewfrom, to, 0);
233 }
234 sprintf(message, "captured by the %s!", to->shipname);
235 Writestr(W_SIGNAL, from, message);
236 sprintf(message, "killed in melee: %d. %s: %d",
237 totalto, from->shipname, totalfrom);
238 Writestr(W_SIGNAL, to, message);
239 mento = 0;
240 return 0;
241 }
242 }
243 return 0;
244 }
245
246 void
247 resolve(void)
248 {
249 int thwart;
250 struct ship *sp, *sq;
251
252 foreachship(sp) {
253 if (sp->file->dir == 0)
254 continue;
255 for (sq = sp + 1; sq < ls; sq++)
256 if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
257 fightitout(sp, sq, 0);
258 thwart = 2;
259 foreachship(sq) {
260 if (sq->file->dir && meleeing(sq, sp))
261 thwart = fightitout(sp, sq, 1);
262 if (!thwart)
263 break;
264 }
265 if (!thwart) {
266 foreachship(sq) {
267 if (sq->file->dir && meleeing(sq, sp))
268 unboard(sq, sp, 0);
269 unboard(sp, sq, 0);
270 }
271 unboard(sp, sp, 1);
272 } else if (thwart == 2)
273 unboard(sp, sp, 1);
274 }
275 }
276
277 void
278 compcombat(void)
279 {
280 int n;
281 struct ship *sp;
282 struct ship *closest;
283 int crew[3], men = 0, target, temp;
284 int r, guns, ready, load, car;
285 int index, rakehim, sternrake;
286 int shootat, hit;
287
288 foreachship(sp) {
289 if (sp->file->captain[0] || sp->file->dir == 0)
290 continue;
291 crew[0] = sp->specs->crew1;
292 crew[1] = sp->specs->crew2;
293 crew[2] = sp->specs->crew3;
294 for (n = 0; n < 3; n++) {
295 if (sp->file->OBP[n].turnsent)
296 men += sp->file->OBP[n].mensent;
297 }
298 for (n = 0; n < 3; n++) {
299 if (sp->file->DBP[n].turnsent)
300 men += sp->file->DBP[n].mensent;
301 }
302 if (men){
303 crew[0] = men/100 ? 0 : crew[0] != 0;
304 crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
305 crew[2] = men%10 ? 0 : crew[2] != 0;
306 }
307 for (r = 0; r < 2; r++) {
308 if (!crew[2])
309 continue;
310 if (sp->file->struck)
311 continue;
312 if (r) {
313 ready = sp->file->readyR;
314 guns = sp->specs->gunR;
315 car = sp->specs->carR;
316 } else {
317 ready = sp->file->readyL;
318 guns = sp->specs->gunL;
319 car = sp->specs->carL;
320 }
321 if (!guns && !car)
322 continue;
323 if ((ready & R_LOADED) == 0)
324 continue;
325 closest = closestenemy(sp, r ? 'r' : 'l', 0);
326 if (closest == 0)
327 continue;
328 if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1)))
329 continue;
330 if (closest->file->struck)
331 continue;
332 target = range(sp, closest);
333 if (target > 10)
334 continue;
335 if (!guns && target >= 3)
336 continue;
337 load = L_ROUND;
338 if (target == 1 && sp->file->loadwith == L_GRAPE)
339 load = L_GRAPE;
340 if (target <= 3 && closest->file->FS)
341 load = L_CHAIN;
342 if (target == 1 && load != L_GRAPE)
343 load = L_DOUBLE;
344 if (load > L_CHAIN && target < 6)
345 shootat = HULL;
346 else
347 shootat = RIGGING;
348 rakehim = gunsbear(sp, closest)
349 && !gunsbear(closest, sp);
350 temp = portside(closest, sp, 1)
351 - closest->file->dir + 1;
352 if (temp < 1)
353 temp += 8;
354 if (temp > 8)
355 temp -= 8;
356 sternrake = temp > 4 && temp < 6;
357 index = guns;
358 if (target < 3)
359 index += car;
360 index = (index - 1) / 3;
361 index = index > 8 ? 8 : index;
362 if (!rakehim)
363 hit = HDT[index][target-1];
364 else
365 hit = HDTrake[index][target-1];
366 if (rakehim && sternrake)
367 hit++;
368 hit += QUAL[index][capship(sp)->specs->qual - 1];
369 for (n = 0; n < 3 && sp->file->captured == 0; n++)
370 if (!crew[n]) {
371 if (index <= 5)
372 hit--;
373 else
374 hit -= 2;
375 }
376 if (ready & R_INITIAL) {
377 if (!r)
378 sp->file->readyL &= ~R_INITIAL;
379 else
380 sp->file->readyR &= ~R_INITIAL;
381 if (index <= 3)
382 hit++;
383 else
384 hit += 2;
385 }
386 if (sp->file->captured != 0) {
387 if (index <= 1)
388 hit--;
389 else
390 hit -= 2;
391 }
392 hit += AMMO[index][load - 1];
393 temp = sp->specs->class;
394 if ((temp >= 5 || temp == 1) && windspeed == 5)
395 hit--;
396 if (windspeed == 6 && temp == 4)
397 hit -= 2;
398 if (windspeed == 6 && temp <= 3)
399 hit--;
400 if (hit >= 0) {
401 if (load != L_GRAPE)
402 hit = hit > 10 ? 10 : hit;
403 table(shootat, load, hit, closest, sp, dieroll());
404 }
405 }
406 }
407 }
408
409 int
410 next(void)
411 {
412 if (++turn % 55 == 0) {
413 if (alive)
414 alive = 0;
415 else
416 people = 0;
417 }
418 if (people <= 0 || windspeed == 7) {
419 struct ship *s;
420 struct ship *bestship = NULL;
421 float net, best = 0.0;
422 foreachship(s) {
423 if (*s->file->captain)
424 continue;
425 net = (float)s->file->points / s->specs->pts;
426 if (net > best) {
427 best = net;
428 bestship = s;
429 }
430 }
431 if (best > 0.0) {
432 char *tp = getenv("WOTD");
433 const char *p;
434 if (tp == 0)
435 p = "Driver";
436 else {
437 if (islower(*tp))
438 *tp = toupper(*tp);
439 p = tp;
440 }
441 strncpy(bestship->file->captain, p,
442 sizeof bestship->file->captain);
443 bestship->file->captain
444 [sizeof bestship->file->captain - 1] = 0;
445 logger(bestship);
446 }
447 return -1;
448 }
449 Write(W_TURN, SHIP(0), turn, 0, 0, 0);
450 if (turn % 7 == 0 && (dieroll() >= cc->windchange || !windspeed)) {
451 switch (dieroll()) {
452 case 1:
453 winddir = 1;
454 break;
455 case 2:
456 break;
457 case 3:
458 winddir++;
459 break;
460 case 4:
461 winddir--;
462 break;
463 case 5:
464 winddir += 2;
465 break;
466 case 6:
467 winddir -= 2;
468 break;
469 }
470 if (winddir > 8)
471 winddir -= 8;
472 if (winddir < 1)
473 winddir += 8;
474 if (windspeed)
475 switch (dieroll()) {
476 case 1:
477 case 2:
478 windspeed--;
479 break;
480 case 5:
481 case 6:
482 windspeed++;
483 break;
484 }
485 else
486 windspeed++;
487 Write(W_WIND, SHIP(0), winddir, windspeed, 0, 0);
488 }
489 return 0;
490 }
491