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