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
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.
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.
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
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.
28 * nav_ship.c: Navigate ships and such
30 * Known contributors to this file:
45 static void swap(struct shpstr *);
48 scuttle_it(struct shpstr *sp)
52 sp->shp_autonav &= ~AN_SCUTTLE;
53 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
54 wu(0, 0, "bad sector (%d,%d) ship %d\n",
55 sp->shp_x, sp->shp_y, sp->shp_uid);
58 if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
60 "%s is not in a harbor at least 2%% eff! Not scuttling.\n",
65 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
66 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
70 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
71 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
73 scuttle_tradeship(sp, 0);
79 nav_check_atdest(struct shpstr *sp)
81 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
82 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
83 (sp->shp_desty[0] == sp->shp_desty[1])) {
87 sp->shp_autonav &= ~AN_AUTONAV;
88 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
89 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
90 if (sp->shp_autonav & AN_SCUTTLE) {
94 /* unload all cargo */
96 wu(0, sp->shp_own, "%s arrived at %s\n",
97 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
102 sp->shp_autonav &= ~AN_LOADING;
105 /* flip the 2 fields that deal with autonav movement. */
109 swap(struct shpstr *sp)
116 tcord = sp->shp_destx[0];
117 sp->shp_destx[0] = sp->shp_destx[1];
118 sp->shp_destx[1] = tcord;
119 tcord = sp->shp_desty[0];
120 sp->shp_desty[0] = sp->shp_desty[1];
121 sp->shp_desty[1] = tcord;
123 for (i = 0; i < TMAX; ++i) {
124 lev[i] = sp->shp_lstart[i];
125 tcomm[i] = sp->shp_tstart[i];
128 for (i = 0; i < TMAX; ++i) {
129 sp->shp_lstart[i] = sp->shp_lend[i];
130 sp->shp_tstart[i] = sp->shp_tend[i];
133 for (i = 0; i < TMAX; ++i) {
134 sp->shp_lend[i] = lev[i];
135 sp->shp_tend[i] = tcomm[i];
139 sp->shp_autonav |= AN_LOADING;
148 nav_loadship(struct shpstr *sp, natid cnum)
150 struct sctstr *sectp;
151 int i, landown, shipown, didsomething[TMAX], rel;
153 for (i = 0; i < TMAX; i++)
156 /* Turn off the loading flag.
157 * if any of the loads fail on the ship
158 * it will be turned back on.
161 sp->shp_autonav &= ~AN_LOADING;
163 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
164 return RET_SYS; /* safety */
165 /* I suspect RET_SYS isn't really what you want here --dfp */
168 landown = sectp->sct_own;
169 shipown = sp->shp_own;
170 rel = getrel(getnatp(sectp->sct_own), cnum);
172 /* loop through each field for that ship */
173 for (i = 0; i < TMAX; ++i) {
174 /* check and see if the data fields have been set. */
176 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
177 /* nothing to do move on. */
182 /* either sea or deity harbor */
186 if (!sect_has_dock(sectp)) {
187 /* we can only load in harbors */
191 if (landown == shipown || rel >= FRIENDLY)
192 didsomething[i] = load_it(sp, sectp, i);
195 /* check for any unsucessful loads */
196 /* if we have any return 0 to stop */
197 /* the nav_ship loop. */
199 for (i = 0; i < TMAX; i++) {
200 if (didsomething[i] == 0)
203 /* All loads were succesful */
209 * 1. Try and move to the next sector/harbor given by the player.
210 * 2. Once we reach a harbor try and load all cargo holds for that ship.
211 * 3. If the ship reaches its max levels set by the player try to use
212 * up all mobility getting to the next harbor.
213 * Continue to loop until the ship runs out of mobility, a load fails,
214 * the ship gets sunk (forts,ect..), the ship hits a mine.
216 * A check has been added for fuel so ships don't end up running
217 * out of mobility in the ocean.
219 * Questions, bugs (fixes) , or new ideas should be directed at
222 * Modified to use shp_nav by Ken Stevens 1995
225 nav_ship(struct shpstr *sp)
227 struct sctstr *sectp;
231 int didsomething = 0;
232 int max_amt, food_amt;
234 struct emp_qelem ship_list;
235 struct emp_qelem *qp, *newqp;
241 struct mchrstr *mcp, *vship;
243 /* just return if no autonaving to do for this ship */
244 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
248 vship = mcp = &mchr[(int)sp->shp_type];
250 /* Make a list of one ships so we can use the navi.c code */
251 emp_initque(&ship_list);
252 mlp = malloc(sizeof(struct ulist));
253 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
254 mlp->unit.ship = *sp;
255 mlp->mobil = sp->shp_mobil;
256 emp_insque(&mlp->queue, &ship_list);
258 quit = 1; /* setup loop, we want to check it 1 time. */
261 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
262 (!(sp->shp_autonav & AN_STANDBY))) {
263 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
265 if (QEMPTY(&ship_list))
267 /* before we move check to see if ship needs fuel. */
268 sectp = getsectp(sp->shp_x, sp->shp_y);
270 sectp->sct_own != 0 &&
272 ((struct mchrstr *)mlp->chrp)->m_fuelu != 0)
274 mlp->unit.ship.shp_fuel = sp->shp_fuel;
276 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
277 sp->shp_destx[0], sp->shp_desty[0],
281 "%s bad path, ship put on standby\n", prship(sp));
282 sp->shp_autonav |= AN_STANDBY;
283 putship(sp->shp_uid, sp);
285 /* We need to free the ship list */
286 qp = ship_list.q_forw;
287 while (qp != &ship_list) {
297 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
299 stopping |= shp_nav_one_sector(&ship_list, dir,
305 nav_check_atdest(sp);
308 quit = 0; /* stop loop */
310 /* Try to load the ship */
311 if (sp->shp_autonav & AN_LOADING) {
312 didsomething = nav_loadship(sp, cnum);
316 /* special case for fishing boats */
317 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
318 food_amt = sp->shp_item[I_FOOD];
319 max_amt = vship->m_item[I_FOOD];
320 sectp = getsectp(sp->shp_x, sp->shp_y);
322 if (food_amt < max_amt && (sectp->sct_own == 0))
325 /* reset flag and check if we can move. */
327 } while (quit); /* end loop */
329 putship(sp->shp_uid, sp);
331 /* We need to free the ship list (just in case) */
332 qp = ship_list.q_forw;
333 while (qp != &ship_list) {