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