Home | History | Annotate | Line # | Download | only in sail
dr_2.c revision 1.3
      1 /*	$NetBSD: dr_2.c,v 1.3 1995/04/22 10:36:48 cgd 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)dr_2.c	8.1 (Berkeley) 5/31/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: dr_2.c,v 1.3 1995/04/22 10:36:48 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include "driver.h"
     45 
     46 #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
     47 
     48 thinkofgrapples()
     49 {
     50 	register struct ship *sp, *sq;
     51 	char friendly;
     52 
     53 	foreachship(sp) {
     54 		if (sp->file->captain[0] || sp->file->dir == 0)
     55 			continue;
     56 		foreachship(sq) {
     57 			friendly = sp->nationality == capship(sq)->nationality;
     58 			if (!friendly) {
     59 				if (sp->file->struck || sp->file->captured != 0)
     60 					continue;
     61 				if (range(sp, sq) != 1)
     62 					continue;
     63 				if (grappled2(sp, sq))
     64 					if (toughmelee(sp, sq, 0, 0))
     65 						ungrap(sp, sq);
     66 					else
     67 						grap(sp, sq);
     68 				else if (couldwin(sp, sq)) {
     69 					grap(sp, sq);
     70 					sp->file->loadwith = L_GRAPE;
     71 				}
     72 			} else
     73 				ungrap(sp, sq);
     74 		}
     75 	}
     76 }
     77 
     78 checkup()
     79 {
     80 	register struct ship *sp, *sq;
     81 	register char explode, sink;
     82 
     83 	foreachship(sp) {
     84 		if (sp->file->dir == 0)
     85 			continue;
     86 		explode = sp->file->explode;
     87 		sink = sp->file->sink;
     88 		if (explode != 1 && sink != 1)
     89 			continue;
     90 		if (die() < 5)
     91 			continue;
     92 		Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
     93 		Write(W_DIR, sp, 0, 0, 0, 0, 0);
     94 		if (snagged(sp))
     95 			foreachship(sq)
     96 				cleansnag(sp, sq, 1);
     97 		if (sink != 1) {
     98 			makesignal(sp, "exploding!", (struct ship *)0);
     99 			foreachship(sq) {
    100 				if (sp != sq && sq->file->dir && range(sp, sq) < 4)
    101 					table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
    102 			}
    103 		} else
    104 			makesignal(sp, "sinking!", (struct ship *)0);
    105 	}
    106 }
    107 
    108 prizecheck()
    109 {
    110 	register struct ship *sp;
    111 
    112 	foreachship(sp) {
    113 		if (sp->file->captured == 0)
    114 			continue;
    115 		if (sp->file->struck || sp->file->dir == 0)
    116 			continue;
    117 		if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
    118 			Write(W_SIGNAL, sp, 1,
    119 				(int)"prize crew overthrown", 0, 0, 0);
    120 			Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
    121 			Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
    122 		}
    123 	}
    124 }
    125 
    126 strend(str)
    127 char *str;
    128 {
    129 	register char *p;
    130 
    131 	for (p = str; *p; p++)
    132 		;
    133 	return p == str ? 0 : p[-1];
    134 }
    135 
    136 closeon(from, to, command, ta, ma, af)
    137 register struct ship *from, *to;
    138 char command[];
    139 int ma, ta, af;
    140 {
    141 	int high;
    142 	char temp[10];
    143 
    144 	temp[0] = command[0] = '\0';
    145 	high = -30000;
    146 	try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
    147 }
    148 
    149 int dtab[] = {0,1,1,2,3,4,4,5};		/* diagonal distances in x==y */
    150 
    151 score(movement, ship, to, onlytemp)
    152 char movement[];
    153 register struct ship *ship, *to;
    154 char onlytemp;
    155 {
    156 	char drift;
    157 	int row, col, dir, total, ran;
    158 	register struct File *fp = ship->file;
    159 
    160 	if ((dir = fp->dir) == 0)
    161 		return 0;
    162 	row = fp->row;
    163 	col = fp->col;
    164 	drift = fp->drift;
    165 	move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
    166 	if (!*movement)
    167 		(void) strcpy(movement, "d");
    168 
    169 	ran = range(ship, to);
    170 	total = -50 * ran;
    171 	if (ran < 4 && gunsbear(ship, to))
    172 		total += 60;
    173 	if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
    174 		total = -30000;
    175 
    176 	if (!onlytemp) {
    177 		fp->row = row;
    178 		fp->col = col;
    179 		fp->dir = dir;
    180 	}
    181 	return total;
    182 }
    183 
    184 move(p, ship, dir, row, col, drift)
    185 register char *p;
    186 register struct ship *ship;
    187 register char *dir;
    188 register short *row, *col;
    189 register char *drift;
    190 {
    191 	int dist;
    192 	char moved = 0;
    193 
    194 	for (; *p; p++) {
    195 		switch (*p) {
    196 		case 'r':
    197 			if (++*dir == 9)
    198 				*dir = 1;
    199 			break;
    200 		case 'l':
    201 			if (--*dir == 0)
    202 				*dir = 8;
    203 			break;
    204 		case '1': case '2': case '3': case '4':
    205 		case '5': case '6': case '7':
    206 			moved++;
    207 			if (*dir % 2 == 0)
    208 				dist = dtab[*p - '0'];
    209 			else
    210 				dist = *p - '0';
    211 			*row -= dr[*dir] * dist;
    212 			*col -= dc[*dir] * dist;
    213 			break;
    214 		}
    215 	}
    216 	if (!moved) {
    217 		if (windspeed != 0 && ++*drift > 2) {
    218 			if (ship->specs->class >= 3 && !snagged(ship)
    219 			    || (turn & 1) == 0) {
    220 				*row -= dr[winddir];
    221 				*col -= dc[winddir];
    222 			}
    223 		}
    224 	} else
    225 		*drift = 0;
    226 }
    227 
    228 try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
    229 register struct ship *f, *t;
    230 int ma, ta, af, *high, rakeme;
    231 char command[], temp[];
    232 {
    233 	register int new, n;
    234 	char st[4];
    235 #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
    236 
    237 	if ((n = strend(temp)) < '1' || n > '9')
    238 		for (n = 1; vma - n >= 0; n++) {
    239 			(void) sprintf(st, "%d", n);
    240 			(void) strcat(temp, st);
    241 			new = score(temp, f, t, rakeme);
    242 			if (new > *high && (!rakeme || rakeyou)) {
    243 				*high = new;
    244 				(void) strcpy(command, temp);
    245 			}
    246 			try(command, temp, ma-n, ta, af, vma-n,
    247 				dir, f, t, high, rakeme);
    248 			rmend(temp);
    249 		}
    250 	if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
    251 		(void) strcat(temp, "r");
    252 		new = score(temp, f, t, rakeme);
    253 		if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
    254 			*high = new;
    255 			(void) strcpy(command, temp);
    256 		}
    257 		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);
    258 		rmend(temp);
    259 	}
    260 	if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
    261 		(void) strcat(temp, "l");
    262 		new = score(temp, f, t, rakeme);
    263 		if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
    264 			*high = new;
    265 			(void) strcpy(command, temp);
    266 		}
    267 		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);
    268 		rmend(temp);
    269 	}
    270 }
    271 
    272 rmend(str)
    273 char *str;
    274 {
    275 	register char *p;
    276 
    277 	for (p = str; *p; p++)
    278 		;
    279 	if (p != str)
    280 		*--p = 0;
    281 }
    282