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