| 1 | #ifndef lint |
| 2 | static char *sccsid = "@(#)dr_3.c 2.2 83/11/01"; |
| 3 | #endif |
| 4 | |
| 5 | #include "driver.h" |
| 6 | |
| 7 | moveall() /* move all comp ships */ |
| 8 | { |
| 9 | register struct ship *sp, *sq; /* r11, r10 */ |
| 10 | register int n; /* r9 */ |
| 11 | struct ship *closest; |
| 12 | register int k, l, m, ma; /* r8, r7, r6, */ |
| 13 | int ta; |
| 14 | char af; |
| 15 | int row[NSHIP], col[NSHIP], dir[NSHIP], r1, r2, c1, c2, d1, d2; |
| 16 | char clast[NSHIP][sizeof SHIP(0)->file->last]; |
| 17 | |
| 18 | /* |
| 19 | * first try to create moves for OUR ships |
| 20 | */ |
| 21 | foreachship(sp) { |
| 22 | if (sp->file->captain[0] || sp->file->dir == 0) |
| 23 | continue; |
| 24 | if (!sp->file->struck && windspeed && !snagged(sp) |
| 25 | && sp->specs->crew3) { |
| 26 | ta = maxturns(sp, &af); |
| 27 | ma = maxmove(sp, sp->file->dir, 0); |
| 28 | closest = closestenemy(sp, 0, 0); |
| 29 | if (closest == 0) |
| 30 | *sp->file->last = '\0'; |
| 31 | else |
| 32 | closeon(sp, closest, sp->file->last, |
| 33 | ta, ma, af); |
| 34 | } else |
| 35 | *sp->file->last = '\0'; |
| 36 | /* |
| 37 | makesignal(sp, "move (%d): %s", 0, turn, sp->file->last); |
| 38 | */ |
| 39 | } |
| 40 | /* |
| 41 | * Then execute the moves for ALL ships (dead ones too), |
| 42 | * saving old positions in row[], col[], dir[], |
| 43 | * and the moves in clase[][]. |
| 44 | * The new positions are written out. |
| 45 | */ |
| 46 | n = 0; |
| 47 | foreachship(sp) { |
| 48 | if (snagged(sp)) |
| 49 | clast[n][0] = '\0'; |
| 50 | else |
| 51 | (void) strcpy(clast[n], sp->file->last); |
| 52 | row[n] = sp->file->row; |
| 53 | col[n] = sp->file->col; |
| 54 | dir[n] = sp->file->dir; |
| 55 | moveship(sp, clast[n]); |
| 56 | n++; |
| 57 | } |
| 58 | /* |
| 59 | * Now resolve collisions. |
| 60 | * This is the tough part. |
| 61 | */ |
| 62 | for (k = 0; stillmoving(clast, k); k++) { |
| 63 | /* |
| 64 | * Step once. |
| 65 | * And propagate the nulls at the end of clast[]. |
| 66 | */ |
| 67 | n = 0; |
| 68 | foreachship(sp) { |
| 69 | if (dir[n]) |
| 70 | step(clast[n][k], sp, row+n, col+n, dir+n); |
| 71 | if (!clast[n][k]) |
| 72 | clast[n][k+1] = '\0'; |
| 73 | n++; |
| 74 | } |
| 75 | /* |
| 76 | * The real stuff. |
| 77 | */ |
| 78 | n = 0; |
| 79 | foreachship(sp) { |
| 80 | if ((d1 = sp->file->dir) == 0 || isolated(sp)) |
| 81 | goto cont1; |
| 82 | r1 = sp->file->row; |
| 83 | c1 = sp->file->col; |
| 84 | sp->file->dir = dir[n]; |
| 85 | sp->file->row = row[n]; |
| 86 | sp->file->col = col[n]; |
| 87 | l = 0; |
| 88 | foreachship(sq) { |
| 89 | if (sp == sq) |
| 90 | goto cont2; |
| 91 | if ((d2 = sq->file->dir) == 0) |
| 92 | goto cont2; |
| 93 | r2 = sq->file->row; |
| 94 | c2 = sq->file->col; |
| 95 | sq->file->dir = dir[l]; |
| 96 | sq->file->row = row[l]; |
| 97 | sq->file->col = col[l]; |
| 98 | if (snagged2(sp, sq) |
| 99 | && push(sp, sq) && range(sp, sq) > 1) { |
| 100 | Write(W_SHIPROW, sq, 0, |
| 101 | sp->file->row - 1, 0, 0, 0); |
| 102 | if (sp->file->dir == 1 |
| 103 | || sp->file->dir == 5) |
| 104 | Write(W_SHIPCOL, sq, 0, |
| 105 | sp->file->col - 1, |
| 106 | 0, 0, 0); |
| 107 | else |
| 108 | Write(W_SHIPCOL, sq, 0, |
| 109 | sp->file->col, 0, 0, 0); |
| 110 | Write(W_SHIPDIR, sq, 0, |
| 111 | sp->file->dir, 0, 0, 0); |
| 112 | } |
| 113 | if (!range(sp, sq) && !fouled2(sp, sq) |
| 114 | && push(sp, sq)) { |
| 115 | makesignal(sp, |
| 116 | "collision with %s (%c%c)", sq); |
| 117 | if (die() < 4) { |
| 118 | makesignal(sp, |
| 119 | "fouled with %s (%c%c)", |
| 120 | sq); |
| 121 | for (m = 0; m < NSHIP && sp->file->fouls[m].turnfoul; m++) |
| 122 | ; |
| 123 | if (m < NSHIP) |
| 124 | Write(W_FOUL, sp, 0, |
| 125 | m, turn, l, 0); |
| 126 | for (m = 0; m < NSHIP && sq->file->fouls[m].turnfoul; m++) |
| 127 | ; |
| 128 | if (m < NSHIP) |
| 129 | Write(W_FOUL, sq, 0, |
| 130 | m, turn, n, 0); |
| 131 | } |
| 132 | clast[n][k+1] = '\0'; |
| 133 | sp->file->row = r2; |
| 134 | sp->file->col = c2; |
| 135 | sp->file->dir = d2; |
| 136 | moveship(sp, clast[n]); |
| 137 | Write(W_SHIPROW, sq, 0, |
| 138 | sp->file->row-1, 0, 0, 0); |
| 139 | if (sp->file->dir == 1 |
| 140 | || sp->file->dir == 5) |
| 141 | Write(W_SHIPCOL, sq, 0, |
| 142 | sp->file->col-1, 0, 0, 0); |
| 143 | else |
| 144 | Write(W_SHIPCOL, sq, 0, |
| 145 | sp->file->col, 0, 0, 0); |
| 146 | Write(W_SHIPDIR, sq, 0, |
| 147 | sp->file->dir, 0, 0, 0); |
| 148 | Write(W_DRIFT, sq, 0, 0, 0, 0, 0); |
| 149 | Write(W_DRIFT, sp, 0, 0, 0, 0, 0); |
| 150 | goto cont2; |
| 151 | } |
| 152 | sq->file->row = r2; |
| 153 | sq->file->col = c2; |
| 154 | sq->file->dir = d2; |
| 155 | cont2: |
| 156 | l++; |
| 157 | } |
| 158 | sp->file->row = r1; |
| 159 | sp->file->col = c1; |
| 160 | sp->file->dir = d1; |
| 161 | cont1: |
| 162 | n++; |
| 163 | } |
| 164 | } |
| 165 | /* |
| 166 | * Clear old moves. |
| 167 | */ |
| 168 | foreachship(sp) |
| 169 | sp->file->last[0] = 0; |
| 170 | } |
| 171 | |
| 172 | stillmoving(last, k) |
| 173 | char last[][sizeof SHIP(0)->file->last]; /* how's that for portability */ |
| 174 | register int k; |
| 175 | { |
| 176 | register struct ship *sp; |
| 177 | register char (*p)[sizeof *last]; /* and this? */ |
| 178 | |
| 179 | p = last; |
| 180 | foreachship(sp) { |
| 181 | if ((*p)[k]) |
| 182 | return 1; |
| 183 | p++; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | isolated(ship) |
| 189 | register struct ship *ship; |
| 190 | { |
| 191 | register struct ship *sp; |
| 192 | |
| 193 | foreachship(sp) { |
| 194 | if (ship != sp && range(ship, sp) <= 10) |
| 195 | return 0; |
| 196 | } |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | push(from, to) |
| 201 | register struct ship *from, *to; |
| 202 | { |
| 203 | register int bs, sb; |
| 204 | |
| 205 | sb = to->specs->class; |
| 206 | bs = from->specs->class; |
| 207 | if (sb > bs) |
| 208 | return 1; |
| 209 | if (sb < bs) |
| 210 | return 0; |
| 211 | return from < to; |
| 212 | } |
| 213 | |
| 214 | step(com, ship, row, col, dir) |
| 215 | register struct ship *ship; |
| 216 | register int *row, *col, *dir; |
| 217 | char com; |
| 218 | { |
| 219 | register int dist; |
| 220 | |
| 221 | switch (com) { |
| 222 | case 'r': |
| 223 | if (++*dir == 9) |
| 224 | *dir = 1; |
| 225 | break; |
| 226 | case 'l': |
| 227 | if (--*dir == 0) |
| 228 | *dir = 8; |
| 229 | break; |
| 230 | case '0': case '1': case '2': case '3': |
| 231 | case '4': case '5': case '6': case '7': |
| 232 | if (*dir % 2 == 0) |
| 233 | dist = dtab[com - '0']; |
| 234 | else |
| 235 | dist = com - '0'; |
| 236 | *row -= dr[*dir] * dist; |
| 237 | *col -= dc[*dir] * dist; |
| 238 | break; |
| 239 | case 'b': |
| 240 | break; |
| 241 | case 'd': |
| 242 | if (ship->specs->class >= 3 && !snagged(ship) |
| 243 | || turn % 2 == 0) { |
| 244 | *row -= dr[winddir]; |
| 245 | *col -= dc[winddir]; |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | sendbp(from, to, sections, isdefense) |
| 252 | register struct ship *from, *to; |
| 253 | int sections; |
| 254 | char isdefense; |
| 255 | { |
| 256 | int n; |
| 257 | register struct BP *bp; |
| 258 | |
| 259 | bp = isdefense ? from->file->DBP : from->file->OBP; |
| 260 | for (n = 0; n < 3 && bp[n].turnsent; n++) |
| 261 | ; |
| 262 | if (n < 3 && sections) { |
| 263 | Write(isdefense ? W_DBP : W_OBP, from, 0, |
| 264 | turn, to-SHIP(0), sections, 0); |
| 265 | if (isdefense) |
| 266 | makesignal(from, "repelling boarders", |
| 267 | (struct ship *)0); |
| 268 | else |
| 269 | makesignal(from, "boarding the %s (%c%c)", to); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | toughmelee(ship, to, isdefense, count) |
| 274 | register struct ship *ship, *to; |
| 275 | int isdefense, count; |
| 276 | { |
| 277 | register struct BP *bp; |
| 278 | register obp = 0; |
| 279 | int n, OBP = 0, DBP = 0, dbp = 0; |
| 280 | int qual; |
| 281 | |
| 282 | qual = ship->specs->qual; |
| 283 | bp = isdefense ? ship->file->DBP : ship->file->OBP; |
| 284 | for (n = 0; n < NBP; n++, bp++) { |
| 285 | if (bp->turnsent && (to == bp->toship || isdefense)) { |
| 286 | obp += bp->mensent / 100 |
| 287 | ? ship->specs->crew1 * qual : 0; |
| 288 | obp += (bp->mensent % 100)/10 |
| 289 | ? ship->specs->crew2 * qual : 0; |
| 290 | obp += bp->mensent % 10 |
| 291 | ? ship->specs->crew3 * qual : 0; |
| 292 | } |
| 293 | } |
| 294 | if (count || isdefense) |
| 295 | return obp; |
| 296 | OBP = toughmelee(to, ship, 0, count + 1); |
| 297 | dbp = toughmelee(ship, to, 1, count + 1); |
| 298 | DBP = toughmelee(to, ship, 1, count + 1); |
| 299 | if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10) |
| 300 | return 1; |
| 301 | else |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | reload() |
| 306 | { |
| 307 | register struct ship *sp; |
| 308 | |
| 309 | foreachship(sp) { |
| 310 | sp->file->loadwith = 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | checksails() |
| 315 | { |
| 316 | register struct ship *sp; |
| 317 | register int rig, full; |
| 318 | struct ship *close; |
| 319 | |
| 320 | foreachship(sp) { |
| 321 | if (sp->file->captain[0] != 0) |
| 322 | continue; |
| 323 | rig = sp->specs->rig1; |
| 324 | if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4) |
| 325 | rig = 0; |
| 326 | if (rig && sp->specs->crew3) { |
| 327 | close = closestenemy(sp, 0, 0); |
| 328 | if (close != 0) { |
| 329 | if (range(sp, close) > 9) |
| 330 | full = 1; |
| 331 | else |
| 332 | full = 0; |
| 333 | } else |
| 334 | full = 0; |
| 335 | } else |
| 336 | full = 0; |
| 337 | if ((sp->file->FS != 0) != full) |
| 338 | Write(W_FS, sp, 0, full, 0, 0, 0); |
| 339 | } |
| 340 | } |