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