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