dr_2.c revision 1.2 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.2 mycroft /*static char sccsid[] = "from: @(#)dr_2.c 5.4 (Berkeley) 6/1/90";*/
36 1.2 mycroft static char rcsid[] = "$Id: dr_2.c,v 1.2 1993/08/01 18:51:51 mycroft Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #include "driver.h"
40 1.1 cgd
41 1.1 cgd #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
42 1.1 cgd
43 1.1 cgd thinkofgrapples()
44 1.1 cgd {
45 1.1 cgd register struct ship *sp, *sq;
46 1.1 cgd char friendly;
47 1.1 cgd
48 1.1 cgd foreachship(sp) {
49 1.1 cgd if (sp->file->captain[0] || sp->file->dir == 0)
50 1.1 cgd continue;
51 1.1 cgd foreachship(sq) {
52 1.1 cgd friendly = sp->nationality == capship(sq)->nationality;
53 1.1 cgd if (!friendly) {
54 1.1 cgd if (sp->file->struck || sp->file->captured != 0)
55 1.1 cgd continue;
56 1.1 cgd if (range(sp, sq) != 1)
57 1.1 cgd continue;
58 1.1 cgd if (grappled2(sp, sq))
59 1.1 cgd if (toughmelee(sp, sq, 0, 0))
60 1.1 cgd ungrap(sp, sq);
61 1.1 cgd else
62 1.1 cgd grap(sp, sq);
63 1.1 cgd else if (couldwin(sp, sq)) {
64 1.1 cgd grap(sp, sq);
65 1.1 cgd sp->file->loadwith = L_GRAPE;
66 1.1 cgd }
67 1.1 cgd } else
68 1.1 cgd ungrap(sp, sq);
69 1.1 cgd }
70 1.1 cgd }
71 1.1 cgd }
72 1.1 cgd
73 1.1 cgd checkup()
74 1.1 cgd {
75 1.1 cgd register struct ship *sp, *sq;
76 1.1 cgd register char explode, sink;
77 1.1 cgd
78 1.1 cgd foreachship(sp) {
79 1.1 cgd if (sp->file->dir == 0)
80 1.1 cgd continue;
81 1.1 cgd explode = sp->file->explode;
82 1.1 cgd sink = sp->file->sink;
83 1.1 cgd if (explode != 1 && sink != 1)
84 1.1 cgd continue;
85 1.1 cgd if (die() < 5)
86 1.1 cgd continue;
87 1.1 cgd Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
88 1.1 cgd Write(W_DIR, sp, 0, 0, 0, 0, 0);
89 1.1 cgd if (snagged(sp))
90 1.1 cgd foreachship(sq)
91 1.1 cgd cleansnag(sp, sq, 1);
92 1.1 cgd if (sink != 1) {
93 1.1 cgd makesignal(sp, "exploding!", (struct ship *)0);
94 1.1 cgd foreachship(sq) {
95 1.1 cgd if (sp != sq && sq->file->dir && range(sp, sq) < 4)
96 1.1 cgd table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
97 1.1 cgd }
98 1.1 cgd } else
99 1.1 cgd makesignal(sp, "sinking!", (struct ship *)0);
100 1.1 cgd }
101 1.1 cgd }
102 1.1 cgd
103 1.1 cgd prizecheck()
104 1.1 cgd {
105 1.1 cgd register struct ship *sp;
106 1.1 cgd
107 1.1 cgd foreachship(sp) {
108 1.1 cgd if (sp->file->captured == 0)
109 1.1 cgd continue;
110 1.1 cgd if (sp->file->struck || sp->file->dir == 0)
111 1.1 cgd continue;
112 1.1 cgd if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
113 1.1 cgd Write(W_SIGNAL, sp, 1,
114 1.1 cgd (int)"prize crew overthrown", 0, 0, 0);
115 1.1 cgd Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
116 1.1 cgd Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
117 1.1 cgd }
118 1.1 cgd }
119 1.1 cgd }
120 1.1 cgd
121 1.1 cgd strend(str)
122 1.1 cgd char *str;
123 1.1 cgd {
124 1.1 cgd register char *p;
125 1.1 cgd
126 1.1 cgd for (p = str; *p; p++)
127 1.1 cgd ;
128 1.1 cgd return p == str ? 0 : p[-1];
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd closeon(from, to, command, ta, ma, af)
132 1.1 cgd register struct ship *from, *to;
133 1.1 cgd char command[];
134 1.1 cgd int ma, ta, af;
135 1.1 cgd {
136 1.1 cgd int high;
137 1.1 cgd char temp[10];
138 1.1 cgd
139 1.1 cgd temp[0] = command[0] = '\0';
140 1.1 cgd high = -30000;
141 1.1 cgd try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
145 1.1 cgd
146 1.1 cgd score(movement, ship, to, onlytemp)
147 1.1 cgd char movement[];
148 1.1 cgd register struct ship *ship, *to;
149 1.1 cgd char onlytemp;
150 1.1 cgd {
151 1.1 cgd char drift;
152 1.1 cgd int row, col, dir, total, ran;
153 1.1 cgd register struct File *fp = ship->file;
154 1.1 cgd
155 1.1 cgd if ((dir = fp->dir) == 0)
156 1.1 cgd return 0;
157 1.1 cgd row = fp->row;
158 1.1 cgd col = fp->col;
159 1.1 cgd drift = fp->drift;
160 1.1 cgd move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
161 1.1 cgd if (!*movement)
162 1.1 cgd (void) strcpy(movement, "d");
163 1.1 cgd
164 1.1 cgd ran = range(ship, to);
165 1.1 cgd total = -50 * ran;
166 1.1 cgd if (ran < 4 && gunsbear(ship, to))
167 1.1 cgd total += 60;
168 1.1 cgd if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
169 1.1 cgd total = -30000;
170 1.1 cgd
171 1.1 cgd if (!onlytemp) {
172 1.1 cgd fp->row = row;
173 1.1 cgd fp->col = col;
174 1.1 cgd fp->dir = dir;
175 1.1 cgd }
176 1.1 cgd return total;
177 1.1 cgd }
178 1.1 cgd
179 1.1 cgd move(p, ship, dir, row, col, drift)
180 1.1 cgd register char *p;
181 1.1 cgd register struct ship *ship;
182 1.1 cgd register char *dir;
183 1.1 cgd register short *row, *col;
184 1.1 cgd register char *drift;
185 1.1 cgd {
186 1.1 cgd int dist;
187 1.1 cgd char moved = 0;
188 1.1 cgd
189 1.1 cgd for (; *p; p++) {
190 1.1 cgd switch (*p) {
191 1.1 cgd case 'r':
192 1.1 cgd if (++*dir == 9)
193 1.1 cgd *dir = 1;
194 1.1 cgd break;
195 1.1 cgd case 'l':
196 1.1 cgd if (--*dir == 0)
197 1.1 cgd *dir = 8;
198 1.1 cgd break;
199 1.1 cgd case '1': case '2': case '3': case '4':
200 1.1 cgd case '5': case '6': case '7':
201 1.1 cgd moved++;
202 1.1 cgd if (*dir % 2 == 0)
203 1.1 cgd dist = dtab[*p - '0'];
204 1.1 cgd else
205 1.1 cgd dist = *p - '0';
206 1.1 cgd *row -= dr[*dir] * dist;
207 1.1 cgd *col -= dc[*dir] * dist;
208 1.1 cgd break;
209 1.1 cgd }
210 1.1 cgd }
211 1.1 cgd if (!moved) {
212 1.1 cgd if (windspeed != 0 && ++*drift > 2) {
213 1.1 cgd if (ship->specs->class >= 3 && !snagged(ship)
214 1.1 cgd || (turn & 1) == 0) {
215 1.1 cgd *row -= dr[winddir];
216 1.1 cgd *col -= dc[winddir];
217 1.1 cgd }
218 1.1 cgd }
219 1.1 cgd } else
220 1.1 cgd *drift = 0;
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
224 1.1 cgd register struct ship *f, *t;
225 1.1 cgd int ma, ta, af, *high, rakeme;
226 1.1 cgd char command[], temp[];
227 1.1 cgd {
228 1.1 cgd register int new, n;
229 1.1 cgd char st[4];
230 1.1 cgd #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
231 1.1 cgd
232 1.1 cgd if ((n = strend(temp)) < '1' || n > '9')
233 1.1 cgd for (n = 1; vma - n >= 0; n++) {
234 1.1 cgd (void) sprintf(st, "%d", n);
235 1.1 cgd (void) strcat(temp, st);
236 1.1 cgd new = score(temp, f, t, rakeme);
237 1.1 cgd if (new > *high && (!rakeme || rakeyou)) {
238 1.1 cgd *high = new;
239 1.1 cgd (void) strcpy(command, temp);
240 1.1 cgd }
241 1.1 cgd try(command, temp, ma-n, ta, af, vma-n,
242 1.1 cgd dir, f, t, high, rakeme);
243 1.1 cgd rmend(temp);
244 1.1 cgd }
245 1.1 cgd if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
246 1.1 cgd (void) strcat(temp, "r");
247 1.1 cgd new = score(temp, f, t, rakeme);
248 1.1 cgd if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
249 1.1 cgd *high = new;
250 1.1 cgd (void) strcpy(command, temp);
251 1.1 cgd }
252 1.1 cgd 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);
253 1.1 cgd rmend(temp);
254 1.1 cgd }
255 1.1 cgd if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
256 1.1 cgd (void) strcat(temp, "l");
257 1.1 cgd new = score(temp, f, t, rakeme);
258 1.1 cgd if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
259 1.1 cgd *high = new;
260 1.1 cgd (void) strcpy(command, temp);
261 1.1 cgd }
262 1.1 cgd 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);
263 1.1 cgd rmend(temp);
264 1.1 cgd }
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd rmend(str)
268 1.1 cgd char *str;
269 1.1 cgd {
270 1.1 cgd register char *p;
271 1.1 cgd
272 1.1 cgd for (p = str; *p; p++)
273 1.1 cgd ;
274 1.1 cgd if (p != str)
275 1.1 cgd *--p = 0;
276 1.1 cgd }
277