2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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, natid cnum)
147 struct sctstr *sectp;
148 int i, landown, shipown, 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 landown = sectp->sct_own;
164 shipown = sp->shp_own;
165 rel = getrel(getnatp(sectp->sct_own), cnum);
167 /* loop through each field for that ship */
168 for (i = 0; i < TMAX; ++i) {
169 /* check and see if the data fields have been set. */
171 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
172 /* nothing to do move on. */
177 /* either sea or deity harbor */
181 if (!sect_has_dock(sectp)) {
182 /* we can only load in harbors */
186 if (landown == shipown || rel >= FRIENDLY)
187 didsomething[i] = load_it(sp, sectp, i);
190 /* check for any unsucessful loads */
191 /* if we have any return 0 to stop */
192 /* the nav_ship loop. */
194 for (i = 0; i < TMAX; i++) {
195 if (didsomething[i] == 0)
198 /* All loads were succesful */
203 nav_load_ship_at_sea(struct shpstr *sp)
207 int max_amt, item_amt;
209 struct sctstr *sectp;
210 struct check_list_st {
213 } check_list[] = {{M_FOOD, I_FOOD}, {M_OIL, I_OIL}};
215 n_items = sizeof(check_list) / sizeof(check_list[0]);
217 mcp = &mchr[(int)sp->shp_type];
218 sectp = getsectp(sp->shp_x, sp->shp_y);
219 for (i = 0; i < n_items; i++) {
220 if (mcp->m_flags & check_list[i].cap) {
221 item_amt = sp->shp_item[check_list[i].item];
222 max_amt = mcp->m_item[check_list[i].item];
223 if (item_amt < max_amt && sectp->sct_type == SCT_WATER)
232 * 1. Try and move to the next sector/harbor given by the player.
233 * 2. Once we reach a harbor try and load all cargo holds for that ship.
234 * 3. If the ship reaches its max levels set by the player try to use
235 * up all mobility getting to the next harbor.
236 * Continue to loop until the ship runs out of mobility, a load fails,
237 * the ship gets sunk (forts,ect..), the ship hits a mine.
239 * Questions, bugs (fixes) , or new ideas should be directed at
242 * Modified to use shp_nav by Ken Stevens 1995
245 nav_ship(struct shpstr *sp)
250 int didsomething = 0;
252 struct emp_qelem ship_list;
253 struct emp_qelem *qp, *newqp;
260 /* just return if no autonaving to do for this ship */
261 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
266 /* Make a list of one ships so we can use the navi.c code */
267 emp_initque(&ship_list);
268 mlp = malloc(sizeof(struct ulist));
269 mlp->chrp = (struct empobj_chr *)(mchr + sp->shp_type);
270 mlp->unit.ship = *sp;
271 mlp->mobil = sp->shp_mobil;
272 emp_insque(&mlp->queue, &ship_list);
274 quit = 1; /* setup loop, we want to check it 1 time. */
277 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
278 (!(sp->shp_autonav & AN_STANDBY))) {
279 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
281 if (QEMPTY(&ship_list))
284 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
285 sp->shp_destx[0], sp->shp_desty[0],
289 "%s bad path, ship put on standby\n", prship(sp));
290 sp->shp_autonav |= AN_STANDBY;
291 putship(sp->shp_uid, sp);
293 /* We need to free the ship list */
294 qp = ship_list.q_forw;
295 while (qp != &ship_list) {
305 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
307 stopping |= shp_nav_one_sector(&ship_list, dir,
313 nav_check_atdest(sp);
316 quit = 0; /* stop loop */
318 /* Try to load the ship */
319 if (sp->shp_autonav & AN_LOADING) {
320 didsomething = nav_loadship(sp, cnum);
324 /* special case for fishing boats and oil derricks */
325 if (nav_load_ship_at_sea(sp))
327 /* reset flag and check if we can move. */
329 } while (quit); /* end loop */
331 putship(sp->shp_uid, sp);
333 /* We need to free the ship list (just in case) */
334 qp = ship_list.q_forw;
335 while (qp != &ship_list) {