2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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);
57 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
58 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
62 if (!scuttle_tradeship(sp, 0)) {
64 "%s doesn't pay here! Not scuttled.\n", prship(sp));
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);
72 putship(sp->shp_uid, sp);
76 nav_check_atdest(struct shpstr *sp)
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])) {
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) {
91 /* unload all cargo */
93 wu(0, sp->shp_own, "%s arrived at %s\n",
94 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
99 sp->shp_autonav &= ~AN_LOADING;
102 /* flip the 2 fields that deal with autonav movement. */
106 swap(struct shpstr *sp)
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;
120 for (i = 0; i < TMAX; ++i) {
121 lev[i] = sp->shp_lstart[i];
122 tcomm[i] = sp->shp_tstart[i];
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];
130 for (i = 0; i < TMAX; ++i) {
131 sp->shp_lend[i] = lev[i];
132 sp->shp_tend[i] = tcomm[i];
136 sp->shp_autonav |= AN_LOADING;
145 nav_loadship(struct shpstr *sp)
147 struct sctstr *sectp;
148 int i, didsomething[TMAX], rel;
150 for (i = 0; i < TMAX; i++)
153 /* Turn off the loading flag.
154 * if any of the loads fail on the ship
155 * it will be turned back on.
158 sp->shp_autonav &= ~AN_LOADING;
160 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
161 return 0; /* safety */
163 rel = relations_with(sectp->sct_own, sp->shp_own);
165 /* loop through each field for that ship */
166 for (i = 0; i < TMAX; ++i) {
167 /* check and see if the data fields have been set. */
169 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
170 /* nothing to do move on. */
174 if (sectp->sct_own == 0) {
175 /* either sea or deity harbor */
179 if (!sect_has_dock(sectp)) {
180 /* we can only load in harbors */
185 didsomething[i] = load_it(sp, sectp, i);
188 /* check for any unsucessful loads */
189 /* if we have any return 0 to stop */
190 /* the nav_ship loop. */
192 for (i = 0; i < TMAX; i++) {
193 if (didsomething[i] == 0)
196 /* All loads were succesful */
201 nav_load_ship_at_sea(struct shpstr *sp)
205 int max_amt, item_amt;
207 struct sctstr *sectp;
208 struct check_list_st {
211 } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
213 n_items = sizeof(check_list) / sizeof(check_list[0]);
215 mcp = &mchr[(int)sp->shp_type];
216 sectp = getsectp(sp->shp_x, sp->shp_y);
217 for (i = 0; i < n_items; i++) {
218 if (mcp->m_flags & check_list[i].cap) {
219 item_amt = sp->shp_item[check_list[i].item];
220 max_amt = mcp->m_item[check_list[i].item];
221 if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
230 * 1. Try and move to the next sector/harbor given by the player.
231 * 2. Once we reach a harbor try and load all cargo holds for that ship.
232 * 3. If the ship reaches its max levels set by the player try to use
233 * up all mobility getting to the next harbor.
234 * Continue to loop until the ship runs out of mobility, a load fails,
235 * the ship gets sunk (forts,ect..), the ship hits a mine.
237 * Questions, bugs (fixes) , or new ideas should be directed at
240 * Modified to use shp_nav by Ken Stevens 1995
243 nav_ship(struct shpstr *sp)
248 int didsomething = 0;
250 struct emp_qelem ship_list;
251 struct emp_qelem *qp, *newqp;
257 /* just return if no autonaving to do for this ship */
258 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
261 /* Make a list of one ships so we can use the navi.c code */
262 emp_initque(&ship_list);
263 mlp = malloc(sizeof(struct ulist));
264 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
265 mlp->unit.ship = *sp;
266 ef_mark_fresh(EF_SHIP, &mlp->unit.ship);
267 mlp->mobil = sp->shp_mobil;
268 emp_insque(&mlp->queue, &ship_list);
271 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
272 (!(sp->shp_autonav & AN_STANDBY))) {
273 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
275 if (QEMPTY(&ship_list))
278 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
279 sp->shp_destx[0], sp->shp_desty[0],
283 "%s bad path, ship put on standby\n", prship(sp));
284 sp->shp_autonav |= AN_STANDBY;
285 putship(sp->shp_uid, sp);
287 /* We need to free the ship list */
288 qp = ship_list.q_forw;
289 while (qp != &ship_list) {
299 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
301 stopping |= shp_nav_one_sector(&ship_list, dir,
307 nav_check_atdest(sp);
310 quit = 0; /* stop loop */
312 /* Try to load the ship */
313 if (sp->shp_autonav & AN_LOADING) {
314 didsomething = nav_loadship(sp);
318 /* special case for fishing boats and oil derricks */
319 if (nav_load_ship_at_sea(sp))
321 /* reset flag and check if we can move. */
323 } while (quit); /* end loop */
325 putship(sp->shp_uid, sp);
327 /* We need to free the ship list (just in case) */
328 qp = ship_list.q_forw;
329 while (qp != &ship_list) {