2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
74 int orders, sub, level;
78 struct shpstr start; /* Used for checking database */
80 coord p0x, p0y, p1x, p1y;
82 s_char *p, *p1, *dest;
87 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
89 while (!player->aborted && nxtitem(&nb, (s_char *)(&ship))) {
90 if (!player->owner || ship.shp_own == 0)
92 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
93 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
99 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
103 memcpy(&start, &ship, sizeof(struct shpstr));
105 "Ship #%d, declare, cancel, suspend, resume, level? ",
107 p = getstarg(player->argp[2], prompt, buf);
108 if (player->aborted || !p)
118 pr("Bad order type!\n");
120 case 'c': /* clear ship fields */
121 ship.shp_mission = 0;
122 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
123 for (i = 0; i < TMAX; i++) {
124 ship.shp_tstart[i] = I_NONE;
125 ship.shp_tend[i] = I_NONE;
126 ship.shp_lstart[i] = 0;
127 ship.shp_lend[i] = 0;
130 case 's': /* suspend ship movement */
131 ship.shp_mission = 0;
132 ship.shp_autonav |= AN_STANDBY;
134 case 'r': /* resume ship movement */
135 ship.shp_mission = 0;
136 ship.shp_autonav &= ~AN_STANDBY;
138 case 'd': /* declare path */
142 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
145 if (!sarg_xy(p, &p0x, &p0y))
153 p = getstarg(player->argp[4], "Second dest? ", buf);
154 if (!p || !*p || !strcmp(p, "-")) {
156 pr("A one-way order has been accepted.\n");
157 } else if (!strncmp(p, "s", 1)) {
158 if (opt_TRADESHIPS) {
159 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
160 pr("You can't auto-scuttle that ship!\n");
164 pr("You can't auto-scuttle that ship!\n");
167 pr("A scuttle order has been accepted.\n");
170 if (!sarg_xy(p, &p1x, &p1y))
172 pr("A circular order has been accepted.\n");
177 * Set new destination and trade type fields.
179 ship.shp_mission = 0;
180 ship.shp_destx[1] = p1x;
181 ship.shp_desty[1] = p1y;
182 ship.shp_destx[0] = p0x;
183 ship.shp_desty[0] = p0y;
185 ship.shp_autonav &= ~(AN_STANDBY + AN_SAILDIR + AN_LOADING);
186 ship.shp_autonav |= AN_AUTONAV;
189 ship.shp_autonav |= AN_SCUTTLE;
192 /* set cargo levels on the ship */
195 /* convert player->argp[3] to an integer */
197 sub = atoi(player->argp[3]);
199 sprintf(buf1, "Field (1-%d) ", TMAX);
200 if (getstarg(player->argp[3], buf1, buf) == 0)
204 /* check to make sure value in within range. */
205 if (sub > TMAX || sub < 1) {
206 pr("Value must range from 1 to %d\n", TMAX);
210 /* to keep sub in range of our arrays
211 subtract 1 so the new range is 0-(TMAX-1)
215 if (ship.shp_autonav & AN_AUTONAV) {
217 dest = getstarg(player->argp[4], "Start or End? ", buf);
218 if (orders) { /* before dest check */
223 pr("You must enter 'start' or 'end'\n");
227 i1 = whatitem(player->argp[5], "Commodity? ");
231 p1 = getstarg(player->argp[6], "Amount? ",
238 level = 0; /* prevent negatives. */
239 pr("You must use positive number! Level set to 0.\n");
241 ship.shp_tstart[sub] = (s_char)i1->i_vtype;
242 ship.shp_lstart[sub] = level;
247 i1 = whatitem(player->argp[5], "Commodity? ");
251 p1 = getstarg(player->argp[6], "Amount? ",
259 pr("You must use positive number! Level set to 0.\n");
261 ship.shp_tend[sub] = (s_char)i1->i_vtype;
262 ship.shp_lend[sub] = level;
268 pr("You need to 'declare' a ship path first, see 'info order'\n");
271 } /* end of switch (*p) */
276 * Set loading flag if ship is already in one
277 * of the specified harbors and a cargo has been
281 if (((ship.shp_x == ship.shp_destx[0])
282 && (ship.shp_y == ship.shp_desty[0])
283 && (ship.shp_lstart[0] != ' '))
284 || ((ship.shp_x == ship.shp_desty[1])
285 && (ship.shp_y == ship.shp_desty[1])
286 && (ship.shp_lstart[1] != ' '))) {
293 ship.shp_autonav |= AN_LOADING;
295 /* swap variables, this keeps
296 the load_it() procedure happy. CZ
298 tcord = ship.shp_destx[0];
299 ship.shp_destx[0] = ship.shp_destx[1];
300 ship.shp_destx[1] = tcord;
301 tcord = ship.shp_desty[0];
302 ship.shp_desty[0] = ship.shp_desty[1];
303 ship.shp_desty[1] = tcord;
305 for (i = 0; i < TMAX; i++) {
306 lev[i] = ship.shp_lstart[i];
307 ship.shp_lstart[i] = ship.shp_lend[i];
308 ship.shp_lend[i] = lev[i];
309 tcomm = ship.shp_tstart[i];
310 ship.shp_tstart[i] = ship.shp_tend[i];
311 ship.shp_tend[i] = tcomm;
315 ** Write ship back to database, then give it
316 ** a kick down the autonav route if necessary.
320 /* Now do a sanity check. */
321 if (!check_ship_ok(&start))
324 putship(ship.shp_uid, &ship);
331 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
334 double mobcost, mobil;
341 mcp = &mchr[(int)sp->shp_type];
342 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
343 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
344 mobil = sp->shp_mobil;
350 mobil += (ship_mob_scale * (float)etu_per_update);
357 prhold(int hold, int itype, int amt)
359 if (itype != I_NONE && amt != 0) {
360 if (CANT_HAPPEN((unsigned)itype > I_MAX))
363 pr("%c", ichr[itype].i_mnem);
377 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
379 while (nxtitem(&nb, (s_char *)(&ship))) {
380 if (!player->owner || ship.shp_own == 0)
382 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
383 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
387 if ((ship.shp_autonav & AN_AUTONAV) ||
388 (ship.shp_path[0] && opt_SAIL)) {
389 if (!nships) { /* 1st ship, print banner */
392 pr("shp# ship type ");
393 pr("[Starting] (Ending) \n");
397 pr("%3d ", ship.shp_own);
399 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
401 if (ship.shp_autonav & AN_AUTONAV) {
404 for (i = 0; i < TMAX; i++)
405 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
407 for (i = 0; i < TMAX; i++)
408 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
410 if (ship.shp_autonav & AN_SCUTTLE)
416 if ((ship.shp_autonav & AN_AUTONAV) ||
417 (ship.shp_path[0] && opt_SAIL)) {
418 if (ship.shp_name[0] != 0) {
421 pr(" %s\n", ship.shp_name);
428 pr("%s: No ship(s)\n", player->argp[1]);
430 pr("%s: No ship(s)\n", "");
433 pr("%d ship%s\n", nships, splur(nships));
437 /* Chad Zabel 1-15-94
438 * New command added to display autonav stats.
451 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
453 while (nxtitem(&nb, (s_char *)(&ship))) {
454 if (!player->owner || ship.shp_own == 0)
456 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
457 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
460 if ((ship.shp_autonav & AN_AUTONAV) ||
461 (ship.shp_path[0] && opt_SAIL)) {
462 if (!nships) { /* 1st ship, print banner */
465 pr("shp# ship type x,y ");
471 pr("%3d ", ship.shp_own);
473 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
474 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
476 if (ship.shp_autonav & AN_AUTONAV) {
478 prxy(" %3d,%-3d", ship.shp_destx[1],
479 ship.shp_desty[1], player->cnum);
482 if ((ship.shp_destx[1] != ship.shp_destx[0])
483 || (ship.shp_desty[1] != ship.shp_desty[0])) {
484 prxy(" %3d,%-3d", ship.shp_destx[0],
485 ship.shp_desty[0], player->cnum);
489 if (ship.shp_autonav & AN_STANDBY)
491 else if (ship.shp_autonav & AN_LOADING)
494 /* ETA calculation */
496 c = BestShipPath(buf, ship.shp_x,
497 ship.shp_y, ship.shp_destx[0],
498 ship.shp_desty[0], ship.shp_own);
500 pr(" no route possible");
504 pr(" route too long");
506 /* distance to destination */
507 eta_calc(&ship, c, &len, &updates);
508 pr(" %3d %4d", len, updates);
511 if (ship.shp_autonav & AN_SCUTTLE)
516 if ((ship.shp_autonav & AN_AUTONAV) ||
517 (ship.shp_path[0] && opt_SAIL)) {
518 if (!(ship.shp_autonav & AN_AUTONAV))
520 if (ship.shp_name[0] != 0) {
523 pr(" %s\n", ship.shp_name);
530 pr("%s: No ship(s)\n", player->argp[1]);
532 pr("%s: No ship(s)\n", "");
535 pr("%d ship%s\n", nships, splur(nships));