]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/misc.c
1 /* $NetBSD: misc.c,v 1.15 2004/11/05 21:30:32 dsl 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.15 2004/11/05 21:30:32 dsl Exp $");
47 #include "pathnames.h"
49 #define distance(x,y) (abs(x) >= abs(y) ? abs(x) + abs(y)/2 : abs(y) + abs(x)/2)
51 static int angle(int, int);
55 range(struct ship
*from
, struct ship
*to
)
57 int bow1r
, bow1c
, bow2r
, bow2c
;
58 int stern1r
, stern1c
, stern2c
, stern2r
;
59 int bb
, bs
, sb
, ss
, result
;
63 stern1r
= bow1r
= from
->file
->row
;
64 stern1c
= bow1c
= from
->file
->col
;
65 stern2r
= bow2r
= to
->file
->row
;
66 stern2c
= bow2c
= to
->file
->col
;
67 result
= bb
= distance(bow2r
- bow1r
, bow2c
- bow1c
);
69 stern2r
+= dr
[to
->file
->dir
];
70 stern2c
+= dc
[to
->file
->dir
];
71 stern1r
+= dr
[from
->file
->dir
];
72 stern1c
+= dc
[from
->file
->dir
];
73 bs
= distance((bow2r
- stern1r
), (bow2c
- stern1c
));
74 sb
= distance((bow1r
- stern2r
), (bow1c
- stern2c
));
75 ss
= distance((stern2r
- stern1r
) ,(stern2c
- stern1c
));
76 result
= min(bb
, min(bs
, min(sb
, ss
)));
82 closestenemy(struct ship
*from
, int side
, int anyship
)
86 int olddist
= 30000, dist
;
87 struct ship
*closest
= 0;
89 a
= capship(from
)->nationality
;
93 if (sp
->file
->dir
== 0)
95 if (a
== capship(sp
)->nationality
&& !anyship
)
97 if (side
&& gunsbear(from
, sp
) != side
)
99 dist
= range(from
, sp
);
100 if (dist
< olddist
) {
109 angle(int dr
, int dc
)
113 if (dc
>= 0 && dr
> 0)
115 else if (dr
<= 0 && dc
> 0)
117 else if (dc
<= 0 && dr
< 0)
123 if ((i
== 0 || i
== 4) && dc
* 2.4 > dr
) {
127 } else if ((i
== 2 || i
== 6) && dr
* 2.4 > dc
) {
135 /* checks for target bow or stern */
137 gunsbear(struct ship
*from
, struct ship
*to
)
142 Dr
= from
->file
->row
- to
->file
->row
;
143 Dc
= to
->file
->col
- from
->file
->col
;
144 for (i
= 2; i
; i
--) {
145 if ((ang
= angle(Dr
, Dc
) - from
->file
->dir
+ 1) < 1)
147 if (ang
>= 2 && ang
<= 4)
149 if (ang
>= 6 && ang
<= 7)
151 Dr
+= dr
[to
->file
->dir
];
152 Dc
+= dc
[to
->file
->dir
];
157 /* returns true if fromship is shooting at onship's starboard side */
159 portside(struct ship
*from
, struct ship
*on
, int quick
)
164 Dr
= from
->file
->row
- on
->file
->row
;
165 Dc
= on
->file
->col
- from
->file
->col
;
167 Dr
+= dr
[on
->file
->dir
];
168 Dc
+= dc
[on
->file
->dir
];
173 ang
= (ang
+ 4 - on
->file
->dir
- 1) % 8 + 1;
178 colours(struct ship
*sp
)
182 if (sp
->file
->struck
)
184 if (sp
->file
->explode
)
188 if (sp
->file
->struck
)
190 flag
= *countryname
[capship(sp
)->nationality
];
191 return sp
->file
->FS
? flag
: tolower((unsigned char)flag
);
195 logger(struct ship
*s
)
200 struct logs log
[NLOG
];
205 if ((fp
= fopen(_PATH_LOGFILE
, "r+")) == NULL
) {
211 if (flock(fileno(fp
), LOCK_EX
) < 0)
214 net
= (float)s
->file
->points
/ s
->specs
->pts
;
216 n
= fread((char *)log
, sizeof(struct logs
), NLOG
, fp
);
217 for (lp
= &log
[n
]; lp
< &log
[NLOG
]; lp
++)
218 lp
->l_name
[0] = lp
->l_uid
= lp
->l_shipnum
219 = lp
->l_gamenum
= lp
->l_netpoints
= 0;
224 putw(persons
+ 1, fp
);
225 for (lp
= log
; lp
< &log
[NLOG
]; lp
++)
226 if (net
> (float)lp
->l_netpoints
227 / scene
[lp
->l_gamenum
].ship
[lp
->l_shipnum
].specs
->pts
) {
228 fwrite((char *)log
, sizeof (struct logs
), lp
- log
, fp
);
229 strcpy(log
[NLOG
-1].l_name
, s
->file
->captain
);
230 log
[NLOG
-1].l_uid
= getuid();
231 log
[NLOG
-1].l_shipnum
= s
->file
->index
;
232 log
[NLOG
-1].l_gamenum
= game
;
233 log
[NLOG
-1].l_netpoints
= s
->file
->points
;
234 fwrite((char *)&log
[NLOG
-1], sizeof (struct logs
), 1, fp
);
235 fwrite((char *)lp
, sizeof (struct logs
), &log
[NLOG
-1] - lp
, fp
);
239 flock(fileno(fp
), LOCK_UN
);