2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
53 static void swap(struct shpstr *);
56 scuttle_it(struct shpstr *sp)
60 sp->shp_autonav &= ~AN_SCUTTLE;
61 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
62 wu(0, 0, "bad sector (%d,%d) ship %d\n", sp->shp_x, sp->shp_y,
66 if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
68 "%s is not in a harbor at least 2%% eff! Not scuttling.\n",
73 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
74 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
78 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
79 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
81 scuttle_tradeship(sp, 0);
87 nav_check_atdest(struct shpstr *sp)
89 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
90 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
91 (sp->shp_desty[0] == sp->shp_desty[1])) {
95 sp->shp_autonav &= ~AN_AUTONAV;
96 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
97 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
98 if (sp->shp_autonav & AN_SCUTTLE) {
102 /* unload all cargo */
104 wu(0, sp->shp_own, "%s arrived at %s\n",
105 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
110 sp->shp_autonav &= ~AN_LOADING;
113 /* flip the 2 fields that deal with autonav movement. */
117 swap(struct shpstr *sp)
124 tcord = sp->shp_destx[0];
125 sp->shp_destx[0] = sp->shp_destx[1];
126 sp->shp_destx[1] = tcord;
127 tcord = sp->shp_desty[0];
128 sp->shp_desty[0] = sp->shp_desty[1];
129 sp->shp_desty[1] = tcord;
131 for (i = 0; i < TMAX; ++i) {
132 lev[i] = sp->shp_lstart[i];
133 tcomm[i] = sp->shp_tstart[i];
136 for (i = 0; i < TMAX; ++i) {
137 sp->shp_lstart[i] = sp->shp_lend[i];
138 sp->shp_tstart[i] = sp->shp_tend[i];
141 for (i = 0; i < TMAX; ++i) {
142 sp->shp_lend[i] = lev[i];
143 sp->shp_tend[i] = tcomm[i];
147 sp->shp_autonav |= AN_LOADING;
156 nav_loadship(struct shpstr *sp, natid cnum)
158 struct sctstr *sectp;
159 int i, landown, shipown, didsomething[TMAX], rel;
161 for (i = 0; i < TMAX; i++)
164 /* Turn off the loading flag.
165 * if any of the loads fail on the ship
166 * it will be turned back on.
169 sp->shp_autonav &= ~AN_LOADING;
171 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
172 return RET_SYS; /* safety */
173 /* I suspect RET_SYS isn't really what you want here --dfp */
176 landown = sectp->sct_own;
177 shipown = sp->shp_own;
178 rel = getrel(getnatp(sectp->sct_own), cnum);
180 /* loop through each field for that ship */
181 for (i = 0; i < TMAX; ++i) {
182 /* check and see if the data fields have been set. */
184 if (sp->shp_tend[i] == I_NONE || sp->shp_lend[i] == 0) {
185 /* nothing to do move on. */
190 /* either sea or deity harbor */
194 if (sectp->sct_type != SCT_HARBR &&
195 (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
196 /* we can only load in harbors */
200 if (landown == shipown || rel >= FRIENDLY)
201 didsomething[i] = load_it(sp, sectp, i);
204 /* check for any unsucessful loads */
205 /* if we have any return 0 to stop */
206 /* the nav_ship loop. */
208 for (i = 0; i < TMAX; i++) {
209 if (didsomething[i] == 0)
212 /* All loads were succesful */
218 * 1. Try and move to the next sector/harbor given by the player.
219 * 2. Once we reach a harbor try and load all cargo holds for that ship.
220 * 3. If the ship reaches its max levels set by the player try to use
221 * up all mobility getting to the next harbor.
222 * Continue to loop until the ship runs out of mobility, a load fails,
223 * the ship gets sunk (forts,ect..), the ship hits a mine.
225 * A check has been added for fuel so ships don't end up running
226 * out of mobility in the ocean.
228 * Questions, bugs (fixes) , or new ideas should be directed at
231 * Modified to use shp_nav by Ken Stevens 1995
234 nav_ship(struct shpstr *sp)
236 struct sctstr *sectp;
240 int didsomething = 0;
241 int max_amt, food_amt;
243 struct emp_qelem ship_list;
244 struct emp_qelem *qp, *newqp;
250 struct mchrstr *mcp, *vship;
252 /* just return if no autonaving to do for this ship */
253 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
257 vship = mcp = &mchr[(int)sp->shp_type];
259 /* Make a list of one ships so we can use the navi.c code */
260 emp_initque(&ship_list);
261 mlp = malloc(sizeof(struct mlist));
262 mlp->mcp = mchr + sp->shp_type;
264 mlp->mobil = (double)sp->shp_mobil;
265 emp_insque(&mlp->queue, &ship_list);
267 quit = 1; /* setup loop, we want to check it 1 time. */
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))
276 /* before we move check to see if ship needs fuel. */
277 sectp = getsectp(sp->shp_x, sp->shp_y);
279 sectp->sct_own != 0 &&
280 sp->shp_fuel <= 0 && mlp->mcp->m_fuelu != 0)
282 mlp->ship.shp_fuel = sp->shp_fuel;
284 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
285 sp->shp_destx[0], sp->shp_desty[0],
287 if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
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 */
325 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
326 food_amt = sp->shp_item[I_FOOD];
327 max_amt = vship->m_item[I_FOOD];
328 sectp = getsectp(sp->shp_x, sp->shp_y);
330 if (food_amt < max_amt && (sectp->sct_own == 0))
333 /* reset flag and check if we can move. */
335 } while (quit); /* end loop */
337 putship(sp->shp_uid, sp);
339 /* We need to free the ship list (just in case) */
340 qp = ship_list.q_forw;
341 while (qp != &(ship_list)) {