]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/misc.c
1 /* $NetBSD: misc.c,v 1.3 1995/04/22 10:37:03 cgd Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid
[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
40 static char rcsid
[] = "$NetBSD: misc.c,v 1.3 1995/04/22 10:37:03 cgd Exp $";
45 #include "pathnames.h"
47 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
51 struct ship
*from
, *to
;
53 register bow1r
, bow1c
, bow2r
, bow2c
;
54 int stern1r
, stern1c
, stern2c
, stern2r
;
55 register int bb
, bs
, sb
, ss
, result
;
59 stern1r
= bow1r
= from
->file
->row
;
60 stern1c
= bow1c
= from
->file
->col
;
61 stern2r
= bow2r
= to
->file
->row
;
62 stern2c
= bow2c
= to
->file
->col
;
63 result
= bb
= distance(bow2r
- bow1r
, bow2c
- bow1c
);
65 stern2r
+= dr
[to
->file
->dir
];
66 stern2c
+= dc
[to
->file
->dir
];
67 stern1r
+= dr
[from
->file
->dir
];
68 stern1c
+= dc
[from
->file
->dir
];
69 bs
= distance((bow2r
- stern1r
), (bow2c
- stern1c
));
70 sb
= distance((bow1r
- stern2r
), (bow1c
- stern2c
));
71 ss
= distance((stern2r
- stern1r
) ,(stern2c
- stern1c
));
72 result
= min(bb
, min(bs
, min(sb
, ss
)));
78 closestenemy(from
, side
, anyship
)
79 register struct ship
*from
;
82 register struct ship
*sp
;
84 int olddist
= 30000, dist
;
85 struct ship
*closest
= 0;
87 a
= capship(from
)->nationality
;
91 if (sp
->file
->dir
== 0)
93 if (a
== capship(sp
)->nationality
&& !anyship
)
95 if (side
&& gunsbear(from
, sp
) != side
)
97 dist
= range(from
, sp
);
111 if (dc
>= 0 && dr
> 0)
113 else if (dr
<= 0 && dc
> 0)
115 else if (dc
<= 0 && dr
< 0)
121 if ((i
== 0 || i
== 4) && dc
* 2.4 > dr
) {
125 } else if ((i
== 2 || i
== 6) && dr
* 2.4 > dc
) {
133 gunsbear(from
, to
) /* checks for target bow or stern */
134 register struct ship
*from
, *to
;
139 Dr
= from
->file
->row
- to
->file
->row
;
140 Dc
= to
->file
->col
- from
->file
->col
;
141 for (i
= 2; i
; i
--) {
142 if ((ang
= angle(Dr
, Dc
) - from
->file
->dir
+ 1) < 1)
144 if (ang
>= 2 && ang
<= 4)
146 if (ang
>= 6 && ang
<= 7)
148 Dr
+= dr
[to
->file
->dir
];
149 Dc
+= dc
[to
->file
->dir
];
154 portside(from
, on
, quick
)
155 register struct ship
*from
, *on
;
156 int quick
; /* returns true if fromship is */
157 { /* shooting at onship's starboard side */
161 Dr
= from
->file
->row
- on
->file
->row
;
162 Dc
= on
->file
->col
- from
->file
->col
;
164 Dr
+= dr
[on
->file
->dir
];
165 Dc
+= dc
[on
->file
->dir
];
170 ang
= (ang
+ 4 - on
->file
->dir
- 1) % 8 + 1;
175 register struct ship
*sp
;
179 if (sp
->file
->struck
)
181 if (sp
->file
->explode
)
185 if (sp
->file
->struck
)
187 flag
= *countryname
[capship(sp
)->nationality
];
188 return sp
->file
->FS
? flag
: tolower(flag
);
191 #include <sys/file.h>
193 register struct ship
*s
;
198 struct logs log
[NLOG
];
200 register struct logs
*lp
;
202 if ((fp
= fopen(_PATH_LOGFILE
, "r+")) == NULL
)
205 if (flock(fileno(fp
), LOCK_EX
) < 0)
208 net
= (float)s
->file
->points
/ s
->specs
->pts
;
210 n
= fread((char *)log
, sizeof(struct logs
), NLOG
, fp
);
211 for (lp
= &log
[n
]; lp
< &log
[NLOG
]; lp
++)
212 lp
->l_name
[0] = lp
->l_uid
= lp
->l_shipnum
213 = lp
->l_gamenum
= lp
->l_netpoints
= 0;
218 (void) putw(persons
+ 1, fp
);
219 for (lp
= log
; lp
< &log
[NLOG
]; lp
++)
220 if (net
> (float)lp
->l_netpoints
221 / scene
[lp
->l_gamenum
].ship
[lp
->l_shipnum
].specs
->pts
) {
222 (void) fwrite((char *)log
,
223 sizeof (struct logs
), lp
- log
, fp
);
224 (void) strcpy(log
[NLOG
-1].l_name
, s
->file
->captain
);
225 log
[NLOG
-1].l_uid
= getuid();
226 log
[NLOG
-1].l_shipnum
= s
->file
->index
;
227 log
[NLOG
-1].l_gamenum
= game
;
228 log
[NLOG
-1].l_netpoints
= s
->file
->points
;
229 (void) fwrite((char *)&log
[NLOG
-1],
230 sizeof (struct logs
), 1, fp
);
231 (void) fwrite((char *)lp
,
232 sizeof (struct logs
), &log
[NLOG
-1] - lp
, fp
);
236 (void) flock(fileno(fp
), LOCK_UN
);