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