assorted.c revision 1.10 1 /* $NetBSD: assorted.c,v 1.10 2001/01/01 21:57:37 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[] = "@(#)assorted.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: assorted.c,v 1.10 2001/01/01 21:57:37 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "extern.h"
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <err.h>
49
50 static void strike (struct ship *, struct ship *);
51
52 void
53 table(int rig, int shot, int hittable, struct ship *on, struct ship *from, int roll)
54 {
55 int hhits = 0, chits = 0, ghits = 0, rhits = 0;
56 int Ghit = 0, Hhit = 0, Rhit = 0, Chit = 0;
57 int guns, car, pc, hull;
58 int crew[3];
59 int n;
60 int rigg[4];
61 const char *message;
62 const struct Tables *tp;
63
64 pc = on->file->pcrew;
65 hull = on->specs->hull;
66 crew[0] = on->specs->crew1;
67 crew[1] = on->specs->crew2;
68 crew[2] = on->specs->crew3;
69 rigg[0] = on->specs->rig1;
70 rigg[1] = on->specs->rig2;
71 rigg[2] = on->specs->rig3;
72 rigg[3] = on->specs->rig4;
73 if (shot == L_GRAPE)
74 Chit = chits = hittable;
75 else {
76 tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
77 Chit = chits = tp->C;
78 Rhit = rhits = tp->R;
79 Hhit = hhits = tp->H;
80 Ghit = ghits = tp->G;
81 if (on->file->FS)
82 rhits *= 2;
83 if (shot == L_CHAIN) {
84 Ghit = ghits = 0;
85 Hhit = hhits = 0;
86 }
87 }
88 if (on->file->captured != 0) {
89 pc -= (chits + 1) / 2;
90 chits /= 2;
91 }
92 for (n = 0; n < 3; n++)
93 if (chits > crew[n]) {
94 chits -= crew[n];
95 crew[n] = 0;
96 } else {
97 crew[n] -= chits;
98 chits = 0;
99 }
100 for (n = 0; n < 3; n++)
101 if (rhits > rigg[n]){
102 rhits -= rigg[n];
103 rigg[n] = 0;
104 } else {
105 rigg[n] -= rhits;
106 rhits = 0;
107 }
108 if (rigg[3] != -1 && rhits > rigg[3]) {
109 rhits -= rigg[3];
110 rigg[3] = 0;
111 } else if (rigg[3] != -1) {
112 rigg[3] -= rhits;
113 }
114 if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
115 makemsg(on, "dismasted!");
116 if (portside(from, on, 0)) {
117 guns = on->specs->gunR;
118 car = on->specs->carR;
119 } else {
120 guns = on->specs->gunL;
121 car = on->specs->carL;
122 }
123 if (ghits > car) {
124 ghits -= car;
125 car = 0;
126 } else {
127 car -= ghits;
128 ghits = 0;
129 }
130 if (ghits > guns){
131 ghits -= guns;
132 guns = 0;
133 } else {
134 guns -= ghits;
135 ghits = 0;
136 }
137 hull -= ghits;
138 if (Ghit)
139 Write(portside(from, on, 0) ? W_GUNR : W_GUNL,
140 on, guns, car, 0, 0);
141 hull -= hhits;
142 hull = hull < 0 ? 0 : hull;
143 if (on->file->captured != 0 && Chit)
144 Write(W_PCREW, on, pc, 0, 0, 0);
145 if (Hhit)
146 Write(W_HULL, on, hull, 0, 0, 0);
147 if (Chit)
148 Write(W_CREW, on, crew[0], crew[1], crew[2], 0);
149 if (Rhit)
150 Write(W_RIGG, on, rigg[0], rigg[1], rigg[2], rigg[3]);
151 switch (shot) {
152 case L_ROUND:
153 message = "firing round shot on $$";
154 break;
155 case L_GRAPE:
156 message = "firing grape shot on $$";
157 break;
158 case L_CHAIN:
159 message = "firing chain shot on $$";
160 break;
161 case L_DOUBLE:
162 message = "firing double shot on $$";
163 break;
164 case L_EXPLODE:
165 message = "exploding shot on $$";
166 break;
167 default:
168 errx(1, "Unknown shot type %d", shot);
169
170 }
171 makesignal(from, message, on);
172 if (roll == 6 && rig) {
173 switch(Rhit) {
174 case 0:
175 message = "fore topsail sheets parted";
176 break;
177 case 1:
178 message = "mizzen shrouds parted";
179 break;
180 case 2:
181 message = "main topsail yard shot away";
182 break;
183 case 4:
184 message = "fore topmast and foremast shrouds shot away";
185 break;
186 case 5:
187 message = "mizzen mast and yard shot through";
188 break;
189 case 6:
190 message = "foremast and spritsail yard shattered";
191 break;
192 case 7:
193 message = "main topmast and mizzen mast shattered";
194 break;
195 default:
196 errx(1, "Bad Rhit = %d", Rhit);
197 }
198 makemsg(on, message);
199 } else if (roll == 6) {
200 switch (Hhit) {
201 case 0:
202 message = "anchor cables severed";
203 break;
204 case 1:
205 message = "two anchor stocks shot away";
206 break;
207 case 2:
208 message = "quarterdeck bulwarks damaged";
209 break;
210 case 3:
211 message = "three gun ports shot away";
212 break;
213 case 4:
214 message = "four guns dismounted";
215 break;
216 case 5:
217 message = "rudder cables shot through";
218 Write(W_TA, on, 0, 0, 0, 0);
219 break;
220 case 6:
221 message = "shot holes below the water line";
222 break;
223 default:
224 errx(1, "Bad Hhit = %d", Hhit);
225 }
226 makemsg(on, message);
227 }
228 /*
229 if (Chit > 1 && on->file->readyL&R_INITIAL && on->file->readyR&R_INITIAL) {
230 on->specs->qual--;
231 if (on->specs->qual <= 0) {
232 makemsg(on, "crew mutinying!");
233 on->specs->qual = 5;
234 Write(W_CAPTURED, on, on->file->index, 0, 0, 0);
235 } else
236 makemsg(on, "crew demoralized");
237 Write(W_QUAL, on, on->specs->qual, 0, 0, 0);
238 }
239 */
240 if (!hull)
241 strike(on, from);
242 }
243
244 void
245 Cleansnag(struct ship *from, struct ship *to, int all, int flag)
246 {
247 if (flag & 1) {
248 Write(W_UNGRAP, from, to->file->index, all, 0, 0);
249 Write(W_UNGRAP, to, from->file->index, all, 0, 0);
250 }
251 if (flag & 2) {
252 Write(W_UNFOUL, from, to->file->index, all, 0, 0);
253 Write(W_UNFOUL, to, from->file->index, all, 0, 0);
254 }
255 if (!snagged2(from, to)) {
256 if (!snagged(from)) {
257 unboard(from, from, 1); /* defense */
258 unboard(from, from, 0); /* defense */
259 } else
260 unboard(from, to, 0); /* offense */
261 if (!snagged(to)) {
262 unboard(to, to, 1); /* defense */
263 unboard(to, to, 0); /* defense */
264 } else
265 unboard(to, from, 0); /* offense */
266 }
267 }
268
269 static void
270 strike(struct ship *ship, struct ship *from)
271 {
272 int points;
273
274 if (ship->file->struck)
275 return;
276 Write(W_STRUCK, ship, 1, 0, 0, 0);
277 points = ship->specs->pts + from->file->points;
278 Write(W_POINTS, from, points, 0, 0, 0);
279 unboard(ship, ship, 0); /* all offense */
280 unboard(ship, ship, 1); /* all defense */
281 switch (dieroll()) {
282 case 3:
283 case 4: /* ship may sink */
284 Write(W_SINK, ship, 1, 0, 0, 0);
285 break;
286 case 5:
287 case 6: /* ship may explode */
288 Write(W_EXPLODE, ship, 1, 0, 0, 0);
289 break;
290 }
291 Writestr(W_SIGNAL, ship, "striking her colours!");
292 }
293