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