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