2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * nav_ship.c: Navigate ships and such
29 * Known contributors to this file:
42 static void swap(struct shpstr *);
45 scuttle_it(struct shpstr *sp)
49 sp->shp_autonav &= ~AN_SCUTTLE;
50 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
51 wu(0, 0, "bad sector (%d,%d) ship %d\n",
52 sp->shp_x, sp->shp_y, sp->shp_uid);
56 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
57 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
61 if (!scuttle_tradeship(sp, 0)) {
63 "%s doesn't pay here! Not scuttled.\n", prship(sp));
66 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
67 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
68 if (sectp->sct_own == sp->shp_own)
69 unit_drop_cargo((struct empobj *)sp, sectp->sct_own);
71 putship(sp->shp_uid, sp);
75 nav_check_atdest(struct shpstr *sp)
77 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
78 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
79 (sp->shp_desty[0] == sp->shp_desty[1])) {
83 sp->shp_autonav &= ~AN_AUTONAV;
84 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
85 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
86 if (sp->shp_autonav & AN_SCUTTLE) {
90 /* unload all cargo */
92 wu(0, sp->shp_own, "%s arrived at %s\n",
93 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
98 sp->shp_autonav &= ~AN_LOADING;
101 /* flip the 2 fields that deal with autonav movement. */
105 swap(struct shpstr *sp)
112 tcord = sp->shp_destx[0];
113 sp->shp_destx[0] = sp->shp_destx[1];
114 sp->shp_destx[1] = tcord;
115 tcord = sp->shp_desty[0];
116 sp->shp_desty[0] = sp->shp_desty[1];
117 sp->shp_desty[1] = tcord;
119 for (i = 0; i < TMAX; ++i) {
120 lev[i] = sp->shp_lstart[i];
121 tcomm[i] = sp->shp_tstart[i];
124 for (i = 0; i < TMAX; ++i) {
125 sp->shp_lstart[i] = sp->shp_lend[i];
126 sp->shp_tstart[i] = sp->shp_tend[i];
129 for (i = 0; i < TMAX; ++i) {
130 sp->shp_lend[i] = lev[i];
131 sp->shp_tend[i] = tcomm[i];
135 sp->shp_autonav |= AN_LOADING;
144 nav_loadship(struct shpstr *sp)
146 struct sctstr *sectp;
147 int i, didsomething[TMAX], rel;
149 for (i = 0; i < TMAX; i++)
152 /* Turn off the loading flag.
153 * if any of the loads fail on the ship
154 * it will be turned back on.
157 sp->shp_autonav &= ~AN_LOADING;
159 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
160 return 0; /* safety */
162 rel = relations_with(sectp->sct_own, sp->shp_own);
164 /* loop through each field for that ship */
165 for (i = 0; i < TMAX; ++i) {
166 /* check and see if the data fields have been set. */
168 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
169 /* nothing to do move on. */
173 if (sectp->sct_own == 0) {
174 /* either sea or deity harbor */
178 if (!sect_has_dock(sectp)) {
179 /* we can only load in harbors */
184 didsomething[i] = load_it(sp, sectp, i);
187 /* check for any unsucessful loads */
188 /* if we have any return 0 to stop */
189 /* the nav_ship loop. */
191 for (i = 0; i < TMAX; i++) {
192 if (didsomething[i] == 0)
195 /* All loads were succesful */
200 nav_load_ship_at_sea(struct shpstr *sp)
204 int max_amt, item_amt;
206 struct sctstr *sectp;
207 struct check_list_st {
210 } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
212 n_items = sizeof(check_list) / sizeof(check_list[0]);
214 mcp = &mchr[(int)sp->shp_type];
215 sectp = getsectp(sp->shp_x, sp->shp_y);
216 for (i = 0; i < n_items; i++) {
217 if (mcp->m_flags & check_list[i].cap) {
218 item_amt = sp->shp_item[check_list[i].item];
219 max_amt = mcp->m_item[check_list[i].item];
220 if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
229 * 1. Try and move to the next sector/harbor given by the player.
230 * 2. Once we reach a harbor try and load all cargo holds for that ship.
231 * 3. If the ship reaches its max levels set by the player try to use
232 * up all mobility getting to the next harbor.
233 * Continue to loop until the ship runs out of mobility, a load fails,
234 * the ship gets sunk (forts,ect..), the ship hits a mine.
236 * Questions, bugs (fixes) , or new ideas should be directed at
239 * Modified to use shp_nav by Ken Stevens 1995
242 nav_ship(struct shpstr *sp)
247 int didsomething = 0;
249 struct emp_qelem ship_list;
250 struct emp_qelem *qp, *newqp;
256 /* just return if no autonaving to do for this ship */
257 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
260 /* Make a list of one ships so we can use the navi.c code */
261 emp_initque(&ship_list);
262 mlp = malloc(sizeof(struct ulist));
263 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
264 mlp->unit.ship = *sp;
265 ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
266 mlp->mobil = sp->shp_mobil;
267 emp_insque(&mlp->queue, &ship_list);
270 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
271 (!(sp->shp_autonav & AN_STANDBY))) {
272 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
274 if (QEMPTY(&ship_list))
277 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
278 sp->shp_destx[0], sp->shp_desty[0],
282 "%s bad path, ship put on standby\n", prship(sp));
283 sp->shp_autonav |= AN_STANDBY;
284 putship(sp->shp_uid, sp);
286 /* We need to free the ship list */
287 qp = ship_list.q_forw;
288 while (qp != &ship_list) {
298 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
300 stopping |= shp_nav_one_sector(&ship_list, dir,
306 nav_check_atdest(sp);
309 quit = 0; /* stop loop */
311 /* Try to load the ship */
312 if (sp->shp_autonav & AN_LOADING) {
313 didsomething = nav_loadship(sp);
317 /* special case for fishing boats and oil derricks */
318 if (nav_load_ship_at_sea(sp))
320 /* reset flag and check if we can move. */
322 } while (quit); /* end loop */
324 putship(sp->shp_uid, sp);
326 /* We need to free the ship list (just in case) */
327 qp = ship_list.q_forw;
328 while (qp != &ship_list) {