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