2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * sail.c: Sail ships during the update
30 * Known contributors to this file:
34 * Steve McClure, 1998-2000
49 double mobil, mobcost;
50 struct fltelemstr *next;
56 /* defines for the real_q member */
57 #define LEADER_VIRTUAL 0
59 #define LEADER_WRONGSECT 2
63 struct fltelemstr *head;
64 struct fltheadstr *next;
67 static void fltp_to_list(struct fltheadstr *, struct emp_qelem *);
70 cost_ship(struct shpstr *sp, struct fltelemstr *ep, struct fltheadstr *fp)
72 double mobcost = shp_mobcost(sp);
77 howfar = (int)sp->shp_mobil - (int)sp->shp_mobquota;
78 howfar = ceil((howfar / mobcost));
84 "Ship #%d can move %d spaces on mobility %d (cost/sect %f)\n",
85 sp->shp_uid, howfar, sp->shp_mobil, mobcost);
87 if ((unsigned)howfar < fp->maxmoves)
88 fp->maxmoves = howfar;
90 ep->mobil = sp->shp_mobil;
91 ep->mobcost = mobcost;
95 sail_find_fleet(struct fltheadstr **head, struct shpstr *sp)
97 struct fltheadstr *fltp;
99 struct fltelemstr *this;
105 if (sp->shp_own == 0)
110 /* If this ship is following, find the head of the follow list. */
111 for (ap = sp; ap; len++, ap = getshipp(follow)) {
112 follow = ap->shp_follow;
114 if (ap->shp_own != sp->shp_own) {
116 "Ship #%d, following #%d, which you don't own.\n",
117 sp->shp_uid, ap->shp_uid);
120 /* Not a follower. */
121 if (ap->shp_path[0] != 'f')
123 /* Following itself */
124 if (follow == ap->shp_uid || follow == sp->shp_uid)
129 "Ship #%d, following #%d, which you don't own.\n",
130 sp->shp_uid, follow);
134 /* This should prevent infinite loops. */
137 "Ship #%d, too many follows (circular follow?).\n",
142 for (stop = 0, cp = ap->shp_path; !stop && *cp; cp++) {
158 /* we found a non-valid char in the path. */
160 wu(0, ap->shp_own, "invalid char '\\%03o' in path of ship %d\n",
161 (unsigned char)*cp, ap->shp_uid);
165 /* if this ship is not sailing anywhere then ignore it. */
169 /* Find the fleet structure we belong to. */
170 for (fltp = *head; fltp && fltp->leader != follow; fltp = fltp->next) ;
173 fltp = malloc(sizeof(*fltp));
174 memset(fltp, 0, sizeof(*fltp));
180 /* Set the leader. */
181 fltp->leader = ap->shp_uid;
182 fltp->real_q = LEADER_REAL;
185 fltp->own = ap->shp_own;
186 fltp->maxmoves = 500;
189 /* If the fleet is not in the same sector as us, no go. */
190 if ((fltp->x != sp->shp_x) || (fltp->y != sp->shp_y)) {
192 "Ship %d not in same sector as its sailing fleet\n",
194 fltp->real_q = LEADER_WRONGSECT;
198 this = malloc(sizeof(*this));
199 memset(this, 0, sizeof(*this));
200 this->num = sp->shp_uid;
201 this->own = sp->shp_own;
202 this->next = fltp->head;
204 cost_ship(sp, this, fltp);
210 sail_nav_fleet(struct fltheadstr *fltp)
212 struct fltelemstr *fe;
213 struct shpstr *sp, ship;
214 struct sctstr *sectp;
218 struct emp_qelem ship_list;
222 switch (fltp->real_q) {
229 case LEADER_WRONGSECT:
236 "Fleet lead by %d is %s, can go %d spaces\n contains ships:",
237 fltp->leader, s, fltp->maxmoves);
238 for (fe = fltp->head; fe; fe = fe->next)
239 wu(0, fltp->own, " %d", fe->num);
240 wu(0, fltp->own, "\n");
242 sectp = getsectp(fltp->x, fltp->y);
243 for (fe = fltp->head; fe; fe = fe->next) {
244 sp = getshipp(fe->num);
245 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
247 " ship #%d (%s) is crewless and can't go on\n",
248 fe->num, cname(fe->own));
251 if ((shp_check_nav(sectp, sp) == CN_LANDLOCKED) &&
252 (dchr[sectp->sct_type].d_nav == NAV_CANAL)) {
254 "Your ship #%d (%s) is too big to fit through the canal.\n",
255 fe->num, cname(fe->own));
261 sp = getshipp(fltp->leader);
262 sectp = getsectp(fltp->x, fltp->y);
263 if (shp_check_nav(sectp, sp) != CN_NAVIGABLE)
264 wu(0, fltp->own, "Your fleet lead by %d is trapped by land.\n",
266 sp = getshipp(fltp->leader);
268 fltp_to_list(fltp, &ship_list); /* hack -KHS 1995 */
269 for (s = sp->shp_path; *s && fltp->maxmoves > 0; s++) {
271 if (0 != shp_nav_one_sector(&ship_list, dir, own, 0))
275 unit_put(&ship_list, own);
276 getship(sp->shp_uid, &ship);
277 fltp->x = ship.shp_x;
278 fltp->y = ship.shp_y;
279 for (p = &ship.shp_path[0]; *s; p++, s++)
282 putship(ship.shp_uid, &ship);
284 if (sp->shp_path[0]) {
286 "Fleet lead by #%d nav'd to %s, path left = %s\n",
287 fltp->leader, xyas(fltp->x, fltp->y, fltp->own), &sp->shp_path);
290 "Fleet lead by #%d nav'd to %s, finished.\n",
291 fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
292 wu(0, sp->shp_own, "Ship #%d has %d mobility now.\n",
293 fe->num, (int)fe->mobil);
302 struct fltheadstr *head = NULL;
303 struct fltheadstr *fltp;
307 for (n = 0; NULL != (sp = getshipp(n)); n++)
308 if (sp->shp_own == cn) {
309 sail_find_fleet(&head, sp);
312 /* see what the fleets fall out into */
313 for (fltp = head; fltp; fltp = fltp->next) {
314 if (sail_nav_fleet(fltp))
315 wu(0, fltp->own, "Your fleet lead by ship #%d has reached %s.\n",
316 fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
319 /* Free up the memory, 'cause I want to. */
320 for (fltp = head; fltp;) {
321 struct fltelemstr *fe;
322 struct fltheadstr *saveh;
324 for (fe = fltp->head; fe;) {
325 struct fltelemstr *saveel;
335 /* The following is a total hack by Ken Stevens to cut down dramatically on repeated code 1995 */
338 fltp_to_list(struct fltheadstr *fltp, struct emp_qelem *list)
340 struct fltelemstr *fe;
345 for (fe = fltp->head; fe; fe = fe->next) {
346 mlp = malloc(sizeof(struct ulist));
347 sp = getshipp(fe->num);
348 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
349 mlp->unit.ship = *sp;
350 ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
351 mlp->mobil = fe->mobil;
352 emp_insque(&mlp->queue, list);