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