X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/a6e8d8ed2cbb18fa9d9277395a46a38295b2d6bf..e51dd5d1fb713d15515575d8a5a600a11434a186:/sail/dr_2.c diff --git a/sail/dr_2.c b/sail/dr_2.c index 236faac7..40a24839 100644 --- a/sail/dr_2.c +++ b/sail/dr_2.c @@ -1,4 +1,4 @@ -/* $NetBSD: dr_2.c,v 1.13 2001/01/01 21:57:37 jwise Exp $ */ +/* $NetBSD: dr_2.c,v 1.27 2019/02/03 10:48:46 mrg Exp $ */ /* * Copyright (c) 1983, 1993 @@ -12,11 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -38,20 +34,36 @@ #if 0 static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: dr_2.c,v 1.13 2001/01/01 21:57:37 jwise Exp $"); +__RCSID("$NetBSD: dr_2.c,v 1.27 2019/02/03 10:48:46 mrg Exp $"); #endif #endif /* not lint */ -#include "driver.h" +#include +#include #include +#include +#include "extern.h" +#include "driver.h" #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5) +static int str_end(const char *); +static int score(struct ship *, struct ship *, char *, size_t, int); +static void move_ship(struct ship *, const char *, unsigned char *, + short *, short *, int *); +static void try(struct ship *f, struct ship *t, + char *command, size_t commandmax, char *temp, size_t tempmax, + int ma, int ta, bool af, int vma, int dir, int *high, + int rakeme); +static void rmend(char *); + +const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */ + void thinkofgrapples(void) { struct ship *sp, *sq; - char friendly; + bool friendly; foreachship(sp) { if (sp->file->captain[0] || sp->file->dir == 0) @@ -63,12 +75,12 @@ thinkofgrapples(void) continue; if (range(sp, sq) != 1) continue; - if (grappled2(sp, sq)) + if (grappled2(sp, sq)) { if (is_toughmelee(sp, sq, 0, 0)) ungrap(sp, sq); else grap(sp, sq); - else if (couldwin(sp, sq)) { + } else if (couldwin(sp, sq)) { grap(sp, sq); sp->file->loadwith = L_GRAPE; } @@ -93,19 +105,26 @@ checkup(void) continue; if (dieroll() < 5) continue; - Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0); - Write(W_DIR, sp, 0, 0, 0, 0); + if (sink == 1) { + send_sink(sp, 2); + } else { + send_explode(sp, 2); + } + send_dir(sp, 0); if (snagged(sp)) foreachship(sq) cleansnag(sp, sq, 1); if (sink != 1) { makemsg(sp, "exploding!"); foreachship(sq) { - if (sp != sq && sq->file->dir && range(sp, sq) < 4) - table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6); + if (sp != sq && sq->file->dir && + range(sp, sq) < 4) + table(sp, sq, RIGGING, L_EXPLODE, + sp->specs->guns/13, 6); } - } else + } else { makemsg(sp, "sinking!"); + } } } @@ -119,15 +138,18 @@ prizecheck(void) continue; if (sp->file->struck || sp->file->dir == 0) continue; - if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) { - Writestr(W_SIGNAL, sp, "prize crew overthrown"); - Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0); - Write(W_CAPTURED, sp, -1, 0, 0, 0); + if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > + sp->file->pcrew * 6) { + send_signal(sp, "prize crew overthrown"); + send_points(sp->file->captured, + sp->file->captured->file->points + - 2 * sp->specs->pts); + send_captured(sp, -1); } } } -int +static int str_end(const char *str) { const char *p; @@ -138,22 +160,23 @@ str_end(const char *str) } void -closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af) +closeon(struct ship *from, struct ship *to, char *command, size_t commandmax, + int ta, int ma, bool af) { int high; char temp[10]; temp[0] = command[0] = '\0'; high = -30000; - try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0); + try(from, to, command, commandmax, temp, sizeof(temp), + ma, ta, af, ma, from->file->dir, &high, 0); } -const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */ - -int -score(char *movement, struct ship *ship, struct ship *to, int onlytemp) +static int +score(struct ship *ship, struct ship *to, char *movement, size_t movementmax, + int onlytemp) { - char drift; + int drift; int row, col, dir, total, ran; struct File *fp = ship->file; @@ -162,9 +185,9 @@ score(char *movement, struct ship *ship, struct ship *to, int onlytemp) row = fp->row; col = fp->col; drift = fp->drift; - move_ship(movement, ship, &fp->dir, &fp->row, &fp->col, &drift); + move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift); if (!*movement) - strcpy(movement, "d"); + strlcpy(movement, "d", movementmax); ran = range(ship, to); total = -50 * ran; @@ -181,8 +204,9 @@ score(char *movement, struct ship *ship, struct ship *to, int onlytemp) return total; } -void -move_ship(const char *p, struct ship *ship, unsigned char *dir, short *row, short *col, char *drift) +static void +move_ship(struct ship *ship, const char *p, unsigned char *dir, + short *row, short *col, int *drift) { int dist; char moved = 0; @@ -221,49 +245,63 @@ move_ship(const char *p, struct ship *ship, unsigned char *dir, short *row, shor *drift = 0; } -void -try(char *command, char *temp, int ma, int ta, int af, int vma, int dir, struct ship *f, struct ship *t, int *high, int rakeme) +static void +try(struct ship *f, struct ship *t, + char *command, size_t commandmax, + char *temp, size_t tempmax, + int ma, int ta, bool af, int vma, int dir, int *high, int rakeme) { int new, n; - char st[4]; + char st[11]; #define rakeyou (gunsbear(f, t) && !gunsbear(t, f)) if ((n = str_end(temp)) < '1' || n > '9') for (n = 1; vma - n >= 0; n++) { - sprintf(st, "%d", n); - strcat(temp, st); - new = score(temp, f, t, rakeme); + snprintf(st, sizeof(st), "%d", n); + strlcat(temp, st, tempmax); + new = score(f, t, temp, tempmax, rakeme); if (new > *high && (!rakeme || rakeyou)) { *high = new; - strcpy(command, temp); + strlcpy(command, temp, commandmax); } - try(command, temp, ma-n, ta, af, vma-n, - dir, f, t, high, rakeme); + try(f, t, command, commandmax, temp, tempmax, + ma-n, ta, af, vma-n, + dir, high, rakeme); rmend(temp); } - if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) { - strcat(temp, "r"); - new = score(temp, f, t, rakeme); - if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) { + if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || + !strlen(temp)) { + strlcat(temp, "r", tempmax); + new = score(f, t, temp, tempmax, rakeme); + if (new > *high && (!rakeme || + (gunsbear(f, t) && !gunsbear(t, f)))) { *high = new; - strcpy(command, temp); + strlcpy(command, temp, commandmax); } - 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); + try(f, t, command, commandmax, temp, tempmax, + ma-1, ta-1, af, + min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), + (dir == 8 ? 1 : dir+1), high, rakeme); rmend(temp); } - if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){ - strcat(temp, "l"); - new = score(temp, f, t, rakeme); - if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){ + if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || + !strlen(temp)) { + strlcat(temp, "l", tempmax); + new = score(f, t, temp, tempmax, rakeme); + if (new > *high && (!rakeme || + (gunsbear(f, t) && !gunsbear(t, f)))) { *high = new; - strcpy(command, temp); + strlcpy(command, temp, commandmax); } - 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); + try(f, t, command, commandmax, temp, tempmax, + ma-1, ta-1, af, + (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), + (dir-1 ? dir -1 : 8), high, rakeme); rmend(temp); } } -void +static void rmend(char *str) { char *p;