1 /* $OpenBSD: dr_2.c,v 1.7 2016/01/08 20:26:33 mestre Exp $ */ 2 /* $NetBSD: dr_2.c,v 1.4 1995/04/24 12:25:12 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <stdlib.h> 34 #include <string.h> 35 36 #include "driver.h" 37 #include "extern.h" 38 #include "player.h" 39 40 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5) 41 42 void 43 thinkofgrapples(void) 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 (is_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 void 74 checkup(void) 75 { 76 struct ship *sp, *sq; 77 char explode, sink; 78 79 foreachship(sp) { 80 if (sp->file->dir == 0) 81 continue; 82 explode = sp->file->explode; 83 sink = sp->file->sink; 84 if (explode != 1 && sink != 1) 85 continue; 86 if (die() < 5) 87 continue; 88 Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0); 89 Write(W_DIR, sp, 0, 0, 0, 0); 90 if (snagged(sp)) 91 foreachship(sq) 92 cleansnag(sp, sq, 1); 93 if (sink != 1) { 94 makemsg(sp, "exploding!"); 95 foreachship(sq) { 96 if (sp != sq && sq->file->dir && range(sp, sq) < 4) 97 table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6); 98 } 99 } else 100 makemsg(sp, "sinking!"); 101 } 102 } 103 104 void 105 prizecheck(void) 106 { 107 struct ship *sp; 108 109 foreachship(sp) { 110 if (sp->file->captured == 0) 111 continue; 112 if (sp->file->struck || sp->file->dir == 0) 113 continue; 114 if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) { 115 Writestr(W_SIGNAL, sp, "prize crew overthrown"); 116 Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0); 117 Write(W_CAPTURED, sp, -1, 0, 0, 0); 118 } 119 } 120 } 121 122 int 123 str_end(const char *str) 124 { 125 const char *p; 126 127 for (p = str; *p; p++) 128 ; 129 return p == str ? 0 : p[-1]; 130 } 131 132 void 133 closeon(struct ship *from, struct ship *to, char command[], size_t commandl, 134 int ta, int ma, int af) 135 { 136 int high; 137 char temp[10]; 138 139 temp[0] = command[0] = '\0'; 140 high = -30000; 141 try(command, commandl, temp, sizeof temp, ma, ta, af, ma, from->file->dir, 142 from, to, &high, 0); 143 } 144 145 const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */ 146 147 int 148 score(char movement[], size_t movementl, struct ship *ship, struct ship *to, 149 int 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 move_ship(movement, ship, &fp->dir, &fp->row, &fp->col, &drift); 161 if (!*movement) 162 (void) strlcpy(movement, "d", movementl); 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 void 180 move_ship(const char *p, struct ship *ship, unsigned char *dir, short *row, 181 short *col, char *drift) 182 { 183 int dist; 184 char moved = 0; 185 186 for (; *p; p++) { 187 switch (*p) { 188 case 'r': 189 if (++*dir == 9) 190 *dir = 1; 191 break; 192 case 'l': 193 if (--*dir == 0) 194 *dir = 8; 195 break; 196 case '1': case '2': case '3': case '4': 197 case '5': case '6': case '7': 198 moved++; 199 if (*dir % 2 == 0) 200 dist = dtab[*p - '0']; 201 else 202 dist = *p - '0'; 203 *row -= dr[*dir] * dist; 204 *col -= dc[*dir] * dist; 205 break; 206 } 207 } 208 if (!moved) { 209 if (windspeed != 0 && ++*drift > 2) { 210 if ((ship->specs->class >= 3 && !snagged(ship)) 211 || (turn & 1) == 0) { 212 *row -= dr[winddir]; 213 *col -= dc[winddir]; 214 } 215 } 216 } else 217 *drift = 0; 218 } 219 220 void 221 try(char command[], size_t commandl, char temp[], size_t templ, int ma, int ta, 222 int af, int vma, int dir, struct ship *f, struct ship *t, int *high, 223 int rakeme) 224 { 225 int new, n; 226 char st[4]; 227 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f)) 228 229 if ((n = str_end(temp)) < '1' || n > '9') 230 for (n = 1; vma - n >= 0; n++) { 231 (void) snprintf(st, sizeof st, "%d", n); 232 (void) strlcat(temp, st, templ); 233 new = score(temp, templ, f, t, rakeme); 234 if (new > *high && (!rakeme || rakeyou)) { 235 *high = new; 236 (void) strlcpy(command, temp, commandl); 237 } 238 try(command, commandl, temp, templ, ma-n, ta, af, vma-n, 239 dir, f, t, high, rakeme); 240 rmend(temp); 241 } 242 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) { 243 (void) strlcat(temp, "r", templ); 244 new = score(temp, templ, f, t, rakeme); 245 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) { 246 *high = new; 247 (void) strlcpy(command, temp, commandl); 248 } 249 try(command, commandl, temp, templ, 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); 250 rmend(temp); 251 } 252 if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){ 253 (void) strlcat(temp, "l", templ); 254 new = score(temp, templ, f, t, rakeme); 255 if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){ 256 *high = new; 257 (void) strlcpy(command, temp, commandl); 258 } 259 try(command, commandl, temp, templ, 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); 260 rmend(temp); 261 } 262 } 263 264 void 265 rmend(char *str) 266 { 267 char *p; 268 269 for (p = str; *p; p++) 270 ; 271 if (p != str) 272 *--p = 0; 273 } 274