]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/assorted.c
Don't cast the return value of calloc().
[bsdgames-darwin.git] / sail / assorted.c
1 /* $NetBSD: assorted.c,v 1.16 2009/03/14 19:35:13 dholland 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)assorted.c 8.2 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: assorted.c,v 1.16 2009/03/14 19:35:13 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include <err.h>
43 #include "extern.h"
44
45 static void strike(struct ship *, struct ship *);
46
47 void
48 table(struct ship *from, struct ship *on,
49 int rig, int shot, int hittable, int roll)
50 {
51 int hhits = 0, chits = 0, ghits = 0, rhits = 0;
52 int Ghit = 0, Hhit = 0, Rhit = 0, Chit = 0;
53 int guns, car, pc, hull;
54 int crew[3];
55 int n;
56 int rigg[4];
57 const char *message;
58 const struct Tables *tp;
59
60 pc = on->file->pcrew;
61 hull = on->specs->hull;
62 crew[0] = on->specs->crew1;
63 crew[1] = on->specs->crew2;
64 crew[2] = on->specs->crew3;
65 rigg[0] = on->specs->rig1;
66 rigg[1] = on->specs->rig2;
67 rigg[2] = on->specs->rig3;
68 rigg[3] = on->specs->rig4;
69 if (shot == L_GRAPE) {
70 Chit = chits = hittable;
71 } else {
72 tp = &(rig ? RigTable : HullTable)[hittable][roll-1];
73 Chit = chits = tp->C;
74 Rhit = rhits = tp->R;
75 Hhit = hhits = tp->H;
76 Ghit = ghits = tp->G;
77 if (on->file->FS)
78 rhits *= 2;
79 if (shot == L_CHAIN) {
80 Ghit = ghits = 0;
81 Hhit = hhits = 0;
82 }
83 }
84 if (on->file->captured != 0) {
85 pc -= (chits + 1) / 2;
86 chits /= 2;
87 }
88 for (n = 0; n < 3; n++) {
89 if (chits > crew[n]) {
90 chits -= crew[n];
91 crew[n] = 0;
92 } else {
93 crew[n] -= chits;
94 chits = 0;
95 }
96 }
97 for (n = 0; n < 3; n++) {
98 if (rhits > rigg[n]) {
99 rhits -= rigg[n];
100 rigg[n] = 0;
101 } else {
102 rigg[n] -= rhits;
103 rhits = 0;
104 }
105 }
106 if (rigg[3] != -1 && rhits > rigg[3]) {
107 rhits -= rigg[3];
108 rigg[3] = 0;
109 } else if (rigg[3] != -1) {
110 rigg[3] -= rhits;
111 }
112 if (rig && !rigg[2] && (!rigg[3] || rigg[3] == -1))
113 makemsg(on, "dismasted!");
114 if (portside(from, on, 0)) {
115 guns = on->specs->gunR;
116 car = on->specs->carR;
117 } else {
118 guns = on->specs->gunL;
119 car = on->specs->carL;
120 }
121 if (ghits > car) {
122 ghits -= car;
123 car = 0;
124 } else {
125 car -= ghits;
126 ghits = 0;
127 }
128 if (ghits > guns) {
129 ghits -= guns;
130 guns = 0;
131 } else {
132 guns -= ghits;
133 ghits = 0;
134 }
135 hull -= ghits;
136 if (Ghit)
137 Write(portside(from, on, 0) ? W_GUNR : W_GUNL,
138 on, guns, car, 0, 0);
139 hull -= hhits;
140 hull = hull < 0 ? 0 : hull;
141 if (on->file->captured != 0 && Chit)
142 Write(W_PCREW, on, pc, 0, 0, 0);
143 if (Hhit)
144 Write(W_HULL, on, hull, 0, 0, 0);
145 if (Chit)
146 Write(W_CREW, on, crew[0], crew[1], crew[2], 0);
147 if (Rhit)
148 Write(W_RIGG, on, rigg[0], rigg[1], rigg[2], rigg[3]);
149 switch (shot) {
150 case L_ROUND:
151 message = "firing round shot on $$";
152 break;
153 case L_GRAPE:
154 message = "firing grape shot on $$";
155 break;
156 case L_CHAIN:
157 message = "firing chain shot on $$";
158 break;
159 case L_DOUBLE:
160 message = "firing double shot on $$";
161 break;
162 case L_EXPLODE:
163 message = "exploding shot on $$";
164 break;
165 default:
166 errx(1, "Unknown shot type %d", shot);
167
168 }
169 makesignal(from, message, on);
170 if (roll == 6 && rig) {
171 switch(Rhit) {
172 case 0:
173 message = "fore topsail sheets parted";
174 break;
175 case 1:
176 message = "mizzen shrouds parted";
177 break;
178 case 2:
179 message = "main topsail yard shot away";
180 break;
181 case 4:
182 message = "fore topmast and foremast shrouds shot away";
183 break;
184 case 5:
185 message = "mizzen mast and yard shot through";
186 break;
187 case 6:
188 message = "foremast and spritsail yard shattered";
189 break;
190 case 7:
191 message = "main topmast and mizzen mast shattered";
192 break;
193 default:
194 errx(1, "Bad Rhit = %d", Rhit);
195 }
196 makemsg(on, message);
197 } else if (roll == 6) {
198 switch (Hhit) {
199 case 0:
200 message = "anchor cables severed";
201 break;
202 case 1:
203 message = "two anchor stocks shot away";
204 break;
205 case 2:
206 message = "quarterdeck bulwarks damaged";
207 break;
208 case 3:
209 message = "three gun ports shot away";
210 break;
211 case 4:
212 message = "four guns dismounted";
213 break;
214 case 5:
215 message = "rudder cables shot through";
216 Write(W_TA, on, 0, 0, 0, 0);
217 break;
218 case 6:
219 message = "shot holes below the water line";
220 break;
221 default:
222 errx(1, "Bad Hhit = %d", Hhit);
223 }
224 makemsg(on, message);
225 }
226 /*
227 if (Chit > 1 && on->file->readyL & R_INITIAL &&
228 on->file->readyR & R_INITIAL) {
229 on->specs->qual--;
230 if (on->specs->qual <= 0) {
231 makemsg(on, "crew mutinying!");
232 on->specs->qual = 5;
233 Write(W_CAPTURED, on, on->file->index, 0, 0, 0);
234 } else {
235 makemsg(on, "crew demoralized");
236 }
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 }