BSD 4_3_Reno release
[unix-history] / usr / src / games / sail / misc.c
CommitLineData
b5f0675e 1/*
1e008c14 2 * Copyright (c) 1983 Regents of the University of California.
c0ca48ec
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
1c15e888
C
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
65c7d3b6
KB
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1c15e888 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
b5f0675e
EW
18 */
19
b40b218a 20#ifndef lint
1c15e888 21static char sccsid[] = "@(#)misc.c 5.5 (Berkeley) 6/1/90";
c0ca48ec 22#endif /* not lint */
b5f0675e 23
b40b218a 24#include "externs.h"
25d40e98 25#include "pathnames.h"
b40b218a
CL
26
27#define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
28
b3a57661
EW
29/* XXX */
30range(from, to)
31struct ship *from, *to;
b40b218a 32{
b3a57661
EW
33 register bow1r, bow1c, bow2r, bow2c;
34 int stern1r, stern1c, stern2c, stern2r;
35 register int bb, bs, sb, ss, result;
b40b218a 36
b3a57661 37 if (!to->file->dir)
6c17b19a 38 return -1;
b3a57661
EW
39 stern1r = bow1r = from->file->row;
40 stern1c = bow1c = from->file->col;
41 stern2r = bow2r = to->file->row;
42 stern2c = bow2c = to->file->col;
43 result = bb = distance(bow2r - bow1r, bow2c - bow1c);
44 if (bb < 5) {
45 stern2r += dr[to->file->dir];
46 stern2c += dc[to->file->dir];
47 stern1r += dr[from->file->dir];
48 stern1c += dc[from->file->dir];
49 bs = distance((bow2r - stern1r), (bow2c - stern1c));
50 sb = distance((bow1r - stern2r), (bow1c - stern2c));
51 ss = distance((stern2r - stern1r) ,(stern2c - stern1c));
52 result = min(bb, min(bs, min(sb, ss)));
b40b218a 53 }
b3a57661 54 return result;
b40b218a
CL
55}
56
b3a57661
EW
57struct ship *
58closestenemy(from, side, anyship)
59register struct ship *from;
60char side, anyship;
b40b218a 61{
b3a57661
EW
62 register struct ship *sp;
63 register char a;
64 int olddist = 30000, dist;
65 struct ship *closest = 0;
66
67 a = capship(from)->nationality;
68 foreachship(sp) {
69 if (sp == from)
70 continue;
71 if (sp->file->dir == 0)
72 continue;
73 if (a == capship(sp)->nationality && !anyship)
74 continue;
75 if (side && gunsbear(from, sp) != side)
76 continue;
77 dist = range(from, sp);
78 if (dist < olddist) {
79 closest = sp;
80 olddist = dist;
81 }
b40b218a 82 }
b3a57661 83 return closest;
b40b218a 84}
6c17b19a
EW
85
86angle(dr, dc)
87register dr, dc;
88{
89 register i;
90
91 if (dc >= 0 && dr > 0)
92 i = 0;
93 else if (dr <= 0 && dc > 0)
94 i = 2;
95 else if (dc <= 0 && dr < 0)
96 i = 4;
97 else
98 i = 6;
99 dr = abs(dr);
100 dc = abs(dc);
101 if ((i == 0 || i == 4) && dc * 2.4 > dr) {
102 i++;
103 if (dc > dr * 2.4)
104 i++;
105 } else if ((i == 2 || i == 6) && dr * 2.4 > dc) {
106 i++;
107 if (dr > dc * 2.4)
108 i++;
109 }
110 return i % 8 + 1;
111}
112
113gunsbear(from, to) /* checks for target bow or stern */
114register struct ship *from, *to;
115{
116 int Dr, Dc, i;
117 register ang;
118
119 Dr = from->file->row - to->file->row;
120 Dc = to->file->col - from->file->col;
121 for (i = 2; i; i--) {
122 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
123 ang += 8;
124 if (ang >= 2 && ang <= 4)
125 return 'r';
126 if (ang >= 6 && ang <= 7)
127 return 'l';
128 Dr += dr[to->file->dir];
129 Dc += dc[to->file->dir];
130 }
131 return 0;
132}
133
134portside(from, on, quick)
135register struct ship *from, *on;
136int quick; /* returns true if fromship is */
137{ /* shooting at onship's starboard side */
138 register ang;
139 register Dr, Dc;
140
141 Dr = from->file->row - on->file->row;
142 Dc = on->file->col - from->file->col;
143 if (quick == -1) {
144 Dr += dr[on->file->dir];
145 Dc += dc[on->file->dir];
146 }
147 ang = angle(Dr, Dc);
148 if (quick != 0)
149 return ang;
150 ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
151 return ang < 5;
152}
153
6c17b19a
EW
154colours(sp)
155register struct ship *sp;
156{
157 register char flag;
158
159 if (sp->file->struck)
160 flag = '!';
161 if (sp->file->explode)
162 flag = '#';
163 if (sp->file->sink)
164 flag = '~';
165 if (sp->file->struck)
166 return flag;
167 flag = *countryname[capship(sp)->nationality];
168 return sp->file->FS ? flag : tolower(flag);
169}
170
13011fd2
EW
171#include <sys/file.h>
172log(s)
173register struct ship *s;
6c17b19a 174{
13011fd2
EW
175 FILE *fp;
176 int persons;
177 int n;
178 struct logs log[NLOG];
179 float net;
180 register struct logs *lp;
181
25d40e98 182 if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
13011fd2
EW
183 return;
184#ifdef LOCK_EX
9a2a5150
EW
185 if (flock(fileno(fp), LOCK_EX) < 0)
186 return;
6c17b19a 187#endif
13011fd2
EW
188 net = (float)s->file->points / s->specs->pts;
189 persons = getw(fp);
190 n = fread((char *)log, sizeof(struct logs), NLOG, fp);
191 for (lp = &log[n]; lp < &log[NLOG]; lp++)
192 lp->l_name[0] = lp->l_uid = lp->l_shipnum
193 = lp->l_gamenum = lp->l_netpoints = 0;
194 rewind(fp);
195 if (persons < 0)
196 (void) putw(1, fp);
197 else
198 (void) putw(persons + 1, fp);
199 for (lp = log; lp < &log[NLOG]; lp++)
200 if (net > (float)lp->l_netpoints
201 / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
202 (void) fwrite((char *)log,
203 sizeof (struct logs), lp - log, fp);
204 (void) strcpy(log[NLOG-1].l_name, s->file->captain);
205 log[NLOG-1].l_uid = getuid();
206 log[NLOG-1].l_shipnum = s->file->index;
207 log[NLOG-1].l_gamenum = game;
208 log[NLOG-1].l_netpoints = s->file->points;
209 (void) fwrite((char *)&log[NLOG-1],
210 sizeof (struct logs), 1, fp);
211 (void) fwrite((char *)lp,
212 sizeof (struct logs), &log[NLOG-1] - lp, fp);
213 break;
214 }
215#ifdef LOCK_EX
9a2a5150 216 (void) flock(fileno(fp), LOCK_UN);
13011fd2
EW
217#endif
218 (void) fclose(fp);
219}