date and time created 90/06/25 12:35:11 by bostic
[unix-history] / usr / src / games / sail / dr_2.c
CommitLineData
b5f0675e 1/*
1e008c14 2 * Copyright (c) 1983 Regents of the University of California.
c0ca48ec
KB
3 * All rights reserved.
4 *
b2e7427f 5 * %sccs.include.redist.c%
b5f0675e
EW
6 */
7
717de9f0 8#ifndef lint
b2e7427f 9static char sccsid[] = "@(#)dr_2.c 5.4 (Berkeley) %G%";
c0ca48ec 10#endif /* not lint */
717de9f0 11
b3a57661
EW
12#include "driver.h"
13
14#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
717de9f0
CL
15
16thinkofgrapples()
17{
b3a57661
EW
18 register struct ship *sp, *sq;
19 char friendly;
717de9f0 20
b3a57661
EW
21 foreachship(sp) {
22 if (sp->file->captain[0] || sp->file->dir == 0)
23 continue;
24 foreachship(sq) {
25 friendly = sp->nationality == capship(sq)->nationality;
26 if (!friendly) {
27 if (sp->file->struck || sp->file->captured != 0)
28 continue;
29 if (range(sp, sq) != 1)
30 continue;
31 if (grappled2(sp, sq))
32 if (toughmelee(sp, sq, 0, 0))
33 ungrap(sp, sq);
34 else
35 grap(sp, sq);
36 else if (couldwin(sp, sq)) {
37 grap(sp, sq);
38 sp->file->loadwith = L_GRAPE;
717de9f0 39 }
b3a57661
EW
40 } else
41 ungrap(sp, sq);
717de9f0
CL
42 }
43 }
44}
45
717de9f0
CL
46checkup()
47{
b3a57661
EW
48 register struct ship *sp, *sq;
49 register char explode, sink;
717de9f0 50
b3a57661 51 foreachship(sp) {
8b4ea3df
EW
52 if (sp->file->dir == 0)
53 continue;
b3a57661
EW
54 explode = sp->file->explode;
55 sink = sp->file->sink;
b3a57661
EW
56 if (explode != 1 && sink != 1)
57 continue;
8b4ea3df
EW
58 if (die() < 5)
59 continue;
60 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
6ca45914
EW
61 Write(W_DIR, sp, 0, 0, 0, 0, 0);
62 if (snagged(sp))
63 foreachship(sq)
64 cleansnag(sp, sq, 1);
b3a57661
EW
65 if (sink != 1) {
66 makesignal(sp, "exploding!", (struct ship *)0);
67 foreachship(sq) {
68 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
69 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
70 }
71 } else
72 makesignal(sp, "sinking!", (struct ship *)0);
717de9f0
CL
73 }
74}
75
76prizecheck()
77{
b3a57661 78 register struct ship *sp;
717de9f0 79
b3a57661
EW
80 foreachship(sp) {
81 if (sp->file->captured == 0)
82 continue;
83 if (sp->file->struck || sp->file->dir == 0)
84 continue;
0483f58f 85 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
b3a57661
EW
86 Write(W_SIGNAL, sp, 1,
87 (int)"prize crew overthrown", 0, 0, 0);
0483f58f
EW
88 Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
89 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
717de9f0
CL
90 }
91 }
92}
93
b3a57661 94strend(str)
717de9f0
CL
95char *str;
96{
b3a57661 97 register char *p;
717de9f0 98
b3a57661
EW
99 for (p = str; *p; p++)
100 ;
101 return p == str ? 0 : p[-1];
717de9f0
CL
102}
103
b3a57661
EW
104closeon(from, to, command, ta, ma, af)
105register struct ship *from, *to;
106char command[];
107int ma, ta, af;
717de9f0
CL
108{
109 int high;
110 char temp[10];
111
112 temp[0] = command[0] = '\0';
113 high = -30000;
b3a57661 114 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
717de9f0
CL
115}
116
b3a57661 117int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
717de9f0 118
b3a57661
EW
119score(movement, ship, to, onlytemp)
120char movement[];
121register struct ship *ship, *to;
122char onlytemp;
717de9f0 123{
1eda8103
EW
124 char drift;
125 int row, col, dir, total, ran;
b3a57661 126 register struct File *fp = ship->file;
717de9f0 127
b3a57661
EW
128 if ((dir = fp->dir) == 0)
129 return 0;
130 row = fp->row;
131 col = fp->col;
132 drift = fp->drift;
133 move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
134 if (!*movement)
135 (void) strcpy(movement, "d");
136
137 ran = range(ship, to);
138 total = -50 * ran;
139 if (ran < 4 && gunsbear(ship, to))
140 total += 60;
141 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
142 total = -30000;
143
144 if (!onlytemp) {
145 fp->row = row;
146 fp->col = col;
147 fp->dir = dir;
148 }
149 return total;
150}
151
b3a57661
EW
152move(p, ship, dir, row, col, drift)
153register char *p;
154register struct ship *ship;
1eda8103
EW
155register char *dir;
156register short *row, *col;
157register char *drift;
b3a57661
EW
158{
159 int dist;
160 char moved = 0;
161
162 for (; *p; p++) {
163 switch (*p) {
164 case 'r':
165 if (++*dir == 9)
166 *dir = 1;
167 break;
168 case 'l':
169 if (--*dir == 0)
170 *dir = 8;
171 break;
172 case '1': case '2': case '3': case '4':
173 case '5': case '6': case '7':
174 moved++;
175 if (*dir % 2 == 0)
176 dist = dtab[*p - '0'];
177 else
178 dist = *p - '0';
179 *row -= dr[*dir] * dist;
180 *col -= dc[*dir] * dist;
181 break;
717de9f0 182 }
717de9f0 183 }
b3a57661 184 if (!moved) {
9d517234 185 if (windspeed != 0 && ++*drift > 2) {
b3a57661 186 if (ship->specs->class >= 3 && !snagged(ship)
acfebbf7 187 || (turn & 1) == 0) {
b3a57661
EW
188 *row -= dr[winddir];
189 *col -= dc[winddir];
190 }
191 }
192 } else
acfebbf7 193 *drift = 0;
717de9f0
CL
194}
195
b3a57661
EW
196try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
197register struct ship *f, *t;
198int ma, ta, af, *high, rakeme;
199char command[], temp[];
717de9f0
CL
200{
201 register int new, n;
202 char st[4];
b3a57661 203#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
717de9f0
CL
204
205 if ((n = strend(temp)) < '1' || n > '9')
b3a57661
EW
206 for (n = 1; vma - n >= 0; n++) {
207 (void) sprintf(st, "%d", n);
208 (void) strcat(temp, st);
209 new = score(temp, f, t, rakeme);
210 if (new > *high && (!rakeme || rakeyou)) {
717de9f0 211 *high = new;
b3a57661 212 (void) strcpy(command, temp);
717de9f0 213 }
b3a57661
EW
214 try(command, temp, ma-n, ta, af, vma-n,
215 dir, f, t, high, rakeme);
717de9f0
CL
216 rmend(temp);
217 }
b3a57661
EW
218 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
219 (void) strcat(temp, "r");
220 new = score(temp, f, t, rakeme);
221 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
717de9f0 222 *high = new;
b3a57661 223 (void) strcpy(command, temp);
717de9f0 224 }
b3a57661 225 try(command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1),f,t,high,rakeme);
717de9f0
CL
226 rmend(temp);
227 }
b3a57661
EW
228 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
229 (void) strcat(temp, "l");
230 new = score(temp, f, t, rakeme);
231 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
717de9f0 232 *high = new;
b3a57661 233 (void) strcpy(command, temp);
717de9f0 234 }
b3a57661 235 try(command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), f, t, high, rakeme);
717de9f0
CL
236 rmend(temp);
237 }
238}
9224751f
EW
239
240rmend(str)
241char *str;
242{
243 register char *p;
244
245 for (p = str; *p; p++)
246 ;
247 if (p != str)
248 *--p = 0;
249}