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