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