Home | History | Annotate | Line # | Download | only in sail
dr_3.c revision 1.15.40.1
      1  1.15.40.1       jym /*	$NetBSD: dr_3.c,v 1.15.40.1 2009/05/13 19:18:05 jym 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.15       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.4  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_3.c	8.1 (Berkeley) 5/31/93";
     36        1.3       cgd #else
     37  1.15.40.1       jym __RCSID("$NetBSD: dr_3.c,v 1.15.40.1 2009/05/13 19:18:05 jym Exp $");
     38        1.3       cgd #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41       1.11     jwise #include <stdlib.h>
     42       1.12     jwise #include <string.h>
     43       1.11     jwise #include "extern.h"
     44        1.1       cgd #include "driver.h"
     45        1.1       cgd 
     46  1.15.40.1       jym static int stillmoving(int);
     47  1.15.40.1       jym static int is_isolated(struct ship *);
     48  1.15.40.1       jym static int push(struct ship *, struct ship *);
     49  1.15.40.1       jym static void step(struct ship *, int, char *);
     50       1.10     jwise 
     51        1.9     jwise /* move all comp ships */
     52        1.4  christos void
     53        1.9     jwise moveall(void)
     54        1.1       cgd {
     55        1.4  christos 	struct ship *sp, *sq;
     56        1.4  christos 	int n;
     57        1.4  christos 	int k, l;
     58        1.1       cgd 	int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
     59        1.1       cgd 	char moved[NSHIP];
     60        1.1       cgd 
     61        1.1       cgd 	/*
     62        1.1       cgd 	 * first try to create moves for OUR ships
     63        1.1       cgd 	 */
     64        1.1       cgd 	foreachship(sp) {
     65        1.1       cgd 		struct ship *closest;
     66        1.1       cgd 		int ma, ta;
     67  1.15.40.1       jym 		bool af;
     68        1.1       cgd 
     69        1.1       cgd 		if (sp->file->captain[0] || sp->file->dir == 0)
     70        1.1       cgd 			continue;
     71        1.1       cgd 		if (!sp->file->struck && windspeed && !snagged(sp)
     72        1.1       cgd 		    && sp->specs->crew3) {
     73        1.1       cgd 			ta = maxturns(sp, &af);
     74        1.1       cgd 			ma = maxmove(sp, sp->file->dir, 0);
     75        1.1       cgd 			closest = closestenemy(sp, 0, 0);
     76        1.1       cgd 			if (closest == 0)
     77        1.1       cgd 				*sp->file->movebuf = '\0';
     78        1.1       cgd 			else
     79        1.1       cgd 				closeon(sp, closest, sp->file->movebuf,
     80  1.15.40.1       jym 					sizeof(sp->file->movebuf),
     81        1.1       cgd 					ta, ma, af);
     82        1.1       cgd 		} else
     83        1.1       cgd 			*sp->file->movebuf = '\0';
     84        1.1       cgd 	}
     85        1.1       cgd 	/*
     86        1.1       cgd 	 * Then execute the moves for ALL ships (dead ones too),
     87        1.1       cgd 	 * checking for collisions and snags at each step.
     88        1.1       cgd 	 * The old positions are saved in row[], col[], dir[].
     89        1.1       cgd 	 * At the end, we compare and write out the changes.
     90        1.1       cgd 	 */
     91        1.1       cgd 	n = 0;
     92        1.1       cgd 	foreachship(sp) {
     93        1.1       cgd 		if (snagged(sp))
     94        1.9     jwise 			strcpy(sp->file->movebuf, "d");
     95        1.1       cgd 		else
     96        1.1       cgd 			if (*sp->file->movebuf != 'd')
     97        1.9     jwise 				strcat(sp->file->movebuf, "d");
     98        1.1       cgd 		row[n] = sp->file->row;
     99        1.1       cgd 		col[n] = sp->file->col;
    100        1.1       cgd 		dir[n] = sp->file->dir;
    101        1.1       cgd 		drift[n] = sp->file->drift;
    102        1.1       cgd 		moved[n] = 0;
    103        1.1       cgd 		n++;
    104        1.1       cgd 	}
    105        1.1       cgd 	/*
    106        1.1       cgd 	 * Now resolve collisions.
    107        1.1       cgd 	 * This is the tough part.
    108        1.1       cgd 	 */
    109        1.1       cgd 	for (k = 0; stillmoving(k); k++) {
    110        1.1       cgd 		/*
    111        1.1       cgd 		 * Step once.
    112        1.1       cgd 		 * And propagate the nulls at the end of sp->file->movebuf.
    113        1.1       cgd 		 */
    114        1.1       cgd 		n = 0;
    115        1.1       cgd 		foreachship(sp) {
    116        1.1       cgd 			if (!sp->file->movebuf[k])
    117        1.1       cgd 				sp->file->movebuf[k+1] = '\0';
    118        1.1       cgd 			else if (sp->file->dir)
    119       1.13     jwise 				step(sp, sp->file->movebuf[k], &moved[n]);
    120        1.1       cgd 			n++;
    121        1.1       cgd 		}
    122        1.1       cgd 		/*
    123        1.1       cgd 		 * The real stuff.
    124        1.1       cgd 		 */
    125        1.1       cgd 		n = 0;
    126        1.1       cgd 		foreachship(sp) {
    127        1.7       jsm 			if (sp->file->dir == 0 || is_isolated(sp))
    128        1.1       cgd 				goto cont1;
    129        1.1       cgd 			l = 0;
    130        1.1       cgd 			foreachship(sq) {
    131        1.1       cgd 				char snap = 0;
    132        1.1       cgd 
    133        1.1       cgd 				if (sp == sq)
    134        1.1       cgd 					goto cont2;
    135        1.1       cgd 				if (sq->file->dir == 0)
    136        1.1       cgd 					goto cont2;
    137        1.1       cgd 				if (!push(sp, sq))
    138        1.1       cgd 					goto cont2;
    139        1.1       cgd 				if (snagged2(sp, sq) && range(sp, sq) > 1)
    140        1.1       cgd 					snap++;
    141        1.1       cgd 				if (!range(sp, sq) && !fouled2(sp, sq)) {
    142        1.5  christos 					makesignal(sp, "collision with $$", sq);
    143        1.8     jwise 					if (dieroll() < 4) {
    144        1.5  christos 						makesignal(sp, "fouled with $$",
    145        1.5  christos 						    sq);
    146  1.15.40.1       jym 						send_foul(sp, l);
    147  1.15.40.1       jym 						send_foul(sq, n);
    148        1.1       cgd 					}
    149        1.1       cgd 					snap++;
    150        1.1       cgd 				}
    151        1.1       cgd 				if (snap) {
    152        1.1       cgd 					sp->file->movebuf[k + 1] = 0;
    153        1.1       cgd 					sq->file->movebuf[k + 1] = 0;
    154        1.1       cgd 					sq->file->row = sp->file->row - 1;
    155        1.1       cgd 					if (sp->file->dir == 1
    156        1.1       cgd 					    || sp->file->dir == 5)
    157        1.1       cgd 						sq->file->col =
    158        1.1       cgd 							sp->file->col - 1;
    159        1.1       cgd 					else
    160        1.1       cgd 						sq->file->col = sp->file->col;
    161        1.1       cgd 					sq->file->dir = sp->file->dir;
    162        1.1       cgd 				}
    163        1.1       cgd 			cont2:
    164        1.1       cgd 				l++;
    165        1.1       cgd 			}
    166        1.1       cgd 		cont1:
    167        1.1       cgd 			n++;
    168        1.1       cgd 		}
    169        1.1       cgd 	}
    170        1.1       cgd 	/*
    171        1.1       cgd 	 * Clear old moves.  And write out new pos.
    172        1.1       cgd 	 */
    173        1.1       cgd 	n = 0;
    174        1.1       cgd 	foreachship(sp) {
    175        1.1       cgd 		if (sp->file->dir != 0) {
    176        1.1       cgd 			*sp->file->movebuf = 0;
    177        1.1       cgd 			if (row[n] != sp->file->row)
    178  1.15.40.1       jym 				send_row(sp, sp->file->row);
    179        1.1       cgd 			if (col[n] != sp->file->col)
    180  1.15.40.1       jym 				send_col(sp, sp->file->col);
    181        1.1       cgd 			if (dir[n] != sp->file->dir)
    182  1.15.40.1       jym 				send_dir(sp, sp->file->dir);
    183        1.1       cgd 			if (drift[n] != sp->file->drift)
    184  1.15.40.1       jym 				send_drift(sp, sp->file->drift);
    185        1.1       cgd 		}
    186        1.1       cgd 		n++;
    187        1.1       cgd 	}
    188        1.1       cgd }
    189        1.1       cgd 
    190       1.10     jwise static int
    191        1.9     jwise stillmoving(int k)
    192        1.1       cgd {
    193        1.4  christos 	struct ship *sp;
    194        1.1       cgd 
    195        1.1       cgd 	foreachship(sp)
    196        1.1       cgd 		if (sp->file->movebuf[k])
    197        1.1       cgd 			return 1;
    198        1.1       cgd 	return 0;
    199        1.1       cgd }
    200        1.1       cgd 
    201       1.10     jwise static int
    202        1.9     jwise is_isolated(struct ship *ship)
    203        1.1       cgd {
    204        1.4  christos 	struct ship *sp;
    205        1.1       cgd 
    206        1.1       cgd 	foreachship(sp) {
    207        1.1       cgd 		if (ship != sp && range(ship, sp) <= 10)
    208        1.1       cgd 			return 0;
    209        1.1       cgd 	}
    210        1.1       cgd 	return 1;
    211        1.1       cgd }
    212        1.1       cgd 
    213       1.10     jwise static int
    214        1.9     jwise push(struct ship *from, struct ship *to)
    215        1.1       cgd {
    216        1.4  christos 	int bs, sb;
    217        1.1       cgd 
    218        1.1       cgd 	sb = to->specs->guns;
    219        1.1       cgd 	bs = from->specs->guns;
    220        1.1       cgd 	if (sb > bs)
    221        1.1       cgd 		return 1;
    222        1.1       cgd 	if (sb < bs)
    223        1.1       cgd 		return 0;
    224        1.1       cgd 	return from < to;
    225        1.1       cgd }
    226        1.1       cgd 
    227       1.10     jwise static void
    228       1.13     jwise step(struct ship *sp, int com, char *moved)
    229        1.1       cgd {
    230        1.4  christos 	int dist;
    231        1.1       cgd 
    232        1.1       cgd 	switch (com) {
    233        1.1       cgd 	case 'r':
    234        1.1       cgd 		if (++sp->file->dir == 9)
    235        1.1       cgd 			sp->file->dir = 1;
    236        1.1       cgd 		break;
    237        1.1       cgd 	case 'l':
    238        1.1       cgd 		if (--sp->file->dir == 0)
    239        1.1       cgd 			sp->file->dir = 8;
    240        1.1       cgd 		break;
    241        1.1       cgd 		case '0': case '1': case '2': case '3':
    242        1.1       cgd 		case '4': case '5': case '6': case '7':
    243        1.1       cgd 		if (sp->file->dir % 2 == 0)
    244        1.1       cgd 			dist = dtab[com - '0'];
    245        1.1       cgd 		else
    246        1.1       cgd 			dist = com - '0';
    247        1.1       cgd 		sp->file->row -= dr[sp->file->dir] * dist;
    248        1.1       cgd 		sp->file->col -= dc[sp->file->dir] * dist;
    249        1.1       cgd 		*moved = 1;
    250        1.1       cgd 		break;
    251        1.1       cgd 	case 'b':
    252        1.1       cgd 		break;
    253        1.1       cgd 	case 'd':
    254        1.1       cgd 		if (!*moved) {
    255        1.1       cgd 			if (windspeed != 0 && ++sp->file->drift > 2 &&
    256        1.4  christos 			    ((sp->specs->class >= 3 && !snagged(sp))
    257        1.1       cgd 			     || (turn & 1) == 0)) {
    258        1.1       cgd 				sp->file->row -= dr[winddir];
    259        1.1       cgd 				sp->file->col -= dc[winddir];
    260        1.1       cgd 			}
    261  1.15.40.1       jym 		} else {
    262        1.1       cgd 			sp->file->drift = 0;
    263  1.15.40.1       jym 		}
    264        1.1       cgd 		break;
    265        1.1       cgd 	}
    266        1.1       cgd }
    267        1.1       cgd 
    268        1.4  christos void
    269        1.9     jwise sendbp(struct ship *from, struct ship *to, int sections, int isdefense)
    270        1.1       cgd {
    271        1.1       cgd 	int n;
    272        1.4  christos 	struct BP *bp;
    273        1.1       cgd 
    274        1.1       cgd 	bp = isdefense ? from->file->DBP : from->file->OBP;
    275        1.1       cgd 	for (n = 0; n < NBP && bp[n].turnsent; n++)
    276        1.1       cgd 		;
    277        1.1       cgd 	if (n < NBP && sections) {
    278  1.15.40.1       jym 		if (isdefense) {
    279  1.15.40.1       jym 			send_dbp(from, n, turn, to->file->index, sections);
    280  1.15.40.1       jym 		} else {
    281  1.15.40.1       jym 			send_obp(from, n, turn, to->file->index, sections);
    282  1.15.40.1       jym 		}
    283        1.1       cgd 		if (isdefense)
    284        1.5  christos 			makemsg(from, "repelling boarders");
    285        1.1       cgd 		else
    286        1.5  christos 			makesignal(from, "boarding the $$", to);
    287        1.1       cgd 	}
    288        1.1       cgd }
    289        1.1       cgd 
    290        1.4  christos int
    291        1.9     jwise is_toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
    292        1.1       cgd {
    293        1.4  christos 	struct BP *bp;
    294        1.4  christos 	int obp = 0;
    295        1.1       cgd 	int n, OBP = 0, DBP = 0, dbp = 0;
    296        1.1       cgd 	int qual;
    297        1.1       cgd 
    298        1.1       cgd 	qual = ship->specs->qual;
    299        1.1       cgd 	bp = isdefense ? ship->file->DBP : ship->file->OBP;
    300        1.1       cgd 	for (n = 0; n < NBP; n++, bp++) {
    301        1.1       cgd 		if (bp->turnsent && (to == bp->toship || isdefense)) {
    302        1.1       cgd 			obp += bp->mensent / 100
    303        1.1       cgd 				? ship->specs->crew1 * qual : 0;
    304        1.1       cgd 			obp += (bp->mensent % 100)/10
    305        1.1       cgd 				? ship->specs->crew2 * qual : 0;
    306        1.1       cgd 			obp += bp->mensent % 10
    307        1.1       cgd 				? ship->specs->crew3 * qual : 0;
    308        1.1       cgd 		}
    309        1.1       cgd 	}
    310        1.1       cgd 	if (count || isdefense)
    311        1.1       cgd 		return obp;
    312        1.7       jsm 	OBP = is_toughmelee(to, ship, 0, count + 1);
    313        1.7       jsm 	dbp = is_toughmelee(ship, to, 1, count + 1);
    314        1.7       jsm 	DBP = is_toughmelee(to, ship, 1, count + 1);
    315        1.1       cgd 	if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
    316        1.1       cgd 		return 1;
    317        1.1       cgd 	else
    318        1.1       cgd 		return 0;
    319        1.1       cgd }
    320        1.1       cgd 
    321        1.4  christos void
    322        1.9     jwise reload(void)
    323        1.1       cgd {
    324        1.4  christos 	struct ship *sp;
    325        1.1       cgd 
    326        1.1       cgd 	foreachship(sp) {
    327        1.1       cgd 		sp->file->loadwith = 0;
    328        1.1       cgd 	}
    329        1.1       cgd }
    330        1.1       cgd 
    331        1.4  christos void
    332        1.9     jwise checksails(void)
    333        1.1       cgd {
    334        1.4  christos 	struct ship *sp;
    335  1.15.40.1       jym 	int rig, full;
    336        1.1       cgd 	struct ship *close;
    337        1.1       cgd 
    338        1.1       cgd 	foreachship(sp) {
    339        1.1       cgd 		if (sp->file->captain[0] != 0)
    340        1.1       cgd 			continue;
    341        1.1       cgd 		rig = sp->specs->rig1;
    342        1.4  christos 		if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
    343        1.1       cgd 			rig = 0;
    344        1.1       cgd 		if (rig && sp->specs->crew3) {
    345        1.1       cgd 			close = closestenemy(sp, 0, 0);
    346        1.1       cgd 			if (close != 0) {
    347        1.1       cgd 				if (range(sp, close) > 9)
    348        1.1       cgd 					full = 1;
    349        1.1       cgd 				else
    350        1.1       cgd 					full = 0;
    351  1.15.40.1       jym 			} else {
    352        1.1       cgd 				full = 0;
    353  1.15.40.1       jym 			}
    354  1.15.40.1       jym 		} else {
    355        1.1       cgd 			full = 0;
    356  1.15.40.1       jym 		}
    357        1.1       cgd 		if ((sp->file->FS != 0) != full)
    358  1.15.40.1       jym 			send_fs(sp, full);
    359        1.1       cgd 	}
    360        1.1       cgd }
    361