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