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. 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
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 $
31 * $DragonFly: src/games/sail/dr_3.c,v 1.3 2006/09/03 17:33:13 pavalos Exp $
36 static bool stillmoving(int);
37 static bool isolated(struct ship
*);
38 static bool push(struct ship
*, struct ship
*);
39 static void step(char, struct ship
*, char *);
41 /* move all comp ships */
48 int row
[NSHIP
], col
[NSHIP
], dir
[NSHIP
], drift
[NSHIP
];
52 * first try to create moves for OUR ships
59 if (sp
->file
->captain
[0] || sp
->file
->dir
== 0)
61 if (!sp
->file
->struck
&& windspeed
&& !snagged(sp
)
62 && sp
->specs
->crew3
) {
63 ta
= maxturns(sp
, &af
);
64 ma
= maxmove(sp
, sp
->file
->dir
, 0);
65 closest
= closestenemy(sp
, 0, 0);
67 *sp
->file
->movebuf
= '\0';
69 closeon(sp
, closest
, sp
->file
->movebuf
,
72 *sp
->file
->movebuf
= '\0';
75 * Then execute the moves for ALL ships (dead ones too),
76 * checking for collisions and snags at each step.
77 * The old positions are saved in row[], col[], dir[].
78 * At the end, we compare and write out the changes.
83 strcpy(sp
->file
->movebuf
, "d");
85 if (*sp
->file
->movebuf
!= 'd')
86 strcat(sp
->file
->movebuf
, "d");
87 row
[n
] = sp
->file
->row
;
88 col
[n
] = sp
->file
->col
;
89 dir
[n
] = sp
->file
->dir
;
90 drift
[n
] = sp
->file
->drift
;
95 * Now resolve collisions.
96 * This is the tough part.
98 for (k
= 0; stillmoving(k
); k
++) {
101 * And propagate the nulls at the end of sp->file->movebuf.
105 if (!sp
->file
->movebuf
[k
])
106 sp
->file
->movebuf
[k
+1] = '\0';
107 else if (sp
->file
->dir
)
108 step(sp
->file
->movebuf
[k
], sp
, &moved
[n
]);
116 if (sp
->file
->dir
== 0 || isolated(sp
))
124 if (sq
->file
->dir
== 0)
128 if (snagged2(sp
, sq
) && range(sp
, sq
) > 1)
130 if (!range(sp
, sq
) && !fouled2(sp
, sq
)) {
132 "collision with %s (%c%c)", sq
);
135 "fouled with %s (%c%c)",
137 Write(W_FOUL
, sp
, l
, 0, 0, 0);
138 Write(W_FOUL
, sq
, n
, 0, 0, 0);
143 sp
->file
->movebuf
[k
+ 1] = 0;
144 sq
->file
->movebuf
[k
+ 1] = 0;
145 sq
->file
->row
= sp
->file
->row
- 1;
146 if (sp
->file
->dir
== 1
147 || sp
->file
->dir
== 5)
151 sq
->file
->col
= sp
->file
->col
;
152 sq
->file
->dir
= sp
->file
->dir
;
162 * Clear old moves. And write out new pos.
166 if (sp
->file
->dir
!= 0) {
167 *sp
->file
->movebuf
= 0;
168 if (row
[n
] != sp
->file
->row
)
169 Write(W_ROW
, sp
, sp
->file
->row
, 0, 0, 0);
170 if (col
[n
] != sp
->file
->col
)
171 Write(W_COL
, sp
, sp
->file
->col
, 0, 0, 0);
172 if (dir
[n
] != sp
->file
->dir
)
173 Write(W_DIR
, sp
, sp
->file
->dir
, 0, 0, 0);
174 if (drift
[n
] != sp
->file
->drift
)
175 Write(W_DRIFT
, sp
, sp
->file
->drift
, 0, 0, 0);
187 if (sp
->file
->movebuf
[k
])
193 isolated(struct ship
*ship
)
198 if (ship
!= sp
&& range(ship
, sp
) <= 10)
205 push(struct ship
*from
, struct ship
*to
)
209 sb
= to
->specs
->guns
;
210 bs
= from
->specs
->guns
;
219 step(char com
, struct ship
*sp
, char *moved
)
225 if (++sp
->file
->dir
== 9)
229 if (--sp
->file
->dir
== 0)
232 case '0': case '1': case '2': case '3':
233 case '4': case '5': case '6': case '7':
234 if (sp
->file
->dir
% 2 == 0)
235 dist
= dtab
[com
- '0'];
238 sp
->file
->row
-= dr
[sp
->file
->dir
] * dist
;
239 sp
->file
->col
-= dc
[sp
->file
->dir
] * dist
;
246 if (windspeed
!= 0 && ++sp
->file
->drift
> 2 &&
247 ((sp
->specs
->class >= 3 && !snagged(sp
))
248 || (turn
& 1) == 0)) {
249 sp
->file
->row
-= dr
[winddir
];
250 sp
->file
->col
-= dc
[winddir
];
260 sendbp(struct ship
*from
, struct ship
*to
, int sections
, char isdefense
)
265 bp
= isdefense
? from
->file
->DBP
: from
->file
->OBP
;
266 for (n
= 0; n
< NBP
&& bp
[n
].turnsent
; n
++)
268 if (n
< NBP
&& sections
) {
269 Write(isdefense
? W_DBP
: W_OBP
, from
,
270 n
, turn
, to
->file
->index
, sections
);
272 makesignal(from
, "repelling boarders", NULL
);
274 makesignal(from
, "boarding the %s (%c%c)", to
);
279 toughmelee(struct ship
*ship
, struct ship
*to
, int isdefense
, int count
)
283 int n
, OBP
= 0, DBP
= 0, dbp
= 0;
286 qual
= ship
->specs
->qual
;
287 bp
= isdefense
? ship
->file
->DBP
: ship
->file
->OBP
;
288 for (n
= 0; n
< NBP
; n
++, bp
++) {
289 if (bp
->turnsent
&& (to
== bp
->toship
|| isdefense
)) {
290 obp
+= bp
->mensent
/ 100
291 ? ship
->specs
->crew1
* qual
: 0;
292 obp
+= (bp
->mensent
% 100)/10
293 ? ship
->specs
->crew2
* qual
: 0;
294 obp
+= bp
->mensent
% 10
295 ? ship
->specs
->crew3
* qual
: 0;
298 if (count
|| isdefense
)
300 OBP
= toughmelee(to
, ship
, 0, count
+ 1);
301 dbp
= toughmelee(ship
, to
, 1, count
+ 1);
302 DBP
= toughmelee(to
, ship
, 1, count
+ 1);
303 if (OBP
> obp
+ 10 || OBP
+ DBP
>= obp
+ dbp
+ 10)
315 sp
->file
->loadwith
= 0;
324 struct ship
*closest
;
327 if (sp
->file
->captain
[0] != 0)
329 rig
= sp
->specs
->rig1
;
330 if (windspeed
== 6 || (windspeed
== 5 && sp
->specs
->class > 4))
332 if (rig
&& sp
->specs
->crew3
) {
333 closest
= closestenemy(sp
, 0, 0);
335 if (range(sp
, closest
) > 9)
343 if ((sp
->file
->FS
!= 0) != full
)
344 Write(W_FS
, sp
, full
, 0, 0, 0);