Home | History | Annotate | Line # | Download | only in sail
dr_1.c revision 1.2
      1 /*
      2  * Copyright (c) 1983 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)dr_1.c	5.4 (Berkeley) 6/1/90";*/
     36 static char rcsid[] = "$Id: dr_1.c,v 1.2 1993/08/01 18:51:52 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include "driver.h"
     40 
     41 unfoul()
     42 {
     43 	register struct ship *sp;
     44 	struct ship *to;
     45 	register int nat;
     46 	register i;
     47 
     48 	foreachship(sp) {
     49 		if (sp->file->captain[0])
     50 			continue;
     51 		nat = capship(sp)->nationality;
     52 		foreachship(to) {
     53 			if (nat != capship(to)->nationality &&
     54 			    !toughmelee(sp, to, 0, 0))
     55 				continue;
     56 			for (i = fouled2(sp, to); --i >= 0;)
     57 				if (die() <= 2)
     58 					cleanfoul(sp, to, 0);
     59 		}
     60 	}
     61 }
     62 
     63 boardcomp()
     64 {
     65 	int crew[3];
     66 	register struct ship *sp, *sq;
     67 
     68 	foreachship(sp) {
     69 		if (*sp->file->captain)
     70 			continue;
     71 		if (sp->file->dir == 0)
     72 			continue;
     73 		if (sp->file->struck || sp->file->captured != 0)
     74 			continue;
     75 		if (!snagged(sp))
     76 			continue;
     77 		crew[0] = sp->specs->crew1 != 0;
     78 		crew[1] = sp->specs->crew2 != 0;
     79 		crew[2] = sp->specs->crew3 != 0;
     80 		foreachship(sq) {
     81 			if (!Xsnagged2(sp, sq))
     82 				continue;
     83 			if (meleeing(sp, sq))
     84 				continue;
     85 			if (!sq->file->dir
     86 				|| sp->nationality == capship(sq)->nationality)
     87 				continue;
     88 			switch (sp->specs->class - sq->specs->class) {
     89 			case -3: case -4: case -5:
     90 				if (crew[0]) {
     91 					/* OBP */
     92 					sendbp(sp, sq, crew[0]*100, 0);
     93 					crew[0] = 0;
     94 				} else if (crew[1]){
     95 					/* OBP */
     96 					sendbp(sp, sq, crew[1]*10, 0);
     97 					crew[1] = 0;
     98 				}
     99 				break;
    100 			case -2:
    101 				if (crew[0] || crew[1]) {
    102 					/* OBP */
    103 					sendbp(sp, sq, crew[0]*100+crew[1]*10,
    104 						0);
    105 					crew[0] = crew[1] = 0;
    106 				}
    107 				break;
    108 			case -1: case 0: case 1:
    109 				if (crew[0]) {
    110 					/* OBP */
    111 					sendbp(sp, sq, crew[0]*100+crew[1]*10,
    112 						0);
    113 					crew[0] = crew[1] = 0;
    114 				}
    115 				break;
    116 			case 2: case 3: case 4: case 5:
    117 				/* OBP */
    118 				sendbp(sp, sq, crew[0]*100+crew[1]*10+crew[2],
    119 					0);
    120 				crew[0] = crew[1] = crew[2] = 0;
    121 				break;
    122 			}
    123 		}
    124 	}
    125 }
    126 
    127 fightitout(from, to, key)
    128 struct ship *from, *to;
    129 int key;
    130 {
    131 	struct ship *fromcap, *tocap;
    132 	int crewfrom[3], crewto[3], menfrom, mento;
    133 	int pcto, pcfrom, fromstrength, strengthto, frominjured, toinjured;
    134 	int topoints;
    135 	int index, totalfrom = 0, totalto = 0;
    136 	int count;
    137 	char message[60];
    138 
    139 	menfrom = mensent(from, to, crewfrom, &fromcap, &pcfrom, key);
    140 	mento = mensent(to, from, crewto, &tocap, &pcto, 0);
    141 	if (fromcap == 0)
    142 		fromcap = from;
    143 	if (tocap == 0)
    144 		tocap = to;
    145 	if (key) {
    146 		if (!menfrom) {		 /* if crew surprised */
    147 			if (fromcap == from)
    148 				menfrom = from->specs->crew1
    149 					+ from->specs->crew2
    150 					+ from->specs->crew3;
    151 			else
    152 				menfrom = from->file->pcrew;
    153 		} else {
    154 			menfrom *= 2;	/* DBP's fight at an advantage */
    155 		}
    156 	}
    157 	fromstrength = menfrom * fromcap->specs->qual;
    158 	strengthto = mento * tocap->specs->qual;
    159 	for (count = 0;
    160 	     (fromstrength < strengthto * 3 && strengthto < fromstrength * 3
    161 	      || fromstrength == -1) && count < 4;
    162 	     count++) {
    163 		index = fromstrength/10;
    164 		if (index > 8)
    165 			index = 8;
    166 		toinjured = MT[index][2 - die() / 3];
    167 		totalto += toinjured;
    168 		index = strengthto/10;
    169 		if (index > 8)
    170 			index = 8;
    171 		frominjured = MT[index][2 - die() / 3];
    172 		totalfrom += frominjured;
    173 		menfrom -= frominjured;
    174 		mento -= toinjured;
    175 		fromstrength = menfrom * fromcap->specs->qual;
    176 		strengthto = mento * tocap->specs->qual;
    177 	}
    178 	if (fromstrength >= strengthto * 3 || count == 4) {
    179 		unboard(to, from, 0);
    180 		subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
    181 		subtract(to, totalto, crewto, tocap, pcto);
    182 		makesignal(from, "boarders from %s repelled", to);
    183 		(void) sprintf(message, "killed in melee: %d.  %s: %d",
    184 			totalto, from->shipname, totalfrom);
    185 		Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0);
    186 		if (key)
    187 			return 1;
    188 	} else if (strengthto >= fromstrength * 3) {
    189 		unboard(from, to, 0);
    190 		subtract(from, totalfrom, crewfrom, fromcap, pcfrom);
    191 		subtract(to, totalto, crewto, tocap, pcto);
    192 		if (key) {
    193 			if (fromcap != from)
    194 				Write(W_POINTS, fromcap, 0,
    195 					fromcap->file->points -
    196 						from->file->struck
    197 						? from->specs->pts
    198 						: 2 * from->specs->pts,
    199 					0, 0, 0);
    200 
    201 /* ptr1 points to the shipspec for the ship that was just unboarded.
    202    I guess that what is going on here is that the pointer is multiplied
    203    or something. */
    204 
    205 			Write(W_CAPTURED, from, 0, to->file->index, 0, 0, 0);
    206 			topoints = 2 * from->specs->pts + to->file->points;
    207 			if (from->file->struck)
    208 				topoints -= from->specs->pts;
    209 			Write(W_POINTS, to, 0, topoints, 0, 0, 0);
    210 			mento = crewto[0] ? crewto[0] : crewto[1];
    211 			if (mento) {
    212 				subtract(to, mento, crewto, tocap, pcto);
    213 				subtract(from, - mento, crewfrom, to, 0);
    214 			}
    215 			(void) sprintf(message, "captured by the %s!",
    216 				to->shipname);
    217 			Write(W_SIGNAL, from, 1, (int) message, 0, 0, 0);
    218 			(void) sprintf(message, "killed in melee: %d.  %s: %d",
    219 				totalto, from->shipname, totalfrom);
    220 			Write(W_SIGNAL, to, 1, (int) message, 0, 0, 0);
    221 			mento = 0;
    222 			return 0;
    223 		}
    224 	}
    225 	return 0;
    226 }
    227 
    228 resolve()
    229 {
    230 	int thwart;
    231 	register struct ship *sp, *sq;
    232 
    233 	foreachship(sp) {
    234 		if (sp->file->dir == 0)
    235 			continue;
    236 		for (sq = sp + 1; sq < ls; sq++)
    237 			if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
    238 				(void) fightitout(sp, sq, 0);
    239 		thwart = 2;
    240 		foreachship(sq) {
    241 			if (sq->file->dir && meleeing(sq, sp))
    242 				thwart = fightitout(sp, sq, 1);
    243 			if (!thwart)
    244 				break;
    245 		}
    246 		if (!thwart) {
    247 			foreachship(sq) {
    248 				if (sq->file->dir && meleeing(sq, sp))
    249 					unboard(sq, sp, 0);
    250 				unboard(sp, sq, 0);
    251 			}
    252 			unboard(sp, sp, 1);
    253 		} else if (thwart == 2)
    254 			unboard(sp, sp, 1);
    255 	}
    256 }
    257 
    258 compcombat()
    259 {
    260 	register n;
    261 	register struct ship *sp;
    262 	struct ship *closest;
    263 	int crew[3], men = 0, target, temp;
    264 	int r, guns, ready, load, car;
    265 	int index, rakehim, sternrake;
    266 	int shootat, hit;
    267 
    268 	foreachship(sp) {
    269 		if (sp->file->captain[0] || sp->file->dir == 0)
    270 			continue;
    271 		crew[0] = sp->specs->crew1;
    272 		crew[1] = sp->specs->crew2;
    273 		crew[2] = sp->specs->crew3;
    274 		for (n = 0; n < 3; n++) {
    275 			if (sp->file->OBP[n].turnsent)
    276 				men += sp->file->OBP[n].mensent;
    277 		}
    278 		for (n = 0; n < 3; n++) {
    279 			if (sp->file->DBP[n].turnsent)
    280 				men += sp->file->DBP[n].mensent;
    281 		}
    282 		if (men){
    283 			crew[0] = men/100 ? 0 : crew[0] != 0;
    284 			crew[1] = (men%100)/10 ? 0 : crew[1] != 0;
    285 			crew[2] = men%10 ? 0 : crew[2] != 0;
    286 		}
    287 		for (r = 0; r < 2; r++) {
    288 			if (!crew[2])
    289 				continue;
    290 			if (sp->file->struck)
    291 				continue;
    292 			if (r) {
    293 				ready = sp->file->readyR;
    294 				guns = sp->specs->gunR;
    295 				car = sp->specs->carR;
    296 			} else {
    297 				ready = sp->file->readyL;
    298 				guns = sp->specs->gunL;
    299 				car = sp->specs->carL;
    300 			}
    301 			if (!guns && !car)
    302 				continue;
    303 			if ((ready & R_LOADED) == 0)
    304 				continue;
    305 			closest = closestenemy(sp, r ? 'r' : 'l', 0);
    306 			if (closest == 0)
    307 				continue;
    308 			if (range(closest, sp) > range(sp, closestenemy(sp, r ? 'r' : 'l', 1)))
    309 				continue;
    310 			if (closest->file->struck)
    311 				continue;
    312 			target = range(sp, closest);
    313 			if (target > 10)
    314 				continue;
    315 			if (!guns && target >= 3)
    316 				continue;
    317 			load = L_ROUND;
    318 			if (target == 1 && sp->file->loadwith == L_GRAPE)
    319 				load = L_GRAPE;
    320 			if (target <= 3 && closest->file->FS)
    321 				load = L_CHAIN;
    322 			if (target == 1 && load != L_GRAPE)
    323 				load = L_DOUBLE;
    324 			if (load > L_CHAIN && target < 6)
    325 				shootat = HULL;
    326 			else
    327 				shootat = RIGGING;
    328 			rakehim = gunsbear(sp, closest)
    329 				&& !gunsbear(closest, sp);
    330 			temp = portside(closest, sp, 1)
    331 				- closest->file->dir + 1;
    332 			if (temp < 1)
    333 				temp += 8;
    334 			if (temp > 8)
    335 				temp -= 8;
    336 			sternrake = temp > 4 && temp < 6;
    337 			index = guns;
    338 			if (target < 3)
    339 				index += car;
    340 			index = (index - 1) / 3;
    341 			index = index > 8 ? 8 : index;
    342 			if (!rakehim)
    343 				hit = HDT[index][target-1];
    344 			else
    345 				hit = HDTrake[index][target-1];
    346 			if (rakehim && sternrake)
    347 				hit++;
    348 			hit += QUAL[index][capship(sp)->specs->qual - 1];
    349 			for (n = 0; n < 3 && sp->file->captured == 0; n++)
    350 				if (!crew[n])
    351 					if (index <= 5)
    352 						hit--;
    353 					else
    354 						hit -= 2;
    355 			if (ready & R_INITIAL) {
    356 				if (!r)
    357 					sp->file->readyL &= ~R_INITIAL;
    358 				else
    359 					sp->file->readyR &= ~R_INITIAL;
    360 				if (index <= 3)
    361 					hit++;
    362 				else
    363 					hit += 2;
    364 			}
    365 			if (sp->file->captured != 0)
    366 				if (index <= 1)
    367 					hit--;
    368 				else
    369 					hit -= 2;
    370 			hit += AMMO[index][load - 1];
    371 			temp = sp->specs->class;
    372 			if ((temp >= 5 || temp == 1) && windspeed == 5)
    373 				hit--;
    374 			if (windspeed == 6 && temp == 4)
    375 				hit -= 2;
    376 			if (windspeed == 6 && temp <= 3)
    377 				hit--;
    378 			if (hit >= 0) {
    379 				if (load != L_GRAPE)
    380 					hit = hit > 10 ? 10 : hit;
    381 				table(shootat, load, hit, closest, sp, die());
    382 			}
    383 		}
    384 	}
    385 }
    386 
    387 next()
    388 {
    389 	if (++turn % 55 == 0)
    390 		if (alive)
    391 			alive = 0;
    392 		else
    393 			people = 0;
    394 	if (people <= 0 || windspeed == 7) {
    395 		register struct ship *s;
    396 		struct ship *bestship;
    397 		float net, best = 0.0;
    398 		foreachship(s) {
    399 			if (*s->file->captain)
    400 				continue;
    401 			net = (float)s->file->points / s->specs->pts;
    402 			if (net > best) {
    403 				best = net;
    404 				bestship = s;
    405 			}
    406 		}
    407 		if (best > 0.0) {
    408 			char *p = getenv("WOTD");
    409 			if (p == 0)
    410 				p = "Driver";
    411 			if (islower(*p))
    412 				*p = toupper(*p);
    413 			(void) strncpy(bestship->file->captain, p,
    414 				sizeof bestship->file->captain);
    415 			bestship->file->captain
    416 				[sizeof bestship->file->captain - 1] = 0;
    417 			log(bestship);
    418 		}
    419 		return -1;
    420 	}
    421 	Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0);
    422 	if (turn % 7 == 0 && (die() >= cc->windchange || !windspeed)) {
    423 		switch (die()) {
    424 		case 1:
    425 			winddir = 1;
    426 			break;
    427 		case 2:
    428 			break;
    429 		case 3:
    430 			winddir++;
    431 			break;
    432 		case 4:
    433 			winddir--;
    434 			break;
    435 		case 5:
    436 			winddir += 2;
    437 			break;
    438 		case 6:
    439 			winddir -= 2;
    440 			break;
    441 		}
    442 		if (winddir > 8)
    443 			winddir -= 8;
    444 		if (winddir < 1)
    445 			winddir += 8;
    446 		if (windspeed)
    447 			switch (die()) {
    448 			case 1:
    449 			case 2:
    450 				windspeed--;
    451 				break;
    452 			case 5:
    453 			case 6:
    454 				windspeed++;
    455 				break;
    456 			}
    457 		else
    458 			windspeed++;
    459 		Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0);
    460 	}
    461 	return 0;
    462 }
    463