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