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