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