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
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
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 $
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 *);
46 moveall(void) /* move all comp ships */
48 struct ship
*sp
, *sq
; /* r11, r10 */
50 int k
, l
; /* r8, r7 */
51 int row
[NSHIP
], col
[NSHIP
], dir
[NSHIP
], drift
[NSHIP
];
55 * first try to create moves for OUR ships
62 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
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);
70 *sp
->file
->movebuf
= '\0';
72 closeon(sp
, closest
, sp
->file
->movebuf
,
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.
86 strcpy(sp
->file
->movebuf
, "d");
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
;
98 * Now resolve collisions.
99 * This is the tough part.
101 for (k
= 0; stillmoving(k
); k
++) {
104 * And propagate the nulls at the end of sp->file->movebuf.
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
]);
119 if (sp
->file
->dir
== 0 || isolated(sp
))
127 if (sq
->file
->dir
== 0)
131 if (snagged2(sp
, sq
) && range(sp
, sq
) > 1)
133 if (!range(sp
, sq
) && !fouled2(sp
, sq
)) {
135 "collision with %s (%c%c)", sq
);
138 "fouled with %s (%c%c)",
140 Write(W_FOUL
, sp
, l
, 0, 0, 0);
141 Write(W_FOUL
, sq
, n
, 0, 0, 0);
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)
154 sq
->file
->col
= sp
->file
->col
;
155 sq
->file
->dir
= sp
->file
->dir
;
165 * Clear old moves. And write out new pos.
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);
190 if (sp
->file
->movebuf
[k
])
196 isolated(struct ship
*ship
)
201 if (ship
!= sp
&& range(ship
, sp
) <= 10)
208 push(struct ship
*from
, struct ship
*to
)
212 sb
= to
->specs
->guns
;
213 bs
= from
->specs
->guns
;
222 step(char com
, struct ship
*sp
, char *moved
)
228 if (++sp
->file
->dir
== 9)
232 if (--sp
->file
->dir
== 0)
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'];
241 sp
->file
->row
-= dr
[sp
->file
->dir
] * dist
;
242 sp
->file
->col
-= dc
[sp
->file
->dir
] * dist
;
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
];
262 sendbp(struct ship
*from
, struct ship
*to
, int sections
, char isdefense
)
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
,
272 n
, turn
, to
->file
->index
, sections
);
274 makesignal(from
, "repelling boarders", NULL
);
276 makesignal(from
, "boarding the %s (%c%c)", to
);
281 toughmelee(struct ship
*ship
, struct ship
*to
, int isdefense
, int count
)
285 int n
, OBP
= 0, DBP
= 0, dbp
= 0;
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;
300 if (count
|| isdefense
)
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)
317 sp
->file
->loadwith
= 0;
326 struct ship
*closest
;
329 if (sp
->file
->captain
[0] != 0)
331 rig
= sp
->specs
->rig1
;
332 if (windspeed
== 6 || (windspeed
== 5 && sp
->specs
->class > 4))
334 if (rig
&& sp
->specs
->crew3
) {
335 closest
= closestenemy(sp
, 0, 0);
337 if (range(sp
, closest
) > 9)
345 if ((sp
->file
->FS
!= 0) != full
)
346 Write(W_FS
, sp
, full
, 0, 0, 0);