2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/sail/misc.c,v 1.5 1999/11/30 03:49:34 billf Exp $";
43 #include "pathnames.h"
45 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
49 struct ship *from, *to;
51 int bow1r, bow1c, bow2r, bow2c;
52 int stern1r, stern1c, stern2c, stern2r;
53 int bb, bs, sb, ss, result;
57 stern1r = bow1r = from->file->row;
58 stern1c = bow1c = from->file->col;
59 stern2r = bow2r = to->file->row;
60 stern2c = bow2c = to->file->col;
61 result = bb = distance(bow2r - bow1r, bow2c - bow1c);
63 stern2r += dr[to->file->dir];
64 stern2c += dc[to->file->dir];
65 stern1r += dr[from->file->dir];
66 stern1c += dc[from->file->dir];
67 bs = distance((bow2r - stern1r), (bow2c - stern1c));
68 sb = distance((bow1r - stern2r), (bow1c - stern2c));
69 ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
70 result = min(bb, min(bs, min(sb, ss)));
76 closestenemy(from, side, anyship)
82 int olddist = 30000, dist;
83 struct ship *closest = 0;
85 a = capship(from)->nationality;
89 if (sp->file->dir == 0)
91 if (a == capship(sp)->nationality && !anyship)
93 if (side && gunsbear(from, sp) != side)
95 dist = range(from, sp);
109 if (dc >= 0 && dr > 0)
111 else if (dr <= 0 && dc > 0)
113 else if (dc <= 0 && dr < 0)
119 if ((i == 0 || i == 4) && dc * 2.4 > dr) {
123 } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
131 gunsbear(from, to) /* checks for target bow or stern */
132 struct ship *from, *to;
137 Dr = from->file->row - to->file->row;
138 Dc = to->file->col - from->file->col;
139 for (i = 2; i; i--) {
140 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
142 if (ang >= 2 && ang <= 4)
144 if (ang >= 6 && ang <= 7)
146 Dr += dr[to->file->dir];
147 Dc += dc[to->file->dir];
152 portside(from, on, quick)
153 struct ship *from, *on;
154 int quick; /* returns true if fromship is */
155 { /* shooting at onship's starboard side */
159 Dr = from->file->row - on->file->row;
160 Dc = on->file->col - from->file->col;
162 Dr += dr[on->file->dir];
163 Dc += dc[on->file->dir];
168 ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
177 if (sp->file->struck)
179 if (sp->file->explode)
183 if (sp->file->struck)
185 flag = *countryname[capship(sp)->nationality];
186 return sp->file->FS ? flag : tolower(flag);
189 #include <sys/file.h>
196 struct logs log[NLOG];
200 if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
203 if (flock(fileno(fp), LOCK_EX) < 0)
206 net = (float)s->file->points / s->specs->pts;
208 n = fread((char *)log, sizeof(struct logs), NLOG, fp);
209 for (lp = &log[n]; lp < &log[NLOG]; lp++)
210 lp->l_name[0] = lp->l_uid = lp->l_shipnum
211 = lp->l_gamenum = lp->l_netpoints = 0;
216 (void) putw(persons + 1, fp);
217 for (lp = log; lp < &log[NLOG]; lp++)
218 if (net > (float)lp->l_netpoints
219 / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
220 (void) fwrite((char *)log,
221 sizeof (struct logs), lp - log, fp);
222 (void) strcpy(log[NLOG-1].l_name, s->file->captain);
223 log[NLOG-1].l_uid = getuid();
224 log[NLOG-1].l_shipnum = s->file->index;
225 log[NLOG-1].l_gamenum = game;
226 log[NLOG-1].l_netpoints = s->file->points;
227 (void) fwrite((char *)&log[NLOG-1],
228 sizeof (struct logs), 1, fp);
229 (void) fwrite((char *)lp,
230 sizeof (struct logs), &log[NLOG-1] - lp, fp);
234 (void) flock(fileno(fp), LOCK_UN);