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