2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)dr_2.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/dr_2.c,v 1.6 1999/11/30 03:49:32 billf Exp $
35 * $DragonFly: src/games/sail/dr_2.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
41 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
49 if (sp->file->captain[0] || sp->file->dir == 0)
52 friendly = sp->nationality == capship(sq)->nationality;
54 if (sp->file->struck || sp->file->captured != 0)
56 if (range(sp, sq) != 1)
58 if (grappled2(sp, sq))
59 if (toughmelee(sp, sq, 0, 0))
63 else if (couldwin(sp, sq)) {
65 sp->file->loadwith = L_GRAPE;
79 if (sp->file->dir == 0)
81 explode = sp->file->explode;
82 sink = sp->file->sink;
83 if (explode != 1 && sink != 1)
87 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
88 Write(W_DIR, sp, 0, 0, 0, 0, 0);
93 makesignal(sp, "exploding!", (struct ship *)0);
95 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
96 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
99 makesignal(sp, "sinking!", (struct ship *)0);
108 if (sp->file->captured == 0)
110 if (sp->file->struck || sp->file->dir == 0)
112 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
113 Write(W_SIGNAL, sp, 1,
114 (int)"prize crew overthrown", 0, 0, 0);
115 Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
116 Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
126 for (p = str; *p; p++)
128 return p == str ? 0 : p[-1];
131 closeon(from, to, command, ta, ma, af)
132 struct ship *from, *to;
139 temp[0] = command[0] = '\0';
141 try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
144 int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
146 score(movement, ship, to, onlytemp)
148 struct ship *ship, *to;
152 int row, col, dir, total, ran;
153 struct File *fp = ship->file;
155 if ((dir = fp->dir) == 0)
160 sail_move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
162 (void) strcpy(movement, "d");
164 ran = range(ship, to);
166 if (ran < 4 && gunsbear(ship, to))
168 if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
179 sail_move(p, ship, dir, row, col, drift)
199 case '1': case '2': case '3': case '4':
200 case '5': case '6': case '7':
203 dist = dtab[*p - '0'];
206 *row -= dr[*dir] * dist;
207 *col -= dc[*dir] * dist;
212 if (windspeed != 0 && ++*drift > 2) {
213 if (ship->specs->class >= 3 && !snagged(ship)
214 || (turn & 1) == 0) {
223 try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
225 int ma, ta, af, vma, dir, *high, rakeme;
226 char command[], temp[];
230 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
232 if ((n = strend(temp)) < '1' || n > '9')
233 for (n = 1; vma - n >= 0; n++) {
234 (void) sprintf(st, "%d", n);
235 (void) strcat(temp, st);
236 new = score(temp, f, t, rakeme);
237 if (new > *high && (!rakeme || rakeyou)) {
239 (void) strcpy(command, temp);
241 try(command, temp, ma-n, ta, af, vma-n,
242 dir, f, t, high, rakeme);
245 if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
246 (void) strcat(temp, "r");
247 new = score(temp, f, t, rakeme);
248 if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
250 (void) strcpy(command, temp);
252 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);
255 if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
256 (void) strcat(temp, "l");
257 new = score(temp, f, t, rakeme);
258 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
260 (void) strcpy(command, temp);
262 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);
272 for (p = str; *p; p++)