]> git.pond.sub.org Git - empserver/blob - src/lib/commands/orde.c
Normalize order's reaction to bad input
[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 shpstr start;        /* Used for checking database */
67     struct ichrstr *i1;
68     coord p0x, p0y, p1x, p1y;
69     int i;
70     char *p, *p1, *dest;
71     char buf1[128];
72     char buf[1024];
73     char prompt[128];
74
75     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
76         return RET_SYN;
77     while (!player->aborted && nxtitem(&nb, (&ship))) {
78         if (!player->owner || ship.shp_own == 0)
79             continue;
80         if (opt_SAIL) {
81             if (*ship.shp_path) {
82                 pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
83                 continue;
84             }
85         }
86         memcpy(&start, &ship, sizeof(struct shpstr));
87         sprintf(prompt,
88                 "Ship #%d, declare, cancel, suspend, resume, level? ",
89                 ship.shp_uid);
90         p = getstarg(player->argp[2], prompt, buf);
91         if (player->aborted || !p || !*p)
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 (!*p || !strcmp(p, "-")) {
130                 pr("A one-way order has been accepted.\n");
131             } else if (!strncmp(p, "s", 1)) {
132                 if (opt_TRADESHIPS) {
133                     if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
134                         pr("You can't auto-scuttle that ship!\n");
135                         return RET_SYN;
136                     }
137                 } else {
138                     pr("You can't auto-scuttle that ship!\n");
139                     return RET_SYN;
140                 }
141                 pr("A scuttle order has been accepted.\n");
142                 scuttling = 1;
143             } else {
144                 if (!sarg_xy(p, &p1x, &p1y))
145                     return RET_SYN;
146                 pr("A circular order has been accepted.\n");
147             }
148
149             /*
150              *  Set new destination and trade type fields.
151              */
152             ship.shp_mission = 0;
153             ship.shp_destx[1] = p1x;
154             ship.shp_desty[1] = p1y;
155             ship.shp_destx[0] = p0x;
156             ship.shp_desty[0] = p0y;
157
158             ship.shp_autonav &= ~(AN_STANDBY | AN_LOADING);
159             ship.shp_autonav |= AN_AUTONAV;
160
161             if (scuttling)
162                 ship.shp_autonav |= AN_SCUTTLE;
163             break;
164
165             /* set cargo levels on the ship */
166
167         case 'l':
168             /* convert player->argp[3] to an integer */
169             sprintf(buf1, "Field (1-%d) ", TMAX);
170             if (!getstarg(player->argp[3], buf1, buf))
171                 return RET_SYN;
172             sub = atoi(buf);
173             /* check to make sure value in within range. */
174             if (sub > TMAX || sub < 1) {
175                 pr("Value must range from 1 to %d\n", TMAX);
176                 return RET_FAIL;
177             }
178
179             /* to keep sub in range of our arrays
180                subtract 1 so the new range is 0-(TMAX-1)
181              */
182             sub = sub - 1;;
183
184             if (ship.shp_autonav & AN_AUTONAV) {
185                 dest = getstarg(player->argp[4], "Start or End? ", buf);
186                 if (!dest)
187                     return RET_FAIL;
188                 switch (*dest) {
189                 default:
190                     pr("You must enter 'start' or 'end'\n");
191                     return RET_SYN;
192                 case 'e':
193                 case 'E':
194                     i1 = whatitem(player->argp[5], "Commodity? ");
195                     if (!i1)
196                         return RET_FAIL;
197                     p1 = getstarg(player->argp[6], "Amount? ",
198                                   buf);
199                     if (!p1)
200                         return RET_SYN;
201                     level = atoi(p1);
202                     if (level < 0) {
203                         level = 0;      /* prevent negatives. */
204                         pr("You must use positive number! Level set to 0.\n");
205                     }
206                     ship.shp_tstart[sub] = i1->i_uid;
207                     ship.shp_lstart[sub] = level;
208                     pr("Order Set \n");
209                     break;
210                 case 's':
211                 case 'S':
212                     i1 = whatitem(player->argp[5], "Commodity? ");
213                     if (!i1)
214                         return RET_FAIL;
215                     p1 = getstarg(player->argp[6], "Amount? ",
216                                   buf);
217                     if (!p1)
218                         return RET_SYN;
219                     level = atoi(p1);
220                     if (level < 0) {
221                         level = 0;
222                         pr("You must use positive number! Level set to 0.\n");
223                     }
224                     ship.shp_tend[sub] = i1->i_uid;
225                     ship.shp_lend[sub] = level;
226                     pr("Order Set \n");
227                     break;
228                 }
229             } else
230                 pr("You need to 'declare' a ship path first, see 'info order'\n");
231
232             break;
233         }                       /* end of switch (*p) */
234
235
236
237         /*
238          *  Set loading flag if ship is already in one
239          *  of the specified harbors and a cargo has been
240          *  specified.
241          */
242
243         if (((ship.shp_x == ship.shp_destx[0])
244              && (ship.shp_y == ship.shp_desty[0])
245              && (ship.shp_lstart[0] != ' '))
246             || ((ship.shp_x == ship.shp_desty[1])
247                 && (ship.shp_y == ship.shp_desty[1])
248                 && (ship.shp_lstart[1] != ' '))) {
249
250             coord tcord;
251             i_type tcomm;
252             short lev[TMAX];
253             int i;
254
255             ship.shp_autonav |= AN_LOADING;
256
257             /*  swap variables, this keeps
258                the load_it() procedure happy. CZ
259              */
260             tcord = ship.shp_destx[0];
261             ship.shp_destx[0] = ship.shp_destx[1];
262             ship.shp_destx[1] = tcord;
263             tcord = ship.shp_desty[0];
264             ship.shp_desty[0] = ship.shp_desty[1];
265             ship.shp_desty[1] = tcord;
266
267             for (i = 0; i < TMAX; i++) {
268                 lev[i] = ship.shp_lstart[i];
269                 ship.shp_lstart[i] = ship.shp_lend[i];
270                 ship.shp_lend[i] = lev[i];
271                 tcomm = ship.shp_tstart[i];
272                 ship.shp_tstart[i] = ship.shp_tend[i];
273                 ship.shp_tend[i] = tcomm;
274             }
275         }
276         /*
277            **  Write ship back to database, then give it
278            **  a kick down the autonav route if necessary.
279          */
280
281
282         /* Now do a sanity check. */
283         if (!check_ship_ok(&start))
284             return RET_SYN;
285
286         putship(ship.shp_uid, &ship);
287     }
288     return RET_OK;
289 }
290
291 static void
292 eta_calc(struct shpstr *sp, char *path, int *len, int *nupdates)
293 {
294     double mobcost, mobil;
295     int i;
296
297     i = strlen(path);
298     *len = i;
299     *nupdates = 1;
300
301     mobcost = shp_mobcost(sp);
302     mobil = sp->shp_mobil;
303     while (i) {
304         if (mobil > 0) {
305             mobil -= mobcost;
306             i--;
307         } else {
308             mobil += (ship_mob_scale * (float)etu_per_update);
309             (*nupdates)++;
310         }
311     }
312 }
313
314 static void
315 prhold(int hold, i_type itype, int amt)
316 {
317     if (itype != I_NONE && amt != 0) {
318         if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
319             return;
320         pr("%d-", hold + 1);
321         pr("%c", ichr[itype].i_mnem);
322         pr(":");
323         pr("%d ", amt);
324     }
325 }
326
327 int
328 qorde(void)
329 {
330     int nships = 0;
331     int i;
332     struct nstr_item nb;
333     struct shpstr ship;
334
335     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
336         return RET_SYN;
337     while (nxtitem(&nb, (&ship))) {
338         if (!player->owner || ship.shp_own == 0)
339             continue;
340         if (!(ship.shp_autonav & AN_AUTONAV)
341             && (!opt_SAIL || !ship.shp_path[0]))
342             continue;
343
344         if (!nships) {          /* 1st ship, print banner */
345             if (player->god)
346                 pr("own ");
347             pr("shp#     ship type    ");
348             pr("[Starting]       (Ending)    \n");
349         }
350         nships++;
351         if (player->god)
352             pr("%3d ", ship.shp_own);
353         pr("%4d", nb.cur);
354         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
355
356         if (ship.shp_autonav & AN_AUTONAV) {
357             pr(" [");
358             for (i = 0; i < TMAX; i++)
359                 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
360             pr("] , (");
361             for (i = 0; i < TMAX; i++)
362                 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
363             pr(")");
364             if (ship.shp_autonav & AN_SCUTTLE)
365                 pr(" scuttling");
366             pr("\n");
367         } else
368             pr(" has a sail path\n");
369
370         if (ship.shp_name[0] != 0) {
371             if (player->god)
372                 pr("    ");
373             pr("       %s\n", ship.shp_name);
374         }
375     }
376     if (!nships) {
377         if (player->argp[1])
378             pr("%s: No ship(s)\n", player->argp[1]);
379         else
380             pr("%s: No ship(s)\n", "");
381         return RET_FAIL;
382     } else
383         pr("%d ship%s\n", nships, splur(nships));
384     return RET_OK;
385 }
386
387 int
388 sorde(void)
389 {
390     int nships = 0;
391     int len, updates;
392     char *c;
393     struct nstr_item nb;
394     struct shpstr ship;
395     char buf[1024];
396
397     if (!snxtitem(&nb, EF_SHIP, player->argp[1], NULL))
398         return RET_SYN;
399     while (nxtitem(&nb, (&ship))) {
400         if (!player->owner || ship.shp_own == 0)
401             continue;
402         if (!(ship.shp_autonav & AN_AUTONAV)
403             && (!opt_SAIL || !ship.shp_path[0]))
404             continue;
405
406         if (!nships) {          /* 1st ship, print banner */
407             if (player->god)
408                 pr("own ");
409             pr("shp#     ship type      x,y    ");
410             pr("start    end   ");
411             pr("len  eta\n");
412         }
413         nships++;
414         if (player->god)
415             pr("%3d ", ship.shp_own);
416         pr("%4d", nb.cur);
417         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
418         prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
419
420         if (ship.shp_autonav & AN_AUTONAV) {
421             /* Destination 1 */
422             prxy(" %3d,%-3d",
423                  ship.shp_destx[1], ship.shp_desty[1], player->cnum);
424
425             /* Destination 2 */
426             if ((ship.shp_destx[1] != ship.shp_destx[0])
427                 || (ship.shp_desty[1] != ship.shp_desty[0])) {
428                 prxy(" %3d,%-3d",
429                      ship.shp_destx[0], ship.shp_desty[0], player->cnum);
430             } else
431                 pr("        ");
432
433             if (ship.shp_autonav & AN_STANDBY)
434                 pr(" suspended");
435             else if (ship.shp_autonav & AN_LOADING)
436                 pr(" loading");
437             else {
438                 /* ETA calculation */
439
440                 c = BestShipPath(buf, ship.shp_x, ship.shp_y,
441                                  ship.shp_destx[0], ship.shp_desty[0],
442                                  ship.shp_own);
443                 if (!c)
444                     pr(" no route possible");
445                 else if (*c == 'h')
446                     pr(" has arrived");
447                 else {
448                     /* distance to destination */
449                     eta_calc(&ship, c, &len, &updates);
450                     pr(" %3d %4d", len, updates);
451                 }
452             }
453             if (ship.shp_autonav & AN_SCUTTLE)
454                 pr(" (scuttling)");
455             pr("\n");
456         } else
457             pr(" has a sail path\n");
458
459         if (ship.shp_name[0] != 0) {
460             if (player->god)
461                 pr("    ");
462             pr("       %s\n", ship.shp_name);
463         }
464     }
465     if (!nships) {
466         if (player->argp[1])
467             pr("%s: No ship(s)\n", player->argp[1]);
468         else
469             pr("%s: No ship(s)\n", "");
470         return RET_FAIL;
471     } else
472         pr("%d ship%s\n", nships, splur(nships));
473     return RET_OK;
474 }