]> git.pond.sub.org Git - empserver/blob - src/lib/commands/retr.c
Change encoding of `not in any group' from space to 0, because that's
[empserver] / src / lib / commands / retr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  retr.c: Set retreat conditionals for ships
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "misc.h"
38 #include "player.h"
39 #include "retreat.h"
40 #include "ship.h"
41 #include "land.h"
42 #include "nsc.h"
43 #include "file.h"
44 #include "path.h"
45 #include "commands.h"
46
47 int
48 retr(void)
49 {
50     char *pq, *fl;
51     int nships;
52     struct nstr_item ni;
53     struct shpstr ship;
54     int isfleet = 0;
55     int rflags = -2;
56     int zero;
57     unsigned i;
58     char buf1[1024];
59     char buf2[1024];
60
61     if (!snxtitem(&ni, EF_SHIP, player->argp[1]))
62         return RET_SYN;
63     nships = 0;
64     if (player->argp[1] != NULL)
65         if (isalpha(player->argp[1][0]))
66             isfleet = RET_GROUP;
67     if (player->argp[2] != NULL)
68         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
69     else
70         pq = NULL;
71
72     if (pq != NULL) {
73         fl = getstarg(player->argp[3],
74                       "Retreat conditions [i|t|s|h|b|d|u|c]? ", buf2);
75         if (!fl)
76             return RET_SYN;
77         rflags = 0 | isfleet;
78
79         for (i = 0; i < strlen(fl); i++) {
80             switch (fl[i]) {
81             case 'I':
82             case 'i':
83                 rflags |= RET_INJURED;
84                 break;
85             case 'T':
86             case 't':
87                 rflags |= RET_TORPED;
88                 break;
89             case 'S':
90             case 's':
91                 rflags |= RET_SONARED;
92                 break;
93             case 'H':
94             case 'h':
95                 rflags |= RET_HELPLESS;
96                 break;
97             case 'B':
98             case 'b':
99                 rflags |= RET_BOMBED;
100                 break;
101             case 'D':
102             case 'd':
103                 rflags |= RET_DCHRGED;
104                 break;
105             case 'U':
106             case 'u':
107                 rflags |= RET_BOARDED;
108                 break;
109             case 'C':
110             case 'c':
111                 rflags = -1;
112                 break;
113             default:
114                 pr("bad condition\n");
115                 /* fall through */
116             case '?':
117                 pr("i\tretreat when injured\n");
118                 pr("t\tretreat when torped\n");
119                 pr("s\tretreat when sonared\n");
120                 pr("h\tretreat when helpless\n");
121                 pr("b\tretreat when bombed\n");
122                 pr("d\tretreat when depth-charged\n");
123                 pr("u\tretreat when boarded\n");
124             }
125         }
126         if (rflags == isfleet) {
127             pr("Must give retreat conditions!\n");
128             return RET_FAIL;
129         }
130     }
131
132     if (rflags == -1)
133         pq = NULL;
134
135     zero = (rflags == -1);
136     if (zero)
137         rflags = 0;
138
139     while (nxtitem(&ni, &ship)) {
140         if (!player->owner || ship.shp_own == 0)
141             continue;
142         if (zero)
143             memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
144
145         if (pq != NULL) {
146             strncpy(ship.shp_rpath, pq, sizeof(ship.shp_rpath) - 1);
147             putship(ship.shp_uid, &ship);
148         }
149         if (rflags >= 0) {
150             ship.shp_rflags = rflags;
151             putship(ship.shp_uid, &ship);
152         }
153         if (nships++ == 0) {
154             if (player->god)
155                 pr("own ");
156             pr("shp#     ship type       x,y   fl path       as flt? flags\n");
157         }
158         if (player->god)
159             pr("%3d ", ship.shp_own);
160         pr("%4d ", ni.cur);
161         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
162         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
163         pr("%1.1s", &ship.shp_fleet);
164         pr(" %-11s", ship.shp_rpath);
165         if (ship.shp_rflags & RET_GROUP)
166             pr("Yes     ");
167         else
168             pr("        ");
169         if (ship.shp_rflags & RET_INJURED)
170             pr("I");
171         if (ship.shp_rflags & RET_TORPED)
172             pr("T");
173         if (ship.shp_rflags & RET_SONARED)
174             pr("S");
175         if (ship.shp_rflags & RET_HELPLESS)
176             pr("H");
177         if (ship.shp_rflags & RET_BOMBED)
178             pr("B");
179         if (ship.shp_rflags & RET_DCHRGED)
180             pr("D");
181         if (ship.shp_rflags & RET_BOARDED)
182             pr("U");
183         pr("\n");
184     }
185     if (nships == 0) {
186         if (player->argp[1])
187             pr("%s: No ship(s)\n", player->argp[1]);
188         else
189             pr("%s: No ship(s)\n", "");
190         return RET_FAIL;
191     } else
192         pr("%d ship%s\n", nships, splur(nships));
193     return RET_OK;
194 }
195
196 int
197 lretr(void)
198 {
199     char *pq, *fl;
200     int nunits;
201     struct nstr_item ni;
202     struct lndstr land;
203     int isarmy = 0;
204     int rflags = -2;
205     int zero;
206     char buf1[1024];
207     char buf2[1024];
208     unsigned i;
209
210     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
211         return RET_SYN;
212     nunits = 0;
213     if (player->argp[1] != NULL)
214         if (isalpha(player->argp[1][0]))
215             isarmy = RET_GROUP;
216     if (player->argp[2] != NULL)
217         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
218     else
219         pq = NULL;
220     if (pq != NULL) {
221         fl = getstarg(player->argp[3], "Retreat conditions [i|h|b|c]? ",
222                       buf2);
223         if (!fl)
224             return RET_SYN;
225         rflags = 0 | isarmy;
226
227         for (i = 0; i < strlen(fl); i++) {
228             switch (fl[i]) {
229             case 'I':
230             case 'i':
231                 rflags |= RET_INJURED;
232                 break;
233             case 'H':
234             case 'h':
235                 rflags |= RET_HELPLESS;
236                 break;
237             case 'B':
238             case 'b':
239                 rflags |= RET_BOMBED;
240                 break;
241             case 'C':
242             case 'c':
243                 rflags = -1;
244                 break;
245             default:
246                 pr("bad condition\n");
247                 /* fall through */
248             case '?':
249                 pr("i\tretreat when injured\n");
250                 pr("h\tretreat when helpless\n");
251                 pr("b\tretreat when bombed\n");
252             }
253         }
254         if (rflags == isarmy) {
255             pr("Must give retreat conditions!\n");
256             return RET_FAIL;
257         }
258     }
259
260     if (rflags == -1)
261         pq = NULL;
262
263     zero = (rflags == -1);
264     if (zero)
265         rflags = 0;
266
267     while (nxtitem(&ni, &land)) {
268         if (!player->owner || land.lnd_own == 0)
269             continue;
270         if (zero)
271             memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
272
273         if (pq != NULL) {
274             strncpy(land.lnd_rpath, pq, sizeof(land.lnd_rpath) - 1);
275             putland(land.lnd_uid, &land);
276         }
277         if (rflags >= 0) {
278             land.lnd_rflags = rflags;
279             putland(land.lnd_uid, &land);
280         }
281
282         if (nunits++ == 0) {
283             if (player->god)
284                 pr("own ");
285             pr("lnd#     unit type       x,y   ar path       as army? flags\n");
286         }
287         if (player->god)
288             pr("%3d ", land.lnd_own);
289         pr("%4d ", ni.cur);
290         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
291         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
292         pr("%1.1s", &land.lnd_army);
293         pr(" %-11s", land.lnd_rpath);
294         if (land.lnd_rflags & RET_GROUP)
295             pr("Yes      ");
296         else
297             pr("         ");
298         if (land.lnd_rflags & RET_INJURED)
299             pr("I");
300         if (land.lnd_rflags & RET_TORPED)
301             pr("T");
302         if (land.lnd_rflags & RET_SONARED)
303             pr("S");
304         if (land.lnd_rflags & RET_HELPLESS)
305             pr("H");
306         if (land.lnd_rflags & RET_BOMBED)
307             pr("B");
308         if (land.lnd_rflags & RET_DCHRGED)
309             pr("D");
310         if (land.lnd_rflags & RET_BOARDED)
311             pr("U");
312         pr("\n");
313     }
314     if (nunits == 0) {
315         if (player->argp[1])
316             pr("%s: No unit(s)\n", player->argp[1]);
317         else
318             pr("%s: No unit(s)\n", "");
319         return RET_FAIL;
320     }
321     pr("%d unit%s\n", nunits, splur(nunits));
322     return RET_OK;
323 }