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