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)) {
78 "You can only autoscuttle trade ships!\n");
82 wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
84 xyas(sp->shp_x, sp->shp_y, sp->shp_own));
86 scuttle_tradeship(sp, 0);
92 nav_check_atdest(register struct shpstr *sp, struct mchrstr *mcp)
94 if ((sp->shp_x == sp->shp_destx[0]) &&
95 (sp->shp_y == sp->shp_desty[0])) {
96 if ((sp->shp_destx[0] == sp->shp_destx[1]) &&
97 (sp->shp_desty[0] == sp->shp_desty[1])) {
101 sp->shp_autonav &= ~AN_AUTONAV;
102 wu(0, sp->shp_own, "%s arrived at %s, finished\n",
104 xyas(sp->shp_x,sp->shp_y,sp->shp_own));
105 if (sp->shp_autonav & AN_SCUTTLE) {
109 /* unload all cargo */
111 wu(0, sp->shp_own, "%s arrived at %s\n",
113 xyas(sp->shp_x,sp->shp_y,sp->shp_own));
118 sp->shp_autonav &= ~AN_LOADING;
121 /* flip the 2 fields that deal with autonav movement. */
125 swap(register struct shpstr *sp)
132 tcord = sp->shp_destx[0];
133 sp->shp_destx[0] = sp->shp_destx[1];
134 sp->shp_destx[1] = tcord;
135 tcord = sp->shp_desty[0];
136 sp->shp_desty[0] = sp->shp_desty[1];
137 sp->shp_desty[1] = tcord;
139 for (i=0;i<TMAX;++i){
140 lev[i] = sp->shp_lstart[i];
141 tcomm[i] = sp->shp_tstart[i]; }
143 for (i=0;i<TMAX;++i) {
144 sp->shp_lstart[i] = sp->shp_lend[i];
145 sp->shp_tstart[i] = sp->shp_tend[i]; }
147 for (i=0;i<TMAX;++i) {
148 sp->shp_lend[i] = lev[i];
149 sp->shp_tend[i] = tcomm[i]; }
152 sp->shp_autonav |= AN_LOADING;
161 nav_loadship(register struct shpstr *sp, natid cnum)
163 struct sctstr *sectp;
175 /* Turn off the loading flag.
176 * if any of the loads fail on the ship
177 * it will be turned back on.
180 sp->shp_autonav &= ~AN_LOADING;
182 if (!(sectp = getsectp(sp->shp_x,sp->shp_y)))
183 return RET_SYS; /* safety */
184 /* I suspect RET_SYS isn't really what you want here --dfp */
187 landown = sectp->sct_own;
188 shipown = sp->shp_own;
189 rel = getrel(getnatp(sectp->sct_own),cnum);
191 /* loop through each field for that ship */
192 for (i=0;i<TMAX;++i) {
193 item = sp->shp_tend[i]; /* commodity */
194 level = sp->shp_lend[i]; /* amount */
196 /* check and see if the data fields have been set. */
198 if (item == ' ' || level == 0) {
199 /* nothing to do move on. */
204 /* either sea or deity harbor */
208 if (sectp->sct_type != SCT_HARBR &&
209 (!opt_BIG_CITY || sectp->sct_type != SCT_CAPIT)) {
210 /* we can only load in harbors */
214 if (landown == shipown || rel >= FRIENDLY)
215 didsomething[i] = load_it(sp,sectp,i);
218 /* check for any unsucessful loads */
219 /* if we have any return 0 to stop */
220 /* the nav_ship loop. */
222 for (i=0;i<TMAX;i++) {
223 if(didsomething[i] == 0)
226 /* All loads were succesful */
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 * A check has been added for fuel so ships don't end up running
240 * out of mobility in the ocean.
242 * Questions, bugs (fixes) , or new ideas should be directed at
245 * Modified to use shp_nav by Ken Stevens 1995
248 nav_ship(register struct shpstr *sp)
250 extern double techfact(int, double);
251 struct sctstr *sectp;
255 int didsomething = 0;
256 int max_amt, food_amt, comm;
258 struct emp_qelem ship_list;
259 struct emp_qelem *qp, *newqp;
265 struct mchrstr *mcp, *vship;
267 /* just return if no autonaving to do for this ship */
268 if (!(sp->shp_autonav & AN_AUTONAV) || (sp->shp_autonav & AN_STANDBY))
272 vship = mcp = &mchr[(int)sp->shp_type];
274 /* Make a list of one ships so we can use the navi.c code */
275 emp_initque(&ship_list);
276 mlp = (struct mlist *) malloc(sizeof(struct mlist));
277 mlp->mcp = mchr + sp->shp_type;
278 bcopy((s_char *)sp, (s_char *)&mlp->ship, sizeof(struct shpstr));
279 mlp->mobil = (double)sp->shp_mobil;
280 emp_insque(&mlp->queue, &ship_list);
282 quit = 1; /* setup loop, we want to check it 1 time. */
285 if ((sp->shp_mobil > 0) && (!(sp->shp_autonav & AN_LOADING)) &&
286 (!(sp->shp_autonav & AN_STANDBY))) {
287 shp_nav(&ship_list, &dummydouble, &dummydouble, &dummyint,
289 if (QEMPTY(&ship_list))
291 /* before we move check to see if ship needs fuel. */
292 sectp = getsectp(sp->shp_x,sp->shp_y);
294 sectp->sct_own != 0 &&
296 mlp->mcp->m_fuelu != 0)
298 mlp->ship.shp_fuel = sp->shp_fuel;
300 cp = BestShipPath(buf, sp->shp_x, sp->shp_y,
301 sp->shp_destx[0], sp->shp_desty[0],
303 if (cp == 0 || (*cp == '\0') || (*cp == '?')) {
305 "%s bad path, ship put on standby\n",
307 sp->shp_autonav |= AN_STANDBY;
308 putship(sp->shp_uid, (s_char *)sp);
310 /* We need to free the ship list */
311 qp = ship_list.q_forw;
312 while (qp != &(ship_list)) {
322 while (*cp && !stopping && sp->shp_own &&
324 dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
326 stopping |= shp_nav_one_sector(&ship_list, dir,
330 /* sp->shp_mobil = (int) mobil;
334 nav_check_atdest(sp, mcp);
337 quit = 0; /* stop loop */
339 /* Try to load the ship */
340 if (sp->shp_autonav & AN_LOADING) {
341 didsomething = nav_loadship(sp, cnum);
345 /* special case for fishing boats */
346 if ((mchr[(int)sp->shp_type].m_flags & M_FOOD) == 1) {
348 comm = com_num(&item);
349 food_amt=getvar(comm,(s_char *) sp, EF_SHIP);
350 max_amt=(vl_find(comm,vship->m_vtype,
353 sectp=getsectp(sp->shp_x,sp->shp_y);
355 if (food_amt<max_amt && (sectp->sct_own==0))
358 /* reset flag and check if we can move. */
360 } while (quit); /* end loop */
362 putship(sp->shp_uid, sp);
364 /* We need to free the ship list (just in case) */
365 qp = ship_list.q_forw;
366 while (qp != &(ship_list)) {