2009-03-15 01:52:52 +03:00
|
|
|
/* $NetBSD: dr_2.c,v 1.25 2009/03/14 22:52:52 dholland Exp $ */
|
1995-04-22 14:36:43 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-04-22 14:36:43 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 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.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-04-22 14:36:43 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2009-03-15 01:52:52 +03:00
|
|
|
__RCSID("$NetBSD: dr_2.c,v 1.25 2009/03/14 22:52:52 dholland Exp $");
|
1995-04-22 14:36:43 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2009-03-14 20:10:01 +03:00
|
|
|
#include <stdbool.h>
|
2001-01-04 06:51:23 +03:00
|
|
|
#include <stdio.h>
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <stdlib.h>
|
2001-01-04 06:51:23 +03:00
|
|
|
#include <string.h>
|
2001-01-04 06:21:16 +03:00
|
|
|
#include "extern.h"
|
|
|
|
#include "driver.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
|
|
|
|
|
2009-03-14 22:35:13 +03:00
|
|
|
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,
|
2009-03-14 23:04:43 +03:00
|
|
|
int ma, int ta, bool af, int vma, int dir, int *high,
|
2009-03-14 22:35:13 +03:00
|
|
|
int rakeme);
|
|
|
|
static void rmend(char *);
|
2001-01-04 04:53:24 +03:00
|
|
|
|
|
|
|
const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
2001-01-02 00:57:37 +03:00
|
|
|
thinkofgrapples(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp, *sq;
|
2009-03-14 20:10:01 +03:00
|
|
|
bool friendly;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->captain[0] || sp->file->dir == 0)
|
|
|
|
continue;
|
|
|
|
foreachship(sq) {
|
|
|
|
friendly = sp->nationality == capship(sq)->nationality;
|
|
|
|
if (!friendly) {
|
|
|
|
if (sp->file->struck || sp->file->captured != 0)
|
|
|
|
continue;
|
|
|
|
if (range(sp, sq) != 1)
|
|
|
|
continue;
|
2009-03-02 09:43:53 +03:00
|
|
|
if (grappled2(sp, sq)) {
|
1999-09-30 22:01:31 +04:00
|
|
|
if (is_toughmelee(sp, sq, 0, 0))
|
1993-03-21 12:45:37 +03:00
|
|
|
ungrap(sp, sq);
|
|
|
|
else
|
|
|
|
grap(sp, sq);
|
2009-03-02 09:43:53 +03:00
|
|
|
} else if (couldwin(sp, sq)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
grap(sp, sq);
|
|
|
|
sp->file->loadwith = L_GRAPE;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ungrap(sp, sq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
2001-01-02 00:57:37 +03:00
|
|
|
checkup(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp, *sq;
|
|
|
|
char explode, sink;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->dir == 0)
|
|
|
|
continue;
|
|
|
|
explode = sp->file->explode;
|
|
|
|
sink = sp->file->sink;
|
|
|
|
if (explode != 1 && sink != 1)
|
|
|
|
continue;
|
2000-12-01 01:02:20 +03:00
|
|
|
if (dieroll() < 5)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
2009-03-15 01:52:52 +03:00
|
|
|
if (sink == 1) {
|
|
|
|
send_sink(sp, 2);
|
|
|
|
} else {
|
|
|
|
send_explode(sp, 2);
|
|
|
|
}
|
|
|
|
send_dir(sp, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (snagged(sp))
|
|
|
|
foreachship(sq)
|
|
|
|
cleansnag(sp, sq, 1);
|
|
|
|
if (sink != 1) {
|
1997-10-14 01:02:57 +04:00
|
|
|
makemsg(sp, "exploding!");
|
1993-03-21 12:45:37 +03:00
|
|
|
foreachship(sq) {
|
2009-03-14 22:35:13 +03:00
|
|
|
if (sp != sq && sq->file->dir &&
|
|
|
|
range(sp, sq) < 4)
|
|
|
|
table(sp, sq, RIGGING, L_EXPLODE,
|
|
|
|
sp->specs->guns/13, 6);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
} else {
|
1997-10-14 01:02:57 +04:00
|
|
|
makemsg(sp, "sinking!");
|
2009-03-14 22:35:13 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
2001-01-02 00:57:37 +03:00
|
|
|
prizecheck(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->captured == 0)
|
|
|
|
continue;
|
|
|
|
if (sp->file->struck || sp->file->dir == 0)
|
|
|
|
continue;
|
2009-03-14 22:35:13 +03:00
|
|
|
if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 >
|
|
|
|
sp->file->pcrew * 6) {
|
2009-03-15 01:52:52 +03:00
|
|
|
send_signal(sp, "prize crew overthrown");
|
|
|
|
send_points(sp->file->captured,
|
2009-03-14 22:35:13 +03:00
|
|
|
sp->file->captured->file->points
|
2009-03-15 01:52:52 +03:00
|
|
|
- 2 * sp->specs->pts);
|
|
|
|
send_captured(sp, -1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 04:53:24 +03:00
|
|
|
static int
|
2001-01-02 00:57:37 +03:00
|
|
|
str_end(const char *str)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (p = str; *p; p++)
|
|
|
|
;
|
|
|
|
return p == str ? 0 : p[-1];
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
2009-03-14 22:35:13 +03:00
|
|
|
closeon(struct ship *from, struct ship *to, char *command, size_t commandmax,
|
2009-03-14 23:04:43 +03:00
|
|
|
int ta, int ma, bool af)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int high;
|
|
|
|
char temp[10];
|
|
|
|
|
|
|
|
temp[0] = command[0] = '\0';
|
|
|
|
high = -30000;
|
2009-03-14 22:35:13 +03:00
|
|
|
try(from, to, command, commandmax, temp, sizeof(temp),
|
|
|
|
ma, ta, af, ma, from->file->dir, &high, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2001-01-04 04:53:24 +03:00
|
|
|
static int
|
2009-03-14 22:35:13 +03:00
|
|
|
score(struct ship *ship, struct ship *to, char *movement, size_t movementmax,
|
|
|
|
int onlytemp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-03-14 20:10:01 +03:00
|
|
|
int drift;
|
1993-03-21 12:45:37 +03:00
|
|
|
int row, col, dir, total, ran;
|
1997-10-13 23:42:53 +04:00
|
|
|
struct File *fp = ship->file;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((dir = fp->dir) == 0)
|
|
|
|
return 0;
|
|
|
|
row = fp->row;
|
|
|
|
col = fp->col;
|
|
|
|
drift = fp->drift;
|
2001-01-04 09:16:51 +03:00
|
|
|
move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!*movement)
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcpy(movement, "d", movementmax);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ran = range(ship, to);
|
|
|
|
total = -50 * ran;
|
|
|
|
if (ran < 4 && gunsbear(ship, to))
|
|
|
|
total += 60;
|
|
|
|
if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
|
|
|
|
total = -30000;
|
|
|
|
|
|
|
|
if (!onlytemp) {
|
|
|
|
fp->row = row;
|
|
|
|
fp->col = col;
|
|
|
|
fp->dir = dir;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2001-01-04 04:53:24 +03:00
|
|
|
static void
|
2009-03-14 22:35:13 +03:00
|
|
|
move_ship(struct ship *ship, const char *p, unsigned char *dir,
|
|
|
|
short *row, short *col, int *drift)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int dist;
|
|
|
|
char moved = 0;
|
|
|
|
|
|
|
|
for (; *p; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case 'r':
|
|
|
|
if (++*dir == 9)
|
|
|
|
*dir = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (--*dir == 0)
|
|
|
|
*dir = 8;
|
|
|
|
break;
|
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7':
|
|
|
|
moved++;
|
|
|
|
if (*dir % 2 == 0)
|
|
|
|
dist = dtab[*p - '0'];
|
|
|
|
else
|
|
|
|
dist = *p - '0';
|
|
|
|
*row -= dr[*dir] * dist;
|
|
|
|
*col -= dc[*dir] * dist;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!moved) {
|
|
|
|
if (windspeed != 0 && ++*drift > 2) {
|
1997-10-13 23:42:53 +04:00
|
|
|
if ((ship->specs->class >= 3 && !snagged(ship))
|
1993-03-21 12:45:37 +03:00
|
|
|
|| (turn & 1) == 0) {
|
|
|
|
*row -= dr[winddir];
|
|
|
|
*col -= dc[winddir];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*drift = 0;
|
|
|
|
}
|
|
|
|
|
2001-01-04 04:53:24 +03:00
|
|
|
static void
|
2009-03-14 22:35:13 +03:00
|
|
|
try(struct ship *f, struct ship *t,
|
|
|
|
char *command, size_t commandmax,
|
|
|
|
char *temp, size_t tempmax,
|
2009-03-14 23:04:43 +03:00
|
|
|
int ma, int ta, bool af, int vma, int dir, int *high, int rakeme)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
int new, n;
|
1993-03-21 12:45:37 +03:00
|
|
|
char st[4];
|
|
|
|
#define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
|
|
|
|
|
1999-09-30 22:01:31 +04:00
|
|
|
if ((n = str_end(temp)) < '1' || n > '9')
|
1993-03-21 12:45:37 +03:00
|
|
|
for (n = 1; vma - n >= 0; n++) {
|
2009-03-14 21:32:47 +03:00
|
|
|
snprintf(st, sizeof(st), "%d", n);
|
|
|
|
strlcat(temp, st, tempmax);
|
|
|
|
new = score(f, t, temp, tempmax, rakeme);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (new > *high && (!rakeme || rakeyou)) {
|
|
|
|
*high = new;
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcpy(command, temp, commandmax);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
try(f, t, command, commandmax, temp, tempmax,
|
|
|
|
ma-n, ta, af, vma-n,
|
|
|
|
dir, high, rakeme);
|
1993-03-21 12:45:37 +03:00
|
|
|
rmend(temp);
|
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') ||
|
|
|
|
!strlen(temp)) {
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcat(temp, "r", tempmax);
|
|
|
|
new = score(f, t, temp, tempmax, rakeme);
|
2009-03-14 22:35:13 +03:00
|
|
|
if (new > *high && (!rakeme ||
|
|
|
|
(gunsbear(f, t) && !gunsbear(t, f)))) {
|
1993-03-21 12:45:37 +03:00
|
|
|
*high = new;
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcpy(command, temp, commandmax);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
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);
|
1993-03-21 12:45:37 +03:00
|
|
|
rmend(temp);
|
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') ||
|
|
|
|
!strlen(temp)) {
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcat(temp, "l", sizeof(temp));
|
|
|
|
new = score(f, t, temp, tempmax, rakeme);
|
2009-03-14 22:35:13 +03:00
|
|
|
if (new > *high && (!rakeme ||
|
|
|
|
(gunsbear(f, t) && !gunsbear(t, f)))) {
|
1993-03-21 12:45:37 +03:00
|
|
|
*high = new;
|
2009-03-14 21:32:47 +03:00
|
|
|
strlcpy(command, temp, commandmax);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-03-14 22:35:13 +03:00
|
|
|
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);
|
1993-03-21 12:45:37 +03:00
|
|
|
rmend(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-04 04:53:24 +03:00
|
|
|
static void
|
2001-01-02 00:57:37 +03:00
|
|
|
rmend(char *str)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (p = str; *p; p++)
|
|
|
|
;
|
|
|
|
if (p != str)
|
|
|
|
*--p = 0;
|
|
|
|
}
|