2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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]))
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 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
130 if (!sarg_xy(p, &p0x, &p0y))
138 p = getstarg(player->argp[4], "Second dest? ", buf);
139 if (!p || !*p || !strcmp(p, "-")) {
141 pr("A one-way order has been accepted.\n");
142 } else if (!strncmp(p, "s", 1)) {
143 if (opt_TRADESHIPS) {
144 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
145 pr("You can't auto-scuttle that ship!\n");
149 pr("You can't auto-scuttle that ship!\n");
152 pr("A scuttle order has been accepted.\n");
155 if (!sarg_xy(p, &p1x, &p1y))
157 pr("A circular order has been accepted.\n");
162 * Set new destination and trade type fields.
164 ship.shp_mission = 0;
165 ship.shp_destx[1] = p1x;
166 ship.shp_desty[1] = p1y;
167 ship.shp_destx[0] = p0x;
168 ship.shp_desty[0] = p0y;
170 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
171 ship.shp_autonav |= AN_AUTONAV;
174 ship.shp_autonav |= AN_SCUTTLE;
177 /* set cargo levels on the ship */
180 /* convert player->argp[3] to an integer */
182 sub = atoi(player->argp[3]);
184 sprintf(buf1, "Field (1-%d) ", TMAX);
185 if (getstarg(player->argp[3], buf1, buf) == 0)
189 /* check to make sure value in within range. */
190 if (sub > TMAX || sub < 1) {
191 pr("Value must range from 1 to %d\n", TMAX);
195 /* to keep sub in range of our arrays
196 subtract 1 so the new range is 0-(TMAX-1)
200 if (ship.shp_autonav & AN_AUTONAV) {
202 dest = getstarg(player->argp[4], "Start or End? ", buf);
203 if (orders) { /* before dest check */
208 pr("You must enter 'start' or 'end'\n");
212 i1 = whatitem(player->argp[5], "Commodity? ");
216 p1 = getstarg(player->argp[6], "Amount? ",
223 level = 0; /* prevent negatives. */
224 pr("You must use positive number! Level set to 0.\n");
226 ship.shp_tstart[sub] = i1->i_uid;
227 ship.shp_lstart[sub] = level;
232 i1 = whatitem(player->argp[5], "Commodity? ");
236 p1 = getstarg(player->argp[6], "Amount? ",
244 pr("You must use positive number! Level set to 0.\n");
246 ship.shp_tend[sub] = i1->i_uid;
247 ship.shp_lend[sub] = level;
253 pr("You need to 'declare' a ship path first, see 'info order'\n");
256 } /* end of switch (*p) */
261 * Set loading flag if ship is already in one
262 * of the specified harbors and a cargo has been
266 if (((ship.shp_x == ship.shp_destx[0])
267 && (ship.shp_y == ship.shp_desty[0])
268 && (ship.shp_lstart[0] != ' '))
269 || ((ship.shp_x == ship.shp_desty[1])
270 && (ship.shp_y == ship.shp_desty[1])
271 && (ship.shp_lstart[1] != ' '))) {
278 ship.shp_autonav |= AN_LOADING;
280 /* swap variables, this keeps
281 the load_it() procedure happy. CZ
283 tcord = ship.shp_destx[0];
284 ship.shp_destx[0] = ship.shp_destx[1];
285 ship.shp_destx[1] = tcord;
286 tcord = ship.shp_desty[0];
287 ship.shp_desty[0] = ship.shp_desty[1];
288 ship.shp_desty[1] = tcord;
290 for (i = 0; i < TMAX; i++) {
291 lev[i] = ship.shp_lstart[i];
292 ship.shp_lstart[i] = ship.shp_lend[i];
293 ship.shp_lend[i] = lev[i];
294 tcomm = ship.shp_tstart[i];
295 ship.shp_tstart[i] = ship.shp_tend[i];
296 ship.shp_tend[i] = tcomm;
300 ** Write ship back to database, then give it
301 ** a kick down the autonav route if necessary.
305 /* Now do a sanity check. */
306 if (!check_ship_ok(&start))
309 putship(ship.shp_uid, &ship);
315 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
317 double mobcost, mobil;
324 mobcost = shp_mobcost(sp);
325 mobil = sp->shp_mobil;
331 mobil += (ship_mob_scale * (float)etu_per_update);
338 prhold(int hold, i_type itype, int amt)
340 if (itype != I_NONE && amt != 0) {
341 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
344 pr("%c", ichr[itype].i_mnem);
358 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
360 while (nxtitem(&nb, (&ship))) {
361 if (!player->owner || ship.shp_own == 0)
363 if (!(ship.shp_autonav & AN_AUTONAV)
364 && (!opt_SAIL || !ship.shp_path[0]))
367 if (!nships) { /* 1st ship, print banner */
370 pr("shp# ship type ");
371 pr("[Starting] (Ending) \n");
375 pr("%3d ", ship.shp_own);
377 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
379 if (ship.shp_autonav & AN_AUTONAV) {
381 for (i = 0; i < TMAX; i++)
382 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
384 for (i = 0; i < TMAX; i++)
385 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
387 if (ship.shp_autonav & AN_SCUTTLE)
391 pr(" has a sail path\n");
393 if (ship.shp_name[0] != 0) {
396 pr(" %s\n", ship.shp_name);
401 pr("%s: No ship(s)\n", player->argp[1]);
403 pr("%s: No ship(s)\n", "");
406 pr("%d ship%s\n", nships, splur(nships));
420 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
422 while (nxtitem(&nb, (&ship))) {
423 if (!player->owner || ship.shp_own == 0)
425 if (!(ship.shp_autonav & AN_AUTONAV)
426 && (!opt_SAIL || !ship.shp_path[0]))
429 if (!nships) { /* 1st ship, print banner */
432 pr("shp# ship type x,y ");
438 pr("%3d ", ship.shp_own);
440 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
441 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
443 if (ship.shp_autonav & AN_AUTONAV) {
446 ship.shp_destx[1], ship.shp_desty[1], player->cnum);
449 if ((ship.shp_destx[1] != ship.shp_destx[0])
450 || (ship.shp_desty[1] != ship.shp_desty[0])) {
452 ship.shp_destx[0], ship.shp_desty[0], player->cnum);
456 if (ship.shp_autonav & AN_STANDBY)
458 else if (ship.shp_autonav & AN_LOADING)
461 /* ETA calculation */
463 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
464 ship.shp_destx[0], ship.shp_desty[0],
467 pr(" no route possible");
471 /* distance to destination */
472 eta_calc(&ship, c, &len, &updates);
473 pr(" %3d %4d", len, updates);
476 if (ship.shp_autonav & AN_SCUTTLE)
480 pr(" has a sail path\n");
482 if (ship.shp_name[0] != 0) {
485 pr(" %s\n", ship.shp_name);
490 pr("%s: No ship(s)\n", player->argp[1]);
492 pr("%s: No ship(s)\n", "");
495 pr("%d ship%s\n", nships, splur(nships));