misc.c revision 1.8
1/* $OpenBSD: misc.c,v 1.8 2015/12/31 16:44:22 mestre 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 <ctype.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <unistd.h> 38 39#include "extern.h" 40#include "pathnames.h" 41 42#define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2) 43 44/* XXX */ 45int 46range(from, to) 47 struct ship *from, *to; 48{ 49 int bow1r, bow1c, bow2r, bow2c; 50 int stern1r, stern1c, stern2c, stern2r; 51 int bb, bs, sb, ss, result; 52 53 if (!to->file->dir) 54 return -1; 55 stern1r = bow1r = from->file->row; 56 stern1c = bow1c = from->file->col; 57 stern2r = bow2r = to->file->row; 58 stern2c = bow2c = to->file->col; 59 result = bb = distance(bow2r - bow1r, bow2c - bow1c); 60 if (bb < 5) { 61 stern2r += dr[to->file->dir]; 62 stern2c += dc[to->file->dir]; 63 stern1r += dr[from->file->dir]; 64 stern1c += dc[from->file->dir]; 65 bs = distance((bow2r - stern1r), (bow2c - stern1c)); 66 sb = distance((bow1r - stern2r), (bow1c - stern2c)); 67 ss = distance((stern2r - stern1r) ,(stern2c - stern1c)); 68 result = min(bb, min(bs, min(sb, ss))); 69 } 70 return result; 71} 72 73struct ship * 74closestenemy(from, side, anyship) 75 struct ship *from; 76 char side, anyship; 77{ 78 struct ship *sp; 79 char a; 80 int olddist = 30000, dist; 81 struct ship *closest = 0; 82 83 a = capship(from)->nationality; 84 foreachship(sp) { 85 if (sp == from) 86 continue; 87 if (sp->file->dir == 0) 88 continue; 89 if (a == capship(sp)->nationality && !anyship) 90 continue; 91 if (side && gunsbear(from, sp) != side) 92 continue; 93 dist = range(from, sp); 94 if (dist < olddist) { 95 closest = sp; 96 olddist = dist; 97 } 98 } 99 return closest; 100} 101 102int 103angle(dr, dc) 104 int dr, dc; 105{ 106 int i; 107 108 if (dc >= 0 && dr > 0) 109 i = 0; 110 else if (dr <= 0 && dc > 0) 111 i = 2; 112 else if (dc <= 0 && dr < 0) 113 i = 4; 114 else 115 i = 6; 116 dr = abs(dr); 117 dc = abs(dc); 118 if ((i == 0 || i == 4) && dc * 2.4 > dr) { 119 i++; 120 if (dc > dr * 2.4) 121 i++; 122 } else if ((i == 2 || i == 6) && dr * 2.4 > dc) { 123 i++; 124 if (dr > dc * 2.4) 125 i++; 126 } 127 return i % 8 + 1; 128} 129 130int 131gunsbear(from, to) /* checks for target bow or stern */ 132 struct ship *from, *to; 133{ 134 int Dr, Dc, i; 135 int ang; 136 137 Dr = from->file->row - to->file->row; 138 Dc = to->file->col - from->file->col; 139 for (i = 2; i; i--) { 140 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1) 141 ang += 8; 142 if (ang >= 2 && ang <= 4) 143 return 'r'; 144 if (ang >= 6 && ang <= 7) 145 return 'l'; 146 Dr += dr[to->file->dir]; 147 Dc += dc[to->file->dir]; 148 } 149 return 0; 150} 151 152int 153portside(from, on, quick) 154 struct ship *from, *on; 155 int quick; /* returns true if fromship is */ 156{ /* shooting at onship's starboard side */ 157 int ang; 158 int Dr, Dc; 159 160 Dr = from->file->row - on->file->row; 161 Dc = on->file->col - from->file->col; 162 if (quick == -1) { 163 Dr += dr[on->file->dir]; 164 Dc += dc[on->file->dir]; 165 } 166 ang = angle(Dr, Dc); 167 if (quick != 0) 168 return ang; 169 ang = (ang + 4 - on->file->dir - 1) % 8 + 1; 170 return ang < 5; 171} 172 173int 174colours(sp) 175 struct ship *sp; 176{ 177 char flag; 178 179 if (sp->file->struck) { 180 flag = '!'; 181 return flag; 182 } 183 if (sp->file->explode) 184 flag = '#'; 185 if (sp->file->sink) 186 flag = '~'; 187 flag = *countryname[capship(sp)->nationality]; 188 return sp->file->FS ? flag : tolower((unsigned char)flag); 189} 190 191void 192logger(s) 193 struct ship *s; 194{ 195 FILE *fp; 196 int persons; 197 int n; 198 struct logs log[NLOG]; 199 float net; 200 struct logs *lp; 201 202 setegid(egid); 203 if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL) { 204 setegid(gid); 205 return; 206 } 207 setegid(gid); 208#ifdef LOCK_EX 209 if (flock(fileno(fp), LOCK_EX) < 0) 210 return; 211#endif 212 net = (float)s->file->points / s->specs->pts; 213 persons = getw(fp); 214 n = fread((char *)log, sizeof(struct logs), NLOG, fp); 215 for (lp = &log[n]; lp < &log[NLOG]; lp++) 216 lp->l_name[0] = lp->l_uid = lp->l_shipnum 217 = lp->l_gamenum = lp->l_netpoints = 0; 218 rewind(fp); 219 if (persons < 0) 220 (void) putw(1, fp); 221 else 222 (void) putw(persons + 1, fp); 223 for (lp = log; lp < &log[NLOG]; lp++) 224 if (net > (float)lp->l_netpoints 225 / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) { 226 (void) fwrite((char *)log, 227 sizeof (struct logs), lp - log, fp); 228 (void) strlcpy(log[NLOG-1].l_name, s->file->captain, 229 sizeof log[NLOG-1].l_name); 230 log[NLOG-1].l_uid = getuid(); 231 log[NLOG-1].l_shipnum = s->file->index; 232 log[NLOG-1].l_gamenum = game; 233 log[NLOG-1].l_netpoints = s->file->points; 234 (void) fwrite((char *)&log[NLOG-1], 235 sizeof (struct logs), 1, fp); 236 (void) fwrite((char *)lp, 237 sizeof (struct logs), &log[NLOG-1] - lp, fp); 238 break; 239 } 240#ifdef LOCK_EX 241 (void) flock(fileno(fp), LOCK_UN); 242#endif 243 (void) fclose(fp); 244} 245