]> git.pond.sub.org Git - empserver/blob - src/lib/commands/orde.c
(SHIPNAMES, opt_SHIPNAMES, Options, carg, name, sorde, qorde)
[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_autonav & AN_AUTONAV)
377             && (!opt_SAIL || !ship.shp_path[0]))
378             continue;
379
380         if (!nships) {          /* 1st ship, print banner */
381             if (player->god)
382                 pr("own ");
383             pr("shp#     ship type    ");
384             pr("[Starting]       (Ending)    \n");
385         }
386         nships++;
387         if (player->god)
388             pr("%3d ", ship.shp_own);
389         pr("%4d", nb.cur);
390         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
391
392         if (ship.shp_autonav & AN_AUTONAV) {
393             pr(" [");
394             for (i = 0; i < TMAX; i++)
395                 prhold(i, ship.shp_tend[i], ship.shp_lend[i]);
396             pr("] , (");
397             for (i = 0; i < TMAX; i++)
398                 prhold(i, ship.shp_tstart[i], ship.shp_lstart[i]);
399             pr(")");
400             if (ship.shp_autonav & AN_SCUTTLE)
401                 pr(" scuttling");
402             pr("\n");
403         } else
404             pr(" has a sail path\n");
405
406         if (ship.shp_name[0] != 0) {
407             if (player->god)
408                 pr("    ");
409             pr("       %s\n", ship.shp_name);
410         }
411     }
412     if (!nships) {
413         if (player->argp[1])
414             pr("%s: No ship(s)\n", player->argp[1]);
415         else
416             pr("%s: No ship(s)\n", "");
417         return RET_FAIL;
418     } else
419         pr("%d ship%s\n", nships, splur(nships));
420     return RET_OK;
421 }
422
423 int
424 sorde(void)
425 {
426     int nships = 0;
427     int len, updates;
428     s_char *c;
429     struct nstr_item nb;
430     struct shpstr ship;
431     s_char buf[1024];
432
433     if (!snxtitem(&nb, EF_SHIP, player->argp[1]))
434         return RET_SYN;
435     while (nxtitem(&nb, (&ship))) {
436         if (!player->owner || ship.shp_own == 0)
437             continue;
438         if (!(ship.shp_autonav & AN_AUTONAV)
439             && (!opt_SAIL || !ship.shp_path[0]))
440             continue;
441
442         if (!nships) {          /* 1st ship, print banner */
443             if (player->god)
444                 pr("own ");
445             pr("shp#     ship type      x,y    ");
446             pr("start    end   ");
447             pr("len  eta\n");
448         }
449         nships++;
450         if (player->god)
451             pr("%3d ", ship.shp_own);
452         pr("%4d", nb.cur);
453         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
454         prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
455
456         if (ship.shp_autonav & AN_AUTONAV) {
457             /* Destination 1 */
458             prxy(" %3d,%-3d", ship.shp_destx[1],
459                  ship.shp_desty[1], player->cnum);
460
461             /* Destination 2 */
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);
466             } else
467                 pr("        ");
468
469             if (ship.shp_autonav & AN_STANDBY)
470                 pr(" suspended");
471             else if (ship.shp_autonav & AN_LOADING)
472                 pr(" loading");
473             else {
474                 /* ETA calculation */
475
476                 c = BestShipPath(buf, ship.shp_x,
477                                  ship.shp_y, ship.shp_destx[0],
478                                  ship.shp_desty[0], ship.shp_own);
479                 if (!c || !*c)
480                     pr(" no route possible");
481                 else if (*c == 'h')
482                     pr(" has arrived");
483                 else if (*c == '?')
484                     pr(" route too long");
485                 else {
486                     /* distance to destination */
487                     eta_calc(&ship, c, &len, &updates);
488                     pr(" %3d %4d", len, updates);
489                 }
490             }
491             if (ship.shp_autonav & AN_SCUTTLE)
492                 pr(" (scuttling)");
493             pr("\n");
494         } else
495             pr(" has a sail path\n");
496
497         if (ship.shp_name[0] != 0) {
498             if (player->god)
499                 pr("    ");
500             pr("       %s\n", ship.shp_name);
501         }
502     }
503     if (!nships) {
504         if (player->argp[1])
505             pr("%s: No ship(s)\n", player->argp[1]);
506         else
507             pr("%s: No ship(s)\n", "");
508         return RET_FAIL;
509     } else
510         pr("%d ship%s\n", nships, splur(nships));
511     return RET_OK;
512 }