]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_ship.c
License upgrade to GPL version 3 or later
[empserver] / src / lib / update / nav_ship.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  nav_ship.c: Navigate ships and such
28  *
29  *  Known contributors to this file:
30  *     Chad Zabel, 1994
31  *     Ken Stevens, 1995
32  */
33
34 #include <config.h>
35
36 #include "nsc.h"
37 #include "path.h"
38 #include "update.h"
39 #include "empobj.h"
40 #include "unit.h"
41
42 static void swap(struct shpstr *);
43
44 static void
45 scuttle_it(struct shpstr *sp)
46 {
47     struct sctstr *sectp;
48
49     sp->shp_autonav &= ~AN_SCUTTLE;
50     if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
51         wu(0, 0, "bad sector (%d,%d) ship %d\n",
52            sp->shp_x, sp->shp_y, sp->shp_uid);
53         return;
54     }
55     if (opt_TRADESHIPS) {
56         if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
57             wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
58             return;
59         }
60     }
61     if (!scuttle_tradeship(sp, 0)) {
62         wu(0, sp->shp_own,
63            "%s doesn't pay here!  Not scuttled.\n", prship(sp));
64         return;
65     }
66     wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
67        prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
68     if (sectp->sct_own == sp->shp_own)
69         unit_drop_cargo((struct empobj *)sp, sectp->sct_own);
70     sp->shp_effic = 0;
71     putship(sp->shp_uid, sp);
72 }
73
74 static void
75 nav_check_atdest(struct shpstr *sp)
76 {
77     if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
78         if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
79             (sp->shp_desty[0] == sp->shp_desty[1])) {
80
81             /* End of road */
82
83             sp->shp_autonav &= ~AN_AUTONAV;
84             wu(0, sp->shp_own, "%s arrived at %s, finished\n",
85                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
86             if (sp->shp_autonav & AN_SCUTTLE) {
87                 scuttle_it(sp);
88             }
89         } else {
90             /* unload all cargo */
91             unload_it(sp);
92             wu(0, sp->shp_own, "%s arrived at %s\n",
93                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
94             /* Swap */
95             swap(sp);
96         }
97     } else
98         sp->shp_autonav &= ~AN_LOADING;
99 }
100
101 /* flip the 2 fields that deal with autonav movement. */
102 /* CZ 6/1/94                                          */
103
104 static void
105 swap(struct shpstr *sp)
106 {
107     coord tcord;
108     i_type tcomm[TMAX];
109     short lev[TMAX];
110     int i;
111
112     tcord = sp->shp_destx[0];
113     sp->shp_destx[0] = sp->shp_destx[1];
114     sp->shp_destx[1] = tcord;
115     tcord = sp->shp_desty[0];
116     sp->shp_desty[0] = sp->shp_desty[1];
117     sp->shp_desty[1] = tcord;
118
119     for (i = 0; i < TMAX; ++i) {
120         lev[i] = sp->shp_lstart[i];
121         tcomm[i] = sp->shp_tstart[i];
122     }
123
124     for (i = 0; i < TMAX; ++i) {
125         sp->shp_lstart[i] = sp->shp_lend[i];
126         sp->shp_tstart[i] = sp->shp_tend[i];
127     }
128
129     for (i = 0; i < TMAX; ++i) {
130         sp->shp_lend[i] = lev[i];
131         sp->shp_tend[i] = tcomm[i];
132     }
133
134     /* set load bit */
135     sp->shp_autonav |= AN_LOADING;
136 }
137
138 /*  New Autonav code.
139  *  Chad Zabel
140  *  6-1-94
141  */
142
143 static int
144 nav_loadship(struct shpstr *sp)
145 {
146     struct sctstr *sectp;
147     int i, didsomething[TMAX], rel;
148
149     for (i = 0; i < TMAX; i++)
150         didsomething[i] = 0;
151
152     /* Turn off the loading flag.
153      * if any of the loads fail on the ship
154      * it will be turned back on.
155      */
156
157     sp->shp_autonav &= ~AN_LOADING;
158
159     if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
160         return 0;               /* safety */
161
162     rel = relations_with(sectp->sct_own, sp->shp_own);
163
164     /* loop through each field for that ship */
165     for (i = 0; i < TMAX; ++i) {
166         /* check and see if the data fields have been set. */
167
168         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
169             /* nothing to do move on. */
170             didsomething[i] = 1;
171             continue;
172         }
173         if (sectp->sct_own == 0) {
174             /* either sea or deity harbor */
175             didsomething[i] = 1;
176             continue;
177         }
178         if (!sect_has_dock(sectp)) {
179             /* we can only load in harbors */
180             didsomething[i] = 1;
181             continue;
182         }
183         if (rel >= FRIENDLY)
184             didsomething[i] = load_it(sp, sectp, i);
185     }
186
187     /* check for any unsucessful loads */
188     /* if we have any return 0 to stop */
189     /* the nav_ship loop.              */
190
191     for (i = 0; i < TMAX; i++) {
192         if (didsomething[i] == 0)
193             return 0;
194     }
195     /* All loads were succesful */
196     return 1;
197 }
198
199 static int
200 nav_load_ship_at_sea(struct shpstr *sp)
201 {
202     int i;
203     int n_items;
204     int max_amt, item_amt;
205     struct mchrstr *mcp;
206     struct sctstr *sectp;
207     struct check_list_st {
208         long cap;
209         i_type item;
210     } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
211
212     n_items = sizeof(check_list) / sizeof(check_list[0]);
213
214     mcp = &mchr[(int)sp->shp_type];
215     sectp = getsectp(sp->shp_x, sp->shp_y);
216     for (i = 0; i < n_items; i++) {
217         if (mcp->m_flags & check_list[i].cap) {
218             item_amt = sp->shp_item[check_list[i].item];
219             max_amt = mcp->m_item[check_list[i].item];
220             if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
221                 return 1;
222         }
223     }
224     return 0;
225 }
226
227 /* new autonav code.
228  *
229  * 1. Try and move to the next sector/harbor given by the player.
230  * 2. Once we reach a harbor try and load all cargo holds for that ship.
231  * 3. If the ship reaches its max levels set by the player try to use
232  *    up all mobility getting to the next harbor.
233  * Continue to loop until the ship runs out of mobility, a load fails,
234  * the ship gets sunk (forts,ect..), the ship hits a mine.
235  *
236  * Questions, bugs (fixes) , or new ideas should be directed at
237  * Chad Zabel.
238  * 6-1-94
239  * Modified to use shp_nav by Ken Stevens 1995
240  */
241 int
242 nav_ship(struct shpstr *sp)
243 {
244     char *cp;
245     int stopping;
246     int quit;
247     int didsomething = 0;
248     char buf[1024];
249     struct emp_qelem ship_list;
250     struct emp_qelem *qp, *newqp;
251     struct ulist *mlp;
252     int dummyint;
253     double dummydouble;
254     int dir;
255
256     /* just return if no autonaving to do for this ship */
257     if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
258         return 0;
259
260     /* Make a list of one ships so we can use the navi.c code */
261     emp_initque(&ship_list);
262     mlp = malloc(sizeof(struct ulist));
263     mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
264     mlp->unit.ship = *sp;
265     ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
266     mlp->mobil = sp->shp_mobil;
267     emp_insque(&mlp->queue, &ship_list);
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 0;
276
277             cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
278                               sp->shp_destx[0], sp->shp_desty[0],
279                               sp->shp_own);
280             if (!cp) {
281                 wu(0, sp->shp_own,
282                    "%s bad path, ship put on standby\n", prship(sp));
283                 sp->shp_autonav |= AN_STANDBY;
284                 putship(sp->shp_uid, sp);
285
286                 /* We need to free the ship list */
287                 qp = ship_list.q_forw;
288                 while (qp != &ship_list) {
289                     newqp = qp->q_forw;
290                     emp_remque(qp);
291                     free(qp);
292                     qp = newqp;
293                 }
294                 return -1;
295             }
296             stopping = 0;
297
298             while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
299                 dir = diridx(*cp++);
300                 stopping |= shp_nav_one_sector(&ship_list, dir,
301                                                sp->shp_own, 0);
302             }
303
304             /* Ship not sunk */
305             if (sp->shp_own)
306                 nav_check_atdest(sp);
307         }
308
309         quit = 0;               /* stop loop */
310
311         /* Try to load the ship */
312         if (sp->shp_autonav & AN_LOADING) {
313             didsomething = nav_loadship(sp);
314             if (didsomething)
315                 quit = 1;
316         }
317         /* special case for fishing boats and oil derricks */
318         if (nav_load_ship_at_sea(sp))
319             quit = 0;
320         /* reset flag and check if we can move. */
321
322     } while (quit);             /* end loop */
323
324     putship(sp->shp_uid, sp);
325
326     /* We need to free the ship list (just in case) */
327     qp = ship_list.q_forw;
328     while (qp != &ship_list) {
329         newqp = qp->q_forw;
330         emp_remque(qp);
331         free(qp);
332         qp = newqp;
333     }
334     return 0;
335 }