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