]> git.pond.sub.org Git - empserver/blob - src/lib/update/sail.c
(fuel, load, shp_check_nav, retreat_ship1, shp_nav_one_sector)
[empserver] / src / lib / update / sail.c
1 /*
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
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 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.
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     for (fe = fltp->head; fe; fe = fe->next) {
243         sp = getshipp(fe->num);
244         if (sp->shp_item[I_MILIT] == 0 && sp->shp_item[I_CIVIL] == 0) {
245             wu(0, fltp->own,
246                "   ship #%d (%s) is crewless and can't go on\n",
247                fe->num, cname(fe->own));
248             error = 1;
249         }
250         if ((shp_check_nav(sectp, sp) == CN_LANDLOCKED) &&
251             (dchr[sectp->sct_type].d_nav == NAV_CANAL)) {
252             wu(0, fltp->own,
253                "Your ship #%d (%s) is too big to fit through the canal.\n",
254                fe->num, cname(fe->own));
255             error = 1;
256         }
257     }
258     if (error)
259         return 0;
260     sp = getshipp(fltp->leader);
261     sectp = getsectp(fltp->x, fltp->y);
262     if (shp_check_nav(sectp, sp) != CN_NAVIGABLE)
263         wu(0, fltp->own, "Your fleet lead by %d is trapped by land.\n",
264            fltp->leader);
265     sp = getshipp(fltp->leader);
266     own = sp->shp_own;
267     fltp_to_list(fltp, &ship_list);     /* hack -KHS 1995 */
268     for (s = sp->shp_path; (*s) && (fltp->maxmoves > 0); s++) {
269         dir = diridx(*s);
270         if (0 != shp_nav_one_sector(&ship_list, dir, own, 0))
271             fltp->maxmoves = 1;
272         --fltp->maxmoves;
273     }
274     shp_put(&ship_list, own);
275     getship(sp->shp_uid, &ship);
276     fltp->x = ship.shp_x;
277     fltp->y = ship.shp_y;
278     for (p = &ship.shp_path[0]; *s; p++, s++)
279         *p = *s;
280     *p = 0;
281     putship(ship.shp_uid, &ship);
282 #ifdef SAILDEBUG
283     if (sp->shp_path[0]) {
284         wu(0, fltp->own,
285            "Fleet lead by #%d nav'd to %s, path left = %s\n",
286            fltp->leader, xyas(fltp->x, fltp->y, fltp->own), &sp->shp_path);
287     } else
288         wu(0, fltp->own,
289            "Fleet lead by #%d nav'd to %s, finished.\n",
290            fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
291     wu(0, sp->shp_own, "Ship #%d has %d mobility now.\n",
292        fe->num, (int)fe->mobil);
293 #endif
294     return 1;
295 }
296
297 void
298 sail_ship(natid cn)
299 {
300     struct shpstr *sp;
301     struct fltheadstr *head = 0;
302     struct fltheadstr *fltp;
303     int n;
304
305
306     for (n = 0; NULL != (sp = getshipp(n)); n++)
307         if (sp->shp_own == cn) {
308             sail_find_fleet(&head, sp);
309         }
310
311     /* see what the fleets fall out into */
312     for (fltp = head; fltp; fltp = fltp->next) {
313         if (sail_nav_fleet(fltp))
314             wu(0, fltp->own, "Your fleet lead by ship #%d has reached %s.\n",
315                fltp->leader, xyas(fltp->x, fltp->y, fltp->own));
316     }
317
318     /* Free up the memory, 'cause I want to. */
319     for (fltp = head; fltp != 0;) {
320         struct fltelemstr *fe;
321         struct fltheadstr *saveh;
322         saveh = fltp->next;
323         for (fe = fltp->head; fe != 0;) {
324             struct fltelemstr *saveel;
325             saveel = fe->next;
326             free(fe);
327             fe = saveel;
328         }
329         free(fltp);
330         fltp = saveh;
331     }
332 }
333
334 /* The following is a total hack by Ken Stevens to cut down dramatically on repeated code 1995 */
335
336 static void
337 fltp_to_list(struct fltheadstr *fltp, struct emp_qelem *list)
338 {
339     struct fltelemstr *fe;
340     struct mlist *mlp;
341     struct shpstr *sp;
342
343     emp_initque(list);
344     for (fe = fltp->head; fe; fe = fe->next) {
345         mlp = malloc(sizeof(struct mlist));
346         sp = getshipp(fe->num);
347         mlp->mcp = mchr + sp->shp_type;
348         mlp->ship = *sp;
349         mlp->mobil = fe->mobil;
350         emp_insque(&mlp->queue, list);
351     }
352 }