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