2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
53 static void fltp_to_list(struct fltheadstr *, struct emp_qelem *);
56 cost_ship(struct shpstr *sp, struct fltelemstr *ep, struct fltheadstr *fp)
62 if (sp->shp_effic > 0) {
63 mobcost = sp->shp_effic * sp->shp_speed * 0.01;
64 mobcost = 480.0 / (mobcost * (1 + (50 + sp->shp_tech) /
65 (double)(200 + sp->shp_tech)));
67 /* the next two lines are not necessary since shp_mobquota is unsigned
68 and therefore cannot be less than 0.
69 if (sp->shp_mobquota<0)
75 howfar = (int)sp->shp_mobil - (int)sp->shp_mobquota;
76 howfar = ceil((howfar / mobcost));
82 "Ship #%d can move %d spaces on mobility %d (cost/sect %f)\n",
83 sp->shp_uid, howfar, sp->shp_mobil, mobcost);
85 if ((unsigned int)howfar < fp->maxmoves)
86 fp->maxmoves = howfar;
88 ep->mobil = sp->shp_mobil;
89 ep->mobcost = mobcost;
93 sail_find_fleet(struct fltheadstr **head, struct shpstr *sp)
95 struct fltheadstr *fltp;
97 struct fltelemstr *this;
103 if (sp->shp_own == 0)
108 /* If this ship is following, find the head of the follow list. */
109 for (ap = sp; ap; len++, ap = getshipp(follow)) {
110 follow = ap->shp_follow;
112 if (ap->shp_own != sp->shp_own) {
114 "Ship #%d, following #%d, which you don't own.\n",
115 sp->shp_uid, ap->shp_uid);
118 /* Not a follower. */
119 if (ap->shp_path[0] != 'f')
121 /* Following itself */
122 if (follow == ap->shp_uid || follow == sp->shp_uid)
127 "Ship #%d, following #%d, which you don't own.\n",
128 sp->shp_uid, follow);
132 /* This should prevent infinite loops. */
135 "Ship #%d, too many follows (circular follow?).\n",
140 for (stop = 0, cp = ap->shp_path; !stop && *cp; cp++) {
156 /* we found a non-valid char in the path. */
158 wu(0, ap->shp_own, "invalid char '\\%03o' in path of ship %d\n",
159 (unsigned char)*cp, ap->shp_uid);
163 /* if this ship is not sailing anywhere then ignore it. */
167 /* Find the fleet structure we belong to. */
168 for (fltp = *head; fltp && fltp->leader != follow; fltp = fltp->next) ;
171 fltp = malloc(sizeof(*fltp));
172 memset(fltp, 0, sizeof(*fltp));
178 /* Set the leader. */
179 fltp->leader = ap->shp_uid;
180 fltp->real_q = LEADER_REAL;
183 fltp->own = ap->shp_own;
184 fltp->maxmoves = 500;
187 /* If the fleet is not in the same sector as us, no go. */
188 if ((fltp->x != sp->shp_x) || (fltp->y != sp->shp_y)) {
190 "Ship %d not in same sector as its sailing fleet\n",
192 fltp->real_q = LEADER_WRONGSECT;
196 this = malloc(sizeof(*this));
197 memset(this, 0, sizeof(*this));
198 this->num = sp->shp_uid;
199 this->own = sp->shp_own;
200 this->next = fltp->head;
202 cost_ship(sp, this, fltp);
208 sail_nav_fleet(struct fltheadstr *fltp)
210 struct fltelemstr *fe;
211 struct shpstr *sp, ship;
212 struct sctstr *sectp;
216 struct emp_qelem ship_list;
220 switch (fltp->real_q) {
227 case LEADER_WRONGSECT:
234 "Fleet lead by %d is %s, can go %d spaces\n contains ships:",
235 fltp->leader, s, fltp->maxmoves);
236 for (fe = fltp->head; fe; fe = fe->next)
237 wu(0, fltp->own, " %d", fe->num);
238 wu(0, fltp->own, "\n");
240 sectp = getsectp(fltp->x, fltp->y);
241 for (fe = fltp->head; fe; fe = fe->next) {
242 sp = getshipp(fe->num);
243 if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
245 " ship #%d (%s) is crewless and can't go on\n",
246 fe->num, cname(fe->own));
249 if ((shp_check_nav(sectp, sp) == CN_LANDLOCKED) &&
250 (dchr[sectp->sct_type].d_nav == NAV_CANAL)) {
252 "Your ship #%d (%s) is too big to fit through the canal.\n",
253 fe->num, cname(fe->own));
259 sp = getshipp(fltp->leader);
260 sectp = getsectp(fltp->x, fltp->y);
261 if (shp_check_nav(sectp, sp) != CN_NAVIGABLE)
262 wu(0, fltp->own, "Your fleet lead by %d is trapped by land.\n",
264 sp = getshipp(fltp->leader);
266 fltp_to_list(fltp, &ship_list); /* hack -KHS 1995 */
267 for (s = sp->shp_path; *s && fltp->maxmoves > 0; s++) {
269 if (0 != shp_nav_one_sector(&ship_list, dir, own, 0))
273 shp_put(&ship_list, own);
274 getship(sp->shp_uid, &ship);
275 fltp->x = ship.shp_x;
276 fltp->y = ship.shp_y;
277 for (p = &ship.shp_path[0]; *s; p++, s++)
280 putship(ship.shp_uid, &ship);
282 if (sp->shp_path[0]) {
284 "Fleet lead by #%d nav'd to %s, path left = %s\n",
285 fltp->leader, xyas(fltp->x, fltp->y, fltp->own), &sp->shp_path);
288 "Fleet lead by #%d nav'd to %s, finished.\n",
289 fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
290 wu(0, sp->shp_own, "Ship #%d has %d mobility now.\n",
291 fe->num, (int)fe->mobil);
300 struct fltheadstr *head = 0;
301 struct fltheadstr *fltp;
305 for (n = 0; NULL != (sp = getshipp(n)); n++)
306 if (sp->shp_own == cn) {
307 sail_find_fleet(&head, sp);
310 /* see what the fleets fall out into */
311 for (fltp = head; fltp; fltp = fltp->next) {
312 if (sail_nav_fleet(fltp))
313 wu(0, fltp->own, "Your fleet lead by ship #%d has reached %s.\n",
314 fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
317 /* Free up the memory, 'cause I want to. */
318 for (fltp = head; fltp != 0;) {
319 struct fltelemstr *fe;
320 struct fltheadstr *saveh;
322 for (fe = fltp->head; fe != 0;) {
323 struct fltelemstr *saveel;
333 /* The following is a total hack by Ken Stevens to cut down dramatically on repeated code 1995 */
336 fltp_to_list(struct fltheadstr *fltp, struct emp_qelem *list)
338 struct fltelemstr *fe;
343 for (fe = fltp->head; fe; fe = fe->next) {
344 mlp = malloc(sizeof(struct mlist));
345 sp = getshipp(fe->num);
346 mlp->mcp = mchr + sp->shp_type;
348 mlp->mobil = fe->mobil;
349 emp_insque(&mlp->queue, list);