misc.c revision 1.6
1/* $OpenBSD: misc.c,v 1.6 2014/11/16 04:49:48 guenther Exp $ */ 2/* $NetBSD: misc.c,v 1.3 1995/04/22 10:37:03 cgd Exp $ */ 3 4/* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <fcntl.h> 34#include <stdlib.h> 35#include <unistd.h> 36#include "extern.h" 37#include "pathnames.h" 38 39#define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2) 40 41/* XXX */ 42int 43range(from, to) 44 struct ship *from, *to; 45{ 46 int bow1r, bow1c, bow2r, bow2c; 47 int stern1r, stern1c, stern2c, stern2r; 48 int bb, bs, sb, ss, result; 49 50 if (!to->file->dir) 51 return -1; 52 stern1r = bow1r = from->file->row; 53 stern1c = bow1c = from->file->col; 54 stern2r = bow2r = to->file->row; 55 stern2c = bow2c = to->file->col; 56 result = bb = distance(bow2r - bow1r, bow2c - bow1c); 57 if (bb < 5) { 58 stern2r += dr[to->file->dir]; 59 stern2c += dc[to->file->dir]; 60 stern1r += dr[from->file->dir]; 61 stern1c += dc[from->file->dir]; 62 bs = distance((bow2r - stern1r), (bow2c - stern1c)); 63 sb = distance((bow1r - stern2r), (bow1c - stern2c)); 64 ss = distance((stern2r - stern1r) ,(stern2c - stern1c)); 65 result = min(bb, min(bs, min(sb, ss))); 66 } 67 return result; 68} 69 70struct ship * 71closestenemy(from, side, anyship) 72 struct ship *from; 73 char side, anyship; 74{ 75 struct ship *sp; 76 char a; 77 int olddist = 30000, dist; 78 struct ship *closest = 0; 79 80 a = capship(from)->nationality; 81 foreachship(sp) { 82 if (sp == from) 83 continue; 84 if (sp->file->dir == 0) 85 continue; 86 if (a == capship(sp)->nationality && !anyship) 87 continue; 88 if (side && gunsbear(from, sp) != side) 89 continue; 90 dist = range(from, sp); 91 if (dist < olddist) { 92 closest = sp; 93 olddist = dist; 94 } 95 } 96 return closest; 97} 98 99int 100angle(dr, dc) 101 int dr, dc; 102{ 103 int i; 104 105 if (dc >= 0 && dr > 0) 106 i = 0; 107 else if (dr <= 0 && dc > 0) 108 i = 2; 109 else if (dc <= 0 && dr < 0) 110 i = 4; 111 else 112 i = 6; 113 dr = abs(dr); 114 dc = abs(dc); 115 if ((i == 0 || i == 4) && dc * 2.4 > dr) { 116 i++; 117 if (dc > dr * 2.4) 118 i++; 119 } else if ((i == 2 || i == 6) && dr * 2.4 > dc) { 120 i++; 121 if (dr > dc * 2.4) 122 i++; 123 } 124 return i % 8 + 1; 125} 126 127int 128gunsbear(from, to) /* checks for target bow or stern */ 129 struct ship *from, *to; 130{ 131 int Dr, Dc, i; 132 int ang; 133 134 Dr = from->file->row - to->file->row; 135 Dc = to->file->col - from->file->col; 136 for (i = 2; i; i--) { 137 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1) 138 ang += 8; 139 if (ang >= 2 && ang <= 4) 140 return 'r'; 141 if (ang >= 6 && ang <= 7) 142 return 'l'; 143 Dr += dr[to->file->dir]; 144 Dc += dc[to->file->dir]; 145 } 146 return 0; 147} 148 149int 150portside(from, on, quick) 151 struct ship *from, *on; 152 int quick; /* returns true if fromship is */ 153{ /* shooting at onship's starboard side */ 154 int ang; 155 int Dr, Dc; 156 157 Dr = from->file->row - on->file->row; 158 Dc = on->file->col - from->file->col; 159 if (quick == -1) { 160 Dr += dr[on->file->dir]; 161 Dc += dc[on->file->dir]; 162 } 163 ang = angle(Dr, Dc); 164 if (quick != 0) 165 return ang; 166 ang = (ang + 4 - on->file->dir - 1) % 8 + 1; 167 return ang < 5; 168} 169 170int 171colours(sp) 172 struct ship *sp; 173{ 174 char flag; 175 176 if (sp->file->struck) { 177 flag = '!'; 178 return flag; 179 } 180 if (sp->file->explode) 181 flag = '#'; 182 if (sp->file->sink) 183 flag = '~'; 184 flag = *countryname[capship(sp)->nationality]; 185 return sp->file->FS ? flag : tolower(flag); 186} 187 188void 189logger(s) 190 struct ship *s; 191{ 192 FILE *fp; 193 int persons; 194 int n; 195 struct logs log[NLOG]; 196 float net; 197 struct logs *lp; 198 199 setegid(egid); 200 if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL) { 201 setegid(gid); 202 return; 203 } 204 setegid(gid); 205#ifdef LOCK_EX 206 if (flock(fileno(fp), LOCK_EX) < 0) 207 return; 208#endif 209 net = (float)s->file->points / s->specs->pts; 210 persons = getw(fp); 211 n = fread((char *)log, sizeof(struct logs), NLOG, fp); 212 for (lp = &log[n]; lp < &log[NLOG]; lp++) 213 lp->l_name[0] = lp->l_uid = lp->l_shipnum 214 = lp->l_gamenum = lp->l_netpoints = 0; 215 rewind(fp); 216 if (persons < 0) 217 (void) putw(1, fp); 218 else 219 (void) putw(persons + 1, fp); 220 for (lp = log; lp < &log[NLOG]; lp++) 221 if (net > (float)lp->l_netpoints 222 / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) { 223 (void) fwrite((char *)log, 224 sizeof (struct logs), lp - log, fp); 225 (void) strlcpy(log[NLOG-1].l_name, s->file->captain, 226 sizeof log[NLOG-1].l_name); 227 log[NLOG-1].l_uid = getuid(); 228 log[NLOG-1].l_shipnum = s->file->index; 229 log[NLOG-1].l_gamenum = game; 230 log[NLOG-1].l_netpoints = s->file->points; 231 (void) fwrite((char *)&log[NLOG-1], 232 sizeof (struct logs), 1, fp); 233 (void) fwrite((char *)lp, 234 sizeof (struct logs), &log[NLOG-1] - lp, fp); 235 break; 236 } 237#ifdef LOCK_EX 238 (void) flock(fileno(fp), LOCK_UN); 239#endif 240 (void) fclose(fp); 241} 242