]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_ship.c
5b0c7017d7b33166547f4024791440ac0fecc1ff
[empserver] / src / lib / update / nav_ship.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  *  nav_ship.c: Navigate ships and such
29  * 
30  *  Known contributors to this file:
31  *     Chad Zabel, 1994
32  *     Ken Stevens, 1995
33  */
34
35 #include "misc.h"
36
37 #include <ctype.h>
38 #include "ship.h"
39 #include "sect.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "nat.h"
43 #include "path.h"
44 #include "file.h"
45 #include "item.h"
46 #include "optlist.h"
47 #include "player.h"
48 #include "update.h"
49 #include "subs.h"
50 #include "common.h"
51 #include <stdlib.h>
52
53 static void swap(struct shpstr *);
54
55 static void
56 scuttle_it(struct shpstr *sp)
57 {
58     struct sctstr *sectp;
59
60     sp->shp_autonav &= ~AN_SCUTTLE;
61     if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
62         wu(0, 0, "bad sector (%d,%d) ship %d\n", sp->shp_x, sp->shp_y,
63            sp->shp_uid);
64         return;
65     }
66     if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
67         wu(0, sp->shp_own,
68            "%s is not in a harbor at least 2%% eff!  Not scuttling.\n",
69            prship(sp));
70         return;
71     }
72     if (opt_TRADESHIPS) {
73         if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
74             wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
75             return;
76         }
77     }
78     wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
79        prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
80     if (opt_TRADESHIPS) {
81         scuttle_tradeship(sp, 0);
82     }
83     scuttle_ship(sp);
84 }
85
86 static void
87 nav_check_atdest(struct shpstr *sp)
88 {
89     if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
90         if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
91             (sp->shp_desty[0] == sp->shp_desty[1])) {
92
93             /* End of road */
94
95             sp->shp_autonav &= ~AN_AUTONAV;
96             wu(0, sp->shp_own, "%s arrived at %s, finished\n",
97                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
98             if (sp->shp_autonav & AN_SCUTTLE) {
99                 scuttle_it(sp);
100             }
101         } else {
102             /* unload all cargo */
103             unload_it(sp);
104             wu(0, sp->shp_own, "%s arrived at %s\n",
105                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
106             /* Swap */
107             swap(sp);
108         }
109     } else
110         sp->shp_autonav &= ~AN_LOADING;
111 }
112
113 /* flip the 2 fields that deal with autonav movement. */
114 /* CZ 6/1/94                                          */
115
116 static void
117 swap(struct shpstr *sp)
118 {
119     coord tcord;
120     i_type tcomm[TMAX];
121     short lev[TMAX];
122     int i;
123
124     tcord = sp->shp_destx[0];
125     sp->shp_destx[0] = sp->shp_destx[1];
126     sp->shp_destx[1] = tcord;
127     tcord = sp->shp_desty[0];
128     sp->shp_desty[0] = sp->shp_desty[1];
129     sp->shp_desty[1] = tcord;
130
131     for (i = 0; i < TMAX; ++i) {
132         lev[i] = sp->shp_lstart[i];
133         tcomm[i] = sp->shp_tstart[i];
134     }
135
136     for (i = 0; i < TMAX; ++i) {
137         sp->shp_lstart[i] = sp->shp_lend[i];
138         sp->shp_tstart[i] = sp->shp_tend[i];
139     }
140
141     for (i = 0; i < TMAX; ++i) {
142         sp->shp_lend[i] = lev[i];
143         sp->shp_tend[i] = tcomm[i];
144     }
145
146     /* set load bit */
147     sp->shp_autonav |= AN_LOADING;
148 }
149
150 /*  New Autonav code.
151  *  Chad Zabel
152  *  6-1-94
153  */
154
155 static int
156 nav_loadship(struct shpstr *sp, natid cnum)
157 {
158     struct sctstr *sectp;
159     int i, landown, shipown, didsomething[TMAX], rel;
160
161     for (i = 0; i < TMAX; i++)
162         didsomething[i] = 0;
163
164     /* Turn off the loading flag.
165      * if any of the loads fail on the ship
166      * it will be turned back on.
167      */
168
169     sp->shp_autonav &= ~AN_LOADING;
170
171     if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
172         return RET_SYS;         /* safety */
173     /* I suspect RET_SYS isn't really what you want here --dfp */
174
175
176     landown = sectp->sct_own;
177     shipown = sp->shp_own;
178     rel = getrel(getnatp(sectp->sct_own), cnum);
179
180     /* loop through each field for that ship */
181     for (i = 0; i < TMAX; ++i) {
182         /* check and see if the data fields have been set. */
183
184         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
185             /* nothing to do move on. */
186             didsomething[i] = 1;
187             continue;
188         }
189         if (landown == 0) {
190             /* either sea or deity harbor */
191             didsomething[i] = 1;
192             continue;
193         }
194         if (sectp->sct_type != SCT_HARBR &&
195             (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
196             /* we can only load in harbors */
197             didsomething[i] = 1;
198             continue;
199         }
200         if (landown == shipown || rel >= FRIENDLY)
201             didsomething[i] = load_it(sp, sectp, i);
202     }
203
204     /* check for any unsucessful loads */
205     /* if we have any return 0 to stop */
206     /* the nav_ship loop.              */
207
208     for (i = 0; i < TMAX; i++) {
209         if (didsomething[i] == 0)
210             return 0;
211     }
212     /* All loads were succesful */
213     return 1;
214 }
215
216 /* new autonav code.
217  * 
218  * 1. Try and move to the next sector/harbor given by the player.
219  * 2. Once we reach a harbor try and load all cargo holds for that ship.
220  * 3. If the ship reaches its max levels set by the player try to use
221  *    up all mobility getting to the next harbor.
222  * Continue to loop until the ship runs out of mobility, a load fails,
223  * the ship gets sunk (forts,ect..), the ship hits a mine.
224  *
225  * A check has been added for fuel so ships don't end up running
226  * out of mobility in the ocean.
227  *
228  * Questions, bugs (fixes) , or new ideas should be directed at
229  * Chad Zabel.  
230  * 6-1-94   
231  * Modified to use shp_nav by Ken Stevens 1995
232  */
233 int
234 nav_ship(struct shpstr *sp)
235 {
236     struct sctstr *sectp;
237     char *cp;
238     int stopping;
239     int quit;
240     int didsomething = 0;
241     int max_amt, food_amt;
242     char buf[1024];
243     struct emp_qelem ship_list;
244     struct emp_qelem *qp, *newqp;
245     struct mlist *mlp;
246     int dummyint;
247     double dummydouble;
248     int dir;
249     natid cnum;
250     struct mchrstr *mcp, *vship;
251
252     /* just return if no autonaving to do for this ship */
253     if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
254         return RET_OK;
255
256     cnum = sp->shp_own;
257     vship = mcp = &mchr[(int)sp->shp_type];
258
259     /* Make a list of one ships so we can use the navi.c code */
260     emp_initque(&ship_list);
261     mlp = malloc(sizeof(struct mlist));
262     mlp->mcp = mchr + sp->shp_type;
263     mlp->ship = *sp;
264     mlp->mobil = (double)sp->shp_mobil;
265     emp_insque(&mlp->queue, &ship_list);
266
267     quit = 1;                   /* setup loop, we want to check it 1 time. */
268
269     do {
270         if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
271             (!(sp->shp_autonav & AN_STANDBY))) {
272             shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
273                     sp->shp_own);
274             if (QEMPTY(&ship_list))
275                 return RET_OK;
276             /* before we move check to see if ship needs fuel. */
277             sectp = getsectp(sp->shp_x, sp->shp_y);
278             if (opt_FUEL &&
279                 sectp->sct_own != 0 &&
280                 sp->shp_fuel <= 0 && mlp->mcp->m_fuelu != 0)
281                 auto_fuel_ship(sp);
282             mlp->ship.shp_fuel = sp->shp_fuel;
283
284             cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
285                               sp->shp_destx[0], sp->shp_desty[0],
286                               sp->shp_own);
287             if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
288                 wu(0, cnum,
289                    "%s bad path, ship put on standby\n", prship(sp));
290                 sp->shp_autonav |= AN_STANDBY;
291                 putship(sp->shp_uid, sp);
292
293                 /* We need to free the ship list */
294                 qp = ship_list.q_forw;
295                 while (qp != &(ship_list)) {
296                     newqp = qp->q_forw;
297                     emp_remque(qp);
298                     free(qp);
299                     qp = newqp;
300                 }
301                 return RET_SYN;
302             }
303             stopping = 0;
304
305             while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
306                 dir = diridx(*cp++);
307                 stopping |= shp_nav_one_sector(&ship_list, dir,
308                                                sp->shp_own, 0);
309             }
310
311             /* Ship not sunk */
312             if (sp->shp_own)
313                 nav_check_atdest(sp);
314         }
315
316         quit = 0;               /* stop loop */
317
318         /* Try to load the ship */
319         if (sp->shp_autonav & AN_LOADING) {
320             didsomething = nav_loadship(sp, cnum);
321             if (didsomething)
322                 quit = 1;
323         }
324         /* special case for fishing boats */
325         if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
326             food_amt = sp->shp_item[I_FOOD];
327             max_amt = vship->m_item[I_FOOD];
328             sectp = getsectp(sp->shp_x, sp->shp_y);
329
330             if (food_amt < max_amt && (sectp->sct_own == 0))
331                 quit = 0;
332         }
333         /* reset flag and check if we can move. */
334
335     } while (quit);             /* end loop */
336
337     putship(sp->shp_uid, sp);
338
339     /* We need to free the ship list (just in case) */
340     qp = ship_list.q_forw;
341     while (qp != &(ship_list)) {
342         newqp = qp->q_forw;
343         emp_remque(qp);
344         free(qp);
345         qp = newqp;
346     }
347     return RET_OK;
348 }