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