]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Streamline signature of show_mission()
[empserver] / src / lib / commands / miss.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  miss.c: set missions for ships/planes/units
29  *
30  *  Known contributors to this file:
31  *     Thomas Ruschak, 1992
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "empobj.h"
39 #include "mission.h"
40 #include "optlist.h"
41 #include "path.h"
42
43 static int show_mission(struct nstr_item *);
44
45 /*
46  *  mission <type> <planes/ships/units> <mission type> <op sector> [<radius>]
47  */
48 int
49 mission(void)
50 {
51     static int ef_with_missions[] = { EF_SHIP, EF_LAND, EF_PLANE, EF_BAD };
52     char *p;
53     int type;
54     int mission;
55     coord x, y;
56     int desired_radius, radius;
57     struct sctstr opsect;
58     union empobj_storage item;
59     struct empobj *gp;
60     int num = 0, mobmax, mobused, dist;
61     struct nstr_item ni;
62     char buf[1024];
63
64     if ((p =
65          getstarg(player->argp[1], "Ship, plane or land unit (p,sh,la)? ",
66                   buf)) == 0)
67         return RET_SYN;
68     type = ef_byname_from(p, ef_with_missions);
69     if (type < 0) {
70         pr("Ships, land units or planes only! (s, l, p)\n");
71         return RET_SYN;
72     }
73     if (!snxtitem(&ni, type, player->argp[2], NULL))
74         return RET_SYN;
75
76     if ((p =
77          getstarg(player->argp[3],
78                   "Mission (int, sup, osup, dsup, esc, res, air, query, clear)? ",
79                   buf)) == 0)
80         return RET_SYN;
81
82 /*
83  * 'i'     interdiction
84  * 's'     support
85  * 'o'     support attacks
86  * 'd'     support defenders
87  * 'e'     escort
88  * 'r'     defensive reserve
89  * 'a'     air defense (intercepts)
90  */
91     switch (*p) {
92     case 'I':
93     case 'i':
94         mission = MI_INTERDICT;
95         break;
96     case 'O':
97     case 'o':
98         mission = MI_OSUPPORT;
99         break;
100     case 'D':
101     case 'd':
102         mission = MI_DSUPPORT;
103         break;
104     case 'S':
105     case 's':
106         mission = MI_SUPPORT;
107         break;
108     case 'C':
109     case 'c':
110         mission = 0;
111         break;
112     case 'E':
113     case 'e':
114         mission = MI_ESCORT;
115         break;
116     case 'R':
117     case 'r':
118         mission = MI_RESERVE;
119         break;
120     case 'A':
121     case 'a':
122         mission = MI_AIR_DEFENSE;
123         break;
124     case 'q':
125         return show_mission(&ni);
126     default:
127         pr("bad condition\n");
128         pr("i\tinterdiction (any)\n");
129         pr("s\tsupport (tactical planes only)\n");
130         pr("o\toffensive support (tactical planes only)\n");
131         pr("d\tdefensive support (tactical planes only)\n");
132         pr("r\treserve (land units only)\n");
133         pr("e\tescort (tactical or escort planes only)\n");
134         pr("a\tair defense (intercept planes only)\n");
135         pr("c\tclear mission\n");
136         pr("q\tquery\n");
137         return RET_SYN;
138     }
139
140     if (mission && !cando(mission, type)) {
141         pr("A %s cannot do that mission!\n", ef_nameof(type));
142         pr("i\tinterdiction (any)\n");
143         pr("s\tsupport (planes only)\n");
144         pr("o\toffensive support (planes only)\n");
145         pr("d\tdefensive support (planes only)\n");
146         pr("r\treserve (land units only)\n");
147         pr("e\tescort (planes only)\n");
148         pr("a\tair defense (planes only)\n");
149         return RET_FAIL;
150     }
151
152     if (mission) {
153         if ((p = getstarg(player->argp[4], "operations point? ", buf)) == 0
154             || *p == 0)
155             return RET_SYN;
156
157         if (*p != '.') {
158             if (!sarg_xy(p, &x, &y))
159                 return RET_SYN;
160
161             if (!getsect(x, y, &opsect))
162                 return RET_FAIL;
163         }
164     } else {
165         x = 0;
166         y = 0;
167     }
168
169     if (player->argp[5] != NULL) {
170         desired_radius = atoi(player->argp[5]);
171         if (desired_radius < 0) {
172             pr("Radius must be greater than zero!\n");
173             return RET_FAIL;
174         }
175     } else {
176         desired_radius = 9999;
177     }
178
179     if ((mobmax = get_empobj_mob_max(type)) == -1)
180         return RET_FAIL;
181
182     mobused = ldround(mission_mob_cost * (double)mobmax, 1);
183
184     while (nxtitem(&ni, &item)) {
185         gp = (struct empobj *)&item;
186
187         if (!player->owner || gp->own == 0)
188             continue;
189
190         if ((mission && (gp->mobil < mobused)) && mission_mob_cost) {
191             pr("%s: not enough mobility! (needs %d)\n",
192                obj_nameof(gp), mobused);
193             continue;
194         }
195         if (mission == MI_RESERVE && !lnd_can_attack((struct lndstr *)gp)) {
196             pr("%s is not designed to fight ground troops\n",
197                obj_nameof(gp));
198             continue;
199         }
200         if (*p == '.') {
201             x = gp->x;
202             y = gp->y;
203             if (!getsect(x, y, &opsect))
204                 return RET_FAIL;
205         }
206
207         dist = mapdist(gp->x, gp->y, x, y);
208         radius = 999;
209         if (mission) {
210             radius = oprange(gp, mission);
211             if (radius < dist) {
212                 pr("%s: out of range! (range %d)\n",
213                    obj_nameof(gp), radius);
214                 continue;
215             }
216         }
217
218         if (radius > desired_radius)
219             radius = desired_radius;
220
221         if ((mission == MI_INTERDICT) && (type == EF_SHIP))
222             if (mchr[(int)gp->type].m_glim == 0) {
223                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
224                 continue;
225             }
226
227         if ((mission == MI_INTERDICT) && (type == EF_LAND))
228             if (lchr[(int)gp->type].l_dam == 0) {
229                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
230                 continue;
231             }
232
233         if ((mission == MI_INTERDICT) && (type == EF_PLANE)) {
234             struct plchrstr *pcp;
235
236             pcp = &plchr[(int)gp->type];
237             if (!(pcp->pl_flags & P_T)) {
238                 pr("Only planes with the tactical ability can interdict.\n"
239                    "%s #%d is ineligible\n",
240                    pcp->pl_name, gp->uid);
241                 continue;
242             }
243         }
244
245         if ((mission == MI_AIR_DEFENSE) && (type == EF_PLANE)) {
246             struct plchrstr *pcp;
247
248             pcp = &plchr[(int)gp->type];
249             if (!(pcp->pl_flags & P_F)) {
250                 pr("Only planes with the intercept abilities can perform air defense.\n"
251                    "%s #%d is ineligible\n",
252                    pcp->pl_name, gp->uid);
253                 continue;
254             }
255         }
256
257         if ((mission == MI_ESCORT) && (type == EF_PLANE)) {
258             struct plchrstr *pcp;
259
260             pcp = &plchr[(int)gp->type];
261             if (!(pcp->pl_flags & P_ESC) && !(pcp->pl_flags & P_F)) {
262                 pr("Only planes with the escort or intercept abilities can escort.\n"
263                    "%s #%d is ineligible\n",
264                    pcp->pl_name, gp->uid);
265                 continue;
266             }
267         }
268
269         if ((mission == MI_SUPPORT || mission == MI_OSUPPORT ||
270              mission == MI_DSUPPORT) && (type == EF_PLANE)) {
271             struct plchrstr *pcp;
272
273             pcp = &plchr[(int)gp->type];
274             if (!(pcp->pl_flags & P_T)) {
275                 pr("Only planes with the tactical ability can support.\n"
276                    "%s #%d is ineligible\n",
277                    pcp->pl_name, gp->uid);
278                 continue;
279             }
280         }
281
282         num++;                  /* good one.. go with it */
283
284         if (mission) {
285             pr("%s on %s mission, centered on %s, radius %d\n",
286                obj_nameof(gp), mission_name(mission),
287                xyas(x, y, player->cnum), radius);
288             gp->mobil -= mobused;
289             gp->radius = radius;
290         } else
291             gp->radius = 0;
292
293         gp->mission = mission;
294         gp->opx = x;
295         gp->opy = y;
296         put_empobj(type, gp->uid, gp);
297     }
298     if (num == 0) {
299         pr("No %s%s\n", ef_nameof(type), splur(num));
300         return RET_FAIL;
301     }
302     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
303     return RET_OK;
304 }
305
306 static int
307 show_mission(struct nstr_item *np)
308 {
309     int first = 1;
310     union empobj_storage item;
311     struct empobj *gp;
312
313     while (nxtitem(np, &item)) {
314         gp = (struct empobj *)&item;
315         if (!player->owner || gp->own == 0)
316             continue;
317
318         if (first) {
319             pr("Thing                         x,y   op-sect rad mission\n");
320             first = 0;
321         }
322         pr("%-25s", obj_nameof(gp));
323         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
324         switch (gp->mission) {
325         case MI_INTERDICT:
326         case MI_SUPPORT:
327         case MI_RESERVE:
328         case MI_ESCORT:
329         case MI_AIR_DEFENSE:
330         case MI_DSUPPORT:
331         case MI_OSUPPORT:
332             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
333             pr("  %4d", gp->radius);
334             break;
335         default:
336             CANT_REACH();
337             /* fall through */
338         case MI_NONE:
339             pr("              ");
340         }
341         if (gp->mission)
342             pr(" is on %s mission\n", mission_name(gp->mission));
343         else
344             pr(" has no mission.\n");
345     }
346
347     return RET_OK;
348 }