games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / sail / misc.c
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)misc.c   8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/sail/misc.c,v 1.5 1999/11/30 03:49:34 billf Exp $
31  * $DragonFly: src/games/sail/misc.c,v 1.4 2006/09/03 17:33:13 pavalos Exp $
32  */
33
34 #include <sys/file.h>
35 #include "externs.h"
36 #include "pathnames.h"
37
38 #define distance(x,y) \
39         (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
40
41 static int angle(int, int);
42
43 /* XXX */
44 int
45 range(struct ship *from, struct ship *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(struct ship *from, char side, char anyship)
73 {
74         struct ship *sp;
75         char a;
76         int olddist = 30000, dist;
77         struct ship *closest = 0;
78
79         a = capship(from)->nationality;
80         foreachship(sp) {
81                 if (sp == from)
82                         continue;
83                 if (sp->file->dir == 0)
84                         continue;
85                 if (a == capship(sp)->nationality && !anyship)
86                         continue;
87                 if (side && gunsbear(from, sp) != side)
88                         continue;
89                 dist = range(from, sp);
90                 if (dist < olddist) {
91                         closest = sp;
92                         olddist = dist;
93                 }
94         }
95         return closest;
96 }
97
98 static int
99 angle(int Dr, int Dc)
100 {
101         int i;
102
103         if (Dc >= 0 && Dr > 0)
104                 i = 0;
105         else if (Dr <= 0 && Dc > 0)
106                 i = 2;
107         else if (Dc <= 0 && Dr < 0)
108                 i = 4;
109         else
110                 i = 6;
111         Dr = abs(Dr);
112         Dc = abs(Dc);
113         if ((i == 0 || i == 4) && Dc * 2.4 > Dr) {
114                 i++;
115                 if (Dc > Dr * 2.4)
116                         i++;
117         } else if ((i == 2 || i == 6) && Dr * 2.4 > Dc) {
118                 i++;
119                 if (Dr > Dc * 2.4)
120                         i++;
121         }
122         return i % 8 + 1;
123 }
124
125 /* checks for target bow or stern */
126 char
127 gunsbear(struct ship *from, struct ship *to)
128 {
129         int Dr, Dc, i;
130         int ang;
131
132         Dr = from->file->row - to->file->row;
133         Dc = to->file->col - from->file->col;
134         for (i = 2; i; i--) {
135                 if ((ang = angle(Dr, Dc) - from->file->dir + 1) < 1)
136                         ang += 8;
137                 if (ang >= 2 && ang <= 4)
138                         return 'r';
139                 if (ang >= 6 && ang <= 7)
140                         return 'l';
141                 Dr += dr[to->file->dir];
142                 Dc += dc[to->file->dir];
143         }
144         return 0;
145 }
146
147 /* returns true if fromship is shooting at onship's starboard side */
148 int
149 portside(struct ship *from, struct ship *on, int quick)
150 {
151         int ang;
152         int Dr, Dc;
153
154         Dr = from->file->row - on->file->row;
155         Dc = on->file->col - from->file->col;
156         if (quick == -1) {
157                 Dr += dr[on->file->dir];
158                 Dc += dc[on->file->dir];
159         }
160         ang = angle(Dr, Dc);
161         if (quick != 0)
162                 return ang;
163         ang = (ang + 4 - on->file->dir - 1) % 8 + 1;
164         return ang < 5;
165 }
166
167 char
168 colours(struct ship *sp)
169 {
170         char flag = '\0';
171
172         if (sp->file->struck)
173                 flag = '!';
174         if (sp->file->explode)
175                 flag = '#';
176         if (sp->file->sink)
177                 flag = '~';
178         if (sp->file->struck)
179                 return flag;
180         flag = *countryname[capship(sp)->nationality];
181         return sp->file->FS ? flag : tolower(flag);
182 }
183
184 void
185 write_log(struct ship *s)
186 {
187         FILE *fp;
188         int persons;
189         int n;
190         struct logs log[NLOG];
191         float net;
192         struct logs *lp;
193
194         if ((fp = fopen(_PATH_LOGFILE, "r+")) == NULL)
195                 return;
196 #ifdef LOCK_EX
197         if (flock(fileno(fp), LOCK_EX) < 0)
198                 return;
199 #endif
200         net = (float)s->file->points / s->specs->pts;
201         persons = getw(fp);
202         n = fread((char *)log, sizeof(struct logs), NLOG, fp);
203         for (lp = &log[n]; lp < &log[NLOG]; lp++)
204                 lp->l_name[0] = lp->l_uid = lp->l_shipnum
205                         = lp->l_gamenum = lp->l_netpoints = 0;
206         rewind(fp);
207         if (persons < 0)
208                 putw(1, fp);
209         else
210                 putw(persons + 1, fp);
211         for (lp = log; lp < &log[NLOG]; lp++)
212                 if (net > (float)lp->l_netpoints
213                     / scene[lp->l_gamenum].ship[lp->l_shipnum].specs->pts) {
214                         fwrite((char *)log,
215                                 sizeof (struct logs), lp - log, fp);
216                         strcpy(log[NLOG-1].l_name, s->file->captain);
217                         log[NLOG-1].l_uid = getuid();
218                         log[NLOG-1].l_shipnum = s->file->index;
219                         log[NLOG-1].l_gamenum = game;
220                         log[NLOG-1].l_netpoints = s->file->points;
221                         fwrite((char *)&log[NLOG-1],
222                                 sizeof (struct logs), 1, fp);
223                         fwrite((char *)lp,
224                                 sizeof (struct logs), &log[NLOG-1] - lp, fp);
225                         break;
226                 }
227 #ifdef LOCK_EX
228         flock(fileno(fp), LOCK_UN);
229 #endif
230         fclose(fp);
231 }