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