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