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