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