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