]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/misc.c
2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $";
40 #include "pathnames.h"
42 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
46 struct ship
*from
, *to
;
48 register bow1r
, bow1c
, bow2r
, bow2c
;
49 int stern1r
, stern1c
, stern2c
, stern2r
;
50 register int bb
, bs
, sb
, ss
, result
;
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
);
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
)));
73 closestenemy(from
, side
, anyship
)
74 register struct ship
*from
;
77 register struct ship
*sp
;
79 int olddist
= 30000, dist
;
80 struct ship
*closest
= 0;
82 a
= capship(from
)->nationality
;
86 if (sp
->file
->dir
== 0)
88 if (a
== capship(sp
)->nationality
&& !anyship
)
90 if (side
&& gunsbear(from
, sp
) != side
)
92 dist
= range(from
, sp
);
106 if (dc
>= 0 && dr
> 0)
108 else if (dr
<= 0 && dc
> 0)
110 else if (dc
<= 0 && dr
< 0)
116 if ((i
== 0 || i
== 4) && dc
* 2.4 > dr
) {
120 } else if ((i
== 2 || i
== 6) && dr
* 2.4 > dc
) {
128 gunsbear(from
, to
) /* checks for target bow or stern */
129 register struct ship
*from
, *to
;
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)
139 if (ang
>= 2 && ang
<= 4)
141 if (ang
>= 6 && ang
<= 7)
143 Dr
+= dr
[to
->file
->dir
];
144 Dc
+= dc
[to
->file
->dir
];
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 */
156 Dr
= from
->file
->row
- on
->file
->row
;
157 Dc
= on
->file
->col
- from
->file
->col
;
159 Dr
+= dr
[on
->file
->dir
];
160 Dc
+= dc
[on
->file
->dir
];
165 ang
= (ang
+ 4 - on
->file
->dir
- 1) % 8 + 1;
170 register struct ship
*sp
;
174 if (sp
->file
->struck
)
176 if (sp
->file
->explode
)
180 if (sp
->file
->struck
)
182 flag
= *countryname
[capship(sp
)->nationality
];
183 return sp
->file
->FS
? flag
: tolower(flag
);
186 #include <sys/file.h>
188 register struct ship
*s
;
193 struct logs log
[NLOG
];
195 register struct logs
*lp
;
197 if ((fp
= fopen(_PATH_LOGFILE
, "r+")) == NULL
)
200 if (flock(fileno(fp
), LOCK_EX
) < 0)
203 net
= (float)s
->file
->points
/ s
->specs
->pts
;
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;
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
);
231 (void) flock(fileno(fp
), LOCK_UN
);