]> git.pond.sub.org Git - empserver/blob - src/lib/update/sail.c
Update copyright notice.
[empserver] / src / lib / update / sail.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sail.c: Sail ships during the update
29  * 
30  *  Known contributors to this file:
31  *     Doug Hay
32  *     Robert Forsman
33  *     Ken Stevens, 1995
34  *     Steve McClure, 1998-2000
35  */
36
37 #include "misc.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "path.h"
41 #include "ship.h"
42 #include "var.h"
43 #include "news.h"
44 #include "file.h"
45 #include "nat.h"
46 #include "xy.h"
47 #include "nsc.h"
48 #include "update.h"
49 #include "subs.h"
50 #include "common.h"
51 #include <math.h>
52 #include <stdlib.h>
53
54 static void fltp_to_list(struct fltheadstr *, struct emp_qelem *);
55
56 static void
57 cost_ship(struct shpstr *sp, struct fltelemstr *ep, struct fltheadstr *fp)
58 {
59     double mobcost;
60     int howfar;
61
62     mobcost = 0.0;
63     if (sp->shp_effic > 0) {
64         mobcost = sp->shp_effic * sp->shp_speed * 0.01;
65         mobcost = 480.0 / (mobcost * (1 + (50 + sp->shp_tech) /
66                                       (double)(200 + sp->shp_tech)));
67     }
68 /* the next two lines are not necessary since shp_mobquota is unsigned
69 and therefore cannot be less than 0. 
70         if (sp->shp_mobquota<0)
71                 sp->shp_mobquota=0;
72 */
73
74     howfar = 0;
75     if (mobcost > 0) {
76         howfar = (int)sp->shp_mobil - (int)sp->shp_mobquota;
77         howfar = ceil((howfar / mobcost));
78     }
79     if (howfar < 0)
80         howfar = 0;
81 #ifdef SAILDEBUG
82     wu(0, fp->own,
83        "Ship #%d can move %d spaces on mobility %d (cost/sect %f)\n",
84        sp->shp_uid, howfar, sp->shp_mobil, mobcost);
85 #endif
86     if ((unsigned int)howfar < fp->maxmoves)
87         fp->maxmoves = howfar;
88
89     ep->mobil = sp->shp_mobil;
90     ep->mobcost = mobcost;
91 }
92
93 static int
94 sail_find_fleet(struct fltheadstr **head, struct shpstr *sp)
95 {
96     struct fltheadstr *fltp;
97     struct shpstr *ap;
98     struct fltelemstr *this;
99     int len = 0;
100     int follow = -1;
101     int stop;
102     s_char *cp;
103
104     if (sp->shp_own == 0)
105         return (0);
106
107
108
109     /* If this ship is following, find the head of the follow list. */
110     for (ap = sp; ap; len++, ap = getshipp(follow)) {
111         follow = ap->shp_follow;
112         /* Not same owner */
113         if (ap->shp_own != sp->shp_own) {
114             wu(0, sp->shp_own,
115                "Ship #%d, following #%d, which you don't own.\n",
116                sp->shp_uid, ap->shp_uid);
117             return (0);
118         }
119         /* Not a follower. */
120         if (ap->shp_path[0] != 'f')
121             break;
122         /* Following itself */
123         if (follow == ap->shp_uid || follow == sp->shp_uid)
124             break;
125     }
126     if (!ap) {
127         wu(0, sp->shp_own,
128            "Ship #%d, following #%d, which you don't own.\n",
129            sp->shp_uid, follow);
130         return (0);
131     }
132
133     /* This should prevent infinite loops. */
134     if (len >= 10) {
135         wu(0, sp->shp_own,
136            "Ship #%d, too many follows (circular follow?).\n",
137            sp->shp_uid);
138         return (0);
139     }
140
141     for (stop = 0, cp = ap->shp_path; (!stop) && (*cp); cp++) {
142         switch (*cp) {
143         case 'y':
144         case 'u':
145         case 'g':
146         case 'j':
147         case 'b':
148         case 'n':
149         case 'h':
150         case 't':
151             break;
152         default:
153             stop = 1;
154         }
155     }
156
157     /* we found a non-valid char in the path. */
158     if (*cp) {
159         wu(0, ap->shp_own, "invalid char '\\%03o' in path of ship %d\n",
160            (unsigned char)*cp, ap->shp_uid);
161         *cp = 0;
162     }
163
164     /* if this ship is not sailing anywhere then ignore it. */
165     if (!*ap->shp_path)
166         return (0);
167
168     /* Find the fleet structure we belong to. */
169     for (fltp = (*head); (fltp && fltp->leader != follow);
170          fltp = fltp->next) ;
171
172     if (!fltp) {
173         fltp = (struct fltheadstr *)malloc(sizeof(*fltp));
174         memset(fltp, 0, sizeof(*fltp));
175
176         /* Fix the links. */
177         fltp->next = (*head);
178         *head = fltp;
179
180         /* Set the leader. */
181         fltp->leader = ap->shp_uid;
182         fltp->real_q = LEADER_REAL;
183         fltp->x = ap->shp_x;
184         fltp->y = ap->shp_y;
185         fltp->own = ap->shp_own;
186         fltp->maxmoves = 500;
187     }
188
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)) {
191         wu(0, sp->shp_own,
192            "Ship %d not in same sector as its sailing fleet\n",
193            sp->shp_uid);
194         fltp->real_q = LEADER_WRONGSECT;
195         return (0);
196     }
197
198     this = (struct fltelemstr *)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;
203     fltp->head = this;
204     cost_ship(sp, this, fltp);
205
206     return 1;
207 }
208
209 static int
210 sail_nav_fleet(struct fltheadstr *fltp)
211 {
212     struct fltelemstr *fe;
213     struct shpstr *sp, ship;
214     struct sctstr *sectp;
215     int error = 0;
216     s_char *s, *p;
217     natid own;
218     struct emp_qelem ship_list;
219     int dir;
220
221 #ifdef SAILDEBUG
222     switch (fltp->real_q) {
223     case LEADER_VIRTUAL:
224         s = "leaderless";
225         break;
226     case LEADER_REAL:
227         s = "real";
228         break;
229     case LEADER_WRONGSECT:
230         s = "scattered";
231         break;
232     default:
233         s = "inconsistent";
234     }
235     wu(0, fltp->own,
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");
241 #endif
242     sectp = getsectp(fltp->x, fltp->y);
243     switch (check_nav(sectp)) {
244     case CN_NAVIGABLE:
245         break;
246     case CN_CONSTRUCTION:
247     case CN_LANDLOCKED:
248     default:
249         wu(0, fltp->own, "Your fleet lead by %d is trapped by land.\n",
250            fltp->leader);
251         return (0);
252     }
253     for (fe = fltp->head; fe; fe = fe->next) {
254         sp = getshipp(fe->num);
255         if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
256             wu(0, fltp->own,
257                "   ship #%d (%s) is crewless and can't go on\n",
258                fe->num, cname(fe->own));
259             error = 1;
260         }
261     }
262     if (error)
263         return (0);
264     sp = getshipp(fltp->leader);
265     own = sp->shp_own;
266     fltp_to_list(fltp, &ship_list);     /* hack -KHS 1995 */
267     for (s = sp->shp_path; (*s) && (fltp->maxmoves > 0); s++) {
268         dir = chkdir(*s, DIR_STOP, DIR_LAST);
269         if (0 != (error = shp_nav_one_sector(&ship_list, dir, own, 0)))
270             fltp->maxmoves = 1;
271         --(fltp->maxmoves);
272     }
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++)
278         *p = *s;
279     *p = 0;
280     putship(ship.shp_uid, &ship);
281 #ifdef SAILDEBUG
282     if (sp->shp_path[0]) {
283         wu(0, fltp->own,
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);
286     } else
287         wu(0, fltp->own,
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);
292 #endif
293     return 1;
294 }
295
296 void
297 sail_ship(natid cn)
298 {
299     struct shpstr *sp;
300     struct fltheadstr *head = 0;
301     struct fltheadstr *fltp;
302     int n;
303
304
305     for (n = 0; NULL != (sp = getshipp(n)); n++)
306         if (sp->shp_own == cn) {
307             sail_find_fleet(&head, sp);
308         }
309
310     /* see what the fleets fall out into */
311     for (fltp = head; fltp; fltp = fltp->next) {
312         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));
315     }
316
317     /* Free up the memory, 'cause I want to. */
318     for (fltp = head; fltp != 0;) {
319         struct fltelemstr *fe;
320         struct fltheadstr *saveh;
321         saveh = fltp->next;
322         for (fe = fltp->head; fe != 0;) {
323             struct fltelemstr *saveel;
324             saveel = fe->next;
325             free(fe);
326             fe = saveel;
327         }
328         free(fltp);
329         fltp = saveh;
330     }
331 }
332
333 /* The following is a total hack by Ken Stevens to cut down dramatically on repeated code 1995 */
334
335 static void
336 fltp_to_list(struct fltheadstr *fltp, struct emp_qelem *list)
337 {
338     struct fltelemstr *fe;
339     struct mlist *mlp;
340     struct shpstr *sp;
341
342     emp_initque(list);
343     for (fe = fltp->head; fe; fe = fe->next) {
344         mlp = (struct mlist *)malloc(sizeof(struct mlist));
345         sp = getshipp(fe->num);
346         mlp->mcp = mchr + sp->shp_type;
347         mlp->ship = *sp;
348         mlp->mobil = fe->mobil;
349         emp_insque(&mlp->queue, list);
350     }
351 }