2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * nav_ship.c: Navigate ships and such
30 * Known contributors to this file:
55 static void swap(register struct shpstr *);
58 scuttle_it(register struct shpstr *sp)
62 sp->shp_autonav &= ~AN_SCUTTLE;
63 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
64 wu(0, 0, "bad sector (%d,%d) ship %d\n", sp->shp_x, sp->shp_y,
68 if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
70 "%s is not in a harbor at least 2%% eff! Not scuttling.\n",
75 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
76 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
80 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
81 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
83 scuttle_tradeship(sp, 0);
89 nav_check_atdest(register struct shpstr *sp)
91 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
92 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
93 (sp->shp_desty[0] == sp->shp_desty[1])) {
97 sp->shp_autonav &= ~AN_AUTONAV;
98 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
99 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
100 if (sp->shp_autonav & AN_SCUTTLE) {
104 /* unload all cargo */
106 wu(0, sp->shp_own, "%s arrived at %s\n",
107 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
112 sp->shp_autonav &= ~AN_LOADING;
115 /* flip the 2 fields that deal with autonav movement. */
119 swap(register struct shpstr *sp)
126 tcord = sp->shp_destx[0];
127 sp->shp_destx[0] = sp->shp_destx[1];
128 sp->shp_destx[1] = tcord;
129 tcord = sp->shp_desty[0];
130 sp->shp_desty[0] = sp->shp_desty[1];
131 sp->shp_desty[1] = tcord;
133 for (i = 0; i < TMAX; ++i) {
134 lev[i] = sp->shp_lstart[i];
135 tcomm[i] = sp->shp_tstart[i];
138 for (i = 0; i < TMAX; ++i) {
139 sp->shp_lstart[i] = sp->shp_lend[i];
140 sp->shp_tstart[i] = sp->shp_tend[i];
143 for (i = 0; i < TMAX; ++i) {
144 sp->shp_lend[i] = lev[i];
145 sp->shp_tend[i] = tcomm[i];
149 sp->shp_autonav |= AN_LOADING;
158 nav_loadship(register struct shpstr *sp, natid cnum)
160 struct sctstr *sectp;
161 int i, landown, shipown, didsomething[TMAX], rel;
163 for (i = 0; i < TMAX; i++)
166 /* Turn off the loading flag.
167 * if any of the loads fail on the ship
168 * it will be turned back on.
171 sp->shp_autonav &= ~AN_LOADING;
173 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
174 return RET_SYS; /* safety */
175 /* I suspect RET_SYS isn't really what you want here --dfp */
178 landown = sectp->sct_own;
179 shipown = sp->shp_own;
180 rel = getrel(getnatp(sectp->sct_own), cnum);
182 /* loop through each field for that ship */
183 for (i = 0; i < TMAX; ++i) {
184 /* check and see if the data fields have been set. */
186 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
187 /* nothing to do move on. */
192 /* either sea or deity harbor */
196 if (sectp->sct_type != SCT_HARBR &&
197 (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
198 /* we can only load in harbors */
202 if (landown == shipown || rel >= FRIENDLY)
203 didsomething[i] = load_it(sp, sectp, i);
206 /* check for any unsucessful loads */
207 /* if we have any return 0 to stop */
208 /* the nav_ship loop. */
210 for (i = 0; i < TMAX; i++) {
211 if (didsomething[i] == 0)
214 /* All loads were succesful */
220 * 1. Try and move to the next sector/harbor given by the player.
221 * 2. Once we reach a harbor try and load all cargo holds for that ship.
222 * 3. If the ship reaches its max levels set by the player try to use
223 * up all mobility getting to the next harbor.
224 * Continue to loop until the ship runs out of mobility, a load fails,
225 * the ship gets sunk (forts,ect..), the ship hits a mine.
227 * A check has been added for fuel so ships don't end up running
228 * out of mobility in the ocean.
230 * Questions, bugs (fixes) , or new ideas should be directed at
233 * Modified to use shp_nav by Ken Stevens 1995
236 nav_ship(register struct shpstr *sp)
238 struct sctstr *sectp;
242 int didsomething = 0;
243 int max_amt, food_amt;
245 struct emp_qelem ship_list;
246 struct emp_qelem *qp, *newqp;
252 struct mchrstr *mcp, *vship;
254 /* just return if no autonaving to do for this ship */
255 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
259 vship = mcp = &mchr[(int)sp->shp_type];
261 /* Make a list of one ships so we can use the navi.c code */
262 emp_initque(&ship_list);
263 mlp = (struct mlist *)malloc(sizeof(struct mlist));
264 mlp->mcp = mchr + sp->shp_type;
266 mlp->mobil = (double)sp->shp_mobil;
267 emp_insque(&mlp->queue, &ship_list);
269 quit = 1; /* setup loop, we want to check it 1 time. */
272 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
273 (!(sp->shp_autonav & AN_STANDBY))) {
274 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
276 if (QEMPTY(&ship_list))
278 /* before we move check to see if ship needs fuel. */
279 sectp = getsectp(sp->shp_x, sp->shp_y);
281 sectp->sct_own != 0 &&
282 sp->shp_fuel <= 0 && mlp->mcp->m_fuelu != 0)
284 mlp->ship.shp_fuel = sp->shp_fuel;
286 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
287 sp->shp_destx[0], sp->shp_desty[0],
289 if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
291 "%s bad path, ship put on standby\n", prship(sp));
292 sp->shp_autonav |= AN_STANDBY;
293 putship(sp->shp_uid, (s_char *)sp);
295 /* We need to free the ship list */
296 qp = ship_list.q_forw;
297 while (qp != &(ship_list)) {
307 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
308 dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
310 stopping |= shp_nav_one_sector(&ship_list, dir,
314 /* sp->shp_mobil = (int) mobil;
318 nav_check_atdest(sp);
321 quit = 0; /* stop loop */
323 /* Try to load the ship */
324 if (sp->shp_autonav & AN_LOADING) {
325 didsomething = nav_loadship(sp, cnum);
329 /* special case for fishing boats */
330 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
331 food_amt = sp->shp_item[I_FOOD];
332 max_amt = vship->m_item[I_FOOD];
333 sectp = getsectp(sp->shp_x, sp->shp_y);
335 if (food_amt < max_amt && (sectp->sct_own == 0))
338 /* reset flag and check if we can move. */
340 } while (quit); /* end loop */
342 putship(sp->shp_uid, sp);
344 /* We need to free the ship list (just in case) */
345 qp = ship_list.q_forw;
346 while (qp != &(ship_list)) {