2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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:
53 ** ORDER <ship> Show orders
54 ** ORDER <ship> c[ancel] Cancel orders
55 ** ORDER <ship> s[top] Suspend orders
56 ** ORDER <ship> r[esume] Resume orders
57 ** ORDER <ship> d[eclare] <dest1> Set destination
58 ** d[eclare] <dest1> <dest2>
59 ** ORDER <ship> l[evel] <field> <start/end> <comm> <level>
63 ** qorder <ship> display cargo levels
64 ** sorder <ship> display statistical info
71 int orders, sub, level;
75 struct shpstr start; /* Used for checking database */
77 coord p0x, p0y, p1x, p1y;
79 s_char *p, *p1, *dest;
84 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
86 while (!player->aborted && nxtitem(&nb, (&ship))) {
87 if (!player->owner || ship.shp_own == 0)
89 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
90 pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
96 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
100 memcpy(&start, &ship, sizeof(struct shpstr));
102 "Ship #%d, declare, cancel, suspend, resume, level? ",
104 p = getstarg(player->argp[2], prompt, buf);
105 if (player->aborted || !p)
115 pr("Bad order type!\n");
117 case 'c': /* clear ship fields */
118 ship.shp_mission = 0;
119 ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
120 for (i = 0; i < TMAX; i++) {
121 ship.shp_tstart[i] = I_NONE;
122 ship.shp_tend[i] = I_NONE;
123 ship.shp_lstart[i] = 0;
124 ship.shp_lend[i] = 0;
127 case 's': /* suspend ship movement */
128 ship.shp_mission = 0;
129 ship.shp_autonav |= AN_STANDBY;
131 case 'r': /* resume ship movement */
132 ship.shp_mission = 0;
133 ship.shp_autonav &= ~AN_STANDBY;
135 case 'd': /* declare path */
139 if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
142 if (!sarg_xy(p, &p0x, &p0y))
150 p = getstarg(player->argp[4], "Second dest? ", buf);
151 if (!p || !*p || !strcmp(p, "-")) {
153 pr("A one-way order has been accepted.\n");
154 } else if (!strncmp(p, "s", 1)) {
155 if (opt_TRADESHIPS) {
156 if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
157 pr("You can't auto-scuttle that ship!\n");
161 pr("You can't auto-scuttle that ship!\n");
164 pr("A scuttle order has been accepted.\n");
167 if (!sarg_xy(p, &p1x, &p1y))
169 pr("A circular order has been accepted.\n");
174 * Set new destination and trade type fields.
176 ship.shp_mission = 0;
177 ship.shp_destx[1] = p1x;
178 ship.shp_desty[1] = p1y;
179 ship.shp_destx[0] = p0x;
180 ship.shp_desty[0] = p0y;
182 ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
183 ship.shp_autonav |= AN_AUTONAV;
186 ship.shp_autonav |= AN_SCUTTLE;
189 /* set cargo levels on the ship */
192 /* convert player->argp[3] to an integer */
194 sub = atoi(player->argp[3]);
196 sprintf(buf1, "Field (1-%d) ", TMAX);
197 if (getstarg(player->argp[3], buf1, buf) == 0)
201 /* check to make sure value in within range. */
202 if (sub > TMAX || sub < 1) {
203 pr("Value must range from 1 to %d\n", TMAX);
207 /* to keep sub in range of our arrays
208 subtract 1 so the new range is 0-(TMAX-1)
212 if (ship.shp_autonav & AN_AUTONAV) {
214 dest = getstarg(player->argp[4], "Start or End? ", buf);
215 if (orders) { /* before dest check */
220 pr("You must enter 'start' or 'end'\n");
224 i1 = whatitem(player->argp[5], "Commodity? ");
228 p1 = getstarg(player->argp[6], "Amount? ",
235 level = 0; /* prevent negatives. */
236 pr("You must use positive number! Level set to 0.\n");
238 ship.shp_tstart[sub] = i1->i_vtype;
239 ship.shp_lstart[sub] = level;
244 i1 = whatitem(player->argp[5], "Commodity? ");
248 p1 = getstarg(player->argp[6], "Amount? ",
256 pr("You must use positive number! Level set to 0.\n");
258 ship.shp_tend[sub] = i1->i_vtype;
259 ship.shp_lend[sub] = level;
265 pr("You need to 'declare' a ship path first, see 'info order'\n");
268 } /* end of switch (*p) */
273 * Set loading flag if ship is already in one
274 * of the specified harbors and a cargo has been
278 if (((ship.shp_x == ship.shp_destx[0])
279 && (ship.shp_y == ship.shp_desty[0])
280 && (ship.shp_lstart[0] != ' '))
281 || ((ship.shp_x == ship.shp_desty[1])
282 && (ship.shp_y == ship.shp_desty[1])
283 && (ship.shp_lstart[1] != ' '))) {
290 ship.shp_autonav |= AN_LOADING;
292 /* swap variables, this keeps
293 the load_it() procedure happy. CZ
295 tcord = ship.shp_destx[0];
296 ship.shp_destx[0] = ship.shp_destx[1];
297 ship.shp_destx[1] = tcord;
298 tcord = ship.shp_desty[0];
299 ship.shp_desty[0] = ship.shp_desty[1];
300 ship.shp_desty[1] = tcord;
302 for (i = 0; i < TMAX; i++) {
303 lev[i] = ship.shp_lstart[i];
304 ship.shp_lstart[i] = ship.shp_lend[i];
305 ship.shp_lend[i] = lev[i];
306 tcomm = ship.shp_tstart[i];
307 ship.shp_tstart[i] = ship.shp_tend[i];
308 ship.shp_tend[i] = tcomm;
312 ** Write ship back to database, then give it
313 ** a kick down the autonav route if necessary.
317 /* Now do a sanity check. */
318 if (!check_ship_ok(&start))
321 putship(ship.shp_uid, &ship);
327 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
329 double mobcost, mobil;
336 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
337 mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
338 mobil = sp->shp_mobil;
344 mobil += (ship_mob_scale * (float)etu_per_update);
351 prhold(int hold, i_type itype, int amt)
353 if (itype != I_NONE && amt != 0) {
354 if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
357 pr("%c", ichr[itype].i_mnem);
371 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
373 while (nxtitem(&nb, (&ship))) {
374 if (!player->owner || ship.shp_own == 0)
376 if (!(ship.shp_autonav & AN_AUTONAV)
377 && (!opt_SAIL || !ship.shp_path[0]))
380 if (!nships) { /* 1st ship, print banner */
383 pr("shp# ship type ");
384 pr("[Starting] (Ending) \n");
388 pr("%3d ", ship.shp_own);
390 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
392 if (ship.shp_autonav & AN_AUTONAV) {
394 for (i = 0; i < TMAX; i++)
395 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
397 for (i = 0; i < TMAX; i++)
398 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
400 if (ship.shp_autonav & AN_SCUTTLE)
404 pr(" has a sail path\n");
406 if (ship.shp_name[0] != 0) {
409 pr(" %s\n", ship.shp_name);
414 pr("%s: No ship(s)\n", player->argp[1]);
416 pr("%s: No ship(s)\n", "");
419 pr("%d ship%s\n", nships, splur(nships));
433 if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
435 while (nxtitem(&nb, (&ship))) {
436 if (!player->owner || ship.shp_own == 0)
438 if (!(ship.shp_autonav & AN_AUTONAV)
439 && (!opt_SAIL || !ship.shp_path[0]))
442 if (!nships) { /* 1st ship, print banner */
445 pr("shp# ship type x,y ");
451 pr("%3d ", ship.shp_own);
453 pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
454 prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
456 if (ship.shp_autonav & AN_AUTONAV) {
458 prxy(" %3d,%-3d", ship.shp_destx[1],
459 ship.shp_desty[1], player->cnum);
462 if ((ship.shp_destx[1] != ship.shp_destx[0])
463 || (ship.shp_desty[1] != ship.shp_desty[0])) {
464 prxy(" %3d,%-3d", ship.shp_destx[0],
465 ship.shp_desty[0], player->cnum);
469 if (ship.shp_autonav & AN_STANDBY)
471 else if (ship.shp_autonav & AN_LOADING)
474 /* ETA calculation */
476 c = BestShipPath(buf, ship.shp_x,
477 ship.shp_y, ship.shp_destx[0],
478 ship.shp_desty[0], ship.shp_own);
480 pr(" no route possible");
484 pr(" route too long");
486 /* distance to destination */
487 eta_calc(&ship, c, &len, &updates);
488 pr(" %3d %4d", len, updates);
491 if (ship.shp_autonav & AN_SCUTTLE)
495 pr(" has a sail path\n");
497 if (ship.shp_name[0] != 0) {
500 pr(" %s\n", ship.shp_name);
505 pr("%s: No ship(s)\n", player->argp[1]);
507 pr("%s: No ship(s)\n", "");
510 pr("%d ship%s\n", nships, splur(nships));