Home | History | Annotate | Line # | Download | only in sail
dr_3.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)dr_3.c	5.4 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include "driver.h"
     39  1.1  cgd 
     40  1.1  cgd moveall()		/* move all comp ships */
     41  1.1  cgd {
     42  1.1  cgd 	register struct ship *sp, *sq;		/* r11, r10 */
     43  1.1  cgd 	register int n;				/* r9 */
     44  1.1  cgd 	register int k, l;			/* r8, r7 */
     45  1.1  cgd 	int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
     46  1.1  cgd 	char moved[NSHIP];
     47  1.1  cgd 
     48  1.1  cgd 	/*
     49  1.1  cgd 	 * first try to create moves for OUR ships
     50  1.1  cgd 	 */
     51  1.1  cgd 	foreachship(sp) {
     52  1.1  cgd 		struct ship *closest;
     53  1.1  cgd 		int ma, ta;
     54  1.1  cgd 		char af;
     55  1.1  cgd 
     56  1.1  cgd 		if (sp->file->captain[0] || sp->file->dir == 0)
     57  1.1  cgd 			continue;
     58  1.1  cgd 		if (!sp->file->struck && windspeed && !snagged(sp)
     59  1.1  cgd 		    && sp->specs->crew3) {
     60  1.1  cgd 			ta = maxturns(sp, &af);
     61  1.1  cgd 			ma = maxmove(sp, sp->file->dir, 0);
     62  1.1  cgd 			closest = closestenemy(sp, 0, 0);
     63  1.1  cgd 			if (closest == 0)
     64  1.1  cgd 				*sp->file->movebuf = '\0';
     65  1.1  cgd 			else
     66  1.1  cgd 				closeon(sp, closest, sp->file->movebuf,
     67  1.1  cgd 					ta, ma, af);
     68  1.1  cgd 		} else
     69  1.1  cgd 			*sp->file->movebuf = '\0';
     70  1.1  cgd 	}
     71  1.1  cgd 	/*
     72  1.1  cgd 	 * Then execute the moves for ALL ships (dead ones too),
     73  1.1  cgd 	 * checking for collisions and snags at each step.
     74  1.1  cgd 	 * The old positions are saved in row[], col[], dir[].
     75  1.1  cgd 	 * At the end, we compare and write out the changes.
     76  1.1  cgd 	 */
     77  1.1  cgd 	n = 0;
     78  1.1  cgd 	foreachship(sp) {
     79  1.1  cgd 		if (snagged(sp))
     80  1.1  cgd 			(void) strcpy(sp->file->movebuf, "d");
     81  1.1  cgd 		else
     82  1.1  cgd 			if (*sp->file->movebuf != 'd')
     83  1.1  cgd 				(void) strcat(sp->file->movebuf, "d");
     84  1.1  cgd 		row[n] = sp->file->row;
     85  1.1  cgd 		col[n] = sp->file->col;
     86  1.1  cgd 		dir[n] = sp->file->dir;
     87  1.1  cgd 		drift[n] = sp->file->drift;
     88  1.1  cgd 		moved[n] = 0;
     89  1.1  cgd 		n++;
     90  1.1  cgd 	}
     91  1.1  cgd 	/*
     92  1.1  cgd 	 * Now resolve collisions.
     93  1.1  cgd 	 * This is the tough part.
     94  1.1  cgd 	 */
     95  1.1  cgd 	for (k = 0; stillmoving(k); k++) {
     96  1.1  cgd 		/*
     97  1.1  cgd 		 * Step once.
     98  1.1  cgd 		 * And propagate the nulls at the end of sp->file->movebuf.
     99  1.1  cgd 		 */
    100  1.1  cgd 		n = 0;
    101  1.1  cgd 		foreachship(sp) {
    102  1.1  cgd 			if (!sp->file->movebuf[k])
    103  1.1  cgd 				sp->file->movebuf[k+1] = '\0';
    104  1.1  cgd 			else if (sp->file->dir)
    105  1.1  cgd 				step(sp->file->movebuf[k], sp, &moved[n]);
    106  1.1  cgd 			n++;
    107  1.1  cgd 		}
    108  1.1  cgd 		/*
    109  1.1  cgd 		 * The real stuff.
    110  1.1  cgd 		 */
    111  1.1  cgd 		n = 0;
    112  1.1  cgd 		foreachship(sp) {
    113  1.1  cgd 			if (sp->file->dir == 0 || isolated(sp))
    114  1.1  cgd 				goto cont1;
    115  1.1  cgd 			l = 0;
    116  1.1  cgd 			foreachship(sq) {
    117  1.1  cgd 				char snap = 0;
    118  1.1  cgd 
    119  1.1  cgd 				if (sp == sq)
    120  1.1  cgd 					goto cont2;
    121  1.1  cgd 				if (sq->file->dir == 0)
    122  1.1  cgd 					goto cont2;
    123  1.1  cgd 				if (!push(sp, sq))
    124  1.1  cgd 					goto cont2;
    125  1.1  cgd 				if (snagged2(sp, sq) && range(sp, sq) > 1)
    126  1.1  cgd 					snap++;
    127  1.1  cgd 				if (!range(sp, sq) && !fouled2(sp, sq)) {
    128  1.1  cgd 					makesignal(sp,
    129  1.1  cgd 						"collision with %s (%c%c)", sq);
    130  1.1  cgd 					if (die() < 4) {
    131  1.1  cgd 						makesignal(sp,
    132  1.1  cgd 							"fouled with %s (%c%c)",
    133  1.1  cgd 							sq);
    134  1.1  cgd 						Write(W_FOUL, sp, 0, l, 0, 0, 0);
    135  1.1  cgd 						Write(W_FOUL, sq, 0, n, 0, 0, 0);
    136  1.1  cgd 					}
    137  1.1  cgd 					snap++;
    138  1.1  cgd 				}
    139  1.1  cgd 				if (snap) {
    140  1.1  cgd 					sp->file->movebuf[k + 1] = 0;
    141  1.1  cgd 					sq->file->movebuf[k + 1] = 0;
    142  1.1  cgd 					sq->file->row = sp->file->row - 1;
    143  1.1  cgd 					if (sp->file->dir == 1
    144  1.1  cgd 					    || sp->file->dir == 5)
    145  1.1  cgd 						sq->file->col =
    146  1.1  cgd 							sp->file->col - 1;
    147  1.1  cgd 					else
    148  1.1  cgd 						sq->file->col = sp->file->col;
    149  1.1  cgd 					sq->file->dir = sp->file->dir;
    150  1.1  cgd 				}
    151  1.1  cgd 			cont2:
    152  1.1  cgd 				l++;
    153  1.1  cgd 			}
    154  1.1  cgd 		cont1:
    155  1.1  cgd 			n++;
    156  1.1  cgd 		}
    157  1.1  cgd 	}
    158  1.1  cgd 	/*
    159  1.1  cgd 	 * Clear old moves.  And write out new pos.
    160  1.1  cgd 	 */
    161  1.1  cgd 	n = 0;
    162  1.1  cgd 	foreachship(sp) {
    163  1.1  cgd 		if (sp->file->dir != 0) {
    164  1.1  cgd 			*sp->file->movebuf = 0;
    165  1.1  cgd 			if (row[n] != sp->file->row)
    166  1.1  cgd 				Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
    167  1.1  cgd 			if (col[n] != sp->file->col)
    168  1.1  cgd 				Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
    169  1.1  cgd 			if (dir[n] != sp->file->dir)
    170  1.1  cgd 				Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
    171  1.1  cgd 			if (drift[n] != sp->file->drift)
    172  1.1  cgd 				Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
    173  1.1  cgd 		}
    174  1.1  cgd 		n++;
    175  1.1  cgd 	}
    176  1.1  cgd }
    177  1.1  cgd 
    178  1.1  cgd stillmoving(k)
    179  1.1  cgd register int k;
    180  1.1  cgd {
    181  1.1  cgd 	register struct ship *sp;
    182  1.1  cgd 
    183  1.1  cgd 	foreachship(sp)
    184  1.1  cgd 		if (sp->file->movebuf[k])
    185  1.1  cgd 			return 1;
    186  1.1  cgd 	return 0;
    187  1.1  cgd }
    188  1.1  cgd 
    189  1.1  cgd isolated(ship)
    190  1.1  cgd register struct ship *ship;
    191  1.1  cgd {
    192  1.1  cgd 	register struct ship *sp;
    193  1.1  cgd 
    194  1.1  cgd 	foreachship(sp) {
    195  1.1  cgd 		if (ship != sp && range(ship, sp) <= 10)
    196  1.1  cgd 			return 0;
    197  1.1  cgd 	}
    198  1.1  cgd 	return 1;
    199  1.1  cgd }
    200  1.1  cgd 
    201  1.1  cgd push(from, to)
    202  1.1  cgd register struct ship *from, *to;
    203  1.1  cgd {
    204  1.1  cgd 	register int bs, sb;
    205  1.1  cgd 
    206  1.1  cgd 	sb = to->specs->guns;
    207  1.1  cgd 	bs = from->specs->guns;
    208  1.1  cgd 	if (sb > bs)
    209  1.1  cgd 		return 1;
    210  1.1  cgd 	if (sb < bs)
    211  1.1  cgd 		return 0;
    212  1.1  cgd 	return from < to;
    213  1.1  cgd }
    214  1.1  cgd 
    215  1.1  cgd step(com, sp, moved)
    216  1.1  cgd char com;
    217  1.1  cgd register struct ship *sp;
    218  1.1  cgd char *moved;
    219  1.1  cgd {
    220  1.1  cgd 	register int dist;
    221  1.1  cgd 
    222  1.1  cgd 	switch (com) {
    223  1.1  cgd 	case 'r':
    224  1.1  cgd 		if (++sp->file->dir == 9)
    225  1.1  cgd 			sp->file->dir = 1;
    226  1.1  cgd 		break;
    227  1.1  cgd 	case 'l':
    228  1.1  cgd 		if (--sp->file->dir == 0)
    229  1.1  cgd 			sp->file->dir = 8;
    230  1.1  cgd 		break;
    231  1.1  cgd 		case '0': case '1': case '2': case '3':
    232  1.1  cgd 		case '4': case '5': case '6': case '7':
    233  1.1  cgd 		if (sp->file->dir % 2 == 0)
    234  1.1  cgd 			dist = dtab[com - '0'];
    235  1.1  cgd 		else
    236  1.1  cgd 			dist = com - '0';
    237  1.1  cgd 		sp->file->row -= dr[sp->file->dir] * dist;
    238  1.1  cgd 		sp->file->col -= dc[sp->file->dir] * dist;
    239  1.1  cgd 		*moved = 1;
    240  1.1  cgd 		break;
    241  1.1  cgd 	case 'b':
    242  1.1  cgd 		break;
    243  1.1  cgd 	case 'd':
    244  1.1  cgd 		if (!*moved) {
    245  1.1  cgd 			if (windspeed != 0 && ++sp->file->drift > 2 &&
    246  1.1  cgd 			    (sp->specs->class >= 3 && !snagged(sp)
    247  1.1  cgd 			     || (turn & 1) == 0)) {
    248  1.1  cgd 				sp->file->row -= dr[winddir];
    249  1.1  cgd 				sp->file->col -= dc[winddir];
    250  1.1  cgd 			}
    251  1.1  cgd 		} else
    252  1.1  cgd 			sp->file->drift = 0;
    253  1.1  cgd 		break;
    254  1.1  cgd 	}
    255  1.1  cgd }
    256  1.1  cgd 
    257  1.1  cgd sendbp(from, to, sections, isdefense)
    258  1.1  cgd register struct ship *from, *to;
    259  1.1  cgd int sections;
    260  1.1  cgd char isdefense;
    261  1.1  cgd {
    262  1.1  cgd 	int n;
    263  1.1  cgd 	register struct BP *bp;
    264  1.1  cgd 
    265  1.1  cgd 	bp = isdefense ? from->file->DBP : from->file->OBP;
    266  1.1  cgd 	for (n = 0; n < NBP && bp[n].turnsent; n++)
    267  1.1  cgd 		;
    268  1.1  cgd 	if (n < NBP && sections) {
    269  1.1  cgd 		Write(isdefense ? W_DBP : W_OBP, from, 0,
    270  1.1  cgd 			n, turn, to->file->index, sections);
    271  1.1  cgd 		if (isdefense)
    272  1.1  cgd 			makesignal(from, "repelling boarders",
    273  1.1  cgd 				(struct ship *)0);
    274  1.1  cgd 		else
    275  1.1  cgd 			makesignal(from, "boarding the %s (%c%c)", to);
    276  1.1  cgd 	}
    277  1.1  cgd }
    278  1.1  cgd 
    279  1.1  cgd toughmelee(ship, to, isdefense, count)
    280  1.1  cgd register struct ship *ship, *to;
    281  1.1  cgd int isdefense, count;
    282  1.1  cgd {
    283  1.1  cgd 	register struct BP *bp;
    284  1.1  cgd 	register obp = 0;
    285  1.1  cgd 	int n, OBP = 0, DBP = 0, dbp = 0;
    286  1.1  cgd 	int qual;
    287  1.1  cgd 
    288  1.1  cgd 	qual = ship->specs->qual;
    289  1.1  cgd 	bp = isdefense ? ship->file->DBP : ship->file->OBP;
    290  1.1  cgd 	for (n = 0; n < NBP; n++, bp++) {
    291  1.1  cgd 		if (bp->turnsent && (to == bp->toship || isdefense)) {
    292  1.1  cgd 			obp += bp->mensent / 100
    293  1.1  cgd 				? ship->specs->crew1 * qual : 0;
    294  1.1  cgd 			obp += (bp->mensent % 100)/10
    295  1.1  cgd 				? ship->specs->crew2 * qual : 0;
    296  1.1  cgd 			obp += bp->mensent % 10
    297  1.1  cgd 				? ship->specs->crew3 * qual : 0;
    298  1.1  cgd 		}
    299  1.1  cgd 	}
    300  1.1  cgd 	if (count || isdefense)
    301  1.1  cgd 		return obp;
    302  1.1  cgd 	OBP = toughmelee(to, ship, 0, count + 1);
    303  1.1  cgd 	dbp = toughmelee(ship, to, 1, count + 1);
    304  1.1  cgd 	DBP = toughmelee(to, ship, 1, count + 1);
    305  1.1  cgd 	if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
    306  1.1  cgd 		return 1;
    307  1.1  cgd 	else
    308  1.1  cgd 		return 0;
    309  1.1  cgd }
    310  1.1  cgd 
    311  1.1  cgd reload()
    312  1.1  cgd {
    313  1.1  cgd 	register struct ship *sp;
    314  1.1  cgd 
    315  1.1  cgd 	foreachship(sp) {
    316  1.1  cgd 		sp->file->loadwith = 0;
    317  1.1  cgd 	}
    318  1.1  cgd }
    319  1.1  cgd 
    320  1.1  cgd checksails()
    321  1.1  cgd {
    322  1.1  cgd 	register struct ship *sp;
    323  1.1  cgd 	register int rig, full;
    324  1.1  cgd 	struct ship *close;
    325  1.1  cgd 
    326  1.1  cgd 	foreachship(sp) {
    327  1.1  cgd 		if (sp->file->captain[0] != 0)
    328  1.1  cgd 			continue;
    329  1.1  cgd 		rig = sp->specs->rig1;
    330  1.1  cgd 		if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
    331  1.1  cgd 			rig = 0;
    332  1.1  cgd 		if (rig && sp->specs->crew3) {
    333  1.1  cgd 			close = closestenemy(sp, 0, 0);
    334  1.1  cgd 			if (close != 0) {
    335  1.1  cgd 				if (range(sp, close) > 9)
    336  1.1  cgd 					full = 1;
    337  1.1  cgd 				else
    338  1.1  cgd 					full = 0;
    339  1.1  cgd 			} else
    340  1.1  cgd 				full = 0;
    341  1.1  cgd 		} else
    342  1.1  cgd 			full = 0;
    343  1.1  cgd 		if ((sp->file->FS != 0) != full)
    344  1.1  cgd 			Write(W_FS, sp, 0, full, 0, 0, 0);
    345  1.1  cgd 	}
    346  1.1  cgd }
    347