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
63 int orders, sub, level;
67 struct shpstr start; /* Used for checking database */
69 coord p0x, p0y, p1x, p1y;
76 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
78 while (!player->aborted && nxtitem(&nb, (&ship))) {
79 if (!player->owner || ship.shp_own == 0)
84 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
88 memcpy(&start, &ship, sizeof(struct shpstr));
90 "Ship #%d, declare, cancel, suspend, resume, level? ",
92 p = getstarg(player->argp[2], prompt, buf);
93 if (player->aborted || !p)
103 pr("Bad order type!\n");
105 case 'c': /* clear ship fields */
106 ship.shp_mission = 0;
107 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
108 for (i = 0; i < TMAX; i++) {
109 ship.shp_tstart[i] = I_NONE;
110 ship.shp_tend[i] = I_NONE;
111 ship.shp_lstart[i] = 0;
112 ship.shp_lend[i] = 0;
115 case 's': /* suspend ship movement */
116 ship.shp_mission = 0;
117 ship.shp_autonav |= AN_STANDBY;
119 case 'r': /* resume ship movement */
120 ship.shp_mission = 0;
121 ship.shp_autonav &= ~AN_STANDBY;
123 case 'd': /* declare path */
127 p = getstarg(player->argp[3], "Destination? ", buf);
130 if (!sarg_xy(p, &p0x, &p0y))
138 p = getstarg(player->argp[4], "Second dest? ", buf);
141 if (!*p || !strcmp(p, "-")) {
143 pr("A one-way order has been accepted.\n");
144 } else if (!strncmp(p, "s", 1)) {
145 if (opt_TRADESHIPS) {
146 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
147 pr("You can't auto-scuttle that ship!\n");
151 pr("You can't auto-scuttle that ship!\n");
154 pr("A scuttle order has been accepted.\n");
157 if (!sarg_xy(p, &p1x, &p1y))
159 pr("A circular order has been accepted.\n");
164 * Set new destination and trade type fields.
166 ship.shp_mission = 0;
167 ship.shp_destx[1] = p1x;
168 ship.shp_desty[1] = p1y;
169 ship.shp_destx[0] = p0x;
170 ship.shp_desty[0] = p0y;
172 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
173 ship.shp_autonav |= AN_AUTONAV;
176 ship.shp_autonav |= AN_SCUTTLE;
179 /* set cargo levels on the ship */
182 /* convert player->argp[3] to an integer */
184 sub = atoi(player->argp[3]);
186 sprintf(buf1, "Field (1-%d) ", TMAX);
187 if (!getstarg(player->argp[3], buf1, buf))
191 /* check to make sure value in within range. */
192 if (sub > TMAX || sub < 1) {
193 pr("Value must range from 1 to %d\n", TMAX);
197 /* to keep sub in range of our arrays
198 subtract 1 so the new range is 0-(TMAX-1)
202 if (ship.shp_autonav & AN_AUTONAV) {
204 dest = getstarg(player->argp[4], "Start or End? ", buf);
205 if (orders) { /* before dest check */
210 pr("You must enter 'start' or 'end'\n");
214 i1 = whatitem(player->argp[5], "Commodity? ");
218 p1 = getstarg(player->argp[6], "Amount? ",
225 level = 0; /* prevent negatives. */
226 pr("You must use positive number! Level set to 0.\n");
228 ship.shp_tstart[sub] = i1->i_uid;
229 ship.shp_lstart[sub] = level;
234 i1 = whatitem(player->argp[5], "Commodity? ");
238 p1 = getstarg(player->argp[6], "Amount? ",
246 pr("You must use positive number! Level set to 0.\n");
248 ship.shp_tend[sub] = i1->i_uid;
249 ship.shp_lend[sub] = level;
255 pr("You need to 'declare' a ship path first, see 'info order'\n");
258 } /* end of switch (*p) */
263 * Set loading flag if ship is already in one
264 * of the specified harbors and a cargo has been
268 if (((ship.shp_x == ship.shp_destx[0])
269 && (ship.shp_y == ship.shp_desty[0])
270 && (ship.shp_lstart[0] != ' '))
271 || ((ship.shp_x == ship.shp_desty[1])
272 && (ship.shp_y == ship.shp_desty[1])
273 && (ship.shp_lstart[1] != ' '))) {
280 ship.shp_autonav |= AN_LOADING;
282 /* swap variables, this keeps
283 the load_it() procedure happy. CZ
285 tcord = ship.shp_destx[0];
286 ship.shp_destx[0] = ship.shp_destx[1];
287 ship.shp_destx[1] = tcord;
288 tcord = ship.shp_desty[0];
289 ship.shp_desty[0] = ship.shp_desty[1];
290 ship.shp_desty[1] = tcord;
292 for (i = 0; i < TMAX; i++) {
293 lev[i] = ship.shp_lstart[i];
294 ship.shp_lstart[i] = ship.shp_lend[i];
295 ship.shp_lend[i] = lev[i];
296 tcomm = ship.shp_tstart[i];
297 ship.shp_tstart[i] = ship.shp_tend[i];
298 ship.shp_tend[i] = tcomm;
302 ** Write ship back to database, then give it
303 ** a kick down the autonav route if necessary.
307 /* Now do a sanity check. */
308 if (!check_ship_ok(&start))
311 putship(ship.shp_uid, &ship);
317 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
319 double mobcost, mobil;
326 mobcost = shp_mobcost(sp);
327 mobil = sp->shp_mobil;
333 mobil += (ship_mob_scale * (float)etu_per_update);
340 prhold(int hold, i_type itype, int amt)
342 if (itype != I_NONE && amt != 0) {
343 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
346 pr("%c", ichr[itype].i_mnem);
360 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
362 while (nxtitem(&nb, (&ship))) {
363 if (!player->owner || ship.shp_own == 0)
365 if (!(ship.shp_autonav & AN_AUTONAV)
366 && (!opt_SAIL || !ship.shp_path[0]))
369 if (!nships) { /* 1st ship, print banner */
372 pr("shp# ship type ");
373 pr("[Starting] (Ending) \n");
377 pr("%3d ", ship.shp_own);
379 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
381 if (ship.shp_autonav & AN_AUTONAV) {
383 for (i = 0; i < TMAX; i++)
384 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
386 for (i = 0; i < TMAX; i++)
387 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
389 if (ship.shp_autonav & AN_SCUTTLE)
393 pr(" has a sail path\n");
395 if (ship.shp_name[0] != 0) {
398 pr(" %s\n", ship.shp_name);
403 pr("%s: No ship(s)\n", player->argp[1]);
405 pr("%s: No ship(s)\n", "");
408 pr("%d ship%s\n", nships, splur(nships));
422 if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
424 while (nxtitem(&nb, (&ship))) {
425 if (!player->owner || ship.shp_own == 0)
427 if (!(ship.shp_autonav & AN_AUTONAV)
428 && (!opt_SAIL || !ship.shp_path[0]))
431 if (!nships) { /* 1st ship, print banner */
434 pr("shp# ship type x,y ");
440 pr("%3d ", ship.shp_own);
442 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
443 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
445 if (ship.shp_autonav & AN_AUTONAV) {
448 ship.shp_destx[1], ship.shp_desty[1], player->cnum);
451 if ((ship.shp_destx[1] != ship.shp_destx[0])
452 || (ship.shp_desty[1] != ship.shp_desty[0])) {
454 ship.shp_destx[0], ship.shp_desty[0], player->cnum);
458 if (ship.shp_autonav & AN_STANDBY)
460 else if (ship.shp_autonav & AN_LOADING)
463 /* ETA calculation */
465 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
466 ship.shp_destx[0], ship.shp_desty[0],
469 pr(" no route possible");
473 /* distance to destination */
474 eta_calc(&ship, c, &len, &updates);
475 pr(" %3d %4d", len, updates);
478 if (ship.shp_autonav & AN_SCUTTLE)
482 pr(" has a sail path\n");
484 if (ship.shp_name[0] != 0) {
487 pr(" %s\n", ship.shp_name);
492 pr("%s: No ship(s)\n", player->argp[1]);
494 pr("%s: No ship(s)\n", "");
497 pr("%d ship%s\n", nships, splur(nships));