Home | History | Annotate | Line # | Download | only in sail
assorted.c revision 1.12
      1  1.12     jwise /*	$NetBSD: assorted.c,v 1.12 2001/01/04 03:21:16 jwise 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.5  christos #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.3       cgd #if 0
     39   1.4       tls static char sccsid[] = "@(#)assorted.c	8.2 (Berkeley) 4/28/95";
     40   1.3       cgd #else
     41  1.12     jwise __RCSID("$NetBSD: assorted.c,v 1.12 2001/01/04 03:21:16 jwise Exp $");
     42   1.3       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.5  christos #include <stdlib.h>
     46   1.5  christos #include <err.h>
     47  1.12     jwise #include "extern.h"
     48   1.1       cgd 
     49  1.11     jwise void	table(int, int, int, struct ship *, struct ship *, int);
     50  1.11     jwise void	Cleansnag(struct ship *, struct ship *, int, int);
     51  1.10     jwise static void strike (struct ship *, struct ship *);
     52   1.5  christos 
     53   1.5  christos void
     54  1.10     jwise table(int rig, int shot, int hittable, struct ship *on, struct ship *from, int roll)
     55   1.1       cgd {
     56   1.5  christos 	int hhits = 0, chits = 0, ghits = 0, rhits = 0;
     57   1.1       cgd 	int Ghit = 0, Hhit = 0, Rhit = 0, Chit = 0;
     58   1.1       cgd 	int guns, car, pc, hull;
     59   1.1       cgd 	int crew[3];
     60   1.5  christos 	int n;
     61   1.1       cgd 	int rigg[4];
     62   1.8       jsm 	const char *message;
     63   1.8       jsm 	const struct Tables *tp;
     64   1.1       cgd 
     65   1.1       cgd 	pc = on->file->pcrew;
     66   1.1       cgd 	hull = on->specs->hull;
     67   1.1       cgd 	crew[0] = on->specs->crew1;
     68   1.1       cgd 	crew[1] = on->specs->crew2;
     69   1.1       cgd 	crew[2] = on->specs->crew3;
     70   1.1       cgd 	rigg[0] = on->specs->rig1;
     71   1.1       cgd 	rigg[1] = on->specs->rig2;
     72   1.1       cgd 	rigg[2] = on->specs->rig3;
     73   1.1       cgd 	rigg[3] = on->specs->rig4;
     74   1.1       cgd 	if (shot == L_GRAPE)
     75   1.1       cgd 		Chit = chits = hittable;
     76   1.1       cgd 	else {
     77   1.1       cgd 		tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
     78   1.1       cgd 		Chit = chits = tp->C;
     79   1.1       cgd 		Rhit = rhits = tp->R;
     80   1.1       cgd 		Hhit = hhits = tp->H;
     81   1.1       cgd 		Ghit = ghits = tp->G;
     82   1.1       cgd 		if (on->file->FS)
     83   1.1       cgd 			rhits *= 2;
     84   1.1       cgd 		if (shot == L_CHAIN) {
     85   1.1       cgd 			Ghit = ghits = 0;
     86   1.1       cgd 			Hhit = hhits = 0;
     87   1.1       cgd 		}
     88   1.1       cgd 	}
     89   1.1       cgd 	if (on->file->captured != 0) {
     90   1.1       cgd 		pc -= (chits + 1) / 2;
     91   1.1       cgd 		chits /= 2;
     92   1.1       cgd 	}
     93   1.1       cgd 	for (n = 0; n < 3; n++)
     94   1.1       cgd 		if (chits > crew[n]) {
     95   1.1       cgd 			chits -= crew[n];
     96   1.1       cgd 			crew[n] = 0;
     97   1.1       cgd 		} else {
     98   1.1       cgd 			crew[n] -= chits;
     99   1.1       cgd 			chits = 0;
    100   1.1       cgd 		}
    101   1.1       cgd 	for (n = 0; n < 3; n++)
    102   1.1       cgd 		if (rhits > rigg[n]){
    103   1.1       cgd 			rhits -= rigg[n];
    104   1.1       cgd 			rigg[n] = 0;
    105   1.1       cgd 		} else {
    106   1.1       cgd 			rigg[n] -= rhits;
    107   1.1       cgd 			rhits = 0;
    108   1.1       cgd 		}
    109   1.1       cgd 	if (rigg[3] != -1 && rhits > rigg[3]) {
    110   1.1       cgd 		rhits -= rigg[3];
    111   1.1       cgd 		rigg[3] = 0;
    112   1.1       cgd 	} else if (rigg[3] != -1) {
    113   1.1       cgd 		rigg[3] -= rhits;
    114   1.1       cgd 	}
    115   1.1       cgd 	if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
    116   1.6  christos 		makemsg(on, "dismasted!");
    117   1.1       cgd 	if (portside(from, on, 0)) {
    118   1.1       cgd 		guns = on->specs->gunR;
    119   1.1       cgd 		car = on->specs->carR;
    120   1.1       cgd 	} else {
    121   1.1       cgd 		guns = on->specs->gunL;
    122   1.1       cgd 		car = on->specs->carL;
    123   1.1       cgd 	}
    124   1.1       cgd 	if (ghits > car) {
    125   1.1       cgd 		ghits -= car;
    126   1.1       cgd 		car = 0;
    127   1.1       cgd 	} else {
    128   1.1       cgd 		car -= ghits;
    129   1.1       cgd 		ghits = 0;
    130   1.1       cgd 	}
    131   1.1       cgd 	if (ghits > guns){
    132   1.1       cgd 		ghits -= guns;
    133   1.1       cgd 		guns = 0;
    134   1.1       cgd 	} else {
    135   1.1       cgd 		guns -= ghits;
    136   1.1       cgd 		ghits = 0;
    137   1.1       cgd 	}
    138   1.1       cgd 	hull -= ghits;
    139   1.1       cgd 	if (Ghit)
    140   1.1       cgd 		Write(portside(from, on, 0) ? W_GUNR : W_GUNL,
    141   1.7   hubertf 			on, guns, car, 0, 0);
    142   1.1       cgd 	hull -= hhits;
    143   1.1       cgd 	hull = hull < 0 ? 0 : hull;
    144   1.1       cgd 	if (on->file->captured != 0 && Chit)
    145   1.7   hubertf 		Write(W_PCREW, on, pc, 0, 0, 0);
    146   1.1       cgd 	if (Hhit)
    147   1.7   hubertf 		Write(W_HULL, on, hull, 0, 0, 0);
    148   1.1       cgd 	if (Chit)
    149   1.7   hubertf 		Write(W_CREW, on, crew[0], crew[1], crew[2], 0);
    150   1.1       cgd 	if (Rhit)
    151   1.7   hubertf 		Write(W_RIGG, on, rigg[0], rigg[1], rigg[2], rigg[3]);
    152   1.1       cgd 	switch (shot) {
    153   1.1       cgd 	case L_ROUND:
    154   1.6  christos 		message = "firing round shot on $$";
    155   1.1       cgd 		break;
    156   1.1       cgd 	case L_GRAPE:
    157   1.6  christos 		message = "firing grape shot on $$";
    158   1.1       cgd 		break;
    159   1.1       cgd 	case L_CHAIN:
    160   1.6  christos 		message = "firing chain shot on $$";
    161   1.1       cgd 		break;
    162   1.1       cgd 	case L_DOUBLE:
    163   1.6  christos 		message = "firing double shot on $$";
    164   1.1       cgd 		break;
    165   1.1       cgd 	case L_EXPLODE:
    166   1.6  christos 		message = "exploding shot on $$";
    167   1.5  christos 		break;
    168   1.5  christos 	default:
    169   1.5  christos 		errx(1, "Unknown shot type %d", shot);
    170   1.5  christos 
    171   1.1       cgd 	}
    172   1.1       cgd 	makesignal(from, message, on);
    173   1.1       cgd 	if (roll == 6 && rig) {
    174   1.1       cgd 		switch(Rhit) {
    175   1.1       cgd 		case 0:
    176   1.1       cgd 			message = "fore topsail sheets parted";
    177   1.1       cgd 			break;
    178   1.1       cgd 		case 1:
    179   1.1       cgd 			message = "mizzen shrouds parted";
    180   1.1       cgd 			break;
    181   1.1       cgd 		case 2:
    182   1.1       cgd 			message = "main topsail yard shot away";
    183   1.1       cgd 			break;
    184   1.1       cgd 		case 4:
    185   1.1       cgd 			message = "fore topmast and foremast shrouds shot away";
    186   1.1       cgd 			break;
    187   1.1       cgd 		case 5:
    188   1.1       cgd 			message = "mizzen mast and yard shot through";
    189   1.1       cgd 			break;
    190   1.1       cgd 		case 6:
    191   1.1       cgd 			message = "foremast and spritsail yard shattered";
    192   1.1       cgd 			break;
    193   1.1       cgd 		case 7:
    194   1.1       cgd 			message = "main topmast and mizzen mast shattered";
    195   1.1       cgd 			break;
    196   1.5  christos 		default:
    197   1.5  christos 			errx(1, "Bad Rhit = %d", Rhit);
    198   1.1       cgd 		}
    199   1.6  christos 		makemsg(on, message);
    200   1.1       cgd 	} else if (roll == 6) {
    201   1.1       cgd 		switch (Hhit) {
    202   1.1       cgd 		case 0:
    203   1.1       cgd 			message = "anchor cables severed";
    204   1.1       cgd 			break;
    205   1.1       cgd 		case 1:
    206   1.1       cgd 			message = "two anchor stocks shot away";
    207   1.1       cgd 			break;
    208   1.1       cgd 		case 2:
    209   1.1       cgd 			message = "quarterdeck bulwarks damaged";
    210   1.1       cgd 			break;
    211   1.1       cgd 		case 3:
    212   1.1       cgd 			message = "three gun ports shot away";
    213   1.1       cgd 			break;
    214   1.1       cgd 		case 4:
    215   1.1       cgd 			message = "four guns dismounted";
    216   1.1       cgd 			break;
    217   1.1       cgd 		case 5:
    218   1.1       cgd 			message = "rudder cables shot through";
    219   1.7   hubertf 			Write(W_TA, on, 0, 0, 0, 0);
    220   1.1       cgd 			break;
    221   1.1       cgd 		case 6:
    222   1.1       cgd 			message = "shot holes below the water line";
    223   1.1       cgd 			break;
    224   1.5  christos 		default:
    225   1.5  christos 			errx(1, "Bad Hhit = %d", Hhit);
    226   1.1       cgd 		}
    227   1.6  christos 		makemsg(on, message);
    228   1.1       cgd 	}
    229   1.1       cgd 	/*
    230   1.1       cgd 	if (Chit > 1 && on->file->readyL&R_INITIAL && on->file->readyR&R_INITIAL) {
    231   1.1       cgd 		on->specs->qual--;
    232   1.1       cgd 		if (on->specs->qual <= 0) {
    233   1.6  christos 			makemsg(on, "crew mutinying!");
    234   1.1       cgd 			on->specs->qual = 5;
    235   1.7   hubertf 			Write(W_CAPTURED, on, on->file->index, 0, 0, 0);
    236   1.1       cgd 		} else
    237   1.6  christos 			makemsg(on, "crew demoralized");
    238   1.7   hubertf 		Write(W_QUAL, on, on->specs->qual, 0, 0, 0);
    239   1.1       cgd 	}
    240   1.1       cgd 	*/
    241   1.1       cgd 	if (!hull)
    242   1.1       cgd 		strike(on, from);
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.5  christos void
    246  1.10     jwise Cleansnag(struct ship *from, struct ship *to, int all, int flag)
    247   1.1       cgd {
    248   1.1       cgd 	if (flag & 1) {
    249   1.7   hubertf 		Write(W_UNGRAP, from, to->file->index, all, 0, 0);
    250   1.7   hubertf 		Write(W_UNGRAP, to, from->file->index, all, 0, 0);
    251   1.1       cgd 	}
    252   1.1       cgd 	if (flag & 2) {
    253   1.7   hubertf 		Write(W_UNFOUL, from, to->file->index, all, 0, 0);
    254   1.7   hubertf 		Write(W_UNFOUL, to, from->file->index, all, 0, 0);
    255   1.1       cgd 	}
    256   1.1       cgd 	if (!snagged2(from, to)) {
    257   1.1       cgd 		if (!snagged(from)) {
    258   1.1       cgd 			unboard(from, from, 1);		/* defense */
    259   1.1       cgd 			unboard(from, from, 0);		/* defense */
    260   1.1       cgd 		} else
    261   1.1       cgd 			unboard(from, to, 0);		/* offense */
    262   1.1       cgd 		if (!snagged(to)) {
    263   1.1       cgd 			unboard(to, to, 1);		/* defense */
    264   1.1       cgd 			unboard(to, to, 0);		/* defense */
    265   1.1       cgd 		} else
    266   1.1       cgd 			unboard(to, from, 0);		/* offense */
    267   1.1       cgd 	}
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.5  christos static void
    271  1.10     jwise strike(struct ship *ship, struct ship *from)
    272   1.1       cgd {
    273   1.1       cgd 	int points;
    274   1.1       cgd 
    275   1.1       cgd 	if (ship->file->struck)
    276   1.1       cgd 		return;
    277   1.7   hubertf 	Write(W_STRUCK, ship, 1, 0, 0, 0);
    278   1.1       cgd 	points = ship->specs->pts + from->file->points;
    279   1.7   hubertf 	Write(W_POINTS, from, points, 0, 0, 0);
    280   1.1       cgd 	unboard(ship, ship, 0);		/* all offense */
    281   1.1       cgd 	unboard(ship, ship, 1);		/* all defense */
    282   1.9     jwise 	switch (dieroll()) {
    283   1.1       cgd 	case 3:
    284   1.1       cgd 	case 4:		/* ship may sink */
    285   1.7   hubertf 		Write(W_SINK, ship, 1, 0, 0, 0);
    286   1.1       cgd 		break;
    287   1.1       cgd 	case 5:
    288   1.1       cgd 	case 6:		/* ship may explode */
    289   1.7   hubertf 		Write(W_EXPLODE, ship, 1, 0, 0, 0);
    290   1.1       cgd 		break;
    291   1.1       cgd 	}
    292   1.7   hubertf 	Writestr(W_SIGNAL, ship, "striking her colours!");
    293   1.1       cgd }
    294