]> git.pond.sub.org Git - empserver/blob - src/lib/commands/orde.c
Break long lines more tastefully
[empserver] / src / lib / commands / orde.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  orde.c: Turn on/off autonavigation
29  *
30  *  Known contributors to this file:
31  *     Chad Zabel, 1994
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "item.h"
39 #include "optlist.h"
40 #include "path.h"
41 #include "ship.h"
42
43 /*
44  *  Command syntax:
45  *
46  *  ORDER <ship>                                  Show orders
47  *  ORDER <ship> c[ancel]                         Cancel orders
48  *  ORDER <ship> s[top]                           Suspend orders
49  *  ORDER <ship> r[esume]                         Resume orders
50  *  ORDER <ship> d[eclare] <dest1>                Set destination
51  *               d[eclare] <dest1> <dest2>
52  *  ORDER <ship> l[evel]   <field> <start/end> <comm> <level>
53  *
54  * New syntax:
55  *  qorder <ship>    display cargo levels
56  *  sorder <ship>    display statistical info
57  */
58
59 int
60 orde(void)
61 {
62     int sub, level;
63     int scuttling = 0;
64     struct nstr_item nb;
65     struct shpstr ship;
66     struct ichrstr *i1;
67     coord p0x, p0y, p1x, p1y;
68     int i;
69     char *p, *p1, *dest;
70     char buf1[128];
71     char buf[1024];
72     char prompt[128];
73
74     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
75         return RET_SYN;
76     while (!player->aborted && nxtitem(&nb, (&ship))) {
77         if (!player->owner || ship.shp_own == 0)
78             continue;
79         if (opt_SAIL) {
80             if (*ship.shp_path) {
81                 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
82                 continue;
83             }
84         }
85         sprintf(prompt,
86                 "Ship #%d, declare, cancel, suspend, resume, level? ",
87                 ship.shp_uid);
88         p = getstarg(player->argp[2], prompt, buf);
89         if (player->aborted || !p || !*p)
90             return RET_FAIL;
91         if (!check_ship_ok(&ship))
92             return RET_FAIL;
93         switch (*p) {
94         default:
95             pr("Bad order type!\n");
96             return RET_SYN;
97         case 'c':               /* clear ship fields  */
98             ship.shp_mission = 0;
99             ship.shp_autonav &= ~(AN_AUTONAV + AN_STANDBY + AN_LOADING);
100             for (i = 0; i < TMAX; i++) {
101                 ship.shp_tstart[i] = I_NONE;
102                 ship.shp_tend[i] = I_NONE;
103                 ship.shp_lstart[i] = 0;
104                 ship.shp_lend[i] = 0;
105             }
106             break;
107         case 's':               /* suspend ship movement  */
108             ship.shp_mission = 0;
109             ship.shp_autonav |= AN_STANDBY;
110             break;
111         case 'r':               /* resume ship movement   */
112             ship.shp_mission = 0;
113             ship.shp_autonav &= ~AN_STANDBY;
114             break;
115         case 'd':               /* declare path */
116             scuttling = 0;
117             /* Need location */
118             p = getstarg(player->argp[3], "Destination? ", buf);
119             if (!p || !*p)
120                 return RET_SYN;
121             if (!sarg_xy(p, &p0x, &p0y))
122                 return RET_SYN;
123             p1x = p0x;
124             p1y = p0y;
125
126             p = getstarg(player->argp[4], "Second dest? ", buf);
127             if (!p)
128                 return RET_FAIL;
129             if (!check_ship_ok(&ship))
130                 return RET_FAIL;
131             if (!*p || !strcmp(p, "-")) {
132                 pr("A one-way order has been accepted.\n");
133             } else if (!strncmp(p, "s", 1)) {
134                 if (opt_TRADESHIPS) {
135                     if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
136                         pr("You can't auto-scuttle that ship!\n");
137                         return RET_SYN;
138                     }
139                 } else {
140                     pr("You can't auto-scuttle that ship!\n");
141                     return RET_SYN;
142                 }
143                 pr("A scuttle order has been accepted.\n");
144                 scuttling = 1;
145             } else {
146                 if (!sarg_xy(p, &p1x, &p1y))
147                     return RET_SYN;
148                 pr("A circular order has been accepted.\n");
149             }
150
151             /*
152              *  Set new destination and trade type fields.
153              */
154             ship.shp_mission = 0;
155             ship.shp_destx[1] = p1x;
156             ship.shp_desty[1] = p1y;
157             ship.shp_destx[0] = p0x;
158             ship.shp_desty[0] = p0y;
159
160             ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
161             ship.shp_autonav |= AN_AUTONAV;
162
163             if (scuttling)
164                 ship.shp_autonav |= AN_SCUTTLE;
165             break;
166
167             /* set cargo levels on the ship */
168
169         case 'l':
170             /* convert player->argp[3] to an integer */
171             sprintf(buf1, "Field (1-%d) ", TMAX);
172             if (!getstarg(player->argp[3], buf1, buf))
173                 return RET_SYN;
174             if (!check_ship_ok(&ship))
175                 return RET_FAIL;
176             sub = atoi(buf);
177             /* check to make sure value in within range. */
178             if (sub > TMAX || sub < 1) {
179                 pr("Value must range from 1 to %d\n", TMAX);
180                 return RET_FAIL;
181             }
182
183             /* to keep sub in range of our arrays
184                subtract 1 so the new range is 0-(TMAX-1)
185              */
186             sub = sub - 1;;
187
188             if (ship.shp_autonav & AN_AUTONAV) {
189                 dest = getstarg(player->argp[4], "Start or end? ", buf);
190                 if (!dest)
191                     return RET_FAIL;
192                 switch (*dest) {
193                 default:
194                     pr("You must enter 'start' or 'end'\n");
195                     return RET_SYN;
196                 case 'e':
197                 case 'E':
198                     i1 = whatitem(player->argp[5], "Commodity? ");
199                     if (!i1)
200                         return RET_FAIL;
201                     p1 = getstarg(player->argp[6], "Amount? ", buf);
202                     if (!p1)
203                         return RET_SYN;
204                     if (!check_ship_ok(&ship))
205                         return RET_FAIL;
206                     level = atoi(p1);
207                     if (level < 0) {
208                         level = 0;      /* prevent negatives. */
209                         pr("You must use positive number! Level set to 0.\n");
210                     }
211                     ship.shp_tstart[sub] = i1->i_uid;
212                     ship.shp_lstart[sub] = level;
213                     pr("Order set\n");
214                     break;
215                 case 's':
216                 case 'S':
217                     i1 = whatitem(player->argp[5], "Commodity? ");
218                     if (!i1)
219                         return RET_FAIL;
220                     p1 = getstarg(player->argp[6], "Amount? ", buf);
221                     if (!p1)
222                         return RET_SYN;
223                     if (!check_ship_ok(&ship))
224                         return RET_FAIL;
225                     level = atoi(p1);
226                     if (level < 0) {
227                         level = 0;
228                         pr("You must use positive number! Level set to 0.\n");
229                     }
230                     ship.shp_tend[sub] = i1->i_uid;
231                     ship.shp_lend[sub] = level;
232                     pr("Order Set \n");
233                     break;
234                 }
235             } else
236                 pr("You need to 'declare' a ship path first, see 'info order'\n");
237
238             break;
239         }                       /* end of switch (*p) */
240
241
242
243         /*
244          *  Set loading flag if ship is already in one
245          *  of the specified harbors and a cargo has been
246          *  specified.
247          */
248
249         if (((ship.shp_x == ship.shp_destx[0])
250              && (ship.shp_y == ship.shp_desty[0])
251              && (ship.shp_lstart[0] != ' '))
252             || ((ship.shp_x == ship.shp_desty[1])
253                 && (ship.shp_y == ship.shp_desty[1])
254                 && (ship.shp_lstart[1] != ' '))) {
255
256             coord tcord;
257             i_type tcomm;
258             short lev[TMAX];
259             int i;
260
261             ship.shp_autonav |= AN_LOADING;
262
263             /*  swap variables, this keeps
264                the load_it() procedure happy. CZ
265              */
266             tcord = ship.shp_destx[0];
267             ship.shp_destx[0] = ship.shp_destx[1];
268             ship.shp_destx[1] = tcord;
269             tcord = ship.shp_desty[0];
270             ship.shp_desty[0] = ship.shp_desty[1];
271             ship.shp_desty[1] = tcord;
272
273             for (i = 0; i < TMAX; i++) {
274                 lev[i] = ship.shp_lstart[i];
275                 ship.shp_lstart[i] = ship.shp_lend[i];
276                 ship.shp_lend[i] = lev[i];
277                 tcomm = ship.shp_tstart[i];
278                 ship.shp_tstart[i] = ship.shp_tend[i];
279                 ship.shp_tend[i] = tcomm;
280             }
281         }
282
283         putship(ship.shp_uid, &ship);
284     }
285     return RET_OK;
286 }
287
288 static void
289 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
290 {
291     double mobcost, mobil;
292     int i;
293
294     i = strlen(path);
295     *len = i;
296     *nupdates = 1;
297
298     mobcost = shp_mobcost(sp);
299     mobil = sp->shp_mobil;
300     while (i) {
301         if (mobil > 0) {
302             mobil -= mobcost;
303             i--;
304         } else {
305             mobil += (ship_mob_scale * (float)etu_per_update);
306             (*nupdates)++;
307         }
308     }
309 }
310
311 static void
312 prhold(int hold, i_type itype, int amt)
313 {
314     if (itype != I_NONE && amt != 0) {
315         if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
316             return;
317         pr("%d-", hold + 1);
318         pr("%c", ichr[itype].i_mnem);
319         pr(":");
320         pr("%d ", amt);
321     }
322 }
323
324 int
325 qorde(void)
326 {
327     int nships = 0;
328     int i;
329     struct nstr_item nb;
330     struct shpstr ship;
331
332     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
333         return RET_SYN;
334     while (nxtitem(&nb, (&ship))) {
335         if (!player->owner || ship.shp_own == 0)
336             continue;
337         if (!(ship.shp_autonav & AN_AUTONAV)
338             && (!opt_SAIL || !ship.shp_path[0]))
339             continue;
340
341         if (!nships) {          /* 1st ship, print banner */
342             if (player->god)
343                 pr("own ");
344             pr("shp#     ship type    ");
345             pr("[Starting]       (Ending)    \n");
346         }
347         nships++;
348         if (player->god)
349             pr("%3d ", ship.shp_own);
350         pr("%4d", nb.cur);
351         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
352
353         if (ship.shp_autonav & AN_AUTONAV) {
354             pr(" [");
355             for (i = 0; i < TMAX; i++)
356                 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
357             pr("] , (");
358             for (i = 0; i < TMAX; i++)
359                 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
360             pr(")");
361             if (ship.shp_autonav & AN_SCUTTLE)
362                 pr(" scuttling");
363             pr("\n");
364         } else
365             pr(" has a sail path\n");
366
367         if (ship.shp_name[0] != 0) {
368             if (player->god)
369                 pr("    ");
370             pr("       %s\n", ship.shp_name);
371         }
372     }
373     if (!nships) {
374         if (player->argp[1])
375             pr("%s: No ship(s)\n", player->argp[1]);
376         else
377             pr("%s: No ship(s)\n", "");
378         return RET_FAIL;
379     } else
380         pr("%d ship%s\n", nships, splur(nships));
381     return RET_OK;
382 }
383
384 int
385 sorde(void)
386 {
387     int nships = 0;
388     int len, updates;
389     char *c;
390     struct nstr_item nb;
391     struct shpstr ship;
392     char buf[1024];
393
394     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
395         return RET_SYN;
396     while (nxtitem(&nb, (&ship))) {
397         if (!player->owner || ship.shp_own == 0)
398             continue;
399         if (!(ship.shp_autonav & AN_AUTONAV)
400             && (!opt_SAIL || !ship.shp_path[0]))
401             continue;
402
403         if (!nships) {          /* 1st ship, print banner */
404             if (player->god)
405                 pr("own ");
406             pr("shp#     ship type      x,y    ");
407             pr("start    end   ");
408             pr("len  eta\n");
409         }
410         nships++;
411         if (player->god)
412             pr("%3d ", ship.shp_own);
413         pr("%4d", nb.cur);
414         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
415         prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
416
417         if (ship.shp_autonav & AN_AUTONAV) {
418             /* Destination 1 */
419             prxy(" %3d,%-3d",
420                  ship.shp_destx[1], ship.shp_desty[1], player->cnum);
421
422             /* Destination 2 */
423             if ((ship.shp_destx[1] != ship.shp_destx[0])
424                 || (ship.shp_desty[1] != ship.shp_desty[0])) {
425                 prxy(" %3d,%-3d",
426                      ship.shp_destx[0], ship.shp_desty[0], player->cnum);
427             } else
428                 pr("        ");
429
430             if (ship.shp_autonav & AN_STANDBY)
431                 pr(" suspended");
432             else if (ship.shp_autonav & AN_LOADING)
433                 pr(" loading");
434             else {
435                 /* ETA calculation */
436
437                 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
438                                  ship.shp_destx[0], ship.shp_desty[0],
439                                  ship.shp_own);
440                 if (!c)
441                     pr(" no route possible");
442                 else if (*c == 'h')
443                     pr(" has arrived");
444                 else {
445                     /* distance to destination */
446                     eta_calc(&ship, c, &len, &updates);
447                     pr(" %3d %4d", len, updates);
448                 }
449             }
450             if (ship.shp_autonav & AN_SCUTTLE)
451                 pr(" (scuttling)");
452             pr("\n");
453         } else
454             pr(" has a sail path\n");
455
456         if (ship.shp_name[0] != 0) {
457             if (player->god)
458                 pr("    ");
459             pr("       %s\n", ship.shp_name);
460         }
461     }
462     if (!nships) {
463         if (player->argp[1])
464             pr("%s: No ship(s)\n", player->argp[1]);
465         else
466             pr("%s: No ship(s)\n", "");
467         return RET_FAIL;
468     } else
469         pr("%d ship%s\n", nships, splur(nships));
470     return RET_OK;
471 }