2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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:
43 static void swap(struct shpstr *);
46 scuttle_it(struct shpstr *sp)
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);
56 if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
58 "%s is not in a harbor at least 2%% eff! Not scuttling.\n",
63 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
64 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
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));
71 scuttle_tradeship(sp, 0);
77 nav_check_atdest(struct shpstr *sp)
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])) {
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) {
92 /* unload all cargo */
94 wu(0, sp->shp_own, "%s arrived at %s\n",
95 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
100 sp->shp_autonav &= ~AN_LOADING;
103 /* flip the 2 fields that deal with autonav movement. */
107 swap(struct shpstr *sp)
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;
121 for (i = 0; i < TMAX; ++i) {
122 lev[i] = sp->shp_lstart[i];
123 tcomm[i] = sp->shp_tstart[i];
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];
131 for (i = 0; i < TMAX; ++i) {
132 sp->shp_lend[i] = lev[i];
133 sp->shp_tend[i] = tcomm[i];
137 sp->shp_autonav |= AN_LOADING;
146 nav_loadship(struct shpstr *sp, natid cnum)
148 struct sctstr *sectp;
149 int i, landown, shipown, didsomething[TMAX], rel;
151 for (i = 0; i < TMAX; i++)
154 /* Turn off the loading flag.
155 * if any of the loads fail on the ship
156 * it will be turned back on.
159 sp->shp_autonav &= ~AN_LOADING;
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 */
166 landown = sectp->sct_own;
167 shipown = sp->shp_own;
168 rel = getrel(getnatp(sectp->sct_own), cnum);
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. */
174 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
175 /* nothing to do move on. */
180 /* either sea or deity harbor */
184 if (!sect_has_dock(sectp)) {
185 /* we can only load in harbors */
189 if (landown == shipown || rel >= FRIENDLY)
190 didsomething[i] = load_it(sp, sectp, i);
193 /* check for any unsucessful loads */
194 /* if we have any return 0 to stop */
195 /* the nav_ship loop. */
197 for (i = 0; i < TMAX; i++) {
198 if (didsomething[i] == 0)
201 /* All loads were succesful */
206 nav_load_ship_at_sea(struct shpstr *sp)
210 int max_amt, item_amt;
212 struct sctstr *sectp;
213 struct check_list_st {
216 } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
218 n_items = sizeof(check_list) / sizeof(check_list[0]);
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)
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.
242 * A check has been added for fuel so ships don't end up running
243 * out of mobility in the ocean.
245 * Questions, bugs (fixes) , or new ideas should be directed at
248 * Modified to use shp_nav by Ken Stevens 1995
251 nav_ship(struct shpstr *sp)
253 struct sctstr *sectp;
257 int didsomething = 0;
259 struct emp_qelem ship_list;
260 struct emp_qelem *qp, *newqp;
267 /* just return if no autonaving to do for this ship */
268 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
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);
281 quit = 1; /* setup loop, we want to check it 1 time. */
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,
288 if (QEMPTY(&ship_list))
290 /* before we move check to see if ship needs fuel. */
291 sectp = getsectp(sp->shp_x, sp->shp_y);
293 sectp->sct_own != 0 &&
295 ((struct mchrstr *)mlp->chrp)->m_fuelu != 0)
297 mlp->unit.ship.shp_fuel = sp->shp_fuel;
299 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
300 sp->shp_destx[0], sp->shp_desty[0],
304 "%s bad path, ship put on standby\n", prship(sp));
305 sp->shp_autonav |= AN_STANDBY;
306 putship(sp->shp_uid, sp);
308 /* We need to free the ship list */
309 qp = ship_list.q_forw;
310 while (qp != &ship_list) {
320 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
322 stopping |= shp_nav_one_sector(&ship_list, dir,
328 nav_check_atdest(sp);
331 quit = 0; /* stop loop */
333 /* Try to load the ship */
334 if (sp->shp_autonav & AN_LOADING) {
335 didsomething = nav_loadship(sp, cnum);
339 /* special case for fishing boats and oil derricks */
340 if (nav_load_ship_at_sea(sp))
342 /* reset flag and check if we can move. */
344 } while (quit); /* end loop */
346 putship(sp->shp_uid, sp);
348 /* We need to free the ship list (just in case) */
349 qp = ship_list.q_forw;
350 while (qp != &ship_list) {