]> git.pond.sub.org Git - empserver/blob - src/lib/update/nav_ship.c
Update copyright notice
[empserver] / src / lib / update / nav_ship.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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, natid cnum)
146 {
147     struct sctstr *sectp;
148     int i, landown, shipown, 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     landown = sectp->sct_own;
164     shipown = sp->shp_own;
165     rel = getrel(getnatp(sectp->sct_own), cnum);
166
167     /* loop through each field for that ship */
168     for (i = 0; i < TMAX; ++i) {
169         /* check and see if the data fields have been set. */
170
171         if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
172             /* nothing to do move on. */
173             didsomething[i] = 1;
174             continue;
175         }
176         if (landown == 0) {
177             /* either sea or deity harbor */
178             didsomething[i] = 1;
179             continue;
180         }
181         if (!sect_has_dock(sectp)) {
182             /* we can only load in harbors */
183             didsomething[i] = 1;
184             continue;
185         }
186         if (landown == shipown || rel >= FRIENDLY)
187             didsomething[i] = load_it(sp, sectp, i);
188     }
189
190     /* check for any unsucessful loads */
191     /* if we have any return 0 to stop */
192     /* the nav_ship loop.              */
193
194     for (i = 0; i < TMAX; i++) {
195         if (didsomething[i] == 0)
196             return 0;
197     }
198     /* All loads were succesful */
199     return 1;
200 }
201
202 static int
203 nav_load_ship_at_sea(struct shpstr *sp)
204 {
205     int i;
206     int n_items;
207     int max_amt, item_amt;
208     struct mchrstr *mcp;
209     struct sctstr *sectp;
210     struct check_list_st {
211         long cap;
212         i_type item;
213     } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
214
215     n_items = sizeof(check_list) / sizeof(check_list[0]);
216
217     mcp = &mchr[(int)sp->shp_type];
218     sectp = getsectp(sp->shp_x, sp->shp_y);
219     for (i = 0; i < n_items; i++) {
220         if (mcp->m_flags & check_list[i].cap) {
221             item_amt = sp->shp_item[check_list[i].item];
222             max_amt = mcp->m_item[check_list[i].item];
223             if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
224                 return 1;
225         }
226     }
227     return 0;
228 }
229
230 /* new autonav code.
231  *
232  * 1. Try and move to the next sector/harbor given by the player.
233  * 2. Once we reach a harbor try and load all cargo holds for that ship.
234  * 3. If the ship reaches its max levels set by the player try to use
235  *    up all mobility getting to the next harbor.
236  * Continue to loop until the ship runs out of mobility, a load fails,
237  * the ship gets sunk (forts,ect..), the ship hits a mine.
238  *
239  * Questions, bugs (fixes) , or new ideas should be directed at
240  * Chad Zabel.
241  * 6-1-94
242  * Modified to use shp_nav by Ken Stevens 1995
243  */
244 int
245 nav_ship(struct shpstr *sp)
246 {
247     char *cp;
248     int stopping;
249     int quit;
250     int didsomething = 0;
251     char buf[1024];
252     struct emp_qelem ship_list;
253     struct emp_qelem *qp, *newqp;
254     struct ulist *mlp;
255     int dummyint;
256     double dummydouble;
257     int dir;
258     natid cnum;
259
260     /* just return if no autonaving to do for this ship */
261     if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
262         return 0;
263
264     cnum = sp->shp_own;
265
266     /* Make a list of one ships so we can use the navi.c code */
267     emp_initque(&ship_list);
268     mlp = malloc(sizeof(struct ulist));
269     mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
270     mlp->unit.ship = *sp;
271     mlp->mobil = sp->shp_mobil;
272     emp_insque(&mlp->queue, &ship_list);
273
274     quit = 1;                   /* setup loop, we want to check it 1 time. */
275
276     do {
277         if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
278             (!(sp->shp_autonav & AN_STANDBY))) {
279             shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
280                     sp->shp_own);
281             if (QEMPTY(&ship_list))
282                 return 0;
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) {
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 -1;
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 and oil derricks */
325         if (nav_load_ship_at_sea(sp))
326             quit = 0;
327         /* reset flag and check if we can move. */
328
329     } while (quit);             /* end loop */
330
331     putship(sp->shp_uid, sp);
332
333     /* We need to free the ship list (just in case) */
334     qp = ship_list.q_forw;
335     while (qp != &ship_list) {
336         newqp = qp->q_forw;
337         emp_remque(qp);
338         free(qp);
339         qp = newqp;
340     }
341     return 0;
342 }