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