2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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;
162 int i, landown, shipown, level, didsomething[TMAX], rel;
164 for (i = 0; i < TMAX; i++)
167 /* Turn off the loading flag.
168 * if any of the loads fail on the ship
169 * it will be turned back on.
172 sp->shp_autonav &= ~AN_LOADING;
174 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
175 return RET_SYS; /* safety */
176 /* I suspect RET_SYS isn't really what you want here --dfp */
179 landown = sectp->sct_own;
180 shipown = sp->shp_own;
181 rel = getrel(getnatp(sectp->sct_own), cnum);
183 /* loop through each field for that ship */
184 for (i = 0; i < TMAX; ++i) {
185 item = sp->shp_tend[i]; /* commodity */
186 level = sp->shp_lend[i]; /* amount */
188 /* check and see if the data fields have been set. */
190 if (item == ' ' || level == 0) {
191 /* nothing to do move on. */
196 /* either sea or deity harbor */
200 if (sectp->sct_type != SCT_HARBR &&
201 (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
202 /* we can only load in harbors */
206 if (landown == shipown || rel >= FRIENDLY)
207 didsomething[i] = load_it(sp, sectp, i);
210 /* check for any unsucessful loads */
211 /* if we have any return 0 to stop */
212 /* the nav_ship loop. */
214 for (i = 0; i < TMAX; i++) {
215 if (didsomething[i] == 0)
218 /* All loads were succesful */
224 * 1. Try and move to the next sector/harbor given by the player.
225 * 2. Once we reach a harbor try and load all cargo holds for that ship.
226 * 3. If the ship reaches its max levels set by the player try to use
227 * up all mobility getting to the next harbor.
228 * Continue to loop until the ship runs out of mobility, a load fails,
229 * the ship gets sunk (forts,ect..), the ship hits a mine.
231 * A check has been added for fuel so ships don't end up running
232 * out of mobility in the ocean.
234 * Questions, bugs (fixes) , or new ideas should be directed at
237 * Modified to use shp_nav by Ken Stevens 1995
240 nav_ship(register struct shpstr *sp)
242 struct sctstr *sectp;
246 int didsomething = 0;
247 int max_amt, food_amt;
249 struct emp_qelem ship_list;
250 struct emp_qelem *qp, *newqp;
256 struct mchrstr *mcp, *vship;
258 /* just return if no autonaving to do for this ship */
259 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
263 vship = mcp = &mchr[(int)sp->shp_type];
265 /* Make a list of one ships so we can use the navi.c code */
266 emp_initque(&ship_list);
267 mlp = (struct mlist *)malloc(sizeof(struct mlist));
268 mlp->mcp = mchr + sp->shp_type;
270 mlp->mobil = (double)sp->shp_mobil;
271 emp_insque(&mlp->queue, &ship_list);
273 quit = 1; /* setup loop, we want to check it 1 time. */
276 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
277 (!(sp->shp_autonav & AN_STANDBY))) {
278 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
280 if (QEMPTY(&ship_list))
282 /* before we move check to see if ship needs fuel. */
283 sectp = getsectp(sp->shp_x, sp->shp_y);
285 sectp->sct_own != 0 &&
286 sp->shp_fuel <= 0 && mlp->mcp->m_fuelu != 0)
288 mlp->ship.shp_fuel = sp->shp_fuel;
290 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
291 sp->shp_destx[0], sp->shp_desty[0],
293 if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
295 "%s bad path, ship put on standby\n", prship(sp));
296 sp->shp_autonav |= AN_STANDBY;
297 putship(sp->shp_uid, (s_char *)sp);
299 /* We need to free the ship list */
300 qp = ship_list.q_forw;
301 while (qp != &(ship_list)) {
311 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
312 dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
314 stopping |= shp_nav_one_sector(&ship_list, dir,
318 /* sp->shp_mobil = (int) mobil;
322 nav_check_atdest(sp);
325 quit = 0; /* stop loop */
327 /* Try to load the ship */
328 if (sp->shp_autonav & AN_LOADING) {
329 didsomething = nav_loadship(sp, cnum);
333 /* special case for fishing boats */
334 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
335 food_amt = sp->shp_item[I_FOOD];
336 max_amt = vl_find(V_FOOD, vship->m_vtype,
337 vship->m_vamt, vship->m_nv);
338 sectp = getsectp(sp->shp_x, sp->shp_y);
340 if (food_amt < max_amt && (sectp->sct_own == 0))
343 /* reset flag and check if we can move. */
345 } while (quit); /* end loop */
347 putship(sp->shp_uid, sp);
349 /* We need to free the ship list (just in case) */
350 qp = ship_list.q_forw;
351 while (qp != &(ship_list)) {