]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Update copyright notice
[empserver] / src / lib / commands / miss.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *     Markus Armbruster, 2005-2009
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "empobj.h"
40 #include "mission.h"
41 #include "optlist.h"
42 #include "path.h"
43
44 static int clear_mission(struct nstr_item *);
45 static int show_mission(struct nstr_item *);
46
47 /*
48  *  mission <type> <planes/ships/units> <mission type> <op sector> [<radius>]
49  */
50 int
51 mission(void)
52 {
53     static int ef_with_missions[] = { EF_SHIP, EF_LAND, EF_PLANE, EF_BAD };
54     char *p;
55     int type;
56     int mission;
57     coord x, y;
58     int radius, range;
59     union empobj_storage item;
60     struct empobj *gp;
61     int num = 0;
62     struct nstr_item ni;
63     char buf[1024];
64
65     p = getstarg(player->argp[1], "Ship, plane or land unit (p,sh,la)? ",
66                  buf);
67     if (!p)
68         return RET_SYN;
69     type = ef_byname_from(p, ef_with_missions);
70     if (type < 0) {
71         pr("Ships, land units or planes only! (s, l, p)\n");
72         return RET_SYN;
73     }
74     if (!snxtitem(&ni, type, player->argp[2], NULL))
75         return RET_SYN;
76
77     p = getstarg(player->argp[3],
78                  "Mission (int, sup, osup, dsup, esc, res, air, query, clear)? ",
79                  buf);
80     if (!p)
81         return RET_SYN;
82
83 /*
84  * 'i'     interdiction
85  * 's'     support
86  * 'o'     support attacks
87  * 'd'     support defenders
88  * 'e'     escort
89  * 'r'     defensive reserve
90  * 'a'     air defense (intercepts)
91  */
92     switch (*p) {
93     case 'I':
94     case 'i':
95         mission = MI_INTERDICT;
96         break;
97     case 'O':
98     case 'o':
99         mission = MI_OSUPPORT;
100         break;
101     case 'D':
102     case 'd':
103         mission = MI_DSUPPORT;
104         break;
105     case 'S':
106     case 's':
107         mission = MI_SUPPORT;
108         break;
109     case 'C':
110     case 'c':
111         return clear_mission(&ni);
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 (!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     p = getstarg(player->argp[4], "operations point? ", buf);
153     if (!p || !*p)
154         return RET_SYN;
155
156     if (*p != '.') {
157         if (!sarg_xy(p, &x, &y))
158             return RET_SYN;
159     }
160
161     if (player->argp[5] != NULL) {
162         radius = atoi(player->argp[5]);
163         if (radius < 0) {
164             pr("Radius can't be negative!\n");
165             return RET_FAIL;
166         }
167     } else
168         radius = 9999;
169
170     while (nxtitem(&ni, &item)) {
171         gp = (struct empobj *)&item;
172
173         if (!player->owner || gp->own == 0)
174             continue;
175
176         if (mission == MI_RESERVE) {
177             if (!lnd_can_attack((struct lndstr *)gp)) {
178                 pr("%s is not designed to fight ground troops\n",
179                    obj_nameof(gp));
180                 continue;
181             }
182             if (!lchr[gp->type].l_rad) {
183                 pr("%s cannot react anywhere!\n", obj_nameof(gp));
184                 continue;
185             }
186         }
187
188         if ((mission == MI_INTERDICT) && (type == EF_SHIP))
189             if (mchr[(int)gp->type].m_glim == 0) {
190                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
191                 continue;
192             }
193
194         if ((mission == MI_INTERDICT) && (type == EF_LAND))
195             if (lchr[(int)gp->type].l_dam == 0) {
196                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
197                 continue;
198             }
199
200         if ((mission == MI_INTERDICT) && (type == EF_PLANE)) {
201             struct plchrstr *pcp;
202
203             pcp = &plchr[(int)gp->type];
204             if (!(pcp->pl_flags & P_T)) {
205                 pr("Only planes with the tactical ability can interdict.\n"
206                    "%s #%d is ineligible\n",
207                    pcp->pl_name, gp->uid);
208                 continue;
209             }
210         }
211
212         if ((mission == MI_AIR_DEFENSE) && (type == EF_PLANE)) {
213             struct plchrstr *pcp;
214
215             pcp = &plchr[(int)gp->type];
216             if (!(pcp->pl_flags & P_F)) {
217                 pr("Only planes with the intercept abilities can perform air defense.\n"
218                    "%s #%d is ineligible\n",
219                    pcp->pl_name, gp->uid);
220                 continue;
221             }
222         }
223
224         if ((mission == MI_ESCORT) && (type == EF_PLANE)) {
225             struct plchrstr *pcp;
226
227             pcp = &plchr[(int)gp->type];
228             if (!(pcp->pl_flags & P_ESC) && !(pcp->pl_flags & P_F)) {
229                 pr("Only planes with the escort or intercept abilities can escort.\n"
230                    "%s #%d is ineligible\n",
231                    pcp->pl_name, gp->uid);
232                 continue;
233             }
234         }
235
236         if ((mission == MI_SUPPORT || mission == MI_OSUPPORT ||
237              mission == MI_DSUPPORT) && (type == EF_PLANE)) {
238             struct plchrstr *pcp;
239
240             pcp = &plchr[(int)gp->type];
241             if (!(pcp->pl_flags & P_T)) {
242                 pr("Only planes with the tactical ability can support.\n"
243                    "%s #%d is ineligible\n",
244                    pcp->pl_name, gp->uid);
245                 continue;
246             }
247         }
248
249         if (type == EF_PLANE && nuk_on_plane((struct plnstr *)gp) >= 0) {
250             pr("%s can't perform a mission while it carries a nuclear weapon",
251                obj_nameof(gp));
252             continue;
253         }
254
255         if (*p == '.') {
256             x = gp->x;
257             y = gp->y;
258         }
259
260         gp->mission = mission;
261         range = oprange(gp);
262         if (range < mapdist(gp->x, gp->y, x, y)) {
263             pr("%s: out of range! (range %d)\n",
264                obj_nameof(gp), range);
265             continue;
266         }
267         gp->opx = x;
268         gp->opy = y;
269         gp->radius = MIN(range, radius);
270         put_empobj(type, gp->uid, gp);
271         num++;
272
273         pr("%s on %s mission, centered on %s, radius %d\n",
274            obj_nameof(gp), mission_name(mission),
275            xyas(x, y, player->cnum), gp->radius);
276     }
277     if (num == 0) {
278         pr("No %s%s\n", ef_nameof(type), splur(num));
279         return RET_FAIL;
280     }
281     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
282     return RET_OK;
283 }
284
285 static int
286 clear_mission(struct nstr_item *np)
287 {
288     union empobj_storage item;
289
290     while (nxtitem(np, &item)) {
291         item.gen.mission = 0;
292         put_empobj(item.gen.ef_type, item.gen.uid, &item);
293     }
294
295     return RET_OK;
296 }
297
298 static int
299 show_mission(struct nstr_item *np)
300 {
301     int first = 1;
302     union empobj_storage item;
303     struct empobj *gp;
304
305     while (nxtitem(np, &item)) {
306         gp = (struct empobj *)&item;
307         if (!player->owner || gp->own == 0)
308             continue;
309
310         if (first) {
311             pr("Thing                         x,y   op-sect rad mission\n");
312             first = 0;
313         }
314         pr("%-25s", obj_nameof(gp));
315         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
316         switch (gp->mission) {
317         case MI_INTERDICT:
318         case MI_SUPPORT:
319         case MI_RESERVE:
320         case MI_ESCORT:
321         case MI_AIR_DEFENSE:
322         case MI_DSUPPORT:
323         case MI_OSUPPORT:
324             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
325             pr("  %4d", gp->radius);
326             break;
327         default:
328             CANT_REACH();
329             /* fall through */
330         case MI_NONE:
331             pr("              ");
332         }
333         if (gp->mission)
334             pr(" is on %s mission\n", mission_name(gp->mission));
335         else
336             pr(" has no mission.\n");
337     }
338
339     return RET_OK;
340 }