Home | History | Annotate | Line # | Download | only in sail
dr_2.c revision 1.17
      1  1.17     jwise /*	$NetBSD: dr_2.c,v 1.17 2001/01/04 06:16:51 jwise Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*
      4   1.3       cgd  * Copyright (c) 1983, 1993
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.5  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.3       cgd #if 0
     39   1.3       cgd static char sccsid[] = "@(#)dr_2.c	8.1 (Berkeley) 5/31/93";
     40   1.3       cgd #else
     41  1.17     jwise __RCSID("$NetBSD: dr_2.c,v 1.17 2001/01/04 06:16:51 jwise Exp $");
     42   1.3       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45  1.16     jwise #include <stdio.h>
     46  1.15     jwise #include <stdlib.h>
     47  1.16     jwise #include <string.h>
     48  1.15     jwise #include "extern.h"
     49   1.1       cgd #include "driver.h"
     50   1.1       cgd 
     51   1.1       cgd #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
     52   1.1       cgd 
     53  1.14     jwise void	thinkofgrapples(void);
     54  1.14     jwise void	checkup(void);
     55  1.14     jwise void	prizecheck(void);
     56  1.14     jwise static int	str_end(const char *);
     57  1.14     jwise void	closeon(struct ship *, struct ship *, char *, int, int, int);
     58  1.17     jwise static int	score(struct ship *, struct ship *, char *, int);
     59  1.17     jwise static void	move_ship(struct ship *, const char *, unsigned char *, short *, short *, char *);
     60  1.17     jwise static void	try(struct ship *, struct ship *, char *, char *, int, int, int, int, int, int *, int);
     61  1.14     jwise static void	rmend(char *);
     62  1.14     jwise 
     63  1.14     jwise const int dtab[] = {0,1,1,2,3,4,4,5};	/* diagonal distances in x==y */
     64  1.14     jwise 
     65   1.5  christos void
     66  1.13     jwise thinkofgrapples(void)
     67   1.1       cgd {
     68   1.5  christos 	struct ship *sp, *sq;
     69   1.1       cgd 	char friendly;
     70   1.1       cgd 
     71   1.1       cgd 	foreachship(sp) {
     72   1.1       cgd 		if (sp->file->captain[0] || sp->file->dir == 0)
     73   1.1       cgd 			continue;
     74   1.1       cgd 		foreachship(sq) {
     75   1.1       cgd 			friendly = sp->nationality == capship(sq)->nationality;
     76   1.1       cgd 			if (!friendly) {
     77   1.1       cgd 				if (sp->file->struck || sp->file->captured != 0)
     78   1.1       cgd 					continue;
     79   1.1       cgd 				if (range(sp, sq) != 1)
     80   1.1       cgd 					continue;
     81   1.1       cgd 				if (grappled2(sp, sq))
     82  1.11       jsm 					if (is_toughmelee(sp, sq, 0, 0))
     83   1.1       cgd 						ungrap(sp, sq);
     84   1.1       cgd 					else
     85   1.1       cgd 						grap(sp, sq);
     86   1.1       cgd 				else if (couldwin(sp, sq)) {
     87   1.1       cgd 					grap(sp, sq);
     88   1.1       cgd 					sp->file->loadwith = L_GRAPE;
     89   1.1       cgd 				}
     90   1.1       cgd 			} else
     91   1.1       cgd 				ungrap(sp, sq);
     92   1.1       cgd 		}
     93   1.1       cgd 	}
     94   1.1       cgd }
     95   1.1       cgd 
     96   1.5  christos void
     97  1.13     jwise checkup(void)
     98   1.1       cgd {
     99   1.5  christos 	struct ship *sp, *sq;
    100   1.5  christos 	char explode, sink;
    101   1.1       cgd 
    102   1.1       cgd 	foreachship(sp) {
    103   1.1       cgd 		if (sp->file->dir == 0)
    104   1.1       cgd 			continue;
    105   1.1       cgd 		explode = sp->file->explode;
    106   1.1       cgd 		sink = sp->file->sink;
    107   1.1       cgd 		if (explode != 1 && sink != 1)
    108   1.1       cgd 			continue;
    109  1.12     jwise 		if (dieroll() < 5)
    110   1.1       cgd 			continue;
    111   1.9   hubertf 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 2, 0, 0, 0);
    112   1.9   hubertf 		Write(W_DIR, sp, 0, 0, 0, 0);
    113   1.1       cgd 		if (snagged(sp))
    114   1.1       cgd 			foreachship(sq)
    115   1.1       cgd 				cleansnag(sp, sq, 1);
    116   1.1       cgd 		if (sink != 1) {
    117   1.6  christos 			makemsg(sp, "exploding!");
    118   1.1       cgd 			foreachship(sq) {
    119   1.1       cgd 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
    120  1.17     jwise 					table(sp, sq, RIGGING, L_EXPLODE, sp->specs->guns/13, 6);
    121   1.1       cgd 			}
    122   1.1       cgd 		} else
    123   1.6  christos 			makemsg(sp, "sinking!");
    124   1.1       cgd 	}
    125   1.1       cgd }
    126   1.1       cgd 
    127   1.5  christos void
    128  1.13     jwise prizecheck(void)
    129   1.1       cgd {
    130   1.5  christos 	struct ship *sp;
    131   1.1       cgd 
    132   1.1       cgd 	foreachship(sp) {
    133   1.1       cgd 		if (sp->file->captured == 0)
    134   1.1       cgd 			continue;
    135   1.1       cgd 		if (sp->file->struck || sp->file->dir == 0)
    136   1.1       cgd 			continue;
    137   1.1       cgd 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
    138   1.9   hubertf 			Writestr(W_SIGNAL, sp, "prize crew overthrown");
    139   1.9   hubertf 			Write(W_POINTS, sp->file->captured, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
    140   1.9   hubertf 			Write(W_CAPTURED, sp, -1, 0, 0, 0);
    141   1.1       cgd 		}
    142   1.1       cgd 	}
    143   1.1       cgd }
    144   1.1       cgd 
    145  1.14     jwise static int
    146  1.13     jwise str_end(const char *str)
    147   1.1       cgd {
    148  1.10       jsm 	const char *p;
    149   1.1       cgd 
    150   1.1       cgd 	for (p = str; *p; p++)
    151   1.1       cgd 		;
    152   1.1       cgd 	return p == str ? 0 : p[-1];
    153   1.1       cgd }
    154   1.1       cgd 
    155   1.5  christos void
    156  1.13     jwise closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af)
    157   1.1       cgd {
    158   1.1       cgd 	int high;
    159   1.1       cgd 	char temp[10];
    160   1.1       cgd 
    161   1.1       cgd 	temp[0] = command[0] = '\0';
    162   1.1       cgd 	high = -30000;
    163  1.17     jwise 	try(from, to, command, temp, ma, ta, af, ma, from->file->dir, &high, 0);
    164   1.1       cgd }
    165   1.1       cgd 
    166  1.14     jwise static int
    167  1.17     jwise score(struct ship *ship, struct ship *to, char *movement, int onlytemp)
    168   1.1       cgd {
    169   1.1       cgd 	char drift;
    170   1.1       cgd 	int row, col, dir, total, ran;
    171   1.5  christos 	struct File *fp = ship->file;
    172   1.1       cgd 
    173   1.1       cgd 	if ((dir = fp->dir) == 0)
    174   1.1       cgd 		return 0;
    175   1.1       cgd 	row = fp->row;
    176   1.1       cgd 	col = fp->col;
    177   1.1       cgd 	drift = fp->drift;
    178  1.17     jwise 	move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
    179   1.1       cgd 	if (!*movement)
    180  1.13     jwise 		strcpy(movement, "d");
    181   1.1       cgd 
    182   1.1       cgd 	ran = range(ship, to);
    183   1.1       cgd 	total = -50 * ran;
    184   1.1       cgd 	if (ran < 4 && gunsbear(ship, to))
    185   1.1       cgd 		total += 60;
    186   1.1       cgd 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
    187   1.1       cgd 		total = -30000;
    188   1.1       cgd 
    189   1.1       cgd 	if (!onlytemp) {
    190   1.1       cgd 		fp->row = row;
    191   1.1       cgd 		fp->col = col;
    192   1.1       cgd 		fp->dir = dir;
    193   1.1       cgd 	}
    194   1.1       cgd 	return total;
    195   1.1       cgd }
    196   1.1       cgd 
    197  1.14     jwise static void
    198  1.17     jwise move_ship(struct ship *ship, const char *p, unsigned char *dir, short *row, short *col, char *drift)
    199   1.1       cgd {
    200   1.1       cgd 	int dist;
    201   1.1       cgd 	char moved = 0;
    202   1.1       cgd 
    203   1.1       cgd 	for (; *p; p++) {
    204   1.1       cgd 		switch (*p) {
    205   1.1       cgd 		case 'r':
    206   1.1       cgd 			if (++*dir == 9)
    207   1.1       cgd 				*dir = 1;
    208   1.1       cgd 			break;
    209   1.1       cgd 		case 'l':
    210   1.1       cgd 			if (--*dir == 0)
    211   1.1       cgd 				*dir = 8;
    212   1.1       cgd 			break;
    213   1.1       cgd 		case '1': case '2': case '3': case '4':
    214   1.1       cgd 		case '5': case '6': case '7':
    215   1.1       cgd 			moved++;
    216   1.1       cgd 			if (*dir % 2 == 0)
    217   1.1       cgd 				dist = dtab[*p - '0'];
    218   1.1       cgd 			else
    219   1.1       cgd 				dist = *p - '0';
    220   1.1       cgd 			*row -= dr[*dir] * dist;
    221   1.1       cgd 			*col -= dc[*dir] * dist;
    222   1.1       cgd 			break;
    223   1.1       cgd 		}
    224   1.1       cgd 	}
    225   1.1       cgd 	if (!moved) {
    226   1.1       cgd 		if (windspeed != 0 && ++*drift > 2) {
    227   1.5  christos 			if ((ship->specs->class >= 3 && !snagged(ship))
    228   1.1       cgd 			    || (turn & 1) == 0) {
    229   1.1       cgd 				*row -= dr[winddir];
    230   1.1       cgd 				*col -= dc[winddir];
    231   1.1       cgd 			}
    232   1.1       cgd 		}
    233   1.1       cgd 	} else
    234   1.1       cgd 		*drift = 0;
    235   1.1       cgd }
    236   1.1       cgd 
    237  1.14     jwise static void
    238  1.17     jwise try(struct ship *f, struct ship *t, char *command, char *temp, int ma, int ta, int af, int vma, int dir, int *high, int rakeme)
    239   1.1       cgd {
    240   1.5  christos 	int new, n;
    241   1.1       cgd 	char st[4];
    242   1.1       cgd #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
    243   1.1       cgd 
    244  1.11       jsm 	if ((n = str_end(temp)) < '1' || n > '9')
    245   1.1       cgd 		for (n = 1; vma - n >= 0; n++) {
    246  1.13     jwise 			sprintf(st, "%d", n);
    247  1.13     jwise 			strcat(temp, st);
    248  1.17     jwise 			new = score(f, t, temp, rakeme);
    249   1.1       cgd 			if (new > *high && (!rakeme || rakeyou)) {
    250   1.1       cgd 				*high = new;
    251  1.13     jwise 				strcpy(command, temp);
    252   1.1       cgd 			}
    253  1.17     jwise 			try(f, t, command, temp, ma-n, ta, af, vma-n,
    254  1.17     jwise 				dir, high, rakeme);
    255   1.1       cgd 			rmend(temp);
    256   1.1       cgd 		}
    257  1.11       jsm 	if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
    258  1.13     jwise 		strcat(temp, "r");
    259  1.17     jwise 		new = score(f, t, temp, rakeme);
    260   1.5  christos 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
    261   1.1       cgd 			*high = new;
    262  1.13     jwise 			strcpy(command, temp);
    263   1.1       cgd 		}
    264  1.17     jwise 		try(f, t, command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1), high, rakeme);
    265   1.1       cgd 		rmend(temp);
    266   1.1       cgd 	}
    267  1.11       jsm 	if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
    268  1.13     jwise 		strcat(temp, "l");
    269  1.17     jwise 		new = score(f, t, temp, rakeme);
    270   1.1       cgd 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
    271   1.1       cgd 			*high = new;
    272  1.13     jwise 			strcpy(command, temp);
    273   1.1       cgd 		}
    274  1.17     jwise 		try(f, t, command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), high, rakeme);
    275   1.1       cgd 		rmend(temp);
    276   1.1       cgd 	}
    277   1.1       cgd }
    278   1.1       cgd 
    279  1.14     jwise static void
    280  1.13     jwise rmend(char *str)
    281   1.1       cgd {
    282   1.5  christos 	char *p;
    283   1.1       cgd 
    284   1.1       cgd 	for (p = str; *p; p++)
    285   1.1       cgd 		;
    286   1.1       cgd 	if (p != str)
    287   1.1       cgd 		*--p = 0;
    288   1.1       cgd }
    289