misc.c revision 1.9 1 /* $NetBSD: misc.c,v 1.9 2001/01/04 03:21:17 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.9 2001/01/04 03:21:17 jwise Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <unistd.h>
47 #include "extern.h"
48 #include "pathnames.h"
49
50 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
51
52 int range(struct ship *, struct ship *);
53 struct ship *closestenemy(struct ship *, int, int);
54 static int angle(int, int);
55 int gunsbear(struct ship *, struct ship *);
56 int portside(struct ship *, struct ship *, int);
57 int colours(struct ship *);
58 void logger(struct ship *);
59
60 /* XXX */
61 int
62 range(struct ship *from, struct ship *to)
63 {
64 int bow1r, bow1c, bow2r, bow2c;
65 int stern1r, stern1c, stern2c, stern2r;
66 int bb, bs, sb, ss, result;
67
68 if (!to->file->dir)
69 return -1;
70 stern1r = bow1r = from->file->row;
71 stern1c = bow1c = from->file->col;
72 stern2r = bow2r = to->file->row;
73 stern2c = bow2c = to->file->col;
74 result = bb = distance(bow2r - bow1r, bow2c - bow1c);
75 if (bb < 5) {
76 stern2r += dr[to->file->dir];
77 stern2c += dc[to->file->dir];
78 stern1r += dr[from->file->dir];
79 stern1c += dc[from->file->dir];
80 bs = distance((bow2r - stern1r), (bow2c - stern1c));
81 sb = distance((bow1r - stern2r), (bow1c - stern2c));
82 ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
83 result = min(bb, min(bs, min(sb, ss)));
84 }
85 return result;
86 }
87
88 struct ship *
89 closestenemy(struct ship *from, int side, int anyship)
90 {
91 struct ship *sp;
92 char a;
93 int olddist = 30000, dist;
94 struct ship *closest = 0;
95
96 a = capship(from)->nationality;
97 foreachship(sp) {
98 if (sp == from)
99 continue;
100 if (sp->file->dir == 0)
101 continue;
102 if (a == capship(sp)->nationality && !anyship)
103 continue;
104 if (side && gunsbear(from, sp) != side)
105 continue;
106 dist = range(from, sp);
107 if (dist < olddist) {
108 closest = sp;
109 olddist = dist;
110 }
111 }
112 return closest;
113 }
114
115 static int
116 angle(int dr, int dc)
117 {
118 int i;
119
120 if (dc >= 0 && dr > 0)
121 i = 0;
122 else if (dr <= 0 && dc > 0)
123 i = 2;
124 else if (dc <= 0 && dr < 0)
125 i = 4;
126 else
127 i = 6;
128 dr = abs(dr);
129 dc = abs(dc);
130 if ((i == 0 || i == 4) && dc * 2.4 > dr) {
131 i++;
132 if (dc > dr * 2.4)
133 i++;
134 } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
135 i++;
136 if (dr > dc * 2.4)
137 i++;
138 }
139 return i % 8 + 1;
140 }
141
142 /* checks for target bow or stern */
143 int
144 gunsbear(struct ship *from, struct ship *to)
145 {
146 int Dr, Dc, i;
147 int ang;
148
149 Dr = from->file->row - to->file->row;
150 Dc = to->file->col - from->file->col;
151 for (i = 2; i; i--) {
152 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
153 ang += 8;
154 if (ang >= 2 && ang <= 4)
155 return 'r';
156 if (ang >= 6 && ang <= 7)
157 return 'l';
158 Dr += dr[to->file->dir];
159 Dc += dc[to->file->dir];
160 }
161 return 0;
162 }
163
164 /* returns true if fromship is shooting at onship's starboard side */
165 int
166 portside(struct ship *from, struct ship *on, int quick)
167 {
168 int ang;
169 int Dr, Dc;
170
171 Dr = from->file->row - on->file->row;
172 Dc = on->file->col - from->file->col;
173 if (quick == -1) {
174 Dr += dr[on->file->dir];
175 Dc += dc[on->file->dir];
176 }
177 ang = angle(Dr, Dc);
178 if (quick != 0)
179 return ang;
180 ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
181 return ang < 5;
182 }
183
184 int
185 colours(struct ship *sp)
186 {
187 char flag = '\0';
188
189 if (sp->file->struck)
190 flag = '!';
191 if (sp->file->explode)
192 flag = '#';
193 if (sp->file->sink)
194 flag = '~';
195 if (sp->file->struck)
196 return flag;
197 flag = *countryname[capship(sp)->nationality];
198 return sp->file->FS ? flag : tolower(flag);
199 }
200
201 void
202 logger(struct ship *s)
203 {
204 FILE *fp;
205 int persons;
206 int n;
207 struct logs log[NLOG];
208 float net;
209 struct logs *lp;
210
211 setegid(egid);
212 if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL) {
213 setegid(gid);
214 return;
215 }
216 setegid(gid);
217 #ifdef LOCK_EX
218 if (flock(fileno(fp), LOCK_EX) < 0)
219 return;
220 #endif
221 net = (float)s->file->points / s->specs->pts;
222 persons = getw(fp);
223 n = fread((char *)log, sizeof(struct logs), NLOG, fp);
224 for (lp = &log[n]; lp < &log[NLOG]; lp++)
225 lp->l_name[0] = lp->l_uid = lp->l_shipnum
226 = lp->l_gamenum = lp->l_netpoints = 0;
227 rewind(fp);
228 if (persons < 0)
229 putw(1, fp);
230 else
231 putw(persons + 1, fp);
232 for (lp = log; lp < &log[NLOG]; lp++)
233 if (net > (float)lp->l_netpoints
234 / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
235 fwrite((char *)log, sizeof (struct logs), lp - log, fp);
236 strcpy(log[NLOG-1].l_name, s->file->captain);
237 log[NLOG-1].l_uid = getuid();
238 log[NLOG-1].l_shipnum = s->file->index;
239 log[NLOG-1].l_gamenum = game;
240 log[NLOG-1].l_netpoints = s->file->points;
241 fwrite((char *)&log[NLOG-1], sizeof (struct logs), 1, fp);
242 fwrite((char *)lp, sizeof (struct logs), &log[NLOG-1] - lp, fp);
243 break;
244 }
245 #ifdef LOCK_EX
246 flock(fileno(fp), LOCK_UN);
247 #endif
248 fclose(fp);
249 }
250