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[] = "@(#)misc.c	5.1 (Berkeley) 5/29/85";
   9: #endif not lint
  10: 
  11: #include "externs.h"
  12: 
  13: #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
  14: 
  15: /* XXX */
  16: range(from, to)
  17: struct ship *from, *to;
  18: {
  19:     register bow1r, bow1c, bow2r, bow2c;
  20:     int stern1r, stern1c, stern2c, stern2r;
  21:     register int bb, bs, sb, ss, result;
  22: 
  23:     if (!to->file->dir)
  24:         return -1;
  25:     stern1r = bow1r = from->file->row;
  26:     stern1c = bow1c = from->file->col;
  27:     stern2r = bow2r = to->file->row;
  28:     stern2c = bow2c = to->file->col;
  29:     result = bb = distance(bow2r - bow1r, bow2c - bow1c);
  30:     if (bb < 5) {
  31:         stern2r += dr[to->file->dir];
  32:         stern2c += dc[to->file->dir];
  33:         stern1r += dr[from->file->dir];
  34:         stern1c += dc[from->file->dir];
  35:         bs = distance((bow2r - stern1r), (bow2c - stern1c));
  36:         sb = distance((bow1r - stern2r), (bow1c - stern2c));
  37:         ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
  38:         result = min(bb, min(bs, min(sb, ss)));
  39:     }
  40:     return result;
  41: }
  42: 
  43: struct ship *
  44: closestenemy(from, side, anyship)
  45: register struct ship *from;
  46: char side, anyship;
  47: {
  48:     register struct ship *sp;
  49:     register char a;
  50:     int olddist = 30000, dist;
  51:     struct ship *closest = 0;
  52: 
  53:     a = capship(from)->nationality;
  54:     foreachship(sp) {
  55:         if (sp == from)
  56:             continue;
  57:         if (sp->file->dir == 0)
  58:             continue;
  59:         if (a == capship(sp)->nationality && !anyship)
  60:             continue;
  61:         if (side && gunsbear(from, sp) != side)
  62:             continue;
  63:         dist = range(from, sp);
  64:         if (dist < olddist) {
  65:             closest = sp;
  66:             olddist = dist;
  67:         }
  68:     }
  69:     return closest;
  70: }
  71: 
  72: angle(dr, dc)
  73: register dr, dc;
  74: {
  75:     register i;
  76: 
  77:     if (dc >= 0 && dr > 0)
  78:         i = 0;
  79:     else if (dr <= 0 && dc > 0)
  80:         i = 2;
  81:     else if (dc <= 0 && dr < 0)
  82:         i = 4;
  83:     else
  84:         i = 6;
  85:     dr = abs(dr);
  86:     dc = abs(dc);
  87:     if ((i == 0 || i == 4) && dc * 2.4 > dr) {
  88:         i++;
  89:         if (dc > dr * 2.4)
  90:             i++;
  91:     } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
  92:         i++;
  93:         if (dr > dc * 2.4)
  94:             i++;
  95:     }
  96:     return i % 8 + 1;
  97: }
  98: 
  99: gunsbear(from, to)      /* checks for target bow or stern */
 100: register struct ship *from, *to;
 101: {
 102:     int Dr, Dc, i;
 103:     register ang;
 104: 
 105:     Dr = from->file->row - to->file->row;
 106:     Dc = to->file->col - from->file->col;
 107:     for (i = 2; i; i--) {
 108:         if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
 109:             ang += 8;
 110:         if (ang >= 2 && ang <= 4)
 111:             return 'r';
 112:         if (ang >= 6 && ang <= 7)
 113:             return 'l';
 114:         Dr += dr[to->file->dir];
 115:         Dc += dc[to->file->dir];
 116:     }
 117:     return 0;
 118: }
 119: 
 120: portside(from, on, quick)
 121: register struct ship *from, *on;
 122: int quick;          /* returns true if fromship is */
 123: {               /* shooting at onship's starboard side */
 124:     register ang;
 125:     register Dr, Dc;
 126: 
 127:     Dr = from->file->row - on->file->row;
 128:     Dc = on->file->col - from->file->col;
 129:     if (quick == -1) {
 130:         Dr += dr[on->file->dir];
 131:         Dc += dc[on->file->dir];
 132:     }
 133:     ang = angle(Dr, Dc);
 134:     if (quick != 0)
 135:         return ang;
 136:     ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
 137:     return ang < 5;
 138: }
 139: 
 140: colours(sp)
 141: register struct ship *sp;
 142: {
 143:     register char flag;
 144: 
 145:     if (sp->file->struck)
 146:         flag = '!';
 147:     if (sp->file->explode)
 148:         flag = '#';
 149:     if (sp->file->sink)
 150:         flag = '~';
 151:     if (sp->file->struck)
 152:         return flag;
 153:     flag = *countryname[capship(sp)->nationality];
 154:     return sp->file->FS ? flag : tolower(flag);
 155: }
 156: 
 157: #include <sys/file.h>
 158: log(s)
 159: register struct ship *s;
 160: {
 161:     FILE *fp;
 162:     int persons;
 163:     int n;
 164:     struct logs log[NLOG];
 165:     float net;
 166:     register struct logs *lp;
 167: 
 168:     if ((fp = fopen(LOGFILE, "r+")) == NULL)
 169:         return;
 170: #ifdef LOCK_EX
 171:     if (flock(fileno(fp), LOCK_EX) < 0)
 172:         return;
 173: #endif
 174:     net = (float)s->file->points / s->specs->pts;
 175:     persons = getw(fp);
 176:     n = fread((char *)log, sizeof(struct logs), NLOG, fp);
 177:     for (lp = &log[n]; lp < &log[NLOG]; lp++)
 178:         lp->l_name[0] = lp->l_uid = lp->l_shipnum
 179:             = lp->l_gamenum = lp->l_netpoints = 0;
 180:     rewind(fp);
 181:     if (persons < 0)
 182:         (void) putw(1, fp);
 183:     else
 184:         (void) putw(persons + 1, fp);
 185:     for (lp = log; lp < &log[NLOG]; lp++)
 186:         if (net > (float)lp->l_netpoints
 187:             / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
 188:             (void) fwrite((char *)log,
 189:                 sizeof (struct logs), lp - log, fp);
 190:             (void) strcpy(log[NLOG-1].l_name, s->file->captain);
 191:             log[NLOG-1].l_uid = getuid();
 192:             log[NLOG-1].l_shipnum = s->file->index;
 193:             log[NLOG-1].l_gamenum = game;
 194:             log[NLOG-1].l_netpoints = s->file->points;
 195:             (void) fwrite((char *)&log[NLOG-1],
 196:                 sizeof (struct logs), 1, fp);
 197:             (void) fwrite((char *)lp,
 198:                 sizeof (struct logs), &log[NLOG-1] - lp, fp);
 199:             break;
 200:         }
 201: #ifdef LOCK_EX
 202:     (void) flock(fileno(fp), LOCK_UN);
 203: #endif
 204:     (void) fclose(fp);
 205: }

Defined functions

angle defined in line 72; used 2 times
gunsbear defined in line 99; used 12 times
log defined in line 158; used 17 times

Defined variables

sccsid defined in line 8; never used

Defined macros

distance defined in line 13; used 4 times
Last modified: 1985-05-30
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1391
Valid CSS Valid XHTML 1.0 Strict