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:
56 extern int check_nav(struct sctstr *sect);
59 scuttle_it(register struct shpstr *sp)
63 sp->shp_autonav &= ~AN_SCUTTLE;
64 if (!(sectp = getsectp(sp->shp_x, sp->shp_y))) {
65 wu(0, 0, "bad sector (%d,%d) ship %d\n", sp->shp_x, sp->shp_y,
69 if (sectp->sct_type != SCT_HARBR || sectp->sct_effic < 2) {
71 "%s is not in a harbor at least 2%% eff! Not scuttling.\n",
76 if (!(mchr[(int)sp->shp_type].m_flags & M_TRADE)) {
77 wu(0, sp->shp_own, "You can only autoscuttle trade ships!\n");
81 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
82 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
84 scuttle_tradeship(sp, 0);
90 nav_check_atdest(register struct shpstr *sp, struct mchrstr *mcp)
92 if ((sp->shp_x == sp->shp_destx[0]) && (sp->shp_y == sp->shp_desty[0])) {
93 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
94 (sp->shp_desty[0] == sp->shp_desty[1])) {
98 sp->shp_autonav &= ~AN_AUTONAV;
99 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
100 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
101 if (sp->shp_autonav & AN_SCUTTLE) {
105 /* unload all cargo */
107 wu(0, sp->shp_own, "%s arrived at %s\n",
108 prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
113 sp->shp_autonav &= ~AN_LOADING;
116 /* flip the 2 fields that deal with autonav movement. */
120 swap(register struct shpstr *sp)
127 tcord = sp->shp_destx[0];
128 sp->shp_destx[0] = sp->shp_destx[1];
129 sp->shp_destx[1] = tcord;
130 tcord = sp->shp_desty[0];
131 sp->shp_desty[0] = sp->shp_desty[1];
132 sp->shp_desty[1] = tcord;
134 for (i = 0; i < TMAX; ++i) {
135 lev[i] = sp->shp_lstart[i];
136 tcomm[i] = sp->shp_tstart[i];
139 for (i = 0; i < TMAX; ++i) {
140 sp->shp_lstart[i] = sp->shp_lend[i];
141 sp->shp_tstart[i] = sp->shp_tend[i];
144 for (i = 0; i < TMAX; ++i) {
145 sp->shp_lend[i] = lev[i];
146 sp->shp_tend[i] = tcomm[i];
150 sp->shp_autonav |= AN_LOADING;
159 nav_loadship(register struct shpstr *sp, natid cnum)
161 struct sctstr *sectp;
163 int i, landown, shipown, level, didsomething[TMAX], rel;
165 for (i = 0; i < TMAX; i++)
168 /* Turn off the loading flag.
169 * if any of the loads fail on the ship
170 * it will be turned back on.
173 sp->shp_autonav &= ~AN_LOADING;
175 if (!(sectp = getsectp(sp->shp_x, sp->shp_y)))
176 return RET_SYS; /* safety */
177 /* I suspect RET_SYS isn't really what you want here --dfp */
180 landown = sectp->sct_own;
181 shipown = sp->shp_own;
182 rel = getrel(getnatp(sectp->sct_own), cnum);
184 /* loop through each field for that ship */
185 for (i = 0; i < TMAX; ++i) {
186 item = sp->shp_tend[i]; /* commodity */
187 level = sp->shp_lend[i]; /* amount */
189 /* check and see if the data fields have been set. */
191 if (item == ' ' || level == 0) {
192 /* nothing to do move on. */
197 /* either sea or deity harbor */
201 if (sectp->sct_type != SCT_HARBR &&
202 (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
203 /* we can only load in harbors */
207 if (landown == shipown || rel >= FRIENDLY)
208 didsomething[i] = load_it(sp, sectp, i);
211 /* check for any unsucessful loads */
212 /* if we have any return 0 to stop */
213 /* the nav_ship loop. */
215 for (i = 0; i < TMAX; i++) {
216 if (didsomething[i] == 0)
219 /* All loads were succesful */
225 * 1. Try and move to the next sector/harbor given by the player.
226 * 2. Once we reach a harbor try and load all cargo holds for that ship.
227 * 3. If the ship reaches its max levels set by the player try to use
228 * up all mobility getting to the next harbor.
229 * Continue to loop until the ship runs out of mobility, a load fails,
230 * the ship gets sunk (forts,ect..), the ship hits a mine.
232 * A check has been added for fuel so ships don't end up running
233 * out of mobility in the ocean.
235 * Questions, bugs (fixes) , or new ideas should be directed at
238 * Modified to use shp_nav by Ken Stevens 1995
241 nav_ship(register struct shpstr *sp)
243 extern double techfact(int, double);
244 struct sctstr *sectp;
248 int didsomething = 0;
249 int max_amt, food_amt, comm;
251 struct emp_qelem ship_list;
252 struct emp_qelem *qp, *newqp;
258 struct mchrstr *mcp, *vship;
260 /* just return if no autonaving to do for this ship */
261 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
265 vship = mcp = &mchr[(int)sp->shp_type];
267 /* Make a list of one ships so we can use the navi.c code */
268 emp_initque(&ship_list);
269 mlp = (struct mlist *)malloc(sizeof(struct mlist));
270 mlp->mcp = mchr + sp->shp_type;
271 bcopy((s_char *)sp, (s_char *)&mlp->ship, sizeof(struct shpstr));
272 mlp->mobil = (double)sp->shp_mobil;
273 emp_insque(&mlp->queue, &ship_list);
275 quit = 1; /* setup loop, we want to check it 1 time. */
278 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
279 (!(sp->shp_autonav & AN_STANDBY))) {
280 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
282 if (QEMPTY(&ship_list))
284 /* before we move check to see if ship needs fuel. */
285 sectp = getsectp(sp->shp_x, sp->shp_y);
287 sectp->sct_own != 0 &&
288 sp->shp_fuel <= 0 && mlp->mcp->m_fuelu != 0)
290 mlp->ship.shp_fuel = sp->shp_fuel;
292 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
293 sp->shp_destx[0], sp->shp_desty[0],
295 if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
297 "%s bad path, ship put on standby\n", prship(sp));
298 sp->shp_autonav |= AN_STANDBY;
299 putship(sp->shp_uid, (s_char *)sp);
301 /* We need to free the ship list */
302 qp = ship_list.q_forw;
303 while (qp != &(ship_list)) {
313 while (*cp && !stopping && sp->shp_own && mlp->mobil > 0.0) {
314 dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
316 stopping |= shp_nav_one_sector(&ship_list, dir,
320 /* sp->shp_mobil = (int) mobil;
324 nav_check_atdest(sp, mcp);
327 quit = 0; /* stop loop */
329 /* Try to load the ship */
330 if (sp->shp_autonav & AN_LOADING) {
331 didsomething = nav_loadship(sp, cnum);
335 /* special case for fishing boats */
336 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
338 comm = com_num(&item);
339 food_amt = getvar(comm, (s_char *)sp, EF_SHIP);
340 max_amt = (vl_find(comm, vship->m_vtype,
341 vship->m_vamt, (int)vship->m_nv));
342 sectp = getsectp(sp->shp_x, sp->shp_y);
344 if (food_amt < max_amt && (sectp->sct_own == 0))
347 /* reset flag and check if we can move. */
349 } while (quit); /* end loop */
351 putship(sp->shp_uid, sp);
353 /* We need to free the ship list (just in case) */
354 qp = ship_list.q_forw;
355 while (qp != &(ship_list)) {