w(1): Limit affect of locale change
[dragonfly.git] / games / sail / dr_3.c
blob7eb84238b9141d93a3dc9c22b983643bc6b26cdc
1 /*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
29 * @(#)dr_3.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $
33 #include "driver.h"
35 static bool stillmoving(int);
36 static bool isolated(struct ship *);
37 static bool push(struct ship *, struct ship *);
38 static void step(char, struct ship *, char *);
40 /* move all comp ships */
41 void
42 moveall(void)
44 struct ship *sp, *sq;
45 int n;
46 int k, l;
47 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
48 char moved[NSHIP];
51 * first try to create moves for OUR ships
53 foreachship(sp) {
54 struct ship *closest;
55 int ma, ta;
56 char af;
58 if (sp->file->captain[0] || sp->file->dir == 0)
59 continue;
60 if (!sp->file->struck && windspeed && !snagged(sp)
61 && sp->specs->crew3) {
62 ta = maxturns(sp, &af);
63 ma = maxmove(sp, sp->file->dir, 0);
64 closest = closestenemy(sp, 0, 0);
65 if (closest == NULL)
66 *sp->file->movebuf = '\0';
67 else
68 closeon(sp, closest, sp->file->movebuf,
69 ta, ma, af);
70 } else
71 *sp->file->movebuf = '\0';
74 * Then execute the moves for ALL ships (dead ones too),
75 * checking for collisions and snags at each step.
76 * The old positions are saved in row[], col[], dir[].
77 * At the end, we compare and write out the changes.
79 n = 0;
80 foreachship(sp) {
81 if (snagged(sp))
82 strcpy(sp->file->movebuf, "d");
83 else
84 if (*sp->file->movebuf != 'd')
85 strcat(sp->file->movebuf, "d");
86 row[n] = sp->file->row;
87 col[n] = sp->file->col;
88 dir[n] = sp->file->dir;
89 drift[n] = sp->file->drift;
90 moved[n] = 0;
91 n++;
94 * Now resolve collisions.
95 * This is the tough part.
97 for (k = 0; stillmoving(k); k++) {
99 * Step once.
100 * And propagate the nulls at the end of sp->file->movebuf.
102 n = 0;
103 foreachship(sp) {
104 if (!sp->file->movebuf[k])
105 sp->file->movebuf[k+1] = '\0';
106 else if (sp->file->dir)
107 step(sp->file->movebuf[k], sp, &moved[n]);
108 n++;
111 * The real stuff.
113 n = 0;
114 foreachship(sp) {
115 if (sp->file->dir == 0 || isolated(sp))
116 goto cont1;
117 l = 0;
118 foreachship(sq) {
119 char snap = 0;
121 if (sp == sq)
122 goto cont2;
123 if (sq->file->dir == 0)
124 goto cont2;
125 if (!push(sp, sq))
126 goto cont2;
127 if (snagged2(sp, sq) && range(sp, sq) > 1)
128 snap++;
129 if (!range(sp, sq) && !fouled2(sp, sq)) {
130 makesignal(sp,
131 "collision with %s (%c%c)", sq);
132 if (die() < 4) {
133 makesignal(sp,
134 "fouled with %s (%c%c)",
135 sq);
136 Write(W_FOUL, sp, l, 0, 0, 0);
137 Write(W_FOUL, sq, n, 0, 0, 0);
139 snap++;
141 if (snap) {
142 sp->file->movebuf[k + 1] = 0;
143 sq->file->movebuf[k + 1] = 0;
144 sq->file->row = sp->file->row - 1;
145 if (sp->file->dir == 1
146 || sp->file->dir == 5)
147 sq->file->col =
148 sp->file->col - 1;
149 else
150 sq->file->col = sp->file->col;
151 sq->file->dir = sp->file->dir;
153 cont2:
154 l++;
156 cont1:
157 n++;
161 * Clear old moves. And write out new pos.
163 n = 0;
164 foreachship(sp) {
165 if (sp->file->dir != 0) {
166 *sp->file->movebuf = 0;
167 if (row[n] != sp->file->row)
168 Write(W_ROW, sp, sp->file->row, 0, 0, 0);
169 if (col[n] != sp->file->col)
170 Write(W_COL, sp, sp->file->col, 0, 0, 0);
171 if (dir[n] != sp->file->dir)
172 Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
173 if (drift[n] != sp->file->drift)
174 Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
176 n++;
180 static bool
181 stillmoving(int k)
183 struct ship *sp;
185 foreachship(sp)
186 if (sp->file->movebuf[k])
187 return 1;
188 return 0;
191 static bool
192 isolated(struct ship *ship)
194 struct ship *sp;
196 foreachship(sp) {
197 if (ship != sp && range(ship, sp) <= 10)
198 return 0;
200 return 1;
203 static bool
204 push(struct ship *from, struct ship *to)
206 int bs, sb;
208 sb = to->specs->guns;
209 bs = from->specs->guns;
210 if (sb > bs)
211 return 1;
212 if (sb < bs)
213 return 0;
214 return from < to;
217 static void
218 step(char com, struct ship *sp, char *moved)
220 int dist;
222 switch (com) {
223 case 'r':
224 if (++sp->file->dir == 9)
225 sp->file->dir = 1;
226 break;
227 case 'l':
228 if (--sp->file->dir == 0)
229 sp->file->dir = 8;
230 break;
231 case '0': case '1': case '2': case '3':
232 case '4': case '5': case '6': case '7':
233 if (sp->file->dir % 2 == 0)
234 dist = dtab[com - '0'];
235 else
236 dist = com - '0';
237 sp->file->row -= dr[sp->file->dir] * dist;
238 sp->file->col -= dc[sp->file->dir] * dist;
239 *moved = 1;
240 break;
241 case 'b':
242 break;
243 case 'd':
244 if (!*moved) {
245 if (windspeed != 0 && ++sp->file->drift > 2 &&
246 ((sp->specs->class >= 3 && !snagged(sp))
247 || (turn & 1) == 0)) {
248 sp->file->row -= dr[winddir];
249 sp->file->col -= dc[winddir];
251 } else {
252 sp->file->drift = 0;
254 break;
258 void
259 sendbp(struct ship *from, struct ship *to, int sections, char isdefense)
261 int n;
262 struct BP *bp;
264 bp = isdefense ? from->file->DBP : from->file->OBP;
265 for (n = 0; n < NBP && bp[n].turnsent; n++)
267 if (n < NBP && sections) {
268 Write(isdefense ? W_DBP : W_OBP, from,
269 n, turn, to->file->index, sections);
270 if (isdefense)
271 makesignal(from, "repelling boarders", NULL);
272 else
273 makesignal(from, "boarding the %s (%c%c)", to);
278 toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
280 struct BP *bp;
281 int obp = 0;
282 int n, OBP = 0, DBP = 0, dbp = 0;
283 int qual;
285 qual = ship->specs->qual;
286 bp = isdefense ? ship->file->DBP : ship->file->OBP;
287 for (n = 0; n < NBP; n++, bp++) {
288 if (bp->turnsent && (to == bp->toship || isdefense)) {
289 obp += bp->mensent / 100
290 ? ship->specs->crew1 * qual : 0;
291 obp += (bp->mensent % 100)/10
292 ? ship->specs->crew2 * qual : 0;
293 obp += bp->mensent % 10
294 ? ship->specs->crew3 * qual : 0;
297 if (count || isdefense)
298 return obp;
299 OBP = toughmelee(to, ship, 0, count + 1);
300 dbp = toughmelee(ship, to, 1, count + 1);
301 DBP = toughmelee(to, ship, 1, count + 1);
302 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
303 return 1;
304 else
305 return 0;
308 void
309 reload(void)
311 struct ship *sp;
313 foreachship(sp) {
314 sp->file->loadwith = 0;
318 void
319 checksails(void)
321 struct ship *sp;
322 int rig, full;
323 struct ship *closest;
325 foreachship(sp) {
326 if (sp->file->captain[0] != 0)
327 continue;
328 rig = sp->specs->rig1;
329 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
330 rig = 0;
331 if (rig && sp->specs->crew3) {
332 closest = closestenemy(sp, 0, 0);
333 if (closest != NULL) {
334 if (range(sp, closest) > 9)
335 full = 1;
336 else
337 full = 0;
338 } else
339 full = 0;
340 } else
341 full = 0;
342 if ((sp->file->FS != 0) != full)
343 Write(W_FS, sp, full, 0, 0, 0);