]> git.pond.sub.org Git - empserver/blob - src/lib/commands/orde.c
Remove a bunch of redundant casts.
[empserver] / src / lib / commands / orde.c
1 /*
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
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 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.
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 <string.h>
36 #include <ctype.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "ship.h"
40 #include "sect.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "nat.h"
44 #include "path.h"
45 #include "file.h"
46 #include "item.h"
47 #include "commands.h"
48 #include "optlist.h"
49
50 /*
51 **  Command syntax:
52 **
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>
60 **
61 **
62 ** New syntax:
63 **  qorder <ship>    display cargo levels     
64 **  sorder <ship>    display statistical info 
65  */
66
67 int
68 orde(void)
69 {
70     int diffeachship = 0;
71     int orders, sub, level;
72     int scuttling = 0;
73     struct nstr_item nb;
74     struct shpstr ship;
75     struct shpstr start;        /* Used for checking database */
76     struct ichrstr *i1;
77     coord p0x, p0y, p1x, p1y;
78     int i;
79     s_char *p, *p1, *dest;
80     s_char buf1[128];
81     s_char buf[1024];
82     s_char prompt[128];
83
84     if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
85         return RET_SYN;
86     while (!player->aborted && nxtitem(&nb, (&ship))) {
87         if (!player->owner || ship.shp_own == 0)
88             continue;
89         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
90             pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
91             continue;
92         }
93         if (opt_SAIL) {
94             if (*ship.shp_path) {
95                 if (!diffeachship)
96                     pr("Ship #%d has a \"sail\" path!\n", ship.shp_uid);
97                 continue;
98             }
99         }
100         memcpy(&start, &ship, sizeof(struct shpstr));
101         sprintf(prompt,
102                 "Ship #%d, declare, cancel, suspend, resume, level? ",
103                 ship.shp_uid);
104         p = getstarg(player->argp[2], prompt, buf);
105         if (player->aborted || !p)
106             return RET_FAIL;
107         if (!*p) {
108             if (!diffeachship)
109                 return RET_FAIL;
110             else
111                 continue;
112         }
113         switch (*p) {
114         default:
115             pr("Bad order type!\n");
116             return RET_SYN;
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;
125             }
126             break;
127         case 's':               /* suspend ship movement  */
128             ship.shp_mission = 0;
129             ship.shp_autonav |= AN_STANDBY;
130             break;
131         case 'r':               /* resume ship movement   */
132             ship.shp_mission = 0;
133             ship.shp_autonav &= ~AN_STANDBY;
134             break;
135         case 'd':               /* declare path */
136             orders = 0;
137             scuttling = 0;
138             /* Need location */
139             if ((p = getstarg(player->argp[3], "Destination? ", buf)) == 0
140                 || *p == 0)
141                 return RET_SYN;
142             if (!sarg_xy(p, &p0x, &p0y))
143                 return RET_SYN;
144             p1x = p0x;
145             p1y = p0y;
146
147
148
149             if (!orders) {
150                 p = getstarg(player->argp[4], "Second dest? ", buf);
151                 if (!p || !*p || !strcmp(p, "-")) {
152                     orders = 1;
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");
158                             return RET_SYN;
159                         }
160                     } else {
161                         pr("You can't auto-scuttle that ship!\n");
162                         return RET_SYN;
163                     }
164                     pr("A scuttle order has been accepted.\n");
165                     scuttling = 1;
166                 } else {
167                     if (!sarg_xy(p, &p1x, &p1y))
168                         return RET_SYN;
169                     pr("A circular order has been accepted.\n");
170                 }
171             }
172
173             /*
174              *  Set new destination and trade type fields.
175              */
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;
181
182             ship.shp_autonav &= ~(AN_STANDBY + AN_SAILDIR + AN_LOADING);
183             ship.shp_autonav |= AN_AUTONAV;
184
185             if (scuttling)
186                 ship.shp_autonav |= AN_SCUTTLE;
187             break;
188
189             /* set cargo levels on the ship */
190
191         case 'l':
192             /* convert player->argp[3] to an integer */
193             if (player->argp[3])
194                 sub = atoi(player->argp[3]);
195             else {
196                 sprintf(buf1, "Field (1-%d) ", TMAX);
197                 if (getstarg(player->argp[3], buf1, buf) == 0)
198                     return RET_SYN;
199                 sub = atoi(buf);
200             }
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);
204                 break;
205             }
206
207             /* to keep sub in range of our arrays 
208                subtract 1 so the new range is 0-(TMAX-1)
209              */
210             sub = sub - 1;;
211
212             if (ship.shp_autonav & AN_AUTONAV) {
213                 orders = 1;
214                 dest = getstarg(player->argp[4], "Start or End? ", buf);
215                 if (orders) {   /* before dest check */
216                     if (!dest)
217                         break;
218                     switch (*dest) {
219                     default:
220                         pr("You must enter 'start' or 'end'\n");
221                         return RET_SYN;
222                     case 'e':
223                     case 'E':
224                         i1 = whatitem(player->argp[5], "Commodity? ");
225                         if (!i1)
226                             break;
227                         else {
228                             p1 = getstarg(player->argp[6], "Amount? ",
229                                           buf);
230                             if (!p1)
231                                 return RET_SYN;
232                             level = atoi(p1);
233                         }
234                         if (level < 0) {
235                             level = 0;  /* prevent negatives. */
236                             pr("You must use positive number! Level set to 0.\n");
237                         }
238                         ship.shp_tstart[sub] = i1->i_vtype;
239                         ship.shp_lstart[sub] = level;
240                         pr("Order Set \n");
241                         break;
242                     case 's':
243                     case 'S':
244                         i1 = whatitem(player->argp[5], "Commodity? ");
245                         if (!i1)
246                             break;
247                         else {
248                             p1 = getstarg(player->argp[6], "Amount? ",
249                                           buf);
250                             if (!p1)
251                                 return RET_SYN;
252                             level = atoi(p1);
253                         }
254                         if (level < 0) {
255                             level = 0;
256                             pr("You must use positive number! Level set to 0.\n");
257                         }
258                         ship.shp_tend[sub] = i1->i_vtype;
259                         ship.shp_lend[sub] = level;
260                         pr("Order Set \n");
261                         break;
262                     }
263                 }
264             } else
265                 pr("You need to 'declare' a ship path first, see 'info order'\n");
266
267             break;
268         }                       /* end of switch (*p) */
269
270
271
272         /*
273          *  Set loading flag if ship is already in one
274          *  of the specified harbors and a cargo has been
275          *  specified.
276          */
277
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] != ' '))) {
284
285             coord tcord;
286             i_type tcomm;
287             short lev[TMAX];
288             int i;
289
290             ship.shp_autonav |= AN_LOADING;
291
292             /*  swap variables, this keeps 
293                the load_it() procedure happy. CZ
294              */
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;
301
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;
309             }
310         }
311         /*
312            **  Write ship back to database, then give it
313            **  a kick down the autonav route if necessary.
314          */
315
316
317         /* Now do a sanity check. */
318         if (!check_ship_ok(&start))
319             return RET_SYN;
320
321         putship(ship.shp_uid, &ship);
322     }
323     return RET_OK;
324 }
325
326 static void
327 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
328 {
329     double mobcost, mobil;
330     int i;
331
332     i = strlen(path);
333     *len = i;
334     *nupdates = 1;
335
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;
339     while (i) {
340         if (mobil > 0) {
341             mobil -= mobcost;
342             i--;
343         } else {
344             mobil += (ship_mob_scale * (float)etu_per_update);
345             (*nupdates)++;
346         }
347     }
348 }
349
350 static void
351 prhold(int hold, i_type itype, int amt)
352 {
353     if (itype != I_NONE && amt != 0) {
354         if (CANT_HAPPEN(itype <= I_NONE || itype > I_MAX))
355             return;
356         pr("%d-", hold + 1);
357         pr("%c", ichr[itype].i_mnem);
358         pr(":");
359         pr("%d ", amt);
360     }
361 }
362
363 int
364 qorde(void)
365 {
366     int nships = 0;
367     int i;
368     struct nstr_item nb;
369     struct shpstr ship;
370
371     if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
372         return RET_SYN;
373     while (nxtitem(&nb, (&ship))) {
374         if (!player->owner || ship.shp_own == 0)
375             continue;
376         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
377             pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
378             continue;
379         }
380
381         if ((ship.shp_autonav & AN_AUTONAV) ||
382             (ship.shp_path[0] && opt_SAIL)) {
383             if (!nships) {      /* 1st ship, print banner */
384                 if (player->god)
385                     pr("own ");
386                 pr("shp#     ship type    ");
387                 pr("[Starting]       (Ending)    \n");
388             }
389             nships++;
390             if (player->god)
391                 pr("%3d ", ship.shp_own);
392             pr("%4d", nb.cur);
393             pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
394         }
395         if (ship.shp_autonav & AN_AUTONAV) {
396
397             pr(" [");
398             for (i = 0; i < TMAX; i++)
399                 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
400             pr("] , (");
401             for (i = 0; i < TMAX; i++)
402                 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
403             pr(")");
404             if (ship.shp_autonav & AN_SCUTTLE)
405                 pr(" scuttling");
406             pr("\n");
407         }
408
409         if (opt_SHIPNAMES) {
410             if ((ship.shp_autonav & AN_AUTONAV) ||
411                 (ship.shp_path[0] && opt_SAIL)) {
412                 if (ship.shp_name[0] != 0) {
413                     if (player->god)
414                         pr("    ");
415                     pr("       %s\n", ship.shp_name);
416                 }
417             }
418         }
419     }
420     if (!nships) {
421         if (player->argp[1])
422             pr("%s: No ship(s)\n", player->argp[1]);
423         else
424             pr("%s: No ship(s)\n", "");
425         return RET_FAIL;
426     } else
427         pr("%d ship%s\n", nships, splur(nships));
428     return RET_OK;
429 }
430
431 /*  Chad Zabel 1-15-94
432  *  New command added to display autonav stats.
433  */
434
435 int
436 sorde(void)
437 {
438     int nships = 0;
439     int len, updates;
440     s_char *c;
441     struct nstr_item nb;
442     struct shpstr ship;
443     s_char buf[1024];
444
445     if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
446         return RET_SYN;
447     while (nxtitem(&nb, (&ship))) {
448         if (!player->owner || ship.shp_own == 0)
449             continue;
450         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
451             pr("bad ship type %d (#%d)\n", ship.shp_type, nb.cur);
452             continue;
453         }
454         if ((ship.shp_autonav & AN_AUTONAV) ||
455             (ship.shp_path[0] && opt_SAIL)) {
456             if (!nships) {      /* 1st ship, print banner */
457                 if (player->god)
458                     pr("own ");
459                 pr("shp#     ship type      x,y    ");
460                 pr("start    end   ");
461                 pr("len  eta\n");
462             }
463             nships++;
464             if (player->god)
465                 pr("%3d ", ship.shp_own);
466             pr("%4d", nb.cur);
467             pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
468             prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
469         }
470         if (ship.shp_autonav & AN_AUTONAV) {
471             /* Destination 1 */
472             prxy(" %3d,%-3d", ship.shp_destx[1],
473                  ship.shp_desty[1], player->cnum);
474
475             /* Destination 2 */
476             if ((ship.shp_destx[1] != ship.shp_destx[0])
477                 || (ship.shp_desty[1] != ship.shp_desty[0])) {
478                 prxy(" %3d,%-3d", ship.shp_destx[0],
479                      ship.shp_desty[0], player->cnum);
480             } else
481                 pr("        ");
482
483             if (ship.shp_autonav & AN_STANDBY)
484                 pr(" suspended");
485             else if (ship.shp_autonav & AN_LOADING)
486                 pr(" loading");
487             else {
488                 /* ETA calculation */
489
490                 c = BestShipPath(buf, ship.shp_x,
491                                  ship.shp_y, ship.shp_destx[0],
492                                  ship.shp_desty[0], ship.shp_own);
493                 if (!c || !*c)
494                     pr(" no route possible");
495                 else if (*c == 'h')
496                     pr(" has arrived");
497                 else if (*c == '?')
498                     pr(" route too long");
499                 else {
500                     /* distance to destination */
501                     eta_calc(&ship, c, &len, &updates);
502                     pr(" %3d %4d", len, updates);
503                 }
504             }
505             if (ship.shp_autonav & AN_SCUTTLE)
506                 pr(" (scuttling)");
507             pr("\n");
508         }
509         if (opt_SHIPNAMES) {
510             if ((ship.shp_autonav & AN_AUTONAV) ||
511                 (ship.shp_path[0] && opt_SAIL)) {
512                 if (!(ship.shp_autonav & AN_AUTONAV))
513                     pr("\n");
514                 if (ship.shp_name[0] != 0) {
515                     if (player->god)
516                         pr("    ");
517                     pr("       %s\n", ship.shp_name);
518                 }
519             }
520         }
521     }
522     if (!nships) {
523         if (player->argp[1])
524             pr("%s: No ship(s)\n", player->argp[1]);
525         else
526             pr("%s: No ship(s)\n", "");
527         return RET_FAIL;
528     } else
529         pr("%d ship%s\n", nships, splur(nships));
530     return RET_OK;
531 }