-/* $NetBSD: dr_2.c,v 1.3 1995/04/22 10:36:48 cgd Exp $ */
+/* $NetBSD: dr_2.c,v 1.27 2019/02/03 10:48:46 mrg Exp $ */
/*
* Copyright (c) 1983, 1993
* 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.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: dr_2.c,v 1.3 1995/04/22 10:36:48 cgd Exp $";
+__RCSID("$NetBSD: dr_2.c,v 1.27 2019/02/03 10:48:46 mrg Exp $");
#endif
#endif /* not lint */
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "extern.h"
#include "driver.h"
#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
-thinkofgrapples()
+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)
{
- register struct ship *sp, *sq;
- char friendly;
+ struct ship *sp, *sq;
+ bool friendly;
foreachship(sp) {
if (sp->file->captain[0] || sp->file->dir == 0)
continue;
if (range(sp, sq) != 1)
continue;
- if (grappled2(sp, sq))
- if (toughmelee(sp, sq, 0, 0))
+ 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;
}
}
}
-checkup()
+void
+checkup(void)
{
- register struct ship *sp, *sq;
- register char explode, sink;
+ struct ship *sp, *sq;
+ char explode, sink;
foreachship(sp) {
if (sp->file->dir == 0)
sink = sp->file->sink;
if (explode != 1 && sink != 1)
continue;
- if (die() < 5)
+ if (dieroll() < 5)
continue;
- Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
- Write(W_DIR, sp, 0, 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) {
- makesignal(sp, "exploding!", (struct ship *)0);
+ 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
- makesignal(sp, "sinking!", (struct ship *)0);
+ } else {
+ makemsg(sp, "sinking!");
+ }
}
}
-prizecheck()
+void
+prizecheck(void)
{
- register struct ship *sp;
+ struct ship *sp;
foreachship(sp) {
if (sp->file->captured == 0)
continue;
if (sp->file->struck || sp->file->dir == 0)
continue;
- if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
- Write(W_SIGNAL, sp, 1,
- (int)"prize crew overthrown", 0, 0, 0);
- Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
- Write(W_CAPTURED, sp, 0, -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);
}
}
}
-strend(str)
-char *str;
+static int
+str_end(const char *str)
{
- register char *p;
+ const char *p;
for (p = str; *p; p++)
;
return p == str ? 0 : p[-1];
}
-closeon(from, to, command, ta, ma, af)
-register struct ship *from, *to;
-char command[];
-int ma, ta, af;
+void
+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);
}
-int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
-
-score(movement, ship, to, onlytemp)
-char movement[];
-register struct ship *ship, *to;
-char 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;
- register struct File *fp = ship->file;
+ struct File *fp = ship->file;
if ((dir = fp->dir) == 0)
return 0;
row = fp->row;
col = fp->col;
drift = fp->drift;
- move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
+ move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
if (!*movement)
- (void) strcpy(movement, "d");
+ strlcpy(movement, "d", movementmax);
ran = range(ship, to);
total = -50 * ran;
return total;
}
-move(p, ship, dir, row, col, drift)
-register char *p;
-register struct ship *ship;
-register char *dir;
-register short *row, *col;
-register 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;
}
if (!moved) {
if (windspeed != 0 && ++*drift > 2) {
- if (ship->specs->class >= 3 && !snagged(ship)
+ if ((ship->specs->class >= 3 && !snagged(ship))
|| (turn & 1) == 0) {
*row -= dr[winddir];
*col -= dc[winddir];
*drift = 0;
}
-try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
-register struct ship *f, *t;
-int ma, ta, af, *high, rakeme;
-char command[], temp[];
+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)
{
- register int new, n;
- char st[4];
+ int new, n;
+ char st[11];
#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
- if ((n = strend(temp)) < '1' || n > '9')
+ if ((n = str_end(temp)) < '1' || n > '9')
for (n = 1; vma - n >= 0; n++) {
- (void) sprintf(st, "%d", n);
- (void) 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;
- (void) 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 = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
- (void) 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;
- (void) 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 = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
- (void) 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;
- (void) 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);
}
}
-rmend(str)
-char *str;
+static void
+rmend(char *str)
{
- register char *p;
+ char *p;
for (p = str; *p; p++)
;