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