2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * orde.c: Turn on/off autonavigation
30 * Known contributors to this file:
46 * ORDER <ship> Show orders
47 * ORDER <ship> c[ancel] Cancel orders
48 * ORDER <ship> s[top] Suspend orders
49 * ORDER <ship> r[esume] Resume orders
50 * ORDER <ship> d[eclare] <dest1> Set destination
51 * d[eclare] <dest1> <dest2>
52 * ORDER <ship> l[evel] <field> <start/end> <comm> <level>
55 * qorder <ship> display cargo levels
56 * sorder <ship> display statistical info
66 struct shpstr start; /* Used for checking database */
68 coord p0x, p0y, p1x, p1y;
75 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
77 while (!player->aborted && nxtitem(&nb, (&ship))) {
78 if (!player->owner || ship.shp_own == 0)
82 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
86 memcpy(&start, &ship, sizeof(struct shpstr));
88 "Ship #%d, declare, cancel, suspend, resume, level? ",
90 p = getstarg(player->argp[2], prompt, buf);
91 if (player->aborted || !p || !*p)
95 pr("Bad order type!\n");
97 case 'c': /* clear ship fields */
99 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
100 for (i = 0; i < TMAX; i++) {
101 ship.shp_tstart[i] = I_NONE;
102 ship.shp_tend[i] = I_NONE;
103 ship.shp_lstart[i] = 0;
104 ship.shp_lend[i] = 0;
107 case 's': /* suspend ship movement */
108 ship.shp_mission = 0;
109 ship.shp_autonav |= AN_STANDBY;
111 case 'r': /* resume ship movement */
112 ship.shp_mission = 0;
113 ship.shp_autonav &= ~AN_STANDBY;
115 case 'd': /* declare path */
118 p = getstarg(player->argp[3], "Destination? ", buf);
121 if (!sarg_xy(p, &p0x, &p0y))
126 p = getstarg(player->argp[4], "Second dest? ", buf);
129 if (!*p || !strcmp(p, "-")) {
130 pr("A one-way order has been accepted.\n");
131 } else if (!strncmp(p, "s", 1)) {
132 if (opt_TRADESHIPS) {
133 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
134 pr("You can't auto-scuttle that ship!\n");
138 pr("You can't auto-scuttle that ship!\n");
141 pr("A scuttle order has been accepted.\n");
144 if (!sarg_xy(p, &p1x, &p1y))
146 pr("A circular order has been accepted.\n");
150 * Set new destination and trade type fields.
152 ship.shp_mission = 0;
153 ship.shp_destx[1] = p1x;
154 ship.shp_desty[1] = p1y;
155 ship.shp_destx[0] = p0x;
156 ship.shp_desty[0] = p0y;
158 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
159 ship.shp_autonav |= AN_AUTONAV;
162 ship.shp_autonav |= AN_SCUTTLE;
165 /* set cargo levels on the ship */
168 /* convert player->argp[3] to an integer */
170 sub = atoi(player->argp[3]);
172 sprintf(buf1, "Field (1-%d) ", TMAX);
173 if (!getstarg(player->argp[3], buf1, buf))
177 /* check to make sure value in within range. */
178 if (sub > TMAX || sub < 1) {
179 pr("Value must range from 1 to %d\n", TMAX);
183 /* to keep sub in range of our arrays
184 subtract 1 so the new range is 0-(TMAX-1)
188 if (ship.shp_autonav & AN_AUTONAV) {
189 dest = getstarg(player->argp[4], "Start or End? ", buf);
194 pr("You must enter 'start' or 'end'\n");
198 i1 = whatitem(player->argp[5], "Commodity? ");
201 p1 = getstarg(player->argp[6], "Amount? ",
207 level = 0; /* prevent negatives. */
208 pr("You must use positive number! Level set to 0.\n");
210 ship.shp_tstart[sub] = i1->i_uid;
211 ship.shp_lstart[sub] = level;
216 i1 = whatitem(player->argp[5], "Commodity? ");
219 p1 = getstarg(player->argp[6], "Amount? ",
226 pr("You must use positive number! Level set to 0.\n");
228 ship.shp_tend[sub] = i1->i_uid;
229 ship.shp_lend[sub] = level;
234 pr("You need to 'declare' a ship path first, see 'info order'\n");
237 } /* end of switch (*p) */
242 * Set loading flag if ship is already in one
243 * of the specified harbors and a cargo has been
247 if (((ship.shp_x == ship.shp_destx[0])
248 && (ship.shp_y == ship.shp_desty[0])
249 && (ship.shp_lstart[0] != ' '))
250 || ((ship.shp_x == ship.shp_desty[1])
251 && (ship.shp_y == ship.shp_desty[1])
252 && (ship.shp_lstart[1] != ' '))) {
259 ship.shp_autonav |= AN_LOADING;
261 /* swap variables, this keeps
262 the load_it() procedure happy. CZ
264 tcord = ship.shp_destx[0];
265 ship.shp_destx[0] = ship.shp_destx[1];
266 ship.shp_destx[1] = tcord;
267 tcord = ship.shp_desty[0];
268 ship.shp_desty[0] = ship.shp_desty[1];
269 ship.shp_desty[1] = tcord;
271 for (i = 0; i < TMAX; i++) {
272 lev[i] = ship.shp_lstart[i];
273 ship.shp_lstart[i] = ship.shp_lend[i];
274 ship.shp_lend[i] = lev[i];
275 tcomm = ship.shp_tstart[i];
276 ship.shp_tstart[i] = ship.shp_tend[i];
277 ship.shp_tend[i] = tcomm;
281 ** Write ship back to database, then give it
282 ** a kick down the autonav route if necessary.
286 /* Now do a sanity check. */
287 if (!check_ship_ok(&start))
290 putship(ship.shp_uid, &ship);
296 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
298 double mobcost, mobil;
305 mobcost = shp_mobcost(sp);
306 mobil = sp->shp_mobil;
312 mobil += (ship_mob_scale * (float)etu_per_update);
319 prhold(int hold, i_type itype, int amt)
321 if (itype != I_NONE && amt != 0) {
322 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
325 pr("%c", ichr[itype].i_mnem);
339 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
341 while (nxtitem(&nb, (&ship))) {
342 if (!player->owner || ship.shp_own == 0)
344 if (!(ship.shp_autonav & AN_AUTONAV)
345 && (!opt_SAIL || !ship.shp_path[0]))
348 if (!nships) { /* 1st ship, print banner */
351 pr("shp# ship type ");
352 pr("[Starting] (Ending) \n");
356 pr("%3d ", ship.shp_own);
358 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
360 if (ship.shp_autonav & AN_AUTONAV) {
362 for (i = 0; i < TMAX; i++)
363 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
365 for (i = 0; i < TMAX; i++)
366 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
368 if (ship.shp_autonav & AN_SCUTTLE)
372 pr(" has a sail path\n");
374 if (ship.shp_name[0] != 0) {
377 pr(" %s\n", ship.shp_name);
382 pr("%s: No ship(s)\n", player->argp[1]);
384 pr("%s: No ship(s)\n", "");
387 pr("%d ship%s\n", nships, splur(nships));
401 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
403 while (nxtitem(&nb, (&ship))) {
404 if (!player->owner || ship.shp_own == 0)
406 if (!(ship.shp_autonav & AN_AUTONAV)
407 && (!opt_SAIL || !ship.shp_path[0]))
410 if (!nships) { /* 1st ship, print banner */
413 pr("shp# ship type x,y ");
419 pr("%3d ", ship.shp_own);
421 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
422 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
424 if (ship.shp_autonav & AN_AUTONAV) {
427 ship.shp_destx[1], ship.shp_desty[1], player->cnum);
430 if ((ship.shp_destx[1] != ship.shp_destx[0])
431 || (ship.shp_desty[1] != ship.shp_desty[0])) {
433 ship.shp_destx[0], ship.shp_desty[0], player->cnum);
437 if (ship.shp_autonav & AN_STANDBY)
439 else if (ship.shp_autonav & AN_LOADING)
442 /* ETA calculation */
444 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
445 ship.shp_destx[0], ship.shp_desty[0],
448 pr(" no route possible");
452 /* distance to destination */
453 eta_calc(&ship, c, &len, &updates);
454 pr(" %3d %4d", len, updates);
457 if (ship.shp_autonav & AN_SCUTTLE)
461 pr(" has a sail path\n");
463 if (ship.shp_name[0] != 0) {
466 pr(" %s\n", ship.shp_name);
471 pr("%s: No ship(s)\n", player->argp[1]);
473 pr("%s: No ship(s)\n", "");
476 pr("%d ship%s\n", nships, splur(nships));