Generally use NULL instead of explicitly casting 0 to some pointer type.
[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  * @(#)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 $
36  */
37
38 #include "driver.h"
39
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 *);
44
45 void
46 moveall(void)           /* move all comp ships */
47 {
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];
53
54         /*
55          * first try to create moves for OUR ships
56          */
57         foreachship(sp) {
58                 struct ship *closest;
59                 int ma, ta;
60                 char af;
61
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';
76         }
77         /*
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.
82          */
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++;
96         }
97         /*
98          * Now resolve collisions.
99          * This is the tough part.
100          */
101         for (k = 0; stillmoving(k); k++) {
102                 /*
103                  * Step once.
104                  * And propagate the nulls at the end of sp->file->movebuf.
105                  */
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++;
113                 }
114                 /*
115                  * The real stuff.
116                  */
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;
124
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, l, 0, 0, 0);
141                                                 Write(W_FOUL, sq, n, 0, 0, 0);
142                                         }
143                                         snap++;
144                                 }
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;
156                                 }
157                         cont2:
158                                 l++;
159                         }
160                 cont1:
161                         n++;
162                 }
163         }
164         /*
165          * Clear old moves.  And write out new pos.
166          */
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, sp->file->row, 0, 0, 0);
173                         if (col[n] != sp->file->col)
174                                 Write(W_COL, sp, sp->file->col, 0, 0, 0);
175                         if (dir[n] != sp->file->dir)
176                                 Write(W_DIR, sp, sp->file->dir, 0, 0, 0);
177                         if (drift[n] != sp->file->drift)
178                                 Write(W_DRIFT, sp, sp->file->drift, 0, 0, 0);
179                 }
180                 n++;
181         }
182 }
183
184 static bool
185 stillmoving(int k)
186 {
187         struct ship *sp;
188
189         foreachship(sp)
190                 if (sp->file->movebuf[k])
191                         return 1;
192         return 0;
193 }
194
195 static bool
196 isolated(struct ship *ship)
197 {
198         struct ship *sp;
199
200         foreachship(sp) {
201                 if (ship != sp && range(ship, sp) <= 10)
202                         return 0;
203         }
204         return 1;
205 }
206
207 static bool
208 push(struct ship *from, struct ship *to)
209 {
210         int bs, sb;
211
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;
219 }
220
221 static void
222 step(char com, struct ship *sp, 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 void
262 sendbp(struct ship *from, struct ship *to, int sections, char isdefense)
263 {
264         int n;
265         struct BP *bp;
266
267         bp = isdefense ? from->file->DBP : from->file->OBP;
268         for (n = 0; n < NBP && bp[n].turnsent; n++)
269                 ;
270         if (n < NBP && sections) {
271                 Write(isdefense ? W_DBP : W_OBP, from,
272                         n, turn, to->file->index, sections);
273                 if (isdefense)
274                         makesignal(from, "repelling boarders", NULL);
275                 else
276                         makesignal(from, "boarding the %s (%c%c)", to);
277         }
278 }
279
280 int
281 toughmelee(struct ship *ship, struct ship *to, int isdefense, int count)
282 {
283         struct BP *bp;
284         int obp = 0;
285         int n, OBP = 0, DBP = 0, dbp = 0;
286         int qual;
287
288         qual = ship->specs->qual;
289         bp = isdefense ? ship->file->DBP : ship->file->OBP;
290         for (n = 0; n < NBP; n++, bp++) {
291                 if (bp->turnsent && (to == bp->toship || isdefense)) {
292                         obp += bp->mensent / 100
293                                 ? ship->specs->crew1 * qual : 0;
294                         obp += (bp->mensent % 100)/10
295                                 ? ship->specs->crew2 * qual : 0;
296                         obp += bp->mensent % 10
297                                 ? ship->specs->crew3 * qual : 0;
298                 }
299         }
300         if (count || isdefense)
301                 return obp;
302         OBP = toughmelee(to, ship, 0, count + 1);
303         dbp = toughmelee(ship, to, 1, count + 1);
304         DBP = toughmelee(to, ship, 1, count + 1);
305         if (OBP > obp + 10 || OBP + DBP >= obp + dbp + 10)
306                 return 1;
307         else
308                 return 0;
309 }
310
311 void
312 reload(void)
313 {
314         struct ship *sp;
315
316         foreachship(sp) {
317                 sp->file->loadwith = 0;
318         }
319 }
320
321 void
322 checksails(void)
323 {
324         struct ship *sp;
325         int rig, full;
326         struct ship *closest;
327
328         foreachship(sp) {
329                 if (sp->file->captain[0] != 0)
330                         continue;
331                 rig = sp->specs->rig1;
332                 if (windspeed == 6 || (windspeed == 5 && sp->specs->class > 4))
333                         rig = 0;
334                 if (rig && sp->specs->crew3) {
335                         closest = closestenemy(sp, 0, 0);
336                         if (closest != 0) {
337                                 if (range(sp, closest) > 9)
338                                         full = 1;
339                                 else
340                                         full = 0;
341                         } else
342                                 full = 0;
343                 } else
344                         full = 0;
345                 if ((sp->file->FS != 0) != full)
346                         Write(W_FS, sp, full, 0, 0, 0);
347         }
348 }