]> git.pond.sub.org Git - empserver/blob - src/lib/commands/sdump.c
Use gcc function attribute `format' to catch bad format strings. Fix
[empserver] / src / lib / commands / sdump.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  *  sdump.c: Dump ship information
29  * 
30  *  Known contributors to this file:
31  *     John Yockey, 1997
32  *     Steve McClure, 1998
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "xy.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "nat.h"
42 #include "nsc.h"
43 #include "file.h"
44 #include "optlist.h"
45 #include "commands.h"
46
47 int
48 sdump(void)
49 {
50     int nships;
51     struct nstr_item ni;
52     struct shpstr ship;
53     int field[128];
54     struct natstr *np;
55     int n, i;
56     time_t now;
57
58     if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
59         return RET_SYN;
60     prdate();
61
62     if (!player->argp[2]) {
63         for (n = 1; n <= 34; n++)
64             field[n - 1] = n;
65         field[n - 1] = 0;
66     } else {
67         n = 2;
68         i = 0;
69         while (player->argp[n]) {
70             if (!strcmp("type", player->argp[n])) {
71                 field[i++] = 1;
72             } else if (!strcmp("x", player->argp[n])) {
73                 field[i++] = 2;
74             } else if (!strcmp("y", player->argp[n])) {
75                 field[i++] = 3;
76             } else if (!strcmp("flt", player->argp[n])) {
77                 field[i++] = 4;
78             } else if (!strcmp("eff", player->argp[n])) {
79                 field[i++] = 5;
80             } else if (!strcmp("civ", player->argp[n])) {
81                 field[i++] = 6;
82             } else if (!strcmp("mil", player->argp[n])) {
83                 field[i++] = 7;
84             } else if (!strcmp("uw", player->argp[n])) {
85                 field[i++] = 8;
86             } else if (!strcmp("food", player->argp[n])) {
87                 field[i++] = 9;
88             } else if (!strcmp("pln", player->argp[n])) {
89                 field[i++] = 10;
90             } else if (!strcmp("he", player->argp[n])) {
91                 field[i++] = 11;
92             } else if (!strcmp("xl", player->argp[n])) {
93                 field[i++] = 12;
94             } else if (!strcmp("land", player->argp[n])) {
95                 field[i++] = 13;
96             } else if (!strcmp("mob", player->argp[n])) {
97                 field[i++] = 14;
98             } else if (!strcmp("fuel", player->argp[n])) {
99                 field[i++] = 15;
100             } else if (!strcmp("tech", player->argp[n])) {
101                 field[i++] = 16;
102             } else if (!strcmp("shell", player->argp[n])) {
103                 field[i++] = 17;
104             } else if (!strcmp("gun", player->argp[n])) {
105                 field[i++] = 18;
106             } else if (!strcmp("petrol", player->argp[n])) {
107                 field[i++] = 19;
108             } else if (!strcmp("iron", player->argp[n])) {
109                 field[i++] = 20;
110             } else if (!strcmp("dust", player->argp[n])) {
111                 field[i++] = 21;
112             } else if (!strcmp("bar", player->argp[n])) {
113                 field[i++] = 22;
114             } else if (!strcmp("oil", player->argp[n])) {
115                 field[i++] = 23;
116             } else if (!strcmp("lcm", player->argp[n])) {
117                 field[i++] = 24;
118             } else if (!strcmp("hcm", player->argp[n])) {
119                 field[i++] = 25;
120             } else if (!strcmp("rad", player->argp[n])) {
121                 field[i++] = 26;
122             } else if (!strcmp("def", player->argp[n])) {
123                 field[i++] = 27;
124             } else if (!strcmp("spd", player->argp[n])) {
125                 field[i++] = 28;
126             } else if (!strcmp("vis", player->argp[n])) {
127                 field[i++] = 29;
128             } else if (!strcmp("rng", player->argp[n])) {
129                 field[i++] = 30;
130             } else if (!strcmp("fir", player->argp[n])) {
131                 field[i++] = 31;
132             } else if (!strcmp("origx", player->argp[n])) {
133                 field[i++] = 32;
134             } else if (!strcmp("origy", player->argp[n])) {
135                 field[i++] = 33;
136             } else if (!strcmp("name", player->argp[n])) {
137                 field[i++] = 34;
138             } else {
139                 pr("Unrecognized field %s\n", player->argp[n]);
140             }
141             if (n++ > 100) {
142                 pr("Too many fields\n");
143                 return RET_FAIL;
144             }
145         }
146         field[i] = 0;
147     }
148
149     if (player->god)
150         pr("   ");
151     time(&now);
152     pr("DUMP SHIPS %ld\n", (long)now);
153     if (player->god)
154         pr("own ");
155     pr("id");
156     n = 0;
157     while (field[n]) {
158         switch (field[n]) {
159         case 1:
160             pr(" type");
161             break;
162         case 2:
163             pr(" x");
164             break;
165         case 3:
166             pr(" y");
167             break;
168         case 4:
169             pr(" flt");
170             break;
171         case 5:
172             pr(" eff");
173             break;
174         case 6:
175             pr(" civ");
176             break;
177         case 7:
178             pr(" mil");
179             break;
180         case 8:
181             pr(" uw");
182             break;
183         case 9:
184             pr(" food");
185             break;
186         case 10:
187             pr(" pln");
188             break;
189         case 11:
190             pr(" he");
191             break;
192         case 12:
193             pr(" xl");
194             break;
195         case 13:
196             pr(" land");
197             break;
198         case 14:
199             pr(" mob");
200             break;
201         case 15:
202             pr(" fuel");
203             break;
204         case 16:
205             pr(" tech");
206             break;
207         case 17:
208             pr(" shell");
209             break;
210         case 18:
211             pr(" gun");
212             break;
213         case 19:
214             pr(" petrol");
215             break;
216         case 20:
217             pr(" iron");
218             break;
219         case 21:
220             pr(" dust");
221             break;
222         case 22:
223             pr(" bar");
224             break;
225         case 23:
226             pr(" oil");
227             break;
228         case 24:
229             pr(" lcm");
230             break;
231         case 25:
232             pr(" hcm");
233             break;
234         case 26:
235             pr(" rad");
236             break;
237         case 27:
238             pr(" def");
239             break;
240         case 28:
241             pr(" spd");
242             break;
243         case 29:
244             pr(" vis");
245             break;
246         case 30:
247             pr(" rng");
248             break;
249         case 31:
250             pr(" fir");
251             break;
252         case 32:
253             pr(" origx");
254             break;
255         case 33:
256             pr(" origy");
257             break;
258         case 34:
259             pr(" name");
260             break;
261         }
262         n++;
263     }
264     pr("\n");
265
266     nships = 0;
267     np = getnatp(player->cnum);
268     while (nxtitem(&ni, (s_char *)&ship)) {
269         if (!player->owner || ship.shp_own == 0)
270             continue;
271         if (ship.shp_type < 0 || ship.shp_type > shp_maxno) {
272             pr("bad ship type %d (#%d)\n", ship.shp_type, ni.cur);
273             continue;
274         }
275         count_planes(&ship);
276         count_units(&ship);
277         nships++;
278         if (player->god)
279             pr("%3d ", ship.shp_own);
280         pr("%d", ni.cur);
281         n = 0;
282         while (field[n]) {
283             switch (field[n++]) {
284             case 1:
285                 pr(" %.4s", mchr[(int)ship.shp_type].m_name);
286                 break;
287             case 2:
288                 pr(" %d", xrel(np, ship.shp_x));
289                 break;
290             case 3:
291                 pr(" %d", yrel(np, ship.shp_y));
292                 break;
293             case 4:
294                 if (ship.shp_fleet == ' ')
295                     pr(" ~");
296                 else
297                     pr(" %c", ship.shp_fleet);
298                 break;
299             case 5:
300                 pr(" %d", ship.shp_effic);
301                 break;
302             case 6:
303                 pr(" %d", ship.shp_item[I_CIVIL]);
304                 break;
305             case 7:
306                 pr(" %d", ship.shp_item[I_MILIT]);
307                 break;
308             case 8:
309                 pr(" %d", ship.shp_item[I_UW]);
310                 break;
311             case 9:
312                 pr(" %d", ship.shp_item[I_FOOD]);
313                 break;
314             case 10:
315                 pr(" %d", ship.shp_nplane);
316                 break;
317             case 11:
318                 pr(" %d", ship.shp_nchoppers);
319                 break;
320             case 12:
321                 pr(" %d", ship.shp_nxlight);
322                 break;
323             case 13:
324                 pr(" %d", ship.shp_nland);
325                 break;
326             case 14:
327                 pr(" %d", ship.shp_mobil);
328                 break;
329             case 15:
330                 if (opt_FUEL)
331                     pr(" %d", ship.shp_fuel);
332                 else
333                     pr(" 0");
334                 break;
335             case 16:
336                 pr(" %d", ship.shp_tech);
337                 break;
338             case 17:
339                 pr(" %d", ship.shp_item[I_SHELL]);
340                 break;
341             case 18:
342                 pr(" %d", ship.shp_item[I_GUN]);
343                 break;
344             case 19:
345                 pr(" %d", ship.shp_item[I_PETROL]);
346                 break;
347             case 20:
348                 pr(" %d", ship.shp_item[I_IRON]);
349                 break;
350             case 21:
351                 pr(" %d", ship.shp_item[I_DUST]);
352                 break;
353             case 22:
354                 pr(" %d", ship.shp_item[I_BAR]);
355                 break;
356             case 23:
357                 pr(" %d", ship.shp_item[I_OIL]);
358                 break;
359             case 24:
360                 pr(" %d", ship.shp_item[I_LCM]);
361                 break;
362             case 25:
363                 pr(" %d", ship.shp_item[I_HCM]);
364                 break;
365             case 26:
366                 pr(" %d", ship.shp_item[I_RAD]);
367                 break;
368             case 27:
369                 pr(" %d", ship.shp_armor);
370                 break;
371             case 28:
372                 pr(" %d", ship.shp_speed);
373                 break;
374             case 29:
375                 pr(" %d", ship.shp_visib);
376                 break;
377             case 30:
378                 pr(" %d", ship.shp_frnge);
379                 break;
380             case 31:
381                 pr(" %d", ship.shp_glim);
382                 break;
383             case 32:
384                 /*
385                  * Disclosing construction site allows pirates to find
386                  * harbors.  Disclose it only to the original owner
387                  * and to deities.  Else dump illegal coordinates
388                  * 1,0
389                  */
390                 if (ship.shp_own == ship.shp_orig_own || player->god)
391                     pr(" %d", xrel(np, ship.shp_orig_x));
392                 else
393                     pr(" 1");
394                 break;
395             case 33:
396                 /* see case 32 */
397                 if (ship.shp_own == ship.shp_orig_own || player->god)
398                     pr(" %d", yrel(np, ship.shp_orig_y));
399                 else
400                     pr(" 0");
401                 break;
402             case 34:
403                 pr(" %c%s%c", '"', ship.shp_name, '"');
404                 break;
405             }
406         }
407         pr("\n");
408     }
409     if (nships == 0) {
410         if (player->argp[1])
411             pr("%s: No ship(s)\n", player->argp[1]);
412         else
413             pr("%s: No ship(s)\n", "");
414         return RET_FAIL;
415     } else
416         pr("%d ship%s\n", nships, splur(nships));
417     return RET_OK;
418 }