Home | History | Annotate | Line # | Download | only in sail
dr_1.c revision 1.9.2.1
      1 /*	$NetBSD: dr_1.c,v 1.9.2.1 1999/12/27 18:29:05 wrstuden 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)dr_1.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: dr_1.c,v 1.9.2.1 1999/12/27 18:29:05 wrstuden Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "driver.h"
     46 #include <stdlib.h>
     47 
     48 void
     49 unfoul()
     50 {
     51 	struct ship *sp;
     52 	struct ship *to;
     53 	int nat;
     54 	int i;
     55 
     56 	foreachship(sp) {
     57 		if (sp->file->captain[0])
     58 			continue;
     59 		nat = capship(sp)->nationality;
     60 		foreachship(to) {
     61 			if (nat != capship(to)->nationality &&
     62 			    !is_toughmelee(sp, to, 0, 0))
     63 				continue;
     64 			for (i = fouled2(sp, to); --i >= 0;)
     65 				if (die() <= 2)
     66 					cleanfoul(sp, to, 0);
     67 		}
     68 	}
     69 }
     70 
     71 void
     72 boardcomp()
     73 {
     74 	int crew[3];
     75 	struct ship *sp, *sq;
     76 
     77 	foreachship(sp) {
     78 		if (*sp->file->captain)
     79 			continue;
     80 		if (sp->file->dir == 0)
     81 			continue;
     82 		if (sp->file->struck || sp->file->captured != 0)
     83 			continue;
     84 		if (!snagged(sp))
     85 			continue;
     86 		crew[0] = sp->specs->crew1 != 0;
     87 		crew[1] = sp->specs->crew2 != 0;
     88 		crew[2] = sp->specs->crew3 != 0;
     89 		foreachship(sq) {
     90 			if (!Xsnagged2(sp, sq))
     91 				continue;
     92 			if (meleeing(sp, sq))
     93 				continue;
     94 			if (!sq->file->dir
     95 				|| sp->nationality == capship(sq)->nationality)
     96 				continue;
     97 			switch (sp->specs->class - sq->specs->class) {
     98 			case -3: case -4: case -5:
     99 				if (crew[0]) {
    100 					/* OBP */
    101 					sendbp(sp, sq, crew[0]*100, 0);
    102 					crew[0] = 0;
    103 				} else if (crew[1]){
    104 					/* OBP */
    105 					sendbp(sp, sq, crew[1]*10, 0);
    106 					crew[1] = 0;
    107 				}
    108 				break;
    109 			case -2:
    110 				if (crew[0] || crew[1]) {
    111 					/* OBP */
    112 					sendbp(sp, sq, crew[0]*100+crew[1]*10,
    113 						0);
    114 					crew[0] = crew[1] = 0;
    115 				}
    116 				break;
    117 			case -1: case 0: case 1:
    118 				if (crew[0]) {
    119 					/* OBP */
    120 					sendbp(sp, sq, crew[0]*100+crew[1]*10,
    121 						0);
    122 					crew[0] = crew[1] = 0;
    123 				}
    124 				break;
    125 			case 2: case 3: case 4: case 5:
    126 				/* OBP */
    127 				sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2],
    128 					0);
    129 				crew[0] = crew[1] = crew[2] = 0;
    130 				break;
    131 			}
    132 		}
    133 	}
    134 }
    135 
    136 int
    137 fightitout(from, to, key)
    138 struct ship *from, *to;
    139 int key;
    140 {
    141 	struct ship *fromcap, *tocap;
    142 	int crewfrom[3], crewto[3], menfrom, mento;
    143 	int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured;
    144 	int topoints;
    145 	int index, totalfrom = 0, totalto = 0;
    146 	int count;
    147 	char message[60];
    148 
    149 	menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key);
    150 	mento = mensent(to, from, crewto, &tocap, &pcto, 0);
    151 	if (fromcap == 0)
    152 		fromcap = from;
    153 	if (tocap == 0)
    154 		tocap = to;
    155 	if (key) {
    156 		if (!menfrom) {		 /* if crew surprised */
    157 			if (fromcap == from)
    158 				menfrom = from->specs->crew1
    159 					+ from->specs->crew2
    160 					+ from->specs->crew3;
    161 			else
    162 				menfrom = from->file->pcrew;
    163 		} else {
    164 			menfrom *= 2;	/* DBP's fight at an advantage */
    165 		}
    166 	}
    167 	fromstrength = menfrom * fromcap->specs->qual;
    168 	strengthto = mento * tocap->specs->qual;
    169 	for (count = 0;
    170 	     ((fromstrength < strengthto * 3 && strengthto < fromstrength * 3)
    171 	      || fromstrength == -1) && count < 4;
    172 	     count++) {
    173 		index = fromstrength/10;
    174 		if (index > 8)
    175 			index = 8;
    176 		toinjured = MT[index][2 - die() / 3];
    177 		totalto += toinjured;
    178 		index = strengthto/10;
    179 		if (index > 8)
    180 			index = 8;
    181 		frominjured = MT[index][2 - die() / 3];
    182 		totalfrom += frominjured;
    183 		menfrom -= frominjured;
    184 		mento -= toinjured;
    185 		fromstrength = menfrom * fromcap->specs->qual;
    186 		strengthto = mento * tocap->specs->qual;
    187 	}
    188 	if (fromstrength >= strengthto * 3 || count == 4) {
    189 		unboard(to, from, 0);
    190 		subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
    191 		subtract(to, totalto, crewto, tocap, pcto);
    192 		makemsg(from, "boarders from %s repelled", to->shipname);
    193 		(void) sprintf(message, "killed in melee: %d.  %s: %d",
    194 			totalto, from->shipname, totalfrom);
    195 		Writestr(W_SIGNAL, to, message);
    196 		if (key)
    197 			return 1;
    198 	} else if (strengthto >= fromstrength * 3) {
    199 		unboard(from, to, 0);
    200 		subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
    201 		subtract(to, totalto, crewto, tocap, pcto);
    202 		if (key) {
    203 			if (fromcap != from)
    204 				Write(W_POINTS, fromcap,
    205 					fromcap->file->points -
    206 						from->file->struck
    207 						? from->specs->pts
    208 						: 2 * from->specs->pts,
    209 					0, 0, 0);
    210 
    211 /* ptr1 points to the shipspec for the ship that was just unboarded.
    212    I guess that what is going on here is that the pointer is multiplied
    213    or something. */
    214 
    215 			Write(W_CAPTURED, from, to->file->index, 0, 0, 0);
    216 			topoints = 2 * from->specs->pts + to->file->points;
    217 			if (from->file->struck)
    218 				topoints -= from->specs->pts;
    219 			Write(W_POINTS, to, topoints, 0, 0, 0);
    220 			mento = crewto[0] ? crewto[0] : crewto[1];
    221 			if (mento) {
    222 				subtract(to, mento, crewto, tocap, pcto);
    223 				subtract(from, - mento, crewfrom, to, 0);
    224 			}
    225 			(void) sprintf(message, "captured by the %s!",
    226 				to->shipname);
    227 			Writestr(W_SIGNAL, from, message);
    228 			(void) sprintf(message, "killed in melee: %d.  %s: %d",
    229 				totalto, from->shipname, totalfrom);
    230 			Writestr(W_SIGNAL, to, message);
    231 			mento = 0;
    232 			return 0;
    233 		}
    234 	}
    235 	return 0;
    236 }
    237 
    238 void
    239 resolve()
    240 {
    241 	int thwart;
    242 	struct ship *sp, *sq;
    243 
    244 	foreachship(sp) {
    245 		if (sp->file->dir == 0)
    246 			continue;
    247 		for (sq = sp + 1; sq < ls; sq++)
    248 			if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
    249 				(void) fightitout(sp, sq, 0);
    250 		thwart = 2;
    251 		foreachship(sq) {
    252 			if (sq->file->dir && meleeing(sq, sp))
    253 				thwart = fightitout(sp, sq, 1);
    254 			if (!thwart)
    255 				break;
    256 		}
    257 		if (!thwart) {
    258 			foreachship(sq) {
    259 				if (sq->file->dir && meleeing(sq, sp))
    260 					unboard(sq, sp, 0);
    261 				unboard(sp, sq, 0);
    262 			}
    263 			unboard(sp, sp, 1);
    264 		} else if (thwart == 2)
    265 			unboard(sp, sp, 1);
    266 	}
    267 }
    268 
    269 void
    270 compcombat()
    271 {
    272 	int n;
    273 	struct ship *sp;
    274 	struct ship *closest;
    275 	int crew[3], men = 0, target, temp;
    276 	int r, guns, ready, load, car;
    277 	int index, rakehim, sternrake;
    278 	int shootat, hit;
    279 
    280 	foreachship(sp) {
    281 		if (sp->file->captain[0] || sp->file->dir == 0)
    282 			continue;
    283 		crew[0] = sp->specs->crew1;
    284 		crew[1] = sp->specs->crew2;
    285 		crew[2] = sp->specs->crew3;
    286 		for (n = 0; n < 3; n++) {
    287 			if (sp->file->OBP[n].turnsent)
    288 				men += sp->file->OBP[n].mensent;
    289 		}
    290 		for (n = 0; n < 3; n++) {
    291 			if (sp->file->DBP[n].turnsent)
    292 				men += sp->file->DBP[n].mensent;
    293 		}
    294 		if (men){
    295 			crew[0] = men/100 ? 0 : crew[0] != 0;
    296 			crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
    297 			crew[2] = men%10 ? 0 : crew[2] != 0;
    298 		}
    299 		for (r = 0; r < 2; r++) {
    300 			if (!crew[2])
    301 				continue;
    302 			if (sp->file->struck)
    303 				continue;
    304 			if (r) {
    305 				ready = sp->file->readyR;
    306 				guns = sp->specs->gunR;
    307 				car = sp->specs->carR;
    308 			} else {
    309 				ready = sp->file->readyL;
    310 				guns = sp->specs->gunL;
    311 				car = sp->specs->carL;
    312 			}
    313 			if (!guns && !car)
    314 				continue;
    315 			if ((ready & R_LOADED) == 0)
    316 				continue;
    317 			closest = closestenemy(sp, r ? 'r' : 'l', 0);
    318 			if (closest == 0)
    319 				continue;
    320 			if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1)))
    321 				continue;
    322 			if (closest->file->struck)
    323 				continue;
    324 			target = range(sp, closest);
    325 			if (target > 10)
    326 				continue;
    327 			if (!guns && target >= 3)
    328 				continue;
    329 			load = L_ROUND;
    330 			if (target == 1 && sp->file->loadwith == L_GRAPE)
    331 				load = L_GRAPE;
    332 			if (target <= 3 && closest->file->FS)
    333 				load = L_CHAIN;
    334 			if (target == 1 && load != L_GRAPE)
    335 				load = L_DOUBLE;
    336 			if (load > L_CHAIN && target < 6)
    337 				shootat = HULL;
    338 			else
    339 				shootat = RIGGING;
    340 			rakehim = gunsbear(sp, closest)
    341 				&& !gunsbear(closest, sp);
    342 			temp = portside(closest, sp, 1)
    343 				- closest->file->dir + 1;
    344 			if (temp < 1)
    345 				temp += 8;
    346 			if (temp > 8)
    347 				temp -= 8;
    348 			sternrake = temp > 4 && temp < 6;
    349 			index = guns;
    350 			if (target < 3)
    351 				index += car;
    352 			index = (index - 1) / 3;
    353 			index = index > 8 ? 8 : index;
    354 			if (!rakehim)
    355 				hit = HDT[index][target-1];
    356 			else
    357 				hit = HDTrake[index][target-1];
    358 			if (rakehim && sternrake)
    359 				hit++;
    360 			hit += QUAL[index][capship(sp)->specs->qual - 1];
    361 			for (n = 0; n < 3 && sp->file->captured == 0; n++)
    362 				if (!crew[n]) {
    363 					if (index <= 5)
    364 						hit--;
    365 					else
    366 						hit -= 2;
    367 				}
    368 			if (ready & R_INITIAL) {
    369 				if (!r)
    370 					sp->file->readyL &= ~R_INITIAL;
    371 				else
    372 					sp->file->readyR &= ~R_INITIAL;
    373 				if (index <= 3)
    374 					hit++;
    375 				else
    376 					hit += 2;
    377 			}
    378 			if (sp->file->captured != 0) {
    379 				if (index <= 1)
    380 					hit--;
    381 				else
    382 					hit -= 2;
    383 			}
    384 			hit += AMMO[index][load - 1];
    385 			temp = sp->specs->class;
    386 			if ((temp >= 5 || temp == 1) && windspeed == 5)
    387 				hit--;
    388 			if (windspeed == 6 && temp == 4)
    389 				hit -= 2;
    390 			if (windspeed == 6 && temp <= 3)
    391 				hit--;
    392 			if (hit >= 0) {
    393 				if (load != L_GRAPE)
    394 					hit = hit > 10 ? 10 : hit;
    395 				table(shootat, load, hit, closest, sp, die());
    396 			}
    397 		}
    398 	}
    399 }
    400 
    401 int
    402 next()
    403 {
    404 	if (++turn % 55 == 0) {
    405 		if (alive)
    406 			alive = 0;
    407 		else
    408 			people = 0;
    409 	}
    410 	if (people <= 0 || windspeed == 7) {
    411 		struct ship *s;
    412 		struct ship *bestship = NULL;
    413 		float net, best = 0.0;
    414 		foreachship(s) {
    415 			if (*s->file->captain)
    416 				continue;
    417 			net = (float)s->file->points / s->specs->pts;
    418 			if (net > best) {
    419 				best = net;
    420 				bestship = s;
    421 			}
    422 		}
    423 		if (best > 0.0) {
    424 			char *tp = getenv("WOTD");
    425 			const char *p;
    426 			if (tp == 0)
    427 				p = "Driver";
    428 			else {
    429 				if (islower(*tp))
    430 					*tp = toupper(*tp);
    431 				p = tp;
    432 			}
    433 			(void) strncpy(bestship->file->captain, p,
    434 				sizeof bestship->file->captain);
    435 			bestship->file->captain
    436 				[sizeof bestship->file->captain - 1] = 0;
    437 			logger(bestship);
    438 		}
    439 		return -1;
    440 	}
    441 	Write(W_TURN, SHIP(0), turn, 0, 0, 0);
    442 	if (turn % 7 == 0 && (die() >= cc->windchange || !windspeed)) {
    443 		switch (die()) {
    444 		case 1:
    445 			winddir = 1;
    446 			break;
    447 		case 2:
    448 			break;
    449 		case 3:
    450 			winddir++;
    451 			break;
    452 		case 4:
    453 			winddir--;
    454 			break;
    455 		case 5:
    456 			winddir += 2;
    457 			break;
    458 		case 6:
    459 			winddir -= 2;
    460 			break;
    461 		}
    462 		if (winddir > 8)
    463 			winddir -= 8;
    464 		if (winddir < 1)
    465 			winddir += 8;
    466 		if (windspeed)
    467 			switch (die()) {
    468 			case 1:
    469 			case 2:
    470 				windspeed--;
    471 				break;
    472 			case 5:
    473 			case 6:
    474 				windspeed++;
    475 				break;
    476 			}
    477 		else
    478 			windspeed++;
    479 		Write(W_WIND, SHIP(0), winddir, windspeed, 0, 0);
    480 	}
    481 	return 0;
    482 }
    483