2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 * orde.c: Turn on/off autonavigation
30 * Known contributors to this file:
55 ** ORDER <ship> Show orders
56 ** ORDER <ship> c[ancel] Cancel orders
57 ** ORDER <ship> s[top] Suspend orders
58 ** ORDER <ship> r[esume] Resume orders
59 ** ORDER <ship> d[eclare] <dest1> Set destination
60 ** d[eclare] <dest1> <dest2>
61 ** ORDER <ship> l[evel] <field> <start/end> <comm> <level>
65 ** qorder <ship> display cargo levels
66 ** sorder <ship> display statistical info
73 int orders, sub, level;
77 struct shpstr start; /* Used for checking database */
79 coord p0x, p0y, p1x, p1y;
81 s_char *p, *p1, *dest;
86 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
88 while (!player->aborted && nxtitem(&nb, (&ship))) {
89 if (!player->owner || ship.shp_own == 0)
94 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
98 memcpy(&start, &ship, sizeof(struct shpstr));
100 "Ship #%d, declare, cancel, suspend, resume, level? ",
102 p = getstarg(player->argp[2], prompt, buf);
103 if (player->aborted || !p)
113 pr("Bad order type!\n");
115 case 'c': /* clear ship fields */
116 ship.shp_mission = 0;
117 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
118 for (i = 0; i < TMAX; i++) {
119 ship.shp_tstart[i] = I_NONE;
120 ship.shp_tend[i] = I_NONE;
121 ship.shp_lstart[i] = 0;
122 ship.shp_lend[i] = 0;
125 case 's': /* suspend ship movement */
126 ship.shp_mission = 0;
127 ship.shp_autonav |= AN_STANDBY;
129 case 'r': /* resume ship movement */
130 ship.shp_mission = 0;
131 ship.shp_autonav &= ~AN_STANDBY;
133 case 'd': /* declare path */
137 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
140 if (!sarg_xy(p, &p0x, &p0y))
148 p = getstarg(player->argp[4], "Second dest? ", buf);
149 if (!p || !*p || !strcmp(p, "-")) {
151 pr("A one-way order has been accepted.\n");
152 } else if (!strncmp(p, "s", 1)) {
153 if (opt_TRADESHIPS) {
154 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
155 pr("You can't auto-scuttle that ship!\n");
159 pr("You can't auto-scuttle that ship!\n");
162 pr("A scuttle order has been accepted.\n");
165 if (!sarg_xy(p, &p1x, &p1y))
167 pr("A circular order has been accepted.\n");
172 * Set new destination and trade type fields.
174 ship.shp_mission = 0;
175 ship.shp_destx[1] = p1x;
176 ship.shp_desty[1] = p1y;
177 ship.shp_destx[0] = p0x;
178 ship.shp_desty[0] = p0y;
180 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
181 ship.shp_autonav |= AN_AUTONAV;
184 ship.shp_autonav |= AN_SCUTTLE;
187 /* set cargo levels on the ship */
190 /* convert player->argp[3] to an integer */
192 sub = atoi(player->argp[3]);
194 sprintf(buf1, "Field (1-%d) ", TMAX);
195 if (getstarg(player->argp[3], buf1, buf) == 0)
199 /* check to make sure value in within range. */
200 if (sub > TMAX || sub < 1) {
201 pr("Value must range from 1 to %d\n", TMAX);
205 /* to keep sub in range of our arrays
206 subtract 1 so the new range is 0-(TMAX-1)
210 if (ship.shp_autonav & AN_AUTONAV) {
212 dest = getstarg(player->argp[4], "Start or End? ", buf);
213 if (orders) { /* before dest check */
218 pr("You must enter 'start' or 'end'\n");
222 i1 = whatitem(player->argp[5], "Commodity? ");
226 p1 = getstarg(player->argp[6], "Amount? ",
233 level = 0; /* prevent negatives. */
234 pr("You must use positive number! Level set to 0.\n");
236 ship.shp_tstart[sub] = i1->i_vtype;
237 ship.shp_lstart[sub] = level;
242 i1 = whatitem(player->argp[5], "Commodity? ");
246 p1 = getstarg(player->argp[6], "Amount? ",
254 pr("You must use positive number! Level set to 0.\n");
256 ship.shp_tend[sub] = i1->i_vtype;
257 ship.shp_lend[sub] = level;
263 pr("You need to 'declare' a ship path first, see 'info order'\n");
266 } /* end of switch (*p) */
271 * Set loading flag if ship is already in one
272 * of the specified harbors and a cargo has been
276 if (((ship.shp_x == ship.shp_destx[0])
277 && (ship.shp_y == ship.shp_desty[0])
278 && (ship.shp_lstart[0] != ' '))
279 || ((ship.shp_x == ship.shp_desty[1])
280 && (ship.shp_y == ship.shp_desty[1])
281 && (ship.shp_lstart[1] != ' '))) {
288 ship.shp_autonav |= AN_LOADING;
290 /* swap variables, this keeps
291 the load_it() procedure happy. CZ
293 tcord = ship.shp_destx[0];
294 ship.shp_destx[0] = ship.shp_destx[1];
295 ship.shp_destx[1] = tcord;
296 tcord = ship.shp_desty[0];
297 ship.shp_desty[0] = ship.shp_desty[1];
298 ship.shp_desty[1] = tcord;
300 for (i = 0; i < TMAX; i++) {
301 lev[i] = ship.shp_lstart[i];
302 ship.shp_lstart[i] = ship.shp_lend[i];
303 ship.shp_lend[i] = lev[i];
304 tcomm = ship.shp_tstart[i];
305 ship.shp_tstart[i] = ship.shp_tend[i];
306 ship.shp_tend[i] = tcomm;
310 ** Write ship back to database, then give it
311 ** a kick down the autonav route if necessary.
315 /* Now do a sanity check. */
316 if (!check_ship_ok(&start))
319 putship(ship.shp_uid, &ship);
325 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
327 double mobcost, mobil;
334 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
335 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
336 mobil = sp->shp_mobil;
342 mobil += (ship_mob_scale * (float)etu_per_update);
349 prhold(int hold, i_type itype, int amt)
351 if (itype != I_NONE && amt != 0) {
352 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
355 pr("%c", ichr[itype].i_mnem);
369 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
371 while (nxtitem(&nb, (&ship))) {
372 if (!player->owner || ship.shp_own == 0)
374 if (!(ship.shp_autonav & AN_AUTONAV)
375 && (!opt_SAIL || !ship.shp_path[0]))
378 if (!nships) { /* 1st ship, print banner */
381 pr("shp# ship type ");
382 pr("[Starting] (Ending) \n");
386 pr("%3d ", ship.shp_own);
388 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
390 if (ship.shp_autonav & AN_AUTONAV) {
392 for (i = 0; i < TMAX; i++)
393 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
395 for (i = 0; i < TMAX; i++)
396 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
398 if (ship.shp_autonav & AN_SCUTTLE)
402 pr(" has a sail path\n");
404 if (ship.shp_name[0] != 0) {
407 pr(" %s\n", ship.shp_name);
412 pr("%s: No ship(s)\n", player->argp[1]);
414 pr("%s: No ship(s)\n", "");
417 pr("%d ship%s\n", nships, splur(nships));
431 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
433 while (nxtitem(&nb, (&ship))) {
434 if (!player->owner || ship.shp_own == 0)
436 if (!(ship.shp_autonav & AN_AUTONAV)
437 && (!opt_SAIL || !ship.shp_path[0]))
440 if (!nships) { /* 1st ship, print banner */
443 pr("shp# ship type x,y ");
449 pr("%3d ", ship.shp_own);
451 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
452 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
454 if (ship.shp_autonav & AN_AUTONAV) {
456 prxy(" %3d,%-3d", ship.shp_destx[1],
457 ship.shp_desty[1], player->cnum);
460 if ((ship.shp_destx[1] != ship.shp_destx[0])
461 || (ship.shp_desty[1] != ship.shp_desty[0])) {
462 prxy(" %3d,%-3d", ship.shp_destx[0],
463 ship.shp_desty[0], player->cnum);
467 if (ship.shp_autonav & AN_STANDBY)
469 else if (ship.shp_autonav & AN_LOADING)
472 /* ETA calculation */
474 c = BestShipPath(buf, ship.shp_x,
475 ship.shp_y, ship.shp_destx[0],
476 ship.shp_desty[0], ship.shp_own);
478 pr(" no route possible");
482 pr(" route too long");
484 /* distance to destination */
485 eta_calc(&ship, c, &len, &updates);
486 pr(" %3d %4d", len, updates);
489 if (ship.shp_autonav & AN_SCUTTLE)
493 pr(" has a sail path\n");
495 if (ship.shp_name[0] != 0) {
498 pr(" %s\n", ship.shp_name);
503 pr("%s: No ship(s)\n", player->argp[1]);
505 pr("%s: No ship(s)\n", "");
508 pr("%d ship%s\n", nships, splur(nships));