]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_ship.c
Fix scuttle_it()'s "is a trade ship" sanity check
[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  *     Markus Armbruster, 2004-2011
33  */
34
35 #include <config.h>
36
37 #include "nsc.h"
38 #include "path.h"
39 #include "update.h"
40 #include "empobj.h"
41 #include "unit.h"
42
43 static void swap(struct shpstr *);
44
45 static void
46 scuttle_it(struct shpstr *sp)
47 {
48     struct sctstr *sectp;
49
50     sp->shp_autonav &= ~AN_SCUTTLE;
51     if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
52         wu(0, 0, "bad sector (%d,%d) ship %d\n",
53            sp->shp_x, sp->shp_y, sp->shp_uid);
54         return;
55     }
56     if (CANT_HAPPEN(!opt_TRADESHIPS
57                     || !(mchr[sp->shp_type].m_flags & M_TRADE)))
58         return;
59     if (!scuttle_tradeship(sp, 0)) {
60         wu(0, sp->shp_own,
61            "%s doesn't pay here!  Not scuttled.\n", prship(sp));
62         return;
63     }
64     wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
65        prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
66     if (sectp->sct_own == sp->shp_own)
67         unit_drop_cargo((struct empobj *)sp, sectp->sct_own);
68     sp->shp_effic = 0;
69     putship(sp->shp_uid, sp);
70 }
71
72 static void
73 nav_check_atdest(struct shpstr *sp)
74 {
75     if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
76         if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
77             (sp->shp_desty[0] == sp->shp_desty[1])) {
78
79             /* End of road */
80
81             sp->shp_autonav &= ~AN_AUTONAV;
82             wu(0, sp->shp_own, "%s arrived at %s, finished\n",
83                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
84             if (sp->shp_autonav & AN_SCUTTLE) {
85                 scuttle_it(sp);
86             }
87         } else {
88             /* unload all cargo */
89             unload_it(sp);
90             wu(0, sp->shp_own, "%s arrived at %s\n",
91                prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
92             /* Swap */
93             swap(sp);
94         }
95     } else
96         sp->shp_autonav &= ~AN_LOADING;
97 }
98
99 /* flip the 2 fields that deal with autonav movement. */
100 /* CZ 6/1/94                                          */
101
102 static void
103 swap(struct shpstr *sp)
104 {
105     coord tcord;
106     i_type tcomm[TMAX];
107     short lev[TMAX];
108     int i;
109
110     tcord = sp->shp_destx[0];
111     sp->shp_destx[0] = sp->shp_destx[1];
112     sp->shp_destx[1] = tcord;
113     tcord = sp->shp_desty[0];
114     sp->shp_desty[0] = sp->shp_desty[1];
115     sp->shp_desty[1] = tcord;
116
117     for (i = 0; i < TMAX; ++i) {
118         lev[i] = sp->shp_lstart[i];
119         tcomm[i] = sp->shp_tstart[i];
120     }
121
122     for (i = 0; i < TMAX; ++i) {
123         sp->shp_lstart[i] = sp->shp_lend[i];
124         sp->shp_tstart[i] = sp->shp_tend[i];
125     }
126
127     for (i = 0; i < TMAX; ++i) {
128         sp->shp_lend[i] = lev[i];
129         sp->shp_tend[i] = tcomm[i];
130     }
131
132     /* set load bit */
133     sp->shp_autonav |= AN_LOADING;
134 }
135
136 /*  New Autonav code.
137  *  Chad Zabel
138  *  6-1-94
139  */
140
141 static int
142 nav_loadship(struct shpstr *sp)
143 {
144     struct sctstr *sectp;
145     int i, didsomething[TMAX], rel;
146
147     for (i = 0; i < TMAX; i++)
148         didsomething[i] = 0;
149
150     /* Turn off the loading flag.
151      * if any of the loads fail on the ship
152      * it will be turned back on.
153      */
154
155     sp->shp_autonav &= ~AN_LOADING;
156
157     if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
158         return 0;               /* safety */
159
160     rel = relations_with(sectp->sct_own, sp->shp_own);
161
162     /* loop through each field for that ship */
163     for (i = 0; i < TMAX; ++i) {
164         /* check and see if the data fields have been set. */
165
166         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
167             /* nothing to do move on. */
168             didsomething[i] = 1;
169             continue;
170         }
171         if (sectp->sct_own == 0) {
172             /* either sea or deity harbor */
173             didsomething[i] = 1;
174             continue;
175         }
176         if (!sect_has_dock(sectp)) {
177             /* we can only load in harbors */
178             didsomething[i] = 1;
179             continue;
180         }
181         if (rel >= FRIENDLY)
182             didsomething[i] = load_it(sp, sectp, i);
183     }
184
185     /* check for any unsucessful loads */
186     /* if we have any return 0 to stop */
187     /* the nav_ship loop.              */
188
189     for (i = 0; i < TMAX; i++) {
190         if (didsomething[i] == 0)
191             return 0;
192     }
193     /* All loads were succesful */
194     return 1;
195 }
196
197 static int
198 nav_load_ship_at_sea(struct shpstr *sp)
199 {
200     int i;
201     int n_items;
202     int max_amt, item_amt;
203     struct mchrstr *mcp;
204     struct sctstr *sectp;
205     struct check_list_st {
206         long cap;
207         i_type item;
208     } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
209
210     n_items = sizeof(check_list) / sizeof(check_list[0]);
211
212     mcp = &mchr[(int)sp->shp_type];
213     sectp = getsectp(sp->shp_x, sp->shp_y);
214     for (i = 0; i < n_items; i++) {
215         if (mcp->m_flags & check_list[i].cap) {
216             item_amt = sp->shp_item[check_list[i].item];
217             max_amt = mcp->m_item[check_list[i].item];
218             if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
219                 return 1;
220         }
221     }
222     return 0;
223 }
224
225 /* new autonav code.
226  *
227  * 1. Try and move to the next sector/harbor given by the player.
228  * 2. Once we reach a harbor try and load all cargo holds for that ship.
229  * 3. If the ship reaches its max levels set by the player try to use
230  *    up all mobility getting to the next harbor.
231  * Continue to loop until the ship runs out of mobility, a load fails,
232  * the ship gets sunk (forts,ect..), the ship hits a mine.
233  *
234  * Questions, bugs (fixes) , or new ideas should be directed at
235  * Chad Zabel.
236  * 6-1-94
237  * Modified to use shp_nav by Ken Stevens 1995
238  */
239 int
240 nav_ship(struct shpstr *sp)
241 {
242     char *cp;
243     int stopping;
244     int quit;
245     int didsomething = 0;
246     char buf[1024];
247     struct emp_qelem ship_list;
248     struct emp_qelem *qp, *newqp;
249     struct ulist *mlp;
250     int dummyint;
251     double dummydouble;
252     int dir;
253
254     /* just return if no autonaving to do for this ship */
255     if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
256         return 0;
257
258     /* Make a list of one ships so we can use the navi.c code */
259     emp_initque(&ship_list);
260     mlp = malloc(sizeof(struct ulist));
261     mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
262     mlp->unit.ship = *sp;
263     ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
264     mlp->mobil = sp->shp_mobil;
265     emp_insque(&mlp->queue, &ship_list);
266
267     do {
268         if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
269             (!(sp->shp_autonav & AN_STANDBY))) {
270             shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
271                     sp->shp_own);
272             if (QEMPTY(&ship_list))
273                 return 0;
274
275             if (path_find(sp->shp_x, sp->shp_y,
276                           sp->shp_destx[0], sp->shp_desty[0],
277                           sp->shp_own, MOB_SAIL) < 0) {
278                 wu(0, sp->shp_own,
279                    "%s bad path, ship put on standby\n", prship(sp));
280                 sp->shp_autonav |= AN_STANDBY;
281                 putship(sp->shp_uid, sp);
282
283                 /* We need to free the ship list */
284                 qp = ship_list.q_forw;
285                 while (qp != &ship_list) {
286                     newqp = qp->q_forw;
287                     emp_remque(qp);
288                     free(qp);
289                     qp = newqp;
290                 }
291                 return -1;
292             }
293             path_find_route(buf, sizeof(buf), sp->shp_x, sp->shp_y,
294                             sp->shp_destx[0], sp->shp_desty[0]);
295             stopping = 0;
296
297             cp = buf;
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 }