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