1 /*- 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)dr_3.c 8.1 (Berkeley) 5/31/93 30 * $FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $ 31 * $DragonFly: src/games/sail/dr_3.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $ 32 */ 33 34 #include "driver.h" 35 36 static bool stillmoving(int); 37 static bool isolated(struct ship *); 38 static bool push(struct ship *, struct ship *); 39 static void step(char, struct ship *, char *); 40 41 /* move all comp ships */ 42 void 43 moveall(void) 44 { 45 struct ship *sp, *sq; 46 int n; 47 int k, l; 48 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP]; 49 char moved[NSHIP]; 50 51 /* 52 * first try to create moves for OUR ships 53 */ 54 foreachship(sp) { 55 struct ship *closest; 56 int ma, ta; 57 char af; 58 59 if (sp->file->captain[0] || sp->file->dir == 0) 60 continue; 61 if (!sp->file->struck && windspeed && !snagged(sp) 62 && sp->specs->crew3) { 63 ta = maxturns(sp, &af); 64 ma = maxmove(sp, sp->file->dir, 0); 65 closest = closestenemy(sp, 0, 0); 66 if (closest == 0) 67 *sp->file->movebuf = '\0'; 68 else 69 closeon(sp, closest, sp->file->movebuf, 70 ta, ma, af); 71 } else 72 *sp->file->movebuf = '\0'; 73 } 74 /* 75 * Then execute the moves for ALL ships (dead ones too), 76 * checking for collisions and snags at each step. 77 * The old positions are saved in row[], col[], dir[]. 78 * At the end, we compare and write out the changes. 79 */ 80 n = 0; 81 foreachship(sp) { 82 if (snagged(sp)) 83 strcpy(sp->file->movebuf, "d"); 84 else 85 if (*sp->file->movebuf != 'd') 86 strcat(sp->file->movebuf, "d"); 87 row[n] = sp->file->row; 88 col[n] = sp->file->col; 89 dir[n] = sp->file->dir; 90 drift[n] = sp->file->drift; 91 moved[n] = 0; 92 n++; 93 } 94 /* 95 * Now resolve collisions. 96 * This is the tough part. 97 */ 98 for (k = 0; stillmoving(k); k++) { 99 /* 100 * Step once. 101 * And propagate the nulls at the end of sp->file->movebuf. 102 */ 103 n = 0; 104 foreachship(sp) { 105 if (!sp->file->movebuf[k]) 106 sp->file->movebuf[k+1] = '\0'; 107 else if (sp->file->dir) 108 step(sp->file->movebuf[k], sp, &moved[n]); 109 n++; 110 } 111 /* 112 * The real stuff. 113 */ 114 n = 0; 115 foreachship(sp) { 116 if (sp->file->dir == 0 || isolated(sp)) 117 goto cont1; 118 l = 0; 119 foreachship(sq) { 120 char snap = 0; 121 122 if (sp == sq) 123 goto cont2; 124 if (sq->file->dir == 0) 125 goto cont2; 126 if (!push(sp, sq)) 127 goto cont2; 128 if (snagged2(sp, sq) && range(sp, sq) > 1) 129 snap++; 130 if (!range(sp, sq) && !fouled2(sp, sq)) { 131 makesignal(sp, 132 "collision with %s (%c%c)", sq); 133 if (die() < 4) { 134 makesignal(sp, 135 "fouled with %s (%c%c)", 136 sq); 137 Write(W_FOUL, sp, l, 0, 0, 0); 138 Write(W_FOUL, sq, n, 0, 0, 0); 139 } 140 snap++; 141 } 142 if (snap) { 143 sp->file->movebuf[k + 1] = 0; 144 sq->file->movebuf[k + 1] = 0; 145 sq->file->row = sp->file->row - 1; 146 if (sp->file->dir == 1 147 || sp->file->dir == 5) 148 sq->file->col = 149 sp->file->col - 1; 150 else 151 sq->file->col = sp->file->col; 152 sq->file->dir = sp->file->dir; 153 } 154 cont2: 155 l++; 156 } 157 cont1: 158 n++; 159 } 160 } 161 /* 162 * Clear old moves. And write out new pos. 163 */ 164 n = 0; 165 foreachship(sp) { 166 if (sp->file->dir != 0) { 167 *sp->file->movebuf = 0; 168 if (row[n] != sp->file->row) 169 Write(W_ROW, sp, sp->file->row, 0, 0, 0); 170 if (col[n] != sp->file->col) 171 Write(W_COL, sp, sp->file->col, 0, 0, 0); 172 if (dir[n] != sp->file->dir) 173 Write(W_DIR, sp, sp->file->dir, 0, 0, 0); 174 if (drift[n] != sp->file->drift) 175 Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0); 176 } 177 n++; 178 } 179 } 180 181 static bool 182 stillmoving(int k) 183 { 184 struct ship *sp; 185 186 foreachship(sp) 187 if (sp->file->movebuf[k]) 188 return 1; 189 return 0; 190 } 191 192 static bool 193 isolated(struct ship *ship) 194 { 195 struct ship *sp; 196 197 foreachship(sp) { 198 if (ship != sp && range(ship, sp) <= 10) 199 return 0; 200 } 201 return 1; 202 } 203 204 static bool 205 push(struct ship *from, struct ship *to) 206 { 207 int bs, sb; 208 209 sb = to->specs->guns; 210 bs = from->specs->guns; 211 if (sb > bs) 212 return 1; 213 if (sb < bs) 214 return 0; 215 return from < to; 216 } 217 218 static void 219 step(char com, struct ship *sp, char *moved) 220 { 221 int dist; 222 223 switch (com) { 224 case 'r': 225 if (++sp->file->dir == 9) 226 sp->file->dir = 1; 227 break; 228 case 'l': 229 if (--sp->file->dir == 0) 230 sp->file->dir = 8; 231 break; 232 case '0': case '1': case '2': case '3': 233 case '4': case '5': case '6': case '7': 234 if (sp->file->dir % 2 == 0) 235 dist = dtab[com - '0']; 236 else 237 dist = com - '0'; 238 sp->file->row -= dr[sp->file->dir] * dist; 239 sp->file->col -= dc[sp->file->dir] * dist; 240 *moved = 1; 241 break; 242 case 'b': 243 break; 244 case 'd': 245 if (!*moved) { 246 if (windspeed != 0 && ++sp->file->drift > 2 && 247 ((sp->specs->class >= 3 && !snagged(sp)) 248 || (turn & 1) == 0)) { 249 sp->file->row -= dr[winddir]; 250 sp->file->col -= dc[winddir]; 251 } 252 } else { 253 sp->file->drift = 0; 254 } 255 break; 256 } 257 } 258 259 void 260 sendbp(struct ship *from, struct ship *to, int sections, char isdefense) 261 { 262 int n; 263 struct BP *bp; 264 265 bp = isdefense ? from->file->DBP : from->file->OBP; 266 for (n = 0; n < NBP && bp[n].turnsent; n++) 267 ; 268 if (n < NBP && sections) { 269 Write(isdefense ? W_DBP : W_OBP, from, 270 n, turn, to->file->index, sections); 271 if (isdefense) 272 makesignal(from, "repelling boarders", NULL); 273 else 274 makesignal(from, "boarding the %s (%c%c)", to); 275 } 276 } 277 278 int 279 toughmelee(struct ship *ship, struct ship *to, int isdefense, int count) 280 { 281 struct BP *bp; 282 int obp = 0; 283 int n, OBP = 0, DBP = 0, dbp = 0; 284 int qual; 285 286 qual = ship->specs->qual; 287 bp = isdefense ? ship->file->DBP : ship->file->OBP; 288 for (n = 0; n < NBP; n++, bp++) { 289 if (bp->turnsent && (to == bp->toship || isdefense)) { 290 obp += bp->mensent / 100 291 ? ship->specs->crew1 * qual : 0; 292 obp += (bp->mensent % 100)/10 293 ? ship->specs->crew2 * qual : 0; 294 obp += bp->mensent % 10 295 ? ship->specs->crew3 * qual : 0; 296 } 297 } 298 if (count || isdefense) 299 return obp; 300 OBP = toughmelee(to, ship, 0, count + 1); 301 dbp = toughmelee(ship, to, 1, count + 1); 302 DBP = toughmelee(to, ship, 1, count + 1); 303 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10) 304 return 1; 305 else 306 return 0; 307 } 308 309 void 310 reload(void) 311 { 312 struct ship *sp; 313 314 foreachship(sp) { 315 sp->file->loadwith = 0; 316 } 317 } 318 319 void 320 checksails(void) 321 { 322 struct ship *sp; 323 int rig, full; 324 struct ship *closest; 325 326 foreachship(sp) { 327 if (sp->file->captain[0] != 0) 328 continue; 329 rig = sp->specs->rig1; 330 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4)) 331 rig = 0; 332 if (rig && sp->specs->crew3) { 333 closest = closestenemy(sp, 0, 0); 334 if (closest != 0) { 335 if (range(sp, closest) > 9) 336 full = 1; 337 else 338 full = 0; 339 } else 340 full = 0; 341 } else 342 full = 0; 343 if ((sp->file->FS != 0) != full) 344 Write(W_FS, sp, full, 0, 0, 0); 345 } 346 } 347