]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/misc.c
1 /* $NetBSD: misc.c,v 1.13 2003/08/07 09:37:43 agc 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)misc.c 8.2 (Berkeley) 4/28/95";
37 __RCSID("$NetBSD: misc.c,v 1.13 2003/08/07 09:37:43 agc Exp $");
46 #include "pathnames.h"
48 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
50 static int angle(int, int);
54 range(struct ship
*from
, struct ship
*to
)
56 int bow1r
, bow1c
, bow2r
, bow2c
;
57 int stern1r
, stern1c
, stern2c
, stern2r
;
58 int bb
, bs
, sb
, ss
, result
;
62 stern1r
= bow1r
= from
->file
->row
;
63 stern1c
= bow1c
= from
->file
->col
;
64 stern2r
= bow2r
= to
->file
->row
;
65 stern2c
= bow2c
= to
->file
->col
;
66 result
= bb
= distance(bow2r
- bow1r
, bow2c
- bow1c
);
68 stern2r
+= dr
[to
->file
->dir
];
69 stern2c
+= dc
[to
->file
->dir
];
70 stern1r
+= dr
[from
->file
->dir
];
71 stern1c
+= dc
[from
->file
->dir
];
72 bs
= distance((bow2r
- stern1r
), (bow2c
- stern1c
));
73 sb
= distance((bow1r
- stern2r
), (bow1c
- stern2c
));
74 ss
= distance((stern2r
- stern1r
) ,(stern2c
- stern1c
));
75 result
= min(bb
, min(bs
, min(sb
, ss
)));
81 closestenemy(struct ship
*from
, int side
, int anyship
)
85 int olddist
= 30000, dist
;
86 struct ship
*closest
= 0;
88 a
= capship(from
)->nationality
;
92 if (sp
->file
->dir
== 0)
94 if (a
== capship(sp
)->nationality
&& !anyship
)
96 if (side
&& gunsbear(from
, sp
) != side
)
98 dist
= range(from
, sp
);
108 angle(int dr
, int dc
)
112 if (dc
>= 0 && dr
> 0)
114 else if (dr
<= 0 && dc
> 0)
116 else if (dc
<= 0 && dr
< 0)
122 if ((i
== 0 || i
== 4) && dc
* 2.4 > dr
) {
126 } else if ((i
== 2 || i
== 6) && dr
* 2.4 > dc
) {
134 /* checks for target bow or stern */
136 gunsbear(struct ship
*from
, struct ship
*to
)
141 Dr
= from
->file
->row
- to
->file
->row
;
142 Dc
= to
->file
->col
- from
->file
->col
;
143 for (i
= 2; i
; i
--) {
144 if ((ang
= angle(Dr
, Dc
) - from
->file
->dir
+ 1) < 1)
146 if (ang
>= 2 && ang
<= 4)
148 if (ang
>= 6 && ang
<= 7)
150 Dr
+= dr
[to
->file
->dir
];
151 Dc
+= dc
[to
->file
->dir
];
156 /* returns true if fromship is shooting at onship's starboard side */
158 portside(struct ship
*from
, struct ship
*on
, int quick
)
163 Dr
= from
->file
->row
- on
->file
->row
;
164 Dc
= on
->file
->col
- from
->file
->col
;
166 Dr
+= dr
[on
->file
->dir
];
167 Dc
+= dc
[on
->file
->dir
];
172 ang
= (ang
+ 4 - on
->file
->dir
- 1) % 8 + 1;
177 colours(struct ship
*sp
)
181 if (sp
->file
->struck
)
183 if (sp
->file
->explode
)
187 if (sp
->file
->struck
)
189 flag
= *countryname
[capship(sp
)->nationality
];
190 return sp
->file
->FS
? flag
: tolower(flag
);
194 logger(struct ship
*s
)
199 struct logs log
[NLOG
];
204 if ((fp
= fopen(_PATH_LOGFILE
, "r+")) == NULL
) {
210 if (flock(fileno(fp
), LOCK_EX
) < 0)
213 net
= (float)s
->file
->points
/ s
->specs
->pts
;
215 n
= fread((char *)log
, sizeof(struct logs
), NLOG
, fp
);
216 for (lp
= &log
[n
]; lp
< &log
[NLOG
]; lp
++)
217 lp
->l_name
[0] = lp
->l_uid
= lp
->l_shipnum
218 = lp
->l_gamenum
= lp
->l_netpoints
= 0;
223 putw(persons
+ 1, fp
);
224 for (lp
= log
; lp
< &log
[NLOG
]; lp
++)
225 if (net
> (float)lp
->l_netpoints
226 / scene
[lp
->l_gamenum
].ship
[lp
->l_shipnum
].specs
->pts
) {
227 fwrite((char *)log
, sizeof (struct logs
), lp
- log
, fp
);
228 strcpy(log
[NLOG
-1].l_name
, s
->file
->captain
);
229 log
[NLOG
-1].l_uid
= getuid();
230 log
[NLOG
-1].l_shipnum
= s
->file
->index
;
231 log
[NLOG
-1].l_gamenum
= game
;
232 log
[NLOG
-1].l_netpoints
= s
->file
->points
;
233 fwrite((char *)&log
[NLOG
-1], sizeof (struct logs
), 1, fp
);
234 fwrite((char *)lp
, sizeof (struct logs
), &log
[NLOG
-1] - lp
, fp
);
238 flock(fileno(fp
), LOCK_UN
);