Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / sail / dr_3.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)dr_3.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/sail/dr_3.c,v 1.6 1999/11/30 03:49:32 billf Exp $";
40 #endif /* not lint */
41
42 #include "driver.h"
43
44 moveall()               /* move all comp ships */
45 {
46         struct ship *sp, *sq;           /* r11, r10 */
47         int n;                          /* r9 */
48         int k, l;                       /* r8, r7 */
49         int row[NSHIP], col[NSHIP], dir[NSHIP], drift[NSHIP];
50         char moved[NSHIP];
51
52         /*
53          * first try to create moves for OUR ships
54          */
55         foreachship(sp) {
56                 struct ship *closest;
57                 int ma, ta;
58                 char af;
59
60                 if (sp->file->captain[0] || sp->file->dir == 0)
61                         continue;
62                 if (!sp->file->struck && windspeed && !snagged(sp)
63                     && sp->specs->crew3) {
64                         ta = maxturns(sp, &af);
65                         ma = maxmove(sp, sp->file->dir, 0);
66                         closest = closestenemy(sp, 0, 0);
67                         if (closest == 0)
68                                 *sp->file->movebuf = '\0';
69                         else
70                                 closeon(sp, closest, sp->file->movebuf,
71                                         ta, ma, af);
72                 } else
73                         *sp->file->movebuf = '\0';
74         }
75         /*
76          * Then execute the moves for ALL ships (dead ones too),
77          * checking for collisions and snags at each step.
78          * The old positions are saved in row[], col[], dir[].
79          * At the end, we compare and write out the changes.
80          */
81         n = 0;
82         foreachship(sp) {
83                 if (snagged(sp))
84                         (void) strcpy(sp->file->movebuf, "d");
85                 else
86                         if (*sp->file->movebuf != 'd')
87                                 (void) strcat(sp->file->movebuf, "d");
88                 row[n] = sp->file->row;
89                 col[n] = sp->file->col;
90                 dir[n] = sp->file->dir;
91                 drift[n] = sp->file->drift;
92                 moved[n] = 0;
93                 n++;
94         }
95         /*
96          * Now resolve collisions.
97          * This is the tough part.
98          */
99         for (k = 0; stillmoving(k); k++) {
100                 /*
101                  * Step once.
102                  * And propagate the nulls at the end of sp->file->movebuf.
103                  */
104                 n = 0;
105                 foreachship(sp) {
106                         if (!sp->file->movebuf[k])
107                                 sp->file->movebuf[k+1] = '\0';
108                         else if (sp->file->dir)
109                                 step(sp->file->movebuf[k], sp, &moved[n]);
110                         n++;
111                 }
112                 /*
113                  * The real stuff.
114                  */
115                 n = 0;
116                 foreachship(sp) {
117                         if (sp->file->dir == 0 || isolated(sp))
118                                 goto cont1;
119                         l = 0;
120                         foreachship(sq) {
121                                 char snap = 0;
122
123                                 if (sp == sq)
124                                         goto cont2;
125                                 if (sq->file->dir == 0)
126                                         goto cont2;
127                                 if (!push(sp, sq))
128                                         goto cont2;
129                                 if (snagged2(sp, sq) && range(sp, sq) > 1)
130                                         snap++;
131                                 if (!range(sp, sq) && !fouled2(sp, sq)) {
132                                         makesignal(sp,
133                                                 "collision with %s (%c%c)", sq);
134                                         if (die() < 4) {
135                                                 makesignal(sp,
136                                                         "fouled with %s (%c%c)",
137                                                         sq);
138                                                 Write(W_FOUL, sp, 0, l, 0, 0, 0);
139                                                 Write(W_FOUL, sq, 0, n, 0, 0, 0);
140                                         }
141                                         snap++;
142                                 }
143                                 if (snap) {
144                                         sp->file->movebuf[k + 1] = 0;
145                                         sq->file->movebuf[k + 1] = 0;
146                                         sq->file->row = sp->file->row - 1;
147                                         if (sp->file->dir == 1
148                                             || sp->file->dir == 5)
149                                                 sq->file->col =
150                                                         sp->file->col - 1;
151                                         else
152                                                 sq->file->col = sp->file->col;
153                                         sq->file->dir = sp->file->dir;
154                                 }
155                         cont2:
156                                 l++;
157                         }
158                 cont1:
159                         n++;
160                 }
161         }
162         /*
163          * Clear old moves.  And write out new pos.
164          */
165         n = 0;
166         foreachship(sp) {
167                 if (sp->file->dir != 0) {
168                         *sp->file->movebuf = 0;
169                         if (row[n] != sp->file->row)
170                                 Write(W_ROW, sp, 0, sp->file->row, 0, 0, 0);
171                         if (col[n] != sp->file->col)
172                                 Write(W_COL, sp, 0, sp->file->col, 0, 0, 0);
173                         if (dir[n] != sp->file->dir)
174                                 Write(W_DIR, sp, 0, sp->file->dir, 0, 0, 0);
175                         if (drift[n] != sp->file->drift)
176                                 Write(W_DRIFT, sp, 0, sp->file->drift, 0, 0, 0);
177                 }
178                 n++;
179         }
180 }
181
182 stillmoving(k)
183 int k;
184 {
185         struct ship *sp;
186
187         foreachship(sp)
188                 if (sp->file->movebuf[k])
189                         return 1;
190         return 0;
191 }
192
193 isolated(ship)
194 struct ship *ship;
195 {
196         struct ship *sp;
197
198         foreachship(sp) {
199                 if (ship != sp && range(ship, sp) <= 10)
200                         return 0;
201         }
202         return 1;
203 }
204
205 push(from, to)
206 struct ship *from, *to;
207 {
208         int bs, sb;
209
210         sb = to->specs->guns;
211         bs = from->specs->guns;
212         if (sb > bs)
213                 return 1;
214         if (sb < bs)
215                 return 0;
216         return from < to;
217 }
218
219 step(com, sp, moved)
220 char com;
221 struct ship *sp;
222 char *moved;
223 {
224         int dist;
225
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];
254                         }
255                 } else
256                         sp->file->drift = 0;
257                 break;
258         }
259 }
260
261 sendbp(from, to, sections, isdefense)
262 struct ship *from, *to;
263 int sections;
264 char isdefense;
265 {
266         int n;
267         struct BP *bp;
268
269         bp = isdefense ? from->file->DBP : from->file->OBP;
270         for (n = 0; n < NBP && bp[n].turnsent; n++)
271                 ;
272         if (n < NBP && sections) {
273                 Write(isdefense ? W_DBP : W_OBP, from, 0,
274                         n, turn, to->file->index, sections);
275                 if (isdefense)
276                         makesignal(from, "repelling boarders",
277                                 (struct ship *)0);
278                 else
279                         makesignal(from, "boarding the %s (%c%c)", to);
280         }
281 }
282
283 toughmelee(ship, to, isdefense, count)
284 struct ship *ship, *to;
285 int isdefense, count;
286 {
287         struct BP *bp;
288         int obp = 0;
289         int n, OBP = 0, DBP = 0, dbp = 0;
290         int qual;
291
292         qual = ship->specs->qual;
293         bp = isdefense ? ship->file->DBP : ship->file->OBP;
294         for (n = 0; n < NBP; n++, bp++) {
295                 if (bp->turnsent && (to == bp->toship || isdefense)) {
296                         obp += bp->mensent / 100
297                                 ? ship->specs->crew1 * qual : 0;
298                         obp += (bp->mensent % 100)/10
299                                 ? ship->specs->crew2 * qual : 0;
300                         obp += bp->mensent % 10
301                                 ? ship->specs->crew3 * qual : 0;
302                 }
303         }
304         if (count || isdefense)
305                 return obp;
306         OBP = toughmelee(to, ship, 0, count + 1);
307         dbp = toughmelee(ship, to, 1, count + 1);
308         DBP = toughmelee(to, ship, 1, count + 1);
309         if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
310                 return 1;
311         else
312                 return 0;
313 }
314
315 reload()
316 {
317         struct ship *sp;
318
319         foreachship(sp) {
320                 sp->file->loadwith = 0;
321         }
322 }
323
324 checksails()
325 {
326         struct ship *sp;
327         int rig, full;
328         struct ship *close;
329
330         foreachship(sp) {
331                 if (sp->file->captain[0] != 0)
332                         continue;
333                 rig = sp->specs->rig1;
334                 if (windspeed == 6 || windspeed == 5 && sp->specs->class > 4)
335                         rig = 0;
336                 if (rig && sp->specs->crew3) {
337                         close = closestenemy(sp, 0, 0);
338                         if (close != 0) {
339                                 if (range(sp, close) > 9)
340                                         full = 1;
341                                 else
342                                         full = 0;
343                         } else
344                                 full = 0;
345                 } else
346                         full = 0;
347                 if ((sp->file->FS != 0) != full)
348                         Write(W_FS, sp, 0, full, 0, 0, 0);
349         }
350 }