]> git.pond.sub.org Git - empserver/blob - src/lib/commands/miss.c
Remove econfig key mission_mob_cost
[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 clear_mission(struct nstr_item *);
44 static int show_mission(struct nstr_item *);
45
46 /*
47  *  mission <type> <planes/ships/units> <mission type> <op sector> [<radius>]
48  */
49 int
50 mission(void)
51 {
52     static int ef_with_missions[] = { EF_SHIP, EF_LAND, EF_PLANE, EF_BAD };
53     char *p;
54     int type;
55     int mission;
56     coord x, y;
57     int radius, range;
58     union empobj_storage item;
59     struct empobj *gp;
60     int num = 0;
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         return clear_mission(&ni);
111     case 'E':
112     case 'e':
113         mission = MI_ESCORT;
114         break;
115     case 'R':
116     case 'r':
117         mission = MI_RESERVE;
118         break;
119     case 'A':
120     case 'a':
121         mission = MI_AIR_DEFENSE;
122         break;
123     case 'q':
124         return show_mission(&ni);
125     default:
126         pr("bad condition\n");
127         pr("i\tinterdiction (any)\n");
128         pr("s\tsupport (tactical planes only)\n");
129         pr("o\toffensive support (tactical planes only)\n");
130         pr("d\tdefensive support (tactical planes only)\n");
131         pr("r\treserve (land units only)\n");
132         pr("e\tescort (tactical or escort planes only)\n");
133         pr("a\tair defense (intercept planes only)\n");
134         pr("c\tclear mission\n");
135         pr("q\tquery\n");
136         return RET_SYN;
137     }
138
139     if (!cando(mission, type)) {
140         pr("A %s cannot do that mission!\n", ef_nameof(type));
141         pr("i\tinterdiction (any)\n");
142         pr("s\tsupport (planes only)\n");
143         pr("o\toffensive support (planes only)\n");
144         pr("d\tdefensive support (planes only)\n");
145         pr("r\treserve (land units only)\n");
146         pr("e\tescort (planes only)\n");
147         pr("a\tair defense (planes only)\n");
148         return RET_FAIL;
149     }
150
151     if ((p = getstarg(player->argp[4], "operations point? ", buf)) == 0
152         || *p == 0)
153         return RET_SYN;
154
155     if (*p != '.') {
156         if (!sarg_xy(p, &x, &y))
157             return RET_SYN;
158     }
159
160     if (player->argp[5] != NULL) {
161         radius = atoi(player->argp[5]);
162         if (radius < 0) {
163             pr("Radius can't be negative!\n");
164             return RET_FAIL;
165         }
166     } else
167         radius = 9999;
168
169     while (nxtitem(&ni, &item)) {
170         gp = (struct empobj *)&item;
171
172         if (!player->owner || gp->own == 0)
173             continue;
174
175         if (mission == MI_RESERVE && !lnd_can_attack((struct lndstr *)gp)) {
176             pr("%s is not designed to fight ground troops\n",
177                obj_nameof(gp));
178             continue;
179         }
180
181         if ((mission == MI_INTERDICT) && (type == EF_SHIP))
182             if (mchr[(int)gp->type].m_glim == 0) {
183                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
184                 continue;
185             }
186
187         if ((mission == MI_INTERDICT) && (type == EF_LAND))
188             if (lchr[(int)gp->type].l_dam == 0) {
189                 pr("%s: cannot fire at range!\n", obj_nameof(gp));
190                 continue;
191             }
192
193         if ((mission == MI_INTERDICT) && (type == EF_PLANE)) {
194             struct plchrstr *pcp;
195
196             pcp = &plchr[(int)gp->type];
197             if (!(pcp->pl_flags & P_T)) {
198                 pr("Only planes with the tactical ability can interdict.\n"
199                    "%s #%d is ineligible\n",
200                    pcp->pl_name, gp->uid);
201                 continue;
202             }
203         }
204
205         if ((mission == MI_AIR_DEFENSE) && (type == EF_PLANE)) {
206             struct plchrstr *pcp;
207
208             pcp = &plchr[(int)gp->type];
209             if (!(pcp->pl_flags & P_F)) {
210                 pr("Only planes with the intercept abilities can perform air defense.\n"
211                    "%s #%d is ineligible\n",
212                    pcp->pl_name, gp->uid);
213                 continue;
214             }
215         }
216
217         if ((mission == MI_ESCORT) && (type == EF_PLANE)) {
218             struct plchrstr *pcp;
219
220             pcp = &plchr[(int)gp->type];
221             if (!(pcp->pl_flags & P_ESC) && !(pcp->pl_flags & P_F)) {
222                 pr("Only planes with the escort or intercept abilities can escort.\n"
223                    "%s #%d is ineligible\n",
224                    pcp->pl_name, gp->uid);
225                 continue;
226             }
227         }
228
229         if ((mission == MI_SUPPORT || mission == MI_OSUPPORT ||
230              mission == MI_DSUPPORT) && (type == EF_PLANE)) {
231             struct plchrstr *pcp;
232
233             pcp = &plchr[(int)gp->type];
234             if (!(pcp->pl_flags & P_T)) {
235                 pr("Only planes with the tactical ability can support.\n"
236                    "%s #%d is ineligible\n",
237                    pcp->pl_name, gp->uid);
238                 continue;
239             }
240         }
241
242         if (*p == '.') {
243             x = gp->x;
244             y = gp->y;
245         }
246
247         gp->mission = mission;
248         range = oprange(gp);
249         if (range < mapdist(gp->x, gp->y, x, y)) {
250             pr("%s: out of range! (range %d)\n",
251                obj_nameof(gp), range);
252             continue;
253         }
254         gp->opx = x;
255         gp->opy = y;
256         gp->radius = MIN(range, radius);
257         put_empobj(type, gp->uid, gp);
258         num++;
259
260         pr("%s on %s mission, centered on %s, radius %d\n",
261            obj_nameof(gp), mission_name(mission),
262            xyas(x, y, player->cnum), gp->radius);
263     }
264     if (num == 0) {
265         pr("No %s%s\n", ef_nameof(type), splur(num));
266         return RET_FAIL;
267     }
268     pr("%d %s%s\n", num, ef_nameof(type), splur(num));
269     return RET_OK;
270 }
271
272 static int
273 clear_mission(struct nstr_item *np)
274 {
275     union empobj_storage item;
276
277     while (nxtitem(np, &item)) {
278         item.gen.mission = 0;
279         put_empobj(item.gen.ef_type, item.gen.uid, &item);
280     }
281
282     return RET_OK;
283 }
284
285 static int
286 show_mission(struct nstr_item *np)
287 {
288     int first = 1;
289     union empobj_storage item;
290     struct empobj *gp;
291
292     while (nxtitem(np, &item)) {
293         gp = (struct empobj *)&item;
294         if (!player->owner || gp->own == 0)
295             continue;
296
297         if (first) {
298             pr("Thing                         x,y   op-sect rad mission\n");
299             first = 0;
300         }
301         pr("%-25s", obj_nameof(gp));
302         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
303         switch (gp->mission) {
304         case MI_INTERDICT:
305         case MI_SUPPORT:
306         case MI_RESERVE:
307         case MI_ESCORT:
308         case MI_AIR_DEFENSE:
309         case MI_DSUPPORT:
310         case MI_OSUPPORT:
311             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
312             pr("  %4d", gp->radius);
313             break;
314         default:
315             CANT_REACH();
316             /* fall through */
317         case MI_NONE:
318             pr("              ");
319         }
320         if (gp->mission)
321             pr(" is on %s mission\n", mission_name(gp->mission));
322         else
323             pr(" has no mission.\n");
324     }
325
326     return RET_OK;
327 }