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