1999-02-10 03:45:45 +03:00
|
|
|
/* $NetBSD: dr_3.c,v 1.6 1999/02/10 00:45:45 hubertf 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.
|
|
|
|
* 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
|
|
|
|
* 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_3.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
1999-02-10 03:45:45 +03:00
|
|
|
__RCSID("$NetBSD: dr_3.c,v 1.6 1999/02/10 00:45:45 hubertf Exp $");
|
1995-04-22 14:36:43 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "driver.h"
|
1997-10-13 23:42:53 +04:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
moveall() /* move all comp ships */
|
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp, *sq;
|
|
|
|
int n;
|
|
|
|
int k, l;
|
1993-03-21 12:45:37 +03:00
|
|
|
int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
|
|
|
|
char moved[NSHIP];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first try to create moves for OUR ships
|
|
|
|
*/
|
|
|
|
foreachship(sp) {
|
|
|
|
struct ship *closest;
|
|
|
|
int ma, ta;
|
|
|
|
char af;
|
|
|
|
|
|
|
|
if (sp->file->captain[0] || sp->file->dir == 0)
|
|
|
|
continue;
|
|
|
|
if (!sp->file->struck && windspeed && !snagged(sp)
|
|
|
|
&& sp->specs->crew3) {
|
|
|
|
ta = maxturns(sp, &af);
|
|
|
|
ma = maxmove(sp, sp->file->dir, 0);
|
|
|
|
closest = closestenemy(sp, 0, 0);
|
|
|
|
if (closest == 0)
|
|
|
|
*sp->file->movebuf = '\0';
|
|
|
|
else
|
|
|
|
closeon(sp, closest, sp->file->movebuf,
|
|
|
|
ta, ma, af);
|
|
|
|
} else
|
|
|
|
*sp->file->movebuf = '\0';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Then execute the moves for ALL ships (dead ones too),
|
|
|
|
* checking for collisions and snags at each step.
|
|
|
|
* The old positions are saved in row[], col[], dir[].
|
|
|
|
* At the end, we compare and write out the changes.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
foreachship(sp) {
|
|
|
|
if (snagged(sp))
|
|
|
|
(void) strcpy(sp->file->movebuf, "d");
|
|
|
|
else
|
|
|
|
if (*sp->file->movebuf != 'd')
|
|
|
|
(void) strcat(sp->file->movebuf, "d");
|
|
|
|
row[n] = sp->file->row;
|
|
|
|
col[n] = sp->file->col;
|
|
|
|
dir[n] = sp->file->dir;
|
|
|
|
drift[n] = sp->file->drift;
|
|
|
|
moved[n] = 0;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now resolve collisions.
|
|
|
|
* This is the tough part.
|
|
|
|
*/
|
|
|
|
for (k = 0; stillmoving(k); k++) {
|
|
|
|
/*
|
|
|
|
* Step once.
|
|
|
|
* And propagate the nulls at the end of sp->file->movebuf.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
foreachship(sp) {
|
|
|
|
if (!sp->file->movebuf[k])
|
|
|
|
sp->file->movebuf[k+1] = '\0';
|
|
|
|
else if (sp->file->dir)
|
|
|
|
step(sp->file->movebuf[k], sp, &moved[n]);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The real stuff.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->dir == 0 || isolated(sp))
|
|
|
|
goto cont1;
|
|
|
|
l = 0;
|
|
|
|
foreachship(sq) {
|
|
|
|
char snap = 0;
|
|
|
|
|
|
|
|
if (sp == sq)
|
|
|
|
goto cont2;
|
|
|
|
if (sq->file->dir == 0)
|
|
|
|
goto cont2;
|
|
|
|
if (!push(sp, sq))
|
|
|
|
goto cont2;
|
|
|
|
if (snagged2(sp, sq) && range(sp, sq) > 1)
|
|
|
|
snap++;
|
|
|
|
if (!range(sp, sq) && !fouled2(sp, sq)) {
|
1997-10-14 01:02:57 +04:00
|
|
|
makesignal(sp, "collision with $$", sq);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (die() < 4) {
|
1997-10-14 01:02:57 +04:00
|
|
|
makesignal(sp, "fouled with $$",
|
|
|
|
sq);
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_FOUL, sp, l, 0, 0, 0);
|
|
|
|
Write(W_FOUL, sq, n, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
snap++;
|
|
|
|
}
|
|
|
|
if (snap) {
|
|
|
|
sp->file->movebuf[k + 1] = 0;
|
|
|
|
sq->file->movebuf[k + 1] = 0;
|
|
|
|
sq->file->row = sp->file->row - 1;
|
|
|
|
if (sp->file->dir == 1
|
|
|
|
|| sp->file->dir == 5)
|
|
|
|
sq->file->col =
|
|
|
|
sp->file->col - 1;
|
|
|
|
else
|
|
|
|
sq->file->col = sp->file->col;
|
|
|
|
sq->file->dir = sp->file->dir;
|
|
|
|
}
|
|
|
|
cont2:
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
cont1:
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Clear old moves. And write out new pos.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->dir != 0) {
|
|
|
|
*sp->file->movebuf = 0;
|
|
|
|
if (row[n] != sp->file->row)
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_ROW, sp, sp->file->row, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (col[n] != sp->file->col)
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_COL, sp, sp->file->col, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (dir[n] != sp->file->dir)
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (drift[n] != sp->file->drift)
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
stillmoving(k)
|
1997-10-13 23:42:53 +04:00
|
|
|
int k;
|
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->movebuf[k])
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
isolated(ship)
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *ship;
|
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 (ship != sp && range(ship, sp) <= 10)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
push(from, to)
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *from, *to;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
int bs, sb;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
sb = to->specs->guns;
|
|
|
|
bs = from->specs->guns;
|
|
|
|
if (sb > bs)
|
|
|
|
return 1;
|
|
|
|
if (sb < bs)
|
|
|
|
return 0;
|
|
|
|
return from < to;
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
step(com, sp, moved)
|
|
|
|
char com;
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *moved;
|
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
int dist;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch (com) {
|
|
|
|
case 'r':
|
|
|
|
if (++sp->file->dir == 9)
|
|
|
|
sp->file->dir = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (--sp->file->dir == 0)
|
|
|
|
sp->file->dir = 8;
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6': case '7':
|
|
|
|
if (sp->file->dir % 2 == 0)
|
|
|
|
dist = dtab[com - '0'];
|
|
|
|
else
|
|
|
|
dist = com - '0';
|
|
|
|
sp->file->row -= dr[sp->file->dir] * dist;
|
|
|
|
sp->file->col -= dc[sp->file->dir] * dist;
|
|
|
|
*moved = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (!*moved) {
|
|
|
|
if (windspeed != 0 && ++sp->file->drift > 2 &&
|
1997-10-13 23:42:53 +04:00
|
|
|
((sp->specs->class >= 3 && !snagged(sp))
|
1993-03-21 12:45:37 +03:00
|
|
|
|| (turn & 1) == 0)) {
|
|
|
|
sp->file->row -= dr[winddir];
|
|
|
|
sp->file->col -= dc[winddir];
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
sp->file->drift = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
sendbp(from, to, sections, isdefense)
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *from, *to;
|
1993-03-21 12:45:37 +03:00
|
|
|
int sections;
|
|
|
|
char isdefense;
|
|
|
|
{
|
|
|
|
int n;
|
1997-10-13 23:42:53 +04:00
|
|
|
struct BP *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
bp = isdefense ? from->file->DBP : from->file->OBP;
|
|
|
|
for (n = 0; n < NBP && bp[n].turnsent; n++)
|
|
|
|
;
|
|
|
|
if (n < NBP && sections) {
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(isdefense ? W_DBP : W_OBP, from,
|
1993-03-21 12:45:37 +03:00
|
|
|
n, turn, to->file->index, sections);
|
|
|
|
if (isdefense)
|
1997-10-14 01:02:57 +04:00
|
|
|
makemsg(from, "repelling boarders");
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1997-10-14 01:02:57 +04:00
|
|
|
makesignal(from, "boarding the $$", to);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
toughmelee(ship, to, isdefense, count)
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *ship, *to;
|
1993-03-21 12:45:37 +03:00
|
|
|
int isdefense, count;
|
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct BP *bp;
|
|
|
|
int obp = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
int n, OBP = 0, DBP = 0, dbp = 0;
|
|
|
|
int qual;
|
|
|
|
|
|
|
|
qual = ship->specs->qual;
|
|
|
|
bp = isdefense ? ship->file->DBP : ship->file->OBP;
|
|
|
|
for (n = 0; n < NBP; n++, bp++) {
|
|
|
|
if (bp->turnsent && (to == bp->toship || isdefense)) {
|
|
|
|
obp += bp->mensent / 100
|
|
|
|
? ship->specs->crew1 * qual : 0;
|
|
|
|
obp += (bp->mensent % 100)/10
|
|
|
|
? ship->specs->crew2 * qual : 0;
|
|
|
|
obp += bp->mensent % 10
|
|
|
|
? ship->specs->crew3 * qual : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count || isdefense)
|
|
|
|
return obp;
|
|
|
|
OBP = toughmelee(to, ship, 0, count + 1);
|
|
|
|
dbp = toughmelee(ship, to, 1, count + 1);
|
|
|
|
DBP = toughmelee(to, ship, 1, count + 1);
|
|
|
|
if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
reload()
|
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
foreachship(sp) {
|
|
|
|
sp->file->loadwith = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-13 23:42:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
checksails()
|
|
|
|
{
|
1997-10-13 23:42:53 +04:00
|
|
|
struct ship *sp;
|
|
|
|
int rig, full;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct ship *close;
|
|
|
|
|
|
|
|
foreachship(sp) {
|
|
|
|
if (sp->file->captain[0] != 0)
|
|
|
|
continue;
|
|
|
|
rig = sp->specs->rig1;
|
1997-10-13 23:42:53 +04:00
|
|
|
if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
|
1993-03-21 12:45:37 +03:00
|
|
|
rig = 0;
|
|
|
|
if (rig && sp->specs->crew3) {
|
|
|
|
close = closestenemy(sp, 0, 0);
|
|
|
|
if (close != 0) {
|
|
|
|
if (range(sp, close) > 9)
|
|
|
|
full = 1;
|
|
|
|
else
|
|
|
|
full = 0;
|
|
|
|
} else
|
|
|
|
full = 0;
|
|
|
|
} else
|
|
|
|
full = 0;
|
|
|
|
if ((sp->file->FS != 0) != full)
|
1999-02-10 03:45:45 +03:00
|
|
|
Write(W_FS, sp, full, 0, 0, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|