]> git.pond.sub.org Git - empserver/blob - src/lib/commands/orde.c
Import of Empire 4.2.12
[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])) return RET_SYN;
91         while (!player->aborted && nxtitem(&nb, (s_char *)(&ship))) {
92                 if (!player->owner || ship.shp_own == 0)
93                         continue;
94                 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
95                         pr("bad ship type %d (#%d)\n",
96                                 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",
103                                         ship.shp_uid);
104                         continue;
105                     }
106                 }
107                 memcpy(&start, &ship, sizeof(struct shpstr));
108                 sprintf(prompt, "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 || *p==0)
146                                 return RET_SYN;
147                         if (!sarg_xy(p, &p0x, &p0y))
148                                 return RET_SYN;
149                         p1x = p0x;
150                         p1y = p0y;
151
152
153                                 
154                         if (!orders) {
155                             p = getstarg(player->argp[4],"Second dest? ", buf);
156                             if (!p || !*p || !strcmp (p, "-")) {
157                                 orders = 1; 
158                                 pr("A one-way order has been accepted.\n");
159                             } else if (!strncmp(p, "s", 1)) {
160                                 if (opt_TRADESHIPS) {
161                                     if (!(mchr[(int)ship.shp_type].m_flags & M_TRADE)) {
162                                         pr("You can't auto-scuttle that ship!\n");
163                                         return RET_SYN;
164                                     }
165                                 } else {
166                                     pr("You can't auto-scuttle that ship!\n");
167                                     return RET_SYN;
168                                 }
169                                 pr("A scuttle order has been accepted.\n");
170                                 scuttling = 1;
171                             } else { 
172                                 if (!sarg_xy(p, &p1x, &p1y))
173                                     return RET_SYN;  
174                                 pr("A circular order has been accepted.\n");
175                             }
176                         }
177
178                         /*
179                          *  Set new destination and trade type fields.
180                          */
181                         ship.shp_mission = 0;
182                         ship.shp_destx[1] = p1x;
183                         ship.shp_desty[1] = p1y;
184                         ship.shp_destx[0] = p0x;
185                         ship.shp_desty[0] = p0y;
186         
187                         ship.shp_autonav &= ~(AN_STANDBY+AN_SAILDIR+AN_LOADING);
188                         ship.shp_autonav |= AN_AUTONAV;
189                   
190                         if (scuttling)
191                             ship.shp_autonav |= AN_SCUTTLE;
192                         break;  
193
194                 /* set cargo levels on the ship */
195         
196                 case 'l' : 
197                         /* convert player->argp[3] to an integer */
198                         if (player->argp[3]) 
199                           sub = atoi(player->argp[3]); 
200                         else { 
201                           sprintf(buf1,"Field (1-%d) ",TMAX);
202                           if (getstarg(player->argp[3],buf1, buf) == 0)
203                                 return RET_SYN;
204                           sub = atoi(buf);
205                         }
206                         /* check to make sure value in within range. */
207                         if (sub>TMAX || sub<1 ) {
208                          pr("Value must range from 1 to %d\n",TMAX);
209                                                     break;   }
210
211                         /* to keep sub in range of our arrays 
212                            subtract 1 so the new range is 0-(TMAX-1)
213                         */
214                            sub = sub - 1; ; 
215
216                            if (ship.shp_autonav & AN_AUTONAV ) {
217                                 orders=1;
218                                 dest = getstarg(player->argp[4],"Start or End? ", buf);
219                                 if (orders) { /* before dest check */
220                                    if (!dest)
221                                         break;
222                                 switch (*dest) {
223                                   default:
224                                      pr("You must enter 'start' or 'end'\n");
225                                      return RET_SYN; 
226                                   case 'e' :
227                                   case 'E' :
228                                      i1 = whatitem(player->argp[5],"Commodity? ");
229                                      if (!i1)
230                                         break;
231                                      else {
232                                         p1 = getstarg(player->argp[6],"Amount? ", buf);
233                                         if (!p1)
234                                                 return RET_SYN;
235                                         level = atoi(p1);
236                                         }
237                                      if (level < 0) { 
238                                         level = 0;  /* prevent negatives. */
239                                         pr("You must use positive number! Level set to 0.\n");
240                                      } 
241                                      ship.shp_tstart[sub] = (s_char)i1->i_mnem;
242                                      ship.shp_lstart[sub] = level;
243                                      pr("Order Set \n"); 
244                                      break; 
245                                   case 's' :
246                                   case 'S' :
247                                      i1 = whatitem(player->argp[5],"Commodity? ");
248                                      if (!i1)
249                                         break;
250                                      else {
251                                         p1 = getstarg(player->argp[6],"Amount? ", buf);
252                                         if (!p1)
253                                                 return RET_SYN;
254                                         level = atoi(p1);
255                                         }
256                                      if (level < 0 ) { 
257                                         level = 0;
258                                         pr("You must use positive number! Level set to 0.\n");
259                                      } 
260                                      ship.shp_tend[sub] = (s_char)i1->i_mnem;
261                                      ship.shp_lend[sub] = level;
262                                      pr("Order Set \n"); 
263                                      break;
264                                 }
265                                 }
266                            }
267                            else
268                               pr("You need to 'declare' a ship path first, see 'info order'\n");
269
270                            break;
271                 } /* end of switch (*p) */
272
273
274         
275                         /*
276                          *  Set loading flag if ship is already in one
277                          *  of the specified harbors and a cargo has been
278                          *  specified.
279                          */
280
281                         if ( ((ship.shp_x == ship.shp_destx[0])
282                                 && (ship.shp_y == ship.shp_desty[0])
283                                 && (ship.shp_lstart[0] != ' '))
284                             || ((ship.shp_x == ship.shp_desty[1])
285                                 && (ship.shp_y == ship.shp_desty[1])
286                                 && (ship.shp_lstart[1] != ' ')) ) {
287
288                                 coord tcord;
289                                 s_char tcomm[TMAX];
290                                 short  lev[TMAX];
291                                 int i;
292
293                                 ship.shp_autonav |= AN_LOADING;
294
295                             /*  swap variables, this keeps 
296                                 the load_it() procedure happy. CZ
297                              */
298                                 tcord = ship.shp_destx[0];
299                                 ship.shp_destx[0] = ship.shp_destx[1];
300                                 ship.shp_destx[1] = tcord;
301                                 tcord = ship.shp_desty[0];
302                                 ship.shp_desty[0] = ship.shp_desty[1];
303                                 ship.shp_desty[1] = tcord;
304
305                                 for(i=0;i<TMAX;i++) {
306                                    lev[i]   = ship.shp_lstart[i];
307                                    ship.shp_lstart[i] = ship.shp_lend[i];
308                                    ship.shp_lend[i]   = lev[i];
309                                    tcomm[i] = ship.shp_tstart[i];
310                                    ship.shp_tstart[i] = ship.shp_tend[i];
311                                    ship.shp_tend[i]   = tcomm[i];
312                                 }
313                         }
314                         /*
315                         **  Write ship back to database, then give it
316                         **  a kick down the autonav route if necessary.
317                         */
318
319
320                 /* Now do a sanity check. */
321                 if (!check_ship_ok(&start))
322                     return RET_SYN;
323                         
324                 putship(ship.shp_uid, &ship);
325                 nships++;
326         }
327         return RET_OK;
328 }
329
330 static void
331 eta_calc(struct shpstr *sp, s_char *path, int *len, int *nupdates)
332 {
333         extern int etu_per_update;
334         extern float ship_mob_scale;
335         struct mchrstr *mcp;
336         double mobcost, mobil;
337         int     i;
338
339         i = strlen(path);
340         *len = i;
341         *nupdates = 1;
342
343         mcp = &mchr[(int)sp->shp_type];
344         mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
345         mobcost = 480.0 / (mobcost + techfact(sp->shp_tech, mobcost));
346         mobil = sp->shp_mobil;
347         while (i) {
348                 if (mobil > 0) {
349                         mobil -= mobcost;
350                         i--;
351                 } else {
352                         mobil += (ship_mob_scale * (float)etu_per_update);
353                         (*nupdates)++;
354                 }
355         }
356 }
357
358 int
359 qorde(void)
360 {
361         int     nships=0;
362         int     i;
363         struct  nstr_item nb;
364         struct  shpstr ship;
365
366         if (!snxtitem(&nb, EF_SHIP, player->argp[1])) return RET_SYN;
367         while (nxtitem(&nb, (s_char *)(&ship))) {
368                 if (!player->owner || ship.shp_own==0)
369                     continue;
370                 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
371                         pr("bad ship type %d (#%d)\n",
372                                 ship.shp_type, nb.cur);
373                         continue;
374                 }
375
376                 if ((ship.shp_autonav & AN_AUTONAV) ||
377                     (ship.shp_path[0] && opt_SAIL)) {
378                         if (!nships) { /* 1st ship, print banner */
379                                 if (player->god) pr("own ");
380                                 pr("shp#     ship type    ");
381                                 pr("[Starting]       (Ending)    \n");
382                         }
383                         nships++;
384                         if (player->god) pr("%3d ", ship.shp_own);
385                         pr("%4d", nb.cur);
386                         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
387                 }
388                 if (ship.shp_autonav & AN_AUTONAV){
389
390                         pr(" [");
391                         for (i=0;i<TMAX;i++) {
392                           if(ship.shp_tend[i] != ' ' && 
393                                 ship.shp_lend[i] != 0 ) {
394                           pr("%d-",i+1);
395                           pr("%c",ship.shp_tend[i]);
396                           pr(":");
397                           pr("%d ",ship.shp_lend[i]);
398                           }
399                         }
400                         pr("] , (");
401                         for (i=0;i<TMAX;i++) {
402                           if(ship.shp_tstart[i] != ' ' &&   
403                              ship.shp_lstart[i] != 0 ) { 
404                           pr("%d-",i+1);
405                           pr("%c",ship.shp_tstart[i]);
406                           pr(":");
407                           pr("%d ",ship.shp_lstart[i]);
408                           }
409                         }
410                         pr(")");
411                         if (ship.shp_autonav & AN_SCUTTLE)
412                             pr(" scuttling");
413                         pr("\n");
414                 }
415
416                 if (opt_SHIPNAMES) {
417                     if ((ship.shp_autonav & AN_AUTONAV) ||
418                         (ship.shp_path[0] && opt_SAIL)) {
419                         if (ship.shp_name[0] != 0) {
420                             if (player->god)
421                                 pr("    ");
422                             pr("       %s\n",ship.shp_name);
423                         }
424                     }
425                 }
426         }
427         if (!nships) {
428                 if (player->argp[1])
429                         pr("%s: No ship(s)\n", player->argp[1]);
430                 else
431                         pr("%s: No ship(s)\n", "");
432                 return RET_FAIL;
433         } else
434                 pr("%d ship%s\n", nships, splur(nships));
435         return RET_OK;
436 }
437 /*  Chad Zabel 1-15-94
438  *  New command added to display autonav stats.
439  */ 
440
441 int
442 sorde(void)
443 {
444         int     nships=0;
445         int     len, updates;
446         s_char  *c;
447         struct  nstr_item nb;
448         struct  shpstr ship;
449         s_char  buf[1024];
450
451         if (!snxtitem(&nb, EF_SHIP, player->argp[1])) return RET_SYN;
452         while (nxtitem(&nb, (s_char *)(&ship))) {
453                 if (!player->owner || ship.shp_own==0)
454                     continue;
455                 if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
456                         pr("bad ship type %d (#%d)\n",
457                                 ship.shp_type, nb.cur);
458                         continue;
459                 }
460                 if ((ship.shp_autonav & AN_AUTONAV) ||
461                     (ship.shp_path[0] && opt_SAIL)) {
462                         if (!nships) { /* 1st ship, print banner */
463                                 if (player->god) pr("own ");
464                                 pr("shp#     ship type      x,y    ");
465                                 pr("start   end   ");
466                                 pr("len  eta\n");
467                         }
468                         nships++;
469                         if (player->god) pr("%3d ", ship.shp_own);
470                         pr("%4d", nb.cur);
471                         pr(" %-16.16s", mchr[(int)ship.shp_type].m_name);
472                         prxy(" %3d,%-3d", ship.shp_x, ship.shp_y, player->cnum);
473                 }
474                 if (ship.shp_autonav & AN_AUTONAV){
475                         /* Destination 1 */
476                         prxy(" %3d,%-3d", ship.shp_destx[1],
477                                        ship.shp_desty[1], player->cnum);
478
479                         /* Destination 2 */
480                         if (   (ship.shp_destx[1]!=ship.shp_destx[0])
481                             || (ship.shp_desty[1]!=ship.shp_desty[0]) ) {
482                                prxy(" %2d,%-2d",ship.shp_destx[0],
483                                         ship.shp_desty[0], player->cnum);
484                         } else pr("      ");
485
486                         if (ship.shp_autonav & AN_STANDBY) 
487                                 pr("  suspended");
488                         else if (ship.shp_autonav & AN_LOADING) 
489                                 pr("  loading");
490                         else {
491                                 /* ETA calculation */
492
493                                 c = BestShipPath(buf, ship.shp_x,
494                                         ship.shp_y, ship.shp_destx[0],
495                                         ship.shp_desty[0], ship.shp_own);
496                                 if (!c || !*c) 
497                                         pr("no route possible");
498                                 else if (*c == 'h') 
499                                         pr("has arrived");
500                                 else if (*c == '?') 
501                                         pr("route too long");
502                                 else {
503                                         /* distance to destination */
504                                         eta_calc(&ship, c, &len, &updates);
505                                         pr(" %3d %4d", len, updates);
506                                 }
507                         }
508                         if (ship.shp_autonav & AN_SCUTTLE)
509                             pr(" (scuttling)");
510                         pr("\n");
511                 }
512                 if (opt_SHIPNAMES) {
513                     if ((ship.shp_autonav & AN_AUTONAV) ||
514                         (ship.shp_path[0] && opt_SAIL)) {
515                         if (!(ship.shp_autonav & AN_AUTONAV))
516                             pr("\n");
517                         if (ship.shp_name[0] != 0) {
518                             if (player->god)
519                                 pr("    ");
520                             pr("       %s\n",ship.shp_name);
521                         }
522                     }
523                 }
524         }
525         if (!nships) {
526                 if (player->argp[1])
527                         pr("%s: No ship(s)\n", player->argp[1]);
528                 else
529                         pr("%s: No ship(s)\n", "");
530                 return RET_FAIL;
531         } else
532                 pr("%d ship%s\n", nships, splur(nships));
533         return RET_OK;
534 }
535
536