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