Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / sail / misc.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)misc.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/sail/misc.c,v 1.5 1999/11/30 03:49:34 billf Exp $";
40 #endif /* not lint */
41
42 #include "externs.h"
43 #include "pathnames.h"
44
45 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
46
47 /* XXX */
48 range(from, to)
49 struct ship *from, *to;
50 {
51         int bow1r, bow1c, bow2r, bow2c;
52         int stern1r, stern1c, stern2c, stern2r;
53         int bb, bs, sb, ss, result;
54
55         if (!to->file->dir)
56                 return -1;
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);
62         if (bb < 5) {
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)));
71         }
72         return result;
73 }
74
75 struct ship *
76 closestenemy(from, side, anyship)
77 struct ship *from;
78 char side, anyship;
79 {
80         struct ship *sp;
81         char a;
82         int olddist = 30000, dist;
83         struct ship *closest = 0;
84
85         a = capship(from)->nationality;
86         foreachship(sp) {
87                 if (sp == from)
88                         continue;
89                 if (sp->file->dir == 0)
90                         continue;
91                 if (a == capship(sp)->nationality && !anyship)
92                         continue;
93                 if (side && gunsbear(from, sp) != side)
94                         continue;
95                 dist = range(from, sp);
96                 if (dist < olddist) {
97                         closest = sp;
98                         olddist = dist;
99                 }
100         }
101         return closest;
102 }
103
104 angle(dr, dc)
105 int dr, dc;
106 {
107         int i;
108
109         if (dc >= 0 && dr > 0)
110                 i = 0;
111         else if (dr <= 0 && dc > 0)
112                 i = 2;
113         else if (dc <= 0 && dr < 0)
114                 i = 4;
115         else
116                 i = 6;
117         dr = abs(dr);
118         dc = abs(dc);
119         if ((i == 0 || i == 4) && dc * 2.4 > dr) {
120                 i++;
121                 if (dc > dr * 2.4)
122                         i++;
123         } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
124                 i++;
125                 if (dr > dc * 2.4)
126                         i++;
127         }
128         return i % 8 + 1;
129 }
130
131 gunsbear(from, to)              /* checks for target bow or stern */
132 struct ship *from, *to;
133 {
134         int Dr, Dc, i;
135         int ang;
136
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)
141                         ang += 8;
142                 if (ang >= 2 && ang <= 4)
143                         return 'r';
144                 if (ang >= 6 && ang <= 7)
145                         return 'l';
146                 Dr += dr[to->file->dir];
147                 Dc += dc[to->file->dir];
148         }
149         return 0;
150 }
151
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 */
156         int ang;
157         int Dr, Dc;
158
159         Dr = from->file->row - on->file->row;
160         Dc = on->file->col - from->file->col;
161         if (quick == -1) {
162                 Dr += dr[on->file->dir];
163                 Dc += dc[on->file->dir];
164         }
165         ang = angle(Dr, Dc);
166         if (quick != 0)
167                 return ang;
168         ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
169         return ang < 5;
170 }
171
172 colours(sp)
173 struct ship *sp;
174 {
175         char flag;
176
177         if (sp->file->struck)
178                 flag = '!';
179         if (sp->file->explode)
180                 flag = '#';
181         if (sp->file->sink)
182                 flag = '~';
183         if (sp->file->struck)
184                 return flag;
185         flag = *countryname[capship(sp)->nationality];
186         return sp->file->FS ? flag : tolower(flag);
187 }
188
189 #include <sys/file.h>
190 log(s)
191 struct ship *s;
192 {
193         FILE *fp;
194         int persons;
195         int n;
196         struct logs log[NLOG];
197         float net;
198         struct logs *lp;
199
200         if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
201                 return;
202 #ifdef LOCK_EX
203         if (flock(fileno(fp), LOCK_EX) < 0)
204                 return;
205 #endif
206         net = (float)s->file->points / s->specs->pts;
207         persons = getw(fp);
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;
212         rewind(fp);
213         if (persons < 0)
214                 (void) putw(1, fp);
215         else
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);
231                         break;
232                 }
233 #ifdef LOCK_EX
234         (void) flock(fileno(fp), LOCK_UN);
235 #endif
236         (void) fclose(fp);
237 }