MFC bandwith, delay, mirroring, and pfs work from HEAD.
[dragonfly.git] / games / sail / dr_3.c
blob7c0ccd60fa93c30202ef70a7937d5ecdbb9ec2bf
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. 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
31 * SUCH DAMAGE.
33 * @(#)dr_3.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $
35 * $DragonFly: src/games/sail/dr_3.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
38 #include "driver.h"
40 static bool stillmoving(int);
41 static bool isolated(struct ship *);
42 static bool push(struct ship *, struct ship *);
43 static void step(char, struct ship *, char *);
45 void
46 moveall(void) /* move all comp ships */
48 struct ship *sp, *sq; /* r11, r10 */
49 int n; /* r9 */
50 int k, l; /* r8, r7 */
51 int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
52 char moved[NSHIP];
55 * first try to create moves for OUR ships
57 foreachship(sp) {
58 struct ship *closest;
59 int ma, ta;
60 char af;
62 if (sp->file->captain[0] || sp->file->dir == 0)
63 continue;
64 if (!sp->file->struck && windspeed && !snagged(sp)
65 && sp->specs->crew3) {
66 ta = maxturns(sp, &af);
67 ma = maxmove(sp, sp->file->dir, 0);
68 closest = closestenemy(sp, 0, 0);
69 if (closest == 0)
70 *sp->file->movebuf = '\0';
71 else
72 closeon(sp, closest, sp->file->movebuf,
73 ta, ma, af);
74 } else
75 *sp->file->movebuf = '\0';
78 * Then execute the moves for ALL ships (dead ones too),
79 * checking for collisions and snags at each step.
80 * The old positions are saved in row[], col[], dir[].
81 * At the end, we compare and write out the changes.
83 n = 0;
84 foreachship(sp) {
85 if (snagged(sp))
86 strcpy(sp->file->movebuf, "d");
87 else
88 if (*sp->file->movebuf != 'd')
89 strcat(sp->file->movebuf, "d");
90 row[n] = sp->file->row;
91 col[n] = sp->file->col;
92 dir[n] = sp->file->dir;
93 drift[n] = sp->file->drift;
94 moved[n] = 0;
95 n++;
98 * Now resolve collisions.
99 * This is the tough part.
101 for (k = 0; stillmoving(k); k++) {
103 * Step once.
104 * And propagate the nulls at the end of sp->file->movebuf.
106 n = 0;
107 foreachship(sp) {
108 if (!sp->file->movebuf[k])
109 sp->file->movebuf[k+1] = '\0';
110 else if (sp->file->dir)
111 step(sp->file->movebuf[k], sp, &moved[n]);
112 n++;
115 * The real stuff.
117 n = 0;
118 foreachship(sp) {
119 if (sp->file->dir == 0 || isolated(sp))
120 goto cont1;
121 l = 0;
122 foreachship(sq) {
123 char snap = 0;
125 if (sp == sq)
126 goto cont2;
127 if (sq->file->dir == 0)
128 goto cont2;
129 if (!push(sp, sq))
130 goto cont2;
131 if (snagged2(sp, sq) && range(sp, sq) > 1)
132 snap++;
133 if (!range(sp, sq) && !fouled2(sp, sq)) {
134 makesignal(sp,
135 "collision with %s (%c%c)", sq);
136 if (die() < 4) {
137 makesignal(sp,
138 "fouled with %s (%c%c)",
139 sq);
140 Write(W_FOUL, sp, 0, l, 0, 0, 0);
141 Write(W_FOUL, sq, 0, n, 0, 0, 0);
143 snap++;
145 if (snap) {
146 sp->file->movebuf[k + 1] = 0;
147 sq->file->movebuf[k + 1] = 0;
148 sq->file->row = sp->file->row - 1;
149 if (sp->file->dir == 1
150 || sp->file->dir == 5)
151 sq->file->col =
152 sp->file->col - 1;
153 else
154 sq->file->col = sp->file->col;
155 sq->file->dir = sp->file->dir;
157 cont2:
158 l++;
160 cont1:
161 n++;
165 * Clear old moves. And write out new pos.
167 n = 0;
168 foreachship(sp) {
169 if (sp->file->dir != 0) {
170 *sp->file->movebuf = 0;
171 if (row[n] != sp->file->row)
172 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
173 if (col[n] != sp->file->col)
174 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
175 if (dir[n] != sp->file->dir)
176 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
177 if (drift[n] != sp->file->drift)
178 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
180 n++;
184 static bool
185 stillmoving(int k)
187 struct ship *sp;
189 foreachship(sp)
190 if (sp->file->movebuf[k])
191 return 1;
192 return 0;
195 static bool
196 isolated(struct ship *ship)
198 struct ship *sp;
200 foreachship(sp) {
201 if (ship != sp && range(ship, sp) <= 10)
202 return 0;
204 return 1;
207 static bool
208 push(struct ship *from, struct ship *to)
210 int bs, sb;
212 sb = to->specs->guns;
213 bs = from->specs->guns;
214 if (sb > bs)
215 return 1;
216 if (sb < bs)
217 return 0;
218 return from < to;
221 static void
222 step(char com, struct ship *sp, char *moved)
224 int dist;
226 switch (com) {
227 case 'r':
228 if (++sp->file->dir == 9)
229 sp->file->dir = 1;
230 break;
231 case 'l':
232 if (--sp->file->dir == 0)
233 sp->file->dir = 8;
234 break;
235 case '0': case '1': case '2': case '3':
236 case '4': case '5': case '6': case '7':
237 if (sp->file->dir % 2 == 0)
238 dist = dtab[com - '0'];
239 else
240 dist = com - '0';
241 sp->file->row -= dr[sp->file->dir] * dist;
242 sp->file->col -= dc[sp->file->dir] * dist;
243 *moved = 1;
244 break;
245 case 'b':
246 break;
247 case 'd':
248 if (!*moved) {
249 if (windspeed != 0 && ++sp->file->drift > 2 &&
250 ((sp->specs->class >= 3 && !snagged(sp))
251 || (turn & 1) == 0)) {
252 sp->file->row -= dr[winddir];
253 sp->file->col -= dc[winddir];
255 } else
256 sp->file->drift = 0;
257 break;
261 void
262 sendbp(struct ship *from, struct ship *to, int sections, char isdefense)
264 int n;
265 struct BP *bp;
267 bp = isdefense ? from->file->DBP : from->file->OBP;
268 for (n = 0; n < NBP && bp[n].turnsent; n++)
270 if (n < NBP && sections) {
271 Write(isdefense ? W_DBP : W_OBP, from, 0,
272 n, turn, to->file->index, sections);
273 if (isdefense)
274 makesignal(from, "repelling boarders",
275 (struct ship *)0);
276 else
277 makesignal(from, "boarding the %s (%c%c)", to);
282 toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
284 struct BP *bp;
285 int obp = 0;
286 int n, OBP = 0, DBP = 0, dbp = 0;
287 int qual;
289 qual = ship->specs->qual;
290 bp = isdefense ? ship->file->DBP : ship->file->OBP;
291 for (n = 0; n < NBP; n++, bp++) {
292 if (bp->turnsent && (to == bp->toship || isdefense)) {
293 obp += bp->mensent / 100
294 ? ship->specs->crew1 * qual : 0;
295 obp += (bp->mensent % 100)/10
296 ? ship->specs->crew2 * qual : 0;
297 obp += bp->mensent % 10
298 ? ship->specs->crew3 * qual : 0;
301 if (count || isdefense)
302 return obp;
303 OBP = toughmelee(to, ship, 0, count + 1);
304 dbp = toughmelee(ship, to, 1, count + 1);
305 DBP = toughmelee(to, ship, 1, count + 1);
306 if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
307 return 1;
308 else
309 return 0;
312 void
313 reload(void)
315 struct ship *sp;
317 foreachship(sp) {
318 sp->file->loadwith = 0;
322 void
323 checksails(void)
325 struct ship *sp;
326 int rig, full;
327 struct ship *closest;
329 foreachship(sp) {
330 if (sp->file->captain[0] != 0)
331 continue;
332 rig = sp->specs->rig1;
333 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
334 rig = 0;
335 if (rig && sp->specs->crew3) {
336 closest = closestenemy(sp, 0, 0);
337 if (closest != 0) {
338 if (range(sp, closest) > 9)
339 full = 1;
340 else
341 full = 0;
342 } else
343 full = 0;
344 } else
345 full = 0;
346 if ((sp->file->FS != 0) != full)
347 Write(W_FS, sp, 0, full, 0, 0, 0);