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