]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Update known contributors comments
[empserver] / src / lib / commands / miss.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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-2008
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     if ((p =
66          getstarg(player->argp[1], "Ship, plane or land unit (p,sh,la)? ",
67                   buf)) == 0)
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     if ((p =
78          getstarg(player->argp[3],
79                   "Mission (int, sup, osup, dsup, esc, res, air, query, clear)? ",
80                   buf)) == 0)
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     if ((p = getstarg(player->argp[4], "operations point? ", buf)) == 0
153         || *p == 0)
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 (*p == '.') {
250             x = gp->x;
251             y = gp->y;
252         }
253
254         gp->mission = mission;
255         range = oprange(gp);
256         if (range < mapdist(gp->x, gp->y, x, y)) {
257             pr("%s: out of range! (range %d)\n",
258                obj_nameof(gp), range);
259             continue;
260         }
261         gp->opx = x;
262         gp->opy = y;
263         gp->radius = MIN(range, radius);
264         put_empobj(type, gp->uid, gp);
265         num++;
266
267         pr("%s on %s mission, centered on %s, radius %d\n",
268            obj_nameof(gp), mission_name(mission),
269            xyas(x, y, player->cnum), gp->radius);
270     }
271     if (num == 0) {
272         pr("No %s%s\n", ef_nameof(type), splur(num));
273         return RET_FAIL;
274     }
275     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
276     return RET_OK;
277 }
278
279 static int
280 clear_mission(struct nstr_item *np)
281 {
282     union empobj_storage item;
283
284     while (nxtitem(np, &item)) {
285         item.gen.mission = 0;
286         put_empobj(item.gen.ef_type, item.gen.uid, &item);
287     }
288
289     return RET_OK;
290 }
291
292 static int
293 show_mission(struct nstr_item *np)
294 {
295     int first = 1;
296     union empobj_storage item;
297     struct empobj *gp;
298
299     while (nxtitem(np, &item)) {
300         gp = (struct empobj *)&item;
301         if (!player->owner || gp->own == 0)
302             continue;
303
304         if (first) {
305             pr("Thing                         x,y   op-sect rad mission\n");
306             first = 0;
307         }
308         pr("%-25s", obj_nameof(gp));
309         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
310         switch (gp->mission) {
311         case MI_INTERDICT:
312         case MI_SUPPORT:
313         case MI_RESERVE:
314         case MI_ESCORT:
315         case MI_AIR_DEFENSE:
316         case MI_DSUPPORT:
317         case MI_OSUPPORT:
318             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
319             pr("  %4d", gp->radius);
320             break;
321         default:
322             CANT_REACH();
323             /* fall through */
324         case MI_NONE:
325             pr("              ");
326         }
327         if (gp->mission)
328             pr(" is on %s mission\n", mission_name(gp->mission));
329         else
330             pr(" has no mission.\n");
331     }
332
333     return RET_OK;
334 }