]> git.pond.sub.org Git - empserver/blob - src/lib/commands/retr.c
8e6b2184cab61441804378f065c7cc0277303b88
[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 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  *  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     s_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 int x;
58     s_char buf1[1024];
59     s_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 = (s_char *)0;
71
72     if (pq != (s_char *)0) {
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 (x = 0; x < strlen(fl); x++)
80             switch (*(fl + x)) {
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         if (rflags == isfleet) {
126             pr("Must give retreat conditions!\n");
127             return RET_FAIL;
128         }
129     }
130
131     if (rflags == -1)
132         pq = (s_char *)0;
133
134     zero = (rflags == -1);
135     if (zero)
136         rflags = 0;
137
138     while (nxtitem(&ni, &ship)) {
139         if (!player->owner || ship.shp_own == 0)
140             continue;
141         if (zero)
142             memset(ship.shp_rpath, 0, sizeof(ship.shp_rpath));
143
144         if (pq != (s_char *)0) {
145             strncpy(ship.shp_rpath, pq, sizeof(ship.shp_rpath));
146             putship(ship.shp_uid, &ship);
147         }
148         if (rflags >= 0) {
149             ship.shp_rflags = rflags;
150             putship(ship.shp_uid, &ship);
151         }
152         if (nships++ == 0) {
153             if (player->god)
154                 pr("own ");
155             pr("shp#     ship type       x,y   fl path       as flt? flags\n");
156         }
157         if (player->god)
158             pr("%3d ", ship.shp_own);
159         pr("%4d ", ni.cur);
160         pr("%-16.16s ", mchr[(int)ship.shp_type].m_name);
161         prxy("%4d,%-4d ", ship.shp_x, ship.shp_y, player->cnum);
162         pr("%c", ship.shp_fleet);
163         pr(" %-11s", ship.shp_rpath);
164         if (ship.shp_rflags & RET_GROUP)
165             pr("Yes     ");
166         else
167             pr("        ");
168         if (ship.shp_rflags & RET_INJURED)
169             pr("I");
170         if (ship.shp_rflags & RET_TORPED)
171             pr("T");
172         if (ship.shp_rflags & RET_SONARED)
173             pr("S");
174         if (ship.shp_rflags & RET_HELPLESS)
175             pr("H");
176         if (ship.shp_rflags & RET_BOMBED)
177             pr("B");
178         if (ship.shp_rflags & RET_DCHRGED)
179             pr("D");
180         if (ship.shp_rflags & RET_BOARDED)
181             pr("U");
182         pr("\n");
183     }
184     if (nships == 0) {
185         if (player->argp[1])
186             pr("%s: No ship(s)\n", player->argp[1]);
187         else
188             pr("%s: No ship(s)\n", "");
189         return RET_FAIL;
190     } else
191         pr("%d ship%s\n", nships, splur(nships));
192     return RET_OK;
193 }
194
195 int
196 lretr(void)
197 {
198     s_char *pq, *fl;
199     int nunits;
200     struct nstr_item ni;
201     struct lndstr land;
202     int isarmy = 0;
203     int rflags = -2;
204     int zero;
205     s_char buf1[1024];
206     s_char buf2[1024];
207     unsigned int x;
208
209     if (!snxtitem(&ni, EF_LAND, player->argp[1]))
210         return RET_SYN;
211     nunits = 0;
212     if (player->argp[1] != NULL)
213         if (isalpha(player->argp[1][0]))
214             isarmy = RET_GROUP;
215     if (player->argp[2] != NULL)
216         pq = getstarg(player->argp[2], "Retreat path? ", buf1);
217     else
218         pq = (s_char *)0;
219     if (pq != (s_char *)0) {
220         fl = getstarg(player->argp[3], "Retreat conditions [i|h|b|c]? ",
221                       buf2);
222         if (!fl)
223             return RET_SYN;
224         rflags = 0 | isarmy;
225
226         for (x = 0; x < strlen(fl); x++)
227             switch (*(fl + x)) {
228             case 'I':
229             case 'i':
230                 rflags |= RET_INJURED;
231                 break;
232             case 'H':
233             case 'h':
234                 rflags |= RET_HELPLESS;
235                 break;
236             case 'B':
237             case 'b':
238                 rflags |= RET_BOMBED;
239                 break;
240             case 'C':
241             case 'c':
242                 rflags = -1;
243                 break;
244             default:
245                 pr("bad condition\n");
246                 /* fall through */
247             case '?':
248                 pr("i\tretreat when injured\n");
249                 pr("h\tretreat when helpless\n");
250                 pr("b\tretreat when bombed\n");
251             }
252         if (rflags == isarmy) {
253             pr("Must give retreat conditions!\n");
254             return RET_FAIL;
255         }
256     }
257
258     if (rflags == -1)
259         pq = (s_char *)0;
260
261     zero = (rflags == -1);
262     if (zero)
263         rflags = 0;
264
265     while (nxtitem(&ni, &land)) {
266         if (!player->owner || land.lnd_own == 0)
267             continue;
268         if (zero)
269             memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
270
271         if (pq != (s_char *)0) {
272             strncpy(land.lnd_rpath, pq, sizeof(land.lnd_rpath));
273             putland(land.lnd_uid, &land);
274         }
275         if (rflags >= 0) {
276             land.lnd_rflags = rflags;
277             putland(land.lnd_uid, &land);
278         }
279
280         if (nunits++ == 0) {
281             if (player->god)
282                 pr("own ");
283             pr("lnd#     unit type       x,y   ar path       as army? flags\n");
284         }
285         if (player->god)
286             pr("%3d ", land.lnd_own);
287         pr("%4d ", ni.cur);
288         pr("%-16.16s ", lchr[(int)land.lnd_type].l_name);
289         prxy("%4d,%-4d ", land.lnd_x, land.lnd_y, player->cnum);
290         pr("%c", land.lnd_army);
291         pr(" %-11s", land.lnd_rpath);
292         if (land.lnd_rflags & RET_GROUP)
293             pr("Yes      ");
294         else
295             pr("         ");
296         if (land.lnd_rflags & RET_INJURED)
297             pr("I");
298         if (land.lnd_rflags & RET_TORPED)
299             pr("T");
300         if (land.lnd_rflags & RET_SONARED)
301             pr("S");
302         if (land.lnd_rflags & RET_HELPLESS)
303             pr("H");
304         if (land.lnd_rflags & RET_BOMBED)
305             pr("B");
306         if (land.lnd_rflags & RET_DCHRGED)
307             pr("D");
308         if (land.lnd_rflags & RET_BOARDED)
309             pr("U");
310         pr("\n");
311     }
312     if (nunits == 0) {
313         if (player->argp[1])
314             pr("%s: No unit(s)\n", player->argp[1]);
315         else
316             pr("%s: No unit(s)\n", "");
317         return RET_FAIL;
318     }
319     pr("%d unit%s\n", nunits, splur(nunits));
320     return RET_OK;
321 }