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