2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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:
47 * ORDER <ship> Show orders
48 * ORDER <ship> c[ancel] Cancel orders
49 * ORDER <ship> s[top] Suspend orders
50 * ORDER <ship> r[esume] Resume orders
51 * ORDER <ship> d[eclare] <dest1> Set destination
52 * d[eclare] <dest1> <dest2>
53 * ORDER <ship> l[evel] <field> <start/end> <comm> <level>
56 * qorder <ship> display cargo levels
57 * sorder <ship> display statistical info
64 int orders, sub, level;
68 struct shpstr start; /* Used for checking database */
70 coord p0x, p0y, p1x, p1y;
77 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
79 while (!player->aborted && nxtitem(&nb, (&ship))) {
80 if (!player->owner || ship.shp_own == 0)
85 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
89 memcpy(&start, &ship, sizeof(struct shpstr));
91 "Ship #%d, declare, cancel, suspend, resume, level? ",
93 p = getstarg(player->argp[2], prompt, buf);
94 if (player->aborted || !p)
104 pr("Bad order type!\n");
106 case 'c': /* clear ship fields */
107 ship.shp_mission = 0;
108 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
109 for (i = 0; i < TMAX; i++) {
110 ship.shp_tstart[i] = I_NONE;
111 ship.shp_tend[i] = I_NONE;
112 ship.shp_lstart[i] = 0;
113 ship.shp_lend[i] = 0;
116 case 's': /* suspend ship movement */
117 ship.shp_mission = 0;
118 ship.shp_autonav |= AN_STANDBY;
120 case 'r': /* resume ship movement */
121 ship.shp_mission = 0;
122 ship.shp_autonav &= ~AN_STANDBY;
124 case 'd': /* declare path */
128 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
131 if (!sarg_xy(p, &p0x, &p0y))
139 p = getstarg(player->argp[4], "Second dest? ", buf);
140 if (!p || !*p || !strcmp(p, "-")) {
142 pr("A one-way order has been accepted.\n");
143 } else if (!strncmp(p, "s", 1)) {
144 if (opt_TRADESHIPS) {
145 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
146 pr("You can't auto-scuttle that ship!\n");
150 pr("You can't auto-scuttle that ship!\n");
153 pr("A scuttle order has been accepted.\n");
156 if (!sarg_xy(p, &p1x, &p1y))
158 pr("A circular order has been accepted.\n");
163 * Set new destination and trade type fields.
165 ship.shp_mission = 0;
166 ship.shp_destx[1] = p1x;
167 ship.shp_desty[1] = p1y;
168 ship.shp_destx[0] = p0x;
169 ship.shp_desty[0] = p0y;
171 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
172 ship.shp_autonav |= AN_AUTONAV;
175 ship.shp_autonav |= AN_SCUTTLE;
178 /* set cargo levels on the ship */
181 /* convert player->argp[3] to an integer */
183 sub = atoi(player->argp[3]);
185 sprintf(buf1, "Field (1-%d) ", TMAX);
186 if (getstarg(player->argp[3], buf1, buf) == 0)
190 /* check to make sure value in within range. */
191 if (sub > TMAX || sub < 1) {
192 pr("Value must range from 1 to %d\n", TMAX);
196 /* to keep sub in range of our arrays
197 subtract 1 so the new range is 0-(TMAX-1)
201 if (ship.shp_autonav & AN_AUTONAV) {
203 dest = getstarg(player->argp[4], "Start or End? ", buf);
204 if (orders) { /* before dest check */
209 pr("You must enter 'start' or 'end'\n");
213 i1 = whatitem(player->argp[5], "Commodity? ");
217 p1 = getstarg(player->argp[6], "Amount? ",
224 level = 0; /* prevent negatives. */
225 pr("You must use positive number! Level set to 0.\n");
227 ship.shp_tstart[sub] = i1->i_uid;
228 ship.shp_lstart[sub] = level;
233 i1 = whatitem(player->argp[5], "Commodity? ");
237 p1 = getstarg(player->argp[6], "Amount? ",
245 pr("You must use positive number! Level set to 0.\n");
247 ship.shp_tend[sub] = i1->i_uid;
248 ship.shp_lend[sub] = level;
254 pr("You need to 'declare' a ship path first, see 'info order'\n");
257 } /* end of switch (*p) */
262 * Set loading flag if ship is already in one
263 * of the specified harbors and a cargo has been
267 if (((ship.shp_x == ship.shp_destx[0])
268 && (ship.shp_y == ship.shp_desty[0])
269 && (ship.shp_lstart[0] != ' '))
270 || ((ship.shp_x == ship.shp_desty[1])
271 && (ship.shp_y == ship.shp_desty[1])
272 && (ship.shp_lstart[1] != ' '))) {
279 ship.shp_autonav |= AN_LOADING;
281 /* swap variables, this keeps
282 the load_it() procedure happy. CZ
284 tcord = ship.shp_destx[0];
285 ship.shp_destx[0] = ship.shp_destx[1];
286 ship.shp_destx[1] = tcord;
287 tcord = ship.shp_desty[0];
288 ship.shp_desty[0] = ship.shp_desty[1];
289 ship.shp_desty[1] = tcord;
291 for (i = 0; i < TMAX; i++) {
292 lev[i] = ship.shp_lstart[i];
293 ship.shp_lstart[i] = ship.shp_lend[i];
294 ship.shp_lend[i] = lev[i];
295 tcomm = ship.shp_tstart[i];
296 ship.shp_tstart[i] = ship.shp_tend[i];
297 ship.shp_tend[i] = tcomm;
301 ** Write ship back to database, then give it
302 ** a kick down the autonav route if necessary.
306 /* Now do a sanity check. */
307 if (!check_ship_ok(&start))
310 putship(ship.shp_uid, &ship);
316 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
318 double mobcost, mobil;
325 mobcost = shp_mobcost(sp);
326 mobil = sp->shp_mobil;
332 mobil += (ship_mob_scale * (float)etu_per_update);
339 prhold(int hold, i_type itype, int amt)
341 if (itype != I_NONE && amt != 0) {
342 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
345 pr("%c", ichr[itype].i_mnem);
359 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
361 while (nxtitem(&nb, (&ship))) {
362 if (!player->owner || ship.shp_own == 0)
364 if (!(ship.shp_autonav & AN_AUTONAV)
365 && (!opt_SAIL || !ship.shp_path[0]))
368 if (!nships) { /* 1st ship, print banner */
371 pr("shp# ship type ");
372 pr("[Starting] (Ending) \n");
376 pr("%3d ", ship.shp_own);
378 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
380 if (ship.shp_autonav & AN_AUTONAV) {
382 for (i = 0; i < TMAX; i++)
383 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
385 for (i = 0; i < TMAX; i++)
386 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
388 if (ship.shp_autonav & AN_SCUTTLE)
392 pr(" has a sail path\n");
394 if (ship.shp_name[0] != 0) {
397 pr(" %s\n", ship.shp_name);
402 pr("%s: No ship(s)\n", player->argp[1]);
404 pr("%s: No ship(s)\n", "");
407 pr("%d ship%s\n", nships, splur(nships));
421 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
423 while (nxtitem(&nb, (&ship))) {
424 if (!player->owner || ship.shp_own == 0)
426 if (!(ship.shp_autonav & AN_AUTONAV)
427 && (!opt_SAIL || !ship.shp_path[0]))
430 if (!nships) { /* 1st ship, print banner */
433 pr("shp# ship type x,y ");
439 pr("%3d ", ship.shp_own);
441 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
442 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
444 if (ship.shp_autonav & AN_AUTONAV) {
447 ship.shp_destx[1], ship.shp_desty[1], player->cnum);
450 if ((ship.shp_destx[1] != ship.shp_destx[0])
451 || (ship.shp_desty[1] != ship.shp_desty[0])) {
453 ship.shp_destx[0], ship.shp_desty[0], player->cnum);
457 if (ship.shp_autonav & AN_STANDBY)
459 else if (ship.shp_autonav & AN_LOADING)
462 /* ETA calculation */
464 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
465 ship.shp_destx[0], ship.shp_desty[0],
468 pr(" no route possible");
472 /* distance to destination */
473 eta_calc(&ship, c, &len, &updates);
474 pr(" %3d %4d", len, updates);
477 if (ship.shp_autonav & AN_SCUTTLE)
481 pr(" has a sail path\n");
483 if (ship.shp_name[0] != 0) {
486 pr(" %s\n", ship.shp_name);
491 pr("%s: No ship(s)\n", player->argp[1]);
493 pr("%s: No ship(s)\n", "");
496 pr("%d ship%s\n", nships, splur(nships));