2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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:
58 ** ORDER <ship> Show orders
59 ** ORDER <ship> c[ancel] Cancel orders
60 ** ORDER <ship> s[top] Suspend orders
61 ** ORDER <ship> r[esume] Resume orders
62 ** ORDER <ship> d[eclare] <dest1> Set destination
63 ** d[eclare] <dest1> <dest2>
64 ** ORDER <ship> l[evel] <field> <start/end> <comm> <level>
68 ** qorder <ship> display cargo levels
69 ** sorder <ship> display statistical info
77 int orders, sub, level;
81 struct shpstr start; /* Used for checking database */
83 coord p0x, p0y, p1x, p1y;
85 s_char *p, *p1, *dest;
90 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
92 while (!player->aborted && nxtitem(&nb, (s_char *)(&ship))) {
93 if (!player->owner || ship.shp_own == 0)
95 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
96 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
100 if (*ship.shp_path) {
102 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
106 memcpy(&start, &ship, sizeof(struct shpstr));
108 "Ship #%d, declare, cancel, suspend, resume, level? ",
110 p = getstarg(player->argp[2], prompt, buf);
111 if (player->aborted || !p)
121 pr("Bad order type!\n");
123 case 'c': /* clear ship fields */
124 ship.shp_mission = 0;
125 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
126 for (i = 0; i < TMAX; i++) {
127 ship.shp_tstart[i] = ' ';
128 ship.shp_tend[i] = ' ';
129 ship.shp_lstart[i] = 0;
130 ship.shp_lend[i] = 0;
133 case 's': /* suspend ship movement */
134 ship.shp_mission = 0;
135 ship.shp_autonav |= AN_STANDBY;
137 case 'r': /* resume ship movement */
138 ship.shp_mission = 0;
139 ship.shp_autonav &= ~AN_STANDBY;
141 case 'd': /* declare path */
145 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
148 if (!sarg_xy(p, &p0x, &p0y))
156 p = getstarg(player->argp[4], "Second dest? ", buf);
157 if (!p || !*p || !strcmp(p, "-")) {
159 pr("A one-way order has been accepted.\n");
160 } else if (!strncmp(p, "s", 1)) {
161 if (opt_TRADESHIPS) {
162 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
163 pr("You can't auto-scuttle that ship!\n");
167 pr("You can't auto-scuttle that ship!\n");
170 pr("A scuttle order has been accepted.\n");
173 if (!sarg_xy(p, &p1x, &p1y))
175 pr("A circular order has been accepted.\n");
180 * Set new destination and trade type fields.
182 ship.shp_mission = 0;
183 ship.shp_destx[1] = p1x;
184 ship.shp_desty[1] = p1y;
185 ship.shp_destx[0] = p0x;
186 ship.shp_desty[0] = p0y;
188 ship.shp_autonav &= ~(AN_STANDBY + AN_SAILDIR + AN_LOADING);
189 ship.shp_autonav |= AN_AUTONAV;
192 ship.shp_autonav |= AN_SCUTTLE;
195 /* set cargo levels on the ship */
198 /* convert player->argp[3] to an integer */
200 sub = atoi(player->argp[3]);
202 sprintf(buf1, "Field (1-%d) ", TMAX);
203 if (getstarg(player->argp[3], buf1, buf) == 0)
207 /* check to make sure value in within range. */
208 if (sub > TMAX || sub < 1) {
209 pr("Value must range from 1 to %d\n", TMAX);
213 /* to keep sub in range of our arrays
214 subtract 1 so the new range is 0-(TMAX-1)
218 if (ship.shp_autonav & AN_AUTONAV) {
220 dest = getstarg(player->argp[4], "Start or End? ", buf);
221 if (orders) { /* before dest check */
226 pr("You must enter 'start' or 'end'\n");
230 i1 = whatitem(player->argp[5], "Commodity? ");
234 p1 = getstarg(player->argp[6], "Amount? ",
241 level = 0; /* prevent negatives. */
242 pr("You must use positive number! Level set to 0.\n");
244 ship.shp_tstart[sub] = (s_char)i1->i_mnem;
245 ship.shp_lstart[sub] = level;
250 i1 = whatitem(player->argp[5], "Commodity? ");
254 p1 = getstarg(player->argp[6], "Amount? ",
262 pr("You must use positive number! Level set to 0.\n");
264 ship.shp_tend[sub] = (s_char)i1->i_mnem;
265 ship.shp_lend[sub] = level;
271 pr("You need to 'declare' a ship path first, see 'info order'\n");
274 } /* end of switch (*p) */
279 * Set loading flag if ship is already in one
280 * of the specified harbors and a cargo has been
284 if (((ship.shp_x == ship.shp_destx[0])
285 && (ship.shp_y == ship.shp_desty[0])
286 && (ship.shp_lstart[0] != ' '))
287 || ((ship.shp_x == ship.shp_desty[1])
288 && (ship.shp_y == ship.shp_desty[1])
289 && (ship.shp_lstart[1] != ' '))) {
296 ship.shp_autonav |= AN_LOADING;
298 /* swap variables, this keeps
299 the load_it() procedure happy. CZ
301 tcord = ship.shp_destx[0];
302 ship.shp_destx[0] = ship.shp_destx[1];
303 ship.shp_destx[1] = tcord;
304 tcord = ship.shp_desty[0];
305 ship.shp_desty[0] = ship.shp_desty[1];
306 ship.shp_desty[1] = tcord;
308 for (i = 0; i < TMAX; i++) {
309 lev[i] = ship.shp_lstart[i];
310 ship.shp_lstart[i] = ship.shp_lend[i];
311 ship.shp_lend[i] = lev[i];
312 tcomm[i] = ship.shp_tstart[i];
313 ship.shp_tstart[i] = ship.shp_tend[i];
314 ship.shp_tend[i] = tcomm[i];
318 ** Write ship back to database, then give it
319 ** a kick down the autonav route if necessary.
323 /* Now do a sanity check. */
324 if (!check_ship_ok(&start))
327 putship(ship.shp_uid, &ship);
334 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
336 extern int etu_per_update;
337 extern float ship_mob_scale;
339 double mobcost, mobil;
346 mcp = &mchr[(int)sp->shp_type];
347 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
348 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
349 mobil = sp->shp_mobil;
355 mobil += (ship_mob_scale * (float)etu_per_update);
369 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
371 while (nxtitem(&nb, (s_char *)(&ship))) {
372 if (!player->owner || ship.shp_own == 0)
374 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
375 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
379 if ((ship.shp_autonav & AN_AUTONAV) ||
380 (ship.shp_path[0] && opt_SAIL)) {
381 if (!nships) { /* 1st ship, print banner */
384 pr("shp# ship type ");
385 pr("[Starting] (Ending) \n");
389 pr("%3d ", ship.shp_own);
391 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
393 if (ship.shp_autonav & AN_AUTONAV) {
396 for (i = 0; i < TMAX; i++) {
397 if (ship.shp_tend[i] != ' ' && ship.shp_lend[i] != 0) {
399 pr("%c", ship.shp_tend[i]);
401 pr("%d ", ship.shp_lend[i]);
405 for (i = 0; i < TMAX; i++) {
406 if (ship.shp_tstart[i] != ' ' && ship.shp_lstart[i] != 0) {
408 pr("%c", ship.shp_tstart[i]);
410 pr("%d ", ship.shp_lstart[i]);
414 if (ship.shp_autonav & AN_SCUTTLE)
420 if ((ship.shp_autonav & AN_AUTONAV) ||
421 (ship.shp_path[0] && opt_SAIL)) {
422 if (ship.shp_name[0] != 0) {
425 pr(" %s\n", ship.shp_name);
432 pr("%s: No ship(s)\n", player->argp[1]);
434 pr("%s: No ship(s)\n", "");
437 pr("%d ship%s\n", nships, splur(nships));
441 /* Chad Zabel 1-15-94
442 * New command added to display autonav stats.
455 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
457 while (nxtitem(&nb, (s_char *)(&ship))) {
458 if (!player->owner || ship.shp_own == 0)
460 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
461 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
464 if ((ship.shp_autonav & AN_AUTONAV) ||
465 (ship.shp_path[0] && opt_SAIL)) {
466 if (!nships) { /* 1st ship, print banner */
469 pr("shp# ship type x,y ");
475 pr("%3d ", ship.shp_own);
477 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
478 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
480 if (ship.shp_autonav & AN_AUTONAV) {
482 prxy(" %3d,%-3d", ship.shp_destx[1],
483 ship.shp_desty[1], player->cnum);
486 if ((ship.shp_destx[1] != ship.shp_destx[0])
487 || (ship.shp_desty[1] != ship.shp_desty[0])) {
488 prxy(" %2d,%-2d", ship.shp_destx[0],
489 ship.shp_desty[0], player->cnum);
493 if (ship.shp_autonav & AN_STANDBY)
495 else if (ship.shp_autonav & AN_LOADING)
498 /* ETA calculation */
500 c = BestShipPath(buf, ship.shp_x,
501 ship.shp_y, ship.shp_destx[0],
502 ship.shp_desty[0], ship.shp_own);
504 pr("no route possible");
508 pr("route too long");
510 /* distance to destination */
511 eta_calc(&ship, c, &len, &updates);
512 pr(" %3d %4d", len, updates);
515 if (ship.shp_autonav & AN_SCUTTLE)
520 if ((ship.shp_autonav & AN_AUTONAV) ||
521 (ship.shp_path[0] && opt_SAIL)) {
522 if (!(ship.shp_autonav & AN_AUTONAV))
524 if (ship.shp_name[0] != 0) {
527 pr(" %s\n", ship.shp_name);
534 pr("%s: No ship(s)\n", player->argp[1]);
536 pr("%s: No ship(s)\n", "");
539 pr("%d ship%s\n", nships, splur(nships));