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