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