Home | History | Annotate | Line # | Download | only in sail
misc.c revision 1.5
      1 /*	$NetBSD: misc.c,v 1.5 1997/10/13 19:44:38 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.5 1997/10/13 19:44:38 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <fcntl.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 #include "extern.h"
     49 #include "pathnames.h"
     50 
     51 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
     52 
     53 /* XXX */
     54 int
     55 range(from, to)
     56 struct ship *from, *to;
     57 {
     58 	int bow1r, bow1c, bow2r, bow2c;
     59 	int stern1r, stern1c, stern2c, stern2r;
     60 	int bb, bs, sb, ss, result;
     61 
     62 	if (!to->file->dir)
     63 		return -1;
     64 	stern1r = bow1r = from->file->row;
     65 	stern1c = bow1c = from->file->col;
     66 	stern2r = bow2r = to->file->row;
     67 	stern2c = bow2c = to->file->col;
     68 	result = bb = distance(bow2r - bow1r, bow2c - bow1c);
     69 	if (bb < 5) {
     70 		stern2r += dr[to->file->dir];
     71 		stern2c += dc[to->file->dir];
     72 		stern1r += dr[from->file->dir];
     73 		stern1c += dc[from->file->dir];
     74 		bs = distance((bow2r - stern1r), (bow2c - stern1c));
     75 		sb = distance((bow1r - stern2r), (bow1c - stern2c));
     76 		ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
     77 		result = min(bb, min(bs, min(sb, ss)));
     78 	}
     79 	return result;
     80 }
     81 
     82 struct ship *
     83 closestenemy(from, side, anyship)
     84 struct ship *from;
     85 char side, 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 int
    112 angle(dr, dc)
    113 int dr, dc;
    114 {
    115 	int i;
    116 
    117 	if (dc >= 0 && dr > 0)
    118 		i = 0;
    119 	else if (dr <= 0 && dc > 0)
    120 		i = 2;
    121 	else if (dc <= 0 && dr < 0)
    122 		i = 4;
    123 	else
    124 		i = 6;
    125 	dr = abs(dr);
    126 	dc = abs(dc);
    127 	if ((i == 0 || i == 4) && dc * 2.4 > dr) {
    128 		i++;
    129 		if (dc > dr * 2.4)
    130 			i++;
    131 	} else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
    132 		i++;
    133 		if (dr > dc * 2.4)
    134 			i++;
    135 	}
    136 	return i % 8 + 1;
    137 }
    138 
    139 int
    140 gunsbear(from, to)		/* checks for target bow or stern */
    141 struct ship *from, *to;
    142 {
    143 	int Dr, Dc, i;
    144 	int ang;
    145 
    146 	Dr = from->file->row - to->file->row;
    147 	Dc = to->file->col - from->file->col;
    148 	for (i = 2; i; i--) {
    149 		if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
    150 			ang += 8;
    151 		if (ang >= 2 && ang <= 4)
    152 			return 'r';
    153 		if (ang >= 6 && ang <= 7)
    154 			return 'l';
    155 		Dr += dr[to->file->dir];
    156 		Dc += dc[to->file->dir];
    157 	}
    158 	return 0;
    159 }
    160 
    161 int
    162 portside(from, on, quick)
    163 struct ship *from, *on;
    164 int quick;			/* returns true if fromship is */
    165 {				/* shooting at onship's starboard side */
    166 	int ang;
    167 	int Dr, Dc;
    168 
    169 	Dr = from->file->row - on->file->row;
    170 	Dc = on->file->col - from->file->col;
    171 	if (quick == -1) {
    172 		Dr += dr[on->file->dir];
    173 		Dc += dc[on->file->dir];
    174 	}
    175 	ang = angle(Dr, Dc);
    176 	if (quick != 0)
    177 		return ang;
    178 	ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
    179 	return ang < 5;
    180 }
    181 
    182 int
    183 colours(sp)
    184 struct ship *sp;
    185 {
    186 	char flag = '\0';
    187 
    188 	if (sp->file->struck)
    189 		flag = '!';
    190 	if (sp->file->explode)
    191 		flag = '#';
    192 	if (sp->file->sink)
    193 		flag = '~';
    194 	if (sp->file->struck)
    195 		return flag;
    196 	flag = *countryname[capship(sp)->nationality];
    197 	return sp->file->FS ? flag : tolower(flag);
    198 }
    199 
    200 void
    201 logger(s)
    202 struct ship *s;
    203 {
    204 	FILE *fp;
    205 	int persons;
    206 	int n;
    207 	struct logs log[NLOG];
    208 	float net;
    209 	struct logs *lp;
    210 
    211 	if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
    212 		return;
    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 		(void) putw(1, fp);
    226 	else
    227 		(void) 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 			(void) fwrite((char *)log,
    232 				sizeof (struct logs), lp - log, fp);
    233 			(void) strcpy(log[NLOG-1].l_name, s->file->captain);
    234 			log[NLOG-1].l_uid = getuid();
    235 			log[NLOG-1].l_shipnum = s->file->index;
    236 			log[NLOG-1].l_gamenum = game;
    237 			log[NLOG-1].l_netpoints = s->file->points;
    238 			(void) fwrite((char *)&log[NLOG-1],
    239 				sizeof (struct logs), 1, fp);
    240 			(void) fwrite((char *)lp,
    241 				sizeof (struct logs), &log[NLOG-1] - lp, fp);
    242 			break;
    243 		}
    244 #ifdef LOCK_EX
    245 	(void) flock(fileno(fp), LOCK_UN);
    246 #endif
    247 	(void) fclose(fp);
    248 }
    249