1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)dr_2.c	5.1 (Berkeley) 5/29/85";
   9: #endif not lint
  10: 
  11: #include "driver.h"
  12: 
  13: #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
  14: 
  15: thinkofgrapples()
  16: {
  17:     register struct ship *sp, *sq;
  18:     char friendly;
  19: 
  20:     foreachship(sp) {
  21:         if (sp->file->captain[0] || sp->file->dir == 0)
  22:             continue;
  23:         foreachship(sq) {
  24:             friendly = sp->nationality == capship(sq)->nationality;
  25:             if (!friendly) {
  26:                 if (sp->file->struck || sp->file->captured != 0)
  27:                     continue;
  28:                 if (range(sp, sq) != 1)
  29:                     continue;
  30:                 if (grappled2(sp, sq))
  31:                     if (toughmelee(sp, sq, 0, 0))
  32:                         ungrap(sp, sq);
  33:                     else
  34:                         grap(sp, sq);
  35:                 else if (couldwin(sp, sq)) {
  36:                     grap(sp, sq);
  37:                     sp->file->loadwith = L_GRAPE;
  38:                 }
  39:             } else
  40:                 ungrap(sp, sq);
  41:         }
  42:     }
  43: }
  44: 
  45: checkup()
  46: {
  47:     register struct ship *sp, *sq;
  48:     register char explode, sink;
  49: 
  50:     foreachship(sp) {
  51:         if (sp->file->dir == 0)
  52:             continue;
  53:         explode = sp->file->explode;
  54:         sink = sp->file->sink;
  55:         if (explode != 1 && sink != 1)
  56:             continue;
  57:         if (die() < 5)
  58:             continue;
  59:         Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
  60:         Write(W_DIR, sp, 0, 0, 0, 0, 0);
  61:         if (snagged(sp))
  62:             foreachship(sq)
  63:                 cleansnag(sp, sq, 1);
  64:         if (sink != 1) {
  65:             makesignal(sp, "exploding!", (struct ship *)0);
  66:             foreachship(sq) {
  67:                 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
  68:                     table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
  69:             }
  70:         } else
  71:             makesignal(sp, "sinking!", (struct ship *)0);
  72:     }
  73: }
  74: 
  75: prizecheck()
  76: {
  77:     register struct ship *sp;
  78: 
  79:     foreachship(sp) {
  80:         if (sp->file->captured == 0)
  81:             continue;
  82:         if (sp->file->struck || sp->file->dir == 0)
  83:             continue;
  84:         if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
  85:             Write(W_SIGNAL, sp, 1,
  86:                 (int)"prize crew overthrown", 0, 0, 0);
  87:             Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
  88:             Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
  89:         }
  90:     }
  91: }
  92: 
  93: strend(str)
  94: char *str;
  95: {
  96:     register char *p;
  97: 
  98:     for (p = str; *p; p++)
  99:         ;
 100:     return p == str ? 0 : p[-1];
 101: }
 102: 
 103: closeon(from, to, command, ta, ma, af)
 104: register struct ship *from, *to;
 105: char command[];
 106: int ma, ta, af;
 107: {
 108:     int high;
 109:     char temp[10];
 110: 
 111:     temp[0] = command[0] = '\0';
 112:     high = -30000;
 113:     try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
 114: }
 115: 
 116: int dtab[] = {0,1,1,2,3,4,4,5};     /* diagonal distances in x==y */
 117: 
 118: score(movement, ship, to, onlytemp)
 119: char movement[];
 120: register struct ship *ship, *to;
 121: char onlytemp;
 122: {
 123:     char drift;
 124:     int row, col, dir, total, ran;
 125:     register struct File *fp = ship->file;
 126: 
 127:     if ((dir = fp->dir) == 0)
 128:         return 0;
 129:     row = fp->row;
 130:     col = fp->col;
 131:     drift = fp->drift;
 132:     move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
 133:     if (!*movement)
 134:         (void) strcpy(movement, "d");
 135: 
 136:     ran = range(ship, to);
 137:     total = -50 * ran;
 138:     if (ran < 4 && gunsbear(ship, to))
 139:         total += 60;
 140:     if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
 141:         total = -30000;
 142: 
 143:     if (!onlytemp) {
 144:         fp->row = row;
 145:         fp->col = col;
 146:         fp->dir = dir;
 147:     }
 148:     return total;
 149: }
 150: 
 151: move(p, ship, dir, row, col, drift)
 152: register char *p;
 153: register struct ship *ship;
 154: register char *dir;
 155: register short *row, *col;
 156: register char *drift;
 157: {
 158:     int dist;
 159:     char moved = 0;
 160: 
 161:     for (; *p; p++) {
 162:         switch (*p) {
 163:         case 'r':
 164:             if (++*dir == 9)
 165:                 *dir = 1;
 166:             break;
 167:         case 'l':
 168:             if (--*dir == 0)
 169:                 *dir = 8;
 170:             break;
 171:         case '1': case '2': case '3': case '4':
 172:         case '5': case '6': case '7':
 173:             moved++;
 174:             if (*dir % 2 == 0)
 175:                 dist = dtab[*p - '0'];
 176:             else
 177:                 dist = *p - '0';
 178:             *row -= dr[*dir] * dist;
 179:             *col -= dc[*dir] * dist;
 180:             break;
 181:         }
 182:     }
 183:     if (!moved) {
 184:         if (windspeed != 0 && ++*drift > 2) {
 185:             if (ship->specs->class >= 3 && !snagged(ship)
 186:                 || (turn & 1) == 0) {
 187:                 *row -= dr[winddir];
 188:                 *col -= dc[winddir];
 189:             }
 190:         }
 191:     } else
 192:         *drift = 0;
 193: }
 194: 
 195: try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
 196: register struct ship *f, *t;
 197: int ma, ta, af, *high, rakeme;
 198: char command[], temp[];
 199: {
 200:     register int new, n;
 201:     char st[4];
 202: #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
 203: 
 204:     if ((n = strend(temp)) < '1' || n > '9')
 205:         for (n = 1; vma - n >= 0; n++) {
 206:             (void) sprintf(st, "%d", n);
 207:             (void) strcat(temp, st);
 208:             new = score(temp, f, t, rakeme);
 209:             if (new > *high && (!rakeme || rakeyou)) {
 210:                 *high = new;
 211:                 (void) strcpy(command, temp);
 212:             }
 213:             try(command, temp, ma-n, ta, af, vma-n,
 214:                 dir, f, t, high, rakeme);
 215:             rmend(temp);
 216:         }
 217:     if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
 218:         (void) strcat(temp, "r");
 219:         new = score(temp, f, t, rakeme);
 220:         if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
 221:             *high = new;
 222:             (void) strcpy(command, temp);
 223:         }
 224:         try(command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1),f,t,high,rakeme);
 225:         rmend(temp);
 226:     }
 227:     if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
 228:         (void) strcat(temp, "l");
 229:         new = score(temp, f, t, rakeme);
 230:         if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
 231:             *high = new;
 232:             (void) strcpy(command, temp);
 233:         }
 234:         try(command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), f, t, high, rakeme);
 235:         rmend(temp);
 236:     }
 237: }
 238: 
 239: rmend(str)
 240: char *str;
 241: {
 242:     register char *p;
 243: 
 244:     for (p = str; *p; p++)
 245:         ;
 246:     if (p != str)
 247:         *--p = 0;
 248: }

Defined functions

checkup defined in line 45; used 1 times
closeon defined in line 103; used 1 times
move defined in line 151; used 1 times
prizecheck defined in line 75; used 1 times
rmend defined in line 239; used 3 times
score defined in line 118; used 3 times
strend defined in line 93; used 3 times
thinkofgrapples defined in line 15; used 1 times
try defined in line 195; used 4 times

Defined variables

dtab defined in line 116; used 1 times
sccsid defined in line 8; never used

Defined macros

couldwin defined in line 13; used 1 times
  • in line 35
rakeyou defined in line 202; used 1 times
Last modified: 1985-05-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1382
Valid CSS Valid XHTML 1.0 Strict