dr_2.c revision 1.9 1 /* $NetBSD: dr_2.c,v 1.9 1999/02/10 00:45:45 hubertf Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: dr_2.c,v 1.9 1999/02/10 00:45:45 hubertf Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "driver.h"
46 #include <stdlib.h>
47
48 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
49
50 void
51 thinkofgrapples()
52 {
53 struct ship *sp, *sq;
54 char friendly;
55
56 foreachship(sp) {
57 if (sp->file->captain[0] || sp->file->dir == 0)
58 continue;
59 foreachship(sq) {
60 friendly = sp->nationality == capship(sq)->nationality;
61 if (!friendly) {
62 if (sp->file->struck || sp->file->captured != 0)
63 continue;
64 if (range(sp, sq) != 1)
65 continue;
66 if (grappled2(sp, sq))
67 if (toughmelee(sp, sq, 0, 0))
68 ungrap(sp, sq);
69 else
70 grap(sp, sq);
71 else if (couldwin(sp, sq)) {
72 grap(sp, sq);
73 sp->file->loadwith = L_GRAPE;
74 }
75 } else
76 ungrap(sp, sq);
77 }
78 }
79 }
80
81 void
82 checkup()
83 {
84 struct ship *sp, *sq;
85 char explode, sink;
86
87 foreachship(sp) {
88 if (sp->file->dir == 0)
89 continue;
90 explode = sp->file->explode;
91 sink = sp->file->sink;
92 if (explode != 1 && sink != 1)
93 continue;
94 if (die() < 5)
95 continue;
96 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
97 Write(W_DIR, sp, 0, 0, 0, 0);
98 if (snagged(sp))
99 foreachship(sq)
100 cleansnag(sp, sq, 1);
101 if (sink != 1) {
102 makemsg(sp, "exploding!");
103 foreachship(sq) {
104 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
105 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
106 }
107 } else
108 makemsg(sp, "sinking!");
109 }
110 }
111
112 void
113 prizecheck()
114 {
115 struct ship *sp;
116
117 foreachship(sp) {
118 if (sp->file->captured == 0)
119 continue;
120 if (sp->file->struck || sp->file->dir == 0)
121 continue;
122 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
123 Writestr(W_SIGNAL, sp, "prize crew overthrown");
124 Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
125 Write(W_CAPTURED, sp, -1, 0, 0, 0);
126 }
127 }
128 }
129
130 int
131 strend(str)
132 char *str;
133 {
134 char *p;
135
136 for (p = str; *p; p++)
137 ;
138 return p == str ? 0 : p[-1];
139 }
140
141 void
142 closeon(from, to, command, ta, ma, af)
143 struct ship *from, *to;
144 char command[];
145 int ma, ta, af;
146 {
147 int high;
148 char temp[10];
149
150 temp[0] = command[0] = '\0';
151 high = -30000;
152 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
153 }
154
155 int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
156
157 int
158 score(movement, ship, to, onlytemp)
159 char movement[];
160 struct ship *ship, *to;
161 char onlytemp;
162 {
163 char drift;
164 int row, col, dir, total, ran;
165 struct File *fp = ship->file;
166
167 if ((dir = fp->dir) == 0)
168 return 0;
169 row = fp->row;
170 col = fp->col;
171 drift = fp->drift;
172 move_ship(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
173 if (!*movement)
174 (void) strcpy(movement, "d");
175
176 ran = range(ship, to);
177 total = -50 * ran;
178 if (ran < 4 && gunsbear(ship, to))
179 total += 60;
180 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
181 total = -30000;
182
183 if (!onlytemp) {
184 fp->row = row;
185 fp->col = col;
186 fp->dir = dir;
187 }
188 return total;
189 }
190
191 void
192 move_ship(p, ship, dir, row, col, drift)
193 char *p;
194 struct ship *ship;
195 unsigned char *dir;
196 short *row, *col;
197 char *drift;
198 {
199 int dist;
200 char moved = 0;
201
202 for (; *p; p++) {
203 switch (*p) {
204 case 'r':
205 if (++*dir == 9)
206 *dir = 1;
207 break;
208 case 'l':
209 if (--*dir == 0)
210 *dir = 8;
211 break;
212 case '1': case '2': case '3': case '4':
213 case '5': case '6': case '7':
214 moved++;
215 if (*dir % 2 == 0)
216 dist = dtab[*p - '0'];
217 else
218 dist = *p - '0';
219 *row -= dr[*dir] * dist;
220 *col -= dc[*dir] * dist;
221 break;
222 }
223 }
224 if (!moved) {
225 if (windspeed != 0 && ++*drift > 2) {
226 if ((ship->specs->class >= 3 && !snagged(ship))
227 || (turn & 1) == 0) {
228 *row -= dr[winddir];
229 *col -= dc[winddir];
230 }
231 }
232 } else
233 *drift = 0;
234 }
235
236 void
237 try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
238 struct ship *f, *t;
239 int ma, ta, af, vma, dir, *high, rakeme;
240 char command[], temp[];
241 {
242 int new, n;
243 char st[4];
244 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
245
246 if ((n = strend(temp)) < '1' || n > '9')
247 for (n = 1; vma - n >= 0; n++) {
248 (void) sprintf(st, "%d", n);
249 (void) strcat(temp, st);
250 new = score(temp, f, t, rakeme);
251 if (new > *high && (!rakeme || rakeyou)) {
252 *high = new;
253 (void) strcpy(command, temp);
254 }
255 try(command, temp, ma-n, ta, af, vma-n,
256 dir, f, t, high, rakeme);
257 rmend(temp);
258 }
259 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)) {
260 (void) strcat(temp, "r");
261 new = score(temp, f, t, rakeme);
262 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
263 *high = new;
264 (void) strcpy(command, temp);
265 }
266 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);
267 rmend(temp);
268 }
269 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
270 (void) strcat(temp, "l");
271 new = score(temp, f, t, rakeme);
272 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
273 *high = new;
274 (void) strcpy(command, temp);
275 }
276 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);
277 rmend(temp);
278 }
279 }
280
281 void
282 rmend(str)
283 char *str;
284 {
285 char *p;
286
287 for (p = str; *p; p++)
288 ;
289 if (p != str)
290 *--p = 0;
291 }
292